org.el 878 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209
  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. (when (fboundp 'defvaralias)
  94. (unless (boundp 'calendar-view-holidays-initially-flag)
  95. (defvaralias 'calendar-view-holidays-initially-flag
  96. 'view-calendar-holidays-initially))
  97. (unless (boundp 'calendar-view-diary-initially-flag)
  98. (defvaralias 'calendar-view-diary-initially-flag
  99. 'view-diary-entries-initially))
  100. (unless (boundp 'diary-fancy-buffer)
  101. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  102. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  103. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  104. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  105. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  106. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  107. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  108. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  109. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  110. (declare-function orgtbl-mode "org-table" (&optional arg))
  111. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  112. (declare-function org-beamer-mode "ox-beamer" ())
  113. (declare-function org-table-edit-field "org-table" (arg))
  114. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (declare-function org-element--parse-objects "org-element"
  124. (beg end acc restriction))
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-contents "org-element" (element))
  127. (declare-function org-element-context "org-element" (&optional element))
  128. (declare-function org-element-interpret-data "org-element"
  129. (data &optional parent))
  130. (declare-function org-element-map "org-element"
  131. (data types fun &optional info first-match no-recursion))
  132. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  133. (declare-function org-element-parse-buffer "org-element"
  134. (&optional granularity visible-only))
  135. (declare-function org-element-property "org-element" (property element))
  136. (declare-function org-element-put-property "org-element"
  137. (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element--parse-objects "org-element"
  140. (beg end acc restriction))
  141. (declare-function org-element-parse-buffer "org-element"
  142. (&optional granularity visible-only))
  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-mew org-mhe org-rmail org-vm org-w3m org-wl)
  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. :type
  313. '(set :greedy t
  314. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  315. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  316. (const :tag " crypt: Encryption of subtrees" org-crypt)
  317. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  318. (const :tag " docview: Links to doc-view buffers" org-docview)
  319. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  320. (const :tag " id: Global IDs for identifying entries" org-id)
  321. (const :tag " info: Links to Info nodes" org-info)
  322. (const :tag " habit: Track your consistency with habits" org-habit)
  323. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  324. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  325. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  326. (const :tag " mew Links to Mew folders/messages" org-mew)
  327. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  328. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  329. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  330. (const :tag " vm: Links to VM folders/messages" org-vm)
  331. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  332. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  333. (const :tag " mouse: Additional mouse support" org-mouse)
  334. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  335. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  336. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  337. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  338. (const :tag "C collector: Collect properties into tables" org-collector)
  339. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  340. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  341. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  342. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  343. (const :tag "C eval: Include command output as text" org-eval)
  344. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  345. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  346. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  347. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  348. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  349. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  350. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  351. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  352. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  353. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  354. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  355. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  356. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  357. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  358. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  359. (const :tag "C registry: A registry for Org-mode links" org-registry)
  360. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  361. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  362. (const :tag "C secretary: Team management with org-mode" org-secretary)
  363. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  364. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  365. (const :tag "C track: Keep up with Org-mode development" org-track)
  366. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  367. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  368. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  369. (defvar org-export-registered-backends) ; From ox.el
  370. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  371. (defcustom org-export-backends '(ascii html icalendar latex)
  372. "List of export back-ends that should be always available.
  373. If a description starts with <C>, the file is not part of Emacs
  374. and loading it will require that you have downloaded and properly
  375. installed the Org mode distribution.
  376. Unlike to `org-modules', libraries in this list will not be
  377. loaded along with Org, but only once the export framework is
  378. needed.
  379. This variable needs to be set before org.el is loaded. If you
  380. need to make a change while Emacs is running, use the customize
  381. interface or run the following code, where VALUE stands for the
  382. new value of the variable, after updating it:
  383. \(progn
  384. \(setq org-export-registered-backends
  385. \(org-remove-if-not
  386. \(lambda (backend)
  387. \(or (memq backend val)
  388. \(catch 'parentp
  389. \(mapc
  390. \(lambda (b)
  391. \(and (org-export-derived-backend-p b (car backend))
  392. \(throw 'parentp t)))
  393. val)
  394. nil)))
  395. org-export-registered-backends))
  396. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  397. \(dolist (backend val)
  398. \(cond
  399. \((not (load (format \"ox-%s\" backend) t t))
  400. \(message \"Problems while trying to load export back-end `%s'\"
  401. backend))
  402. \((not (memq backend new-list)) (push backend new-list))))
  403. \(set-default var new-list)))
  404. Adding a back-end to this list will also pull the back-end it
  405. depends on, if any."
  406. :group 'org
  407. :group 'org-export
  408. :set (lambda (var val)
  409. (if (not (featurep 'ox)) (set-default var val)
  410. ;; Any back-end not required anymore (not present in VAL and not
  411. ;; a parent of any back-end in the new value) is removed from the
  412. ;; list of registered back-ends.
  413. (setq org-export-registered-backends
  414. (org-remove-if-not
  415. (lambda (backend)
  416. (or (memq backend val)
  417. (catch 'parentp
  418. (mapc
  419. (lambda (b)
  420. (and (org-export-derived-backend-p b (car backend))
  421. (throw 'parentp t)))
  422. val)
  423. nil)))
  424. org-export-registered-backends))
  425. ;; Now build NEW-LIST of both new back-ends and required
  426. ;; parents.
  427. (let ((new-list (mapcar 'car org-export-registered-backends)))
  428. (dolist (backend val)
  429. (cond
  430. ((not (load (format "ox-%s" backend) t t))
  431. (message "Problems while trying to load export back-end `%s'"
  432. backend))
  433. ((not (memq backend new-list)) (push backend new-list))))
  434. ;; Set VAR to that list with fixed dependencies.
  435. (set-default var new-list))))
  436. :type '(set :greedy t
  437. (const :tag " ascii Export buffer to ASCII format" ascii)
  438. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  439. (const :tag " html Export buffer to HTML format" html)
  440. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  441. (const :tag " latex Export buffer to LaTeX format" latex)
  442. (const :tag " man Export buffer to MAN format" man)
  443. (const :tag " md Export buffer to Markdown format" md)
  444. (const :tag " odt Export buffer to ODT format" odt)
  445. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  446. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  447. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  448. (const :tag "C groff Export buffer to Groff format" groff)
  449. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  450. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  451. (eval-after-load 'ox
  452. '(mapc
  453. (lambda (backend)
  454. (condition-case nil (require (intern (format "ox-%s" backend)))
  455. (error (message "Problems while trying to load export back-end `%s'"
  456. backend))))
  457. org-export-backends))
  458. (defcustom org-support-shift-select nil
  459. "Non-nil means make shift-cursor commands select text when possible.
  460. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  461. start selecting a region, or enlarge regions started in this way.
  462. In Org-mode, in special contexts, these same keys are used for
  463. other purposes, important enough to compete with shift selection.
  464. Org tries to balance these needs by supporting `shift-select-mode'
  465. outside these special contexts, under control of this variable.
  466. The default of this variable is nil, to avoid confusing behavior. Shifted
  467. cursor keys will then execute Org commands in the following contexts:
  468. - on a headline, changing TODO state (left/right) and priority (up/down)
  469. - on a time stamp, changing the time
  470. - in a plain list item, changing the bullet type
  471. - in a property definition line, switching between allowed values
  472. - in the BEGIN line of a clock table (changing the time block).
  473. Outside these contexts, the commands will throw an error.
  474. When this variable is t and the cursor is not in a special
  475. context, Org-mode will support shift-selection for making and
  476. enlarging regions. To make this more effective, the bullet
  477. cycling will no longer happen anywhere in an item line, but only
  478. if the cursor is exactly on the bullet.
  479. If you set this variable to the symbol `always', then the keys
  480. will not be special in headlines, property lines, and item lines,
  481. to make shift selection work there as well. If this is what you
  482. want, you can use the following alternative commands: `C-c C-t'
  483. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  484. can be used to switch TODO sets, `C-c -' to cycle item bullet
  485. types, and properties can be edited by hand or in column view.
  486. However, when the cursor is on a timestamp, shift-cursor commands
  487. will still edit the time stamp - this is just too good to give up.
  488. XEmacs user should have this variable set to nil, because
  489. `shift-select-mode' is in Emacs 23 or later only."
  490. :group 'org
  491. :type '(choice
  492. (const :tag "Never" nil)
  493. (const :tag "When outside special context" t)
  494. (const :tag "Everywhere except timestamps" always)))
  495. (defcustom org-loop-over-headlines-in-active-region nil
  496. "Shall some commands act upon headlines in the active region?
  497. When set to `t', some commands will be performed in all headlines
  498. within the active region.
  499. When set to `start-level', some commands will be performed in all
  500. headlines within the active region, provided that these headlines
  501. are of the same level than the first one.
  502. When set to a string, those commands will be performed on the
  503. matching headlines within the active region. Such string must be
  504. a tags/property/todo match as it is used in the agenda tags view.
  505. The list of commands is: `org-schedule', `org-deadline',
  506. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  507. `org-archive-to-archive-sibling'. The archiving commands skip
  508. already archived entries."
  509. :type '(choice (const :tag "Don't loop" nil)
  510. (const :tag "All headlines in active region" t)
  511. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  512. (string :tag "Tags/Property/Todo matcher"))
  513. :version "24.1"
  514. :group 'org-todo
  515. :group 'org-archive)
  516. (defgroup org-startup nil
  517. "Options concerning startup of Org-mode."
  518. :tag "Org Startup"
  519. :group 'org)
  520. (defcustom org-startup-folded t
  521. "Non-nil means entering Org-mode will switch to OVERVIEW.
  522. This can also be configured on a per-file basis by adding one of
  523. the following lines anywhere in the buffer:
  524. #+STARTUP: fold (or `overview', this is equivalent)
  525. #+STARTUP: nofold (or `showall', this is equivalent)
  526. #+STARTUP: content
  527. #+STARTUP: showeverything
  528. By default, this option is ignored when Org opens agenda files
  529. for the first time. If you want the agenda to honor the startup
  530. option, set `org-agenda-inhibit-startup' to nil."
  531. :group 'org-startup
  532. :type '(choice
  533. (const :tag "nofold: show all" nil)
  534. (const :tag "fold: overview" t)
  535. (const :tag "content: all headlines" content)
  536. (const :tag "show everything, even drawers" showeverything)))
  537. (defcustom org-startup-truncated t
  538. "Non-nil means entering Org-mode will set `truncate-lines'.
  539. This is useful since some lines containing links can be very long and
  540. uninteresting. Also tables look terrible when wrapped."
  541. :group 'org-startup
  542. :type 'boolean)
  543. (defcustom org-startup-indented nil
  544. "Non-nil means turn on `org-indent-mode' on startup.
  545. This can also be configured on a per-file basis by adding one of
  546. the following lines anywhere in the buffer:
  547. #+STARTUP: indent
  548. #+STARTUP: noindent"
  549. :group 'org-structure
  550. :type '(choice
  551. (const :tag "Not" nil)
  552. (const :tag "Globally (slow on startup in large files)" t)))
  553. (defcustom org-use-sub-superscripts t
  554. "Non-nil means interpret \"_\" and \"^\" for display.
  555. When this option is turned on, you can use TeX-like syntax for sub- and
  556. superscripts. Several characters after \"_\" or \"^\" will be
  557. considered as a single item - so grouping with {} is normally not
  558. needed. For example, the following things will be parsed as single
  559. sub- or superscripts.
  560. 10^24 or 10^tau several digits will be considered 1 item.
  561. 10^-12 or 10^-tau a leading sign with digits or a word
  562. x^2-y^3 will be read as x^2 - y^3, because items are
  563. terminated by almost any nonword/nondigit char.
  564. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  565. Still, ambiguity is possible - so when in doubt use {} to enclose
  566. the sub/superscript. If you set this variable to the symbol
  567. `{}', the braces are *required* in order to trigger
  568. interpretations as sub/superscript. This can be helpful in
  569. documents that need \"_\" frequently in plain text."
  570. :group 'org-startup
  571. :version "24.1"
  572. :type '(choice
  573. (const :tag "Always interpret" t)
  574. (const :tag "Only with braces" {})
  575. (const :tag "Never interpret" nil)))
  576. (defcustom org-startup-with-beamer-mode nil
  577. "Non-nil means turn on `org-beamer-mode' on startup.
  578. This can also be configured on a per-file basis by adding one of
  579. the following lines anywhere in the buffer:
  580. #+STARTUP: beamer"
  581. :group 'org-startup
  582. :version "24.1"
  583. :type 'boolean)
  584. (defcustom org-startup-align-all-tables nil
  585. "Non-nil means align all tables when visiting a file.
  586. This is useful when the column width in tables is forced with <N> cookies
  587. in table fields. Such tables will look correct only after the first re-align.
  588. This can also be configured on a per-file basis by adding one of
  589. the following lines anywhere in the buffer:
  590. #+STARTUP: align
  591. #+STARTUP: noalign"
  592. :group 'org-startup
  593. :type 'boolean)
  594. (defcustom org-startup-with-inline-images nil
  595. "Non-nil means show inline images when loading a new Org file.
  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: inlineimages
  599. #+STARTUP: noinlineimages"
  600. :group 'org-startup
  601. :version "24.1"
  602. :type 'boolean)
  603. (defcustom org-startup-with-latex-preview nil
  604. "Non-nil means preview LaTeX fragments when loading a new Org file.
  605. This can also be configured on a per-file basis by adding one of
  606. the followinglines anywhere in the buffer:
  607. #+STARTUP: latexpreview
  608. #+STARTUP: nolatexpreview"
  609. :group 'org-startup
  610. :version "24.4"
  611. :package-version '(Org . "8.0")
  612. :type 'boolean)
  613. (defcustom org-insert-mode-line-in-empty-file nil
  614. "Non-nil means insert the first line setting Org-mode in empty files.
  615. When the function `org-mode' is called interactively in an empty file, this
  616. normally means that the file name does not automatically trigger Org-mode.
  617. To ensure that the file will always be in Org-mode in the future, a
  618. line enforcing Org-mode will be inserted into the buffer, if this option
  619. has been set."
  620. :group 'org-startup
  621. :type 'boolean)
  622. (defcustom org-replace-disputed-keys nil
  623. "Non-nil means use alternative key bindings for some keys.
  624. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  625. These keys are also used by other packages like shift-selection-mode'
  626. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  627. If you want to use Org-mode together with one of these other modes,
  628. or more generally if you would like to move some Org-mode commands to
  629. other keys, set this variable and configure the keys with the variable
  630. `org-disputed-keys'.
  631. This option is only relevant at load-time of Org-mode, and must be set
  632. *before* org.el is loaded. Changing it requires a restart of Emacs to
  633. become effective."
  634. :group 'org-startup
  635. :type 'boolean)
  636. (defcustom org-use-extra-keys nil
  637. "Non-nil means use extra key sequence definitions for certain commands.
  638. This happens automatically if you run XEmacs or if `window-system'
  639. is nil. This variable lets you do the same manually. You must
  640. set it before loading org.
  641. Example: on Carbon Emacs 22 running graphically, with an external
  642. keyboard on a Powerbook, the default way of setting M-left might
  643. not work for either Alt or ESC. Setting this variable will make
  644. it work for ESC."
  645. :group 'org-startup
  646. :type 'boolean)
  647. (if (fboundp 'defvaralias)
  648. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  649. (defcustom org-disputed-keys
  650. '(([(shift up)] . [(meta p)])
  651. ([(shift down)] . [(meta n)])
  652. ([(shift left)] . [(meta -)])
  653. ([(shift right)] . [(meta +)])
  654. ([(control shift right)] . [(meta shift +)])
  655. ([(control shift left)] . [(meta shift -)]))
  656. "Keys for which Org-mode and other modes compete.
  657. This is an alist, cars are the default keys, second element specifies
  658. the alternative to use when `org-replace-disputed-keys' is t.
  659. Keys can be specified in any syntax supported by `define-key'.
  660. The value of this option takes effect only at Org-mode's startup,
  661. therefore you'll have to restart Emacs to apply it after changing."
  662. :group 'org-startup
  663. :type 'alist)
  664. (defun org-key (key)
  665. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  666. Or return the original if not disputed.
  667. Also apply the translations defined in `org-xemacs-key-equivalents'."
  668. (when org-replace-disputed-keys
  669. (let* ((nkey (key-description key))
  670. (x (org-find-if (lambda (x)
  671. (equal (key-description (car x)) nkey))
  672. org-disputed-keys)))
  673. (setq key (if x (cdr x) key))))
  674. (when (featurep 'xemacs)
  675. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  676. key)
  677. (defun org-find-if (predicate seq)
  678. (catch 'exit
  679. (while seq
  680. (if (funcall predicate (car seq))
  681. (throw 'exit (car seq))
  682. (pop seq)))))
  683. (defun org-defkey (keymap key def)
  684. "Define a key, possibly translated, as returned by `org-key'."
  685. (define-key keymap (org-key key) def))
  686. (defcustom org-ellipsis nil
  687. "The ellipsis to use in the Org-mode outline.
  688. When nil, just use the standard three dots. When a string, use that instead,
  689. When a face, use the standard 3 dots, but with the specified face.
  690. The change affects only Org-mode (which will then use its own display table).
  691. Changing this requires executing `M-x org-mode' in a buffer to become
  692. effective."
  693. :group 'org-startup
  694. :type '(choice (const :tag "Default" nil)
  695. (face :tag "Face" :value org-warning)
  696. (string :tag "String" :value "...#")))
  697. (defvar org-display-table nil
  698. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  699. (defgroup org-keywords nil
  700. "Keywords in Org-mode."
  701. :tag "Org Keywords"
  702. :group 'org)
  703. (defcustom org-deadline-string "DEADLINE:"
  704. "String to mark deadline entries.
  705. A deadline is this string, followed by a time stamp. Should be a word,
  706. terminated by a colon. You can insert a schedule keyword and
  707. a timestamp with \\[org-deadline].
  708. Changes become only effective after restarting Emacs."
  709. :group 'org-keywords
  710. :type 'string)
  711. (defcustom org-scheduled-string "SCHEDULED:"
  712. "String to mark scheduled TODO entries.
  713. A schedule is this string, followed by a time stamp. Should be a word,
  714. terminated by a colon. You can insert a schedule keyword and
  715. a timestamp with \\[org-schedule].
  716. Changes become only effective after restarting Emacs."
  717. :group 'org-keywords
  718. :type 'string)
  719. (defcustom org-closed-string "CLOSED:"
  720. "String used as the prefix for timestamps logging closing a TODO entry."
  721. :group 'org-keywords
  722. :type 'string)
  723. (defcustom org-clock-string "CLOCK:"
  724. "String used as prefix for timestamps clocking work hours on an item."
  725. :group 'org-keywords
  726. :type 'string)
  727. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  728. org-scheduled-string "\\|"
  729. org-deadline-string "\\|"
  730. org-closed-string "\\|"
  731. org-clock-string "\\)")
  732. "Matches a line with planning or clock info.")
  733. (defcustom org-comment-string "COMMENT"
  734. "Entries starting with this keyword will never be exported.
  735. An entry can be toggled between COMMENT and normal with
  736. \\[org-toggle-comment].
  737. Changes become only effective after restarting Emacs."
  738. :group 'org-keywords
  739. :type 'string)
  740. (defcustom org-quote-string "QUOTE"
  741. "Entries starting with this keyword will be exported in fixed-width font.
  742. Quoting applies only to the text in the entry following the headline, and does
  743. not extend beyond the next headline, even if that is lower level.
  744. An entry can be toggled between QUOTE and normal with
  745. \\[org-toggle-fixed-width-section]."
  746. :group 'org-keywords
  747. :type 'string)
  748. (defconst org-repeat-re
  749. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  750. "Regular expression for specifying repeated events.
  751. After a match, group 1 contains the repeat expression.")
  752. (defgroup org-structure nil
  753. "Options concerning the general structure of Org-mode files."
  754. :tag "Org Structure"
  755. :group 'org)
  756. (defgroup org-reveal-location nil
  757. "Options about how to make context of a location visible."
  758. :tag "Org Reveal Location"
  759. :group 'org-structure)
  760. (defconst org-context-choice
  761. '(choice
  762. (const :tag "Always" t)
  763. (const :tag "Never" nil)
  764. (repeat :greedy t :tag "Individual contexts"
  765. (cons
  766. (choice :tag "Context"
  767. (const agenda)
  768. (const org-goto)
  769. (const occur-tree)
  770. (const tags-tree)
  771. (const link-search)
  772. (const mark-goto)
  773. (const bookmark-jump)
  774. (const isearch)
  775. (const default))
  776. (boolean))))
  777. "Contexts for the reveal options.")
  778. (defcustom org-show-hierarchy-above '((default . t))
  779. "Non-nil means show full hierarchy when revealing a location.
  780. Org-mode often shows locations in an org-mode file which might have
  781. been invisible before. When this is set, the hierarchy of headings
  782. above the exposed location is shown.
  783. Turning this off for example for sparse trees makes them very compact.
  784. Instead of t, this can also be an alist specifying this option for different
  785. contexts. Valid contexts are
  786. agenda when exposing an entry from the agenda
  787. org-goto when using the command `org-goto' on key C-c C-j
  788. occur-tree when using the command `org-occur' on key C-c /
  789. tags-tree when constructing a sparse tree based on tags matches
  790. link-search when exposing search matches associated with a link
  791. mark-goto when exposing the jump goal of a mark
  792. bookmark-jump when exposing a bookmark location
  793. isearch when exiting from an incremental search
  794. default default for all contexts not set explicitly"
  795. :group 'org-reveal-location
  796. :type org-context-choice)
  797. (defcustom org-show-following-heading '((default . nil))
  798. "Non-nil means show following heading when revealing a location.
  799. Org-mode often shows locations in an org-mode file which might have
  800. been invisible before. When this is set, the heading following the
  801. match is shown.
  802. Turning this off for example for sparse trees makes them very compact,
  803. but makes it harder to edit the location of the match. In such a case,
  804. use the command \\[org-reveal] to show more context.
  805. Instead of t, this can also be an alist specifying this option for different
  806. contexts. See `org-show-hierarchy-above' for valid contexts."
  807. :group 'org-reveal-location
  808. :type org-context-choice)
  809. (defcustom org-show-siblings '((default . nil) (isearch t))
  810. "Non-nil means show all sibling heading when revealing a location.
  811. Org-mode often shows locations in an org-mode file which might have
  812. been invisible before. When this is set, the sibling of the current entry
  813. heading are all made visible. If `org-show-hierarchy-above' is t,
  814. the same happens on each level of the hierarchy above the current entry.
  815. By default this is on for the isearch context, off for all other contexts.
  816. Turning this off for example for sparse trees makes them very compact,
  817. but makes it harder to edit the location of the match. In such a case,
  818. use the command \\[org-reveal] to show more context.
  819. Instead of t, this can also be an alist specifying this option for different
  820. contexts. See `org-show-hierarchy-above' for valid contexts."
  821. :group 'org-reveal-location
  822. :type org-context-choice)
  823. (defcustom org-show-entry-below '((default . nil))
  824. "Non-nil means show the entry below a headline when revealing a location.
  825. Org-mode often shows locations in an org-mode file which might have
  826. been invisible before. When this is set, the text below the headline that is
  827. exposed is also shown.
  828. By default this is off for all contexts.
  829. Instead of t, this can also be an alist specifying this option for different
  830. contexts. See `org-show-hierarchy-above' for valid contexts."
  831. :group 'org-reveal-location
  832. :type org-context-choice)
  833. (defcustom org-indirect-buffer-display 'other-window
  834. "How should indirect tree buffers be displayed?
  835. This applies to indirect buffers created with the commands
  836. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  837. Valid values are:
  838. current-window Display in the current window
  839. other-window Just display in another window.
  840. dedicated-frame Create one new frame, and re-use it each time.
  841. new-frame Make a new frame each time. Note that in this case
  842. previously-made indirect buffers are kept, and you need to
  843. kill these buffers yourself."
  844. :group 'org-structure
  845. :group 'org-agenda-windows
  846. :type '(choice
  847. (const :tag "In current window" current-window)
  848. (const :tag "In current frame, other window" other-window)
  849. (const :tag "Each time a new frame" new-frame)
  850. (const :tag "One dedicated frame" dedicated-frame)))
  851. (defcustom org-use-speed-commands nil
  852. "Non-nil means activate single letter commands at beginning of a headline.
  853. This may also be a function to test for appropriate locations where speed
  854. commands should be active."
  855. :group 'org-structure
  856. :type '(choice
  857. (const :tag "Never" nil)
  858. (const :tag "At beginning of headline stars" t)
  859. (function)))
  860. (defcustom org-speed-commands-user nil
  861. "Alist of additional speed commands.
  862. This list will be checked before `org-speed-commands-default'
  863. when the variable `org-use-speed-commands' is non-nil
  864. and when the cursor is at the beginning of a headline.
  865. The car if each entry is a string with a single letter, which must
  866. be assigned to `self-insert-command' in the global map.
  867. The cdr is either a command to be called interactively, a function
  868. to be called, or a form to be evaluated.
  869. An entry that is just a list with a single string will be interpreted
  870. as a descriptive headline that will be added when listing the speed
  871. commands in the Help buffer using the `?' speed command."
  872. :group 'org-structure
  873. :type '(repeat :value ("k" . ignore)
  874. (choice :value ("k" . ignore)
  875. (list :tag "Descriptive Headline" (string :tag "Headline"))
  876. (cons :tag "Letter and Command"
  877. (string :tag "Command letter")
  878. (choice
  879. (function)
  880. (sexp))))))
  881. (defgroup org-cycle nil
  882. "Options concerning visibility cycling in Org-mode."
  883. :tag "Org Cycle"
  884. :group 'org-structure)
  885. (defcustom org-cycle-skip-children-state-if-no-children t
  886. "Non-nil means skip CHILDREN state in entries that don't have any."
  887. :group 'org-cycle
  888. :type 'boolean)
  889. (defcustom org-cycle-max-level nil
  890. "Maximum level which should still be subject to visibility cycling.
  891. Levels higher than this will, for cycling, be treated as text, not a headline.
  892. When `org-odd-levels-only' is set, a value of N in this variable actually
  893. means 2N-1 stars as the limiting headline.
  894. When nil, cycle all levels.
  895. Note that the limiting level of cycling is also influenced by
  896. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  897. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  898. than its value."
  899. :group 'org-cycle
  900. :type '(choice
  901. (const :tag "No limit" nil)
  902. (integer :tag "Maximum level")))
  903. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  904. "Names of drawers. Drawers are not opened by cycling on the headline above.
  905. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  906. this:
  907. :DRAWERNAME:
  908. .....
  909. :END:
  910. The drawer \"PROPERTIES\" is special for capturing properties through
  911. the property API.
  912. Drawers can be defined on the per-file basis with a line like:
  913. #+DRAWERS: HIDDEN STATE PROPERTIES"
  914. :group 'org-structure
  915. :group 'org-cycle
  916. :type '(repeat (string :tag "Drawer Name")))
  917. (defcustom org-hide-block-startup nil
  918. "Non-nil means entering Org-mode will fold all blocks.
  919. This can also be set in on a per-file basis with
  920. #+STARTUP: hideblocks
  921. #+STARTUP: showblocks"
  922. :group 'org-startup
  923. :group 'org-cycle
  924. :type 'boolean)
  925. (defcustom org-cycle-global-at-bob nil
  926. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  927. This makes it possible to do global cycling without having to use S-TAB or
  928. \\[universal-argument] TAB. For this special case to work, the first line
  929. of the buffer must not be a headline -- it may be empty or some other text.
  930. When used in this way, `org-cycle-hook' is disabled temporarily to make
  931. sure the cursor stays at the beginning of the buffer. When this option is
  932. nil, don't do anything special at the beginning of the buffer."
  933. :group 'org-cycle
  934. :type 'boolean)
  935. (defcustom org-cycle-level-after-item/entry-creation t
  936. "Non-nil means cycle entry level or item indentation in new empty entries.
  937. When the cursor is at the end of an empty headline, i.e., with only stars
  938. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  939. and then all possible ancestor states, before returning to the original state.
  940. This makes data entry extremely fast: M-RET to create a new headline,
  941. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  942. When the cursor is at the end of an empty plain list item, one TAB will
  943. make it a subitem, two or more tabs will back up to make this an item
  944. higher up in the item hierarchy."
  945. :group 'org-cycle
  946. :type 'boolean)
  947. (defcustom org-cycle-emulate-tab t
  948. "Where should `org-cycle' emulate TAB.
  949. nil Never
  950. white Only in completely white lines
  951. whitestart Only at the beginning of lines, before the first non-white char
  952. t Everywhere except in headlines
  953. exc-hl-bol Everywhere except at the start of a headline
  954. If TAB is used in a place where it does not emulate TAB, the current subtree
  955. visibility is cycled."
  956. :group 'org-cycle
  957. :type '(choice (const :tag "Never" nil)
  958. (const :tag "Only in completely white lines" white)
  959. (const :tag "Before first char in a line" whitestart)
  960. (const :tag "Everywhere except in headlines" t)
  961. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  962. ))
  963. (defcustom org-cycle-separator-lines 2
  964. "Number of empty lines needed to keep an empty line between collapsed trees.
  965. If you leave an empty line between the end of a subtree and the following
  966. headline, this empty line is hidden when the subtree is folded.
  967. Org-mode will leave (exactly) one empty line visible if the number of
  968. empty lines is equal or larger to the number given in this variable.
  969. So the default 2 means at least 2 empty lines after the end of a subtree
  970. are needed to produce free space between a collapsed subtree and the
  971. following headline.
  972. If the number is negative, and the number of empty lines is at least -N,
  973. all empty lines are shown.
  974. Special case: when 0, never leave empty lines in collapsed view."
  975. :group 'org-cycle
  976. :type 'integer)
  977. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  978. (defcustom org-pre-cycle-hook nil
  979. "Hook that is run before visibility cycling is happening.
  980. The function(s) in this hook must accept a single argument which indicates
  981. the new state that will be set right after running this hook. The
  982. argument is a symbol. Before a global state change, it can have the values
  983. `overview', `content', or `all'. Before a local state change, it can have
  984. the values `folded', `children', or `subtree'."
  985. :group 'org-cycle
  986. :type 'hook)
  987. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  988. org-cycle-hide-drawers
  989. org-cycle-hide-inline-tasks
  990. org-cycle-show-empty-lines
  991. org-optimize-window-after-visibility-change)
  992. "Hook that is run after `org-cycle' has changed the buffer visibility.
  993. The function(s) in this hook must accept a single argument which indicates
  994. the new state that was set by the most recent `org-cycle' command. The
  995. argument is a symbol. After a global state change, it can have the values
  996. `overview', `contents', or `all'. After a local state change, it can have
  997. the values `folded', `children', or `subtree'."
  998. :group 'org-cycle
  999. :type 'hook)
  1000. (defgroup org-edit-structure nil
  1001. "Options concerning structure editing in Org-mode."
  1002. :tag "Org Edit Structure"
  1003. :group 'org-structure)
  1004. (defcustom org-odd-levels-only nil
  1005. "Non-nil means skip even levels and only use odd levels for the outline.
  1006. This has the effect that two stars are being added/taken away in
  1007. promotion/demotion commands. It also influences how levels are
  1008. handled by the exporters.
  1009. Changing it requires restart of `font-lock-mode' to become effective
  1010. for fontification also in regions already fontified.
  1011. You may also set this on a per-file basis by adding one of the following
  1012. lines to the buffer:
  1013. #+STARTUP: odd
  1014. #+STARTUP: oddeven"
  1015. :group 'org-edit-structure
  1016. :group 'org-appearance
  1017. :type 'boolean)
  1018. (defcustom org-adapt-indentation t
  1019. "Non-nil means adapt indentation to outline node level.
  1020. When this variable is set, Org assumes that you write outlines by
  1021. indenting text in each node to align with the headline (after the stars).
  1022. The following issues are influenced by this variable:
  1023. - When this is set and the *entire* text in an entry is indented, the
  1024. indentation is increased by one space in a demotion command, and
  1025. decreased by one in a promotion command. If any line in the entry
  1026. body starts with text at column 0, indentation is not changed at all.
  1027. - Property drawers and planning information is inserted indented when
  1028. this variable s set. When nil, they will not be indented.
  1029. - TAB indents a line relative to context. The lines below a headline
  1030. will be indented when this variable is set.
  1031. Note that this is all about true indentation, by adding and removing
  1032. space characters. See also `org-indent.el' which does level-dependent
  1033. indentation in a virtual way, i.e. at display time in Emacs."
  1034. :group 'org-edit-structure
  1035. :type 'boolean)
  1036. (defcustom org-special-ctrl-a/e nil
  1037. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1038. When t, `C-a' will bring back the cursor to the beginning of the
  1039. headline text, i.e. after the stars and after a possible TODO
  1040. keyword. In an item, this will be the position after bullet and
  1041. check-box, if any. When the cursor is already at that position,
  1042. another `C-a' will bring it to the beginning of the line.
  1043. `C-e' will jump to the end of the headline, ignoring the presence
  1044. of tags in the headline. A second `C-e' will then jump to the
  1045. true end of the line, after any tags. This also means that, when
  1046. this variable is non-nil, `C-e' also will never jump beyond the
  1047. end of the heading of a folded section, i.e. not after the
  1048. ellipses.
  1049. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1050. normally, going to the true line boundary first. Only a directly
  1051. following, identical keypress will bring the cursor to the
  1052. special positions.
  1053. This may also be a cons cell where the behavior for `C-a' and
  1054. `C-e' is set separately."
  1055. :group 'org-edit-structure
  1056. :type '(choice
  1057. (const :tag "off" nil)
  1058. (const :tag "on: after stars/bullet and before tags first" t)
  1059. (const :tag "reversed: true line boundary first" reversed)
  1060. (cons :tag "Set C-a and C-e separately"
  1061. (choice :tag "Special C-a"
  1062. (const :tag "off" nil)
  1063. (const :tag "on: after stars/bullet first" t)
  1064. (const :tag "reversed: before stars/bullet first" reversed))
  1065. (choice :tag "Special C-e"
  1066. (const :tag "off" nil)
  1067. (const :tag "on: before tags first" t)
  1068. (const :tag "reversed: after tags first" reversed)))))
  1069. (if (fboundp 'defvaralias)
  1070. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  1071. (defcustom org-special-ctrl-k nil
  1072. "Non-nil means `C-k' will behave specially in headlines.
  1073. When nil, `C-k' will call the default `kill-line' command.
  1074. When t, the following will happen while the cursor is in the headline:
  1075. - When the cursor is at the beginning of a headline, kill the entire
  1076. line and possible the folded subtree below the line.
  1077. - When in the middle of the headline text, kill the headline up to the tags.
  1078. - When after the headline text, kill the tags."
  1079. :group 'org-edit-structure
  1080. :type 'boolean)
  1081. (defcustom org-ctrl-k-protect-subtree nil
  1082. "Non-nil means, do not delete a hidden subtree with C-k.
  1083. When set to the symbol `error', simply throw an error when C-k is
  1084. used to kill (part-of) a headline that has hidden text behind it.
  1085. Any other non-nil value will result in a query to the user, if it is
  1086. OK to kill that hidden subtree. When nil, kill without remorse."
  1087. :group 'org-edit-structure
  1088. :version "24.1"
  1089. :type '(choice
  1090. (const :tag "Do not protect hidden subtrees" nil)
  1091. (const :tag "Protect hidden subtrees with a security query" t)
  1092. (const :tag "Never kill a hidden subtree with C-k" error)))
  1093. (defcustom org-catch-invisible-edits nil
  1094. "Check if in invisible region before inserting or deleting a character.
  1095. Valid values are:
  1096. nil Do not check, so just do invisible edits.
  1097. error Throw an error and do nothing.
  1098. show Make point visible, and do the requested edit.
  1099. show-and-error Make point visible, then throw an error and abort the edit.
  1100. smart Make point visible, and do insertion/deletion if it is
  1101. adjacent to visible text and the change feels predictable.
  1102. Never delete a previously invisible character or add in the
  1103. middle or right after an invisible region. Basically, this
  1104. allows insertion and backward-delete right before ellipses.
  1105. FIXME: maybe in this case we should not even show?"
  1106. :group 'org-edit-structure
  1107. :version "24.1"
  1108. :type '(choice
  1109. (const :tag "Do not check" nil)
  1110. (const :tag "Throw error when trying to edit" error)
  1111. (const :tag "Unhide, but do not do the edit" show-and-error)
  1112. (const :tag "Show invisible part and do the edit" show)
  1113. (const :tag "Be smart and do the right thing" smart)))
  1114. (defcustom org-yank-folded-subtrees t
  1115. "Non-nil means when yanking subtrees, fold them.
  1116. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1117. it starts with a heading and all other headings in it are either children
  1118. or siblings, then fold all the subtrees. However, do this only if no
  1119. text after the yank would be swallowed into a folded tree by this action."
  1120. :group 'org-edit-structure
  1121. :type 'boolean)
  1122. (defcustom org-yank-adjusted-subtrees nil
  1123. "Non-nil means when yanking subtrees, adjust the level.
  1124. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1125. this function for details."
  1126. :group 'org-edit-structure
  1127. :type 'boolean)
  1128. (defcustom org-M-RET-may-split-line '((default . t))
  1129. "Non-nil means M-RET will split the line at the cursor position.
  1130. When nil, it will go to the end of the line before making a
  1131. new line.
  1132. You may also set this option in a different way for different
  1133. contexts. Valid contexts are:
  1134. headline when creating a new headline
  1135. item when creating a new item
  1136. table in a table field
  1137. default the value to be used for all contexts not explicitly
  1138. customized"
  1139. :group 'org-structure
  1140. :group 'org-table
  1141. :type '(choice
  1142. (const :tag "Always" t)
  1143. (const :tag "Never" nil)
  1144. (repeat :greedy t :tag "Individual contexts"
  1145. (cons
  1146. (choice :tag "Context"
  1147. (const headline)
  1148. (const item)
  1149. (const table)
  1150. (const default))
  1151. (boolean)))))
  1152. (defcustom org-insert-heading-respect-content nil
  1153. "Non-nil means insert new headings after the current subtree.
  1154. When nil, the new heading is created directly after the current line.
  1155. The commands \\[org-insert-heading-respect-content] and
  1156. \\[org-insert-todo-heading-respect-content] turn this variable on
  1157. for the duration of the command."
  1158. :group 'org-structure
  1159. :type 'boolean)
  1160. (defcustom org-blank-before-new-entry '((heading . auto)
  1161. (plain-list-item . auto))
  1162. "Should `org-insert-heading' leave a blank line before new heading/item?
  1163. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1164. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1165. which case Org will look at the surrounding headings/items and try to
  1166. make an intelligent decision whether to insert a blank line or not.
  1167. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1168. set, the setting here is ignored and no empty line is inserted, to avoid
  1169. breaking the list structure."
  1170. :group 'org-edit-structure
  1171. :type '(list
  1172. (cons (const heading)
  1173. (choice (const :tag "Never" nil)
  1174. (const :tag "Always" t)
  1175. (const :tag "Auto" auto)))
  1176. (cons (const plain-list-item)
  1177. (choice (const :tag "Never" nil)
  1178. (const :tag "Always" t)
  1179. (const :tag "Auto" auto)))))
  1180. (defcustom org-insert-heading-hook nil
  1181. "Hook being run after inserting a new heading."
  1182. :group 'org-edit-structure
  1183. :type 'hook)
  1184. (defcustom org-enable-fixed-width-editor t
  1185. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1186. This currently only means they are never auto-wrapped.
  1187. When nil, such lines will be treated like ordinary lines.
  1188. See also the QUOTE keyword."
  1189. :group 'org-edit-structure
  1190. :type 'boolean)
  1191. (defcustom org-goto-auto-isearch t
  1192. "Non-nil means typing characters in `org-goto' starts incremental search.
  1193. When nil, you can use these keybindings to navigate the buffer:
  1194. q Quit the org-goto interface
  1195. n Go to the next visible heading
  1196. p Go to the previous visible heading
  1197. f Go one heading forward on same level
  1198. b Go one heading backward on same level
  1199. u Go one heading up"
  1200. :group 'org-edit-structure
  1201. :type 'boolean)
  1202. (defgroup org-sparse-trees nil
  1203. "Options concerning sparse trees in Org-mode."
  1204. :tag "Org Sparse Trees"
  1205. :group 'org-structure)
  1206. (defcustom org-highlight-sparse-tree-matches t
  1207. "Non-nil means highlight all matches that define a sparse tree.
  1208. The highlights will automatically disappear the next time the buffer is
  1209. changed by an edit command."
  1210. :group 'org-sparse-trees
  1211. :type 'boolean)
  1212. (defcustom org-remove-highlights-with-change t
  1213. "Non-nil means any change to the buffer will remove temporary highlights.
  1214. Such highlights are created by `org-occur' and `org-clock-display'.
  1215. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1216. The highlights created by `org-preview-latex-fragment' always need
  1217. `C-c C-c' to be removed."
  1218. :group 'org-sparse-trees
  1219. :group 'org-time
  1220. :type 'boolean)
  1221. (defcustom org-occur-hook '(org-first-headline-recenter)
  1222. "Hook that is run after `org-occur' has constructed a sparse tree.
  1223. This can be used to recenter the window to show as much of the structure
  1224. as possible."
  1225. :group 'org-sparse-trees
  1226. :type 'hook)
  1227. (defgroup org-imenu-and-speedbar nil
  1228. "Options concerning imenu and speedbar in Org-mode."
  1229. :tag "Org Imenu and Speedbar"
  1230. :group 'org-structure)
  1231. (defcustom org-imenu-depth 2
  1232. "The maximum level for Imenu access to Org-mode headlines.
  1233. This also applied for speedbar access."
  1234. :group 'org-imenu-and-speedbar
  1235. :type 'integer)
  1236. (defgroup org-table nil
  1237. "Options concerning tables in Org-mode."
  1238. :tag "Org Table"
  1239. :group 'org)
  1240. (defcustom org-enable-table-editor 'optimized
  1241. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1242. When nil, such lines will be treated like ordinary lines.
  1243. When equal to the symbol `optimized', the table editor will be optimized to
  1244. do the following:
  1245. - Automatic overwrite mode in front of whitespace in table fields.
  1246. This makes the structure of the table stay in tact as long as the edited
  1247. field does not exceed the column width.
  1248. - Minimize the number of realigns. Normally, the table is aligned each time
  1249. TAB or RET are pressed to move to another field. With optimization this
  1250. happens only if changes to a field might have changed the column width.
  1251. Optimization requires replacing the functions `self-insert-command',
  1252. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1253. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1254. very good at guessing when a re-align will be necessary, but you can always
  1255. force one with \\[org-ctrl-c-ctrl-c].
  1256. If you would like to use the optimized version in Org-mode, but the
  1257. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1258. This variable can be used to turn on and off the table editor during a session,
  1259. but in order to toggle optimization, a restart is required.
  1260. See also the variable `org-table-auto-blank-field'."
  1261. :group 'org-table
  1262. :type '(choice
  1263. (const :tag "off" nil)
  1264. (const :tag "on" t)
  1265. (const :tag "on, optimized" optimized)))
  1266. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1267. (version<= emacs-version "24.1"))
  1268. "Non-nil means cluster self-insert commands for undo when possible.
  1269. If this is set, then, like in the Emacs command loop, 20 consecutive
  1270. characters will be undone together.
  1271. This is configurable, because there is some impact on typing performance."
  1272. :group 'org-table
  1273. :type 'boolean)
  1274. (defcustom org-table-tab-recognizes-table.el t
  1275. "Non-nil means TAB will automatically notice a table.el table.
  1276. When it sees such a table, it moves point into it and - if necessary -
  1277. calls `table-recognize-table'."
  1278. :group 'org-table-editing
  1279. :type 'boolean)
  1280. (defgroup org-link nil
  1281. "Options concerning links in Org-mode."
  1282. :tag "Org Link"
  1283. :group 'org)
  1284. (defvar org-link-abbrev-alist-local nil
  1285. "Buffer-local version of `org-link-abbrev-alist', which see.
  1286. The value of this is taken from the #+LINK lines.")
  1287. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1288. (defcustom org-link-abbrev-alist nil
  1289. "Alist of link abbreviations.
  1290. The car of each element is a string, to be replaced at the start of a link.
  1291. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1292. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1293. [[linkkey:tag][description]]
  1294. The 'linkkey' must be a word word, starting with a letter, followed
  1295. by letters, numbers, '-' or '_'.
  1296. If REPLACE is a string, the tag will simply be appended to create the link.
  1297. If the string contains \"%s\", the tag will be inserted there. If the string
  1298. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1299. at that point (see the function `url-hexify-string'). If the string contains
  1300. the specifier \"%(my-function)\", then the custom function `my-function' will
  1301. be invoked: this function takes the tag as its only argument and must return
  1302. a string.
  1303. REPLACE may also be a function that will be called with the tag as the
  1304. only argument to create the link, which should be returned as a string.
  1305. See the manual for examples."
  1306. :group 'org-link
  1307. :type '(repeat
  1308. (cons
  1309. (string :tag "Protocol")
  1310. (choice
  1311. (string :tag "Format")
  1312. (function)))))
  1313. (defcustom org-descriptive-links t
  1314. "Non-nil means Org will display descriptive links.
  1315. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1316. \"Org Website\", hiding the link itself and just displaying its
  1317. description. When set to `nil', Org will display the full links
  1318. literally.
  1319. You can interactively set the value of this variable by calling
  1320. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1321. :group 'org-link
  1322. :type 'boolean)
  1323. (defcustom org-link-file-path-type 'adaptive
  1324. "How the path name in file links should be stored.
  1325. Valid values are:
  1326. relative Relative to the current directory, i.e. the directory of the file
  1327. into which the link is being inserted.
  1328. absolute Absolute path, if possible with ~ for home directory.
  1329. noabbrev Absolute path, no abbreviation of home directory.
  1330. adaptive Use relative path for files in the current directory and sub-
  1331. directories of it. For other files, use an absolute path."
  1332. :group 'org-link
  1333. :type '(choice
  1334. (const relative)
  1335. (const absolute)
  1336. (const noabbrev)
  1337. (const adaptive)))
  1338. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1339. "Types of links that should be activated in Org-mode files.
  1340. This is a list of symbols, each leading to the activation of a certain link
  1341. type. In principle, it does not hurt to turn on most link types - there may
  1342. be a small gain when turning off unused link types. The types are:
  1343. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1344. angle Links in angular brackets that may contain whitespace like
  1345. <bbdb:Carsten Dominik>.
  1346. plain Plain links in normal text, no whitespace, like http://google.com.
  1347. radio Text that is matched by a radio target, see manual for details.
  1348. tag Tag settings in a headline (link to tag search).
  1349. date Time stamps (link to calendar).
  1350. footnote Footnote labels.
  1351. Changing this variable requires a restart of Emacs to become effective."
  1352. :group 'org-link
  1353. :type '(set :greedy t
  1354. (const :tag "Double bracket links" bracket)
  1355. (const :tag "Angular bracket links" angle)
  1356. (const :tag "Plain text links" plain)
  1357. (const :tag "Radio target matches" radio)
  1358. (const :tag "Tags" tag)
  1359. (const :tag "Timestamps" date)
  1360. (const :tag "Footnotes" footnote)))
  1361. (defcustom org-make-link-description-function nil
  1362. "Function to use for generating link descriptions from links.
  1363. When nil, the link location will be used. This function must take
  1364. two parameters: the first one is the link, the second one is the
  1365. description generated by `org-insert-link'. The function should
  1366. return the description to use."
  1367. :group 'org-link
  1368. :type 'function)
  1369. (defgroup org-link-store nil
  1370. "Options concerning storing links in Org-mode."
  1371. :tag "Org Store Link"
  1372. :group 'org-link)
  1373. (defcustom org-url-hexify-p t
  1374. "When non-nil, hexify URL when creating a link."
  1375. :type 'boolean
  1376. :version "24.3"
  1377. :group 'org-link-store)
  1378. (defcustom org-email-link-description-format "Email %c: %.30s"
  1379. "Format of the description part of a link to an email or usenet message.
  1380. The following %-escapes will be replaced by corresponding information:
  1381. %F full \"From\" field
  1382. %f name, taken from \"From\" field, address if no name
  1383. %T full \"To\" field
  1384. %t first name in \"To\" field, address if no name
  1385. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1386. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1387. %s subject
  1388. %d date
  1389. %m message-id.
  1390. You may use normal field width specification between the % and the letter.
  1391. This is for example useful to limit the length of the subject.
  1392. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1393. :group 'org-link-store
  1394. :type 'string)
  1395. (defcustom org-from-is-user-regexp
  1396. (let (r1 r2)
  1397. (when (and user-mail-address (not (string= user-mail-address "")))
  1398. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1399. (when (and user-full-name (not (string= user-full-name "")))
  1400. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1401. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1402. "Regexp matched against the \"From:\" header of an email or usenet message.
  1403. It should match if the message is from the user him/herself."
  1404. :group 'org-link-store
  1405. :type 'regexp)
  1406. (defcustom org-context-in-file-links t
  1407. "Non-nil means file links from `org-store-link' contain context.
  1408. A search string will be added to the file name with :: as separator and
  1409. used to find the context when the link is activated by the command
  1410. `org-open-at-point'. When this option is t, the entire active region
  1411. will be placed in the search string of the file link. If set to a
  1412. positive integer, only the first n lines of context will be stored.
  1413. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1414. negates this setting for the duration of the command."
  1415. :group 'org-link-store
  1416. :type '(choice boolean integer))
  1417. (defcustom org-keep-stored-link-after-insertion nil
  1418. "Non-nil means keep link in list for entire session.
  1419. The command `org-store-link' adds a link pointing to the current
  1420. location to an internal list. These links accumulate during a session.
  1421. The command `org-insert-link' can be used to insert links into any
  1422. Org-mode file (offering completion for all stored links). When this
  1423. option is nil, every link which has been inserted once using \\[org-insert-link]
  1424. will be removed from the list, to make completing the unused links
  1425. more efficient."
  1426. :group 'org-link-store
  1427. :type 'boolean)
  1428. (defgroup org-link-follow nil
  1429. "Options concerning following links in Org-mode."
  1430. :tag "Org Follow Link"
  1431. :group 'org-link)
  1432. (defcustom org-link-translation-function nil
  1433. "Function to translate links with different syntax to Org syntax.
  1434. This can be used to translate links created for example by the Planner
  1435. or emacs-wiki packages to Org syntax.
  1436. The function must accept two parameters, a TYPE containing the link
  1437. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1438. which is everything after the link protocol. It should return a cons
  1439. with possibly modified values of type and path.
  1440. Org contains a function for this, so if you set this variable to
  1441. `org-translate-link-from-planner', you should be able follow many
  1442. links created by planner."
  1443. :group 'org-link-follow
  1444. :type 'function)
  1445. (defcustom org-follow-link-hook nil
  1446. "Hook that is run after a link has been followed."
  1447. :group 'org-link-follow
  1448. :type 'hook)
  1449. (defcustom org-tab-follows-link nil
  1450. "Non-nil means on links TAB will follow the link.
  1451. Needs to be set before org.el is loaded.
  1452. This really should not be used, it does not make sense, and the
  1453. implementation is bad."
  1454. :group 'org-link-follow
  1455. :type 'boolean)
  1456. (defcustom org-return-follows-link nil
  1457. "Non-nil means on links RET will follow the link.
  1458. In tables, the special behavior of RET has precedence."
  1459. :group 'org-link-follow
  1460. :type 'boolean)
  1461. (defcustom org-mouse-1-follows-link
  1462. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1463. "Non-nil means mouse-1 on a link will follow the link.
  1464. A longer mouse click will still set point. Does not work on XEmacs.
  1465. Needs to be set before org.el is loaded."
  1466. :group 'org-link-follow
  1467. :type 'boolean)
  1468. (defcustom org-mark-ring-length 4
  1469. "Number of different positions to be recorded in the ring.
  1470. Changing this requires a restart of Emacs to work correctly."
  1471. :group 'org-link-follow
  1472. :type 'integer)
  1473. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1474. "Non-nil means internal links in Org files must exactly match a headline.
  1475. When nil, the link search tries to match a phrase with all words
  1476. in the search text."
  1477. :group 'org-link-follow
  1478. :version "24.1"
  1479. :type '(choice
  1480. (const :tag "Use fuzzy text search" nil)
  1481. (const :tag "Match only exact headline" t)
  1482. (const :tag "Match exact headline or query to create it"
  1483. query-to-create)))
  1484. (defcustom org-link-frame-setup
  1485. '((vm . vm-visit-folder-other-frame)
  1486. (vm-imap . vm-visit-imap-folder-other-frame)
  1487. (gnus . org-gnus-no-new-news)
  1488. (file . find-file-other-window)
  1489. (wl . wl-other-frame))
  1490. "Setup the frame configuration for following links.
  1491. When following a link with Emacs, it may often be useful to display
  1492. this link in another window or frame. This variable can be used to
  1493. set this up for the different types of links.
  1494. For VM, use any of
  1495. `vm-visit-folder'
  1496. `vm-visit-folder-other-window'
  1497. `vm-visit-folder-other-frame'
  1498. For Gnus, use any of
  1499. `gnus'
  1500. `gnus-other-frame'
  1501. `org-gnus-no-new-news'
  1502. For FILE, use any of
  1503. `find-file'
  1504. `find-file-other-window'
  1505. `find-file-other-frame'
  1506. For Wanderlust use any of
  1507. `wl'
  1508. `wl-other-frame'
  1509. For the calendar, use the variable `calendar-setup'.
  1510. For BBDB, it is currently only possible to display the matches in
  1511. another window."
  1512. :group 'org-link-follow
  1513. :type '(list
  1514. (cons (const vm)
  1515. (choice
  1516. (const vm-visit-folder)
  1517. (const vm-visit-folder-other-window)
  1518. (const vm-visit-folder-other-frame)))
  1519. (cons (const gnus)
  1520. (choice
  1521. (const gnus)
  1522. (const gnus-other-frame)
  1523. (const org-gnus-no-new-news)))
  1524. (cons (const file)
  1525. (choice
  1526. (const find-file)
  1527. (const find-file-other-window)
  1528. (const find-file-other-frame)))
  1529. (cons (const wl)
  1530. (choice
  1531. (const wl)
  1532. (const wl-other-frame)))))
  1533. (defcustom org-display-internal-link-with-indirect-buffer nil
  1534. "Non-nil means use indirect buffer to display infile links.
  1535. Activating internal links (from one location in a file to another location
  1536. in the same file) normally just jumps to the location. When the link is
  1537. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1538. is displayed in
  1539. another window. When this option is set, the other window actually displays
  1540. an indirect buffer clone of the current buffer, to avoid any visibility
  1541. changes to the current buffer."
  1542. :group 'org-link-follow
  1543. :type 'boolean)
  1544. (defcustom org-open-non-existing-files nil
  1545. "Non-nil means `org-open-file' will open non-existing files.
  1546. When nil, an error will be generated.
  1547. This variable applies only to external applications because they
  1548. might choke on non-existing files. If the link is to a file that
  1549. will be opened in Emacs, the variable is ignored."
  1550. :group 'org-link-follow
  1551. :type 'boolean)
  1552. (defcustom org-open-directory-means-index-dot-org nil
  1553. "Non-nil means a link to a directory really means to index.org.
  1554. When nil, following a directory link will run dired or open a finder/explorer
  1555. window on that directory."
  1556. :group 'org-link-follow
  1557. :type 'boolean)
  1558. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1559. "Function and arguments to call for following mailto links.
  1560. This is a list with the first element being a Lisp function, and the
  1561. remaining elements being arguments to the function. In string arguments,
  1562. %a will be replaced by the address, and %s will be replaced by the subject
  1563. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1564. :group 'org-link-follow
  1565. :type '(choice
  1566. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1567. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1568. (const :tag "message-mail" (message-mail "%a" "%s"))
  1569. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1570. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1571. "Non-nil means ask for confirmation before executing shell links.
  1572. Shell links can be dangerous: just think about a link
  1573. [[shell:rm -rf ~/*][Google Search]]
  1574. This link would show up in your Org-mode document as \"Google Search\",
  1575. but really it would remove your entire home directory.
  1576. Therefore we advise against setting this variable to nil.
  1577. Just change it to `y-or-n-p' if you want to confirm with a
  1578. single keystroke rather than having to type \"yes\"."
  1579. :group 'org-link-follow
  1580. :type '(choice
  1581. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1582. (const :tag "with y-or-n (faster)" y-or-n-p)
  1583. (const :tag "no confirmation (dangerous)" nil)))
  1584. (put 'org-confirm-shell-link-function
  1585. 'safe-local-variable
  1586. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1587. (defcustom org-confirm-shell-link-not-regexp ""
  1588. "A regexp to skip confirmation for shell links."
  1589. :group 'org-link-follow
  1590. :version "24.1"
  1591. :type 'regexp)
  1592. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1593. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1594. Elisp links can be dangerous: just think about a link
  1595. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1596. This link would show up in your Org-mode document as \"Google Search\",
  1597. but really it would remove your entire home directory.
  1598. Therefore we advise against setting this variable to nil.
  1599. Just change it to `y-or-n-p' if you want to confirm with a
  1600. single keystroke rather than having to type \"yes\"."
  1601. :group 'org-link-follow
  1602. :type '(choice
  1603. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1604. (const :tag "with y-or-n (faster)" y-or-n-p)
  1605. (const :tag "no confirmation (dangerous)" nil)))
  1606. (put 'org-confirm-shell-link-function
  1607. 'safe-local-variable
  1608. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1609. (defcustom org-confirm-elisp-link-not-regexp ""
  1610. "A regexp to skip confirmation for Elisp links."
  1611. :group 'org-link-follow
  1612. :version "24.1"
  1613. :type 'regexp)
  1614. (defconst org-file-apps-defaults-gnu
  1615. '((remote . emacs)
  1616. (system . mailcap)
  1617. (t . mailcap))
  1618. "Default file applications on a UNIX or GNU/Linux system.
  1619. See `org-file-apps'.")
  1620. (defconst org-file-apps-defaults-macosx
  1621. '((remote . emacs)
  1622. (t . "open %s")
  1623. (system . "open %s")
  1624. ("ps.gz" . "gv %s")
  1625. ("eps.gz" . "gv %s")
  1626. ("dvi" . "xdvi %s")
  1627. ("fig" . "xfig %s"))
  1628. "Default file applications on a MacOS X system.
  1629. The system \"open\" is known as a default, but we use X11 applications
  1630. for some files for which the OS does not have a good default.
  1631. See `org-file-apps'.")
  1632. (defconst org-file-apps-defaults-windowsnt
  1633. (list
  1634. '(remote . emacs)
  1635. (cons t
  1636. (list (if (featurep 'xemacs)
  1637. 'mswindows-shell-execute
  1638. 'w32-shell-execute)
  1639. "open" 'file))
  1640. (cons 'system
  1641. (list (if (featurep 'xemacs)
  1642. 'mswindows-shell-execute
  1643. 'w32-shell-execute)
  1644. "open" 'file)))
  1645. "Default file applications on a Windows NT system.
  1646. The system \"open\" is used for most files.
  1647. See `org-file-apps'.")
  1648. (defcustom org-file-apps
  1649. '(
  1650. (auto-mode . emacs)
  1651. ("\\.mm\\'" . default)
  1652. ("\\.x?html?\\'" . default)
  1653. ("\\.pdf\\'" . default)
  1654. )
  1655. "External applications for opening `file:path' items in a document.
  1656. Org-mode uses system defaults for different file types, but
  1657. you can use this variable to set the application for a given file
  1658. extension. The entries in this list are cons cells where the car identifies
  1659. files and the cdr the corresponding command. Possible values for the
  1660. file identifier are
  1661. \"string\" A string as a file identifier can be interpreted in different
  1662. ways, depending on its contents:
  1663. - Alphanumeric characters only:
  1664. Match links with this file extension.
  1665. Example: (\"pdf\" . \"evince %s\")
  1666. to open PDFs with evince.
  1667. - Regular expression: Match links where the
  1668. filename matches the regexp. If you want to
  1669. use groups here, use shy groups.
  1670. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1671. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1672. to open *.html and *.xhtml with firefox.
  1673. - Regular expression which contains (non-shy) groups:
  1674. Match links where the whole link, including \"::\", and
  1675. anything after that, matches the regexp.
  1676. In a custom command string, %1, %2, etc. are replaced with
  1677. the parts of the link that were matched by the groups.
  1678. For backwards compatibility, if a command string is given
  1679. that does not use any of the group matches, this case is
  1680. handled identically to the second one (i.e. match against
  1681. file name only).
  1682. In a custom lisp form, you can access the group matches with
  1683. (match-string n link).
  1684. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1685. to open [[file:document.pdf::5]] with evince at page 5.
  1686. `directory' Matches a directory
  1687. `remote' Matches a remote file, accessible through tramp or efs.
  1688. Remote files most likely should be visited through Emacs
  1689. because external applications cannot handle such paths.
  1690. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1691. so all files Emacs knows how to handle. Using this with
  1692. command `emacs' will open most files in Emacs. Beware that this
  1693. will also open html files inside Emacs, unless you add
  1694. (\"html\" . default) to the list as well.
  1695. t Default for files not matched by any of the other options.
  1696. `system' The system command to open files, like `open' on Windows
  1697. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1698. that will be selected if you call `C-c C-o' with a double
  1699. \\[universal-argument] \\[universal-argument] prefix.
  1700. Possible values for the command are:
  1701. `emacs' The file will be visited by the current Emacs process.
  1702. `default' Use the default application for this file type, which is the
  1703. association for t in the list, most likely in the system-specific
  1704. part.
  1705. This can be used to overrule an unwanted setting in the
  1706. system-specific variable.
  1707. `system' Use the system command for opening files, like \"open\".
  1708. This command is specified by the entry whose car is `system'.
  1709. Most likely, the system-specific version of this variable
  1710. does define this command, but you can overrule/replace it
  1711. here.
  1712. string A command to be executed by a shell; %s will be replaced
  1713. by the path to the file.
  1714. sexp A Lisp form which will be evaluated. The file path will
  1715. be available in the Lisp variable `file'.
  1716. For more examples, see the system specific constants
  1717. `org-file-apps-defaults-macosx'
  1718. `org-file-apps-defaults-windowsnt'
  1719. `org-file-apps-defaults-gnu'."
  1720. :group 'org-link-follow
  1721. :type '(repeat
  1722. (cons (choice :value ""
  1723. (string :tag "Extension")
  1724. (const :tag "System command to open files" system)
  1725. (const :tag "Default for unrecognized files" t)
  1726. (const :tag "Remote file" remote)
  1727. (const :tag "Links to a directory" directory)
  1728. (const :tag "Any files that have Emacs modes"
  1729. auto-mode))
  1730. (choice :value ""
  1731. (const :tag "Visit with Emacs" emacs)
  1732. (const :tag "Use default" default)
  1733. (const :tag "Use the system command" system)
  1734. (string :tag "Command")
  1735. (sexp :tag "Lisp form")))))
  1736. (defcustom org-doi-server-url "http://dx.doi.org/"
  1737. "The URL of the DOI server."
  1738. :type 'string
  1739. :version "24.3"
  1740. :group 'org-link-follow)
  1741. (defgroup org-refile nil
  1742. "Options concerning refiling entries in Org-mode."
  1743. :tag "Org Refile"
  1744. :group 'org)
  1745. (defcustom org-directory "~/org"
  1746. "Directory with org files.
  1747. This is just a default location to look for Org files. There is no need
  1748. at all to put your files into this directory. It is only used in the
  1749. following situations:
  1750. 1. When a capture template specifies a target file that is not an
  1751. absolute path. The path will then be interpreted relative to
  1752. `org-directory'
  1753. 2. When a capture note is filed away in an interactive way (when exiting the
  1754. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1755. with `org-directory' as the default path."
  1756. :group 'org-refile
  1757. :group 'org-remember
  1758. :group 'org-capture
  1759. :type 'directory)
  1760. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1761. "Default target for storing notes.
  1762. Used as a fall back file for org-remember.el and org-capture.el, for
  1763. templates that do not specify a target file."
  1764. :group 'org-refile
  1765. :group 'org-remember
  1766. :group 'org-capture
  1767. :type '(choice
  1768. (const :tag "Default from remember-data-file" nil)
  1769. file))
  1770. (defcustom org-goto-interface 'outline
  1771. "The default interface to be used for `org-goto'.
  1772. Allowed values are:
  1773. outline The interface shows an outline of the relevant file
  1774. and the correct heading is found by moving through
  1775. the outline or by searching with incremental search.
  1776. outline-path-completion Headlines in the current buffer are offered via
  1777. completion. This is the interface also used by
  1778. the refile command."
  1779. :group 'org-refile
  1780. :type '(choice
  1781. (const :tag "Outline" outline)
  1782. (const :tag "Outline-path-completion" outline-path-completion)))
  1783. (defcustom org-goto-max-level 5
  1784. "Maximum target level when running `org-goto' with refile interface."
  1785. :group 'org-refile
  1786. :type 'integer)
  1787. (defcustom org-reverse-note-order nil
  1788. "Non-nil means store new notes at the beginning of a file or entry.
  1789. When nil, new notes will be filed to the end of a file or entry.
  1790. This can also be a list with cons cells of regular expressions that
  1791. are matched against file names, and values."
  1792. :group 'org-remember
  1793. :group 'org-capture
  1794. :group 'org-refile
  1795. :type '(choice
  1796. (const :tag "Reverse always" t)
  1797. (const :tag "Reverse never" nil)
  1798. (repeat :tag "By file name regexp"
  1799. (cons regexp boolean))))
  1800. (defcustom org-log-refile nil
  1801. "Information to record when a task is refiled.
  1802. Possible values are:
  1803. nil Don't add anything
  1804. time Add a time stamp to the task
  1805. note Prompt for a note and add it with template `org-log-note-headings'
  1806. This option can also be set with on a per-file-basis with
  1807. #+STARTUP: nologrefile
  1808. #+STARTUP: logrefile
  1809. #+STARTUP: lognoterefile
  1810. You can have local logging settings for a subtree by setting the LOGGING
  1811. property to one or more of these keywords.
  1812. When bulk-refiling from the agenda, the value `note' is forbidden and
  1813. will temporarily be changed to `time'."
  1814. :group 'org-refile
  1815. :group 'org-progress
  1816. :version "24.1"
  1817. :type '(choice
  1818. (const :tag "No logging" nil)
  1819. (const :tag "Record timestamp" time)
  1820. (const :tag "Record timestamp with note." note)))
  1821. (defcustom org-refile-targets nil
  1822. "Targets for refiling entries with \\[org-refile].
  1823. This is a list of cons cells. Each cell contains:
  1824. - a specification of the files to be considered, either a list of files,
  1825. or a symbol whose function or variable value will be used to retrieve
  1826. a file name or a list of file names. If you use `org-agenda-files' for
  1827. that, all agenda files will be scanned for targets. Nil means consider
  1828. headings in the current buffer.
  1829. - A specification of how to find candidate refile targets. This may be
  1830. any of:
  1831. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1832. This tag has to be present in all target headlines, inheritance will
  1833. not be considered.
  1834. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1835. todo keyword.
  1836. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1837. headlines that are refiling targets.
  1838. - a cons cell (:level . N). Any headline of level N is considered a target.
  1839. Note that, when `org-odd-levels-only' is set, level corresponds to
  1840. order in hierarchy, not to the number of stars.
  1841. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1842. Note that, when `org-odd-levels-only' is set, level corresponds to
  1843. order in hierarchy, not to the number of stars.
  1844. Each element of this list generates a set of possible targets.
  1845. The union of these sets is presented (with completion) to
  1846. the user by `org-refile'.
  1847. You can set the variable `org-refile-target-verify-function' to a function
  1848. to verify each headline found by the simple criteria above.
  1849. When this variable is nil, all top-level headlines in the current buffer
  1850. are used, equivalent to the value `((nil . (:level . 1))'."
  1851. :group 'org-refile
  1852. :type '(repeat
  1853. (cons
  1854. (choice :value org-agenda-files
  1855. (const :tag "All agenda files" org-agenda-files)
  1856. (const :tag "Current buffer" nil)
  1857. (function) (variable) (file))
  1858. (choice :tag "Identify target headline by"
  1859. (cons :tag "Specific tag" (const :value :tag) (string))
  1860. (cons :tag "TODO keyword" (const :value :todo) (string))
  1861. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1862. (cons :tag "Level number" (const :value :level) (integer))
  1863. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1864. (defcustom org-refile-target-verify-function nil
  1865. "Function to verify if the headline at point should be a refile target.
  1866. The function will be called without arguments, with point at the
  1867. beginning of the headline. It should return t and leave point
  1868. where it is if the headline is a valid target for refiling.
  1869. If the target should not be selected, the function must return nil.
  1870. In addition to this, it may move point to a place from where the search
  1871. should be continued. For example, the function may decide that the entire
  1872. subtree of the current entry should be excluded and move point to the end
  1873. of the subtree."
  1874. :group 'org-refile
  1875. :type 'function)
  1876. (defcustom org-refile-use-cache nil
  1877. "Non-nil means cache refile targets to speed up the process.
  1878. The cache for a particular file will be updated automatically when
  1879. the buffer has been killed, or when any of the marker used for flagging
  1880. refile targets no longer points at a live buffer.
  1881. If you have added new entries to a buffer that might themselves be targets,
  1882. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1883. find that easier, `C-u C-u C-u C-c C-w'."
  1884. :group 'org-refile
  1885. :version "24.1"
  1886. :type 'boolean)
  1887. (defcustom org-refile-use-outline-path nil
  1888. "Non-nil means provide refile targets as paths.
  1889. So a level 3 headline will be available as level1/level2/level3.
  1890. When the value is `file', also include the file name (without directory)
  1891. into the path. In this case, you can also stop the completion after
  1892. the file name, to get entries inserted as top level in the file.
  1893. When `full-file-path', include the full file path."
  1894. :group 'org-refile
  1895. :type '(choice
  1896. (const :tag "Not" nil)
  1897. (const :tag "Yes" t)
  1898. (const :tag "Start with file name" file)
  1899. (const :tag "Start with full file path" full-file-path)))
  1900. (defcustom org-outline-path-complete-in-steps t
  1901. "Non-nil means complete the outline path in hierarchical steps.
  1902. When Org-mode uses the refile interface to select an outline path
  1903. \(see variable `org-refile-use-outline-path'), the completion of
  1904. the path can be done is a single go, or if can be done in steps down
  1905. the headline hierarchy. Going in steps is probably the best if you
  1906. do not use a special completion package like `ido' or `icicles'.
  1907. However, when using these packages, going in one step can be very
  1908. fast, while still showing the whole path to the entry."
  1909. :group 'org-refile
  1910. :type 'boolean)
  1911. (defcustom org-refile-allow-creating-parent-nodes nil
  1912. "Non-nil means allow to create new nodes as refile targets.
  1913. New nodes are then created by adding \"/new node name\" to the completion
  1914. of an existing node. When the value of this variable is `confirm',
  1915. new node creation must be confirmed by the user (recommended)
  1916. When nil, the completion must match an existing entry.
  1917. Note that, if the new heading is not seen by the criteria
  1918. listed in `org-refile-targets', multiple instances of the same
  1919. heading would be created by trying again to file under the new
  1920. heading."
  1921. :group 'org-refile
  1922. :type '(choice
  1923. (const :tag "Never" nil)
  1924. (const :tag "Always" t)
  1925. (const :tag "Prompt for confirmation" confirm)))
  1926. (defcustom org-refile-active-region-within-subtree nil
  1927. "Non-nil means also refile active region within a subtree.
  1928. By default `org-refile' doesn't allow refiling regions if they
  1929. don't contain a set of subtrees, but it might be convenient to
  1930. do so sometimes: in that case, the first line of the region is
  1931. converted to a headline before refiling."
  1932. :group 'org-refile
  1933. :version "24.1"
  1934. :type 'boolean)
  1935. (defgroup org-todo nil
  1936. "Options concerning TODO items in Org-mode."
  1937. :tag "Org TODO"
  1938. :group 'org)
  1939. (defgroup org-progress nil
  1940. "Options concerning Progress logging in Org-mode."
  1941. :tag "Org Progress"
  1942. :group 'org-time)
  1943. (defvar org-todo-interpretation-widgets
  1944. '((:tag "Sequence (cycling hits every state)" sequence)
  1945. (:tag "Type (cycling directly to DONE)" type))
  1946. "The available interpretation symbols for customizing `org-todo-keywords'.
  1947. Interested libraries should add to this list.")
  1948. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1949. "List of TODO entry keyword sequences and their interpretation.
  1950. \\<org-mode-map>This is a list of sequences.
  1951. Each sequence starts with a symbol, either `sequence' or `type',
  1952. indicating if the keywords should be interpreted as a sequence of
  1953. action steps, or as different types of TODO items. The first
  1954. keywords are states requiring action - these states will select a headline
  1955. for inclusion into the global TODO list Org-mode produces. If one of
  1956. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1957. signify that no further action is necessary. If \"|\" is not found,
  1958. the last keyword is treated as the only DONE state of the sequence.
  1959. The command \\[org-todo] cycles an entry through these states, and one
  1960. additional state where no keyword is present. For details about this
  1961. cycling, see the manual.
  1962. TODO keywords and interpretation can also be set on a per-file basis with
  1963. the special #+SEQ_TODO and #+TYP_TODO lines.
  1964. Each keyword can optionally specify a character for fast state selection
  1965. \(in combination with the variable `org-use-fast-todo-selection')
  1966. and specifiers for state change logging, using the same syntax that
  1967. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1968. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1969. indicates to record a time stamp each time this state is selected.
  1970. Each keyword may also specify if a timestamp or a note should be
  1971. recorded when entering or leaving the state, by adding additional
  1972. characters in the parenthesis after the keyword. This looks like this:
  1973. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1974. record only the time of the state change. With X and Y being either
  1975. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1976. Y when leaving the state if and only if the *target* state does not
  1977. define X. You may omit any of the fast-selection key or X or /Y,
  1978. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1979. For backward compatibility, this variable may also be just a list
  1980. of keywords. In this case the interpretation (sequence or type) will be
  1981. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1982. :group 'org-todo
  1983. :group 'org-keywords
  1984. :type '(choice
  1985. (repeat :tag "Old syntax, just keywords"
  1986. (string :tag "Keyword"))
  1987. (repeat :tag "New syntax"
  1988. (cons
  1989. (choice
  1990. :tag "Interpretation"
  1991. ;;Quick and dirty way to see
  1992. ;;`org-todo-interpretations'. This takes the
  1993. ;;place of item arguments
  1994. :convert-widget
  1995. (lambda (widget)
  1996. (widget-put widget
  1997. :args (mapcar
  1998. #'(lambda (x)
  1999. (widget-convert
  2000. (cons 'const x)))
  2001. org-todo-interpretation-widgets))
  2002. widget))
  2003. (repeat
  2004. (string :tag "Keyword"))))))
  2005. (defvar org-todo-keywords-1 nil
  2006. "All TODO and DONE keywords active in a buffer.")
  2007. (make-variable-buffer-local 'org-todo-keywords-1)
  2008. (defvar org-todo-keywords-for-agenda nil)
  2009. (defvar org-done-keywords-for-agenda nil)
  2010. (defvar org-drawers-for-agenda nil)
  2011. (defvar org-todo-keyword-alist-for-agenda nil)
  2012. (defvar org-tag-alist-for-agenda nil)
  2013. (defvar org-agenda-contributing-files nil)
  2014. (defvar org-not-done-keywords nil)
  2015. (make-variable-buffer-local 'org-not-done-keywords)
  2016. (defvar org-done-keywords nil)
  2017. (make-variable-buffer-local 'org-done-keywords)
  2018. (defvar org-todo-heads nil)
  2019. (make-variable-buffer-local 'org-todo-heads)
  2020. (defvar org-todo-sets nil)
  2021. (make-variable-buffer-local 'org-todo-sets)
  2022. (defvar org-todo-log-states nil)
  2023. (make-variable-buffer-local 'org-todo-log-states)
  2024. (defvar org-todo-kwd-alist nil)
  2025. (make-variable-buffer-local 'org-todo-kwd-alist)
  2026. (defvar org-todo-key-alist nil)
  2027. (make-variable-buffer-local 'org-todo-key-alist)
  2028. (defvar org-todo-key-trigger nil)
  2029. (make-variable-buffer-local 'org-todo-key-trigger)
  2030. (defcustom org-todo-interpretation 'sequence
  2031. "Controls how TODO keywords are interpreted.
  2032. This variable is in principle obsolete and is only used for
  2033. backward compatibility, if the interpretation of todo keywords is
  2034. not given already in `org-todo-keywords'. See that variable for
  2035. more information."
  2036. :group 'org-todo
  2037. :group 'org-keywords
  2038. :type '(choice (const sequence)
  2039. (const type)))
  2040. (defcustom org-use-fast-todo-selection t
  2041. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2042. This variable describes if and under what circumstances the cycling
  2043. mechanism for TODO keywords will be replaced by a single-key, direct
  2044. selection scheme.
  2045. When nil, fast selection is never used.
  2046. When the symbol `prefix', it will be used when `org-todo' is called
  2047. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2048. `C-u t' in an agenda buffer.
  2049. When t, fast selection is used by default. In this case, the prefix
  2050. argument forces cycling instead.
  2051. In all cases, the special interface is only used if access keys have
  2052. actually been assigned by the user, i.e. if keywords in the configuration
  2053. are followed by a letter in parenthesis, like TODO(t)."
  2054. :group 'org-todo
  2055. :type '(choice
  2056. (const :tag "Never" nil)
  2057. (const :tag "By default" t)
  2058. (const :tag "Only with C-u C-c C-t" prefix)))
  2059. (defcustom org-provide-todo-statistics t
  2060. "Non-nil means update todo statistics after insert and toggle.
  2061. ALL-HEADLINES means update todo statistics by including headlines
  2062. with no TODO keyword as well, counting them as not done.
  2063. A list of TODO keywords means the same, but skip keywords that are
  2064. not in this list.
  2065. When this is set, todo statistics is updated in the parent of the
  2066. current entry each time a todo state is changed."
  2067. :group 'org-todo
  2068. :type '(choice
  2069. (const :tag "Yes, only for TODO entries" t)
  2070. (const :tag "Yes, including all entries" 'all-headlines)
  2071. (repeat :tag "Yes, for TODOs in this list"
  2072. (string :tag "TODO keyword"))
  2073. (other :tag "No TODO statistics" nil)))
  2074. (defcustom org-hierarchical-todo-statistics t
  2075. "Non-nil means TODO statistics covers just direct children.
  2076. When nil, all entries in the subtree are considered.
  2077. This has only an effect if `org-provide-todo-statistics' is set.
  2078. To set this to nil for only a single subtree, use a COOKIE_DATA
  2079. property and include the word \"recursive\" into the value."
  2080. :group 'org-todo
  2081. :type 'boolean)
  2082. (defcustom org-after-todo-state-change-hook nil
  2083. "Hook which is run after the state of a TODO item was changed.
  2084. The new state (a string with a TODO keyword, or nil) is available in the
  2085. Lisp variable `org-state'."
  2086. :group 'org-todo
  2087. :type 'hook)
  2088. (defvar org-blocker-hook nil
  2089. "Hook for functions that are allowed to block a state change.
  2090. Functions in this hook should not modify the buffer.
  2091. Each function gets as its single argument a property list,
  2092. see `org-trigger-hook' for more information about this list.
  2093. If any of the functions in this hook returns nil, the state change
  2094. is blocked.")
  2095. (defvar org-trigger-hook nil
  2096. "Hook for functions that are triggered by a state change.
  2097. Each function gets as its single argument a property list with at
  2098. least the following elements:
  2099. (:type type-of-change :position pos-at-entry-start
  2100. :from old-state :to new-state)
  2101. Depending on the type, more properties may be present.
  2102. This mechanism is currently implemented for:
  2103. TODO state changes
  2104. ------------------
  2105. :type todo-state-change
  2106. :from previous state (keyword as a string), or nil, or a symbol
  2107. 'todo' or 'done', to indicate the general type of state.
  2108. :to new state, like in :from")
  2109. (defcustom org-enforce-todo-dependencies nil
  2110. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2111. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2112. be blocked if any prior sibling is not yet done.
  2113. Finally, if the parent is blocked because of ordered siblings of its own,
  2114. the child will also be blocked."
  2115. :set (lambda (var val)
  2116. (set var val)
  2117. (if val
  2118. (add-hook 'org-blocker-hook
  2119. 'org-block-todo-from-children-or-siblings-or-parent)
  2120. (remove-hook 'org-blocker-hook
  2121. 'org-block-todo-from-children-or-siblings-or-parent)))
  2122. :group 'org-todo
  2123. :type 'boolean)
  2124. (defcustom org-enforce-todo-checkbox-dependencies nil
  2125. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2126. When this is nil, checkboxes have no influence on switching TODO states.
  2127. When non-nil, you first need to check off all check boxes before the TODO
  2128. entry can be switched to DONE.
  2129. This variable needs to be set before org.el is loaded, and you need to
  2130. restart Emacs after a change to make the change effective. The only way
  2131. to change is while Emacs is running is through the customize interface."
  2132. :set (lambda (var val)
  2133. (set var val)
  2134. (if val
  2135. (add-hook 'org-blocker-hook
  2136. 'org-block-todo-from-checkboxes)
  2137. (remove-hook 'org-blocker-hook
  2138. 'org-block-todo-from-checkboxes)))
  2139. :group 'org-todo
  2140. :type 'boolean)
  2141. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2142. "Non-nil means inserting a TODO heading is treated as state change.
  2143. So when the command \\[org-insert-todo-heading] is used, state change
  2144. logging will apply if appropriate. When nil, the new TODO item will
  2145. be inserted directly, and no logging will take place."
  2146. :group 'org-todo
  2147. :type 'boolean)
  2148. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2149. "Non-nil means switching TODO states with S-cursor counts as state change.
  2150. This is the default behavior. However, setting this to nil allows a
  2151. convenient way to select a TODO state and bypass any logging associated
  2152. with that."
  2153. :group 'org-todo
  2154. :type 'boolean)
  2155. (defcustom org-todo-state-tags-triggers nil
  2156. "Tag changes that should be triggered by TODO state changes.
  2157. This is a list. Each entry is
  2158. (state-change (tag . flag) .......)
  2159. State-change can be a string with a state, and empty string to indicate the
  2160. state that has no TODO keyword, or it can be one of the symbols `todo'
  2161. or `done', meaning any not-done or done state, respectively."
  2162. :group 'org-todo
  2163. :group 'org-tags
  2164. :type '(repeat
  2165. (cons (choice :tag "When changing to"
  2166. (const :tag "Not-done state" todo)
  2167. (const :tag "Done state" done)
  2168. (string :tag "State"))
  2169. (repeat
  2170. (cons :tag "Tag action"
  2171. (string :tag "Tag")
  2172. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2173. (defcustom org-log-done nil
  2174. "Information to record when a task moves to the DONE state.
  2175. Possible values are:
  2176. nil Don't add anything, just change the keyword
  2177. time Add a time stamp to the task
  2178. note Prompt for a note and add it with template `org-log-note-headings'
  2179. This option can also be set with on a per-file-basis with
  2180. #+STARTUP: nologdone
  2181. #+STARTUP: logdone
  2182. #+STARTUP: lognotedone
  2183. You can have local logging settings for a subtree by setting the LOGGING
  2184. property to one or more of these keywords."
  2185. :group 'org-todo
  2186. :group 'org-progress
  2187. :type '(choice
  2188. (const :tag "No logging" nil)
  2189. (const :tag "Record CLOSED timestamp" time)
  2190. (const :tag "Record CLOSED timestamp with note." note)))
  2191. ;; Normalize old uses of org-log-done.
  2192. (cond
  2193. ((eq org-log-done t) (setq org-log-done 'time))
  2194. ((and (listp org-log-done) (memq 'done org-log-done))
  2195. (setq org-log-done 'note)))
  2196. (defcustom org-log-reschedule nil
  2197. "Information to record when the scheduling date of a tasks is modified.
  2198. Possible values are:
  2199. nil Don't add anything, just change the date
  2200. time Add a time stamp to the task
  2201. note Prompt for a note and add it with template `org-log-note-headings'
  2202. This option can also be set with on a per-file-basis with
  2203. #+STARTUP: nologreschedule
  2204. #+STARTUP: logreschedule
  2205. #+STARTUP: lognotereschedule"
  2206. :group 'org-todo
  2207. :group 'org-progress
  2208. :type '(choice
  2209. (const :tag "No logging" nil)
  2210. (const :tag "Record timestamp" time)
  2211. (const :tag "Record timestamp with note." note)))
  2212. (defcustom org-log-redeadline nil
  2213. "Information to record when the deadline date of a tasks is modified.
  2214. Possible values are:
  2215. nil Don't add anything, just change the date
  2216. time Add a time stamp to the task
  2217. note Prompt for a note and add it with template `org-log-note-headings'
  2218. This option can also be set with on a per-file-basis with
  2219. #+STARTUP: nologredeadline
  2220. #+STARTUP: logredeadline
  2221. #+STARTUP: lognoteredeadline
  2222. You can have local logging settings for a subtree by setting the LOGGING
  2223. property to one or more of these keywords."
  2224. :group 'org-todo
  2225. :group 'org-progress
  2226. :type '(choice
  2227. (const :tag "No logging" nil)
  2228. (const :tag "Record timestamp" time)
  2229. (const :tag "Record timestamp with note." note)))
  2230. (defcustom org-log-note-clock-out nil
  2231. "Non-nil means record a note when clocking out of an item.
  2232. This can also be configured on a per-file basis by adding one of
  2233. the following lines anywhere in the buffer:
  2234. #+STARTUP: lognoteclock-out
  2235. #+STARTUP: nolognoteclock-out"
  2236. :group 'org-todo
  2237. :group 'org-progress
  2238. :type 'boolean)
  2239. (defcustom org-log-done-with-time t
  2240. "Non-nil means the CLOSED time stamp will contain date and time.
  2241. When nil, only the date will be recorded."
  2242. :group 'org-progress
  2243. :type 'boolean)
  2244. (defcustom org-log-note-headings
  2245. '((done . "CLOSING NOTE %t")
  2246. (state . "State %-12s from %-12S %t")
  2247. (note . "Note taken on %t")
  2248. (reschedule . "Rescheduled from %S on %t")
  2249. (delschedule . "Not scheduled, was %S on %t")
  2250. (redeadline . "New deadline from %S on %t")
  2251. (deldeadline . "Removed deadline, was %S on %t")
  2252. (refile . "Refiled on %t")
  2253. (clock-out . ""))
  2254. "Headings for notes added to entries.
  2255. The value is an alist, with the car being a symbol indicating the note
  2256. context, and the cdr is the heading to be used. The heading may also be the
  2257. empty string.
  2258. %t in the heading will be replaced by a time stamp.
  2259. %T will be an active time stamp instead the default inactive one
  2260. %d will be replaced by a short-format time stamp.
  2261. %D will be replaced by an active short-format time stamp.
  2262. %s will be replaced by the new TODO state, in double quotes.
  2263. %S will be replaced by the old TODO state, in double quotes.
  2264. %u will be replaced by the user name.
  2265. %U will be replaced by the full user name.
  2266. In fact, it is not a good idea to change the `state' entry, because
  2267. agenda log mode depends on the format of these entries."
  2268. :group 'org-todo
  2269. :group 'org-progress
  2270. :type '(list :greedy t
  2271. (cons (const :tag "Heading when closing an item" done) string)
  2272. (cons (const :tag
  2273. "Heading when changing todo state (todo sequence only)"
  2274. state) string)
  2275. (cons (const :tag "Heading when just taking a note" note) string)
  2276. (cons (const :tag "Heading when clocking out" clock-out) string)
  2277. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2278. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2279. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2280. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2281. (cons (const :tag "Heading when refiling" refile) string)))
  2282. (unless (assq 'note org-log-note-headings)
  2283. (push '(note . "%t") org-log-note-headings))
  2284. (defcustom org-log-into-drawer nil
  2285. "Non-nil means insert state change notes and time stamps into a drawer.
  2286. When nil, state changes notes will be inserted after the headline and
  2287. any scheduling and clock lines, but not inside a drawer.
  2288. The value of this variable should be the name of the drawer to use.
  2289. LOGBOOK is proposed as the default drawer for this purpose, you can
  2290. also set this to a string to define the drawer of your choice.
  2291. A value of t is also allowed, representing \"LOGBOOK\".
  2292. A value of t or nil can also be set with on a per-file-basis with
  2293. #+STARTUP: logdrawer
  2294. #+STARTUP: nologdrawer
  2295. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2296. will be ignored.
  2297. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2298. a subtree."
  2299. :group 'org-todo
  2300. :group 'org-progress
  2301. :type '(choice
  2302. (const :tag "Not into a drawer" nil)
  2303. (const :tag "LOGBOOK" t)
  2304. (string :tag "Other")))
  2305. (if (fboundp 'defvaralias)
  2306. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2307. (defun org-log-into-drawer ()
  2308. "Return the value of `org-log-into-drawer', but let properties overrule.
  2309. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2310. used instead of the default value."
  2311. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2312. (cond
  2313. ((not p) org-log-into-drawer)
  2314. ((equal p "nil") nil)
  2315. ((equal p "t") "LOGBOOK")
  2316. (t p))))
  2317. (defcustom org-log-state-notes-insert-after-drawers nil
  2318. "Non-nil means insert state change notes after any drawers in entry.
  2319. Only the drawers that *immediately* follow the headline and the
  2320. deadline/scheduled line are skipped.
  2321. When nil, insert notes right after the heading and perhaps the line
  2322. with deadline/scheduling if present.
  2323. This variable will have no effect if `org-log-into-drawer' is
  2324. set."
  2325. :group 'org-todo
  2326. :group 'org-progress
  2327. :type 'boolean)
  2328. (defcustom org-log-states-order-reversed t
  2329. "Non-nil means the latest state note will be directly after heading.
  2330. When nil, the state change notes will be ordered according to time.
  2331. This option can also be set with on a per-file-basis with
  2332. #+STARTUP: logstatesreversed
  2333. #+STARTUP: nologstatesreversed"
  2334. :group 'org-todo
  2335. :group 'org-progress
  2336. :type 'boolean)
  2337. (defcustom org-todo-repeat-to-state nil
  2338. "The TODO state to which a repeater should return the repeating task.
  2339. By default this is the first task in a TODO sequence, or the previous state
  2340. in a TODO_TYP set. But you can specify another task here.
  2341. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2342. :group 'org-todo
  2343. :version "24.1"
  2344. :type '(choice (const :tag "Head of sequence" nil)
  2345. (string :tag "Specific state")))
  2346. (defcustom org-log-repeat 'time
  2347. "Non-nil means record moving through the DONE state when triggering repeat.
  2348. An auto-repeating task is immediately switched back to TODO when
  2349. marked DONE. If you are not logging state changes (by adding \"@\"
  2350. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2351. record a closing note, there will be no record of the task moving
  2352. through DONE. This variable forces taking a note anyway.
  2353. nil Don't force a record
  2354. time Record a time stamp
  2355. note Prompt for a note and add it with template `org-log-note-headings'
  2356. This option can also be set with on a per-file-basis with
  2357. #+STARTUP: nologrepeat
  2358. #+STARTUP: logrepeat
  2359. #+STARTUP: lognoterepeat
  2360. You can have local logging settings for a subtree by setting the LOGGING
  2361. property to one or more of these keywords."
  2362. :group 'org-todo
  2363. :group 'org-progress
  2364. :type '(choice
  2365. (const :tag "Don't force a record" nil)
  2366. (const :tag "Force recording the DONE state" time)
  2367. (const :tag "Force recording a note with the DONE state" note)))
  2368. (defgroup org-priorities nil
  2369. "Priorities in Org-mode."
  2370. :tag "Org Priorities"
  2371. :group 'org-todo)
  2372. (defcustom org-enable-priority-commands t
  2373. "Non-nil means priority commands are active.
  2374. When nil, these commands will be disabled, so that you never accidentally
  2375. set a priority."
  2376. :group 'org-priorities
  2377. :type 'boolean)
  2378. (defcustom org-highest-priority ?A
  2379. "The highest priority of TODO items. A character like ?A, ?B etc.
  2380. Must have a smaller ASCII number than `org-lowest-priority'."
  2381. :group 'org-priorities
  2382. :type 'character)
  2383. (defcustom org-lowest-priority ?C
  2384. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2385. Must have a larger ASCII number than `org-highest-priority'."
  2386. :group 'org-priorities
  2387. :type 'character)
  2388. (defcustom org-default-priority ?B
  2389. "The default priority of TODO items.
  2390. This is the priority an item gets if no explicit priority is given.
  2391. When starting to cycle on an empty priority the first step in the cycle
  2392. depends on `org-priority-start-cycle-with-default'. The resulting first
  2393. step priority must not exceed the range from `org-highest-priority' to
  2394. `org-lowest-priority' which means that `org-default-priority' has to be
  2395. in this range exclusive or inclusive the range boundaries. Else the
  2396. first step refuses to set the default and the second will fall back
  2397. to (depending on the command used) the highest or lowest priority."
  2398. :group 'org-priorities
  2399. :type 'character)
  2400. (defcustom org-priority-start-cycle-with-default t
  2401. "Non-nil means start with default priority when starting to cycle.
  2402. When this is nil, the first step in the cycle will be (depending on the
  2403. command used) one higher or lower than the default priority.
  2404. See also `org-default-priority'."
  2405. :group 'org-priorities
  2406. :type 'boolean)
  2407. (defcustom org-get-priority-function nil
  2408. "Function to extract the priority from a string.
  2409. The string is normally the headline. If this is nil Org computes the
  2410. priority from the priority cookie like [#A] in the headline. It returns
  2411. an integer, increasing by 1000 for each priority level.
  2412. The user can set a different function here, which should take a string
  2413. as an argument and return the numeric priority."
  2414. :group 'org-priorities
  2415. :version "24.1"
  2416. :type 'function)
  2417. (defgroup org-time nil
  2418. "Options concerning time stamps and deadlines in Org-mode."
  2419. :tag "Org Time"
  2420. :group 'org)
  2421. (defcustom org-insert-labeled-timestamps-at-point nil
  2422. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2423. When nil, these labeled time stamps are forces into the second line of an
  2424. entry, just after the headline. When scheduling from the global TODO list,
  2425. the time stamp will always be forced into the second line."
  2426. :group 'org-time
  2427. :type 'boolean)
  2428. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2429. "Formats for `format-time-string' which are used for time stamps.
  2430. It is not recommended to change this constant.")
  2431. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2432. "Number of minutes to round time stamps to.
  2433. These are two values, the first applies when first creating a time stamp.
  2434. The second applies when changing it with the commands `S-up' and `S-down'.
  2435. When changing the time stamp, this means that it will change in steps
  2436. of N minutes, as given by the second value.
  2437. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2438. numbers should be factors of 60, so for example 5, 10, 15.
  2439. When this is larger than 1, you can still force an exact time stamp by using
  2440. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2441. and by using a prefix arg to `S-up/down' to specify the exact number
  2442. of minutes to shift."
  2443. :group 'org-time
  2444. :get #'(lambda (var) ; Make sure both elements are there
  2445. (if (integerp (default-value var))
  2446. (list (default-value var) 5)
  2447. (default-value var)))
  2448. :type '(list
  2449. (integer :tag "when inserting times")
  2450. (integer :tag "when modifying times")))
  2451. ;; Normalize old customizations of this variable.
  2452. (when (integerp org-time-stamp-rounding-minutes)
  2453. (setq org-time-stamp-rounding-minutes
  2454. (list org-time-stamp-rounding-minutes
  2455. org-time-stamp-rounding-minutes)))
  2456. (defcustom org-display-custom-times nil
  2457. "Non-nil means overlay custom formats over all time stamps.
  2458. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2459. To turn this on on a per-file basis, insert anywhere in the file:
  2460. #+STARTUP: customtime"
  2461. :group 'org-time
  2462. :set 'set-default
  2463. :type 'sexp)
  2464. (make-variable-buffer-local 'org-display-custom-times)
  2465. (defcustom org-time-stamp-custom-formats
  2466. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2467. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2468. These are overlaid over the default ISO format if the variable
  2469. `org-display-custom-times' is set. Time like %H:%M should be at the
  2470. end of the second format. The custom formats are also honored by export
  2471. commands, if custom time display is turned on at the time of export."
  2472. :group 'org-time
  2473. :type 'sexp)
  2474. (defun org-time-stamp-format (&optional long inactive)
  2475. "Get the right format for a time string."
  2476. (let ((f (if long (cdr org-time-stamp-formats)
  2477. (car org-time-stamp-formats))))
  2478. (if inactive
  2479. (concat "[" (substring f 1 -1) "]")
  2480. f)))
  2481. (defcustom org-time-clocksum-format
  2482. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2483. "The format string used when creating CLOCKSUM lines.
  2484. This is also used when Org mode generates a time duration.
  2485. The value can be a single format string containing two
  2486. %-sequences, which will be filled with the number of hours and
  2487. minutes in that order.
  2488. Alternatively, the value can be a plist associating any of the
  2489. keys :years, :months, :weeks, :days, :hours or :minutes with
  2490. format strings. The time duration is formatted using only the
  2491. time components that are needed and concatenating the results.
  2492. If a time unit in absent, it falls back to the next smallest
  2493. unit.
  2494. The keys :require-years, :require-months, :require-days,
  2495. :require-weeks, :require-hours, :require-minutes are also
  2496. meaningful. A non-nil value for these keys indicates that the
  2497. corresponding time component should always be included, even if
  2498. its value is 0.
  2499. For example,
  2500. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2501. :require-minutes t)
  2502. means durations longer than a day will be expressed in days,
  2503. hours and minutes, and durations less than a day will always be
  2504. expressed in hours and minutes (even for durations less than an
  2505. hour).
  2506. The value
  2507. \(:days \"%dd\" :minutes \"%dm\")
  2508. means durations longer than a day will be expressed in days and
  2509. minutes, and durations less than a day will be expressed entirely
  2510. in minutes (even for durations longer than an hour)."
  2511. :group 'org-time
  2512. :group 'org-clock
  2513. :version "24.4"
  2514. :package-version '(Org . "8.0")
  2515. :type '(choice (string :tag "Format string")
  2516. (set :tag "Plist"
  2517. (group :inline t (const :tag "Years" :years)
  2518. (string :tag "Format string"))
  2519. (group :inline t
  2520. (const :tag "Always show years" :require-years)
  2521. (const t))
  2522. (group :inline t (const :tag "Months" :months)
  2523. (string :tag "Format string"))
  2524. (group :inline t
  2525. (const :tag "Always show months" :require-months)
  2526. (const t))
  2527. (group :inline t (const :tag "Weeks" :weeks)
  2528. (string :tag "Format string"))
  2529. (group :inline t
  2530. (const :tag "Always show weeks" :require-weeks)
  2531. (const t))
  2532. (group :inline t (const :tag "Days" :days)
  2533. (string :tag "Format string"))
  2534. (group :inline t
  2535. (const :tag "Always show days" :require-days)
  2536. (const t))
  2537. (group :inline t (const :tag "Hours" :hours)
  2538. (string :tag "Format string"))
  2539. (group :inline t
  2540. (const :tag "Always show hours" :require-hours)
  2541. (const t))
  2542. (group :inline t (const :tag "Minutes" :minutes)
  2543. (string :tag "Format string"))
  2544. (group :inline t
  2545. (const :tag "Always show minutes" :require-minutes)
  2546. (const t)))))
  2547. (defcustom org-time-clocksum-use-fractional nil
  2548. "When non-nil, \\[org-clock-display] uses fractional times.
  2549. See `org-time-clocksum-format' for more on time clock formats."
  2550. :group 'org-time
  2551. :group 'org-clock
  2552. :version "24.3"
  2553. :type 'boolean)
  2554. (defcustom org-time-clocksum-use-effort-durations t
  2555. "When non-nil, \\[org-clock-display] uses effort durations.
  2556. E.g. by default, one day is considered to be a 8 hours effort,
  2557. so a task that has been clocked for 16 hours will be displayed
  2558. as during 2 days in the clock display or in the clocktable.
  2559. See `org-effort-durations' on how to set effort durations
  2560. and `org-time-clocksum-format' for more on time clock formats."
  2561. :group 'org-time
  2562. :group 'org-clock
  2563. :version "24.4"
  2564. :package-version '(Org . "8.0")
  2565. :type 'boolean)
  2566. (defcustom org-time-clocksum-fractional-format "%.2f"
  2567. "The format string used when creating CLOCKSUM lines,
  2568. or when Org mode generates a time duration, if
  2569. `org-time-clocksum-use-fractional' is enabled.
  2570. The value can be a single format string containing one
  2571. %-sequence, which will be filled with the number of hours as
  2572. a float.
  2573. Alternatively, the value can be a plist associating any of the
  2574. keys :years, :months, :weeks, :days, :hours or :minutes with
  2575. a format string. The time duration is formatted using the
  2576. largest time unit which gives a non-zero integer part. If all
  2577. specified formats have zero integer part, the smallest time unit
  2578. is used."
  2579. :group 'org-time
  2580. :type '(choice (string :tag "Format string")
  2581. (set (group :inline t (const :tag "Years" :years)
  2582. (string :tag "Format string"))
  2583. (group :inline t (const :tag "Months" :months)
  2584. (string :tag "Format string"))
  2585. (group :inline t (const :tag "Weeks" :weeks)
  2586. (string :tag "Format string"))
  2587. (group :inline t (const :tag "Days" :days)
  2588. (string :tag "Format string"))
  2589. (group :inline t (const :tag "Hours" :hours)
  2590. (string :tag "Format string"))
  2591. (group :inline t (const :tag "Minutes" :minutes)
  2592. (string :tag "Format string")))))
  2593. (defcustom org-deadline-warning-days 14
  2594. "Number of days before expiration during which a deadline becomes active.
  2595. This variable governs the display in sparse trees and in the agenda.
  2596. When 0 or negative, it means use this number (the absolute value of it)
  2597. even if a deadline has a different individual lead time specified.
  2598. Custom commands can set this variable in the options section."
  2599. :group 'org-time
  2600. :group 'org-agenda-daily/weekly
  2601. :type 'integer)
  2602. (defcustom org-scheduled-delay-days 0
  2603. "Number of days before a scheduled item becomes active.
  2604. This variable governs the display in sparse trees and in the agenda.
  2605. The default value (i.e. 0) means: don't delay scheduled item.
  2606. When negative, it means use this number (the absolute value of it)
  2607. even if a scheduled item has a different individual delay time
  2608. specified.
  2609. Custom commands can set this variable in the options section."
  2610. :group 'org-time
  2611. :group 'org-agenda-daily/weekly
  2612. :version "24.4"
  2613. :package-version '(Org . "8.0")
  2614. :type 'integer)
  2615. (defcustom org-read-date-prefer-future t
  2616. "Non-nil means assume future for incomplete date input from user.
  2617. This affects the following situations:
  2618. 1. The user gives a month but not a year.
  2619. For example, if it is April and you enter \"feb 2\", this will be read
  2620. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2621. 2. The user gives a day, but no month.
  2622. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2623. read this as the third of *next* month. However, if you enter \"17\",
  2624. it will be considered as *this* month.
  2625. If you set this variable to the symbol `time', then also the following
  2626. will work:
  2627. 3. If the user gives a time.
  2628. If the time is before now, it will be interpreted as tomorrow.
  2629. Currently none of this works for ISO week specifications.
  2630. When this option is nil, the current day, month and year will always be
  2631. used as defaults.
  2632. See also `org-agenda-jump-prefer-future'."
  2633. :group 'org-time
  2634. :type '(choice
  2635. (const :tag "Never" nil)
  2636. (const :tag "Check month and day" t)
  2637. (const :tag "Check month, day, and time" time)))
  2638. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2639. "Should the agenda jump command prefer the future for incomplete dates?
  2640. The default is to do the same as configured in `org-read-date-prefer-future'.
  2641. But you can also set a deviating value here.
  2642. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2643. :group 'org-agenda
  2644. :group 'org-time
  2645. :version "24.1"
  2646. :type '(choice
  2647. (const :tag "Use org-read-date-prefer-future"
  2648. org-read-date-prefer-future)
  2649. (const :tag "Never" nil)
  2650. (const :tag "Always" t)))
  2651. (defcustom org-read-date-force-compatible-dates t
  2652. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2653. Depending on the system Emacs is running on, certain dates cannot
  2654. be represented with the type used internally to represent time.
  2655. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2656. correctly. Some systems allow for earlier dates, some for later,
  2657. some for both. One way to find out it to insert any date into an
  2658. Org buffer, putting the cursor on the year and hitting S-up and
  2659. S-down to test the range.
  2660. When this variable is set to t, the date/time prompt will not let
  2661. you specify dates outside the 1970-2037 range, so it is certain that
  2662. these dates will work in whatever version of Emacs you are
  2663. running, and also that you can move a file from one Emacs implementation
  2664. to another. WHenever Org is forcing the year for you, it will display
  2665. a message and beep.
  2666. When this variable is nil, Org will check if the date is
  2667. representable in the specific Emacs implementation you are using.
  2668. If not, it will force a year, usually the current year, and beep
  2669. to remind you. Currently this setting is not recommended because
  2670. the likelihood that you will open your Org files in an Emacs that
  2671. has limited date range is not negligible.
  2672. A workaround for this problem is to use diary sexp dates for time
  2673. stamps outside of this range."
  2674. :group 'org-time
  2675. :version "24.1"
  2676. :type 'boolean)
  2677. (defcustom org-read-date-display-live t
  2678. "Non-nil means display current interpretation of date prompt live.
  2679. This display will be in an overlay, in the minibuffer."
  2680. :group 'org-time
  2681. :type 'boolean)
  2682. (defcustom org-read-date-popup-calendar t
  2683. "Non-nil means pop up a calendar when prompting for a date.
  2684. In the calendar, the date can be selected with mouse-1. However, the
  2685. minibuffer will also be active, and you can simply enter the date as well.
  2686. When nil, only the minibuffer will be available."
  2687. :group 'org-time
  2688. :type 'boolean)
  2689. (if (fboundp 'defvaralias)
  2690. (defvaralias 'org-popup-calendar-for-date-prompt
  2691. 'org-read-date-popup-calendar))
  2692. (make-obsolete-variable
  2693. 'org-read-date-minibuffer-setup-hook
  2694. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2695. (defcustom org-read-date-minibuffer-setup-hook nil
  2696. "Hook to be used to set up keys for the date/time interface.
  2697. Add key definitions to `minibuffer-local-map', which will be a
  2698. temporary copy.
  2699. WARNING: This option is obsolete, you should use
  2700. `org-read-date-minibuffer-local-map' to set up keys."
  2701. :group 'org-time
  2702. :type 'hook)
  2703. (defcustom org-extend-today-until 0
  2704. "The hour when your day really ends. Must be an integer.
  2705. This has influence for the following applications:
  2706. - When switching the agenda to \"today\". It it is still earlier than
  2707. the time given here, the day recognized as TODAY is actually yesterday.
  2708. - When a date is read from the user and it is still before the time given
  2709. here, the current date and time will be assumed to be yesterday, 23:59.
  2710. Also, timestamps inserted in capture templates follow this rule.
  2711. IMPORTANT: This is a feature whose implementation is and likely will
  2712. remain incomplete. Really, it is only here because past midnight seems to
  2713. be the favorite working time of John Wiegley :-)"
  2714. :group 'org-time
  2715. :type 'integer)
  2716. (defcustom org-use-effective-time nil
  2717. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2718. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2719. \"effective time\" of any timestamps between midnight and 8am will be
  2720. 23:59 of the previous day."
  2721. :group 'org-time
  2722. :version "24.1"
  2723. :type 'boolean)
  2724. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2725. "When non-nil, use the last clock out time for `org-todo'.
  2726. Note that this option has precedence over the combined use of
  2727. `org-use-effective-time' and `org-extend-today-until'."
  2728. :group 'org-time
  2729. :version "24.4"
  2730. :package-version '(Org . "8.0")
  2731. :type 'boolean)
  2732. (defcustom org-edit-timestamp-down-means-later nil
  2733. "Non-nil means S-down will increase the time in a time stamp.
  2734. When nil, S-up will increase."
  2735. :group 'org-time
  2736. :type 'boolean)
  2737. (defcustom org-calendar-follow-timestamp-change t
  2738. "Non-nil means make the calendar window follow timestamp changes.
  2739. When a timestamp is modified and the calendar window is visible, it will be
  2740. moved to the new date."
  2741. :group 'org-time
  2742. :type 'boolean)
  2743. (defgroup org-tags nil
  2744. "Options concerning tags in Org-mode."
  2745. :tag "Org Tags"
  2746. :group 'org)
  2747. (defcustom org-tag-alist nil
  2748. "List of tags allowed in Org-mode files.
  2749. When this list is nil, Org-mode will base TAG input on what is already in the
  2750. buffer.
  2751. The value of this variable is an alist, the car of each entry must be a
  2752. keyword as a string, the cdr may be a character that is used to select
  2753. that tag through the fast-tag-selection interface.
  2754. See the manual for details."
  2755. :group 'org-tags
  2756. :type '(repeat
  2757. (choice
  2758. (cons (string :tag "Tag name")
  2759. (character :tag "Access char"))
  2760. (list :tag "Start radio group"
  2761. (const :startgroup)
  2762. (option (string :tag "Group description")))
  2763. (list :tag "End radio group"
  2764. (const :endgroup)
  2765. (option (string :tag "Group description")))
  2766. (const :tag "New line" (:newline)))))
  2767. (defcustom org-tag-persistent-alist nil
  2768. "List of tags that will always appear in all Org-mode files.
  2769. This is in addition to any in buffer settings or customizations
  2770. of `org-tag-alist'.
  2771. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2772. The value of this variable is an alist, the car of each entry must be a
  2773. keyword as a string, the cdr may be a character that is used to select
  2774. that tag through the fast-tag-selection interface.
  2775. See the manual for details.
  2776. To disable these tags on a per-file basis, insert anywhere in the file:
  2777. #+STARTUP: noptag"
  2778. :group 'org-tags
  2779. :type '(repeat
  2780. (choice
  2781. (cons (string :tag "Tag name")
  2782. (character :tag "Access char"))
  2783. (const :tag "Start radio group" (:startgroup))
  2784. (const :tag "End radio group" (:endgroup))
  2785. (const :tag "New line" (:newline)))))
  2786. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2787. "If non-nil, always offer completion for all tags of all agenda files.
  2788. Instead of customizing this variable directly, you might want to
  2789. set it locally for capture buffers, because there no list of
  2790. tags in that file can be created dynamically (there are none).
  2791. (add-hook 'org-capture-mode-hook
  2792. (lambda ()
  2793. (set (make-local-variable
  2794. 'org-complete-tags-always-offer-all-agenda-tags)
  2795. t)))"
  2796. :group 'org-tags
  2797. :version "24.1"
  2798. :type 'boolean)
  2799. (defvar org-file-tags nil
  2800. "List of tags that can be inherited by all entries in the file.
  2801. The tags will be inherited if the variable `org-use-tag-inheritance'
  2802. says they should be.
  2803. This variable is populated from #+FILETAGS lines.")
  2804. (defcustom org-use-fast-tag-selection 'auto
  2805. "Non-nil means use fast tag selection scheme.
  2806. This is a special interface to select and deselect tags with single keys.
  2807. When nil, fast selection is never used.
  2808. When the symbol `auto', fast selection is used if and only if selection
  2809. characters for tags have been configured, either through the variable
  2810. `org-tag-alist' or through a #+TAGS line in the buffer.
  2811. When t, fast selection is always used and selection keys are assigned
  2812. automatically if necessary."
  2813. :group 'org-tags
  2814. :type '(choice
  2815. (const :tag "Always" t)
  2816. (const :tag "Never" nil)
  2817. (const :tag "When selection characters are configured" 'auto)))
  2818. (defcustom org-fast-tag-selection-single-key nil
  2819. "Non-nil means fast tag selection exits after first change.
  2820. When nil, you have to press RET to exit it.
  2821. During fast tag selection, you can toggle this flag with `C-c'.
  2822. This variable can also have the value `expert'. In this case, the window
  2823. displaying the tags menu is not even shown, until you press C-c again."
  2824. :group 'org-tags
  2825. :type '(choice
  2826. (const :tag "No" nil)
  2827. (const :tag "Yes" t)
  2828. (const :tag "Expert" expert)))
  2829. (defvar org-fast-tag-selection-include-todo nil
  2830. "Non-nil means fast tags selection interface will also offer TODO states.
  2831. This is an undocumented feature, you should not rely on it.")
  2832. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2833. "The column to which tags should be indented in a headline.
  2834. If this number is positive, it specifies the column. If it is negative,
  2835. it means that the tags should be flushright to that column. For example,
  2836. -80 works well for a normal 80 character screen.
  2837. When 0, place tags directly after headline text, with only one space in
  2838. between."
  2839. :group 'org-tags
  2840. :type 'integer)
  2841. (defcustom org-auto-align-tags t
  2842. "Non-nil keeps tags aligned when modifying headlines.
  2843. Some operations (i.e. demoting) change the length of a headline and
  2844. therefore shift the tags around. With this option turned on, after
  2845. each such operation the tags are again aligned to `org-tags-column'."
  2846. :group 'org-tags
  2847. :type 'boolean)
  2848. (defcustom org-use-tag-inheritance t
  2849. "Non-nil means tags in levels apply also for sublevels.
  2850. When nil, only the tags directly given in a specific line apply there.
  2851. This may also be a list of tags that should be inherited, or a regexp that
  2852. matches tags that should be inherited. Additional control is possible
  2853. with the variable `org-tags-exclude-from-inheritance' which gives an
  2854. explicit list of tags to be excluded from inheritance, even if the value of
  2855. `org-use-tag-inheritance' would select it for inheritance.
  2856. If this option is t, a match early-on in a tree can lead to a large
  2857. number of matches in the subtree when constructing the agenda or creating
  2858. a sparse tree. If you only want to see the first match in a tree during
  2859. a search, check out the variable `org-tags-match-list-sublevels'."
  2860. :group 'org-tags
  2861. :type '(choice
  2862. (const :tag "Not" nil)
  2863. (const :tag "Always" t)
  2864. (repeat :tag "Specific tags" (string :tag "Tag"))
  2865. (regexp :tag "Tags matched by regexp")))
  2866. (defcustom org-tags-exclude-from-inheritance nil
  2867. "List of tags that should never be inherited.
  2868. This is a way to exclude a few tags from inheritance. For way to do
  2869. the opposite, to actively allow inheritance for selected tags,
  2870. see the variable `org-use-tag-inheritance'."
  2871. :group 'org-tags
  2872. :type '(repeat (string :tag "Tag")))
  2873. (defun org-tag-inherit-p (tag)
  2874. "Check if TAG is one that should be inherited."
  2875. (cond
  2876. ((member tag org-tags-exclude-from-inheritance) nil)
  2877. ((eq org-use-tag-inheritance t) t)
  2878. ((not org-use-tag-inheritance) nil)
  2879. ((stringp org-use-tag-inheritance)
  2880. (string-match org-use-tag-inheritance tag))
  2881. ((listp org-use-tag-inheritance)
  2882. (member tag org-use-tag-inheritance))
  2883. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2884. (defcustom org-tags-match-list-sublevels t
  2885. "Non-nil means list also sublevels of headlines matching a search.
  2886. This variable applies to tags/property searches, and also to stuck
  2887. projects because this search is based on a tags match as well.
  2888. When set to the symbol `indented', sublevels are indented with
  2889. leading dots.
  2890. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2891. the sublevels of a headline matching a tag search often also match
  2892. the same search. Listing all of them can create very long lists.
  2893. Setting this variable to nil causes subtrees of a match to be skipped.
  2894. This variable is semi-obsolete and probably should always be true. It
  2895. is better to limit inheritance to certain tags using the variables
  2896. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2897. :group 'org-tags
  2898. :type '(choice
  2899. (const :tag "No, don't list them" nil)
  2900. (const :tag "Yes, do list them" t)
  2901. (const :tag "List them, indented with leading dots" indented)))
  2902. (defcustom org-tags-sort-function nil
  2903. "When set, tags are sorted using this function as a comparator."
  2904. :group 'org-tags
  2905. :type '(choice
  2906. (const :tag "No sorting" nil)
  2907. (const :tag "Alphabetical" string<)
  2908. (const :tag "Reverse alphabetical" string>)
  2909. (function :tag "Custom function" nil)))
  2910. (defvar org-tags-history nil
  2911. "History of minibuffer reads for tags.")
  2912. (defvar org-last-tags-completion-table nil
  2913. "The last used completion table for tags.")
  2914. (defvar org-after-tags-change-hook nil
  2915. "Hook that is run after the tags in a line have changed.")
  2916. (defgroup org-properties nil
  2917. "Options concerning properties in Org-mode."
  2918. :tag "Org Properties"
  2919. :group 'org)
  2920. (defcustom org-property-format "%-10s %s"
  2921. "How property key/value pairs should be formatted by `indent-line'.
  2922. When `indent-line' hits a property definition, it will format the line
  2923. according to this format, mainly to make sure that the values are
  2924. lined-up with respect to each other."
  2925. :group 'org-properties
  2926. :type 'string)
  2927. (defcustom org-properties-postprocess-alist nil
  2928. "Alist of properties and functions to adjust inserted values.
  2929. Elements of this alist must be of the form
  2930. ([string] [function])
  2931. where [string] must be a property name and [function] must be a
  2932. lambda expression: this lambda expression must take one argument,
  2933. the value to adjust, and return the new value as a string.
  2934. For example, this element will allow the property \"Remaining\"
  2935. to be updated wrt the relation between the \"Effort\" property
  2936. and the clock summary:
  2937. ((\"Remaining\" (lambda(value)
  2938. (let ((clocksum (org-clock-sum-current-item))
  2939. (effort (org-duration-string-to-minutes
  2940. (org-entry-get (point) \"Effort\"))))
  2941. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2942. :group 'org-properties
  2943. :version "24.1"
  2944. :type '(alist :key-type (string :tag "Property")
  2945. :value-type (function :tag "Function")))
  2946. (defcustom org-use-property-inheritance nil
  2947. "Non-nil means properties apply also for sublevels.
  2948. This setting is chiefly used during property searches. Turning it on can
  2949. cause significant overhead when doing a search, which is why it is not
  2950. on by default.
  2951. When nil, only the properties directly given in the current entry count.
  2952. When t, every property is inherited. The value may also be a list of
  2953. properties that should have inheritance, or a regular expression matching
  2954. properties that should be inherited.
  2955. However, note that some special properties use inheritance under special
  2956. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2957. and the properties ending in \"_ALL\" when they are used as descriptor
  2958. for valid values of a property.
  2959. Note for programmers:
  2960. When querying an entry with `org-entry-get', you can control if inheritance
  2961. should be used. By default, `org-entry-get' looks only at the local
  2962. properties. You can request inheritance by setting the inherit argument
  2963. to t (to force inheritance) or to `selective' (to respect the setting
  2964. in this variable)."
  2965. :group 'org-properties
  2966. :type '(choice
  2967. (const :tag "Not" nil)
  2968. (const :tag "Always" t)
  2969. (repeat :tag "Specific properties" (string :tag "Property"))
  2970. (regexp :tag "Properties matched by regexp")))
  2971. (defun org-property-inherit-p (property)
  2972. "Check if PROPERTY is one that should be inherited."
  2973. (cond
  2974. ((eq org-use-property-inheritance t) t)
  2975. ((not org-use-property-inheritance) nil)
  2976. ((stringp org-use-property-inheritance)
  2977. (string-match org-use-property-inheritance property))
  2978. ((listp org-use-property-inheritance)
  2979. (member property org-use-property-inheritance))
  2980. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2981. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2982. "The default column format, if no other format has been defined.
  2983. This variable can be set on the per-file basis by inserting a line
  2984. #+COLUMNS: %25ITEM ....."
  2985. :group 'org-properties
  2986. :type 'string)
  2987. (defcustom org-columns-ellipses ".."
  2988. "The ellipses to be used when a field in column view is truncated.
  2989. When this is the empty string, as many characters as possible are shown,
  2990. but then there will be no visual indication that the field has been truncated.
  2991. When this is a string of length N, the last N characters of a truncated
  2992. field are replaced by this string. If the column is narrower than the
  2993. ellipses string, only part of the ellipses string will be shown."
  2994. :group 'org-properties
  2995. :type 'string)
  2996. (defcustom org-columns-modify-value-for-display-function nil
  2997. "Function that modifies values for display in column view.
  2998. For example, it can be used to cut out a certain part from a time stamp.
  2999. The function must take 2 arguments:
  3000. column-title The title of the column (*not* the property name)
  3001. value The value that should be modified.
  3002. The function should return the value that should be displayed,
  3003. or nil if the normal value should be used."
  3004. :group 'org-properties
  3005. :type 'function)
  3006. (defcustom org-effort-property "Effort"
  3007. "The property that is being used to keep track of effort estimates.
  3008. Effort estimates given in this property need to have the format H:MM."
  3009. :group 'org-properties
  3010. :group 'org-progress
  3011. :type '(string :tag "Property"))
  3012. (defconst org-global-properties-fixed
  3013. '(("VISIBILITY_ALL" . "folded children content all")
  3014. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3015. "List of property/value pairs that can be inherited by any entry.
  3016. These are fixed values, for the preset properties. The user variable
  3017. that can be used to add to this list is `org-global-properties'.
  3018. The entries in this list are cons cells where the car is a property
  3019. name and cdr is a string with the value. If the value represents
  3020. multiple items like an \"_ALL\" property, separate the items by
  3021. spaces.")
  3022. (defcustom org-global-properties nil
  3023. "List of property/value pairs that can be inherited by any entry.
  3024. This list will be combined with the constant `org-global-properties-fixed'.
  3025. The entries in this list are cons cells where the car is a property
  3026. name and cdr is a string with the value.
  3027. You can set buffer-local values for the same purpose in the variable
  3028. `org-file-properties' this by adding lines like
  3029. #+PROPERTY: NAME VALUE"
  3030. :group 'org-properties
  3031. :type '(repeat
  3032. (cons (string :tag "Property")
  3033. (string :tag "Value"))))
  3034. (defvar org-file-properties nil
  3035. "List of property/value pairs that can be inherited by any entry.
  3036. Valid for the current buffer.
  3037. This variable is populated from #+PROPERTY lines.")
  3038. (make-variable-buffer-local 'org-file-properties)
  3039. (defgroup org-agenda nil
  3040. "Options concerning agenda views in Org-mode."
  3041. :tag "Org Agenda"
  3042. :group 'org)
  3043. (defvar org-category nil
  3044. "Variable used by org files to set a category for agenda display.
  3045. Such files should use a file variable to set it, for example
  3046. # -*- mode: org; org-category: \"ELisp\"
  3047. or contain a special line
  3048. #+CATEGORY: ELisp
  3049. If the file does not specify a category, then file's base name
  3050. is used instead.")
  3051. (make-variable-buffer-local 'org-category)
  3052. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3053. (defcustom org-agenda-files nil
  3054. "The files to be used for agenda display.
  3055. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3056. \\[org-remove-file]. You can also use customize to edit the list.
  3057. If an entry is a directory, all files in that directory that are matched by
  3058. `org-agenda-file-regexp' will be part of the file list.
  3059. If the value of the variable is not a list but a single file name, then
  3060. the list of agenda files is actually stored and maintained in that file, one
  3061. agenda file per line. In this file paths can be given relative to
  3062. `org-directory'. Tilde expansion and environment variable substitution
  3063. are also made."
  3064. :group 'org-agenda
  3065. :type '(choice
  3066. (repeat :tag "List of files and directories" file)
  3067. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3068. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3069. "Regular expression to match files for `org-agenda-files'.
  3070. If any element in the list in that variable contains a directory instead
  3071. of a normal file, all files in that directory that are matched by this
  3072. regular expression will be included."
  3073. :group 'org-agenda
  3074. :type 'regexp)
  3075. (defcustom org-agenda-text-search-extra-files nil
  3076. "List of extra files to be searched by text search commands.
  3077. These files will be search in addition to the agenda files by the
  3078. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3079. Note that these files will only be searched for text search commands,
  3080. not for the other agenda views like todo lists, tag searches or the weekly
  3081. agenda. This variable is intended to list notes and possibly archive files
  3082. that should also be searched by these two commands.
  3083. In fact, if the first element in the list is the symbol `agenda-archives',
  3084. than all archive files of all agenda files will be added to the search
  3085. scope."
  3086. :group 'org-agenda
  3087. :type '(set :greedy t
  3088. (const :tag "Agenda Archives" agenda-archives)
  3089. (repeat :inline t (file))))
  3090. (if (fboundp 'defvaralias)
  3091. (defvaralias 'org-agenda-multi-occur-extra-files
  3092. 'org-agenda-text-search-extra-files))
  3093. (defcustom org-agenda-skip-unavailable-files nil
  3094. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3095. A nil value means to remove them, after a query, from the list."
  3096. :group 'org-agenda
  3097. :type 'boolean)
  3098. (defcustom org-calendar-to-agenda-key [?c]
  3099. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3100. The command `org-calendar-goto-agenda' will be bound to this key. The
  3101. default is the character `c' because then `c' can be used to switch back and
  3102. forth between agenda and calendar."
  3103. :group 'org-agenda
  3104. :type 'sexp)
  3105. (defcustom org-calendar-insert-diary-entry-key [?i]
  3106. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3107. This option is irrelevant until `org-agenda-diary-file' has been configured
  3108. to point to an Org-mode file. When that is the case, the command
  3109. `org-agenda-diary-entry' will be bound to the key given here, by default
  3110. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3111. if you want to continue doing this, you need to change this to a different
  3112. key."
  3113. :group 'org-agenda
  3114. :type 'sexp)
  3115. (defcustom org-agenda-diary-file 'diary-file
  3116. "File to which to add new entries with the `i' key in agenda and calendar.
  3117. When this is the symbol `diary-file', the functionality in the Emacs
  3118. calendar will be used to add entries to the `diary-file'. But when this
  3119. points to a file, `org-agenda-diary-entry' will be used instead."
  3120. :group 'org-agenda
  3121. :type '(choice
  3122. (const :tag "The standard Emacs diary file" diary-file)
  3123. (file :tag "Special Org file diary entries")))
  3124. (eval-after-load "calendar"
  3125. '(progn
  3126. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3127. 'org-calendar-goto-agenda)
  3128. (add-hook 'calendar-mode-hook
  3129. (lambda ()
  3130. (unless (eq org-agenda-diary-file 'diary-file)
  3131. (define-key calendar-mode-map
  3132. org-calendar-insert-diary-entry-key
  3133. 'org-agenda-diary-entry))))))
  3134. (defgroup org-latex nil
  3135. "Options for embedding LaTeX code into Org-mode."
  3136. :tag "Org LaTeX"
  3137. :group 'org)
  3138. (defcustom org-format-latex-options
  3139. '(:foreground default :background default :scale 1.0
  3140. :html-foreground "Black" :html-background "Transparent"
  3141. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3142. "Options for creating images from LaTeX fragments.
  3143. This is a property list with the following properties:
  3144. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3145. `default' means use the foreground of the default face.
  3146. `auto' means use the foreground from the text face.
  3147. :background the background color, or \"Transparent\".
  3148. `default' means use the background of the default face.
  3149. `auto' means use the background from the text face.
  3150. :scale a scaling factor for the size of the images, to get more pixels
  3151. :html-foreground, :html-background, :html-scale
  3152. the same numbers for HTML export.
  3153. :matchers a list indicating which matchers should be used to
  3154. find LaTeX fragments. Valid members of this list are:
  3155. \"begin\" find environments
  3156. \"$1\" find single characters surrounded by $.$
  3157. \"$\" find math expressions surrounded by $...$
  3158. \"$$\" find math expressions surrounded by $$....$$
  3159. \"\\(\" find math expressions surrounded by \\(...\\)
  3160. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3161. :group 'org-latex
  3162. :type 'plist)
  3163. (defcustom org-format-latex-signal-error t
  3164. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3165. When nil, just push out a message."
  3166. :group 'org-latex
  3167. :version "24.1"
  3168. :type 'boolean)
  3169. (defcustom org-latex-to-mathml-jar-file nil
  3170. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3171. Use this to specify additional executable file say a jar file.
  3172. When using MathToWeb as the converter, specify the full-path to
  3173. your mathtoweb.jar file."
  3174. :group 'org-latex
  3175. :version "24.1"
  3176. :type '(choice
  3177. (const :tag "None" nil)
  3178. (file :tag "JAR file" :must-match t)))
  3179. (defcustom org-latex-to-mathml-convert-command nil
  3180. "Command to convert LaTeX fragments to MathML.
  3181. Replace format-specifiers in the command as noted below and use
  3182. `shell-command' to convert LaTeX to MathML.
  3183. %j: Executable file in fully expanded form as specified by
  3184. `org-latex-to-mathml-jar-file'.
  3185. %I: Input LaTeX file in fully expanded form
  3186. %o: Output MathML file
  3187. This command is used by `org-create-math-formula'.
  3188. When using MathToWeb as the converter, set this to
  3189. \"java -jar %j -unicode -force -df %o %I\"."
  3190. :group 'org-latex
  3191. :version "24.1"
  3192. :type '(choice
  3193. (const :tag "None" nil)
  3194. (string :tag "\nShell command")))
  3195. (defcustom org-latex-create-formula-image-program 'dvipng
  3196. "Program to convert LaTeX fragments with.
  3197. dvipng Process the LaTeX fragments to dvi file, then convert
  3198. dvi files to png files using dvipng.
  3199. This will also include processing of non-math environments.
  3200. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3201. to convert pdf files to png files"
  3202. :group 'org-latex
  3203. :version "24.1"
  3204. :type '(choice
  3205. (const :tag "dvipng" dvipng)
  3206. (const :tag "imagemagick" imagemagick)))
  3207. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3208. "Path to store latex preview images.
  3209. A relative path here creates many directories relative to the
  3210. processed org files paths. An absolute path puts all preview
  3211. images at the same place."
  3212. :group 'org-latex
  3213. :version "24.3"
  3214. :type 'string)
  3215. (defun org-format-latex-mathml-available-p ()
  3216. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3217. (save-match-data
  3218. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3219. org-latex-to-mathml-convert-command)
  3220. (let ((executable (car (split-string
  3221. org-latex-to-mathml-convert-command))))
  3222. (when (executable-find executable)
  3223. (if (string-match
  3224. "%j" org-latex-to-mathml-convert-command)
  3225. (file-readable-p org-latex-to-mathml-jar-file)
  3226. t))))))
  3227. (defcustom org-format-latex-header "\\documentclass{article}
  3228. \\usepackage[usenames]{color}
  3229. \\usepackage{amsmath}
  3230. \\usepackage[mathscr]{eucal}
  3231. \\pagestyle{empty} % do not remove
  3232. \[PACKAGES]
  3233. \[DEFAULT-PACKAGES]
  3234. % The settings below are copied from fullpage.sty
  3235. \\setlength{\\textwidth}{\\paperwidth}
  3236. \\addtolength{\\textwidth}{-3cm}
  3237. \\setlength{\\oddsidemargin}{1.5cm}
  3238. \\addtolength{\\oddsidemargin}{-2.54cm}
  3239. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3240. \\setlength{\\textheight}{\\paperheight}
  3241. \\addtolength{\\textheight}{-\\headheight}
  3242. \\addtolength{\\textheight}{-\\headsep}
  3243. \\addtolength{\\textheight}{-\\footskip}
  3244. \\addtolength{\\textheight}{-3cm}
  3245. \\setlength{\\topmargin}{1.5cm}
  3246. \\addtolength{\\topmargin}{-2.54cm}"
  3247. "The document header used for processing LaTeX fragments.
  3248. It is imperative that this header make sure that no page number
  3249. appears on the page. The package defined in the variables
  3250. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3251. will either replace the placeholder \"[PACKAGES]\" in this
  3252. header, or they will be appended."
  3253. :group 'org-latex
  3254. :type 'string)
  3255. (defun org-set-packages-alist (var val)
  3256. "Set the packages alist and make sure it has 3 elements per entry."
  3257. (set var (mapcar (lambda (x)
  3258. (if (and (consp x) (= (length x) 2))
  3259. (list (car x) (nth 1 x) t)
  3260. x))
  3261. val)))
  3262. (defun org-get-packages-alist (var)
  3263. "Get the packages alist and make sure it has 3 elements per entry."
  3264. (mapcar (lambda (x)
  3265. (if (and (consp x) (= (length x) 2))
  3266. (list (car x) (nth 1 x) t)
  3267. x))
  3268. (default-value var)))
  3269. (defcustom org-latex-default-packages-alist
  3270. '(("AUTO" "inputenc" t)
  3271. ("T1" "fontenc" t)
  3272. ("" "fixltx2e" nil)
  3273. ("" "graphicx" t)
  3274. ("" "longtable" nil)
  3275. ("" "float" nil)
  3276. ("" "wrapfig" nil)
  3277. ("" "soul" t)
  3278. ("" "textcomp" t)
  3279. ("" "marvosym" t)
  3280. ("" "wasysym" t)
  3281. ("" "latexsym" t)
  3282. ("" "amssymb" t)
  3283. ("" "hyperref" nil)
  3284. "\\tolerance=1000")
  3285. "Alist of default packages to be inserted in the header.
  3286. Change this only if one of the packages here causes an
  3287. incompatibility with another package you are using.
  3288. The packages in this list are needed by one part or another of
  3289. Org mode to function properly:
  3290. - inputenc, fontenc: for basic font and character selection
  3291. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3292. symbols used for interpreting the entities in `org-entities'.
  3293. You can skip some of these packages if you don't use any of the
  3294. symbols in it.
  3295. - graphicx: for including images
  3296. - float, wrapfig: for figure placement
  3297. - longtable: for long tables
  3298. - hyperref: for cross references
  3299. Therefore you should not modify this variable unless you know
  3300. what you are doing. The one reason to change it anyway is that
  3301. you might be loading some other package that conflicts with one
  3302. of the default packages. Each cell is of the format
  3303. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3304. the package also needs to be included when compiling LaTeX
  3305. snippets into images for inclusion into non-LaTeX output."
  3306. :group 'org-latex
  3307. :group 'org-export-latex
  3308. :set 'org-set-packages-alist
  3309. :get 'org-get-packages-alist
  3310. :version "24.1"
  3311. :type '(repeat
  3312. (choice
  3313. (list :tag "options/package pair"
  3314. (string :tag "options")
  3315. (string :tag "package")
  3316. (boolean :tag "Snippet"))
  3317. (string :tag "A line of LaTeX"))))
  3318. (defcustom org-latex-packages-alist nil
  3319. "Alist of packages to be inserted in every LaTeX header.
  3320. These will be inserted after `org-latex-default-packages-alist'.
  3321. Each cell is of the format:
  3322. \(\"options\" \"package\" snippet-flag)
  3323. SNIPPET-FLAG, when t, indicates that this package is also needed
  3324. when turning LaTeX snippets into images for inclusion into
  3325. non-LaTeX output.
  3326. Make sure that you only list packages here which:
  3327. - you want in every file
  3328. - do not conflict with the setup in `org-format-latex-header'.
  3329. - do not conflict with the default packages in
  3330. `org-latex-default-packages-alist'."
  3331. :group 'org-latex
  3332. :group 'org-export-latex
  3333. :set 'org-set-packages-alist
  3334. :get 'org-get-packages-alist
  3335. :type '(repeat
  3336. (choice
  3337. (list :tag "options/package pair"
  3338. (string :tag "options")
  3339. (string :tag "package")
  3340. (boolean :tag "Snippet"))
  3341. (string :tag "A line of LaTeX"))))
  3342. (defgroup org-appearance nil
  3343. "Settings for Org-mode appearance."
  3344. :tag "Org Appearance"
  3345. :group 'org)
  3346. (defcustom org-level-color-stars-only nil
  3347. "Non-nil means fontify only the stars in each headline.
  3348. When nil, the entire headline is fontified.
  3349. Changing it requires restart of `font-lock-mode' to become effective
  3350. also in regions already fontified."
  3351. :group 'org-appearance
  3352. :type 'boolean)
  3353. (defcustom org-hide-leading-stars nil
  3354. "Non-nil means hide the first N-1 stars in a headline.
  3355. This works by using the face `org-hide' for these stars. This
  3356. face is white for a light background, and black for a dark
  3357. background. You may have to customize the face `org-hide' to
  3358. make this work.
  3359. Changing it requires restart of `font-lock-mode' to become effective
  3360. also in regions already fontified.
  3361. You may also set this on a per-file basis by adding one of the following
  3362. lines to the buffer:
  3363. #+STARTUP: hidestars
  3364. #+STARTUP: showstars"
  3365. :group 'org-appearance
  3366. :type 'boolean)
  3367. (defcustom org-hidden-keywords nil
  3368. "List of symbols corresponding to keywords to be hidden the org buffer.
  3369. For example, a value '(title) for this list will make the document's title
  3370. appear in the buffer without the initial #+TITLE: keyword."
  3371. :group 'org-appearance
  3372. :version "24.1"
  3373. :type '(set (const :tag "#+AUTHOR" author)
  3374. (const :tag "#+DATE" date)
  3375. (const :tag "#+EMAIL" email)
  3376. (const :tag "#+TITLE" title)))
  3377. (defcustom org-custom-properties nil
  3378. "List of properties (as strings) with a special meaning.
  3379. The default use of these custom properties is to let the user
  3380. hide them with `org-toggle-custom-properties-visibility'."
  3381. :group 'org-properties
  3382. :group 'org-appearance
  3383. :version "24.3"
  3384. :type '(repeat (string :tag "Property Name")))
  3385. (defcustom org-fontify-done-headline nil
  3386. "Non-nil means change the face of a headline if it is marked DONE.
  3387. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3388. When this is non-nil, the headline after the keyword is set to the
  3389. `org-headline-done' as an additional indication."
  3390. :group 'org-appearance
  3391. :type 'boolean)
  3392. (defcustom org-fontify-emphasized-text t
  3393. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3394. Changing this variable requires a restart of Emacs to take effect."
  3395. :group 'org-appearance
  3396. :type 'boolean)
  3397. (defcustom org-fontify-whole-heading-line nil
  3398. "Non-nil means fontify the whole line for headings.
  3399. This is useful when setting a background color for the
  3400. org-level-* faces."
  3401. :group 'org-appearance
  3402. :type 'boolean)
  3403. (defcustom org-highlight-latex-and-related nil
  3404. "Non-nil means highlight LaTeX related syntax in the buffer.
  3405. When non nil, the value should be a list containing any of the
  3406. following symbols:
  3407. `latex' Highlight LaTeX snippets and environments.
  3408. `script' Highlight subscript and superscript.
  3409. `entities' Highlight entities."
  3410. :group 'org-appearance
  3411. :type '(choice
  3412. (const :tag "No highlighting" nil)
  3413. (set :greedy t :tag "Highlight"
  3414. (const :tag "LaTeX snippets and environments" latex)
  3415. (const :tag "Subscript and superscript" script)
  3416. (const :tag "Entities" entities))))
  3417. (defcustom org-hide-emphasis-markers nil
  3418. "Non-nil mean font-lock should hide the emphasis marker characters."
  3419. :group 'org-appearance
  3420. :type 'boolean)
  3421. (defcustom org-pretty-entities nil
  3422. "Non-nil means show entities as UTF8 characters.
  3423. When nil, the \\name form remains in the buffer."
  3424. :group 'org-appearance
  3425. :version "24.1"
  3426. :type 'boolean)
  3427. (defcustom org-pretty-entities-include-sub-superscripts t
  3428. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3429. :group 'org-appearance
  3430. :version "24.1"
  3431. :type 'boolean)
  3432. (defvar org-emph-re nil
  3433. "Regular expression for matching emphasis.
  3434. After a match, the match groups contain these elements:
  3435. 0 The match of the full regular expression, including the characters
  3436. before and after the proper match
  3437. 1 The character before the proper match, or empty at beginning of line
  3438. 2 The proper match, including the leading and trailing markers
  3439. 3 The leading marker like * or /, indicating the type of highlighting
  3440. 4 The text between the emphasis markers, not including the markers
  3441. 5 The character after the match, empty at the end of a line")
  3442. (defvar org-verbatim-re nil
  3443. "Regular expression for matching verbatim text.")
  3444. (defvar org-emphasis-regexp-components) ; defined just below
  3445. (defvar org-emphasis-alist) ; defined just below
  3446. (defun org-set-emph-re (var val)
  3447. "Set variable and compute the emphasis regular expression."
  3448. (set var val)
  3449. (when (and (boundp 'org-emphasis-alist)
  3450. (boundp 'org-emphasis-regexp-components)
  3451. org-emphasis-alist org-emphasis-regexp-components)
  3452. (let* ((e org-emphasis-regexp-components)
  3453. (pre (car e))
  3454. (post (nth 1 e))
  3455. (border (nth 2 e))
  3456. (body (nth 3 e))
  3457. (nl (nth 4 e))
  3458. (body1 (concat body "*?"))
  3459. (markers (mapconcat 'car org-emphasis-alist ""))
  3460. (vmarkers (mapconcat
  3461. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3462. org-emphasis-alist "")))
  3463. ;; make sure special characters appear at the right position in the class
  3464. (if (string-match "\\^" markers)
  3465. (setq markers (concat (replace-match "" t t markers) "^")))
  3466. (if (string-match "-" markers)
  3467. (setq markers (concat (replace-match "" t t markers) "-")))
  3468. (if (string-match "\\^" vmarkers)
  3469. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3470. (if (string-match "-" vmarkers)
  3471. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3472. (if (> nl 0)
  3473. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3474. (int-to-string nl) "\\}")))
  3475. ;; Make the regexp
  3476. (setq org-emph-re
  3477. (concat "\\([" pre "]\\|^\\)"
  3478. "\\("
  3479. "\\([" markers "]\\)"
  3480. "\\("
  3481. "[^" border "]\\|"
  3482. "[^" border "]"
  3483. body1
  3484. "[^" border "]"
  3485. "\\)"
  3486. "\\3\\)"
  3487. "\\([" post "]\\|$\\)"))
  3488. (setq org-verbatim-re
  3489. (concat "\\([" pre "]\\|^\\)"
  3490. "\\("
  3491. "\\([" vmarkers "]\\)"
  3492. "\\("
  3493. "[^" border "]\\|"
  3494. "[^" border "]"
  3495. body1
  3496. "[^" border "]"
  3497. "\\)"
  3498. "\\3\\)"
  3499. "\\([" post "]\\|$\\)")))))
  3500. (defcustom org-emphasis-regexp-components
  3501. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3502. "Components used to build the regular expression for emphasis.
  3503. This is a list with five entries. Terminology: In an emphasis string
  3504. like \" *strong word* \", we call the initial space PREMATCH, the final
  3505. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3506. and \"trong wor\" is the body. The different components in this variable
  3507. specify what is allowed/forbidden in each part:
  3508. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3509. post Chars allowed as postmatch. End of line will be allowed too.
  3510. border The chars *forbidden* as border characters.
  3511. body-regexp A regexp like \".\" to match a body character. Don't use
  3512. non-shy groups here, and don't allow newline here.
  3513. newline The maximum number of newlines allowed in an emphasis exp.
  3514. Use customize to modify this, or restart Emacs after changing it."
  3515. :group 'org-appearance
  3516. :set 'org-set-emph-re
  3517. :type '(list
  3518. (sexp :tag "Allowed chars in pre ")
  3519. (sexp :tag "Allowed chars in post ")
  3520. (sexp :tag "Forbidden chars in border ")
  3521. (sexp :tag "Regexp for body ")
  3522. (integer :tag "number of newlines allowed")
  3523. (option (boolean :tag "Please ignore this button"))))
  3524. (defcustom org-emphasis-alist
  3525. `(("*" bold "<b>" "</b>")
  3526. ("/" italic "<i>" "</i>")
  3527. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3528. ("=" org-code "<code>" "</code>" verbatim)
  3529. ("~" org-verbatim "<code>" "</code>" verbatim)
  3530. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3531. "<del>" "</del>")
  3532. )
  3533. "Special syntax for emphasized text.
  3534. Text starting and ending with a special character will be emphasized, for
  3535. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3536. characters, the face to be used by font-lock for highlighting in Org-mode
  3537. Emacs buffers, and the HTML tags to be used for this.
  3538. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3539. Use customize to modify this, or restart Emacs after changing it."
  3540. :group 'org-appearance
  3541. :set 'org-set-emph-re
  3542. :type '(repeat
  3543. (list
  3544. (string :tag "Marker character")
  3545. (choice
  3546. (face :tag "Font-lock-face")
  3547. (plist :tag "Face property list"))
  3548. (string :tag "HTML start tag")
  3549. (string :tag "HTML end tag")
  3550. (option (const verbatim)))))
  3551. (defvar org-protecting-blocks
  3552. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3553. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3554. This is needed for font-lock setup.")
  3555. ;;; Miscellaneous options
  3556. (defgroup org-completion nil
  3557. "Completion in Org-mode."
  3558. :tag "Org Completion"
  3559. :group 'org)
  3560. (defcustom org-completion-use-ido nil
  3561. "Non-nil means use ido completion wherever possible.
  3562. Note that `ido-mode' must be active for this variable to be relevant.
  3563. If you decide to turn this variable on, you might well want to turn off
  3564. `org-outline-path-complete-in-steps'.
  3565. See also `org-completion-use-iswitchb'."
  3566. :group 'org-completion
  3567. :type 'boolean)
  3568. (defcustom org-completion-use-iswitchb nil
  3569. "Non-nil means use iswitchb completion wherever possible.
  3570. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3571. If you decide to turn this variable on, you might well want to turn off
  3572. `org-outline-path-complete-in-steps'.
  3573. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3574. :group 'org-completion
  3575. :type 'boolean)
  3576. (defcustom org-completion-fallback-command 'hippie-expand
  3577. "The expansion command called by \\[pcomplete] in normal context.
  3578. Normal means, no org-mode-specific context."
  3579. :group 'org-completion
  3580. :type 'function)
  3581. ;;; Functions and variables from their packages
  3582. ;; Declared here to avoid compiler warnings
  3583. ;; XEmacs only
  3584. (defvar outline-mode-menu-heading)
  3585. (defvar outline-mode-menu-show)
  3586. (defvar outline-mode-menu-hide)
  3587. (defvar zmacs-regions) ; XEmacs regions
  3588. ;; Emacs only
  3589. (defvar mark-active)
  3590. ;; Various packages
  3591. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3592. (declare-function calendar-forward-day "cal-move" (arg))
  3593. (declare-function calendar-goto-date "cal-move" (date))
  3594. (declare-function calendar-goto-today "cal-move" ())
  3595. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3596. (defvar calc-embedded-close-formula)
  3597. (defvar calc-embedded-open-formula)
  3598. (declare-function cdlatex-tab "ext:cdlatex" ())
  3599. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3600. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3601. (defvar font-lock-unfontify-region-function)
  3602. (declare-function iswitchb-read-buffer "iswitchb"
  3603. (prompt &optional default require-match start matches-set))
  3604. (defvar iswitchb-temp-buflist)
  3605. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3606. (defvar org-agenda-tags-todo-honor-ignore-options)
  3607. (declare-function org-agenda-skip "org-agenda" ())
  3608. (declare-function
  3609. org-agenda-format-item "org-agenda"
  3610. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3611. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3612. (declare-function org-agenda-change-all-lines "org-agenda"
  3613. (newhead hdmarker &optional fixface just-this))
  3614. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3615. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3616. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3617. (beg end))
  3618. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3619. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3620. "org-agenda" (&optional end))
  3621. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3622. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3623. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3624. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3625. (declare-function org-indent-mode "org-indent" (&optional arg))
  3626. (declare-function parse-time-string "parse-time" (string))
  3627. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3628. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3629. (defvar remember-data-file)
  3630. (defvar texmathp-why)
  3631. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3632. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3633. (defvar org-latex-regexps)
  3634. ;;; Autoload and prepare some org modules
  3635. ;; Some table stuff that needs to be defined here, because it is used
  3636. ;; by the functions setting up org-mode or checking for table context.
  3637. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3638. "Detect an org-type or table-type table.")
  3639. (defconst org-table-line-regexp "^[ \t]*|"
  3640. "Detect an org-type table line.")
  3641. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3642. "Detect an org-type table line.")
  3643. (defconst org-table-hline-regexp "^[ \t]*|-"
  3644. "Detect an org-type table hline.")
  3645. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3646. "Detect a table-type table hline.")
  3647. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3648. "Detect the first line outside a table when searching from within it.
  3649. This works for both table types.")
  3650. ;; Autoload the functions in org-table.el that are needed by functions here.
  3651. (eval-and-compile
  3652. (org-autoload "org-table"
  3653. '(org-table-begin org-table-blank-field org-table-end)))
  3654. ;;;###autoload
  3655. (defun turn-on-orgtbl ()
  3656. "Unconditionally turn on `orgtbl-mode'."
  3657. (require 'org-table)
  3658. (orgtbl-mode 1))
  3659. (defun org-at-table-p (&optional table-type)
  3660. "Return t if the cursor is inside an org-type table.
  3661. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3662. (if org-enable-table-editor
  3663. (save-excursion
  3664. (beginning-of-line 1)
  3665. (looking-at (if table-type org-table-any-line-regexp
  3666. org-table-line-regexp)))
  3667. nil))
  3668. (defsubst org-table-p () (org-at-table-p))
  3669. (defun org-at-table.el-p ()
  3670. "Return t if and only if we are at a table.el table."
  3671. (and (org-at-table-p 'any)
  3672. (save-excursion
  3673. (goto-char (org-table-begin 'any))
  3674. (looking-at org-table1-hline-regexp))))
  3675. (defun org-table-recognize-table.el ()
  3676. "If there is a table.el table nearby, recognize it and move into it."
  3677. (if org-table-tab-recognizes-table.el
  3678. (if (org-at-table.el-p)
  3679. (progn
  3680. (beginning-of-line 1)
  3681. (if (looking-at org-table-dataline-regexp)
  3682. nil
  3683. (if (looking-at org-table1-hline-regexp)
  3684. (progn
  3685. (beginning-of-line 2)
  3686. (if (looking-at org-table-any-border-regexp)
  3687. (beginning-of-line -1)))))
  3688. (if (re-search-forward "|" (org-table-end t) t)
  3689. (progn
  3690. (require 'table)
  3691. (if (table--at-cell-p (point))
  3692. t
  3693. (message "recognizing table.el table...")
  3694. (table-recognize-table)
  3695. (message "recognizing table.el table...done")))
  3696. (error "This should not happen"))
  3697. t)
  3698. nil)
  3699. nil))
  3700. (defun org-at-table-hline-p ()
  3701. "Return t if the cursor is inside a hline in a table."
  3702. (if org-enable-table-editor
  3703. (save-excursion
  3704. (beginning-of-line 1)
  3705. (looking-at org-table-hline-regexp))
  3706. nil))
  3707. (defvar org-table-clean-did-remove-column nil)
  3708. (defun org-table-map-tables (function &optional quietly)
  3709. "Apply FUNCTION to the start of all tables in the buffer."
  3710. (save-excursion
  3711. (save-restriction
  3712. (widen)
  3713. (goto-char (point-min))
  3714. (while (re-search-forward org-table-any-line-regexp nil t)
  3715. (unless quietly
  3716. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3717. (beginning-of-line 1)
  3718. (when (and (looking-at org-table-line-regexp)
  3719. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3720. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3721. (save-excursion (funcall function))
  3722. (or (looking-at org-table-line-regexp)
  3723. (forward-char 1)))
  3724. (re-search-forward org-table-any-border-regexp nil 1))))
  3725. (unless quietly (message "Mapping tables: done")))
  3726. ;; Declare and autoload functions from ox.el and al.
  3727. (declare-function org-export-get-environment "ox"
  3728. (&optional backend subtreep ext-plist))
  3729. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3730. ;; Declare and autoload functions from org-agenda.el
  3731. (eval-and-compile
  3732. (org-autoload "org-agenda"
  3733. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3734. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3735. (declare-function org-clock-update-mode-line "org-clock" ())
  3736. (declare-function org-resolve-clocks "org-clock"
  3737. (&optional also-non-dangling-p prompt last-valid))
  3738. (defvar org-clock-start-time)
  3739. (defvar org-clock-marker (make-marker)
  3740. "Marker recording the last clock-in.")
  3741. (defvar org-clock-hd-marker (make-marker)
  3742. "Marker recording the last clock-in, but the headline position.")
  3743. (defvar org-clock-heading ""
  3744. "The heading of the current clock entry.")
  3745. (defun org-clock-is-active ()
  3746. "Return non-nil if clock is currently running.
  3747. The return value is actually the clock marker."
  3748. (marker-buffer org-clock-marker))
  3749. (eval-and-compile
  3750. (org-autoload "org-clock" '(org-clock-remove-overlays
  3751. org-clock-update-time-maybe
  3752. org-clocktable-shift)))
  3753. (defun org-check-running-clock ()
  3754. "Check if the current buffer contains the running clock.
  3755. If yes, offer to stop it and to save the buffer with the changes."
  3756. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3757. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3758. (buffer-name))))
  3759. (org-clock-out)
  3760. (when (y-or-n-p "Save changed buffer?")
  3761. (save-buffer))))
  3762. (defun org-clocktable-try-shift (dir n)
  3763. "Check if this line starts a clock table, if yes, shift the time block."
  3764. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3765. (org-clocktable-shift dir n)))
  3766. ;;;###autoload
  3767. (defun org-clock-persistence-insinuate ()
  3768. "Set up hooks for clock persistence."
  3769. (require 'org-clock)
  3770. (add-hook 'org-mode-hook 'org-clock-load)
  3771. (add-hook 'kill-emacs-hook 'org-clock-save))
  3772. ;; Define the variable already here, to make sure we have it.
  3773. (defvar org-indent-mode nil
  3774. "Non-nil if Org-Indent mode is enabled.
  3775. Use the command `org-indent-mode' to change this variable.")
  3776. ;; Autoload archiving code
  3777. ;; The stuff that is needed for cycling and tags has to be defined here.
  3778. (defgroup org-archive nil
  3779. "Options concerning archiving in Org-mode."
  3780. :tag "Org Archive"
  3781. :group 'org-structure)
  3782. (defcustom org-archive-location "%s_archive::"
  3783. "The location where subtrees should be archived.
  3784. The value of this variable is a string, consisting of two parts,
  3785. separated by a double-colon. The first part is a filename and
  3786. the second part is a headline.
  3787. When the filename is omitted, archiving happens in the same file.
  3788. %s in the filename will be replaced by the current file
  3789. name (without the directory part). Archiving to a different file
  3790. is useful to keep archived entries from contributing to the
  3791. Org-mode Agenda.
  3792. The archived entries will be filed as subtrees of the specified
  3793. headline. When the headline is omitted, the subtrees are simply
  3794. filed away at the end of the file, as top-level entries. Also in
  3795. the heading you can use %s to represent the file name, this can be
  3796. useful when using the same archive for a number of different files.
  3797. Here are a few examples:
  3798. \"%s_archive::\"
  3799. If the current file is Projects.org, archive in file
  3800. Projects.org_archive, as top-level trees. This is the default.
  3801. \"::* Archived Tasks\"
  3802. Archive in the current file, under the top-level headline
  3803. \"* Archived Tasks\".
  3804. \"~/org/archive.org::\"
  3805. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3806. \"~/org/archive.org::* From %s\"
  3807. Archive in file ~/org/archive.org (absolute path), under headlines
  3808. \"From FILENAME\" where file name is the current file name.
  3809. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3810. The \"datetree/\" string is special, signifying to archive
  3811. items to the datetree. Items are placed in either the CLOSED
  3812. date of the item, or the current date if there is no CLOSED date.
  3813. The heading will be a subentry to the current date. There doesn't
  3814. need to be a heading, but there always needs to be a slash after
  3815. datetree. For example, to store archived items directly in the
  3816. datetree, use \"~/org/datetree.org::datetree/\".
  3817. \"basement::** Finished Tasks\"
  3818. Archive in file ./basement (relative path), as level 3 trees
  3819. below the level 2 heading \"** Finished Tasks\".
  3820. You may set this option on a per-file basis by adding to the buffer a
  3821. line like
  3822. #+ARCHIVE: basement::** Finished Tasks
  3823. You may also define it locally for a subtree by setting an ARCHIVE property
  3824. in the entry. If such a property is found in an entry, or anywhere up
  3825. the hierarchy, it will be used."
  3826. :group 'org-archive
  3827. :type 'string)
  3828. (defcustom org-archive-tag "ARCHIVE"
  3829. "The tag that marks a subtree as archived.
  3830. An archived subtree does not open during visibility cycling, and does
  3831. not contribute to the agenda listings.
  3832. After changing this, font-lock must be restarted in the relevant buffers to
  3833. get the proper fontification."
  3834. :group 'org-archive
  3835. :group 'org-keywords
  3836. :type 'string)
  3837. (defcustom org-agenda-skip-archived-trees t
  3838. "Non-nil means the agenda will skip any items located in archived trees.
  3839. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3840. variable is no longer recommended, you should leave it at the value t.
  3841. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3842. :group 'org-archive
  3843. :group 'org-agenda-skip
  3844. :type 'boolean)
  3845. (defcustom org-columns-skip-archived-trees t
  3846. "Non-nil means ignore archived trees when creating column view."
  3847. :group 'org-archive
  3848. :group 'org-properties
  3849. :type 'boolean)
  3850. (defcustom org-cycle-open-archived-trees nil
  3851. "Non-nil means `org-cycle' will open archived trees.
  3852. An archived tree is a tree marked with the tag ARCHIVE.
  3853. When nil, archived trees will stay folded. You can still open them with
  3854. normal outline commands like `show-all', but not with the cycling commands."
  3855. :group 'org-archive
  3856. :group 'org-cycle
  3857. :type 'boolean)
  3858. (defcustom org-sparse-tree-open-archived-trees nil
  3859. "Non-nil means sparse tree construction shows matches in archived trees.
  3860. When nil, matches in these trees are highlighted, but the trees are kept in
  3861. collapsed state."
  3862. :group 'org-archive
  3863. :group 'org-sparse-trees
  3864. :type 'boolean)
  3865. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3866. "The default date type when building a sparse tree.
  3867. When this is nil, a date is a scheduled or a deadline timestamp.
  3868. Otherwise, these types are allowed:
  3869. all: all timestamps
  3870. active: only active timestamps (<...>)
  3871. inactive: only inactive timestamps (<...)
  3872. scheduled: only scheduled timestamps
  3873. deadline: only deadline timestamps"
  3874. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3875. (const :tag "All timestamps" all)
  3876. (const :tag "Only active timestamps" active)
  3877. (const :tag "Only inactive timestamps" inactive)
  3878. (const :tag "Only scheduled timestamps" scheduled)
  3879. (const :tag "Only deadline timestamps" deadline))
  3880. :version "24.3"
  3881. :group 'org-sparse-trees)
  3882. (defun org-cycle-hide-archived-subtrees (state)
  3883. "Re-hide all archived subtrees after a visibility state change."
  3884. (when (and (not org-cycle-open-archived-trees)
  3885. (not (memq state '(overview folded))))
  3886. (save-excursion
  3887. (let* ((globalp (memq state '(contents all)))
  3888. (beg (if globalp (point-min) (point)))
  3889. (end (if globalp (point-max) (org-end-of-subtree t))))
  3890. (org-hide-archived-subtrees beg end)
  3891. (goto-char beg)
  3892. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3893. (message "%s" (substitute-command-keys
  3894. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3895. (defun org-force-cycle-archived ()
  3896. "Cycle subtree even if it is archived."
  3897. (interactive)
  3898. (setq this-command 'org-cycle)
  3899. (let ((org-cycle-open-archived-trees t))
  3900. (call-interactively 'org-cycle)))
  3901. (defun org-hide-archived-subtrees (beg end)
  3902. "Re-hide all archived subtrees after a visibility state change."
  3903. (save-excursion
  3904. (let* ((re (concat ":" org-archive-tag ":")))
  3905. (goto-char beg)
  3906. (while (re-search-forward re end t)
  3907. (when (org-at-heading-p)
  3908. (org-flag-subtree t)
  3909. (org-end-of-subtree t))))))
  3910. (declare-function outline-end-of-heading "outline" ())
  3911. (declare-function outline-flag-region "outline" (from to flag))
  3912. (defun org-flag-subtree (flag)
  3913. (save-excursion
  3914. (org-back-to-heading t)
  3915. (outline-end-of-heading)
  3916. (outline-flag-region (point)
  3917. (progn (org-end-of-subtree t) (point))
  3918. flag)))
  3919. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3920. (eval-and-compile
  3921. (org-autoload "org-archive"
  3922. '(org-add-archive-files)))
  3923. ;; Autoload Column View Code
  3924. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3925. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3926. (declare-function org-columns-compute "org-colview" (property))
  3927. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3928. '(org-columns-number-to-string
  3929. org-columns-get-format-and-top-level
  3930. org-columns-compute
  3931. org-columns-remove-overlays))
  3932. ;; Autoload ID code
  3933. (declare-function org-id-store-link "org-id")
  3934. (declare-function org-id-locations-load "org-id")
  3935. (declare-function org-id-locations-save "org-id")
  3936. (defvar org-id-track-globally)
  3937. (org-autoload "org-id"
  3938. '(org-id-new
  3939. org-id-copy
  3940. org-id-get-with-outline-path-completion
  3941. org-id-get-with-outline-drilling))
  3942. ;;; Variables for pre-computed regular expressions, all buffer local
  3943. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3944. "Matches first line of a hidden block.")
  3945. (make-variable-buffer-local 'org-drawer-regexp)
  3946. (defvar org-todo-regexp nil
  3947. "Matches any of the TODO state keywords.")
  3948. (make-variable-buffer-local 'org-todo-regexp)
  3949. (defvar org-not-done-regexp nil
  3950. "Matches any of the TODO state keywords except the last one.")
  3951. (make-variable-buffer-local 'org-not-done-regexp)
  3952. (defvar org-not-done-heading-regexp nil
  3953. "Matches a TODO headline that is not done.")
  3954. (make-variable-buffer-local 'org-not-done-regexp)
  3955. (defvar org-todo-line-regexp nil
  3956. "Matches a headline and puts TODO state into group 2 if present.")
  3957. (make-variable-buffer-local 'org-todo-line-regexp)
  3958. (defvar org-complex-heading-regexp nil
  3959. "Matches a headline and puts everything into groups:
  3960. group 1: the stars
  3961. group 2: The todo keyword, maybe
  3962. group 3: Priority cookie
  3963. group 4: True headline
  3964. group 5: Tags")
  3965. (make-variable-buffer-local 'org-complex-heading-regexp)
  3966. (defvar org-complex-heading-regexp-format nil
  3967. "Printf format to make regexp to match an exact headline.
  3968. This regexp will match the headline of any node which has the
  3969. exact headline text that is put into the format, but may have any
  3970. TODO state, priority and tags.")
  3971. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3972. (defvar org-todo-line-tags-regexp nil
  3973. "Matches a headline and puts TODO state into group 2 if present.
  3974. Also put tags into group 4 if tags are present.")
  3975. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3976. (defvar org-ds-keyword-length 12
  3977. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3978. (make-variable-buffer-local 'org-ds-keyword-length)
  3979. (defvar org-deadline-regexp nil
  3980. "Matches the DEADLINE keyword.")
  3981. (make-variable-buffer-local 'org-deadline-regexp)
  3982. (defvar org-deadline-time-regexp nil
  3983. "Matches the DEADLINE keyword together with a time stamp.")
  3984. (make-variable-buffer-local 'org-deadline-time-regexp)
  3985. (defvar org-deadline-line-regexp nil
  3986. "Matches the DEADLINE keyword and the rest of the line.")
  3987. (make-variable-buffer-local 'org-deadline-line-regexp)
  3988. (defvar org-scheduled-regexp nil
  3989. "Matches the SCHEDULED keyword.")
  3990. (make-variable-buffer-local 'org-scheduled-regexp)
  3991. (defvar org-scheduled-time-regexp nil
  3992. "Matches the SCHEDULED keyword together with a time stamp.")
  3993. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3994. (defvar org-closed-time-regexp nil
  3995. "Matches the CLOSED keyword together with a time stamp.")
  3996. (make-variable-buffer-local 'org-closed-time-regexp)
  3997. (defvar org-keyword-time-regexp nil
  3998. "Matches any of the 4 keywords, together with the time stamp.")
  3999. (make-variable-buffer-local 'org-keyword-time-regexp)
  4000. (defvar org-keyword-time-not-clock-regexp nil
  4001. "Matches any of the 3 keywords, together with the time stamp.")
  4002. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4003. (defvar org-maybe-keyword-time-regexp nil
  4004. "Matches a timestamp, possibly preceded by a keyword.")
  4005. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4006. (defvar org-all-time-keywords nil
  4007. "List of time keywords.")
  4008. (make-variable-buffer-local 'org-all-time-keywords)
  4009. (defconst org-plain-time-of-day-regexp
  4010. (concat
  4011. "\\(\\<[012]?[0-9]"
  4012. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4013. "\\(--?"
  4014. "\\(\\<[012]?[0-9]"
  4015. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4016. "\\)?")
  4017. "Regular expression to match a plain time or time range.
  4018. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4019. groups carry important information:
  4020. 0 the full match
  4021. 1 the first time, range or not
  4022. 8 the second time, if it is a range.")
  4023. (defconst org-plain-time-extension-regexp
  4024. (concat
  4025. "\\(\\<[012]?[0-9]"
  4026. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4027. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4028. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  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. 7 hours of duration
  4033. 9 minutes of duration")
  4034. (defconst org-stamp-time-of-day-regexp
  4035. (concat
  4036. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4037. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4038. "\\(--?"
  4039. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4040. "Regular expression to match a timestamp time or time range.
  4041. After a match, the following groups carry important information:
  4042. 0 the full match
  4043. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4044. 2 the first time, range or not
  4045. 4 the second time, if it is a range.")
  4046. (defconst org-startup-options
  4047. '(("fold" org-startup-folded t)
  4048. ("overview" org-startup-folded t)
  4049. ("nofold" org-startup-folded nil)
  4050. ("showall" org-startup-folded nil)
  4051. ("showeverything" org-startup-folded showeverything)
  4052. ("content" org-startup-folded content)
  4053. ("indent" org-startup-indented t)
  4054. ("noindent" org-startup-indented nil)
  4055. ("hidestars" org-hide-leading-stars t)
  4056. ("showstars" org-hide-leading-stars nil)
  4057. ("odd" org-odd-levels-only t)
  4058. ("oddeven" org-odd-levels-only nil)
  4059. ("align" org-startup-align-all-tables t)
  4060. ("noalign" org-startup-align-all-tables nil)
  4061. ("inlineimages" org-startup-with-inline-images t)
  4062. ("noinlineimages" org-startup-with-inline-images nil)
  4063. ("latexpreview" org-startup-with-latex-preview t)
  4064. ("nolatexpreview" org-startup-with-latex-preview nil)
  4065. ("customtime" org-display-custom-times t)
  4066. ("logdone" org-log-done time)
  4067. ("lognotedone" org-log-done note)
  4068. ("nologdone" org-log-done nil)
  4069. ("lognoteclock-out" org-log-note-clock-out t)
  4070. ("nolognoteclock-out" org-log-note-clock-out nil)
  4071. ("logrepeat" org-log-repeat state)
  4072. ("lognoterepeat" org-log-repeat note)
  4073. ("logdrawer" org-log-into-drawer t)
  4074. ("nologdrawer" org-log-into-drawer nil)
  4075. ("logstatesreversed" org-log-states-order-reversed t)
  4076. ("nologstatesreversed" org-log-states-order-reversed nil)
  4077. ("nologrepeat" org-log-repeat nil)
  4078. ("logreschedule" org-log-reschedule time)
  4079. ("lognotereschedule" org-log-reschedule note)
  4080. ("nologreschedule" org-log-reschedule nil)
  4081. ("logredeadline" org-log-redeadline time)
  4082. ("lognoteredeadline" org-log-redeadline note)
  4083. ("nologredeadline" org-log-redeadline nil)
  4084. ("logrefile" org-log-refile time)
  4085. ("lognoterefile" org-log-refile note)
  4086. ("nologrefile" org-log-refile nil)
  4087. ("fninline" org-footnote-define-inline t)
  4088. ("nofninline" org-footnote-define-inline nil)
  4089. ("fnlocal" org-footnote-section nil)
  4090. ("fnauto" org-footnote-auto-label t)
  4091. ("fnprompt" org-footnote-auto-label nil)
  4092. ("fnconfirm" org-footnote-auto-label confirm)
  4093. ("fnplain" org-footnote-auto-label plain)
  4094. ("fnadjust" org-footnote-auto-adjust t)
  4095. ("nofnadjust" org-footnote-auto-adjust nil)
  4096. ("constcgs" constants-unit-system cgs)
  4097. ("constSI" constants-unit-system SI)
  4098. ("noptag" org-tag-persistent-alist nil)
  4099. ("hideblocks" org-hide-block-startup t)
  4100. ("nohideblocks" org-hide-block-startup nil)
  4101. ("beamer" org-startup-with-beamer-mode t)
  4102. ("entitiespretty" org-pretty-entities t)
  4103. ("entitiesplain" org-pretty-entities nil))
  4104. "Variable associated with STARTUP options for org-mode.
  4105. Each element is a list of three items: the startup options (as written
  4106. in the #+STARTUP line), the corresponding variable, and the value to set
  4107. this variable to if the option is found. An optional forth element PUSH
  4108. means to push this value onto the list in the variable.")
  4109. (defun org-update-property-plist (key val props)
  4110. "Update PROPS with KEY and VAL."
  4111. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4112. (key (if appending (substring key 0 (- (length key) 1)) key))
  4113. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4114. (previous (cdr (assoc key props))))
  4115. (if appending
  4116. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4117. (cons (cons key val) remainder))))
  4118. (defconst org-block-regexp
  4119. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4120. "Regular expression for hiding blocks.")
  4121. (defconst org-heading-keyword-regexp-format
  4122. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4123. "Printf format for a regexp matching a headline with some keyword.
  4124. This regexp will match the headline of any node which has the
  4125. exact keyword that is put into the format. The keyword isn't in
  4126. any group by default, but the stars and the body are.")
  4127. (defconst org-heading-keyword-maybe-regexp-format
  4128. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4129. "Printf format for a regexp matching a headline, possibly with some keyword.
  4130. This regexp can match any headline with the specified keyword, or
  4131. without a keyword. The keyword isn't in any group by default,
  4132. but the stars and the body are.")
  4133. (defun org-set-regexps-and-options ()
  4134. "Precompute regular expressions for current buffer."
  4135. (when (derived-mode-p 'org-mode)
  4136. (org-set-local 'org-todo-kwd-alist nil)
  4137. (org-set-local 'org-todo-key-alist nil)
  4138. (org-set-local 'org-todo-key-trigger nil)
  4139. (org-set-local 'org-todo-keywords-1 nil)
  4140. (org-set-local 'org-done-keywords nil)
  4141. (org-set-local 'org-todo-heads nil)
  4142. (org-set-local 'org-todo-sets nil)
  4143. (org-set-local 'org-todo-log-states nil)
  4144. (org-set-local 'org-file-properties nil)
  4145. (org-set-local 'org-file-tags nil)
  4146. (let ((re (org-make-options-regexp
  4147. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4148. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4149. "SETUPFILE" "OPTIONS")
  4150. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4151. (splitre "[ \t]+")
  4152. (scripts org-use-sub-superscripts)
  4153. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4154. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4155. (start 0))
  4156. (save-excursion
  4157. (save-restriction
  4158. (widen)
  4159. (goto-char (point-min))
  4160. (while (or (and ext-setup-or-nil
  4161. (string-match re ext-setup-or-nil start)
  4162. (setq start (match-end 0)))
  4163. (and (setq ext-setup-or-nil nil start 0)
  4164. (re-search-forward re nil t)))
  4165. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4166. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4167. (if (stringp value) (setq value (org-trim value)))
  4168. (cond
  4169. ((equal key "CATEGORY")
  4170. (setq cat value))
  4171. ((member key '("SEQ_TODO" "TODO"))
  4172. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4173. ((equal key "TYP_TODO")
  4174. (push (cons 'type (org-split-string value splitre)) kwds))
  4175. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4176. ;; general TODO-like setup
  4177. (push (cons (intern (downcase (match-string 1 key)))
  4178. (org-split-string value splitre)) kwds))
  4179. ((equal key "TAGS")
  4180. (setq tags (append tags (if tags '("\\n") nil)
  4181. (org-split-string value splitre))))
  4182. ((equal key "COLUMNS")
  4183. (org-set-local 'org-columns-default-format value))
  4184. ((equal key "LINK")
  4185. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4186. (push (cons (match-string 1 value)
  4187. (org-trim (match-string 2 value)))
  4188. links)))
  4189. ((equal key "PRIORITIES")
  4190. (setq prio (org-split-string value " +")))
  4191. ((equal key "PROPERTY")
  4192. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4193. (setq props (org-update-property-plist (match-string 1 value)
  4194. (match-string 2 value)
  4195. props))))
  4196. ((equal key "FILETAGS")
  4197. (when (string-match "\\S-" value)
  4198. (setq ftags
  4199. (append
  4200. ftags
  4201. (apply 'append
  4202. (mapcar (lambda (x) (org-split-string x ":"))
  4203. (org-split-string value)))))))
  4204. ((equal key "DRAWERS")
  4205. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4206. ((equal key "CONSTANTS")
  4207. (setq const (append const (org-split-string value splitre))))
  4208. ((equal key "STARTUP")
  4209. (let ((opts (org-split-string value splitre))
  4210. l var val)
  4211. (while (setq l (pop opts))
  4212. (when (setq l (assoc l org-startup-options))
  4213. (setq var (nth 1 l) val (nth 2 l))
  4214. (if (not (nth 3 l))
  4215. (set (make-local-variable var) val)
  4216. (if (not (listp (symbol-value var)))
  4217. (set (make-local-variable var) nil))
  4218. (set (make-local-variable var) (symbol-value var))
  4219. (add-to-list var val))))))
  4220. ((equal key "ARCHIVE")
  4221. (setq arch value)
  4222. (remove-text-properties 0 (length arch)
  4223. '(face t fontified t) arch))
  4224. ((equal key "OPTIONS")
  4225. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4226. (setq scripts (read (match-string 2 value)))))
  4227. ((equal key "SETUPFILE")
  4228. (setq setup-contents (org-file-contents
  4229. (expand-file-name
  4230. (org-remove-double-quotes value))
  4231. 'noerror))
  4232. (if (not ext-setup-or-nil)
  4233. (setq ext-setup-or-nil setup-contents start 0)
  4234. (setq ext-setup-or-nil
  4235. (concat (substring ext-setup-or-nil 0 start)
  4236. "\n" setup-contents "\n"
  4237. (substring ext-setup-or-nil start)))))))
  4238. ;; search for property blocks
  4239. (goto-char (point-min))
  4240. (while (re-search-forward org-block-regexp nil t)
  4241. (when (equal "PROPERTY" (upcase (match-string 1)))
  4242. (setq value (replace-regexp-in-string
  4243. "[\n\r]" " " (match-string 4)))
  4244. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4245. (setq props (org-update-property-plist (match-string 1 value)
  4246. (match-string 2 value)
  4247. props)))))))
  4248. (org-set-local 'org-use-sub-superscripts scripts)
  4249. (when cat
  4250. (org-set-local 'org-category (intern cat))
  4251. (push (cons "CATEGORY" cat) props))
  4252. (when prio
  4253. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4254. (setq prio (mapcar 'string-to-char prio))
  4255. (org-set-local 'org-highest-priority (nth 0 prio))
  4256. (org-set-local 'org-lowest-priority (nth 1 prio))
  4257. (org-set-local 'org-default-priority (nth 2 prio)))
  4258. (and props (org-set-local 'org-file-properties (nreverse props)))
  4259. (and ftags (org-set-local 'org-file-tags
  4260. (mapcar 'org-add-prop-inherited ftags)))
  4261. (and drawers (org-set-local 'org-drawers drawers))
  4262. (and arch (org-set-local 'org-archive-location arch))
  4263. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4264. ;; Process the TODO keywords
  4265. (unless kwds
  4266. ;; Use the global values as if they had been given locally.
  4267. (setq kwds (default-value 'org-todo-keywords))
  4268. (if (stringp (car kwds))
  4269. (setq kwds (list (cons org-todo-interpretation
  4270. (default-value 'org-todo-keywords)))))
  4271. (setq kwds (reverse kwds)))
  4272. (setq kwds (nreverse kwds))
  4273. (let (inter kws kw)
  4274. (while (setq kws (pop kwds))
  4275. (let ((kws (or
  4276. (run-hook-with-args-until-success
  4277. 'org-todo-setup-filter-hook kws)
  4278. kws)))
  4279. (setq inter (pop kws) sep (member "|" kws)
  4280. kws0 (delete "|" (copy-sequence kws))
  4281. kwsa nil
  4282. kws1 (mapcar
  4283. (lambda (x)
  4284. ;; 1 2
  4285. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4286. (progn
  4287. (setq kw (match-string 1 x)
  4288. key (and (match-end 2) (match-string 2 x))
  4289. log (org-extract-log-state-settings x))
  4290. (push (cons kw (and key (string-to-char key))) kwsa)
  4291. (and log (push log org-todo-log-states))
  4292. kw)
  4293. (error "Invalid TODO keyword %s" x)))
  4294. kws0)
  4295. kwsa (if kwsa (append '((:startgroup))
  4296. (nreverse kwsa)
  4297. '((:endgroup))))
  4298. hw (car kws1)
  4299. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4300. tail (list inter hw (car dws) (org-last dws))))
  4301. (add-to-list 'org-todo-heads hw 'append)
  4302. (push kws1 org-todo-sets)
  4303. (setq org-done-keywords (append org-done-keywords dws nil))
  4304. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4305. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4306. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4307. (setq org-todo-sets (nreverse org-todo-sets)
  4308. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4309. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4310. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4311. ;; Process the constants
  4312. (when const
  4313. (let (e cst)
  4314. (while (setq e (pop const))
  4315. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4316. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4317. (setq org-table-formula-constants-local cst)))
  4318. ;; Process the tags.
  4319. (when tags
  4320. (let (e tgs)
  4321. (while (setq e (pop tags))
  4322. (cond
  4323. ((equal e "{") (push '(:startgroup) tgs))
  4324. ((equal e "}") (push '(:endgroup) tgs))
  4325. ((equal e "\\n") (push '(:newline) tgs))
  4326. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4327. (push (cons (match-string 1 e)
  4328. (string-to-char (match-string 2 e)))
  4329. tgs))
  4330. (t (push (list e) tgs))))
  4331. (org-set-local 'org-tag-alist nil)
  4332. (while (setq e (pop tgs))
  4333. (or (and (stringp (car e))
  4334. (assoc (car e) org-tag-alist))
  4335. (push e org-tag-alist)))))
  4336. ;; Compute the regular expressions and other local variables.
  4337. ;; Using `org-outline-regexp-bol' would complicate them much,
  4338. ;; because of the fixed white space at the end of that string.
  4339. (if (not org-done-keywords)
  4340. (setq org-done-keywords (and org-todo-keywords-1
  4341. (list (org-last org-todo-keywords-1)))))
  4342. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4343. (length org-scheduled-string)
  4344. (length org-clock-string)
  4345. (length org-closed-string)))
  4346. org-drawer-regexp
  4347. (concat "^[ \t]*:\\("
  4348. (mapconcat 'regexp-quote org-drawers "\\|")
  4349. "\\):[ \t]*$")
  4350. org-not-done-keywords
  4351. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4352. org-todo-regexp
  4353. (concat "\\("
  4354. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4355. "\\)")
  4356. org-not-done-regexp
  4357. (concat "\\("
  4358. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4359. "\\)")
  4360. org-not-done-heading-regexp
  4361. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4362. org-todo-line-regexp
  4363. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4364. org-complex-heading-regexp
  4365. (concat "^\\(\\*+\\)"
  4366. "\\(?: +" org-todo-regexp "\\)?"
  4367. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4368. "\\(?: +\\(.*?\\)\\)??"
  4369. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4370. "[ \t]*$")
  4371. org-complex-heading-regexp-format
  4372. (concat "^\\(\\*+\\)"
  4373. "\\(?: +" org-todo-regexp "\\)?"
  4374. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4375. "\\(?: +"
  4376. ;; Stats cookies can be stuck to body.
  4377. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4378. "\\(%s\\)"
  4379. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4380. "\\)"
  4381. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4382. "[ \t]*$")
  4383. org-todo-line-tags-regexp
  4384. (concat "^\\(\\*+\\)"
  4385. "\\(?: +" org-todo-regexp "\\)?"
  4386. "\\(?: +\\(.*?\\)\\)??"
  4387. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4388. "[ \t]*$")
  4389. org-deadline-regexp (concat "\\<" org-deadline-string)
  4390. org-deadline-time-regexp
  4391. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4392. org-deadline-line-regexp
  4393. (concat "\\<\\(" org-deadline-string "\\).*")
  4394. org-scheduled-regexp
  4395. (concat "\\<" org-scheduled-string)
  4396. org-scheduled-time-regexp
  4397. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4398. org-closed-time-regexp
  4399. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4400. org-keyword-time-regexp
  4401. (concat "\\<\\(" org-scheduled-string
  4402. "\\|" org-deadline-string
  4403. "\\|" org-closed-string
  4404. "\\|" org-clock-string "\\)"
  4405. " *[[<]\\([^]>]+\\)[]>]")
  4406. org-keyword-time-not-clock-regexp
  4407. (concat "\\<\\(" org-scheduled-string
  4408. "\\|" org-deadline-string
  4409. "\\|" org-closed-string
  4410. "\\)"
  4411. " *[[<]\\([^]>]+\\)[]>]")
  4412. org-maybe-keyword-time-regexp
  4413. (concat "\\(\\<\\(" org-scheduled-string
  4414. "\\|" org-deadline-string
  4415. "\\|" org-closed-string
  4416. "\\|" org-clock-string "\\)\\)?"
  4417. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4418. org-all-time-keywords
  4419. (mapcar (lambda (w) (substring w 0 -1))
  4420. (list org-scheduled-string org-deadline-string
  4421. org-clock-string org-closed-string)))
  4422. (org-compute-latex-and-related-regexp)
  4423. (org-set-font-lock-defaults))))
  4424. (defun org-file-contents (file &optional noerror)
  4425. "Return the contents of FILE, as a string."
  4426. (if (or (not file)
  4427. (not (file-readable-p file)))
  4428. (if noerror
  4429. (progn
  4430. (message "Cannot read file \"%s\"" file)
  4431. (ding) (sit-for 2)
  4432. "")
  4433. (error "Cannot read file \"%s\"" file))
  4434. (with-temp-buffer
  4435. (insert-file-contents file)
  4436. (buffer-string))))
  4437. (defun org-extract-log-state-settings (x)
  4438. "Extract the log state setting from a TODO keyword string.
  4439. This will extract info from a string like \"WAIT(w@/!)\"."
  4440. (let (kw key log1 log2)
  4441. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4442. (setq kw (match-string 1 x)
  4443. key (and (match-end 2) (match-string 2 x))
  4444. log1 (and (match-end 3) (match-string 3 x))
  4445. log2 (and (match-end 4) (match-string 4 x)))
  4446. (and (or log1 log2)
  4447. (list kw
  4448. (and log1 (if (equal log1 "!") 'time 'note))
  4449. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4450. (defun org-remove-keyword-keys (list)
  4451. "Remove a pair of parenthesis at the end of each string in LIST."
  4452. (mapcar (lambda (x)
  4453. (if (string-match "(.*)$" x)
  4454. (substring x 0 (match-beginning 0))
  4455. x))
  4456. list))
  4457. (defun org-assign-fast-keys (alist)
  4458. "Assign fast keys to a keyword-key alist.
  4459. Respect keys that are already there."
  4460. (let (new e (alt ?0))
  4461. (while (setq e (pop alist))
  4462. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4463. (cdr e)) ;; Key already assigned.
  4464. (push e new)
  4465. (let ((clist (string-to-list (downcase (car e))))
  4466. (used (append new alist)))
  4467. (when (= (car clist) ?@)
  4468. (pop clist))
  4469. (while (and clist (rassoc (car clist) used))
  4470. (pop clist))
  4471. (unless clist
  4472. (while (rassoc alt used)
  4473. (incf alt)))
  4474. (push (cons (car e) (or (car clist) alt)) new))))
  4475. (nreverse new)))
  4476. ;;; Some variables used in various places
  4477. (defvar org-window-configuration nil
  4478. "Used in various places to store a window configuration.")
  4479. (defvar org-selected-window nil
  4480. "Used in various places to store a window configuration.")
  4481. (defvar org-finish-function nil
  4482. "Function to be called when `C-c C-c' is used.
  4483. This is for getting out of special buffers like capture.")
  4484. ;; FIXME: Occasionally check by commenting these, to make sure
  4485. ;; no other functions uses these, forgetting to let-bind them.
  4486. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4487. (defvar org-last-state)
  4488. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4489. ;; Defined somewhere in this file, but used before definition.
  4490. (defvar org-entities) ;; defined in org-entities.el
  4491. (defvar org-struct-menu)
  4492. (defvar org-org-menu)
  4493. (defvar org-tbl-menu)
  4494. ;;;; Define the Org-mode
  4495. ;; We use a before-change function to check if a table might need
  4496. ;; an update.
  4497. (defvar org-table-may-need-update t
  4498. "Indicates that a table might need an update.
  4499. This variable is set by `org-before-change-function'.
  4500. `org-table-align' sets it back to nil.")
  4501. (defun org-before-change-function (beg end)
  4502. "Every change indicates that a table might need an update."
  4503. (setq org-table-may-need-update t))
  4504. (defvar org-mode-map)
  4505. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4506. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4507. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4508. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4509. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4510. (defvar org-table-buffer-is-an nil)
  4511. (defvar bidi-paragraph-direction)
  4512. (defvar buffer-face-mode-face)
  4513. (require 'outline)
  4514. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4515. (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"))
  4516. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4517. ;; Other stuff we need.
  4518. (require 'time-date)
  4519. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4520. (require 'easymenu)
  4521. (require 'overlay)
  4522. ;; (require 'org-macs) moved higher up in the file before it is first used
  4523. (require 'org-entities)
  4524. ;; (require 'org-compat) moved higher up in the file before it is first used
  4525. (require 'org-faces)
  4526. (require 'org-list)
  4527. (require 'org-pcomplete)
  4528. (require 'org-src)
  4529. (require 'org-footnote)
  4530. (require 'org-macro)
  4531. ;; babel
  4532. (require 'ob)
  4533. ;;;###autoload
  4534. (define-derived-mode org-mode outline-mode "Org"
  4535. "Outline-based notes management and organizer, alias
  4536. \"Carsten's outline-mode for keeping track of everything.\"
  4537. Org-mode develops organizational tasks around a NOTES file which
  4538. contains information about projects as plain text. Org-mode is
  4539. implemented on top of outline-mode, which is ideal to keep the content
  4540. of large files well structured. It supports ToDo items, deadlines and
  4541. time stamps, which magically appear in the diary listing of the Emacs
  4542. calendar. Tables are easily created with a built-in table editor.
  4543. Plain text URL-like links connect to websites, emails (VM), Usenet
  4544. messages (Gnus), BBDB entries, and any files related to the project.
  4545. For printing and sharing of notes, an Org-mode file (or a part of it)
  4546. can be exported as a structured ASCII or HTML file.
  4547. The following commands are available:
  4548. \\{org-mode-map}"
  4549. ;; Get rid of Outline menus, they are not needed
  4550. ;; Need to do this here because define-derived-mode sets up
  4551. ;; the keymap so late. Still, it is a waste to call this each time
  4552. ;; we switch another buffer into org-mode.
  4553. (if (featurep 'xemacs)
  4554. (when (boundp 'outline-mode-menu-heading)
  4555. ;; Assume this is Greg's port, it uses easymenu
  4556. (easy-menu-remove outline-mode-menu-heading)
  4557. (easy-menu-remove outline-mode-menu-show)
  4558. (easy-menu-remove outline-mode-menu-hide))
  4559. (define-key org-mode-map [menu-bar headings] 'undefined)
  4560. (define-key org-mode-map [menu-bar hide] 'undefined)
  4561. (define-key org-mode-map [menu-bar show] 'undefined))
  4562. (org-load-modules-maybe)
  4563. (easy-menu-add org-org-menu)
  4564. (easy-menu-add org-tbl-menu)
  4565. (org-install-agenda-files-menu)
  4566. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4567. (add-to-invisibility-spec '(org-cwidth))
  4568. (add-to-invisibility-spec '(org-hide-block . t))
  4569. (when (featurep 'xemacs)
  4570. (org-set-local 'line-move-ignore-invisible t))
  4571. (org-set-local 'outline-regexp org-outline-regexp)
  4572. (org-set-local 'outline-level 'org-outline-level)
  4573. (setq bidi-paragraph-direction 'left-to-right)
  4574. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4575. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4576. (when (and org-ellipsis
  4577. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4578. (fboundp 'make-glyph-code))
  4579. (unless org-display-table
  4580. (setq org-display-table (make-display-table)))
  4581. (set-display-table-slot
  4582. org-display-table 4
  4583. (vconcat (mapcar
  4584. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4585. org-ellipsis)))
  4586. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4587. (setq buffer-display-table org-display-table))
  4588. (org-set-regexps-and-options)
  4589. (when (and org-tag-faces (not org-tags-special-faces-re))
  4590. ;; tag faces set outside customize.... force initialization.
  4591. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4592. ;; Calc embedded
  4593. (org-set-local 'calc-embedded-open-mode "# ")
  4594. ;; Modify a few syntax entries
  4595. (modify-syntax-entry ?< "(")
  4596. (modify-syntax-entry ?> ")")
  4597. (modify-syntax-entry ?{ "(")
  4598. (modify-syntax-entry ?} ")")
  4599. (modify-syntax-entry ?@ "w")
  4600. (modify-syntax-entry ?\" "\"")
  4601. (if org-startup-truncated (setq truncate-lines t))
  4602. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4603. (org-set-local 'font-lock-unfontify-region-function
  4604. 'org-unfontify-region)
  4605. ;; Activate before-change-function
  4606. (org-set-local 'org-table-may-need-update t)
  4607. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4608. 'local)
  4609. ;; Check for running clock before killing a buffer
  4610. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4611. ;; Initialize macros templates.
  4612. (org-macro-initialize-templates)
  4613. ;; Initialize radio targets.
  4614. (org-update-radio-target-regexp)
  4615. ;; Indentation.
  4616. (org-set-local 'indent-line-function 'org-indent-line)
  4617. (org-set-local 'indent-region-function 'org-indent-region)
  4618. ;; Filling and auto-filling.
  4619. (org-setup-filling)
  4620. ;; Comments.
  4621. (org-setup-comments-handling)
  4622. ;; Beginning/end of defun
  4623. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4624. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4625. ;; Next error for sparse trees
  4626. (org-set-local 'next-error-function 'org-occur-next-match)
  4627. ;; Make sure dependence stuff works reliably, even for users who set it
  4628. ;; too late :-(
  4629. (if org-enforce-todo-dependencies
  4630. (add-hook 'org-blocker-hook
  4631. 'org-block-todo-from-children-or-siblings-or-parent)
  4632. (remove-hook 'org-blocker-hook
  4633. 'org-block-todo-from-children-or-siblings-or-parent))
  4634. (if org-enforce-todo-checkbox-dependencies
  4635. (add-hook 'org-blocker-hook
  4636. 'org-block-todo-from-checkboxes)
  4637. (remove-hook 'org-blocker-hook
  4638. 'org-block-todo-from-checkboxes))
  4639. ;; Align options lines
  4640. (org-set-local
  4641. 'align-mode-rules-list
  4642. '((org-in-buffer-settings
  4643. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4644. (modes . '(org-mode)))))
  4645. ;; Imenu
  4646. (org-set-local 'imenu-create-index-function
  4647. 'org-imenu-get-tree)
  4648. ;; Make isearch reveal context
  4649. (if (or (featurep 'xemacs)
  4650. (not (boundp 'outline-isearch-open-invisible-function)))
  4651. ;; Emacs 21 and XEmacs make use of the hook
  4652. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4653. ;; Emacs 22 deals with this through a special variable
  4654. (org-set-local 'outline-isearch-open-invisible-function
  4655. (lambda (&rest ignore) (org-show-context 'isearch)))
  4656. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4657. ;; Setup the pcomplete hooks
  4658. (set (make-local-variable 'pcomplete-command-completion-function)
  4659. 'org-pcomplete-initial)
  4660. (set (make-local-variable 'pcomplete-command-name-function)
  4661. 'org-command-at-point)
  4662. (set (make-local-variable 'pcomplete-default-completion-function)
  4663. 'ignore)
  4664. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4665. 'org-parse-arguments)
  4666. (set (make-local-variable 'pcomplete-termination-string) "")
  4667. (when (>= emacs-major-version 23)
  4668. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4669. ;; If empty file that did not turn on org-mode automatically, make it to.
  4670. (if (and org-insert-mode-line-in-empty-file
  4671. (org-called-interactively-p 'any)
  4672. (= (point-min) (point-max)))
  4673. (insert "# -*- mode: org -*-\n\n"))
  4674. (unless org-inhibit-startup
  4675. (org-unmodified
  4676. (and org-startup-with-beamer-mode (org-beamer-mode))
  4677. (when org-startup-align-all-tables
  4678. (org-table-map-tables 'org-table-align 'quietly))
  4679. (when org-startup-with-inline-images
  4680. (org-display-inline-images))
  4681. (when org-startup-with-latex-preview
  4682. (org-preview-latex-fragment))
  4683. (unless org-inhibit-startup-visibility-stuff
  4684. (org-set-startup-visibility))))
  4685. ;; Try to set org-hide correctly
  4686. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4687. (defvar org-mode-transpose-word-syntax-table
  4688. (let ((st (make-syntax-table)))
  4689. (mapc (lambda(c) (modify-syntax-entry
  4690. (string-to-char (car c)) "w p" st))
  4691. org-emphasis-alist)
  4692. st))
  4693. (when (fboundp 'abbrev-table-put)
  4694. (abbrev-table-put org-mode-abbrev-table
  4695. :parents (list text-mode-abbrev-table)))
  4696. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4697. (defsubst org-fix-ellipsis-at-bol ()
  4698. (save-excursion (goto-char (window-start)) (recenter 0)))
  4699. (defun org-find-invisible-foreground ()
  4700. (let ((candidates (remove
  4701. "unspecified-bg"
  4702. (nconc
  4703. (list (face-background 'default)
  4704. (face-background 'org-default))
  4705. (mapcar
  4706. (lambda (alist)
  4707. (when (boundp alist)
  4708. (cdr (assoc 'background-color (symbol-value alist)))))
  4709. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4710. (list (face-foreground 'org-hide))))))
  4711. (car (remove nil candidates))))
  4712. (defun org-current-time (&optional rounding-minutes past)
  4713. "Current time, possibly rounded to ROUNDING-MINUTES.
  4714. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4715. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4716. the rounding returns a past time."
  4717. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4718. (car org-time-stamp-rounding-minutes)))
  4719. (time (decode-time)) res)
  4720. (if (< r 1)
  4721. (current-time)
  4722. (setq res
  4723. (apply 'encode-time
  4724. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4725. (nthcdr 2 time))))
  4726. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4727. (seconds-to-time (- (org-float-time res) (* r 60)))
  4728. res))))
  4729. (defun org-today ()
  4730. "Return today date, considering `org-extend-today-until'."
  4731. (time-to-days
  4732. (time-subtract (current-time)
  4733. (list 0 (* 3600 org-extend-today-until) 0))))
  4734. ;;;; Font-Lock stuff, including the activators
  4735. (defvar org-mouse-map (make-sparse-keymap))
  4736. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4737. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4738. (when org-mouse-1-follows-link
  4739. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4740. (when org-tab-follows-link
  4741. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4742. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4743. (require 'font-lock)
  4744. (defconst org-non-link-chars "]\t\n\r<>")
  4745. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4746. "shell" "elisp" "doi" "message"))
  4747. (defvar org-link-types-re nil
  4748. "Matches a link that has a url-like prefix like \"http:\"")
  4749. (defvar org-link-re-with-space nil
  4750. "Matches a link with spaces, optional angular brackets around it.")
  4751. (defvar org-link-re-with-space2 nil
  4752. "Matches a link with spaces, optional angular brackets around it.")
  4753. (defvar org-link-re-with-space3 nil
  4754. "Matches a link with spaces, only for internal part in bracket links.")
  4755. (defvar org-angle-link-re nil
  4756. "Matches link with angular brackets, spaces are allowed.")
  4757. (defvar org-plain-link-re nil
  4758. "Matches plain link, without spaces.")
  4759. (defvar org-bracket-link-regexp nil
  4760. "Matches a link in double brackets.")
  4761. (defvar org-bracket-link-analytic-regexp nil
  4762. "Regular expression used to analyze links.
  4763. Here is what the match groups contain after a match:
  4764. 1: http:
  4765. 2: http
  4766. 3: path
  4767. 4: [desc]
  4768. 5: desc")
  4769. (defvar org-bracket-link-analytic-regexp++ nil
  4770. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4771. (defvar org-any-link-re nil
  4772. "Regular expression matching any link.")
  4773. (defconst org-match-sexp-depth 3
  4774. "Number of stacked braces for sub/superscript matching.")
  4775. (defun org-create-multibrace-regexp (left right n)
  4776. "Create a regular expression which will match a balanced sexp.
  4777. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4778. as single character strings.
  4779. The regexp returned will match the entire expression including the
  4780. delimiters. It will also define a single group which contains the
  4781. match except for the outermost delimiters. The maximum depth of
  4782. stacked delimiters is N. Escaping delimiters is not possible."
  4783. (let* ((nothing (concat "[^" left right "]*?"))
  4784. (or "\\|")
  4785. (re nothing)
  4786. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4787. (while (> n 1)
  4788. (setq n (1- n)
  4789. re (concat re or next)
  4790. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4791. (concat left "\\(" re "\\)" right)))
  4792. (defvar org-match-substring-regexp
  4793. (concat
  4794. "\\(\\S-\\)\\([_^]\\)\\("
  4795. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4796. "\\|"
  4797. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4798. "\\|"
  4799. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4800. "The regular expression matching a sub- or superscript.")
  4801. (defvar org-match-substring-with-braces-regexp
  4802. (concat
  4803. "\\(\\S-\\)\\([_^]\\)\\("
  4804. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4805. "\\)")
  4806. "The regular expression matching a sub- or superscript, forcing braces.")
  4807. (defun org-make-link-regexps ()
  4808. "Update the link regular expressions.
  4809. This should be called after the variable `org-link-types' has changed."
  4810. (setq org-link-types-re
  4811. (concat
  4812. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4813. org-link-re-with-space
  4814. (concat
  4815. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4816. "\\([^" org-non-link-chars " ]"
  4817. "[^" org-non-link-chars "]*"
  4818. "[^" org-non-link-chars " ]\\)>?")
  4819. org-link-re-with-space2
  4820. (concat
  4821. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4822. "\\([^" org-non-link-chars " ]"
  4823. "[^\t\n\r]*"
  4824. "[^" org-non-link-chars " ]\\)>?")
  4825. org-link-re-with-space3
  4826. (concat
  4827. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4828. "\\([^" org-non-link-chars " ]"
  4829. "[^\t\n\r]*\\)")
  4830. org-angle-link-re
  4831. (concat
  4832. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4833. "\\([^" org-non-link-chars " ]"
  4834. "[^" org-non-link-chars "]*"
  4835. "\\)>")
  4836. org-plain-link-re
  4837. (concat
  4838. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4839. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4840. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4841. org-bracket-link-regexp
  4842. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4843. org-bracket-link-analytic-regexp
  4844. (concat
  4845. "\\[\\["
  4846. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4847. "\\([^]]+\\)"
  4848. "\\]"
  4849. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4850. "\\]")
  4851. org-bracket-link-analytic-regexp++
  4852. (concat
  4853. "\\[\\["
  4854. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4855. "\\([^]]+\\)"
  4856. "\\]"
  4857. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4858. "\\]")
  4859. org-any-link-re
  4860. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4861. org-angle-link-re "\\)\\|\\("
  4862. org-plain-link-re "\\)")))
  4863. (org-make-link-regexps)
  4864. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4865. "Regular expression for fast time stamp matching.")
  4866. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4867. "Regular expression for fast time stamp matching.")
  4868. (defconst org-ts-regexp0
  4869. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4870. "Regular expression matching time strings for analysis.
  4871. This one does not require the space after the date, so it can be used
  4872. on a string that terminates immediately after the date.")
  4873. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4874. "Regular expression matching time strings for analysis.")
  4875. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4876. "Regular expression matching time stamps, with groups.")
  4877. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4878. "Regular expression matching time stamps (also [..]), with groups.")
  4879. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4880. "Regular expression matching a time stamp range.")
  4881. (defconst org-tr-regexp-both
  4882. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4883. "Regular expression matching a time stamp range.")
  4884. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4885. org-ts-regexp "\\)?")
  4886. "Regular expression matching a time stamp or time stamp range.")
  4887. (defconst org-tsr-regexp-both
  4888. (concat org-ts-regexp-both "\\(--?-?"
  4889. org-ts-regexp-both "\\)?")
  4890. "Regular expression matching a time stamp or time stamp range.
  4891. The time stamps may be either active or inactive.")
  4892. (defvar org-emph-face nil)
  4893. (defun org-do-emphasis-faces (limit)
  4894. "Run through the buffer and add overlays to emphasized strings."
  4895. (let (rtn a)
  4896. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4897. (if (not (= (char-after (match-beginning 3))
  4898. (char-after (match-beginning 4))))
  4899. (progn
  4900. (setq rtn t)
  4901. (setq a (assoc (match-string 3) org-emphasis-alist))
  4902. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4903. 'face
  4904. (nth 1 a))
  4905. (and (nth 4 a)
  4906. (org-remove-flyspell-overlays-in
  4907. (match-beginning 0) (match-end 0)))
  4908. (add-text-properties (match-beginning 2) (match-end 2)
  4909. '(font-lock-multiline t org-emphasis t))
  4910. (when org-hide-emphasis-markers
  4911. (add-text-properties (match-end 4) (match-beginning 5)
  4912. '(invisible org-link))
  4913. (add-text-properties (match-beginning 3) (match-end 3)
  4914. '(invisible org-link)))))
  4915. (backward-char 1))
  4916. rtn))
  4917. (defun org-emphasize (&optional char)
  4918. "Insert or change an emphasis, i.e. a font like bold or italic.
  4919. If there is an active region, change that region to a new emphasis.
  4920. If there is no region, just insert the marker characters and position
  4921. the cursor between them.
  4922. CHAR should be either the marker character, or the first character of the
  4923. HTML tag associated with that emphasis. If CHAR is a space, the means
  4924. to remove the emphasis of the selected region.
  4925. If char is not given (for example in an interactive call) it
  4926. will be prompted for."
  4927. (interactive)
  4928. (let ((eal org-emphasis-alist) e det
  4929. (erc org-emphasis-regexp-components)
  4930. (prompt "")
  4931. (string "") beg end move tag c s)
  4932. (if (org-region-active-p)
  4933. (setq beg (region-beginning) end (region-end)
  4934. string (buffer-substring beg end))
  4935. (setq move t))
  4936. (while (setq e (pop eal))
  4937. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4938. c (aref tag 0))
  4939. (push (cons c (string-to-char (car e))) det)
  4940. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4941. (substring tag 1)))))
  4942. (setq det (nreverse det))
  4943. (unless char
  4944. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4945. (setq char (read-char-exclusive)))
  4946. (setq char (or (cdr (assoc char det)) char))
  4947. (if (equal char ?\ )
  4948. (setq s "" move nil)
  4949. (unless (assoc (char-to-string char) org-emphasis-alist)
  4950. (error "No such emphasis marker: \"%c\"" char))
  4951. (setq s (char-to-string char)))
  4952. (while (and (> (length string) 1)
  4953. (equal (substring string 0 1) (substring string -1))
  4954. (assoc (substring string 0 1) org-emphasis-alist))
  4955. (setq string (substring string 1 -1)))
  4956. (setq string (concat s string s))
  4957. (if beg (delete-region beg end))
  4958. (unless (or (bolp)
  4959. (string-match (concat "[" (nth 0 erc) "\n]")
  4960. (char-to-string (char-before (point)))))
  4961. (insert " "))
  4962. (unless (or (eobp)
  4963. (string-match (concat "[" (nth 1 erc) "\n]")
  4964. (char-to-string (char-after (point)))))
  4965. (insert " ") (backward-char 1))
  4966. (insert string)
  4967. (and move (backward-char 1))))
  4968. (defconst org-nonsticky-props
  4969. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4970. (defsubst org-rear-nonsticky-at (pos)
  4971. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4972. (defun org-activate-plain-links (limit)
  4973. "Run through the buffer and add overlays to links."
  4974. (let (f hl)
  4975. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4976. (not (org-in-src-block-p)))
  4977. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4978. (setq f (get-text-property (match-beginning 0) 'face))
  4979. (setq hl (org-match-string-no-properties 0))
  4980. (if (or (eq f 'org-tag)
  4981. (and (listp f) (memq 'org-tag f)))
  4982. nil
  4983. (add-text-properties (match-beginning 0) (match-end 0)
  4984. (list 'mouse-face 'highlight
  4985. 'face 'org-link
  4986. 'htmlize-link `(:uri ,hl)
  4987. 'keymap org-mouse-map))
  4988. (org-rear-nonsticky-at (match-end 0)))
  4989. t)))
  4990. (defun org-activate-code (limit)
  4991. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4992. (progn
  4993. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4994. (remove-text-properties (match-beginning 0) (match-end 0)
  4995. '(display t invisible t intangible t))
  4996. t)))
  4997. (defcustom org-src-fontify-natively nil
  4998. "When non-nil, fontify code in code blocks."
  4999. :type 'boolean
  5000. :version "24.1"
  5001. :group 'org-appearance
  5002. :group 'org-babel)
  5003. (defcustom org-allow-promoting-top-level-subtree nil
  5004. "When non-nil, allow promoting a top level subtree.
  5005. The leading star of the top level headline will be replaced
  5006. by a #."
  5007. :type 'boolean
  5008. :version "24.1"
  5009. :group 'org-appearance)
  5010. (defun org-fontify-meta-lines-and-blocks (limit)
  5011. (condition-case nil
  5012. (org-fontify-meta-lines-and-blocks-1 limit)
  5013. (error (message "org-mode fontification error"))))
  5014. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5015. "Fontify #+ lines and blocks, in the correct ways."
  5016. (let ((case-fold-search t))
  5017. (if (re-search-forward
  5018. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5019. limit t)
  5020. (let ((beg (match-beginning 0))
  5021. (block-start (match-end 0))
  5022. (block-end nil)
  5023. (lang (match-string 7))
  5024. (beg1 (line-beginning-position 2))
  5025. (dc1 (downcase (match-string 2)))
  5026. (dc3 (downcase (match-string 3)))
  5027. end end1 quoting block-type ovl)
  5028. (cond
  5029. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5030. ;; a single line of backend-specific content
  5031. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5032. (remove-text-properties (match-beginning 0) (match-end 0)
  5033. '(display t invisible t intangible t))
  5034. (add-text-properties (match-beginning 1) (match-end 3)
  5035. '(font-lock-fontified t face org-meta-line))
  5036. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5037. '(font-lock-fontified t face org-block))
  5038. ; for backend-specific code
  5039. t)
  5040. ((and (match-end 4) (equal dc3 "+begin"))
  5041. ;; Truly a block
  5042. (setq block-type (downcase (match-string 5))
  5043. quoting (member block-type org-protecting-blocks))
  5044. (when (re-search-forward
  5045. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5046. nil t) ;; on purpose, we look further than LIMIT
  5047. (setq end (min (point-max) (match-end 0))
  5048. end1 (min (point-max) (1- (match-beginning 0))))
  5049. (setq block-end (match-beginning 0))
  5050. (when quoting
  5051. (remove-text-properties beg end
  5052. '(display t invisible t intangible t)))
  5053. (add-text-properties
  5054. beg end
  5055. '(font-lock-fontified t font-lock-multiline t))
  5056. (add-text-properties beg beg1 '(face org-meta-line))
  5057. (add-text-properties end1 (min (point-max) (1+ end))
  5058. '(face org-meta-line)) ; for end_src
  5059. (cond
  5060. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5061. (org-src-font-lock-fontify-block lang block-start block-end)
  5062. ;; remove old background overlays
  5063. (mapc (lambda (ov)
  5064. (if (eq (overlay-get ov 'face) 'org-block-background)
  5065. (delete-overlay ov)))
  5066. (overlays-at (/ (+ beg1 block-end) 2)))
  5067. ;; add a background overlay
  5068. (setq ovl (make-overlay beg1 block-end))
  5069. (overlay-put ovl 'face 'org-block-background)
  5070. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5071. (quoting
  5072. (add-text-properties beg1 (min (point-max) (1+ end1))
  5073. '(face org-block))) ; end of source block
  5074. ((not org-fontify-quote-and-verse-blocks))
  5075. ((string= block-type "quote")
  5076. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5077. ((string= block-type "verse")
  5078. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5079. (add-text-properties beg beg1 '(face org-block-begin-line))
  5080. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5081. '(face org-block-end-line))
  5082. t))
  5083. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5084. (add-text-properties
  5085. beg (match-end 3)
  5086. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5087. '(font-lock-fontified t invisible t)
  5088. '(font-lock-fontified t face org-document-info-keyword)))
  5089. (add-text-properties
  5090. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5091. (if (string-equal dc1 "+title:")
  5092. '(font-lock-fontified t face org-document-title)
  5093. '(font-lock-fontified t face org-document-info))))
  5094. ((or (equal dc1 "+results")
  5095. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5096. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5097. "+call:" "+header:" "+headers:" "+name:"))
  5098. (and (match-end 4) (equal dc3 "+attr")))
  5099. (add-text-properties
  5100. beg (match-end 0)
  5101. '(font-lock-fontified t face org-meta-line))
  5102. t)
  5103. ((member dc3 '(" " ""))
  5104. (add-text-properties
  5105. beg (match-end 0)
  5106. '(font-lock-fontified t face font-lock-comment-face)))
  5107. ((not (member (char-after beg) '(?\ ?\t)))
  5108. ;; just any other in-buffer setting, but not indented
  5109. (add-text-properties
  5110. beg (match-end 0)
  5111. '(font-lock-fontified t face org-meta-line))
  5112. t)
  5113. (t nil))))))
  5114. (defun org-activate-angle-links (limit)
  5115. "Run through the buffer and add overlays to links."
  5116. (if (and (re-search-forward org-angle-link-re limit t)
  5117. (not (org-in-src-block-p)))
  5118. (progn
  5119. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5120. (add-text-properties (match-beginning 0) (match-end 0)
  5121. (list 'mouse-face 'highlight
  5122. 'keymap org-mouse-map))
  5123. (org-rear-nonsticky-at (match-end 0))
  5124. t)))
  5125. (defun org-activate-footnote-links (limit)
  5126. "Run through the buffer and add overlays to footnotes."
  5127. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5128. (when fn
  5129. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5130. (org-remove-flyspell-overlays-in beg end)
  5131. (add-text-properties beg end
  5132. (list 'mouse-face 'highlight
  5133. 'keymap org-mouse-map
  5134. 'help-echo
  5135. (if (= (point-at-bol) beg)
  5136. "Footnote definition"
  5137. "Footnote reference")
  5138. 'font-lock-fontified t
  5139. 'font-lock-multiline t
  5140. 'face 'org-footnote))))))
  5141. (defun org-activate-bracket-links (limit)
  5142. "Run through the buffer and add overlays to bracketed links."
  5143. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5144. (not (org-in-src-block-p)))
  5145. (let* ((hl (org-match-string-no-properties 1))
  5146. (help (concat "LINK: " hl))
  5147. ;; FIXME: Above we should remove the escapes. But that
  5148. ;; requires another match, protecting match data, a lot
  5149. ;; of overhead for font-lock.
  5150. (ip (org-maybe-intangible
  5151. (list 'invisible 'org-link
  5152. 'keymap org-mouse-map 'mouse-face 'highlight
  5153. 'font-lock-multiline t 'help-echo help
  5154. 'htmlize-link `(:uri ,hl))))
  5155. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5156. 'font-lock-multiline t 'help-echo help
  5157. 'htmlize-link `(:uri ,hl))))
  5158. ;; We need to remove the invisible property here. Table narrowing
  5159. ;; may have made some of this invisible.
  5160. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5161. (remove-text-properties (match-beginning 0) (match-end 0)
  5162. '(invisible nil))
  5163. (if (match-end 3)
  5164. (progn
  5165. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5166. (org-rear-nonsticky-at (match-beginning 3))
  5167. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5168. (org-rear-nonsticky-at (match-end 3))
  5169. (add-text-properties (match-end 3) (match-end 0) ip)
  5170. (org-rear-nonsticky-at (match-end 0)))
  5171. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5172. (org-rear-nonsticky-at (match-beginning 1))
  5173. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5174. (org-rear-nonsticky-at (match-end 1))
  5175. (add-text-properties (match-end 1) (match-end 0) ip)
  5176. (org-rear-nonsticky-at (match-end 0)))
  5177. t)))
  5178. (defun org-activate-dates (limit)
  5179. "Run through the buffer and add overlays to dates."
  5180. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5181. (not (equal (char-before (match-beginning 0)) 91)))
  5182. (progn
  5183. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5184. (add-text-properties (match-beginning 0) (match-end 0)
  5185. (list 'mouse-face 'highlight
  5186. 'keymap org-mouse-map))
  5187. (org-rear-nonsticky-at (match-end 0))
  5188. (when org-display-custom-times
  5189. (if (match-end 3)
  5190. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5191. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5192. t)))
  5193. (defvar org-target-link-regexp nil
  5194. "Regular expression matching radio targets in plain text.")
  5195. (make-variable-buffer-local 'org-target-link-regexp)
  5196. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5197. "Regular expression matching a link target.")
  5198. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5199. "Regular expression matching a radio target.")
  5200. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5201. "Regular expression matching any target.")
  5202. (defun org-activate-target-links (limit)
  5203. "Run through the buffer and add overlays to target matches."
  5204. (when org-target-link-regexp
  5205. (let ((case-fold-search t))
  5206. (if (re-search-forward org-target-link-regexp limit t)
  5207. (progn
  5208. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5209. (add-text-properties (match-beginning 0) (match-end 0)
  5210. (list 'mouse-face 'highlight
  5211. 'keymap org-mouse-map
  5212. 'help-echo "Radio target link"
  5213. 'org-linked-text t))
  5214. (org-rear-nonsticky-at (match-end 0))
  5215. t)))))
  5216. (defun org-update-radio-target-regexp ()
  5217. "Find all radio targets in this file and update the regular expression."
  5218. (interactive)
  5219. (when (memq 'radio org-activate-links)
  5220. (setq org-target-link-regexp
  5221. (org-make-target-link-regexp (org-all-targets 'radio)))
  5222. (org-restart-font-lock)))
  5223. (defun org-hide-wide-columns (limit)
  5224. (let (s e)
  5225. (setq s (text-property-any (point) (or limit (point-max))
  5226. 'org-cwidth t))
  5227. (when s
  5228. (setq e (next-single-property-change s 'org-cwidth))
  5229. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5230. (goto-char e)
  5231. t)))
  5232. (defvar org-latex-and-related-regexp nil
  5233. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5234. (defvar org-match-substring-regexp)
  5235. (defvar org-match-substring-with-braces-regexp)
  5236. (defun org-compute-latex-and-related-regexp ()
  5237. "Compute regular expression for LaTeX, entities and sub/superscript.
  5238. Result depends on variable `org-highlight-latex-and-related'."
  5239. (org-set-local
  5240. 'org-latex-and-related-regexp
  5241. (let* ((re-sub
  5242. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5243. ((eq org-use-sub-superscripts '{})
  5244. (list org-match-substring-with-braces-regexp))
  5245. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5246. (re-latex
  5247. (when (memq 'latex org-highlight-latex-and-related)
  5248. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5249. (delq nil
  5250. (mapcar (lambda (x)
  5251. (and (member (car x) matchers) (nth 1 x)))
  5252. org-latex-regexps)))))
  5253. (re-entities
  5254. (when (memq 'entities org-highlight-latex-and-related)
  5255. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5256. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5257. (defun org-do-latex-and-related (limit)
  5258. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5259. LIMIT bounds the search for syntax to highlight. Stop at first
  5260. highlighted object, if any. Return t if some highlighting was
  5261. done, nil otherwise."
  5262. (when (org-string-nw-p org-latex-and-related-regexp)
  5263. (catch 'found
  5264. (while (re-search-forward org-latex-and-related-regexp limit t)
  5265. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5266. 'face))
  5267. '(org-code org-verbatim underline))
  5268. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5269. '(?_ ?^))
  5270. 1
  5271. 0)))
  5272. (font-lock-prepend-text-property
  5273. (+ offset (match-beginning 0)) (match-end 0)
  5274. 'face 'org-latex-and-related)
  5275. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5276. '(font-lock-multiline t)))
  5277. (throw 'found t)))
  5278. nil)))
  5279. (defun org-restart-font-lock ()
  5280. "Restart `font-lock-mode', to force refontification."
  5281. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5282. (font-lock-mode -1)
  5283. (font-lock-mode 1)))
  5284. (defun org-all-targets (&optional radio)
  5285. "Return a list of all targets in this file.
  5286. When optional argument RADIO is non-nil, only find radio
  5287. targets."
  5288. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5289. (save-excursion
  5290. (goto-char (point-min))
  5291. (while (re-search-forward re nil t)
  5292. ;; Make sure point is really within the object.
  5293. (backward-char)
  5294. (let ((obj (org-element-context)))
  5295. (when (memq (org-element-type obj) '(radio-target target))
  5296. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5297. rtn)))
  5298. (defun org-make-target-link-regexp (targets)
  5299. "Make regular expression matching all strings in TARGETS.
  5300. The regular expression finds the targets also if there is a line break
  5301. between words."
  5302. (and targets
  5303. (concat
  5304. "\\<\\("
  5305. (mapconcat
  5306. (lambda (x)
  5307. (setq x (regexp-quote x))
  5308. (while (string-match " +" x)
  5309. (setq x (replace-match "\\s-+" t t x)))
  5310. x)
  5311. targets
  5312. "\\|")
  5313. "\\)\\>")))
  5314. (defun org-activate-tags (limit)
  5315. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5316. (progn
  5317. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5318. (add-text-properties (match-beginning 1) (match-end 1)
  5319. (list 'mouse-face 'highlight
  5320. 'keymap org-mouse-map))
  5321. (org-rear-nonsticky-at (match-end 1))
  5322. t)))
  5323. (defun org-outline-level ()
  5324. "Compute the outline level of the heading at point.
  5325. If this is called at a normal headline, the level is the number of stars.
  5326. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5327. (save-excursion
  5328. (if (not (condition-case nil
  5329. (org-back-to-heading t)
  5330. (error nil)))
  5331. 0
  5332. (looking-at org-outline-regexp)
  5333. (1- (- (match-end 0) (match-beginning 0))))))
  5334. (defvar org-font-lock-keywords nil)
  5335. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5336. "Regular expression matching a property line.")
  5337. (defvar org-font-lock-hook nil
  5338. "Functions to be called for special font lock stuff.")
  5339. (defvar org-font-lock-set-keywords-hook nil
  5340. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5341. This is called after `org-font-lock-extra-keywords' is defined, but before
  5342. it is installed to be used by font lock. This can be useful if something
  5343. needs to be inserted at a specific position in the font-lock sequence.")
  5344. (defun org-font-lock-hook (limit)
  5345. "Run `org-font-lock-hook' within LIMIT."
  5346. (run-hook-with-args 'org-font-lock-hook limit))
  5347. (defun org-set-font-lock-defaults ()
  5348. "Set font lock defaults for the current buffer."
  5349. (let* ((em org-fontify-emphasized-text)
  5350. (lk org-activate-links)
  5351. (org-font-lock-extra-keywords
  5352. (list
  5353. ;; Call the hook
  5354. '(org-font-lock-hook)
  5355. ;; Headlines
  5356. `(,(if org-fontify-whole-heading-line
  5357. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5358. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5359. (1 (org-get-level-face 1))
  5360. (2 (org-get-level-face 2))
  5361. (3 (org-get-level-face 3)))
  5362. ;; Table lines
  5363. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5364. (1 'org-table t))
  5365. ;; Table internals
  5366. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5367. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5368. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5369. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5370. ;; Drawers
  5371. (list org-drawer-regexp '(0 'org-special-keyword t))
  5372. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5373. ;; Properties
  5374. (list org-property-re
  5375. '(1 'org-special-keyword t)
  5376. '(3 'org-property-value t))
  5377. ;; Links
  5378. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5379. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5380. (if (memq 'plain lk) '(org-activate-plain-links))
  5381. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5382. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5383. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5384. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5385. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5386. '(org-hide-wide-columns (0 nil append))
  5387. ;; TODO keyword
  5388. (list (format org-heading-keyword-regexp-format
  5389. org-todo-regexp)
  5390. '(2 (org-get-todo-face 2) t))
  5391. ;; DONE
  5392. (if org-fontify-done-headline
  5393. (list (format org-heading-keyword-regexp-format
  5394. (concat
  5395. "\\(?:"
  5396. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5397. "\\)"))
  5398. '(2 'org-headline-done t))
  5399. nil)
  5400. ;; Priorities
  5401. '(org-font-lock-add-priority-faces)
  5402. ;; Tags
  5403. '(org-font-lock-add-tag-faces)
  5404. ;; Special keywords
  5405. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5406. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5407. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5408. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5409. ;; Emphasis
  5410. (if em
  5411. (if (featurep 'xemacs)
  5412. '(org-do-emphasis-faces (0 nil append))
  5413. '(org-do-emphasis-faces)))
  5414. ;; Checkboxes
  5415. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5416. 1 'org-checkbox prepend)
  5417. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5418. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5419. (0 (org-get-checkbox-statistics-face) t)))
  5420. ;; Description list items
  5421. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5422. 1 'org-list-dt prepend)
  5423. ;; ARCHIVEd headings
  5424. (list (concat
  5425. org-outline-regexp-bol
  5426. "\\(.*:" org-archive-tag ":.*\\)")
  5427. '(1 'org-archived prepend))
  5428. ;; Specials
  5429. '(org-do-latex-and-related)
  5430. '(org-fontify-entities)
  5431. '(org-raise-scripts)
  5432. ;; Code
  5433. '(org-activate-code (1 'org-code t))
  5434. ;; COMMENT
  5435. (list (format org-heading-keyword-regexp-format
  5436. (concat "\\("
  5437. org-comment-string "\\|" org-quote-string
  5438. "\\)"))
  5439. '(2 'org-special-keyword t))
  5440. ;; Blocks and meta lines
  5441. '(org-fontify-meta-lines-and-blocks)
  5442. )))
  5443. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5444. (run-hooks 'org-font-lock-set-keywords-hook)
  5445. ;; Now set the full font-lock-keywords
  5446. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5447. (org-set-local 'font-lock-defaults
  5448. '(org-font-lock-keywords t nil nil backward-paragraph))
  5449. (kill-local-variable 'font-lock-keywords) nil))
  5450. (defun org-toggle-pretty-entities ()
  5451. "Toggle the composition display of entities as UTF8 characters."
  5452. (interactive)
  5453. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5454. (org-restart-font-lock)
  5455. (if org-pretty-entities
  5456. (message "Entities are displayed as UTF8 characters")
  5457. (save-restriction
  5458. (widen)
  5459. (org-decompose-region (point-min) (point-max))
  5460. (message "Entities are displayed plain"))))
  5461. (defvar org-custom-properties-overlays nil
  5462. "List of overlays used for custom properties.")
  5463. (make-variable-buffer-local 'org-custom-properties-overlays)
  5464. (defun org-toggle-custom-properties-visibility ()
  5465. "Display or hide properties in `org-custom-properties'."
  5466. (interactive)
  5467. (if org-custom-properties-overlays
  5468. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5469. (setq org-custom-properties-overlays nil))
  5470. (unless (not org-custom-properties)
  5471. (save-excursion
  5472. (save-restriction
  5473. (widen)
  5474. (goto-char (point-min))
  5475. (while (re-search-forward org-property-re nil t)
  5476. (mapc (lambda(p)
  5477. (when (equal p (substring (match-string 1) 1 -1))
  5478. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5479. (overlay-put o 'invisible t)
  5480. (overlay-put o 'org-custom-property t)
  5481. (push o org-custom-properties-overlays))))
  5482. org-custom-properties)))))))
  5483. (defun org-fontify-entities (limit)
  5484. "Find an entity to fontify."
  5485. (let (ee)
  5486. (when org-pretty-entities
  5487. (catch 'match
  5488. (while (re-search-forward
  5489. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5490. limit t)
  5491. (if (and (not (org-in-indented-comment-line))
  5492. (setq ee (org-entity-get (match-string 1)))
  5493. (= (length (nth 6 ee)) 1))
  5494. (let*
  5495. ((end (if (equal (match-string 2) "{}")
  5496. (match-end 2)
  5497. (match-end 1))))
  5498. (add-text-properties
  5499. (match-beginning 0) end
  5500. (list 'font-lock-fontified t))
  5501. (compose-region (match-beginning 0) end
  5502. (nth 6 ee) nil)
  5503. (backward-char 1)
  5504. (throw 'match t))))
  5505. nil))))
  5506. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5507. "Fontify string S like in Org-mode."
  5508. (with-temp-buffer
  5509. (insert s)
  5510. (let ((org-odd-levels-only odd-levels))
  5511. (org-mode)
  5512. (font-lock-fontify-buffer)
  5513. (buffer-string))))
  5514. (defvar org-m nil)
  5515. (defvar org-l nil)
  5516. (defvar org-f nil)
  5517. (defun org-get-level-face (n)
  5518. "Get the right face for match N in font-lock matching of headlines."
  5519. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5520. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5521. (if org-cycle-level-faces
  5522. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5523. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5524. (cond
  5525. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5526. ((eq n 2) org-f)
  5527. (t (if org-level-color-stars-only nil org-f))))
  5528. (defun org-get-todo-face (kwd)
  5529. "Get the right face for a TODO keyword KWD.
  5530. If KWD is a number, get the corresponding match group."
  5531. (if (numberp kwd) (setq kwd (match-string kwd)))
  5532. (or (org-face-from-face-or-color
  5533. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5534. (and (member kwd org-done-keywords) 'org-done)
  5535. 'org-todo))
  5536. (defun org-face-from-face-or-color (context inherit face-or-color)
  5537. "Create a face list that inherits INHERIT, but sets the foreground color.
  5538. When FACE-OR-COLOR is not a string, just return it."
  5539. (if (stringp face-or-color)
  5540. (list :inherit inherit
  5541. (cdr (assoc context org-faces-easy-properties))
  5542. face-or-color)
  5543. face-or-color))
  5544. (defun org-font-lock-add-tag-faces (limit)
  5545. "Add the special tag faces."
  5546. (when (and org-tag-faces org-tags-special-faces-re)
  5547. (while (re-search-forward org-tags-special-faces-re limit t)
  5548. (add-text-properties (match-beginning 1) (match-end 1)
  5549. (list 'face (org-get-tag-face 1)
  5550. 'font-lock-fontified t))
  5551. (backward-char 1))))
  5552. (defun org-font-lock-add-priority-faces (limit)
  5553. "Add the special priority faces."
  5554. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5555. (when (save-match-data (org-at-heading-p))
  5556. (add-text-properties
  5557. (match-beginning 0) (match-end 0)
  5558. (list 'face (or (org-face-from-face-or-color
  5559. 'priority 'org-priority
  5560. (cdr (assoc (char-after (match-beginning 1))
  5561. org-priority-faces)))
  5562. 'org-priority)
  5563. 'font-lock-fontified t)))))
  5564. (defun org-get-tag-face (kwd)
  5565. "Get the right face for a TODO keyword KWD.
  5566. If KWD is a number, get the corresponding match group."
  5567. (if (numberp kwd) (setq kwd (match-string kwd)))
  5568. (or (org-face-from-face-or-color
  5569. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5570. 'org-tag))
  5571. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5572. "Remove fontification and activation overlays from links."
  5573. (font-lock-default-unfontify-region beg end)
  5574. (let* ((buffer-undo-list t)
  5575. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5576. (inhibit-modification-hooks t)
  5577. deactivate-mark buffer-file-name buffer-file-truename)
  5578. (org-decompose-region beg end)
  5579. (remove-text-properties beg end
  5580. '(mouse-face t keymap t org-linked-text t
  5581. invisible t intangible t
  5582. org-no-flyspell t org-emphasis t))
  5583. (org-remove-font-lock-display-properties beg end)))
  5584. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5585. ((raise 0.3) (height 0.7))
  5586. ((raise -0.5))
  5587. ((raise 0.5)))
  5588. "Display properties for showing superscripts and subscripts.")
  5589. (defun org-remove-font-lock-display-properties (beg end)
  5590. "Remove specific display properties that have been added by font lock.
  5591. The will remove the raise properties that are used to show superscripts
  5592. and subscripts."
  5593. (let (next prop)
  5594. (while (< beg end)
  5595. (setq next (next-single-property-change beg 'display nil end)
  5596. prop (get-text-property beg 'display))
  5597. (if (member prop org-script-display)
  5598. (put-text-property beg next 'display nil))
  5599. (setq beg next))))
  5600. (defun org-raise-scripts (limit)
  5601. "Add raise properties to sub/superscripts."
  5602. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5603. (if (re-search-forward
  5604. (if (eq org-use-sub-superscripts t)
  5605. org-match-substring-regexp
  5606. org-match-substring-with-braces-regexp)
  5607. limit t)
  5608. (let* ((pos (point)) table-p comment-p
  5609. (mpos (match-beginning 3))
  5610. (emph-p (get-text-property mpos 'org-emphasis))
  5611. (link-p (get-text-property mpos 'mouse-face))
  5612. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5613. (goto-char (point-at-bol))
  5614. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5615. comment-p (org-looking-at-p "[ \t]*#"))
  5616. (goto-char pos)
  5617. ;; FIXME: Should we go back one character here, for a_b^c
  5618. ;; (goto-char (1- pos)) ;????????????????????
  5619. (if (or comment-p emph-p link-p keyw-p)
  5620. t
  5621. (put-text-property (match-beginning 3) (match-end 0)
  5622. 'display
  5623. (if (equal (char-after (match-beginning 2)) ?^)
  5624. (nth (if table-p 3 1) org-script-display)
  5625. (nth (if table-p 2 0) org-script-display)))
  5626. (add-text-properties (match-beginning 2) (match-end 2)
  5627. (list 'invisible t
  5628. 'org-dwidth t 'org-dwidth-n 1))
  5629. (if (and (eq (char-after (match-beginning 3)) ?{)
  5630. (eq (char-before (match-end 3)) ?}))
  5631. (progn
  5632. (add-text-properties
  5633. (match-beginning 3) (1+ (match-beginning 3))
  5634. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5635. (add-text-properties
  5636. (1- (match-end 3)) (match-end 3)
  5637. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5638. t)))))
  5639. ;;;; Visibility cycling, including org-goto and indirect buffer
  5640. ;;; Cycling
  5641. (defvar org-cycle-global-status nil)
  5642. (make-variable-buffer-local 'org-cycle-global-status)
  5643. (put 'org-cycle-global-status 'org-state t)
  5644. (defvar org-cycle-subtree-status nil)
  5645. (make-variable-buffer-local 'org-cycle-subtree-status)
  5646. (put 'org-cycle-subtree-status 'org-state t)
  5647. (defvar org-inlinetask-min-level)
  5648. ;;;###autoload
  5649. (defun org-cycle (&optional arg)
  5650. "TAB-action and visibility cycling for Org-mode.
  5651. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5652. is outline visibility cycling, but it also invokes other actions
  5653. in special contexts.
  5654. - When this function is called with a prefix argument, rotate the entire
  5655. buffer through 3 states (global cycling)
  5656. 1. OVERVIEW: Show only top-level headlines.
  5657. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5658. 3. SHOW ALL: Show everything.
  5659. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5660. determined by the variable `org-startup-folded', and by any VISIBILITY
  5661. properties in the buffer.
  5662. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5663. including any drawers.
  5664. - When inside a table, re-align the table and move to the next field.
  5665. - When point is at the beginning of a headline, rotate the subtree started
  5666. by this line through 3 different states (local cycling)
  5667. 1. FOLDED: Only the main headline is shown.
  5668. 2. CHILDREN: The main headline and the direct children are shown.
  5669. From this state, you can move to one of the children
  5670. and zoom in further.
  5671. 3. SUBTREE: Show the entire subtree, including body text.
  5672. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5673. - When point is at the beginning of an empty headline and the variable
  5674. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5675. of the headline by demoting and promoting it to likely levels. This
  5676. speeds up creation document structure by pressing TAB once or several
  5677. times right after creating a new headline.
  5678. - When there is a numeric prefix, go up to a heading with level ARG, do
  5679. a `show-subtree' and return to the previous cursor position. If ARG
  5680. is negative, go up that many levels.
  5681. - When point is not at the beginning of a headline, execute the global
  5682. binding for TAB, which is re-indenting the line. See the option
  5683. `org-cycle-emulate-tab' for details.
  5684. - Special case: if point is at the beginning of the buffer and there is
  5685. no headline in line 1, this function will act as if called with prefix arg
  5686. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5687. But only if also the variable `org-cycle-global-at-bob' is t."
  5688. (interactive "P")
  5689. (org-load-modules-maybe)
  5690. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5691. (and org-cycle-level-after-item/entry-creation
  5692. (or (org-cycle-level)
  5693. (org-cycle-item-indentation))))
  5694. (let* (message-log-max ; Don't populate the *Messages* buffer
  5695. (limit-level
  5696. (or org-cycle-max-level
  5697. (and (boundp 'org-inlinetask-min-level)
  5698. org-inlinetask-min-level
  5699. (1- org-inlinetask-min-level))))
  5700. (nstars (and limit-level
  5701. (if org-odd-levels-only
  5702. (and limit-level (1- (* limit-level 2)))
  5703. limit-level)))
  5704. (org-outline-regexp
  5705. (if (not (derived-mode-p 'org-mode))
  5706. outline-regexp
  5707. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5708. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5709. (not (looking-at org-outline-regexp))))
  5710. (org-cycle-hook
  5711. (if bob-special
  5712. (delq 'org-optimize-window-after-visibility-change
  5713. (copy-sequence org-cycle-hook))
  5714. org-cycle-hook))
  5715. (pos (point)))
  5716. (if (or bob-special (equal arg '(4)))
  5717. ;; special case: use global cycling
  5718. (setq arg t))
  5719. (cond
  5720. ((equal arg '(16))
  5721. (setq last-command 'dummy)
  5722. (org-set-startup-visibility)
  5723. (message "Startup visibility, plus VISIBILITY properties"))
  5724. ((equal arg '(64))
  5725. (show-all)
  5726. (message "Entire buffer visible, including drawers"))
  5727. ;; Table: enter it or move to the next field.
  5728. ((org-at-table-p 'any)
  5729. (if (org-at-table.el-p)
  5730. (message "Use C-c ' to edit table.el tables")
  5731. (if arg (org-table-edit-field t)
  5732. (org-table-justify-field-maybe)
  5733. (call-interactively 'org-table-next-field))))
  5734. ((run-hook-with-args-until-success
  5735. 'org-tab-after-check-for-table-hook))
  5736. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5737. ((eq arg t) (org-cycle-internal-global))
  5738. ;; Drawers: delegate to `org-flag-drawer'.
  5739. ((and org-drawers org-drawer-regexp
  5740. (save-excursion
  5741. (beginning-of-line 1)
  5742. (looking-at org-drawer-regexp)))
  5743. (org-flag-drawer ; toggle block visibility
  5744. (not (get-char-property (match-end 0) 'invisible))))
  5745. ;; Show-subtree, ARG levels up from here.
  5746. ((integerp arg)
  5747. (save-excursion
  5748. (org-back-to-heading)
  5749. (outline-up-heading (if (< arg 0) (- arg)
  5750. (- (funcall outline-level) arg)))
  5751. (org-show-subtree)))
  5752. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5753. ((and (featurep 'org-inlinetask)
  5754. (org-inlinetask-at-task-p)
  5755. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5756. (org-inlinetask-toggle-visibility))
  5757. ((org-try-cdlatex-tab))
  5758. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5759. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5760. (save-excursion (beginning-of-line 1)
  5761. (looking-at org-outline-regexp)))
  5762. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5763. (org-cycle-internal-local))
  5764. ;; From there: TAB emulation and template completion.
  5765. (buffer-read-only (org-back-to-heading))
  5766. ((run-hook-with-args-until-success
  5767. 'org-tab-after-check-for-cycling-hook))
  5768. ((org-try-structure-completion))
  5769. ((run-hook-with-args-until-success
  5770. 'org-tab-before-tab-emulation-hook))
  5771. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5772. (or (not (bolp))
  5773. (not (looking-at org-outline-regexp))))
  5774. (call-interactively (global-key-binding "\t")))
  5775. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5776. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5777. (or (and (eq org-cycle-emulate-tab 'white)
  5778. (= (match-end 0) (point-at-eol)))
  5779. (and (eq org-cycle-emulate-tab 'whitestart)
  5780. (>= (match-end 0) pos))))
  5781. t
  5782. (eq org-cycle-emulate-tab t))
  5783. (call-interactively (global-key-binding "\t")))
  5784. (t (save-excursion
  5785. (org-back-to-heading)
  5786. (org-cycle)))))))
  5787. (defun org-cycle-internal-global ()
  5788. "Do the global cycling action."
  5789. ;; Hack to avoid display of messages for .org attachments in Gnus
  5790. (let (message-log-max ; Don't populate the *Messages* buffer
  5791. (ga (string-match "\\*fontification" (buffer-name))))
  5792. (cond
  5793. ((and (eq last-command this-command)
  5794. (eq org-cycle-global-status 'overview))
  5795. ;; We just created the overview - now do table of contents
  5796. ;; This can be slow in very large buffers, so indicate action
  5797. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5798. (unless ga (message "CONTENTS..."))
  5799. (org-content)
  5800. (unless ga (message "CONTENTS...done"))
  5801. (setq org-cycle-global-status 'contents)
  5802. (run-hook-with-args 'org-cycle-hook 'contents))
  5803. ((and (eq last-command this-command)
  5804. (eq org-cycle-global-status 'contents))
  5805. ;; We just showed the table of contents - now show everything
  5806. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5807. (show-all)
  5808. (unless ga (message "SHOW ALL"))
  5809. (setq org-cycle-global-status 'all)
  5810. (run-hook-with-args 'org-cycle-hook 'all))
  5811. (t
  5812. ;; Default action: go to overview
  5813. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5814. (org-overview)
  5815. (unless ga (message "OVERVIEW"))
  5816. (setq org-cycle-global-status 'overview)
  5817. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5818. (defun org-cycle-internal-local ()
  5819. "Do the local cycling action."
  5820. (let (message-log-max ; Don't populate the *Messages* buffer
  5821. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5822. ;; First, determine end of headline (EOH), end of subtree or item
  5823. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5824. (save-excursion
  5825. (if (org-at-item-p)
  5826. (progn
  5827. (beginning-of-line)
  5828. (setq struct (org-list-struct))
  5829. (setq eoh (point-at-eol))
  5830. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5831. (setq has-children (org-list-has-child-p (point) struct)))
  5832. (org-back-to-heading)
  5833. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5834. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5835. (setq has-children
  5836. (or (save-excursion
  5837. (let ((level (funcall outline-level)))
  5838. (outline-next-heading)
  5839. (and (org-at-heading-p t)
  5840. (> (funcall outline-level) level))))
  5841. (save-excursion
  5842. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5843. ;; Determine end invisible part of buffer (EOL)
  5844. (beginning-of-line 2)
  5845. ;; XEmacs doesn't have `next-single-char-property-change'
  5846. (if (featurep 'xemacs)
  5847. (while (and (not (eobp)) ;; this is like `next-line'
  5848. (get-char-property (1- (point)) 'invisible))
  5849. (beginning-of-line 2))
  5850. (while (and (not (eobp)) ;; this is like `next-line'
  5851. (get-char-property (1- (point)) 'invisible))
  5852. (goto-char (next-single-char-property-change (point) 'invisible))
  5853. (and (eolp) (beginning-of-line 2))))
  5854. (setq eol (point)))
  5855. ;; Find out what to do next and set `this-command'
  5856. (cond
  5857. ((= eos eoh)
  5858. ;; Nothing is hidden behind this heading
  5859. (unless (org-before-first-heading-p)
  5860. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5861. (message "EMPTY ENTRY")
  5862. (setq org-cycle-subtree-status nil)
  5863. (save-excursion
  5864. (goto-char eos)
  5865. (outline-next-heading)
  5866. (if (outline-invisible-p) (org-flag-heading nil))))
  5867. ((and (or (>= eol eos)
  5868. (not (string-match "\\S-" (buffer-substring eol eos))))
  5869. (or has-children
  5870. (not (setq children-skipped
  5871. org-cycle-skip-children-state-if-no-children))))
  5872. ;; Entire subtree is hidden in one line: children view
  5873. (unless (org-before-first-heading-p)
  5874. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5875. (if (org-at-item-p)
  5876. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5877. (org-show-entry)
  5878. (org-with-limited-levels (show-children))
  5879. ;; FIXME: This slows down the func way too much.
  5880. ;; How keep drawers hidden in subtree anyway?
  5881. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5882. ;; (org-cycle-hide-drawers 'subtree))
  5883. ;; Fold every list in subtree to top-level items.
  5884. (when (eq org-cycle-include-plain-lists 'integrate)
  5885. (save-excursion
  5886. (org-back-to-heading)
  5887. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5888. (beginning-of-line 1)
  5889. (let* ((struct (org-list-struct))
  5890. (prevs (org-list-prevs-alist struct))
  5891. (end (org-list-get-bottom-point struct)))
  5892. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5893. (org-list-get-all-items (point) struct prevs))
  5894. (goto-char end))))))
  5895. (message "CHILDREN")
  5896. (save-excursion
  5897. (goto-char eos)
  5898. (outline-next-heading)
  5899. (if (outline-invisible-p) (org-flag-heading nil)))
  5900. (setq org-cycle-subtree-status 'children)
  5901. (unless (org-before-first-heading-p)
  5902. (run-hook-with-args 'org-cycle-hook 'children)))
  5903. ((or children-skipped
  5904. (and (eq last-command this-command)
  5905. (eq org-cycle-subtree-status 'children)))
  5906. ;; We just showed the children, or no children are there,
  5907. ;; now show everything.
  5908. (unless (org-before-first-heading-p)
  5909. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5910. (outline-flag-region eoh eos nil)
  5911. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5912. (setq org-cycle-subtree-status 'subtree)
  5913. (unless (org-before-first-heading-p)
  5914. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5915. (t
  5916. ;; Default action: hide the subtree.
  5917. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5918. (outline-flag-region eoh eos t)
  5919. (message "FOLDED")
  5920. (setq org-cycle-subtree-status 'folded)
  5921. (unless (org-before-first-heading-p)
  5922. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5923. ;;;###autoload
  5924. (defun org-global-cycle (&optional arg)
  5925. "Cycle the global visibility. For details see `org-cycle'.
  5926. With \\[universal-argument] prefix arg, switch to startup visibility.
  5927. With a numeric prefix, show all headlines up to that level."
  5928. (interactive "P")
  5929. (let ((org-cycle-include-plain-lists
  5930. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5931. (cond
  5932. ((integerp arg)
  5933. (show-all)
  5934. (hide-sublevels arg)
  5935. (setq org-cycle-global-status 'contents))
  5936. ((equal arg '(4))
  5937. (org-set-startup-visibility)
  5938. (message "Startup visibility, plus VISIBILITY properties."))
  5939. (t
  5940. (org-cycle '(4))))))
  5941. (defun org-set-startup-visibility ()
  5942. "Set the visibility required by startup options and properties."
  5943. (cond
  5944. ((eq org-startup-folded t)
  5945. (org-cycle '(4)))
  5946. ((eq org-startup-folded 'content)
  5947. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5948. (org-cycle '(4)) (org-cycle '(4)))))
  5949. (unless (eq org-startup-folded 'showeverything)
  5950. (if org-hide-block-startup (org-hide-block-all))
  5951. (org-set-visibility-according-to-property 'no-cleanup)
  5952. (org-cycle-hide-archived-subtrees 'all)
  5953. (org-cycle-hide-drawers 'all)
  5954. (org-cycle-show-empty-lines t)))
  5955. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5956. "Switch subtree visibilities according to :VISIBILITY: property."
  5957. (interactive)
  5958. (let (org-show-entry-below state)
  5959. (save-excursion
  5960. (goto-char (point-min))
  5961. (while (re-search-forward
  5962. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5963. nil t)
  5964. (setq state (match-string 1))
  5965. (save-excursion
  5966. (org-back-to-heading t)
  5967. (hide-subtree)
  5968. (org-reveal)
  5969. (cond
  5970. ((equal state '("fold" "folded"))
  5971. (hide-subtree))
  5972. ((equal state "children")
  5973. (org-show-hidden-entry)
  5974. (show-children))
  5975. ((equal state "content")
  5976. (save-excursion
  5977. (save-restriction
  5978. (org-narrow-to-subtree)
  5979. (org-content))))
  5980. ((member state '("all" "showall"))
  5981. (show-subtree)))))
  5982. (unless no-cleanup
  5983. (org-cycle-hide-archived-subtrees 'all)
  5984. (org-cycle-hide-drawers 'all)
  5985. (org-cycle-show-empty-lines 'all)))))
  5986. ;; This function uses outline-regexp instead of the more fundamental
  5987. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5988. ;; buffers, where outline-regexp is needed.
  5989. (defun org-overview ()
  5990. "Switch to overview mode, showing only top-level headlines.
  5991. Really, this shows all headlines with level equal or greater than the level
  5992. of the first headline in the buffer. This is important, because if the
  5993. first headline is not level one, then (hide-sublevels 1) gives confusing
  5994. results."
  5995. (interactive)
  5996. (let ((l (org-current-line))
  5997. (level (save-excursion
  5998. (goto-char (point-min))
  5999. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6000. (progn
  6001. (goto-char (match-beginning 0))
  6002. (funcall outline-level))))))
  6003. (and level (hide-sublevels level))
  6004. (recenter '(4))
  6005. (org-goto-line l)))
  6006. (defun org-content (&optional arg)
  6007. "Show all headlines in the buffer, like a table of contents.
  6008. With numerical argument N, show content up to level N."
  6009. (interactive "P")
  6010. (save-excursion
  6011. ;; Visit all headings and show their offspring
  6012. (and (integerp arg) (org-overview))
  6013. (goto-char (point-max))
  6014. (catch 'exit
  6015. (while (and (progn (condition-case nil
  6016. (outline-previous-visible-heading 1)
  6017. (error (goto-char (point-min))))
  6018. t)
  6019. (looking-at org-outline-regexp))
  6020. (if (integerp arg)
  6021. (show-children (1- arg))
  6022. (show-branches))
  6023. (if (bobp) (throw 'exit nil))))))
  6024. (defun org-optimize-window-after-visibility-change (state)
  6025. "Adjust the window after a change in outline visibility.
  6026. This function is the default value of the hook `org-cycle-hook'."
  6027. (when (get-buffer-window (current-buffer))
  6028. (cond
  6029. ((eq state 'content) nil)
  6030. ((eq state 'all) nil)
  6031. ((eq state 'folded) nil)
  6032. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6033. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6034. (defun org-remove-empty-overlays-at (pos)
  6035. "Remove outline overlays that do not contain non-white stuff."
  6036. (mapc
  6037. (lambda (o)
  6038. (and (eq 'outline (overlay-get o 'invisible))
  6039. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6040. (overlay-end o))))
  6041. (delete-overlay o)))
  6042. (overlays-at pos)))
  6043. (defun org-clean-visibility-after-subtree-move ()
  6044. "Fix visibility issues after moving a subtree."
  6045. ;; First, find a reasonable region to look at:
  6046. ;; Start two siblings above, end three below
  6047. (let* ((beg (save-excursion
  6048. (and (org-get-last-sibling)
  6049. (org-get-last-sibling))
  6050. (point)))
  6051. (end (save-excursion
  6052. (and (org-get-next-sibling)
  6053. (org-get-next-sibling)
  6054. (org-get-next-sibling))
  6055. (if (org-at-heading-p)
  6056. (point-at-eol)
  6057. (point))))
  6058. (level (looking-at "\\*+"))
  6059. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6060. (save-excursion
  6061. (save-restriction
  6062. (narrow-to-region beg end)
  6063. (when re
  6064. ;; Properly fold already folded siblings
  6065. (goto-char (point-min))
  6066. (while (re-search-forward re nil t)
  6067. (if (and (not (outline-invisible-p))
  6068. (save-excursion
  6069. (goto-char (point-at-eol)) (outline-invisible-p)))
  6070. (hide-entry))))
  6071. (org-cycle-show-empty-lines 'overview)
  6072. (org-cycle-hide-drawers 'overview)))))
  6073. (defun org-cycle-show-empty-lines (state)
  6074. "Show empty lines above all visible headlines.
  6075. The region to be covered depends on STATE when called through
  6076. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6077. entire buffer covered. Note that an empty line is only shown if there
  6078. are at least `org-cycle-separator-lines' empty lines before the headline."
  6079. (when (not (= org-cycle-separator-lines 0))
  6080. (save-excursion
  6081. (let* ((n (abs org-cycle-separator-lines))
  6082. (re (cond
  6083. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6084. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6085. (t (let ((ns (number-to-string (- n 2))))
  6086. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6087. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6088. beg end b e)
  6089. (cond
  6090. ((memq state '(overview contents t))
  6091. (setq beg (point-min) end (point-max)))
  6092. ((memq state '(children folded))
  6093. (setq beg (point) end (progn (org-end-of-subtree t t)
  6094. (beginning-of-line 2)
  6095. (point)))))
  6096. (when beg
  6097. (goto-char beg)
  6098. (while (re-search-forward re end t)
  6099. (unless (get-char-property (match-end 1) 'invisible)
  6100. (setq e (match-end 1))
  6101. (if (< org-cycle-separator-lines 0)
  6102. (setq b (save-excursion
  6103. (goto-char (match-beginning 0))
  6104. (org-back-over-empty-lines)
  6105. (if (save-excursion
  6106. (goto-char (max (point-min) (1- (point))))
  6107. (org-at-heading-p))
  6108. (1- (point))
  6109. (point))))
  6110. (setq b (match-beginning 1)))
  6111. (outline-flag-region b e nil)))))))
  6112. ;; Never hide empty lines at the end of the file.
  6113. (save-excursion
  6114. (goto-char (point-max))
  6115. (outline-previous-heading)
  6116. (outline-end-of-heading)
  6117. (if (and (looking-at "[ \t\n]+")
  6118. (= (match-end 0) (point-max)))
  6119. (outline-flag-region (point) (match-end 0) nil))))
  6120. (defun org-show-empty-lines-in-parent ()
  6121. "Move to the parent and re-show empty lines before visible headlines."
  6122. (save-excursion
  6123. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6124. (org-cycle-show-empty-lines context))))
  6125. (defun org-files-list ()
  6126. "Return `org-agenda-files' list, plus all open org-mode files.
  6127. This is useful for operations that need to scan all of a user's
  6128. open and agenda-wise Org files."
  6129. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6130. (dolist (buf (buffer-list))
  6131. (with-current-buffer buf
  6132. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6133. (let ((file (expand-file-name (buffer-file-name))))
  6134. (unless (member file files)
  6135. (push file files))))))
  6136. files))
  6137. (defsubst org-entry-beginning-position ()
  6138. "Return the beginning position of the current entry."
  6139. (save-excursion (outline-back-to-heading t) (point)))
  6140. (defsubst org-entry-end-position ()
  6141. "Return the end position of the current entry."
  6142. (save-excursion (outline-next-heading) (point)))
  6143. (defun org-cycle-hide-drawers (state)
  6144. "Re-hide all drawers after a visibility state change."
  6145. (when (and (derived-mode-p 'org-mode)
  6146. (not (memq state '(overview folded contents))))
  6147. (save-excursion
  6148. (let* ((globalp (memq state '(contents all)))
  6149. (beg (if globalp (point-min) (point)))
  6150. (end (if globalp (point-max)
  6151. (if (eq state 'children)
  6152. (save-excursion (outline-next-heading) (point))
  6153. (org-end-of-subtree t)))))
  6154. (goto-char beg)
  6155. (while (re-search-forward org-drawer-regexp end t)
  6156. (org-flag-drawer t))))))
  6157. (defun org-cycle-hide-inline-tasks (state)
  6158. "Re-hide inline task when switching to 'contents visibility state."
  6159. (when (and (eq state 'contents)
  6160. (boundp 'org-inlinetask-min-level)
  6161. org-inlinetask-min-level)
  6162. (hide-sublevels (1- org-inlinetask-min-level))))
  6163. (defun org-flag-drawer (flag)
  6164. "When FLAG is non-nil, hide the drawer we are within.
  6165. Otherwise make it visible."
  6166. (save-excursion
  6167. (beginning-of-line 1)
  6168. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6169. (let ((b (match-end 0)))
  6170. (if (re-search-forward
  6171. "^[ \t]*:END:"
  6172. (save-excursion (outline-next-heading) (point)) t)
  6173. (outline-flag-region b (point-at-eol) flag)
  6174. (error ":END: line missing at position %s" b))))))
  6175. (defun org-subtree-end-visible-p ()
  6176. "Is the end of the current subtree visible?"
  6177. (pos-visible-in-window-p
  6178. (save-excursion (org-end-of-subtree t) (point))))
  6179. (defun org-first-headline-recenter (&optional N)
  6180. "Move cursor to the first headline and recenter the headline.
  6181. Optional argument N means put the headline into the Nth line of the window."
  6182. (goto-char (point-min))
  6183. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6184. (beginning-of-line)
  6185. (recenter (prefix-numeric-value N))))
  6186. ;;; Saving and restoring visibility
  6187. (defun org-outline-overlay-data (&optional use-markers)
  6188. "Return a list of the locations of all outline overlays.
  6189. These are overlays with the `invisible' property value `outline'.
  6190. The return value is a list of cons cells, with start and stop
  6191. positions for each overlay.
  6192. If USE-MARKERS is set, return the positions as markers."
  6193. (let (beg end)
  6194. (save-excursion
  6195. (save-restriction
  6196. (widen)
  6197. (delq nil
  6198. (mapcar (lambda (o)
  6199. (when (eq (overlay-get o 'invisible) 'outline)
  6200. (setq beg (overlay-start o)
  6201. end (overlay-end o))
  6202. (and beg end (> end beg)
  6203. (if use-markers
  6204. (cons (move-marker (make-marker) beg)
  6205. (move-marker (make-marker) end))
  6206. (cons beg end)))))
  6207. (overlays-in (point-min) (point-max))))))))
  6208. (defun org-set-outline-overlay-data (data)
  6209. "Create visibility overlays for all positions in DATA.
  6210. DATA should have been made by `org-outline-overlay-data'."
  6211. (let (o)
  6212. (save-excursion
  6213. (save-restriction
  6214. (widen)
  6215. (show-all)
  6216. (mapc (lambda (c)
  6217. (outline-flag-region (car c) (cdr c) t))
  6218. data)))))
  6219. ;;; Folding of blocks
  6220. (defvar org-hide-block-overlays nil
  6221. "Overlays hiding blocks.")
  6222. (make-variable-buffer-local 'org-hide-block-overlays)
  6223. (defun org-block-map (function &optional start end)
  6224. "Call FUNCTION at the head of all source blocks in the current buffer.
  6225. Optional arguments START and END can be used to limit the range."
  6226. (let ((start (or start (point-min)))
  6227. (end (or end (point-max))))
  6228. (save-excursion
  6229. (goto-char start)
  6230. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6231. (save-excursion
  6232. (save-match-data
  6233. (goto-char (match-beginning 0))
  6234. (funcall function)))))))
  6235. (defun org-hide-block-toggle-all ()
  6236. "Toggle the visibility of all blocks in the current buffer."
  6237. (org-block-map #'org-hide-block-toggle))
  6238. (defun org-hide-block-all ()
  6239. "Fold all blocks in the current buffer."
  6240. (interactive)
  6241. (org-show-block-all)
  6242. (org-block-map #'org-hide-block-toggle-maybe))
  6243. (defun org-show-block-all ()
  6244. "Unfold all blocks in the current buffer."
  6245. (interactive)
  6246. (mapc 'delete-overlay org-hide-block-overlays)
  6247. (setq org-hide-block-overlays nil))
  6248. (defun org-hide-block-toggle-maybe ()
  6249. "Toggle visibility of block at point."
  6250. (interactive)
  6251. (let ((case-fold-search t))
  6252. (if (save-excursion
  6253. (beginning-of-line 1)
  6254. (looking-at org-block-regexp))
  6255. (progn (org-hide-block-toggle)
  6256. t) ;; to signal that we took action
  6257. nil))) ;; to signal that we did not
  6258. (defun org-hide-block-toggle (&optional force)
  6259. "Toggle the visibility of the current block."
  6260. (interactive)
  6261. (save-excursion
  6262. (beginning-of-line)
  6263. (if (re-search-forward org-block-regexp nil t)
  6264. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6265. (end (match-end 0)) ;; end of entire body
  6266. ov)
  6267. (if (memq t (mapcar (lambda (overlay)
  6268. (eq (overlay-get overlay 'invisible)
  6269. 'org-hide-block))
  6270. (overlays-at start)))
  6271. (if (or (not force) (eq force 'off))
  6272. (mapc (lambda (ov)
  6273. (when (member ov org-hide-block-overlays)
  6274. (setq org-hide-block-overlays
  6275. (delq ov org-hide-block-overlays)))
  6276. (when (eq (overlay-get ov 'invisible)
  6277. 'org-hide-block)
  6278. (delete-overlay ov)))
  6279. (overlays-at start)))
  6280. (setq ov (make-overlay start end))
  6281. (overlay-put ov 'invisible 'org-hide-block)
  6282. ;; make the block accessible to isearch
  6283. (overlay-put
  6284. ov 'isearch-open-invisible
  6285. (lambda (ov)
  6286. (when (member ov org-hide-block-overlays)
  6287. (setq org-hide-block-overlays
  6288. (delq ov org-hide-block-overlays)))
  6289. (when (eq (overlay-get ov 'invisible)
  6290. 'org-hide-block)
  6291. (delete-overlay ov))))
  6292. (push ov org-hide-block-overlays)))
  6293. (error "Not looking at a source block"))))
  6294. ;; org-tab-after-check-for-cycling-hook
  6295. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6296. ;; Remove overlays when changing major mode
  6297. (add-hook 'org-mode-hook
  6298. (lambda () (org-add-hook 'change-major-mode-hook
  6299. 'org-show-block-all 'append 'local)))
  6300. ;;; Org-goto
  6301. (defvar org-goto-window-configuration nil)
  6302. (defvar org-goto-marker nil)
  6303. (defvar org-goto-map)
  6304. (defun org-goto-map ()
  6305. "Set the keymap `org-goto'."
  6306. (setq org-goto-map
  6307. (let ((map (make-sparse-keymap)))
  6308. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6309. mouse-drag-region universal-argument org-occur))
  6310. cmd)
  6311. (while (setq cmd (pop cmds))
  6312. (substitute-key-definition cmd cmd map global-map)))
  6313. (suppress-keymap map)
  6314. (org-defkey map "\C-m" 'org-goto-ret)
  6315. (org-defkey map [(return)] 'org-goto-ret)
  6316. (org-defkey map [(left)] 'org-goto-left)
  6317. (org-defkey map [(right)] 'org-goto-right)
  6318. (org-defkey map [(control ?g)] 'org-goto-quit)
  6319. (org-defkey map "\C-i" 'org-cycle)
  6320. (org-defkey map [(tab)] 'org-cycle)
  6321. (org-defkey map [(down)] 'outline-next-visible-heading)
  6322. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6323. (if org-goto-auto-isearch
  6324. (if (fboundp 'define-key-after)
  6325. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6326. nil)
  6327. (org-defkey map "q" 'org-goto-quit)
  6328. (org-defkey map "n" 'outline-next-visible-heading)
  6329. (org-defkey map "p" 'outline-previous-visible-heading)
  6330. (org-defkey map "f" 'outline-forward-same-level)
  6331. (org-defkey map "b" 'outline-backward-same-level)
  6332. (org-defkey map "u" 'outline-up-heading))
  6333. (org-defkey map "/" 'org-occur)
  6334. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6335. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6336. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6337. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6338. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6339. map)))
  6340. (defconst org-goto-help
  6341. "Browse buffer copy, to find location or copy text.%s
  6342. RET=jump to location C-g=quit and return to previous location
  6343. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6344. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6345. ;; FIXME: Docstring does not mention both interfaces
  6346. (defun org-goto (&optional alternative-interface)
  6347. "Look up a different location in the current file, keeping current visibility.
  6348. When you want look-up or go to a different location in a
  6349. document, the fastest way is often to fold the entire buffer and
  6350. then dive into the tree. This method has the disadvantage, that
  6351. the previous location will be folded, which may not be what you
  6352. want.
  6353. This command works around this by showing a copy of the current
  6354. buffer in an indirect buffer, in overview mode. You can dive
  6355. into the tree in that copy, use org-occur and incremental search
  6356. to find a location. When pressing RET or `Q', the command
  6357. returns to the original buffer in which the visibility is still
  6358. unchanged. After RET it will also jump to the location selected
  6359. in the indirect buffer and expose the headline hierarchy above.
  6360. With a prefix argument, use the alternative interface: e.g. if
  6361. `org-goto-interface' is 'outline use 'outline-path-completion."
  6362. (interactive "P")
  6363. (org-goto-map)
  6364. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6365. (org-refile-use-outline-path t)
  6366. (org-refile-target-verify-function nil)
  6367. (interface
  6368. (if (not alternative-interface)
  6369. org-goto-interface
  6370. (if (eq org-goto-interface 'outline)
  6371. 'outline-path-completion
  6372. 'outline)))
  6373. (org-goto-start-pos (point))
  6374. (selected-point
  6375. (if (eq interface 'outline)
  6376. (car (org-get-location (current-buffer) org-goto-help))
  6377. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6378. (org-refile-check-position pa)
  6379. (nth 3 pa)))))
  6380. (if selected-point
  6381. (progn
  6382. (org-mark-ring-push org-goto-start-pos)
  6383. (goto-char selected-point)
  6384. (if (or (outline-invisible-p) (org-invisible-p2))
  6385. (org-show-context 'org-goto)))
  6386. (message "Quit"))))
  6387. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6388. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6389. (defvar org-goto-local-auto-isearch-map) ; defined below
  6390. (defun org-get-location (buf help)
  6391. "Let the user select a location in the Org-mode buffer BUF.
  6392. This function uses a recursive edit. It returns the selected position
  6393. or nil."
  6394. (org-no-popups
  6395. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6396. (isearch-hide-immediately nil)
  6397. (isearch-search-fun-function
  6398. (lambda () 'org-goto-local-search-headings))
  6399. (org-goto-selected-point org-goto-exit-command))
  6400. (save-excursion
  6401. (save-window-excursion
  6402. (delete-other-windows)
  6403. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6404. (org-pop-to-buffer-same-window
  6405. (condition-case nil
  6406. (make-indirect-buffer (current-buffer) "*org-goto*")
  6407. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6408. (with-output-to-temp-buffer "*Org Help*"
  6409. (princ (format help (if org-goto-auto-isearch
  6410. " Just type for auto-isearch."
  6411. " n/p/f/b/u to navigate, q to quit."))))
  6412. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6413. (setq buffer-read-only nil)
  6414. (let ((org-startup-truncated t)
  6415. (org-startup-folded nil)
  6416. (org-startup-align-all-tables nil))
  6417. (org-mode)
  6418. (org-overview))
  6419. (setq buffer-read-only t)
  6420. (if (and (boundp 'org-goto-start-pos)
  6421. (integer-or-marker-p org-goto-start-pos))
  6422. (let ((org-show-hierarchy-above t)
  6423. (org-show-siblings t)
  6424. (org-show-following-heading t))
  6425. (goto-char org-goto-start-pos)
  6426. (and (outline-invisible-p) (org-show-context)))
  6427. (goto-char (point-min)))
  6428. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6429. (message "Select location and press RET")
  6430. (use-local-map org-goto-map)
  6431. (recursive-edit)))
  6432. (kill-buffer "*org-goto*")
  6433. (cons org-goto-selected-point org-goto-exit-command))))
  6434. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6435. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6436. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6437. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6438. (defun org-goto-local-search-headings (string bound noerror)
  6439. "Search and make sure that any matches are in headlines."
  6440. (catch 'return
  6441. (while (if isearch-forward
  6442. (search-forward string bound noerror)
  6443. (search-backward string bound noerror))
  6444. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6445. (and (member :headline context)
  6446. (not (member :tags context))))
  6447. (throw 'return (point))))))
  6448. (defun org-goto-local-auto-isearch ()
  6449. "Start isearch."
  6450. (interactive)
  6451. (goto-char (point-min))
  6452. (let ((keys (this-command-keys)))
  6453. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6454. (isearch-mode t)
  6455. (isearch-process-search-char (string-to-char keys)))))
  6456. (defun org-goto-ret (&optional arg)
  6457. "Finish `org-goto' by going to the new location."
  6458. (interactive "P")
  6459. (setq org-goto-selected-point (point)
  6460. org-goto-exit-command 'return)
  6461. (throw 'exit nil))
  6462. (defun org-goto-left ()
  6463. "Finish `org-goto' by going to the new location."
  6464. (interactive)
  6465. (if (org-at-heading-p)
  6466. (progn
  6467. (beginning-of-line 1)
  6468. (setq org-goto-selected-point (point)
  6469. org-goto-exit-command 'left)
  6470. (throw 'exit nil))
  6471. (error "Not on a heading")))
  6472. (defun org-goto-right ()
  6473. "Finish `org-goto' by going to the new location."
  6474. (interactive)
  6475. (if (org-at-heading-p)
  6476. (progn
  6477. (setq org-goto-selected-point (point)
  6478. org-goto-exit-command 'right)
  6479. (throw 'exit nil))
  6480. (error "Not on a heading")))
  6481. (defun org-goto-quit ()
  6482. "Finish `org-goto' without cursor motion."
  6483. (interactive)
  6484. (setq org-goto-selected-point nil)
  6485. (setq org-goto-exit-command 'quit)
  6486. (throw 'exit nil))
  6487. ;;; Indirect buffer display of subtrees
  6488. (defvar org-indirect-dedicated-frame nil
  6489. "This is the frame being used for indirect tree display.")
  6490. (defvar org-last-indirect-buffer nil)
  6491. (defun org-tree-to-indirect-buffer (&optional arg)
  6492. "Create indirect buffer and narrow it to current subtree.
  6493. With a numerical prefix ARG, go up to this level and then take that tree.
  6494. If ARG is negative, go up that many levels.
  6495. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6496. indirect buffer previously made with this command, to avoid proliferation of
  6497. indirect buffers. However, when you call the command with a \
  6498. \\[universal-argument] prefix, or
  6499. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6500. is kept so that you can work with several indirect buffers at the same time.
  6501. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6502. \\[universal-argument] prefix also
  6503. requests that a new frame be made for the new buffer, so that the dedicated
  6504. frame is not changed."
  6505. (interactive "P")
  6506. (let ((cbuf (current-buffer))
  6507. (cwin (selected-window))
  6508. (pos (point))
  6509. beg end level heading ibuf)
  6510. (save-excursion
  6511. (org-back-to-heading t)
  6512. (when (numberp arg)
  6513. (setq level (org-outline-level))
  6514. (if (< arg 0) (setq arg (+ level arg)))
  6515. (while (> (setq level (org-outline-level)) arg)
  6516. (org-up-heading-safe)))
  6517. (setq beg (point)
  6518. heading (org-get-heading))
  6519. (org-end-of-subtree t t)
  6520. (if (org-at-heading-p) (backward-char 1))
  6521. (setq end (point)))
  6522. (if (and (buffer-live-p org-last-indirect-buffer)
  6523. (not (eq org-indirect-buffer-display 'new-frame))
  6524. (not arg))
  6525. (kill-buffer org-last-indirect-buffer))
  6526. (setq ibuf (org-get-indirect-buffer cbuf)
  6527. org-last-indirect-buffer ibuf)
  6528. (cond
  6529. ((or (eq org-indirect-buffer-display 'new-frame)
  6530. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6531. (select-frame (make-frame))
  6532. (delete-other-windows)
  6533. (org-pop-to-buffer-same-window ibuf)
  6534. (org-set-frame-title heading))
  6535. ((eq org-indirect-buffer-display 'dedicated-frame)
  6536. (raise-frame
  6537. (select-frame (or (and org-indirect-dedicated-frame
  6538. (frame-live-p org-indirect-dedicated-frame)
  6539. org-indirect-dedicated-frame)
  6540. (setq org-indirect-dedicated-frame (make-frame)))))
  6541. (delete-other-windows)
  6542. (org-pop-to-buffer-same-window ibuf)
  6543. (org-set-frame-title (concat "Indirect: " heading)))
  6544. ((eq org-indirect-buffer-display 'current-window)
  6545. (org-pop-to-buffer-same-window ibuf))
  6546. ((eq org-indirect-buffer-display 'other-window)
  6547. (pop-to-buffer ibuf))
  6548. (t (error "Invalid value")))
  6549. (if (featurep 'xemacs)
  6550. (save-excursion (org-mode) (turn-on-font-lock)))
  6551. (narrow-to-region beg end)
  6552. (show-all)
  6553. (goto-char pos)
  6554. (run-hook-with-args 'org-cycle-hook 'all)
  6555. (and (window-live-p cwin) (select-window cwin))))
  6556. (defun org-get-indirect-buffer (&optional buffer)
  6557. (setq buffer (or buffer (current-buffer)))
  6558. (let ((n 1) (base (buffer-name buffer)) bname)
  6559. (while (buffer-live-p
  6560. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6561. (setq n (1+ n)))
  6562. (condition-case nil
  6563. (make-indirect-buffer buffer bname 'clone)
  6564. (error (make-indirect-buffer buffer bname)))))
  6565. (defun org-set-frame-title (title)
  6566. "Set the title of the current frame to the string TITLE."
  6567. ;; FIXME: how to name a single frame in XEmacs???
  6568. (unless (featurep 'xemacs)
  6569. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6570. ;;;; Structure editing
  6571. ;;; Inserting headlines
  6572. (defun org-previous-line-empty-p ()
  6573. (save-excursion
  6574. (and (not (bobp))
  6575. (or (beginning-of-line 0) t)
  6576. (save-match-data
  6577. (looking-at "[ \t]*$")))))
  6578. (defun org-insert-heading (&optional force-heading invisible-ok)
  6579. "Insert a new heading or item with same depth at point.
  6580. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6581. If point is at the beginning of a headline, insert a sibling before the
  6582. current headline. If point is not at the beginning, split the line,
  6583. create the new headline with the text in the current line after point
  6584. \(but see also the variable `org-M-RET-may-split-line').
  6585. With a double prefix arg, force the heading to be inserted at the
  6586. end of the parent subtree.
  6587. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6588. This is important for non-interactive uses of the command."
  6589. (interactive "P")
  6590. (if (or (= (buffer-size) 0)
  6591. (and (not (save-excursion
  6592. (and (ignore-errors (org-back-to-heading invisible-ok))
  6593. (org-at-heading-p))))
  6594. (or force-heading (not (org-in-item-p)))))
  6595. (progn
  6596. (insert "\n* ")
  6597. (run-hooks 'org-insert-heading-hook))
  6598. (when (or force-heading (not (org-insert-item)))
  6599. (let* ((empty-line-p nil)
  6600. (level nil)
  6601. (on-heading (org-at-heading-p))
  6602. (head (save-excursion
  6603. (condition-case nil
  6604. (progn
  6605. (org-back-to-heading invisible-ok)
  6606. (when (and (not on-heading)
  6607. (featurep 'org-inlinetask)
  6608. (integerp org-inlinetask-min-level)
  6609. (>= (length (match-string 0))
  6610. org-inlinetask-min-level))
  6611. ;; Find a heading level before the inline task
  6612. (while (and (setq level (org-up-heading-safe))
  6613. (>= level org-inlinetask-min-level)))
  6614. (if (org-at-heading-p)
  6615. (org-back-to-heading invisible-ok)
  6616. (error "This should not happen")))
  6617. (setq empty-line-p (org-previous-line-empty-p))
  6618. (match-string 0))
  6619. (error "*"))))
  6620. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6621. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6622. pos hide-previous previous-pos)
  6623. (cond
  6624. ((and (org-at-heading-p) (bolp)
  6625. (or (bobp)
  6626. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6627. ;; insert before the current line
  6628. (open-line (if blank 2 1)))
  6629. ((and (bolp)
  6630. (not org-insert-heading-respect-content)
  6631. (or (bobp)
  6632. (save-excursion
  6633. (backward-char 1) (not (outline-invisible-p)))))
  6634. ;; insert right here
  6635. nil)
  6636. (t
  6637. ;; somewhere in the line
  6638. (save-excursion
  6639. (setq previous-pos (point-at-bol))
  6640. (end-of-line)
  6641. (setq hide-previous (outline-invisible-p)))
  6642. (and org-insert-heading-respect-content (org-show-subtree))
  6643. (let ((split
  6644. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6645. (save-excursion
  6646. (let ((p (point)))
  6647. (goto-char (point-at-bol))
  6648. (and (looking-at org-complex-heading-regexp)
  6649. (match-beginning 4)
  6650. (> p (match-beginning 4)))))))
  6651. tags pos)
  6652. (cond
  6653. (org-insert-heading-respect-content
  6654. (if (not (equal force-heading '(16)))
  6655. (progn
  6656. (org-end-of-subtree nil t)
  6657. (and (looking-at "^\\*") (backward-char 1))
  6658. (while (and (not (bobp))
  6659. (member (char-before) '(?\ ?\t ?\n)))
  6660. (backward-delete-char 1)))
  6661. (org-up-heading-safe)
  6662. (org-end-of-subtree nil t))
  6663. (when (featurep 'org-inlinetask)
  6664. (while (and (not (eobp))
  6665. (looking-at "\\(\\*+\\)[ \t]+")
  6666. (>= (length (match-string 1))
  6667. org-inlinetask-min-level))
  6668. (org-end-of-subtree nil t)))
  6669. (or (bolp) (newline))
  6670. (or (org-previous-line-empty-p)
  6671. (and blank (newline)))
  6672. (open-line 1))
  6673. ((org-at-heading-p)
  6674. (when hide-previous
  6675. (show-children)
  6676. (org-show-entry))
  6677. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6678. (setq tags (and (match-end 2) (match-string 2)))
  6679. (and (match-end 1)
  6680. (delete-region (match-beginning 1) (match-end 1)))
  6681. (setq pos (point-at-bol))
  6682. (or split (end-of-line 1))
  6683. (delete-horizontal-space)
  6684. (if (string-match "\\`\\*+\\'"
  6685. (buffer-substring (point-at-bol) (point)))
  6686. (insert " "))
  6687. (newline (if blank 2 1))
  6688. (when tags
  6689. (save-excursion
  6690. (goto-char pos)
  6691. (end-of-line 1)
  6692. (insert " " tags)
  6693. (org-set-tags nil 'align))))
  6694. (t
  6695. (or split (end-of-line 1))
  6696. (newline (if blank 2 1)))))))
  6697. (insert head) (just-one-space)
  6698. (setq pos (point))
  6699. (end-of-line 1)
  6700. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6701. (when (and org-insert-heading-respect-content hide-previous)
  6702. (save-excursion
  6703. (goto-char previous-pos)
  6704. (hide-subtree)))
  6705. (run-hooks 'org-insert-heading-hook)))))
  6706. (defun org-get-heading (&optional no-tags no-todo)
  6707. "Return the heading of the current entry, without the stars.
  6708. When NO-TAGS is non-nil, don't include tags.
  6709. When NO-TODO is non-nil, don't include TODO keywords."
  6710. (save-excursion
  6711. (org-back-to-heading t)
  6712. (cond
  6713. ((and no-tags no-todo)
  6714. (looking-at org-complex-heading-regexp)
  6715. (match-string 4))
  6716. (no-tags
  6717. (looking-at (concat org-outline-regexp
  6718. "\\(.*?\\)"
  6719. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6720. (match-string 1))
  6721. (no-todo
  6722. (looking-at org-todo-line-regexp)
  6723. (match-string 3))
  6724. (t (looking-at org-heading-regexp)
  6725. (match-string 2)))))
  6726. (defvar orgstruct-mode) ; defined below
  6727. (defun org-heading-components ()
  6728. "Return the components of the current heading.
  6729. This is a list with the following elements:
  6730. - the level as an integer
  6731. - the reduced level, different if `org-odd-levels-only' is set.
  6732. - the TODO keyword, or nil
  6733. - the priority character, like ?A, or nil if no priority is given
  6734. - the headline text itself, or the tags string if no headline text
  6735. - the tags string, or nil."
  6736. (save-excursion
  6737. (org-back-to-heading t)
  6738. (if (let (case-fold-search)
  6739. (looking-at
  6740. (if orgstruct-mode
  6741. org-heading-regexp
  6742. org-complex-heading-regexp)))
  6743. (if orgstruct-mode
  6744. (list (length (match-string 1))
  6745. (org-reduced-level (length (match-string 1)))
  6746. nil
  6747. nil
  6748. (match-string 2)
  6749. nil)
  6750. (list (length (match-string 1))
  6751. (org-reduced-level (length (match-string 1)))
  6752. (org-match-string-no-properties 2)
  6753. (and (match-end 3) (aref (match-string 3) 2))
  6754. (org-match-string-no-properties 4)
  6755. (org-match-string-no-properties 5))))))
  6756. (defun org-get-entry ()
  6757. "Get the entry text, after heading, entire subtree."
  6758. (save-excursion
  6759. (org-back-to-heading t)
  6760. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6761. (defun org-insert-heading-after-current ()
  6762. "Insert a new heading with same level as current, after current subtree."
  6763. (interactive)
  6764. (org-back-to-heading)
  6765. (org-insert-heading)
  6766. (org-move-subtree-down)
  6767. (end-of-line 1))
  6768. (defun org-insert-heading-respect-content (invisible-ok)
  6769. "Insert heading with `org-insert-heading-respect-content' set to t."
  6770. (interactive "P")
  6771. (let ((org-insert-heading-respect-content t))
  6772. (org-insert-heading t invisible-ok)))
  6773. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6774. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6775. (interactive "P")
  6776. (let ((org-insert-heading-respect-content t))
  6777. (org-insert-todo-heading force-state t)))
  6778. (defun org-insert-todo-heading (arg &optional force-heading)
  6779. "Insert a new heading with the same level and TODO state as current heading.
  6780. If the heading has no TODO state, or if the state is DONE, use the first
  6781. state (TODO by default). Also one prefix arg, force first state. With two
  6782. prefix args, force inserting at the end of the parent subtree."
  6783. (interactive "P")
  6784. (when (or force-heading (not (org-insert-item 'checkbox)))
  6785. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6786. force-heading))
  6787. (save-excursion
  6788. (org-back-to-heading)
  6789. (outline-previous-heading)
  6790. (looking-at org-todo-line-regexp))
  6791. (let*
  6792. ((new-mark-x
  6793. (if (or arg
  6794. (not (match-beginning 2))
  6795. (member (match-string 2) org-done-keywords))
  6796. (car org-todo-keywords-1)
  6797. (match-string 2)))
  6798. (new-mark
  6799. (or
  6800. (run-hook-with-args-until-success
  6801. 'org-todo-get-default-hook new-mark-x nil)
  6802. new-mark-x)))
  6803. (beginning-of-line 1)
  6804. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6805. (if org-treat-insert-todo-heading-as-state-change
  6806. (org-todo new-mark)
  6807. (insert new-mark " "))))
  6808. (when org-provide-todo-statistics
  6809. (org-update-parent-todo-statistics))))
  6810. (defun org-insert-subheading (arg)
  6811. "Insert a new subheading and demote it.
  6812. Works for outline headings and for plain lists alike."
  6813. (interactive "P")
  6814. (org-insert-heading arg)
  6815. (cond
  6816. ((org-at-heading-p) (org-do-demote))
  6817. ((org-at-item-p) (org-indent-item))))
  6818. (defun org-insert-todo-subheading (arg)
  6819. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6820. Works for outline headings and for plain lists alike."
  6821. (interactive "P")
  6822. (org-insert-todo-heading arg)
  6823. (cond
  6824. ((org-at-heading-p) (org-do-demote))
  6825. ((org-at-item-p) (org-indent-item))))
  6826. ;;; Promotion and Demotion
  6827. (defvar org-after-demote-entry-hook nil
  6828. "Hook run after an entry has been demoted.
  6829. The cursor will be at the beginning of the entry.
  6830. When a subtree is being demoted, the hook will be called for each node.")
  6831. (defvar org-after-promote-entry-hook nil
  6832. "Hook run after an entry has been promoted.
  6833. The cursor will be at the beginning of the entry.
  6834. When a subtree is being promoted, the hook will be called for each node.")
  6835. (defun org-promote-subtree ()
  6836. "Promote the entire subtree.
  6837. See also `org-promote'."
  6838. (interactive)
  6839. (save-excursion
  6840. (org-with-limited-levels (org-map-tree 'org-promote)))
  6841. (org-fix-position-after-promote))
  6842. (defun org-demote-subtree ()
  6843. "Demote the entire subtree. See `org-demote'.
  6844. See also `org-promote'."
  6845. (interactive)
  6846. (save-excursion
  6847. (org-with-limited-levels (org-map-tree 'org-demote)))
  6848. (org-fix-position-after-promote))
  6849. (defun org-do-promote ()
  6850. "Promote the current heading higher up the tree.
  6851. If the region is active in `transient-mark-mode', promote all headings
  6852. in the region."
  6853. (interactive)
  6854. (save-excursion
  6855. (if (org-region-active-p)
  6856. (org-map-region 'org-promote (region-beginning) (region-end))
  6857. (org-promote)))
  6858. (org-fix-position-after-promote))
  6859. (defun org-do-demote ()
  6860. "Demote the current heading lower down the tree.
  6861. If the region is active in `transient-mark-mode', demote all headings
  6862. in the region."
  6863. (interactive)
  6864. (save-excursion
  6865. (if (org-region-active-p)
  6866. (org-map-region 'org-demote (region-beginning) (region-end))
  6867. (org-demote)))
  6868. (org-fix-position-after-promote))
  6869. (defun org-fix-position-after-promote ()
  6870. "Make sure that after pro/demotion cursor position is right."
  6871. (let ((pos (point)))
  6872. (when (save-excursion
  6873. (beginning-of-line 1)
  6874. (looking-at org-todo-line-regexp)
  6875. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6876. (cond ((eobp) (insert " "))
  6877. ((eolp) (insert " "))
  6878. ((equal (char-after) ?\ ) (forward-char 1))))))
  6879. (defun org-current-level ()
  6880. "Return the level of the current entry, or nil if before the first headline.
  6881. The level is the number of stars at the beginning of the headline."
  6882. (save-excursion
  6883. (org-with-limited-levels
  6884. (if (ignore-errors (org-back-to-heading t))
  6885. (funcall outline-level)))))
  6886. (defun org-get-previous-line-level ()
  6887. "Return the outline depth of the last headline before the current line.
  6888. Returns 0 for the first headline in the buffer, and nil if before the
  6889. first headline."
  6890. (let ((current-level (org-current-level))
  6891. (prev-level (when (> (line-number-at-pos) 1)
  6892. (save-excursion
  6893. (beginning-of-line 0)
  6894. (org-current-level)))))
  6895. (cond ((null current-level) nil) ; Before first headline
  6896. ((null prev-level) 0) ; At first headline
  6897. (prev-level))))
  6898. (defun org-reduced-level (l)
  6899. "Compute the effective level of a heading.
  6900. This takes into account the setting of `org-odd-levels-only'."
  6901. (cond
  6902. ((zerop l) 0)
  6903. (org-odd-levels-only (1+ (floor (/ l 2))))
  6904. (t l)))
  6905. (defun org-level-increment ()
  6906. "Return the number of stars that will be added or removed at a
  6907. time to headlines when structure editing, based on the value of
  6908. `org-odd-levels-only'."
  6909. (if org-odd-levels-only 2 1))
  6910. (defun org-get-valid-level (level &optional change)
  6911. "Rectify a level change under the influence of `org-odd-levels-only'
  6912. LEVEL is a current level, CHANGE is by how much the level should be
  6913. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6914. even level numbers will become the next higher odd number."
  6915. (if org-odd-levels-only
  6916. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6917. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6918. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6919. (max 1 (+ level (or change 0)))))
  6920. (if (boundp 'define-obsolete-function-alias)
  6921. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6922. (define-obsolete-function-alias 'org-get-legal-level
  6923. 'org-get-valid-level)
  6924. (define-obsolete-function-alias 'org-get-legal-level
  6925. 'org-get-valid-level "23.1")))
  6926. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6927. ;; ̀org-with-limited-levels'
  6928. (defun org-promote ()
  6929. "Promote the current heading higher up the tree.
  6930. If the region is active in `transient-mark-mode', promote all headings
  6931. in the region."
  6932. (org-back-to-heading t)
  6933. (let* ((level (save-match-data (funcall outline-level)))
  6934. (after-change-functions (remove 'flyspell-after-change-function
  6935. after-change-functions))
  6936. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6937. (diff (abs (- level (length up-head) -1))))
  6938. (cond ((and (= level 1) org-called-with-limited-levels
  6939. org-allow-promoting-top-level-subtree)
  6940. (replace-match "# " nil t))
  6941. ((= level 1)
  6942. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6943. (t (replace-match up-head nil t)))
  6944. ;; Fixup tag positioning
  6945. (unless (= level 1)
  6946. (and org-auto-align-tags (org-set-tags nil t))
  6947. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6948. (run-hooks 'org-after-promote-entry-hook)))
  6949. (defun org-demote ()
  6950. "Demote the current heading lower down the tree.
  6951. If the region is active in `transient-mark-mode', demote all headings
  6952. in the region."
  6953. (org-back-to-heading t)
  6954. (let* ((level (save-match-data (funcall outline-level)))
  6955. (after-change-functions (remove 'flyspell-after-change-function
  6956. after-change-functions))
  6957. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6958. (diff (abs (- level (length down-head) -1))))
  6959. (replace-match down-head nil t)
  6960. ;; Fixup tag positioning
  6961. (and org-auto-align-tags (org-set-tags nil t))
  6962. (if org-adapt-indentation (org-fixup-indentation diff))
  6963. (run-hooks 'org-after-demote-entry-hook)))
  6964. (defun org-cycle-level ()
  6965. "Cycle the level of an empty headline through possible states.
  6966. This goes first to child, then to parent, level, then up the hierarchy.
  6967. After top level, it switches back to sibling level."
  6968. (interactive)
  6969. (let ((org-adapt-indentation nil))
  6970. (when (org-point-at-end-of-empty-headline)
  6971. (setq this-command 'org-cycle-level) ; Only needed for caching
  6972. (let ((cur-level (org-current-level))
  6973. (prev-level (org-get-previous-line-level)))
  6974. (cond
  6975. ;; If first headline in file, promote to top-level.
  6976. ((= prev-level 0)
  6977. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6978. do (org-do-promote)))
  6979. ;; If same level as prev, demote one.
  6980. ((= prev-level cur-level)
  6981. (org-do-demote))
  6982. ;; If parent is top-level, promote to top level if not already.
  6983. ((= prev-level 1)
  6984. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6985. do (org-do-promote)))
  6986. ;; If top-level, return to prev-level.
  6987. ((= cur-level 1)
  6988. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6989. do (org-do-demote)))
  6990. ;; If less than prev-level, promote one.
  6991. ((< cur-level prev-level)
  6992. (org-do-promote))
  6993. ;; If deeper than prev-level, promote until higher than
  6994. ;; prev-level.
  6995. ((> cur-level prev-level)
  6996. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6997. do (org-do-promote))))
  6998. t))))
  6999. (defun org-map-tree (fun)
  7000. "Call FUN for every heading underneath the current one."
  7001. (org-back-to-heading)
  7002. (let ((level (funcall outline-level)))
  7003. (save-excursion
  7004. (funcall fun)
  7005. (while (and (progn
  7006. (outline-next-heading)
  7007. (> (funcall outline-level) level))
  7008. (not (eobp)))
  7009. (funcall fun)))))
  7010. (defun org-map-region (fun beg end)
  7011. "Call FUN for every heading between BEG and END."
  7012. (let ((org-ignore-region t))
  7013. (save-excursion
  7014. (setq end (copy-marker end))
  7015. (goto-char beg)
  7016. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7017. (< (point) end))
  7018. (funcall fun))
  7019. (while (and (progn
  7020. (outline-next-heading)
  7021. (< (point) end))
  7022. (not (eobp)))
  7023. (funcall fun)))))
  7024. (defvar org-property-end-re) ; silence byte-compiler
  7025. (defun org-fixup-indentation (diff)
  7026. "Change the indentation in the current entry by DIFF.
  7027. However, if any line in the current entry has no indentation, or if it
  7028. would end up with no indentation after the change, nothing at all is done."
  7029. (save-excursion
  7030. (let ((end (save-excursion (outline-next-heading)
  7031. (point-marker)))
  7032. (prohibit (if (> diff 0)
  7033. "^\\S-"
  7034. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7035. col)
  7036. (unless (save-excursion (end-of-line 1)
  7037. (re-search-forward prohibit end t))
  7038. (while (and (< (point) end)
  7039. (re-search-forward "^[ \t]+" end t))
  7040. (goto-char (match-end 0))
  7041. (setq col (current-column))
  7042. (if (< diff 0) (replace-match ""))
  7043. (org-indent-to-column (+ diff col))))
  7044. (move-marker end nil))))
  7045. (defun org-convert-to-odd-levels ()
  7046. "Convert an org-mode file with all levels allowed to one with odd levels.
  7047. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7048. level 5 etc."
  7049. (interactive)
  7050. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7051. (let ((outline-level 'org-outline-level)
  7052. (org-odd-levels-only nil) n)
  7053. (save-excursion
  7054. (goto-char (point-min))
  7055. (while (re-search-forward "^\\*\\*+ " nil t)
  7056. (setq n (- (length (match-string 0)) 2))
  7057. (while (>= (setq n (1- n)) 0)
  7058. (org-demote))
  7059. (end-of-line 1))))))
  7060. (defun org-convert-to-oddeven-levels ()
  7061. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7062. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7063. file contains a section with an even level, conversion would
  7064. destroy the structure of the file. An error is signaled in this
  7065. case."
  7066. (interactive)
  7067. (goto-char (point-min))
  7068. ;; First check if there are no even levels
  7069. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7070. (org-show-context t)
  7071. (error "Not all levels are odd in this file. Conversion not possible"))
  7072. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7073. (let ((outline-regexp org-outline-regexp)
  7074. (outline-level 'org-outline-level)
  7075. (org-odd-levels-only nil) n)
  7076. (save-excursion
  7077. (goto-char (point-min))
  7078. (while (re-search-forward "^\\*\\*+ " nil t)
  7079. (setq n (/ (1- (length (match-string 0))) 2))
  7080. (while (>= (setq n (1- n)) 0)
  7081. (org-promote))
  7082. (end-of-line 1))))))
  7083. (defun org-tr-level (n)
  7084. "Make N odd if required."
  7085. (if org-odd-levels-only (1+ (/ n 2)) n))
  7086. ;;; Vertical tree motion, cutting and pasting of subtrees
  7087. (defun org-move-subtree-up (&optional arg)
  7088. "Move the current subtree up past ARG headlines of the same level."
  7089. (interactive "p")
  7090. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7091. (defun org-move-subtree-down (&optional arg)
  7092. "Move the current subtree down past ARG headlines of the same level."
  7093. (interactive "p")
  7094. (setq arg (prefix-numeric-value arg))
  7095. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7096. 'org-get-last-sibling))
  7097. (ins-point (make-marker))
  7098. (cnt (abs arg))
  7099. (col (current-column))
  7100. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7101. ;; Select the tree
  7102. (org-back-to-heading)
  7103. (setq beg0 (point))
  7104. (save-excursion
  7105. (setq ne-beg (org-back-over-empty-lines))
  7106. (setq beg (point)))
  7107. (save-match-data
  7108. (save-excursion (outline-end-of-heading)
  7109. (setq folded (outline-invisible-p)))
  7110. (outline-end-of-subtree))
  7111. (outline-next-heading)
  7112. (setq ne-end (org-back-over-empty-lines))
  7113. (setq end (point))
  7114. (goto-char beg0)
  7115. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7116. ;; include less whitespace
  7117. (save-excursion
  7118. (goto-char beg)
  7119. (forward-line (- ne-beg ne-end))
  7120. (setq beg (point))))
  7121. ;; Find insertion point, with error handling
  7122. (while (> cnt 0)
  7123. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7124. (progn (goto-char beg0)
  7125. (user-error "Cannot move past superior level or buffer limit")))
  7126. (setq cnt (1- cnt)))
  7127. (if (> arg 0)
  7128. ;; Moving forward - still need to move over subtree
  7129. (progn (org-end-of-subtree t t)
  7130. (save-excursion
  7131. (org-back-over-empty-lines)
  7132. (or (bolp) (newline)))))
  7133. (setq ne-ins (org-back-over-empty-lines))
  7134. (move-marker ins-point (point))
  7135. (setq txt (buffer-substring beg end))
  7136. (org-save-markers-in-region beg end)
  7137. (delete-region beg end)
  7138. (org-remove-empty-overlays-at beg)
  7139. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7140. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7141. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7142. (let ((bbb (point)))
  7143. (insert-before-markers txt)
  7144. (org-reinstall-markers-in-region bbb)
  7145. (move-marker ins-point bbb))
  7146. (or (bolp) (insert "\n"))
  7147. (setq ins-end (point))
  7148. (goto-char ins-point)
  7149. (org-skip-whitespace)
  7150. (when (and (< arg 0)
  7151. (org-first-sibling-p)
  7152. (> ne-ins ne-beg))
  7153. ;; Move whitespace back to beginning
  7154. (save-excursion
  7155. (goto-char ins-end)
  7156. (let ((kill-whole-line t))
  7157. (kill-line (- ne-ins ne-beg)) (point)))
  7158. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7159. (move-marker ins-point nil)
  7160. (if folded
  7161. (hide-subtree)
  7162. (org-show-entry)
  7163. (show-children)
  7164. (org-cycle-hide-drawers 'children))
  7165. (org-clean-visibility-after-subtree-move)
  7166. ;; move back to the initial column we were at
  7167. (move-to-column col)))
  7168. (defvar org-subtree-clip ""
  7169. "Clipboard for cut and paste of subtrees.
  7170. This is actually only a copy of the kill, because we use the normal kill
  7171. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7172. (defvar org-subtree-clip-folded nil
  7173. "Was the last copied subtree folded?
  7174. This is used to fold the tree back after pasting.")
  7175. (defun org-cut-subtree (&optional n)
  7176. "Cut the current subtree into the clipboard.
  7177. With prefix arg N, cut this many sequential subtrees.
  7178. This is a short-hand for marking the subtree and then cutting it."
  7179. (interactive "p")
  7180. (org-copy-subtree n 'cut))
  7181. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7182. "Cut the current subtree into the clipboard.
  7183. With prefix arg N, cut this many sequential subtrees.
  7184. This is a short-hand for marking the subtree and then copying it.
  7185. If CUT is non-nil, actually cut the subtree.
  7186. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7187. of some markers in the region, even if CUT is non-nil. This is
  7188. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7189. (interactive "p")
  7190. (let (beg end folded (beg0 (point)))
  7191. (if (org-called-interactively-p 'any)
  7192. (org-back-to-heading nil) ; take what looks like a subtree
  7193. (org-back-to-heading t)) ; take what is really there
  7194. (setq beg (point))
  7195. (skip-chars-forward " \t\r\n")
  7196. (save-match-data
  7197. (if nosubtrees
  7198. (outline-next-heading)
  7199. (save-excursion (outline-end-of-heading)
  7200. (setq folded (outline-invisible-p)))
  7201. (condition-case nil
  7202. (org-forward-heading-same-level (1- n) t)
  7203. (error nil))
  7204. (org-end-of-subtree t t)))
  7205. (setq end (point))
  7206. (goto-char beg0)
  7207. (when (> end beg)
  7208. (setq org-subtree-clip-folded folded)
  7209. (when (or cut force-store-markers)
  7210. (org-save-markers-in-region beg end))
  7211. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7212. (setq org-subtree-clip (current-kill 0))
  7213. (message "%s: Subtree(s) with %d characters"
  7214. (if cut "Cut" "Copied")
  7215. (length org-subtree-clip)))))
  7216. (defun org-paste-subtree (&optional level tree for-yank)
  7217. "Paste the clipboard as a subtree, with modification of headline level.
  7218. The entire subtree is promoted or demoted in order to match a new headline
  7219. level.
  7220. If the cursor is at the beginning of a headline, the same level as
  7221. that headline is used to paste the tree.
  7222. If not, the new level is derived from the *visible* headings
  7223. before and after the insertion point, and taken to be the inferior headline
  7224. level of the two. So if the previous visible heading is level 3 and the
  7225. next is level 4 (or vice versa), level 4 will be used for insertion.
  7226. This makes sure that the subtree remains an independent subtree and does
  7227. not swallow low level entries.
  7228. You can also force a different level, either by using a numeric prefix
  7229. argument, or by inserting the heading marker by hand. For example, if the
  7230. cursor is after \"*****\", then the tree will be shifted to level 5.
  7231. If optional TREE is given, use this text instead of the kill ring.
  7232. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7233. move back over whitespace before inserting, and move point to the end of
  7234. the inserted text when done."
  7235. (interactive "P")
  7236. (setq tree (or tree (and kill-ring (current-kill 0))))
  7237. (unless (org-kill-is-subtree-p tree)
  7238. (error "%s"
  7239. (substitute-command-keys
  7240. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7241. (org-with-limited-levels
  7242. (let* ((visp (not (outline-invisible-p)))
  7243. (txt tree)
  7244. (^re_ "\\(\\*+\\)[ \t]*")
  7245. (old-level (if (string-match org-outline-regexp-bol txt)
  7246. (- (match-end 0) (match-beginning 0) 1)
  7247. -1))
  7248. (force-level (cond (level (prefix-numeric-value level))
  7249. ((and (looking-at "[ \t]*$")
  7250. (string-match
  7251. "^\\*+$" (buffer-substring
  7252. (point-at-bol) (point))))
  7253. (- (match-end 1) (match-beginning 1)))
  7254. ((and (bolp)
  7255. (looking-at org-outline-regexp))
  7256. (- (match-end 0) (point) 1))))
  7257. (previous-level (save-excursion
  7258. (condition-case nil
  7259. (progn
  7260. (outline-previous-visible-heading 1)
  7261. (if (looking-at ^re_)
  7262. (- (match-end 0) (match-beginning 0) 1)
  7263. 1))
  7264. (error 1))))
  7265. (next-level (save-excursion
  7266. (condition-case nil
  7267. (progn
  7268. (or (looking-at org-outline-regexp)
  7269. (outline-next-visible-heading 1))
  7270. (if (looking-at ^re_)
  7271. (- (match-end 0) (match-beginning 0) 1)
  7272. 1))
  7273. (error 1))))
  7274. (new-level (or force-level (max previous-level next-level)))
  7275. (shift (if (or (= old-level -1)
  7276. (= new-level -1)
  7277. (= old-level new-level))
  7278. 0
  7279. (- new-level old-level)))
  7280. (delta (if (> shift 0) -1 1))
  7281. (func (if (> shift 0) 'org-demote 'org-promote))
  7282. (org-odd-levels-only nil)
  7283. beg end newend)
  7284. ;; Remove the forced level indicator
  7285. (if force-level
  7286. (delete-region (point-at-bol) (point)))
  7287. ;; Paste
  7288. (beginning-of-line (if (bolp) 1 2))
  7289. (setq beg (point))
  7290. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7291. (insert-before-markers txt)
  7292. (unless (string-match "\n\\'" txt) (insert "\n"))
  7293. (setq newend (point))
  7294. (org-reinstall-markers-in-region beg)
  7295. (setq end (point))
  7296. (goto-char beg)
  7297. (skip-chars-forward " \t\n\r")
  7298. (setq beg (point))
  7299. (if (and (outline-invisible-p) visp)
  7300. (save-excursion (outline-show-heading)))
  7301. ;; Shift if necessary
  7302. (unless (= shift 0)
  7303. (save-restriction
  7304. (narrow-to-region beg end)
  7305. (while (not (= shift 0))
  7306. (org-map-region func (point-min) (point-max))
  7307. (setq shift (+ delta shift)))
  7308. (goto-char (point-min))
  7309. (setq newend (point-max))))
  7310. (when (or (org-called-interactively-p 'interactive) for-yank)
  7311. (message "Clipboard pasted as level %d subtree" new-level))
  7312. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7313. kill-ring
  7314. (eq org-subtree-clip (current-kill 0))
  7315. org-subtree-clip-folded)
  7316. ;; The tree was folded before it was killed/copied
  7317. (hide-subtree))
  7318. (and for-yank (goto-char newend)))))
  7319. (defun org-kill-is-subtree-p (&optional txt)
  7320. "Check if the current kill is an outline subtree, or a set of trees.
  7321. Returns nil if kill does not start with a headline, or if the first
  7322. headline level is not the largest headline level in the tree.
  7323. So this will actually accept several entries of equal levels as well,
  7324. which is OK for `org-paste-subtree'.
  7325. If optional TXT is given, check this string instead of the current kill."
  7326. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7327. (re (org-get-limited-outline-regexp))
  7328. (^re (concat "^" re))
  7329. (start-level (and kill
  7330. (string-match
  7331. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7332. kill)
  7333. (- (match-end 2) (match-beginning 2) 1)))
  7334. (start (1+ (or (match-beginning 2) -1))))
  7335. (if (not start-level)
  7336. (progn
  7337. nil) ;; does not even start with a heading
  7338. (catch 'exit
  7339. (while (setq start (string-match ^re kill (1+ start)))
  7340. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7341. (throw 'exit nil)))
  7342. t))))
  7343. (defvar org-markers-to-move nil
  7344. "Markers that should be moved with a cut-and-paste operation.
  7345. Those markers are stored together with their positions relative to
  7346. the start of the region.")
  7347. (defun org-save-markers-in-region (beg end)
  7348. "Check markers in region.
  7349. If these markers are between BEG and END, record their position relative
  7350. to BEG, so that after moving the block of text, we can put the markers back
  7351. into place.
  7352. This function gets called just before an entry or tree gets cut from the
  7353. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7354. called immediately, to move the markers with the entries."
  7355. (setq org-markers-to-move nil)
  7356. (when (featurep 'org-clock)
  7357. (org-clock-save-markers-for-cut-and-paste beg end))
  7358. (when (featurep 'org-agenda)
  7359. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7360. (defun org-check-and-save-marker (marker beg end)
  7361. "Check if MARKER is between BEG and END.
  7362. If yes, remember the marker and the distance to BEG."
  7363. (when (and (marker-buffer marker)
  7364. (equal (marker-buffer marker) (current-buffer)))
  7365. (if (and (>= marker beg) (< marker end))
  7366. (push (cons marker (- marker beg)) org-markers-to-move))))
  7367. (defun org-reinstall-markers-in-region (beg)
  7368. "Move all remembered markers to their position relative to BEG."
  7369. (mapc (lambda (x)
  7370. (move-marker (car x) (+ beg (cdr x))))
  7371. org-markers-to-move)
  7372. (setq org-markers-to-move nil))
  7373. (defun org-narrow-to-subtree ()
  7374. "Narrow buffer to the current subtree."
  7375. (interactive)
  7376. (save-excursion
  7377. (save-match-data
  7378. (org-with-limited-levels
  7379. (narrow-to-region
  7380. (progn (org-back-to-heading t) (point))
  7381. (progn (org-end-of-subtree t t)
  7382. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7383. (point)))))))
  7384. (defun org-narrow-to-block ()
  7385. "Narrow buffer to the current block."
  7386. (interactive)
  7387. (let* ((case-fold-search t)
  7388. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7389. "^[ \t]*#\\+end_.*")))
  7390. (if blockp
  7391. (narrow-to-region (car blockp) (cdr blockp))
  7392. (error "Not in a block"))))
  7393. (eval-when-compile
  7394. (defvar org-property-drawer-re))
  7395. (defvar org-property-start-re) ;; defined below
  7396. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7397. "Clone the task (subtree) at point N times.
  7398. The clones will be inserted as siblings.
  7399. In interactive use, the user will be prompted for the number of
  7400. clones to be produced, and for a time SHIFT, which may be a
  7401. repeater as used in time stamps, for example `+3d'.
  7402. When a valid repeater is given and the entry contains any time
  7403. stamps, the clones will become a sequence in time, with time
  7404. stamps in the subtree shifted for each clone produced. If SHIFT
  7405. is nil or the empty string, time stamps will be left alone. The
  7406. ID property of the original subtree is removed.
  7407. If the original subtree did contain time stamps with a repeater,
  7408. the following will happen:
  7409. - the repeater will be removed in each clone
  7410. - an additional clone will be produced, with the current, unshifted
  7411. date(s) in the entry.
  7412. - the original entry will be placed *after* all the clones, with
  7413. repeater intact.
  7414. - the start days in the repeater in the original entry will be shifted
  7415. to past the last clone.
  7416. In this way you can spell out a number of instances of a repeating task,
  7417. and still retain the repeater to cover future instances of the task."
  7418. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7419. (let (beg end template task idprop
  7420. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7421. (drawer-re org-drawer-regexp))
  7422. (if (not (and (integerp n) (> n 0)))
  7423. (error "Invalid number of replications %s" n))
  7424. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7425. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7426. shift)))
  7427. (error "Invalid shift specification %s" shift))
  7428. (when doshift
  7429. (setq shift-n (string-to-number (match-string 1 shift))
  7430. shift-what (cdr (assoc (match-string 2 shift)
  7431. '(("d" . day) ("w" . week)
  7432. ("m" . month) ("y" . year))))))
  7433. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7434. (setq nmin 1 nmax n)
  7435. (org-back-to-heading t)
  7436. (setq beg (point))
  7437. (setq idprop (org-entry-get nil "ID"))
  7438. (org-end-of-subtree t t)
  7439. (or (bolp) (insert "\n"))
  7440. (setq end (point))
  7441. (setq template (buffer-substring beg end))
  7442. (when (and doshift
  7443. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7444. (delete-region beg end)
  7445. (setq end beg)
  7446. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7447. (goto-char end)
  7448. (loop for n from nmin to nmax do
  7449. ;; prepare clone
  7450. (with-temp-buffer
  7451. (insert template)
  7452. (org-mode)
  7453. (goto-char (point-min))
  7454. (org-show-subtree)
  7455. (and idprop (if org-clone-delete-id
  7456. (org-entry-delete nil "ID")
  7457. (org-id-get-create t)))
  7458. (unless (= n 0)
  7459. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7460. (kill-whole-line))
  7461. (goto-char (point-min))
  7462. (while (re-search-forward drawer-re nil t)
  7463. (mapc (lambda (d)
  7464. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7465. (goto-char (point-min))
  7466. (when doshift
  7467. (while (re-search-forward org-ts-regexp-both nil t)
  7468. (org-timestamp-change (* n shift-n) shift-what))
  7469. (unless (= n n-no-remove)
  7470. (goto-char (point-min))
  7471. (while (re-search-forward org-ts-regexp nil t)
  7472. (save-excursion
  7473. (goto-char (match-beginning 0))
  7474. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7475. (delete-region (match-beginning 1) (match-end 1)))))))
  7476. (setq task (buffer-string)))
  7477. (insert task))
  7478. (goto-char beg)))
  7479. ;;; Outline Sorting
  7480. (defun org-sort (with-case)
  7481. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7482. Optional argument WITH-CASE means sort case-sensitively."
  7483. (interactive "P")
  7484. (cond
  7485. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7486. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7487. (t
  7488. (org-call-with-arg 'org-sort-entries with-case))))
  7489. (defun org-sort-remove-invisible (s)
  7490. "Remove invisible links from string S."
  7491. (remove-text-properties 0 (length s) org-rm-props s)
  7492. (while (string-match org-bracket-link-regexp s)
  7493. (setq s (replace-match (if (match-end 2)
  7494. (match-string 3 s)
  7495. (match-string 1 s)) t t s)))
  7496. (let ((st (format " %s " s)))
  7497. (while (string-match org-emph-re st)
  7498. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7499. (setq s (substring st 1 -1)))
  7500. s)
  7501. (defvar org-priority-regexp) ; defined later in the file
  7502. (defvar org-after-sorting-entries-or-items-hook nil
  7503. "Hook that is run after a bunch of entries or items have been sorted.
  7504. When children are sorted, the cursor is in the parent line when this
  7505. hook gets called. When a region or a plain list is sorted, the cursor
  7506. will be in the first entry of the sorted region/list.")
  7507. (defun org-sort-entries
  7508. (&optional with-case sorting-type getkey-func compare-func property)
  7509. "Sort entries on a certain level of an outline tree.
  7510. If there is an active region, the entries in the region are sorted.
  7511. Else, if the cursor is before the first entry, sort the top-level items.
  7512. Else, the children of the entry at point are sorted.
  7513. Sorting can be alphabetically, numerically, by date/time as given by
  7514. a time stamp, by a property, by priority order, or by a custom function.
  7515. The command prompts for the sorting type unless it has been given to the
  7516. function through the SORTING-TYPE argument, which needs to be a character,
  7517. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7518. precise meaning of each character:
  7519. n Numerically, by converting the beginning of the entry/item to a number.
  7520. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7521. o By order of TODO keywords.
  7522. t By date/time, either the first active time stamp in the entry, or, if
  7523. none exist, by the first inactive one.
  7524. s By the scheduled date/time.
  7525. d By deadline date/time.
  7526. c By creation time, which is assumed to be the first inactive time stamp
  7527. at the beginning of a line.
  7528. p By priority according to the cookie.
  7529. r By the value of a property.
  7530. Capital letters will reverse the sort order.
  7531. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7532. called with point at the beginning of the record. It must return either
  7533. a string or a number that should serve as the sorting key for that record.
  7534. Comparing entries ignores case by default. However, with an optional argument
  7535. WITH-CASE, the sorting considers case as well.
  7536. Sorting is done against the visible part of the headlines, it ignores hidden
  7537. links."
  7538. (interactive "P")
  7539. (let ((case-func (if with-case 'identity 'downcase))
  7540. (cmstr
  7541. ;; The clock marker is lost when using `sort-subr', let's
  7542. ;; store the clocking string.
  7543. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7544. (save-excursion
  7545. (goto-char org-clock-marker)
  7546. (looking-back "^.*") (match-string-no-properties 0))))
  7547. start beg end stars re re2
  7548. txt what tmp)
  7549. ;; Find beginning and end of region to sort
  7550. (cond
  7551. ((org-region-active-p)
  7552. ;; we will sort the region
  7553. (setq end (region-end)
  7554. what "region")
  7555. (goto-char (region-beginning))
  7556. (if (not (org-at-heading-p)) (outline-next-heading))
  7557. (setq start (point)))
  7558. ((or (org-at-heading-p)
  7559. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7560. ;; we will sort the children of the current headline
  7561. (org-back-to-heading)
  7562. (setq start (point)
  7563. end (progn (org-end-of-subtree t t)
  7564. (or (bolp) (insert "\n"))
  7565. (org-back-over-empty-lines)
  7566. (point))
  7567. what "children")
  7568. (goto-char start)
  7569. (show-subtree)
  7570. (outline-next-heading))
  7571. (t
  7572. ;; we will sort the top-level entries in this file
  7573. (goto-char (point-min))
  7574. (or (org-at-heading-p) (outline-next-heading))
  7575. (setq start (point))
  7576. (goto-char (point-max))
  7577. (beginning-of-line 1)
  7578. (when (looking-at ".*?\\S-")
  7579. ;; File ends in a non-white line
  7580. (end-of-line 1)
  7581. (insert "\n"))
  7582. (setq end (point-max))
  7583. (setq what "top-level")
  7584. (goto-char start)
  7585. (show-all)))
  7586. (setq beg (point))
  7587. (if (>= beg end) (error "Nothing to sort"))
  7588. (looking-at "\\(\\*+\\)")
  7589. (setq stars (match-string 1)
  7590. re (concat "^" (regexp-quote stars) " +")
  7591. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7592. txt (buffer-substring beg end))
  7593. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7594. (if (and (not (equal stars "*")) (string-match re2 txt))
  7595. (error "Region to sort contains a level above the first entry"))
  7596. (unless sorting-type
  7597. (message
  7598. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7599. [t]ime [s]cheduled [d]eadline [c]reated
  7600. A/N/P/R/O/F/T/S/D/C means reversed:"
  7601. what)
  7602. (setq sorting-type (read-char-exclusive))
  7603. (unless getkey-func
  7604. (and (= (downcase sorting-type) ?f)
  7605. (setq getkey-func
  7606. (org-icompleting-read "Sort using function: "
  7607. obarray 'fboundp t nil nil))
  7608. (setq getkey-func (intern getkey-func))))
  7609. (and (= (downcase sorting-type) ?r)
  7610. (not property)
  7611. (setq property
  7612. (org-icompleting-read "Property: "
  7613. (mapcar 'list (org-buffer-property-keys t))
  7614. nil t))))
  7615. (message "Sorting entries...")
  7616. (save-restriction
  7617. (narrow-to-region start end)
  7618. (let ((dcst (downcase sorting-type))
  7619. (case-fold-search nil)
  7620. (now (current-time)))
  7621. (sort-subr
  7622. (/= dcst sorting-type)
  7623. ;; This function moves to the beginning character of the "record" to
  7624. ;; be sorted.
  7625. (lambda nil
  7626. (if (re-search-forward re nil t)
  7627. (goto-char (match-beginning 0))
  7628. (goto-char (point-max))))
  7629. ;; This function moves to the last character of the "record" being
  7630. ;; sorted.
  7631. (lambda nil
  7632. (save-match-data
  7633. (condition-case nil
  7634. (outline-forward-same-level 1)
  7635. (error
  7636. (goto-char (point-max))))))
  7637. ;; This function returns the value that gets sorted against.
  7638. (lambda nil
  7639. (cond
  7640. ((= dcst ?n)
  7641. (if (looking-at org-complex-heading-regexp)
  7642. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7643. nil))
  7644. ((= dcst ?a)
  7645. (if (looking-at org-complex-heading-regexp)
  7646. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7647. nil))
  7648. ((= dcst ?t)
  7649. (let ((end (save-excursion (outline-next-heading) (point))))
  7650. (if (or (re-search-forward org-ts-regexp end t)
  7651. (re-search-forward org-ts-regexp-both end t))
  7652. (org-time-string-to-seconds (match-string 0))
  7653. (org-float-time now))))
  7654. ((= dcst ?c)
  7655. (let ((end (save-excursion (outline-next-heading) (point))))
  7656. (if (re-search-forward
  7657. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7658. end t)
  7659. (org-time-string-to-seconds (match-string 0))
  7660. (org-float-time now))))
  7661. ((= dcst ?s)
  7662. (let ((end (save-excursion (outline-next-heading) (point))))
  7663. (if (re-search-forward org-scheduled-time-regexp end t)
  7664. (org-time-string-to-seconds (match-string 1))
  7665. (org-float-time now))))
  7666. ((= dcst ?d)
  7667. (let ((end (save-excursion (outline-next-heading) (point))))
  7668. (if (re-search-forward org-deadline-time-regexp end t)
  7669. (org-time-string-to-seconds (match-string 1))
  7670. (org-float-time now))))
  7671. ((= dcst ?p)
  7672. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7673. (string-to-char (match-string 2))
  7674. org-default-priority))
  7675. ((= dcst ?r)
  7676. (or (org-entry-get nil property) ""))
  7677. ((= dcst ?o)
  7678. (if (looking-at org-complex-heading-regexp)
  7679. (- 9999 (length (member (match-string 2)
  7680. org-todo-keywords-1)))))
  7681. ((= dcst ?f)
  7682. (if getkey-func
  7683. (progn
  7684. (setq tmp (funcall getkey-func))
  7685. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7686. tmp)
  7687. (error "Invalid key function `%s'" getkey-func)))
  7688. (t (error "Invalid sorting type `%c'" sorting-type))))
  7689. nil
  7690. (cond
  7691. ((= dcst ?a) 'string<)
  7692. ((= dcst ?f) compare-func)
  7693. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7694. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7695. ;; Reset the clock marker if needed
  7696. (when cmstr
  7697. (save-excursion
  7698. (goto-char start)
  7699. (search-forward cmstr nil t)
  7700. (move-marker org-clock-marker (point))))
  7701. (message "Sorting entries...done")))
  7702. (defun org-do-sort (table what &optional with-case sorting-type)
  7703. "Sort TABLE of WHAT according to SORTING-TYPE.
  7704. The user will be prompted for the SORTING-TYPE if the call to this
  7705. function does not specify it. WHAT is only for the prompt, to indicate
  7706. what is being sorted. The sorting key will be extracted from
  7707. the car of the elements of the table.
  7708. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7709. (unless sorting-type
  7710. (message
  7711. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7712. what)
  7713. (setq sorting-type (read-char-exclusive)))
  7714. (let ((dcst (downcase sorting-type))
  7715. extractfun comparefun)
  7716. ;; Define the appropriate functions
  7717. (cond
  7718. ((= dcst ?n)
  7719. (setq extractfun 'string-to-number
  7720. comparefun (if (= dcst sorting-type) '< '>)))
  7721. ((= dcst ?a)
  7722. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7723. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7724. comparefun (if (= dcst sorting-type)
  7725. 'string<
  7726. (lambda (a b) (and (not (string< a b))
  7727. (not (string= a b)))))))
  7728. ((= dcst ?t)
  7729. (setq extractfun
  7730. (lambda (x)
  7731. (if (or (string-match org-ts-regexp x)
  7732. (string-match org-ts-regexp-both x))
  7733. (org-float-time
  7734. (org-time-string-to-time (match-string 0 x)))
  7735. 0))
  7736. comparefun (if (= dcst sorting-type) '< '>)))
  7737. (t (error "Invalid sorting type `%c'" sorting-type)))
  7738. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7739. table)
  7740. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7741. ;;; The orgstruct minor mode
  7742. ;; Define a minor mode which can be used in other modes in order to
  7743. ;; integrate the org-mode structure editing commands.
  7744. ;; This is really a hack, because the org-mode structure commands use
  7745. ;; keys which normally belong to the major mode. Here is how it
  7746. ;; works: The minor mode defines all the keys necessary to operate the
  7747. ;; structure commands, but wraps the commands into a function which
  7748. ;; tests if the cursor is currently at a headline or a plain list
  7749. ;; item. If that is the case, the structure command is used,
  7750. ;; temporarily setting many Org-mode variables like regular
  7751. ;; expressions for filling etc. However, when any of those keys is
  7752. ;; used at a different location, function uses `key-binding' to look
  7753. ;; up if the key has an associated command in another currently active
  7754. ;; keymap (minor modes, major mode, global), and executes that
  7755. ;; command. There might be problems if any of the keys is otherwise
  7756. ;; used as a prefix key.
  7757. (defcustom orgstruct-heading-prefix-regexp ""
  7758. "Regexp that matches the custom prefix of Org headlines in
  7759. orgstruct(++)-mode."
  7760. :group 'org
  7761. :version "24.4"
  7762. :package-version '(Org . "8.0")
  7763. :type 'string)
  7764. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7765. (defcustom orgstruct-setup-hook nil
  7766. "Hook run after orgstruct-mode-map is filled."
  7767. :group 'org
  7768. :type 'hook)
  7769. (defvar orgstruct-initialized nil)
  7770. (defvar org-local-vars nil
  7771. "List of local variables, for use by `orgstruct-mode'.")
  7772. ;;;###autoload
  7773. (define-minor-mode orgstruct-mode
  7774. "Toggle the minor mode `orgstruct-mode'.
  7775. This mode is for using Org-mode structure commands in other
  7776. modes. The following keys behave as if Org-mode were active, if
  7777. the cursor is on a headline, or on a plain list item (both as
  7778. defined by Org-mode)."
  7779. nil " OrgStruct" (make-sparse-keymap)
  7780. (when orgstruct-mode
  7781. (org-load-modules-maybe)
  7782. (unless orgstruct-initialized
  7783. (orgstruct-setup)
  7784. (setq orgstruct-initialized t))))
  7785. ;;;###autoload
  7786. (defun turn-on-orgstruct ()
  7787. "Unconditionally turn on `orgstruct-mode'."
  7788. (orgstruct-mode 1))
  7789. (defvar org-fb-vars nil)
  7790. (make-variable-buffer-local 'org-fb-vars)
  7791. (defun orgstruct++-mode (&optional arg)
  7792. "Toggle `orgstruct-mode', the enhanced version of it.
  7793. In addition to setting orgstruct-mode, this also exports all
  7794. indentation and autofilling variables from org-mode into the
  7795. buffer. It will also recognize item context in multiline items."
  7796. (interactive "P")
  7797. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7798. (if (< arg 1)
  7799. (progn (orgstruct-mode -1)
  7800. (mapc (lambda(v)
  7801. (org-set-local (car v)
  7802. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7803. org-fb-vars))
  7804. (orgstruct-mode 1)
  7805. (setq org-fb-vars nil)
  7806. (let (var val)
  7807. (mapc
  7808. (lambda (x)
  7809. (when (string-match
  7810. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7811. (symbol-name (car x)))
  7812. (setq var (car x) val (nth 1 x))
  7813. (push (list var `(quote ,(eval var))) org-fb-vars)
  7814. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7815. org-local-vars)
  7816. (org-set-local 'orgstruct-is-++ t))))
  7817. (defvar orgstruct-is-++ nil
  7818. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7819. (make-variable-buffer-local 'orgstruct-is-++)
  7820. ;;;###autoload
  7821. (defun turn-on-orgstruct++ ()
  7822. "Unconditionally turn on `orgstruct++-mode'."
  7823. (orgstruct++-mode 1))
  7824. (defun orgstruct-error ()
  7825. "Error when there is no default binding for a structure key."
  7826. (interactive)
  7827. (error "This key has no function outside structure elements"))
  7828. (defun orgstruct-setup ()
  7829. "Setup orgstruct keymap."
  7830. (dolist (f
  7831. '("org-meta"
  7832. "org-shift"
  7833. "org-shiftmeta"
  7834. org-shifttab
  7835. org-backward-element
  7836. org-backward-heading-same-level
  7837. org-ctrl-c-ret
  7838. org-cycle
  7839. org-forward-heading-same-level
  7840. org-insert-heading
  7841. org-insert-heading-respect-content
  7842. org-kill-note-or-show-branches
  7843. org-mark-subtree
  7844. org-narrow-to-subtree
  7845. org-promote-subtree
  7846. org-reveal
  7847. org-show-subtree
  7848. org-sort
  7849. org-up-element
  7850. outline-demote
  7851. outline-next-visible-heading
  7852. outline-previous-visible-heading
  7853. outline-promote
  7854. outline-up-heading
  7855. show-children))
  7856. (dolist (f (if (stringp f)
  7857. (let ((flist))
  7858. (dolist (postfix
  7859. '("-return" "tab" "left" "right" "up" "down")
  7860. flist)
  7861. (let ((f (intern (concat f postfix))))
  7862. (when (fboundp f)
  7863. (push f flist)))))
  7864. (list f)))
  7865. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7866. (where-is-internal f outline-mode-map)))
  7867. ;; TODO use local-function-key-map
  7868. (dolist (rep '(("<tab>" . "TAB")
  7869. ("<return>" . "RET")
  7870. ("<escape>" . "ESC")
  7871. ("<delete>" . "DEL")))
  7872. (setq binding (read-kbd-macro (replace-regexp-in-string
  7873. (regexp-quote (car rep))
  7874. (cdr rep)
  7875. (key-description binding)))))
  7876. (let ((key (lookup-key orgstruct-mode-map binding)))
  7877. (when (or (not key) (numberp key))
  7878. (condition-case nil
  7879. (org-defkey orgstruct-mode-map
  7880. binding
  7881. (orgstruct-make-binding f binding))
  7882. (error nil)))))))
  7883. (run-hooks 'orgstruct-setup-hook))
  7884. (defun orgstruct-make-binding (fun key)
  7885. "Create a function for binding in the structure minor mode.
  7886. FUN is the command to call inside a table. KEY is the key that
  7887. should be checked in for a command to execute outside of tables."
  7888. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7889. (let ((nname name)
  7890. (i 0))
  7891. (while (fboundp (intern nname))
  7892. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7893. (setq name (intern nname)))
  7894. (eval
  7895. (let ((bindings '((org-heading-regexp
  7896. (concat "^"
  7897. orgstruct-heading-prefix-regexp
  7898. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7899. (org-outline-regexp
  7900. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7901. (org-outline-regexp-bol
  7902. (concat "^" org-outline-regexp))
  7903. (outline-regexp org-outline-regexp)
  7904. (outline-heading-end-regexp "\n")
  7905. (outline-level 'outline-level)
  7906. (outline-heading-alist))))
  7907. `(defun ,name (arg)
  7908. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7909. "Outside of structure, run the binding of `"
  7910. (key-description key) "'.")
  7911. (interactive "p")
  7912. (unless
  7913. (let* ,bindings
  7914. (when (org-context-p 'headline 'item
  7915. ,(when (memq fun '(org-insert-heading))
  7916. '(when orgstruct-is-++
  7917. 'item-body)))
  7918. (org-run-like-in-org-mode
  7919. (lambda ()
  7920. (interactive)
  7921. (let* ,bindings
  7922. (call-interactively ',fun))))
  7923. t))
  7924. (let* ((orgstruct-mode)
  7925. (binding (key-binding ,key)))
  7926. (if (keymapp binding)
  7927. (set-temporary-overlay-map binding)
  7928. (call-interactively
  7929. (or binding 'orgstruct-error))))))))
  7930. name))
  7931. (defun org-contextualize-keys (alist contexts)
  7932. "Return valid elements in ALIST depending on CONTEXTS.
  7933. `org-agenda-custom-commands' or `org-capture-templates' are the
  7934. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7935. or `org-capture-templates-contexts' are the associated contexts
  7936. definitions."
  7937. (let ((contexts
  7938. ;; normalize contexts
  7939. (mapcar
  7940. (lambda(c) (cond ((listp (cadr c))
  7941. (list (car c) (car c) (cadr c)))
  7942. ((string= "" (cadr c))
  7943. (list (car c) (car c) (caddr c)))
  7944. (t c))) contexts))
  7945. (a alist) c r s)
  7946. ;; loop over all commands or templates
  7947. (while (setq c (pop a))
  7948. (let (vrules repl)
  7949. (cond
  7950. ((not (assoc (car c) contexts))
  7951. (push c r))
  7952. ((and (assoc (car c) contexts)
  7953. (setq vrules (org-contextualize-validate-key
  7954. (car c) contexts)))
  7955. (mapc (lambda (vr)
  7956. (when (not (equal (car vr) (cadr vr)))
  7957. (setq repl vr))) vrules)
  7958. (if (not repl) (push c r)
  7959. (push (cadr repl) s)
  7960. (push
  7961. (cons (car c)
  7962. (cdr (or (assoc (cadr repl) alist)
  7963. (error "Undefined key `%s' as contextual replacement for `%s'"
  7964. (cadr repl) (car c)))))
  7965. r))))))
  7966. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7967. (delq
  7968. nil
  7969. (delete-dups
  7970. (mapcar (lambda (x)
  7971. (let ((tpl (car x)))
  7972. (when (not (delq
  7973. nil
  7974. (mapcar (lambda(y)
  7975. (equal y tpl)) s))) x)))
  7976. (reverse r))))))
  7977. (defun org-contextualize-validate-key (key contexts)
  7978. "Check CONTEXTS for agenda or capture KEY."
  7979. (let (r rr res)
  7980. (while (setq r (pop contexts))
  7981. (mapc
  7982. (lambda (rr)
  7983. (when
  7984. (and (equal key (car r))
  7985. (if (functionp rr) (funcall rr)
  7986. (or (and (eq (car rr) 'in-file)
  7987. (buffer-file-name)
  7988. (string-match (cdr rr) (buffer-file-name)))
  7989. (and (eq (car rr) 'in-mode)
  7990. (string-match (cdr rr) (symbol-name major-mode)))
  7991. (and (eq (car rr) 'in-buffer)
  7992. (string-match (cdr rr) (buffer-name)))
  7993. (when (and (eq (car rr) 'not-in-file)
  7994. (buffer-file-name))
  7995. (not (string-match (cdr rr) (buffer-file-name))))
  7996. (when (eq (car rr) 'not-in-mode)
  7997. (not (string-match (cdr rr) (symbol-name major-mode))))
  7998. (when (eq (car rr) 'not-in-buffer)
  7999. (not (string-match (cdr rr) (buffer-name)))))))
  8000. (push r res)))
  8001. (car (last r))))
  8002. (delete-dups (delq nil res))))
  8003. (defun org-context-p (&rest contexts)
  8004. "Check if local context is any of CONTEXTS.
  8005. Possible values in the list of contexts are `table', `headline', and `item'."
  8006. (let ((pos (point)))
  8007. (goto-char (point-at-bol))
  8008. (prog1 (or (and (memq 'table contexts)
  8009. (looking-at "[ \t]*|"))
  8010. (and (memq 'headline contexts)
  8011. (looking-at org-outline-regexp))
  8012. (and (memq 'item contexts)
  8013. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8014. (and (memq 'item-body contexts)
  8015. (org-in-item-p)))
  8016. (goto-char pos))))
  8017. (defun org-get-local-variables ()
  8018. "Return a list of all local variables in an Org mode buffer."
  8019. (let (varlist)
  8020. (with-current-buffer (get-buffer-create "*Org tmp*")
  8021. (erase-buffer)
  8022. (org-mode)
  8023. (setq varlist (buffer-local-variables)))
  8024. (kill-buffer "*Org tmp*")
  8025. (delq nil
  8026. (mapcar
  8027. (lambda (x)
  8028. (setq x
  8029. (if (symbolp x)
  8030. (list x)
  8031. (list (car x) (cdr x))))
  8032. (if (and (not (get (car x) 'org-state))
  8033. (string-match
  8034. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8035. (symbol-name (car x))))
  8036. x nil))
  8037. varlist))))
  8038. (defun org-clone-local-variables (from-buffer &optional regexp)
  8039. "Clone local variables from FROM-BUFFER.
  8040. Optional argument REGEXP selects variables to clone."
  8041. (mapc
  8042. (lambda (pair)
  8043. (and (symbolp (car pair))
  8044. (or (null regexp)
  8045. (string-match regexp (symbol-name (car pair))))
  8046. (set (make-local-variable (car pair))
  8047. (cdr pair))))
  8048. (buffer-local-variables from-buffer)))
  8049. ;;;###autoload
  8050. (defun org-run-like-in-org-mode (cmd)
  8051. "Run a command, pretending that the current buffer is in Org-mode.
  8052. This will temporarily bind local variables that are typically bound in
  8053. Org-mode to the values they have in Org-mode, and then interactively
  8054. call CMD."
  8055. (org-load-modules-maybe)
  8056. (unless org-local-vars
  8057. (setq org-local-vars (org-get-local-variables)))
  8058. (let (binds)
  8059. (dolist (var org-local-vars)
  8060. (when (or (not (boundp (car var)))
  8061. (eq (symbol-value (car var))
  8062. (default-value (car var))))
  8063. (push (list (car var) `(quote ,(cadr var))) binds)))
  8064. (eval `(let ,binds
  8065. (call-interactively (quote ,cmd))))))
  8066. ;;;; Archiving
  8067. (defun org-get-category (&optional pos force-refresh)
  8068. "Get the category applying to position POS."
  8069. (save-match-data
  8070. (if force-refresh (org-refresh-category-properties))
  8071. (let ((pos (or pos (point))))
  8072. (or (get-text-property pos 'org-category)
  8073. (progn (org-refresh-category-properties)
  8074. (get-text-property pos 'org-category))))))
  8075. (defun org-refresh-category-properties ()
  8076. "Refresh category text properties in the buffer."
  8077. (let ((case-fold-search t)
  8078. (inhibit-read-only t)
  8079. (def-cat (cond
  8080. ((null org-category)
  8081. (if buffer-file-name
  8082. (file-name-sans-extension
  8083. (file-name-nondirectory buffer-file-name))
  8084. "???"))
  8085. ((symbolp org-category) (symbol-name org-category))
  8086. (t org-category)))
  8087. beg end cat pos optionp)
  8088. (org-with-silent-modifications
  8089. (save-excursion
  8090. (save-restriction
  8091. (widen)
  8092. (goto-char (point-min))
  8093. (put-text-property (point) (point-max) 'org-category def-cat)
  8094. (while (re-search-forward
  8095. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8096. (setq pos (match-end 0)
  8097. optionp (equal (char-after (match-beginning 0)) ?#)
  8098. cat (org-trim (match-string 2)))
  8099. (if optionp
  8100. (setq beg (point-at-bol) end (point-max))
  8101. (org-back-to-heading t)
  8102. (setq beg (point) end (org-end-of-subtree t t)))
  8103. (put-text-property beg end 'org-category cat)
  8104. (put-text-property beg end 'org-category-position beg)
  8105. (goto-char pos)))))))
  8106. (defun org-refresh-properties (dprop tprop)
  8107. "Refresh buffer text properties.
  8108. DPROP is the drawer property and TPROP is the corresponding text
  8109. property to set."
  8110. (let ((case-fold-search t)
  8111. (inhibit-read-only t) p)
  8112. (org-with-silent-modifications
  8113. (save-excursion
  8114. (save-restriction
  8115. (widen)
  8116. (goto-char (point-min))
  8117. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8118. (setq p (org-match-string-no-properties 1))
  8119. (save-excursion
  8120. (org-back-to-heading t)
  8121. (put-text-property
  8122. (point-at-bol) (point-at-eol) tprop p))))))))
  8123. ;;;; Link Stuff
  8124. ;;; Link abbreviations
  8125. (defun org-link-expand-abbrev (link)
  8126. "Apply replacements as defined in `org-link-abbrev-alist'."
  8127. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8128. (let* ((key (match-string 1 link))
  8129. (as (or (assoc key org-link-abbrev-alist-local)
  8130. (assoc key org-link-abbrev-alist)))
  8131. (tag (and (match-end 2) (match-string 3 link)))
  8132. rpl)
  8133. (if (not as)
  8134. link
  8135. (setq rpl (cdr as))
  8136. (cond
  8137. ((symbolp rpl) (funcall rpl tag))
  8138. ((string-match "%(\\([^)]+\\))" rpl)
  8139. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8140. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8141. ((string-match "%h" rpl)
  8142. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8143. (t (concat rpl tag)))))
  8144. link))
  8145. ;;; Storing and inserting links
  8146. (defvar org-insert-link-history nil
  8147. "Minibuffer history for links inserted with `org-insert-link'.")
  8148. (defvar org-stored-links nil
  8149. "Contains the links stored with `org-store-link'.")
  8150. (defvar org-store-link-plist nil
  8151. "Plist with info about the most recently link created with `org-store-link'.")
  8152. (defvar org-link-protocols nil
  8153. "Link protocols added to Org-mode using `org-add-link-type'.")
  8154. (defvar org-store-link-functions nil
  8155. "List of functions that are called to create and store a link.
  8156. Each function will be called in turn until one returns a non-nil
  8157. value. Each function should check if it is responsible for creating
  8158. this link (for example by looking at the major mode).
  8159. If not, it must exit and return nil.
  8160. If yes, it should return a non-nil value after a calling
  8161. `org-store-link-props' with a list of properties and values.
  8162. Special properties are:
  8163. :type The link prefix, like \"http\". This must be given.
  8164. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8165. This is obligatory as well.
  8166. :description Optional default description for the second pair
  8167. of brackets in an Org-mode link. The user can still change
  8168. this when inserting this link into an Org-mode buffer.
  8169. In addition to these, any additional properties can be specified
  8170. and then used in capture templates.")
  8171. (defun org-add-link-type (type &optional follow export)
  8172. "Add TYPE to the list of `org-link-types'.
  8173. Re-compute all regular expressions depending on `org-link-types'
  8174. FOLLOW and EXPORT are two functions.
  8175. FOLLOW should take the link path as the single argument and do whatever
  8176. is necessary to follow the link, for example find a file or display
  8177. a mail message.
  8178. EXPORT should format the link path for export to one of the export formats.
  8179. It should be a function accepting three arguments:
  8180. path the path of the link, the text after the prefix (like \"http:\")
  8181. desc the description of the link, if any, or a description added by
  8182. org-export-normalize-links if there is none
  8183. format the export format, a symbol like `html' or `latex' or `ascii'..
  8184. The function may use the FORMAT information to return different values
  8185. depending on the format. The return value will be put literally into
  8186. the exported file. If the return value is nil, this means Org should
  8187. do what it normally does with links which do not have EXPORT defined.
  8188. Org-mode has a built-in default for exporting links. If you are happy with
  8189. this default, there is no need to define an export function for the link
  8190. type. For a simple example of an export function, see `org-bbdb.el'."
  8191. (add-to-list 'org-link-types type t)
  8192. (org-make-link-regexps)
  8193. (if (assoc type org-link-protocols)
  8194. (setcdr (assoc type org-link-protocols) (list follow export))
  8195. (push (list type follow export) org-link-protocols)))
  8196. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8197. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8198. ;;;###autoload
  8199. (defun org-store-link (arg)
  8200. "\\<org-mode-map>Store an org-link to the current location.
  8201. This link is added to `org-stored-links' and can later be inserted
  8202. into an org-buffer with \\[org-insert-link].
  8203. For some link types, a prefix arg is interpreted.
  8204. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8205. For file links, arg negates `org-context-in-file-links'.
  8206. A double prefix arg force skipping storing functions that are not
  8207. part of Org's core."
  8208. (interactive "P")
  8209. (org-load-modules-maybe)
  8210. (setq org-store-link-plist nil) ; reset
  8211. (org-with-limited-levels
  8212. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8213. (cond
  8214. ((and (not (equal arg '(16)))
  8215. (setq sfuns
  8216. (delq
  8217. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8218. org-store-link-functions))
  8219. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8220. (or (and (cdr sfuns)
  8221. (funcall (intern
  8222. (completing-read "Which function for creating the link? "
  8223. sfunsn t (car sfunsn)))))
  8224. (funcall (caar sfuns)))
  8225. (setq link (plist-get org-store-link-plist :link)
  8226. desc (or (plist-get org-store-link-plist :description) link))))
  8227. ((org-src-edit-buffer-p)
  8228. (let (label gc)
  8229. (while (or (not label)
  8230. (save-excursion
  8231. (save-restriction
  8232. (widen)
  8233. (goto-char (point-min))
  8234. (re-search-forward
  8235. (regexp-quote (format org-coderef-label-format label))
  8236. nil t))))
  8237. (when label (message "Label exists already") (sit-for 2))
  8238. (setq label (read-string "Code line label: " label)))
  8239. (end-of-line 1)
  8240. (setq link (format org-coderef-label-format label))
  8241. (setq gc (- 79 (length link)))
  8242. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8243. (insert link)
  8244. (setq link (concat "(" label ")") desc nil)))
  8245. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8246. ;; We are in the agenda, link to referenced location
  8247. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8248. (get-text-property (point) 'org-marker))))
  8249. (when m
  8250. (org-with-point-at m
  8251. (setq agenda-link
  8252. (if (org-called-interactively-p 'any)
  8253. (call-interactively 'org-store-link)
  8254. (org-store-link nil)))))))
  8255. ((eq major-mode 'calendar-mode)
  8256. (let ((cd (calendar-cursor-to-date)))
  8257. (setq link
  8258. (format-time-string
  8259. (car org-time-stamp-formats)
  8260. (apply 'encode-time
  8261. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8262. nil nil nil))))
  8263. (org-store-link-props :type "calendar" :date cd)))
  8264. ((eq major-mode 'help-mode)
  8265. (setq link (concat "help:" (save-excursion
  8266. (goto-char (point-min))
  8267. (looking-at "^[^ ]+")
  8268. (match-string 0))))
  8269. (org-store-link-props :type "help"))
  8270. ((eq major-mode 'w3-mode)
  8271. (setq cpltxt (if (and (buffer-name)
  8272. (not (string-match "Untitled" (buffer-name))))
  8273. (buffer-name)
  8274. (url-view-url t))
  8275. link (url-view-url t))
  8276. (org-store-link-props :type "w3" :url (url-view-url t)))
  8277. ((setq search (run-hook-with-args-until-success
  8278. 'org-create-file-search-functions))
  8279. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8280. "::" search))
  8281. (setq cpltxt (or description link)))
  8282. ((eq major-mode 'image-mode)
  8283. (setq cpltxt (concat "file:"
  8284. (abbreviate-file-name buffer-file-name))
  8285. link cpltxt)
  8286. (org-store-link-props :type "image" :file buffer-file-name))
  8287. ((eq major-mode 'dired-mode)
  8288. ;; link to the file in the current line
  8289. (let ((file (dired-get-filename nil t)))
  8290. (setq file (if file
  8291. (abbreviate-file-name
  8292. (expand-file-name (dired-get-filename nil t)))
  8293. ;; otherwise, no file so use current directory.
  8294. default-directory))
  8295. (setq cpltxt (concat "file:" file)
  8296. link cpltxt)))
  8297. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8298. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8299. (cond
  8300. ((org-in-regexp "<<\\(.*?\\)>>")
  8301. (setq cpltxt
  8302. (concat "file:"
  8303. (abbreviate-file-name
  8304. (buffer-file-name (buffer-base-buffer)))
  8305. "::" (match-string 1))
  8306. link cpltxt))
  8307. ((and (featurep 'org-id)
  8308. (or (eq org-id-link-to-org-use-id t)
  8309. (and (org-called-interactively-p 'any)
  8310. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8311. (and (eq org-id-link-to-org-use-id
  8312. 'create-if-interactive-and-no-custom-id)
  8313. (not custom-id))))
  8314. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8315. ;; We can make a link using the ID.
  8316. (setq link (condition-case nil
  8317. (prog1 (org-id-store-link)
  8318. (setq desc (plist-get org-store-link-plist :description)))
  8319. (error
  8320. ;; probably before first headline, link to file only
  8321. (concat "file:"
  8322. (abbreviate-file-name
  8323. (buffer-file-name (buffer-base-buffer))))))))
  8324. (t
  8325. ;; Just link to current headline
  8326. (setq cpltxt (concat "file:"
  8327. (abbreviate-file-name
  8328. (buffer-file-name (buffer-base-buffer)))))
  8329. ;; Add a context search string
  8330. (when (org-xor org-context-in-file-links arg)
  8331. (let* ((ee (org-element-at-point))
  8332. (et (org-element-type ee))
  8333. (ev (plist-get (cadr ee) :value)))
  8334. (setq txt (cond
  8335. ((org-at-heading-p) nil)
  8336. ((eq et 'keyword) ev)
  8337. ((org-region-active-p)
  8338. (buffer-substring (region-beginning) (region-end)))))
  8339. (when (or (null txt) (string-match "\\S-" txt))
  8340. (setq cpltxt
  8341. (concat cpltxt "::"
  8342. (condition-case nil
  8343. (org-make-org-heading-search-string txt)
  8344. (error "")))
  8345. desc (or (and (eq et 'keyword) ev)
  8346. (nth 4 (ignore-errors (org-heading-components)))
  8347. "NONE")))))
  8348. (if (string-match "::\\'" cpltxt)
  8349. (setq cpltxt (substring cpltxt 0 -2)))
  8350. (setq link cpltxt))))
  8351. ((buffer-file-name (buffer-base-buffer))
  8352. ;; Just link to this file here.
  8353. (setq cpltxt (concat "file:"
  8354. (abbreviate-file-name
  8355. (buffer-file-name (buffer-base-buffer)))))
  8356. ;; Add a context string
  8357. (when (org-xor org-context-in-file-links arg)
  8358. (setq txt (if (org-region-active-p)
  8359. (buffer-substring (region-beginning) (region-end))
  8360. (buffer-substring (point-at-bol) (point-at-eol))))
  8361. ;; Only use search option if there is some text.
  8362. (when (string-match "\\S-" txt)
  8363. (setq cpltxt
  8364. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8365. desc "NONE")))
  8366. (setq link cpltxt))
  8367. ((org-called-interactively-p 'interactive)
  8368. (user-error "No method for storing a link from this buffer"))
  8369. (t (setq link nil)))
  8370. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8371. (setq link (or link cpltxt)
  8372. desc (or desc cpltxt))
  8373. (cond ((equal desc "NONE") (setq desc nil))
  8374. ((string-match org-bracket-link-regexp desc)
  8375. (setq desc (replace-regexp-in-string
  8376. org-bracket-link-regexp
  8377. (concat "\\3" (if (equal (length (match-string 0 desc))
  8378. (length desc)) "*" "")) desc))))
  8379. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8380. (progn
  8381. (setq org-stored-links
  8382. (cons (list link desc) org-stored-links))
  8383. (message "Stored: %s" (or desc link))
  8384. (when custom-id
  8385. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8386. "::#" custom-id))
  8387. (setq org-stored-links
  8388. (cons (list link desc) org-stored-links))))
  8389. (or agenda-link (and link (org-make-link-string link desc)))))))
  8390. (defun org-store-link-props (&rest plist)
  8391. "Store link properties, extract names and addresses."
  8392. (let (x adr)
  8393. (when (setq x (plist-get plist :from))
  8394. (setq adr (mail-extract-address-components x))
  8395. (setq plist (plist-put plist :fromname (car adr)))
  8396. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8397. (when (setq x (plist-get plist :to))
  8398. (setq adr (mail-extract-address-components x))
  8399. (setq plist (plist-put plist :toname (car adr)))
  8400. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8401. (let ((from (plist-get plist :from))
  8402. (to (plist-get plist :to)))
  8403. (when (and from to org-from-is-user-regexp)
  8404. (setq plist
  8405. (plist-put plist :fromto
  8406. (if (string-match org-from-is-user-regexp from)
  8407. (concat "to %t")
  8408. (concat "from %f"))))))
  8409. (setq org-store-link-plist plist))
  8410. (defun org-add-link-props (&rest plist)
  8411. "Add these properties to the link property list."
  8412. (let (key value)
  8413. (while plist
  8414. (setq key (pop plist) value (pop plist))
  8415. (setq org-store-link-plist
  8416. (plist-put org-store-link-plist key value)))))
  8417. (defun org-email-link-description (&optional fmt)
  8418. "Return the description part of an email link.
  8419. This takes information from `org-store-link-plist' and formats it
  8420. according to FMT (default from `org-email-link-description-format')."
  8421. (setq fmt (or fmt org-email-link-description-format))
  8422. (let* ((p org-store-link-plist)
  8423. (to (plist-get p :toaddress))
  8424. (from (plist-get p :fromaddress))
  8425. (table
  8426. (list
  8427. (cons "%c" (plist-get p :fromto))
  8428. (cons "%F" (plist-get p :from))
  8429. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8430. (cons "%T" (plist-get p :to))
  8431. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8432. (cons "%s" (plist-get p :subject))
  8433. (cons "%d" (plist-get p :date))
  8434. (cons "%m" (plist-get p :message-id)))))
  8435. (when (string-match "%c" fmt)
  8436. ;; Check if the user wrote this message
  8437. (if (and org-from-is-user-regexp from to
  8438. (save-match-data (string-match org-from-is-user-regexp from)))
  8439. (setq fmt (replace-match "to %t" t t fmt))
  8440. (setq fmt (replace-match "from %f" t t fmt))))
  8441. (org-replace-escapes fmt table)))
  8442. (defun org-make-org-heading-search-string (&optional string)
  8443. "Make search string for the current headline or STRING."
  8444. (let ((s (or string
  8445. (and (derived-mode-p 'org-mode)
  8446. (save-excursion
  8447. (org-back-to-heading t)
  8448. (org-element-property :raw-value (org-element-at-point))))))
  8449. (lines org-context-in-file-links))
  8450. (or string (setq s (concat "*" s))) ; Add * for headlines
  8451. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8452. (when (and string (integerp lines) (> lines 0))
  8453. (let ((slines (org-split-string s "\n")))
  8454. (when (< lines (length slines))
  8455. (setq s (mapconcat
  8456. 'identity
  8457. (reverse (nthcdr (- (length slines) lines)
  8458. (reverse slines))) "\n")))))
  8459. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8460. (defun org-make-link-string (link &optional description)
  8461. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8462. (unless (string-match "\\S-" link)
  8463. (error "Empty link"))
  8464. (when (and description
  8465. (stringp description)
  8466. (not (string-match "\\S-" description)))
  8467. (setq description nil))
  8468. (when (stringp description)
  8469. ;; Remove brackets from the description, they are fatal.
  8470. (while (string-match "\\[" description)
  8471. (setq description (replace-match "{" t t description)))
  8472. (while (string-match "\\]" description)
  8473. (setq description (replace-match "}" t t description))))
  8474. (when (equal link description)
  8475. ;; No description needed, it is identical
  8476. (setq description nil))
  8477. (when (and (not description)
  8478. (not (string-match (org-image-file-name-regexp) link))
  8479. (not (equal link (org-link-escape link))))
  8480. (setq description (org-extract-attributes link)))
  8481. (setq link
  8482. (cond ((string-match (org-image-file-name-regexp) link) link)
  8483. ((string-match org-link-types-re link)
  8484. (concat (match-string 1 link)
  8485. (org-link-escape (substring link (match-end 1)))))
  8486. (t (org-link-escape link))))
  8487. (concat "[[" link "]"
  8488. (if description (concat "[" description "]") "")
  8489. "]"))
  8490. (defconst org-link-escape-chars
  8491. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8492. "List of characters that should be escaped in link.
  8493. This is the list that is used for internal purposes.")
  8494. (defconst org-link-escape-chars-browser
  8495. '(?\ )
  8496. "List of escapes for characters that are problematic in links.
  8497. This is the list that is used before handing over to the browser.")
  8498. (defun org-link-escape (text &optional table merge)
  8499. "Return percent escaped representation of TEXT.
  8500. TEXT is a string with the text to escape.
  8501. Optional argument TABLE is a list with characters that should be
  8502. escaped. When nil, `org-link-escape-chars' is used.
  8503. If optional argument MERGE is set, merge TABLE into
  8504. `org-link-escape-chars'."
  8505. (cond
  8506. ((and table merge)
  8507. (mapc (lambda (defchr)
  8508. (unless (member defchr table)
  8509. (setq table (cons defchr table)))) org-link-escape-chars))
  8510. ((null table)
  8511. (setq table org-link-escape-chars)))
  8512. (mapconcat
  8513. (lambda (char)
  8514. (if (or (member char table)
  8515. (and (or (< char 32) (= char 37) (> char 126))
  8516. org-url-hexify-p))
  8517. (mapconcat (lambda (sequence-element)
  8518. (format "%%%.2X" sequence-element))
  8519. (or (encode-coding-char char 'utf-8)
  8520. (error "Unable to percent escape character: %s"
  8521. (char-to-string char))) "")
  8522. (char-to-string char))) text ""))
  8523. (defun org-link-unescape (str)
  8524. "Unhex hexified Unicode strings as returned from the JavaScript function
  8525. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8526. (unless (and (null str) (string= "" str))
  8527. (let ((pos 0) (case-fold-search t) unhexed)
  8528. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8529. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8530. (setq str (replace-match unhexed t t str))
  8531. (setq pos (+ pos (length unhexed))))))
  8532. str)
  8533. (defun org-link-unescape-compound (hex)
  8534. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8535. Note: this function also decodes single byte encodings like
  8536. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8537. (save-match-data
  8538. (let* ((bytes (cdr (split-string hex "%")))
  8539. (ret "")
  8540. (eat 0)
  8541. (sum 0))
  8542. (while bytes
  8543. (let* ((val (string-to-number (pop bytes) 16))
  8544. (shift-xor
  8545. (if (= 0 eat)
  8546. (cond
  8547. ((>= val 252) (cons 6 252))
  8548. ((>= val 248) (cons 5 248))
  8549. ((>= val 240) (cons 4 240))
  8550. ((>= val 224) (cons 3 224))
  8551. ((>= val 192) (cons 2 192))
  8552. (t (cons 0 0)))
  8553. (cons 6 128))))
  8554. (if (>= val 192) (setq eat (car shift-xor)))
  8555. (setq val (logxor val (cdr shift-xor)))
  8556. (setq sum (+ (lsh sum (car shift-xor)) val))
  8557. (if (> eat 0) (setq eat (- eat 1)))
  8558. (cond
  8559. ((= 0 eat) ;multi byte
  8560. (setq ret (concat ret (org-char-to-string sum)))
  8561. (setq sum 0))
  8562. ((not bytes) ; single byte(s)
  8563. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8564. )) ;; end (while bytes
  8565. ret )))
  8566. (defun org-link-unescape-single-byte-sequence (hex)
  8567. "Unhexify hex-encoded single byte character sequences."
  8568. (mapconcat (lambda (byte)
  8569. (char-to-string (string-to-number byte 16)))
  8570. (cdr (split-string hex "%")) ""))
  8571. (defun org-xor (a b)
  8572. "Exclusive or."
  8573. (if a (not b) b))
  8574. (defun org-fixup-message-id-for-http (s)
  8575. "Replace special characters in a message id, so it can be used in an http query."
  8576. (when (string-match "%" s)
  8577. (setq s (mapconcat (lambda (c)
  8578. (if (eq c ?%)
  8579. "%25"
  8580. (char-to-string c)))
  8581. s "")))
  8582. (while (string-match "<" s)
  8583. (setq s (replace-match "%3C" t t s)))
  8584. (while (string-match ">" s)
  8585. (setq s (replace-match "%3E" t t s)))
  8586. (while (string-match "@" s)
  8587. (setq s (replace-match "%40" t t s)))
  8588. s)
  8589. (defun org-link-prettify (link)
  8590. "Return a human-readable representation of LINK.
  8591. The car of LINK must be a raw link the cdr of LINK must be either
  8592. a link description or nil."
  8593. (let ((desc (or (cadr link) "<no description>")))
  8594. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8595. "<" (car link) ">")))
  8596. ;;;###autoload
  8597. (defun org-insert-link-global ()
  8598. "Insert a link like Org-mode does.
  8599. This command can be called in any mode to insert a link in Org-mode syntax."
  8600. (interactive)
  8601. (org-load-modules-maybe)
  8602. (org-run-like-in-org-mode 'org-insert-link))
  8603. (defun org-insert-all-links (&optional keep)
  8604. "Insert all links in `org-stored-links'."
  8605. (interactive "P")
  8606. (let ((links (copy-sequence org-stored-links)) l)
  8607. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8608. (insert "- ")
  8609. (org-insert-link nil (car l) (cadr l))
  8610. (insert "\n"))))
  8611. (defun org-link-fontify-links-to-this-file ()
  8612. "Fontify links to the current file in `org-stored-links'."
  8613. (let ((f (buffer-file-name)) a b)
  8614. (setq a (mapcar (lambda(l)
  8615. (let ((ll (car l)))
  8616. (when (and (string-match "^file:\\(.+\\)::" ll)
  8617. (equal f (expand-file-name (match-string 1 ll))))
  8618. ll)))
  8619. org-stored-links))
  8620. (when (featurep 'org-id)
  8621. (setq b (mapcar (lambda(l)
  8622. (let ((ll (car l)))
  8623. (when (and (string-match "^id:\\(.+\\)$" ll)
  8624. (equal f (expand-file-name
  8625. (or (org-id-find-id-file
  8626. (match-string 1 ll)) ""))))
  8627. ll)))
  8628. org-stored-links)))
  8629. (mapcar (lambda(l)
  8630. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8631. (delq nil (append a b)))))
  8632. (defvar org-link-links-in-this-file nil)
  8633. (defun org-insert-link (&optional complete-file link-location default-description)
  8634. "Insert a link. At the prompt, enter the link.
  8635. Completion can be used to insert any of the link protocol prefixes like
  8636. http or ftp in use.
  8637. The history can be used to select a link previously stored with
  8638. `org-store-link'. When the empty string is entered (i.e. if you just
  8639. press RET at the prompt), the link defaults to the most recently
  8640. stored link. As SPC triggers completion in the minibuffer, you need to
  8641. use M-SPC or C-q SPC to force the insertion of a space character.
  8642. You will also be prompted for a description, and if one is given, it will
  8643. be displayed in the buffer instead of the link.
  8644. If there is already a link at point, this command will allow you to edit link
  8645. and description parts.
  8646. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8647. be selected using completion. The path to the file will be relative to the
  8648. current directory if the file is in the current directory or a subdirectory.
  8649. Otherwise, the link will be the absolute path as completed in the minibuffer
  8650. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8651. option `org-link-file-path-type'.
  8652. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8653. the current directory or below.
  8654. With three \\[universal-argument] prefixes, negate the meaning of
  8655. `org-keep-stored-link-after-insertion'.
  8656. If `org-make-link-description-function' is non-nil, this function will be
  8657. called with the link target, and the result will be the default
  8658. link description.
  8659. If the LINK-LOCATION parameter is non-nil, this value will be
  8660. used as the link location instead of reading one interactively.
  8661. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8662. be used as the default description."
  8663. (interactive "P")
  8664. (let* ((wcf (current-window-configuration))
  8665. (origbuf (current-buffer))
  8666. (region (if (org-region-active-p)
  8667. (buffer-substring (region-beginning) (region-end))))
  8668. (remove (and region (list (region-beginning) (region-end))))
  8669. (desc region)
  8670. tmphist ; byte-compile incorrectly complains about this
  8671. (link link-location)
  8672. (abbrevs org-link-abbrev-alist-local)
  8673. entry file all-prefixes auto-desc)
  8674. (cond
  8675. (link-location) ; specified by arg, just use it.
  8676. ((org-in-regexp org-bracket-link-regexp 1)
  8677. ;; We do have a link at point, and we are going to edit it.
  8678. (setq remove (list (match-beginning 0) (match-end 0)))
  8679. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8680. (setq link (read-string "Link: "
  8681. (org-link-unescape
  8682. (org-match-string-no-properties 1)))))
  8683. ((or (org-in-regexp org-angle-link-re)
  8684. (org-in-regexp org-plain-link-re))
  8685. ;; Convert to bracket link
  8686. (setq remove (list (match-beginning 0) (match-end 0))
  8687. link (read-string "Link: "
  8688. (org-remove-angle-brackets (match-string 0)))))
  8689. ((member complete-file '((4) (16)))
  8690. ;; Completing read for file names.
  8691. (setq link (org-file-complete-link complete-file)))
  8692. (t
  8693. ;; Read link, with completion for stored links.
  8694. (org-link-fontify-links-to-this-file)
  8695. (org-switch-to-buffer-other-window "*Org Links*")
  8696. (with-current-buffer "*Org Links*"
  8697. (erase-buffer)
  8698. (insert "Insert a link.
  8699. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8700. (when org-stored-links
  8701. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8702. (insert (mapconcat 'org-link-prettify
  8703. (reverse org-stored-links) "\n")))
  8704. (goto-char (point-min)))
  8705. (let ((cw (selected-window)))
  8706. (select-window (get-buffer-window "*Org Links*" 'visible))
  8707. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8708. (unless (pos-visible-in-window-p (point-max))
  8709. (org-fit-window-to-buffer))
  8710. (and (window-live-p cw) (select-window cw)))
  8711. ;; Fake a link history, containing the stored links.
  8712. (setq tmphist (append (mapcar 'car org-stored-links)
  8713. org-insert-link-history))
  8714. (setq all-prefixes (append (mapcar 'car abbrevs)
  8715. (mapcar 'car org-link-abbrev-alist)
  8716. org-link-types))
  8717. (unwind-protect
  8718. (progn
  8719. (setq link
  8720. (org-completing-read
  8721. "Link: "
  8722. (append
  8723. (mapcar (lambda (x) (concat x ":"))
  8724. all-prefixes)
  8725. (mapcar 'car org-stored-links))
  8726. nil nil nil
  8727. 'tmphist
  8728. (caar org-stored-links)))
  8729. (if (not (string-match "\\S-" link))
  8730. (error "No link selected"))
  8731. (mapc (lambda(l)
  8732. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8733. org-stored-links)
  8734. (if (or (member link all-prefixes)
  8735. (and (equal ":" (substring link -1))
  8736. (member (substring link 0 -1) all-prefixes)
  8737. (setq link (substring link 0 -1))))
  8738. (setq link (with-current-buffer origbuf
  8739. (org-link-try-special-completion link)))))
  8740. (set-window-configuration wcf)
  8741. (kill-buffer "*Org Links*"))
  8742. (setq entry (assoc link org-stored-links))
  8743. (or entry (push link org-insert-link-history))
  8744. (setq desc (or desc (nth 1 entry)))))
  8745. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8746. (not org-keep-stored-link-after-insertion))
  8747. (setq org-stored-links (delq (assoc link org-stored-links)
  8748. org-stored-links)))
  8749. (if (string-match org-plain-link-re link)
  8750. ;; URL-like link, normalize the use of angular brackets.
  8751. (setq link (org-remove-angle-brackets link)))
  8752. ;; Check if we are linking to the current file with a search
  8753. ;; option If yes, simplify the link by using only the search
  8754. ;; option.
  8755. (when (and buffer-file-name
  8756. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8757. (let* ((path (match-string 1 link))
  8758. (case-fold-search nil)
  8759. (search (match-string 2 link)))
  8760. (save-match-data
  8761. (if (equal (file-truename buffer-file-name) (file-truename path))
  8762. ;; We are linking to this same file, with a search option
  8763. (setq link search)))))
  8764. ;; Check if we can/should use a relative path. If yes, simplify the link
  8765. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8766. (let* ((type (match-string 1 link))
  8767. (path (match-string 2 link))
  8768. (origpath path)
  8769. (case-fold-search nil))
  8770. (cond
  8771. ((or (eq org-link-file-path-type 'absolute)
  8772. (equal complete-file '(16)))
  8773. (setq path (abbreviate-file-name (expand-file-name path))))
  8774. ((eq org-link-file-path-type 'noabbrev)
  8775. (setq path (expand-file-name path)))
  8776. ((eq org-link-file-path-type 'relative)
  8777. (setq path (file-relative-name path)))
  8778. (t
  8779. (save-match-data
  8780. (if (string-match (concat "^" (regexp-quote
  8781. (expand-file-name
  8782. (file-name-as-directory
  8783. default-directory))))
  8784. (expand-file-name path))
  8785. ;; We are linking a file with relative path name.
  8786. (setq path (substring (expand-file-name path)
  8787. (match-end 0)))
  8788. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8789. (setq link (concat type path))
  8790. (if (equal desc origpath)
  8791. (setq desc path))))
  8792. (if org-make-link-description-function
  8793. (setq desc
  8794. (or (condition-case nil
  8795. (funcall org-make-link-description-function link desc)
  8796. (error (progn (message "Can't get link description from `%s'"
  8797. (symbol-name org-make-link-description-function))
  8798. (sit-for 2) nil)))
  8799. (read-string "Description: " default-description)))
  8800. (if default-description (setq desc default-description)
  8801. (setq desc (or (and auto-desc desc)
  8802. (read-string "Description: " desc)))))
  8803. (unless (string-match "\\S-" desc) (setq desc nil))
  8804. (if remove (apply 'delete-region remove))
  8805. (insert (org-make-link-string link desc))))
  8806. (defun org-link-try-special-completion (type)
  8807. "If there is completion support for link type TYPE, offer it."
  8808. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8809. (if (functionp fun)
  8810. (funcall fun)
  8811. (read-string "Link (no completion support): " (concat type ":")))))
  8812. (defun org-file-complete-link (&optional arg)
  8813. "Create a file link using completion."
  8814. (let (file link)
  8815. (setq file (org-iread-file-name "File: "))
  8816. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8817. (pwd1 (file-name-as-directory (abbreviate-file-name
  8818. (expand-file-name ".")))))
  8819. (cond
  8820. ((equal arg '(16))
  8821. (setq link (concat
  8822. "file:"
  8823. (abbreviate-file-name (expand-file-name file)))))
  8824. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8825. (setq link (concat "file:" (match-string 1 file))))
  8826. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8827. (expand-file-name file))
  8828. (setq link (concat
  8829. "file:" (match-string 1 (expand-file-name file)))))
  8830. (t (setq link (concat "file:" file)))))
  8831. link))
  8832. (defun org-iread-file-name (&rest args)
  8833. "Read-file-name using `ido-mode' speedup if available.
  8834. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8835. See `read-file-name' for a description of parameters."
  8836. (org-without-partial-completion
  8837. (if (and org-completion-use-ido
  8838. (fboundp 'ido-read-file-name)
  8839. (boundp 'ido-mode) ido-mode
  8840. (listp (second args)))
  8841. (let ((ido-enter-matching-directory nil))
  8842. (apply 'ido-read-file-name args))
  8843. (apply 'read-file-name args))))
  8844. (defun org-completing-read (&rest args)
  8845. "Completing-read with SPACE being a normal character."
  8846. (let ((enable-recursive-minibuffers t)
  8847. (minibuffer-local-completion-map
  8848. (copy-keymap minibuffer-local-completion-map)))
  8849. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8850. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8851. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8852. (apply 'org-icompleting-read args)))
  8853. (defun org-completing-read-no-i (&rest args)
  8854. (let (org-completion-use-ido org-completion-use-iswitchb)
  8855. (apply 'org-completing-read args)))
  8856. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8857. "Use iswitch as a completing-read replacement to choose from choices.
  8858. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8859. from."
  8860. (let* ((iswitchb-use-virtual-buffers nil)
  8861. (iswitchb-make-buflist-hook
  8862. (lambda ()
  8863. (setq iswitchb-temp-buflist choices))))
  8864. (iswitchb-read-buffer prompt)))
  8865. (defun org-icompleting-read (&rest args)
  8866. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8867. (org-without-partial-completion
  8868. (if (and org-completion-use-ido
  8869. (fboundp 'ido-completing-read)
  8870. (boundp 'ido-mode) ido-mode
  8871. (listp (second args)))
  8872. (let ((ido-enter-matching-directory nil))
  8873. (apply 'ido-completing-read (concat (car args))
  8874. (if (consp (car (nth 1 args)))
  8875. (mapcar 'car (nth 1 args))
  8876. (nth 1 args))
  8877. (cddr args)))
  8878. (if (and org-completion-use-iswitchb
  8879. (boundp 'iswitchb-mode) iswitchb-mode
  8880. (listp (second args)))
  8881. (apply 'org-iswitchb-completing-read (concat (car args))
  8882. (if (consp (car (nth 1 args)))
  8883. (mapcar 'car (nth 1 args))
  8884. (nth 1 args))
  8885. (cddr args))
  8886. (apply 'completing-read args)))))
  8887. (defun org-extract-attributes (s)
  8888. "Extract the attributes cookie from a string and set as text property."
  8889. (let (a attr (start 0) key value)
  8890. (save-match-data
  8891. (when (string-match "{{\\([^}]+\\)}}$" s)
  8892. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8893. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8894. (setq key (match-string 1 a) value (match-string 2 a)
  8895. start (match-end 0)
  8896. attr (plist-put attr (intern key) value))))
  8897. (org-add-props s nil 'org-attr attr))
  8898. s))
  8899. (defun org-extract-attributes-from-string (tag)
  8900. (let (key value attr)
  8901. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8902. (setq key (match-string 1 tag) value (match-string 2 tag)
  8903. tag (replace-match "" t t tag)
  8904. attr (plist-put attr (intern key) value)))
  8905. (cons tag attr)))
  8906. (defun org-attributes-to-string (plist)
  8907. "Format a property list into an HTML attribute list."
  8908. (let ((s "") key value)
  8909. (while plist
  8910. (setq key (pop plist) value (pop plist))
  8911. (and value
  8912. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8913. s))
  8914. ;;; Opening/following a link
  8915. (defvar org-link-search-failed nil)
  8916. (defvar org-open-link-functions nil
  8917. "Hook for functions finding a plain text link.
  8918. These functions must take a single argument, the link content.
  8919. They will be called for links that look like [[link text][description]]
  8920. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8921. like a filename (e.g. \"./blue.png\").
  8922. These functions will be called *before* Org attempts to resolve the
  8923. link by doing text searches in the current buffer - so if you want a
  8924. link \"[[target]]\" to still find \"<<target>>\", your function should
  8925. handle this as a special case.
  8926. When the function does handle the link, it must return a non-nil value.
  8927. If it decides that it is not responsible for this link, it must return
  8928. nil to indicate that that Org-mode can continue with other options
  8929. like exact and fuzzy text search.")
  8930. (defun org-next-link (&optional search-backward)
  8931. "Move forward to the next link.
  8932. If the link is in hidden text, expose it."
  8933. (interactive "P")
  8934. (when (and org-link-search-failed (eq this-command last-command))
  8935. (goto-char (point-min))
  8936. (message "Link search wrapped back to beginning of buffer"))
  8937. (setq org-link-search-failed nil)
  8938. (let* ((pos (point))
  8939. (ct (org-context))
  8940. (a (assoc :link ct))
  8941. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  8942. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  8943. ((looking-at org-any-link-re)
  8944. ;; Don't stay stuck at link without an org-link face
  8945. (forward-char (if search-backward -1 1))))
  8946. (if (funcall srch-fun org-any-link-re nil t)
  8947. (progn
  8948. (goto-char (match-beginning 0))
  8949. (if (outline-invisible-p) (org-show-context)))
  8950. (goto-char pos)
  8951. (setq org-link-search-failed t)
  8952. (message "No further link found"))))
  8953. (defun org-previous-link ()
  8954. "Move backward to the previous link.
  8955. If the link is in hidden text, expose it."
  8956. (interactive)
  8957. (funcall 'org-next-link t))
  8958. (defun org-translate-link (s)
  8959. "Translate a link string if a translation function has been defined."
  8960. (if (and org-link-translation-function
  8961. (fboundp org-link-translation-function)
  8962. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8963. (progn
  8964. (setq s (funcall org-link-translation-function
  8965. (match-string 1 s) (match-string 2 s)))
  8966. (concat (car s) ":" (cdr s)))
  8967. s))
  8968. (defun org-translate-link-from-planner (type path)
  8969. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8970. This is still an experimental function, your mileage may vary."
  8971. (cond
  8972. ((member type '("http" "https" "news" "ftp"))
  8973. ;; standard Internet links are the same.
  8974. nil)
  8975. ((and (equal type "irc") (string-match "^//" path))
  8976. ;; Planner has two / at the beginning of an irc link, we have 1.
  8977. ;; We should have zero, actually....
  8978. (setq path (substring path 1)))
  8979. ((and (equal type "lisp") (string-match "^/" path))
  8980. ;; Planner has a slash, we do not.
  8981. (setq type "elisp" path (substring path 1)))
  8982. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8983. ;; A typical message link. Planner has the id after the final slash,
  8984. ;; we separate it with a hash mark
  8985. (setq path (concat (match-string 1 path) "#"
  8986. (org-remove-angle-brackets (match-string 2 path)))))
  8987. )
  8988. (cons type path))
  8989. (defun org-find-file-at-mouse (ev)
  8990. "Open file link or URL at mouse."
  8991. (interactive "e")
  8992. (mouse-set-point ev)
  8993. (org-open-at-point 'in-emacs))
  8994. (defun org-open-at-mouse (ev)
  8995. "Open file link or URL at mouse.
  8996. See the docstring of `org-open-file' for details."
  8997. (interactive "e")
  8998. (mouse-set-point ev)
  8999. (if (eq major-mode 'org-agenda-mode)
  9000. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9001. (org-open-at-point))
  9002. (defvar org-window-config-before-follow-link nil
  9003. "The window configuration before following a link.
  9004. This is saved in case the need arises to restore it.")
  9005. (defvar org-open-link-marker (make-marker)
  9006. "Marker pointing to the location where `org-open-at-point; was called.")
  9007. ;;;###autoload
  9008. (defun org-open-at-point-global ()
  9009. "Follow a link like Org-mode does.
  9010. This command can be called in any mode to follow a link that has
  9011. Org-mode syntax."
  9012. (interactive)
  9013. (org-run-like-in-org-mode 'org-open-at-point))
  9014. ;;;###autoload
  9015. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9016. "Open a link in the string S, as if it was in Org-mode."
  9017. (interactive "sLink: \nP")
  9018. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9019. (with-temp-buffer
  9020. (let ((org-inhibit-startup (not reference-buffer)))
  9021. (org-mode)
  9022. (insert s)
  9023. (goto-char (point-min))
  9024. (when reference-buffer
  9025. (setq org-link-abbrev-alist-local
  9026. (with-current-buffer reference-buffer
  9027. org-link-abbrev-alist-local)))
  9028. (org-open-at-point arg reference-buffer)))))
  9029. (defvar org-open-at-point-functions nil
  9030. "Hook that is run when following a link at point.
  9031. Functions in this hook must return t if they identify and follow
  9032. a link at point. If they don't find anything interesting at point,
  9033. they must return nil.")
  9034. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9035. (defun org-open-at-point (&optional arg reference-buffer)
  9036. "Open link at or after point.
  9037. If there is no link at point, this function will search forward up to
  9038. the end of the current line.
  9039. Normally, files will be opened by an appropriate application. If the
  9040. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9041. With a double prefix argument, try to open outside of Emacs, in the
  9042. application the system uses for this file type."
  9043. (interactive "P")
  9044. ;; if in a code block, then open the block's results
  9045. (unless (call-interactively #'org-babel-open-src-block-result)
  9046. (org-load-modules-maybe)
  9047. (move-marker org-open-link-marker (point))
  9048. (setq org-window-config-before-follow-link (current-window-configuration))
  9049. (org-remove-occur-highlights nil nil t)
  9050. (cond
  9051. ((and (org-at-heading-p)
  9052. (not (org-at-timestamp-p t))
  9053. (not (org-in-regexp
  9054. (concat org-plain-link-re "\\|"
  9055. org-bracket-link-regexp "\\|"
  9056. org-angle-link-re "\\|"
  9057. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9058. (not (get-text-property (point) 'org-linked-text)))
  9059. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9060. (lk0 (car lkall))
  9061. (lk (if (stringp lk0) (list lk0) lk0))
  9062. (lkend (cdr lkall)))
  9063. (mapcar (lambda(l)
  9064. (search-forward l nil lkend)
  9065. (goto-char (match-beginning 0))
  9066. (org-open-at-point))
  9067. lk))
  9068. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9069. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9070. ((and (org-at-timestamp-p t)
  9071. (not (org-in-regexp org-bracket-link-regexp)))
  9072. (org-follow-timestamp-link))
  9073. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9074. (not (org-in-regexp org-any-link-re)))
  9075. (org-footnote-action))
  9076. (t
  9077. (let (type path link line search (pos (point)))
  9078. (catch 'match
  9079. (save-excursion
  9080. (skip-chars-forward "^]\n\r")
  9081. (when (org-in-regexp org-bracket-link-regexp 1)
  9082. (setq link (org-extract-attributes
  9083. (org-link-unescape (org-match-string-no-properties 1))))
  9084. (while (string-match " *\n *" link)
  9085. (setq link (replace-match " " t t link)))
  9086. (setq link (org-link-expand-abbrev link))
  9087. (cond
  9088. ((or (file-name-absolute-p link)
  9089. (string-match "^\\.\\.?/" link))
  9090. (setq type "file" path link))
  9091. ((string-match org-link-re-with-space3 link)
  9092. (setq type (match-string 1 link) path (match-string 2 link)))
  9093. ((string-match "^help:+\\(.+\\)" link)
  9094. (setq type "help" path (match-string 1 link)))
  9095. (t (setq type "thisfile" path link)))
  9096. (throw 'match t)))
  9097. (when (get-text-property (point) 'org-linked-text)
  9098. (setq type "thisfile"
  9099. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9100. (1+ (point)) (point))
  9101. path (buffer-substring
  9102. (or (previous-single-property-change pos 'org-linked-text)
  9103. (point-min))
  9104. (or (next-single-property-change pos 'org-linked-text)
  9105. (point-max))))
  9106. (throw 'match t))
  9107. (save-excursion
  9108. (when (or (org-in-regexp org-angle-link-re)
  9109. (let ((match (org-in-regexp org-plain-link-re)))
  9110. ;; Check a plain link is not within a bracket link
  9111. (and match
  9112. (save-excursion
  9113. (progn
  9114. (goto-char (car match))
  9115. (not (org-in-regexp org-bracket-link-regexp))))))
  9116. (let ((line_ending (save-excursion (end-of-line) (point))))
  9117. ;; We are in a line before a plain or bracket link
  9118. (or (re-search-forward org-plain-link-re line_ending t)
  9119. (re-search-forward org-bracket-link-regexp line_ending t))))
  9120. (setq type (match-string 1)
  9121. path (org-link-unescape (match-string 2)))
  9122. (throw 'match t)))
  9123. (save-excursion
  9124. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9125. (setq type "tags"
  9126. path (match-string 1))
  9127. (while (string-match ":" path)
  9128. (setq path (replace-match "+" t t path)))
  9129. (throw 'match t)))
  9130. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9131. (setq type "tree-match"
  9132. path (match-string 1))
  9133. (throw 'match t)))
  9134. (unless path
  9135. (user-error "No link found"))
  9136. ;; switch back to reference buffer
  9137. ;; needed when if called in a temporary buffer through
  9138. ;; org-open-link-from-string
  9139. (with-current-buffer (or reference-buffer (current-buffer))
  9140. ;; Remove any trailing spaces in path
  9141. (if (string-match " +\\'" path)
  9142. (setq path (replace-match "" t t path)))
  9143. (if (and org-link-translation-function
  9144. (fboundp org-link-translation-function))
  9145. ;; Check if we need to translate the link
  9146. (let ((tmp (funcall org-link-translation-function type path)))
  9147. (setq type (car tmp) path (cdr tmp))))
  9148. (cond
  9149. ((assoc type org-link-protocols)
  9150. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9151. ((equal type "help")
  9152. (let ((f-or-v (intern path)))
  9153. (cond ((fboundp f-or-v)
  9154. (describe-function f-or-v))
  9155. ((boundp f-or-v)
  9156. (describe-variable f-or-v))
  9157. (t (error "Not a known function or variable")))))
  9158. ((equal type "mailto")
  9159. (let ((cmd (car org-link-mailto-program))
  9160. (args (cdr org-link-mailto-program)) args1
  9161. (address path) (subject "") a)
  9162. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9163. (setq address (match-string 1 path)
  9164. subject (org-link-escape (match-string 2 path))))
  9165. (while args
  9166. (cond
  9167. ((not (stringp (car args))) (push (pop args) args1))
  9168. (t (setq a (pop args))
  9169. (if (string-match "%a" a)
  9170. (setq a (replace-match address t t a)))
  9171. (if (string-match "%s" a)
  9172. (setq a (replace-match subject t t a)))
  9173. (push a args1))))
  9174. (apply cmd (nreverse args1))))
  9175. ((member type '("http" "https" "ftp" "news"))
  9176. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9177. (org-link-escape
  9178. path org-link-escape-chars-browser)
  9179. path))))
  9180. ((string= type "doi")
  9181. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9182. (org-link-escape
  9183. path org-link-escape-chars-browser)
  9184. path))))
  9185. ((member type '("message"))
  9186. (browse-url (concat type ":" path)))
  9187. ((string= type "tags")
  9188. (org-tags-view arg path))
  9189. ((string= type "tree-match")
  9190. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9191. ((string= type "file")
  9192. (if (string-match "::\\([0-9]+\\)\\'" path)
  9193. (setq line (string-to-number (match-string 1 path))
  9194. path (substring path 0 (match-beginning 0)))
  9195. (if (string-match "::\\(.+\\)\\'" path)
  9196. (setq search (match-string 1 path)
  9197. path (substring path 0 (match-beginning 0)))))
  9198. (if (string-match "[*?{]" (file-name-nondirectory path))
  9199. (dired path)
  9200. (org-open-file path arg line search)))
  9201. ((string= type "shell")
  9202. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9203. (cmd path))
  9204. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9205. (string-match org-confirm-shell-link-not-regexp cmd))
  9206. (not org-confirm-shell-link-function)
  9207. (funcall org-confirm-shell-link-function
  9208. (format "Execute \"%s\" in shell? "
  9209. (org-add-props cmd nil
  9210. 'face 'org-warning))))
  9211. (progn
  9212. (message "Executing %s" cmd)
  9213. (shell-command cmd buf)
  9214. (if (featurep 'midnight)
  9215. (setq clean-buffer-list-kill-buffer-names
  9216. (cons buf clean-buffer-list-kill-buffer-names))))
  9217. (error "Abort"))))
  9218. ((string= type "elisp")
  9219. (let ((cmd path))
  9220. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9221. (string-match org-confirm-elisp-link-not-regexp cmd))
  9222. (not org-confirm-elisp-link-function)
  9223. (funcall org-confirm-elisp-link-function
  9224. (format "Execute \"%s\" as elisp? "
  9225. (org-add-props cmd nil
  9226. 'face 'org-warning))))
  9227. (message "%s => %s" cmd
  9228. (if (equal (string-to-char cmd) ?\()
  9229. (eval (read cmd))
  9230. (call-interactively (read cmd))))
  9231. (error "Abort"))))
  9232. ((and (string= type "thisfile")
  9233. (run-hook-with-args-until-success
  9234. 'org-open-link-functions path)))
  9235. ((string= type "thisfile")
  9236. (if arg
  9237. (switch-to-buffer-other-window
  9238. (org-get-buffer-for-internal-link (current-buffer)))
  9239. (org-mark-ring-push))
  9240. (let ((cmd `(org-link-search
  9241. ,path
  9242. ,(cond ((equal arg '(4)) ''occur)
  9243. ((equal arg '(16)) ''org-occur))
  9244. ,pos)))
  9245. (condition-case nil (let ((org-link-search-inhibit-query t))
  9246. (eval cmd))
  9247. (error (progn (widen) (eval cmd))))))
  9248. (t (browse-url-at-point)))))))
  9249. (move-marker org-open-link-marker nil)
  9250. (run-hook-with-args 'org-follow-link-hook)))
  9251. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9252. "Offer links in the current entry and return the selected link.
  9253. If there is only one link, return it.
  9254. If NTH is an integer, return the NTH link found.
  9255. If ZERO is a string, check also this string for a link, and if
  9256. there is one, return it."
  9257. (with-current-buffer buffer
  9258. (save-excursion
  9259. (save-restriction
  9260. (widen)
  9261. (goto-char marker)
  9262. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9263. "\\(" org-angle-link-re "\\)\\|"
  9264. "\\(" org-plain-link-re "\\)"))
  9265. (cnt ?0)
  9266. (in-emacs (if (integerp nth) nil nth))
  9267. have-zero end links link c)
  9268. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9269. (push (match-string 0 zero) links)
  9270. (setq cnt (1- cnt) have-zero t))
  9271. (save-excursion
  9272. (org-back-to-heading t)
  9273. (setq end (save-excursion (outline-next-heading) (point)))
  9274. (while (re-search-forward re end t)
  9275. (push (match-string 0) links))
  9276. (setq links (org-uniquify (reverse links))))
  9277. (cond
  9278. ((null links)
  9279. (message "No links"))
  9280. ((equal (length links) 1)
  9281. (setq link (car links)))
  9282. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9283. (setq link (nth (if have-zero nth (1- nth)) links)))
  9284. (t ; we have to select a link
  9285. (save-excursion
  9286. (save-window-excursion
  9287. (delete-other-windows)
  9288. (with-output-to-temp-buffer "*Select Link*"
  9289. (mapc (lambda (l)
  9290. (if (not (string-match org-bracket-link-regexp l))
  9291. (princ (format "[%c] %s\n" (incf cnt)
  9292. (org-remove-angle-brackets l)))
  9293. (if (match-end 3)
  9294. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9295. (match-string 3 l) (match-string 1 l)))
  9296. (princ (format "[%c] %s\n" (incf cnt)
  9297. (match-string 1 l))))))
  9298. links))
  9299. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9300. (message "Select link to open, RET to open all:")
  9301. (setq c (read-char-exclusive))
  9302. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9303. (when (equal c ?q) (error "Abort"))
  9304. (if (equal c ?\C-m)
  9305. (setq link links)
  9306. (setq nth (- c ?0))
  9307. (if have-zero (setq nth (1+ nth)))
  9308. (unless (and (integerp nth) (>= (length links) nth))
  9309. (error "Invalid link selection"))
  9310. (setq link (nth (1- nth) links)))))
  9311. (cons link end))))))
  9312. ;; Add special file links that specify the way of opening
  9313. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9314. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9315. (defun org-open-file-with-system (path)
  9316. "Open file at PATH using the system way of opening it."
  9317. (org-open-file path 'system))
  9318. (defun org-open-file-with-emacs (path)
  9319. "Open file at PATH in Emacs."
  9320. (org-open-file path 'emacs))
  9321. ;;; File search
  9322. (defvar org-create-file-search-functions nil
  9323. "List of functions to construct the right search string for a file link.
  9324. These functions are called in turn with point at the location to
  9325. which the link should point.
  9326. A function in the hook should first test if it would like to
  9327. handle this file type, for example by checking the `major-mode'
  9328. or the file extension. If it decides not to handle this file, it
  9329. should just return nil to give other functions a chance. If it
  9330. does handle the file, it must return the search string to be used
  9331. when following the link. The search string will be part of the
  9332. file link, given after a double colon, and `org-open-at-point'
  9333. will automatically search for it. If special measures must be
  9334. taken to make the search successful, another function should be
  9335. added to the companion hook `org-execute-file-search-functions',
  9336. which see.
  9337. A function in this hook may also use `setq' to set the variable
  9338. `description' to provide a suggestion for the descriptive text to
  9339. be used for this link when it gets inserted into an Org-mode
  9340. buffer with \\[org-insert-link].")
  9341. (defvar org-execute-file-search-functions nil
  9342. "List of functions to execute a file search triggered by a link.
  9343. Functions added to this hook must accept a single argument, the
  9344. search string that was part of the file link, the part after the
  9345. double colon. The function must first check if it would like to
  9346. handle this search, for example by checking the `major-mode' or
  9347. the file extension. If it decides not to handle this search, it
  9348. should just return nil to give other functions a chance. If it
  9349. does handle the search, it must return a non-nil value to keep
  9350. other functions from trying.
  9351. Each function can access the current prefix argument through the
  9352. variable `current-prefix-argument'. Note that a single prefix is
  9353. used to force opening a link in Emacs, so it may be good to only
  9354. use a numeric or double prefix to guide the search function.
  9355. In case this is needed, a function in this hook can also restore
  9356. the window configuration before `org-open-at-point' was called using:
  9357. (set-window-configuration org-window-config-before-follow-link)")
  9358. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9359. (defun org-link-search (s &optional type avoid-pos stealth)
  9360. "Search for a link search option.
  9361. If S is surrounded by forward slashes, it is interpreted as a
  9362. regular expression. In org-mode files, this will create an `org-occur'
  9363. sparse tree. In ordinary files, `occur' will be used to list matches.
  9364. If the current buffer is in `dired-mode', grep will be used to search
  9365. in all files. If AVOID-POS is given, ignore matches near that position.
  9366. When optional argument STEALTH is non-nil, do not modify
  9367. visibility around point, thus ignoring
  9368. `org-show-hierarchy-above', `org-show-following-heading' and
  9369. `org-show-siblings' variables."
  9370. (let ((case-fold-search t)
  9371. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9372. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9373. (append '(("") (" ") ("\t") ("\n"))
  9374. org-emphasis-alist)
  9375. "\\|") "\\)"))
  9376. (pos (point))
  9377. (pre nil) (post nil)
  9378. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9379. (cond
  9380. ;; First check if there are any special search functions
  9381. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9382. ;; Now try the builtin stuff
  9383. ((and (equal (string-to-char s0) ?#)
  9384. (> (length s0) 1)
  9385. (save-excursion
  9386. (goto-char (point-min))
  9387. (and
  9388. (re-search-forward
  9389. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9390. (setq type 'dedicated
  9391. pos (match-beginning 0))))
  9392. ;; There is an exact target for this
  9393. (goto-char pos)
  9394. (org-back-to-heading t)))
  9395. ((save-excursion
  9396. (goto-char (point-min))
  9397. (and
  9398. (re-search-forward
  9399. (concat "<<" (regexp-quote s0) ">>") nil t)
  9400. (setq type 'dedicated
  9401. pos (match-beginning 0))))
  9402. ;; There is an exact target for this
  9403. (goto-char pos))
  9404. ((save-excursion
  9405. (goto-char (point-min))
  9406. (and
  9407. (re-search-forward
  9408. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9409. (setq type 'dedicated pos (match-beginning 0))))
  9410. ;; Found an invisible target.
  9411. (goto-char pos))
  9412. ((save-excursion
  9413. (goto-char (point-min))
  9414. (and
  9415. (re-search-forward
  9416. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9417. (setq type 'dedicated pos (match-beginning 0))))
  9418. ;; Found an element with a matching #+name affiliated keyword.
  9419. (goto-char pos))
  9420. ((and (string-match "^(\\(.*\\))$" s0)
  9421. (save-excursion
  9422. (goto-char (point-min))
  9423. (and
  9424. (re-search-forward
  9425. (concat "[^[]" (regexp-quote
  9426. (format org-coderef-label-format
  9427. (match-string 1 s0))))
  9428. nil t)
  9429. (setq type 'dedicated
  9430. pos (1+ (match-beginning 0))))))
  9431. ;; There is a coderef target for this
  9432. (goto-char pos))
  9433. ((string-match "^/\\(.*\\)/$" s)
  9434. ;; A regular expression
  9435. (cond
  9436. ((derived-mode-p 'org-mode)
  9437. (org-occur (match-string 1 s)))
  9438. (t (org-do-occur (match-string 1 s)))))
  9439. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9440. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9441. (goto-char (point-min))
  9442. (cond
  9443. ((let (case-fold-search)
  9444. (re-search-forward (format org-complex-heading-regexp-format
  9445. (regexp-quote s))
  9446. nil t))
  9447. ;; OK, found a match
  9448. (setq type 'dedicated)
  9449. (goto-char (match-beginning 0)))
  9450. ((and (not org-link-search-inhibit-query)
  9451. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9452. (y-or-n-p "No match - create this as a new heading? "))
  9453. (goto-char (point-max))
  9454. (or (bolp) (newline))
  9455. (insert "* " s "\n")
  9456. (beginning-of-line 0))
  9457. (t
  9458. (goto-char pos)
  9459. (error "No match"))))
  9460. (t
  9461. ;; A normal search string
  9462. (when (equal (string-to-char s) ?*)
  9463. ;; Anchor on headlines, post may include tags.
  9464. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9465. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9466. s (substring s 1)))
  9467. (remove-text-properties
  9468. 0 (length s)
  9469. '(face nil mouse-face nil keymap nil fontified nil) s)
  9470. ;; Make a series of regular expressions to find a match
  9471. (setq words (org-split-string s "[ \n\r\t]+")
  9472. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9473. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9474. "\\)" markers)
  9475. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9476. re2a (concat "[ \t\r\n]" re2a_)
  9477. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9478. re4 (concat "[^a-zA-Z_]" re4_)
  9479. re1 (concat pre re2 post)
  9480. re3 (concat pre (if pre re4_ re4) post)
  9481. re5 (concat pre ".*" re4)
  9482. re2 (concat pre re2)
  9483. re2a (concat pre (if pre re2a_ re2a))
  9484. re4 (concat pre (if pre re4_ re4))
  9485. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9486. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9487. re5 "\\)"
  9488. ))
  9489. (cond
  9490. ((eq type 'org-occur) (org-occur reall))
  9491. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9492. (t (goto-char (point-min))
  9493. (setq type 'fuzzy)
  9494. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9495. (org-search-not-self 1 re1 nil t)
  9496. (org-search-not-self 1 re2 nil t)
  9497. (org-search-not-self 1 re2a nil t)
  9498. (org-search-not-self 1 re3 nil t)
  9499. (org-search-not-self 1 re4 nil t)
  9500. (org-search-not-self 1 re5 nil t)
  9501. )
  9502. (goto-char (match-beginning 1))
  9503. (goto-char pos)
  9504. (error "No match"))))))
  9505. (and (derived-mode-p 'org-mode)
  9506. (not stealth)
  9507. (org-show-context 'link-search))
  9508. type))
  9509. (defun org-search-not-self (group &rest args)
  9510. "Execute `re-search-forward', but only accept matches that do not
  9511. enclose the position of `org-open-link-marker'."
  9512. (let ((m org-open-link-marker))
  9513. (catch 'exit
  9514. (while (apply 're-search-forward args)
  9515. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9516. (goto-char (match-end group))
  9517. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9518. (> (match-beginning 0) (marker-position m))
  9519. (< (match-end 0) (marker-position m)))
  9520. (save-match-data
  9521. (or (not (org-in-regexp
  9522. org-bracket-link-analytic-regexp 1))
  9523. (not (match-end 4)) ; no description
  9524. (and (<= (match-beginning 4) (point))
  9525. (>= (match-end 4) (point))))))
  9526. (throw 'exit (point))))))))
  9527. (defun org-get-buffer-for-internal-link (buffer)
  9528. "Return a buffer to be used for displaying the link target of internal links."
  9529. (cond
  9530. ((not org-display-internal-link-with-indirect-buffer)
  9531. buffer)
  9532. ((string-match "(Clone)$" (buffer-name buffer))
  9533. (message "Buffer is already a clone, not making another one")
  9534. ;; we also do not modify visibility in this case
  9535. buffer)
  9536. (t ; make a new indirect buffer for displaying the link
  9537. (let* ((bn (buffer-name buffer))
  9538. (ibn (concat bn "(Clone)"))
  9539. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9540. (with-current-buffer ib (org-overview))
  9541. ib))))
  9542. (defun org-do-occur (regexp &optional cleanup)
  9543. "Call the Emacs command `occur'.
  9544. If CLEANUP is non-nil, remove the printout of the regular expression
  9545. in the *Occur* buffer. This is useful if the regex is long and not useful
  9546. to read."
  9547. (occur regexp)
  9548. (when cleanup
  9549. (let ((cwin (selected-window)) win beg end)
  9550. (when (setq win (get-buffer-window "*Occur*"))
  9551. (select-window win))
  9552. (goto-char (point-min))
  9553. (when (re-search-forward "match[a-z]+" nil t)
  9554. (setq beg (match-end 0))
  9555. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9556. (setq end (1- (match-beginning 0)))))
  9557. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9558. (goto-char (point-min))
  9559. (select-window cwin))))
  9560. ;;; The mark ring for links jumps
  9561. (defvar org-mark-ring nil
  9562. "Mark ring for positions before jumps in Org-mode.")
  9563. (defvar org-mark-ring-last-goto nil
  9564. "Last position in the mark ring used to go back.")
  9565. ;; Fill and close the ring
  9566. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9567. (loop for i from 1 to org-mark-ring-length do
  9568. (push (make-marker) org-mark-ring))
  9569. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9570. org-mark-ring)
  9571. (defun org-mark-ring-push (&optional pos buffer)
  9572. "Put the current position or POS into the mark ring and rotate it."
  9573. (interactive)
  9574. (setq pos (or pos (point)))
  9575. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9576. (move-marker (car org-mark-ring)
  9577. (or pos (point))
  9578. (or buffer (current-buffer)))
  9579. (message "%s"
  9580. (substitute-command-keys
  9581. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9582. (defun org-mark-ring-goto (&optional n)
  9583. "Jump to the previous position in the mark ring.
  9584. With prefix arg N, jump back that many stored positions. When
  9585. called several times in succession, walk through the entire ring.
  9586. Org-mode commands jumping to a different position in the current file,
  9587. or to another Org-mode file, automatically push the old position
  9588. onto the ring."
  9589. (interactive "p")
  9590. (let (p m)
  9591. (if (eq last-command this-command)
  9592. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9593. (setq p org-mark-ring))
  9594. (setq org-mark-ring-last-goto p)
  9595. (setq m (car p))
  9596. (org-pop-to-buffer-same-window (marker-buffer m))
  9597. (goto-char m)
  9598. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9599. (defun org-remove-angle-brackets (s)
  9600. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9601. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9602. s)
  9603. (defun org-add-angle-brackets (s)
  9604. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9605. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9606. s)
  9607. (defun org-remove-double-quotes (s)
  9608. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9609. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9610. s)
  9611. ;;; Following specific links
  9612. (defun org-follow-timestamp-link ()
  9613. "Open an agenda view for the time-stamp date/range at point."
  9614. (cond
  9615. ((org-at-date-range-p t)
  9616. (let ((org-agenda-start-on-weekday)
  9617. (t1 (match-string 1))
  9618. (t2 (match-string 2)) tt1 tt2)
  9619. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9620. tt2 (time-to-days (org-time-string-to-time t2)))
  9621. (let ((org-agenda-buffer-tmp-name
  9622. (format "*Org Agenda(a:%s)"
  9623. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9624. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9625. ((org-at-timestamp-p t)
  9626. (let ((org-agenda-buffer-tmp-name
  9627. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9628. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9629. (substring (match-string 1) 0 10)))
  9630. 1)))
  9631. (t (error "This should not happen"))))
  9632. ;;; Following file links
  9633. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9634. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9635. (declare-function mailcap-mime-info
  9636. "mailcap" (string &optional request no-decode))
  9637. (defvar org-wait nil)
  9638. (defun org-open-file (path &optional in-emacs line search)
  9639. "Open the file at PATH.
  9640. First, this expands any special file name abbreviations. Then the
  9641. configuration variable `org-file-apps' is checked if it contains an
  9642. entry for this file type, and if yes, the corresponding command is launched.
  9643. If no application is found, Emacs simply visits the file.
  9644. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9645. With a double \\[universal-argument] \\[universal-argument] \
  9646. prefix arg, Org tries to avoid opening in Emacs
  9647. and to use an external application to visit the file.
  9648. Optional LINE specifies a line to go to, optional SEARCH a string
  9649. to search for. If LINE or SEARCH is given, the file will be
  9650. opened in Emacs, unless an entry from org-file-apps that makes
  9651. use of groups in a regexp matches.
  9652. If you want to change the way frames are used when following a
  9653. link, please customize `org-link-frame-setup'.
  9654. If the file does not exist, an error is thrown."
  9655. (let* ((file (if (equal path "")
  9656. buffer-file-name
  9657. (substitute-in-file-name (expand-file-name path))))
  9658. (file-apps (append org-file-apps (org-default-apps)))
  9659. (apps (org-remove-if
  9660. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9661. (apps-dlink (org-remove-if-not
  9662. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9663. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9664. (dirp (if remp nil (file-directory-p file)))
  9665. (file (if (and dirp org-open-directory-means-index-dot-org)
  9666. (concat (file-name-as-directory file) "index.org")
  9667. file))
  9668. (a-m-a-p (assq 'auto-mode apps))
  9669. (dfile (downcase file))
  9670. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9671. (link (cond ((and (eq line nil)
  9672. (eq search nil))
  9673. file)
  9674. (line
  9675. (concat file "::" (number-to-string line)))
  9676. (search
  9677. (concat file "::" search))))
  9678. (dlink (downcase link))
  9679. (old-buffer (current-buffer))
  9680. (old-pos (point))
  9681. (old-mode major-mode)
  9682. ext cmd link-match-data)
  9683. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9684. (setq ext (match-string 1 dfile))
  9685. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9686. (setq ext (match-string 1 dfile))))
  9687. (cond
  9688. ((member in-emacs '((16) system))
  9689. (setq cmd (cdr (assoc 'system apps))))
  9690. (in-emacs (setq cmd 'emacs))
  9691. (t
  9692. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9693. (and dirp (cdr (assoc 'directory apps)))
  9694. ; first, try matching against apps-dlink
  9695. ; if we get a match here, store the match data for later
  9696. (let ((match (assoc-default dlink apps-dlink
  9697. 'string-match)))
  9698. (if match
  9699. (progn (setq link-match-data (match-data))
  9700. match)
  9701. (progn (setq in-emacs (or in-emacs line search))
  9702. nil))) ; if we have no match in apps-dlink,
  9703. ; always open the file in emacs if line or search
  9704. ; is given (for backwards compatibility)
  9705. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9706. 'string-match)
  9707. (cdr (assoc ext apps))
  9708. (cdr (assoc t apps))))))
  9709. (when (eq cmd 'system)
  9710. (setq cmd (cdr (assoc 'system apps))))
  9711. (when (eq cmd 'default)
  9712. (setq cmd (cdr (assoc t apps))))
  9713. (when (eq cmd 'mailcap)
  9714. (require 'mailcap)
  9715. (mailcap-parse-mailcaps)
  9716. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9717. (command (mailcap-mime-info mime-type)))
  9718. (if (stringp command)
  9719. (setq cmd command)
  9720. (setq cmd 'emacs))))
  9721. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9722. (not (file-exists-p file))
  9723. (not org-open-non-existing-files))
  9724. (error "No such file: %s" file))
  9725. (cond
  9726. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9727. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9728. (while (string-match "['\"]%s['\"]" cmd)
  9729. (setq cmd (replace-match "%s" t t cmd)))
  9730. (while (string-match "%s" cmd)
  9731. (setq cmd (replace-match
  9732. (save-match-data
  9733. (shell-quote-argument
  9734. (convert-standard-filename file)))
  9735. t t cmd)))
  9736. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9737. (save-match-data
  9738. (let ((match-index 1)
  9739. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9740. (set-match-data link-match-data)
  9741. (while (<= match-index number-of-groups)
  9742. (let ((regex (concat "%" (number-to-string match-index)))
  9743. (replace-with (match-string match-index dlink)))
  9744. (while (string-match regex cmd)
  9745. (setq cmd (replace-match replace-with t t cmd))))
  9746. (setq match-index (+ match-index 1)))))
  9747. (save-window-excursion
  9748. (message "Running %s...done" cmd)
  9749. (start-process-shell-command cmd nil cmd)
  9750. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9751. ((or (stringp cmd)
  9752. (eq cmd 'emacs))
  9753. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9754. (widen)
  9755. (if line (org-goto-line line)
  9756. (if search (org-link-search search))))
  9757. ((consp cmd)
  9758. (let ((file (convert-standard-filename file)))
  9759. (save-match-data
  9760. (set-match-data link-match-data)
  9761. (eval cmd))))
  9762. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9763. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9764. (or (not (equal old-buffer (current-buffer)))
  9765. (not (equal old-pos (point))))
  9766. (org-mark-ring-push old-pos old-buffer))))
  9767. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9768. "This function returns non-nil if `entry' uses a regular
  9769. expression which should be matched against the whole link by
  9770. org-open-file.
  9771. It assumes that is the case when the entry uses a regular
  9772. expression which has at least one grouping construct and the
  9773. action is either a lisp form or a command string containing
  9774. '%1', i.e. using at least one subexpression match as a
  9775. parameter."
  9776. (let ((selector (car entry))
  9777. (action (cdr entry)))
  9778. (if (stringp selector)
  9779. (and (> (regexp-opt-depth selector) 0)
  9780. (or (and (stringp action)
  9781. (string-match "%[0-9]" action))
  9782. (consp action)))
  9783. nil)))
  9784. (defun org-default-apps ()
  9785. "Return the default applications for this operating system."
  9786. (cond
  9787. ((eq system-type 'darwin)
  9788. org-file-apps-defaults-macosx)
  9789. ((eq system-type 'windows-nt)
  9790. org-file-apps-defaults-windowsnt)
  9791. (t org-file-apps-defaults-gnu)))
  9792. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9793. "Convert extensions to regular expressions in the cars of LIST.
  9794. Also, weed out any non-string entries, because the return value is used
  9795. only for regexp matching.
  9796. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9797. point to the symbol `emacs', indicating that the file should
  9798. be opened in Emacs."
  9799. (append
  9800. (delq nil
  9801. (mapcar (lambda (x)
  9802. (if (not (stringp (car x)))
  9803. nil
  9804. (if (string-match "\\W" (car x))
  9805. x
  9806. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9807. list))
  9808. (if add-auto-mode
  9809. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9810. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9811. (defun org-file-remote-p (file)
  9812. "Test whether FILE specifies a location on a remote system.
  9813. Return non-nil if the location is indeed remote.
  9814. For example, the filename \"/user@host:/foo\" specifies a location
  9815. on the system \"/user@host:\"."
  9816. (cond ((fboundp 'file-remote-p)
  9817. (file-remote-p file))
  9818. ((fboundp 'tramp-handle-file-remote-p)
  9819. (tramp-handle-file-remote-p file))
  9820. ((and (boundp 'ange-ftp-name-format)
  9821. (string-match (car ange-ftp-name-format) file))
  9822. t)))
  9823. ;;;; Refiling
  9824. (defun org-get-org-file ()
  9825. "Read a filename, with default directory `org-directory'."
  9826. (let ((default (or org-default-notes-file remember-data-file)))
  9827. (read-file-name (format "File name [%s]: " default)
  9828. (file-name-as-directory org-directory)
  9829. default)))
  9830. (defun org-notes-order-reversed-p ()
  9831. "Check if the current file should receive notes in reversed order."
  9832. (cond
  9833. ((not org-reverse-note-order) nil)
  9834. ((eq t org-reverse-note-order) t)
  9835. ((not (listp org-reverse-note-order)) nil)
  9836. (t (catch 'exit
  9837. (let ((all org-reverse-note-order)
  9838. entry)
  9839. (while (setq entry (pop all))
  9840. (if (string-match (car entry) buffer-file-name)
  9841. (throw 'exit (cdr entry))))
  9842. nil)))))
  9843. (defvar org-refile-target-table nil
  9844. "The list of refile targets, created by `org-refile'.")
  9845. (defvar org-agenda-new-buffers nil
  9846. "Buffers created to visit agenda files.")
  9847. (defvar org-refile-cache nil
  9848. "Cache for refile targets.")
  9849. (defvar org-refile-markers nil
  9850. "All the markers used for caching refile locations.")
  9851. (defun org-refile-marker (pos)
  9852. "Get a new refile marker, but only if caching is in use."
  9853. (if (not org-refile-use-cache)
  9854. pos
  9855. (let ((m (make-marker)))
  9856. (move-marker m pos)
  9857. (push m org-refile-markers)
  9858. m)))
  9859. (defun org-refile-cache-clear ()
  9860. "Clear the refile cache and disable all the markers."
  9861. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9862. (setq org-refile-markers nil)
  9863. (setq org-refile-cache nil)
  9864. (message "Refile cache has been cleared"))
  9865. (defun org-refile-cache-check-set (set)
  9866. "Check if all the markers in the cache still have live buffers."
  9867. (let (marker)
  9868. (catch 'exit
  9869. (while (and set (setq marker (nth 3 (pop set))))
  9870. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9871. (when (and marker (null (marker-buffer marker)))
  9872. (message "not found") (sit-for 3)
  9873. (throw 'exit nil)))
  9874. t)))
  9875. (defun org-refile-cache-put (set &rest identifiers)
  9876. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9877. (let* ((key (sha1 (prin1-to-string identifiers)))
  9878. (entry (assoc key org-refile-cache)))
  9879. (if entry
  9880. (setcdr entry set)
  9881. (push (cons key set) org-refile-cache))))
  9882. (defun org-refile-cache-get (&rest identifiers)
  9883. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9884. (cond
  9885. ((not org-refile-cache) nil)
  9886. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9887. (t
  9888. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9889. org-refile-cache))))
  9890. (and set (org-refile-cache-check-set set) set)))))
  9891. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9892. "Produce a table with refile targets."
  9893. (let ((case-fold-search nil)
  9894. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9895. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9896. targets tgs txt re files f desc descre fast-path-p level pos0)
  9897. (message "Getting targets...")
  9898. (with-current-buffer (or default-buffer (current-buffer))
  9899. (while (setq entry (pop entries))
  9900. (setq files (car entry) desc (cdr entry))
  9901. (setq fast-path-p nil)
  9902. (cond
  9903. ((null files) (setq files (list (current-buffer))))
  9904. ((eq files 'org-agenda-files)
  9905. (setq files (org-agenda-files 'unrestricted)))
  9906. ((and (symbolp files) (fboundp files))
  9907. (setq files (funcall files)))
  9908. ((and (symbolp files) (boundp files))
  9909. (setq files (symbol-value files))))
  9910. (if (stringp files) (setq files (list files)))
  9911. (cond
  9912. ((eq (car desc) :tag)
  9913. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9914. ((eq (car desc) :todo)
  9915. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9916. ((eq (car desc) :regexp)
  9917. (setq descre (cdr desc)))
  9918. ((eq (car desc) :level)
  9919. (setq descre (concat "^\\*\\{" (number-to-string
  9920. (if org-odd-levels-only
  9921. (1- (* 2 (cdr desc)))
  9922. (cdr desc)))
  9923. "\\}[ \t]")))
  9924. ((eq (car desc) :maxlevel)
  9925. (setq fast-path-p t)
  9926. (setq descre (concat "^\\*\\{1," (number-to-string
  9927. (if org-odd-levels-only
  9928. (1- (* 2 (cdr desc)))
  9929. (cdr desc)))
  9930. "\\}[ \t]")))
  9931. (t (error "Bad refiling target description %s" desc)))
  9932. (while (setq f (pop files))
  9933. (with-current-buffer
  9934. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9935. (or
  9936. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9937. (progn
  9938. (if (bufferp f) (setq f (buffer-file-name
  9939. (buffer-base-buffer f))))
  9940. (setq f (and f (expand-file-name f)))
  9941. (if (eq org-refile-use-outline-path 'file)
  9942. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9943. (save-excursion
  9944. (save-restriction
  9945. (widen)
  9946. (goto-char (point-min))
  9947. (while (re-search-forward descre nil t)
  9948. (goto-char (setq pos0 (point-at-bol)))
  9949. (catch 'next
  9950. (when org-refile-target-verify-function
  9951. (save-match-data
  9952. (or (funcall org-refile-target-verify-function)
  9953. (throw 'next t))))
  9954. (when (and (looking-at org-complex-heading-regexp)
  9955. (not (member (match-string 4) excluded-entries))
  9956. (match-string 4))
  9957. (setq level (org-reduced-level
  9958. (- (match-end 1) (match-beginning 1)))
  9959. txt (org-link-display-format (match-string 4))
  9960. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9961. re (format org-complex-heading-regexp-format
  9962. (regexp-quote (match-string 4))))
  9963. (when org-refile-use-outline-path
  9964. (setq txt (mapconcat
  9965. 'org-protect-slash
  9966. (append
  9967. (if (eq org-refile-use-outline-path
  9968. 'file)
  9969. (list (file-name-nondirectory
  9970. (buffer-file-name
  9971. (buffer-base-buffer))))
  9972. (if (eq org-refile-use-outline-path
  9973. 'full-file-path)
  9974. (list (buffer-file-name
  9975. (buffer-base-buffer)))))
  9976. (org-get-outline-path fast-path-p
  9977. level txt)
  9978. (list txt))
  9979. "/")))
  9980. (push (list txt f re (org-refile-marker (point)))
  9981. tgs)))
  9982. (when (= (point) pos0)
  9983. ;; verification function has not moved point
  9984. (goto-char (point-at-eol))))))))
  9985. (when org-refile-use-cache
  9986. (org-refile-cache-put tgs (buffer-file-name) descre))
  9987. (setq targets (append tgs targets))
  9988. ))))
  9989. (message "Getting targets...done")
  9990. (nreverse targets)))
  9991. (defun org-protect-slash (s)
  9992. (while (string-match "/" s)
  9993. (setq s (replace-match "\\" t t s)))
  9994. s)
  9995. (defvar org-olpa (make-vector 20 nil))
  9996. (defun org-get-outline-path (&optional fastp level heading)
  9997. "Return the outline path to the current entry, as a list.
  9998. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9999. routine which makes outline path derivations for an entire file,
  10000. avoiding backtracing. Refile target collection makes use of that."
  10001. (if fastp
  10002. (progn
  10003. (if (> level 19)
  10004. (error "Outline path failure, more than 19 levels"))
  10005. (loop for i from level upto 19 do
  10006. (aset org-olpa i nil))
  10007. (prog1
  10008. (delq nil (append org-olpa nil))
  10009. (aset org-olpa level heading)))
  10010. (let (rtn case-fold-search)
  10011. (save-excursion
  10012. (save-restriction
  10013. (widen)
  10014. (while (org-up-heading-safe)
  10015. (when (looking-at org-complex-heading-regexp)
  10016. (push (org-match-string-no-properties 4) rtn)))
  10017. rtn)))))
  10018. (defun org-format-outline-path (path &optional width prefix separator)
  10019. "Format the outline path PATH for display.
  10020. WIDTH is the maximum number of characters that is available.
  10021. PREFIX is a prefix to be included in the returned string,
  10022. such as the file name.
  10023. SEPARATOR is inserted between the different parts of the path,
  10024. the default is \"/\"."
  10025. (setq width (or width 79))
  10026. (if prefix (setq width (- width (length prefix))))
  10027. (if (not path)
  10028. (or prefix "")
  10029. (let* ((nsteps (length path))
  10030. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10031. (maxwidth (if (<= total-width width)
  10032. 10000 ;; everything fits
  10033. ;; we need to shorten the level headings
  10034. (/ (- width nsteps) nsteps)))
  10035. (org-odd-levels-only nil)
  10036. (n 0)
  10037. (total (1+ (length prefix))))
  10038. (setq maxwidth (max maxwidth 10))
  10039. (concat prefix
  10040. (if prefix (or separator "/"))
  10041. (mapconcat
  10042. (lambda (h)
  10043. (setq n (1+ n))
  10044. (if (and (= n nsteps) (< maxwidth 10000))
  10045. (setq maxwidth (- total-width total)))
  10046. (if (< (length h) maxwidth)
  10047. (progn (setq total (+ total (length h) 1)) h)
  10048. (setq h (substring h 0 (- maxwidth 2))
  10049. total (+ total maxwidth 1))
  10050. (if (string-match "[ \t]+\\'" h)
  10051. (setq h (substring h 0 (match-beginning 0))))
  10052. (setq h (concat h "..")))
  10053. (org-add-props h nil 'face
  10054. (nth (% (1- n) org-n-level-faces)
  10055. org-level-faces))
  10056. h)
  10057. path (or separator "/"))))))
  10058. (defun org-display-outline-path (&optional file current separator just-return-string)
  10059. "Display the current outline path in the echo area.
  10060. If FILE is non-nil, prepend the output with the file name.
  10061. If CURRENT is non-nil, append the current heading to the output.
  10062. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10063. the different parts of the path and defaults to \"/\".
  10064. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10065. (interactive "P")
  10066. (let* (case-fold-search
  10067. message-log-max ; Don't populate the *Messages* buffer
  10068. (bfn (buffer-file-name (buffer-base-buffer)))
  10069. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10070. res)
  10071. (if current (setq path (append path
  10072. (save-excursion
  10073. (org-back-to-heading t)
  10074. (if (looking-at org-complex-heading-regexp)
  10075. (list (match-string 4)))))))
  10076. (setq res
  10077. (org-format-outline-path
  10078. path
  10079. (1- (frame-width))
  10080. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10081. separator))
  10082. (if just-return-string
  10083. (org-no-properties res)
  10084. (message "%s" res))))
  10085. (defvar org-refile-history nil
  10086. "History for refiling operations.")
  10087. (defvar org-after-refile-insert-hook nil
  10088. "Hook run after `org-refile' has inserted its stuff at the new location.
  10089. Note that this is still *before* the stuff will be removed from
  10090. the *old* location.")
  10091. (defvar org-capture-last-stored-marker)
  10092. (defvar org-refile-keep nil
  10093. "Non-nil means `org-refile' will copy instead of refile.")
  10094. (defun org-copy ()
  10095. "Like `org-refile', but copy."
  10096. (interactive)
  10097. (let ((org-refile-keep t))
  10098. (funcall 'org-refile nil nil nil "Copy")))
  10099. (defun org-refile (&optional goto default-buffer rfloc msg)
  10100. "Move the entry or entries at point to another heading.
  10101. The list of target headings is compiled using the information in
  10102. `org-refile-targets', which see.
  10103. At the target location, the entry is filed as a subitem of the target
  10104. heading. Depending on `org-reverse-note-order', the new subitem will
  10105. either be the first or the last subitem.
  10106. If there is an active region, all entries in that region will be moved.
  10107. However, the region must fulfill the requirement that the first heading
  10108. is the first one sets the top-level of the moved text - at most siblings
  10109. below it are allowed.
  10110. With prefix arg GOTO, the command will only visit the target location
  10111. and not actually move anything.
  10112. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10113. go to the location where the last refiling operation has put the subtree.
  10114. With a prefix argument of `2', refile to the running clock.
  10115. RFLOC can be a refile location obtained in a different way.
  10116. MSG is a string to replace \"Refile\" in the default prompt with
  10117. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10118. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10119. If you are using target caching (see `org-refile-use-cache'),
  10120. you have to clear the target cache in order to find new targets.
  10121. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10122. prefix argument (`C-u C-u C-u C-c C-w')."
  10123. (interactive "P")
  10124. (if (member goto '(0 (64)))
  10125. (org-refile-cache-clear)
  10126. (let* ((actionmsg (or msg "Refile"))
  10127. (cbuf (current-buffer))
  10128. (regionp (org-region-active-p))
  10129. (region-start (and regionp (region-beginning)))
  10130. (region-end (and regionp (region-end)))
  10131. (region-length (and regionp (- region-end region-start)))
  10132. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10133. pos it nbuf file re level reversed)
  10134. (setq last-command nil)
  10135. (when regionp
  10136. (goto-char region-start)
  10137. (or (bolp) (goto-char (point-at-bol)))
  10138. (setq region-start (point))
  10139. (unless (or (org-kill-is-subtree-p
  10140. (buffer-substring region-start region-end))
  10141. (prog1 org-refile-active-region-within-subtree
  10142. (org-toggle-heading)))
  10143. (error "The region is not a (sequence of) subtree(s)")))
  10144. (if (equal goto '(16))
  10145. (org-refile-goto-last-stored)
  10146. (when (or
  10147. (and (equal goto 2)
  10148. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10149. (prog1
  10150. (setq it (list (or org-clock-heading "running clock")
  10151. (buffer-file-name
  10152. (marker-buffer org-clock-hd-marker))
  10153. ""
  10154. (marker-position org-clock-hd-marker)))
  10155. (setq goto nil)))
  10156. (setq it (or rfloc
  10157. (let (heading-text)
  10158. (save-excursion
  10159. (unless goto
  10160. (org-back-to-heading t)
  10161. (setq heading-text
  10162. (nth 4 (org-heading-components))))
  10163. (org-refile-get-location
  10164. (cond (goto "Goto")
  10165. (regionp (concat actionmsg " region to"))
  10166. (t (concat actionmsg " subtree \""
  10167. heading-text "\" to")))
  10168. default-buffer
  10169. (and (not (equal '(4) goto))
  10170. org-refile-allow-creating-parent-nodes)
  10171. goto))))))
  10172. (setq file (nth 1 it)
  10173. re (nth 2 it)
  10174. pos (nth 3 it))
  10175. (if (and (not goto)
  10176. pos
  10177. (equal (buffer-file-name) file)
  10178. (if regionp
  10179. (and (>= pos region-start)
  10180. (<= pos region-end))
  10181. (and (>= pos (point))
  10182. (< pos (save-excursion
  10183. (org-end-of-subtree t t))))))
  10184. (error "Cannot refile to position inside the tree or region"))
  10185. (setq nbuf (or (find-buffer-visiting file)
  10186. (find-file-noselect file)))
  10187. (if goto
  10188. (progn
  10189. (org-pop-to-buffer-same-window nbuf)
  10190. (goto-char pos)
  10191. (org-show-context 'org-goto))
  10192. (if regionp
  10193. (progn
  10194. (org-kill-new (buffer-substring region-start region-end))
  10195. (org-save-markers-in-region region-start region-end))
  10196. (org-copy-subtree 1 nil t))
  10197. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10198. (find-file-noselect file)))
  10199. (setq reversed (org-notes-order-reversed-p))
  10200. (save-excursion
  10201. (save-restriction
  10202. (widen)
  10203. (if pos
  10204. (progn
  10205. (goto-char pos)
  10206. (looking-at org-outline-regexp)
  10207. (setq level (org-get-valid-level (funcall outline-level) 1))
  10208. (goto-char
  10209. (if reversed
  10210. (or (outline-next-heading) (point-max))
  10211. (or (save-excursion (org-get-next-sibling))
  10212. (org-end-of-subtree t t)
  10213. (point-max)))))
  10214. (setq level 1)
  10215. (if (not reversed)
  10216. (goto-char (point-max))
  10217. (goto-char (point-min))
  10218. (or (outline-next-heading) (goto-char (point-max)))))
  10219. (if (not (bolp)) (newline))
  10220. (org-paste-subtree level)
  10221. (when org-log-refile
  10222. (org-add-log-setup 'refile nil nil 'findpos
  10223. org-log-refile)
  10224. (unless (eq org-log-refile 'note)
  10225. (save-excursion (org-add-log-note))))
  10226. (and org-auto-align-tags
  10227. (let ((org-loop-over-headlines-in-active-region nil))
  10228. (org-set-tags nil t)))
  10229. (with-demoted-errors
  10230. (bookmark-set "org-refile-last-stored"))
  10231. ;; If we are refiling for capture, make sure that the
  10232. ;; last-capture pointers point here
  10233. (when (org-bound-and-true-p org-refile-for-capture)
  10234. (with-demoted-errors
  10235. (bookmark-set "org-capture-last-stored-marker"))
  10236. (move-marker org-capture-last-stored-marker (point)))
  10237. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10238. (run-hooks 'org-after-refile-insert-hook))))
  10239. (unless org-refile-keep
  10240. (if regionp
  10241. (delete-region (point) (+ (point) region-length))
  10242. (org-cut-subtree)))
  10243. (when (featurep 'org-inlinetask)
  10244. (org-inlinetask-remove-END-maybe))
  10245. (setq org-markers-to-move nil)
  10246. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10247. (defun org-refile-goto-last-stored ()
  10248. "Go to the location where the last refile was stored."
  10249. (interactive)
  10250. (bookmark-jump "org-refile-last-stored")
  10251. (message "This is the location of the last refile"))
  10252. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10253. no-exclude)
  10254. "Prompt the user for a refile location, using PROMPT.
  10255. PROMPT should not be suffixed with a colon and a space, because
  10256. this function appends the default value from
  10257. `org-refile-history' automatically, if that is not empty.
  10258. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10259. this is used for the GOTO interface."
  10260. (let ((org-refile-targets org-refile-targets)
  10261. (org-refile-use-outline-path org-refile-use-outline-path)
  10262. excluded-entries)
  10263. (when (and (derived-mode-p 'org-mode)
  10264. (not org-refile-use-cache)
  10265. (not no-exclude))
  10266. (org-map-tree
  10267. (lambda()
  10268. (setq excluded-entries
  10269. (append excluded-entries (list (org-get-heading t t)))))))
  10270. (setq org-refile-target-table
  10271. (org-refile-get-targets default-buffer excluded-entries)))
  10272. (unless org-refile-target-table
  10273. (error "No refile targets"))
  10274. (let* ((cbuf (current-buffer))
  10275. (partial-completion-mode nil)
  10276. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10277. (cfunc (if (and org-refile-use-outline-path
  10278. org-outline-path-complete-in-steps)
  10279. 'org-olpath-completing-read
  10280. 'org-icompleting-read))
  10281. (extra (if org-refile-use-outline-path "/" ""))
  10282. (cbnex (concat (buffer-name) extra))
  10283. (filename (and cfn (expand-file-name cfn)))
  10284. (tbl (mapcar
  10285. (lambda (x)
  10286. (if (and (not (member org-refile-use-outline-path
  10287. '(file full-file-path)))
  10288. (not (equal filename (nth 1 x))))
  10289. (cons (concat (car x) extra " ("
  10290. (file-name-nondirectory (nth 1 x)) ")")
  10291. (cdr x))
  10292. (cons (concat (car x) extra) (cdr x))))
  10293. org-refile-target-table))
  10294. (completion-ignore-case t)
  10295. cdef
  10296. (prompt (concat prompt
  10297. (or (and (car org-refile-history)
  10298. (concat " (default " (car org-refile-history) ")"))
  10299. (and (assoc cbnex tbl) (setq cdef cbnex)
  10300. (concat " (default " cbnex ")"))) ": "))
  10301. pa answ parent-target child parent old-hist)
  10302. (setq old-hist org-refile-history)
  10303. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10304. nil 'org-refile-history (or cdef (car org-refile-history))))
  10305. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10306. (org-refile-check-position pa)
  10307. (if pa
  10308. (progn
  10309. (when (or (not org-refile-history)
  10310. (not (eq old-hist org-refile-history))
  10311. (not (equal (car pa) (car org-refile-history))))
  10312. (setq org-refile-history
  10313. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10314. org-refile-history
  10315. (cdr org-refile-history))))
  10316. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10317. (pop org-refile-history)))
  10318. pa)
  10319. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10320. (progn
  10321. (setq parent (match-string 1 answ)
  10322. child (match-string 2 answ))
  10323. (setq parent-target (or (assoc parent tbl)
  10324. (assoc (concat parent "/") tbl)))
  10325. (when (and parent-target
  10326. (or (eq new-nodes t)
  10327. (and (eq new-nodes 'confirm)
  10328. (y-or-n-p (format "Create new node \"%s\"? "
  10329. child)))))
  10330. (org-refile-new-child parent-target child)))
  10331. (error "Invalid target location")))))
  10332. (declare-function org-string-nw-p "org-macs" (s))
  10333. (defun org-refile-check-position (refile-pointer)
  10334. "Check if the refile pointer matches the headline to which it points."
  10335. (let* ((file (nth 1 refile-pointer))
  10336. (re (nth 2 refile-pointer))
  10337. (pos (nth 3 refile-pointer))
  10338. buffer)
  10339. (if (and (not (markerp pos)) (not file))
  10340. (error "Please save the buffer to a file before refiling")
  10341. (when (org-string-nw-p re)
  10342. (setq buffer (if (markerp pos)
  10343. (marker-buffer pos)
  10344. (or (find-buffer-visiting file)
  10345. (find-file-noselect file))))
  10346. (with-current-buffer buffer
  10347. (save-excursion
  10348. (save-restriction
  10349. (widen)
  10350. (goto-char pos)
  10351. (beginning-of-line 1)
  10352. (unless (org-looking-at-p re)
  10353. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10354. (defun org-refile-new-child (parent-target child)
  10355. "Use refile target PARENT-TARGET to add new CHILD below it."
  10356. (unless parent-target
  10357. (error "Cannot find parent for new node"))
  10358. (let ((file (nth 1 parent-target))
  10359. (pos (nth 3 parent-target))
  10360. level)
  10361. (with-current-buffer (or (find-buffer-visiting file)
  10362. (find-file-noselect file))
  10363. (save-excursion
  10364. (save-restriction
  10365. (widen)
  10366. (if pos
  10367. (goto-char pos)
  10368. (goto-char (point-max))
  10369. (if (not (bolp)) (newline)))
  10370. (when (looking-at org-outline-regexp)
  10371. (setq level (funcall outline-level))
  10372. (org-end-of-subtree t t))
  10373. (org-back-over-empty-lines)
  10374. (insert "\n" (make-string
  10375. (if pos (org-get-valid-level level 1) 1) ?*)
  10376. " " child "\n")
  10377. (beginning-of-line 0)
  10378. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10379. (defun org-olpath-completing-read (prompt collection &rest args)
  10380. "Read an outline path like a file name."
  10381. (let ((thetable collection)
  10382. (org-completion-use-ido nil) ; does not work with ido.
  10383. (org-completion-use-iswitchb nil)) ; or iswitchb
  10384. (apply
  10385. 'org-icompleting-read prompt
  10386. (lambda (string predicate &optional flag)
  10387. (let (rtn r f (l (length string)))
  10388. (cond
  10389. ((eq flag nil)
  10390. ;; try completion
  10391. (try-completion string thetable))
  10392. ((eq flag t)
  10393. ;; all-completions
  10394. (setq rtn (all-completions string thetable predicate))
  10395. (mapcar
  10396. (lambda (x)
  10397. (setq r (substring x l))
  10398. (if (string-match " ([^)]*)$" x)
  10399. (setq f (match-string 0 x))
  10400. (setq f ""))
  10401. (if (string-match "/" r)
  10402. (concat string (substring r 0 (match-end 0)) f)
  10403. x))
  10404. rtn))
  10405. ((eq flag 'lambda)
  10406. ;; exact match?
  10407. (assoc string thetable)))))
  10408. args)))
  10409. ;;;; Dynamic blocks
  10410. (defun org-find-dblock (name)
  10411. "Find the first dynamic block with name NAME in the buffer.
  10412. If not found, stay at current position and return nil."
  10413. (let ((case-fold-search t) pos)
  10414. (save-excursion
  10415. (goto-char (point-min))
  10416. (setq pos (and (re-search-forward
  10417. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10418. (match-beginning 0))))
  10419. (if pos (goto-char pos))
  10420. pos))
  10421. (defconst org-dblock-start-re
  10422. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10423. "Matches the start line of a dynamic block, with parameters.")
  10424. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10425. "Matches the end of a dynamic block.")
  10426. (defun org-create-dblock (plist)
  10427. "Create a dynamic block section, with parameters taken from PLIST.
  10428. PLIST must contain a :name entry which is used as name of the block."
  10429. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10430. (end-of-line 1)
  10431. (newline))
  10432. (let ((col (current-column))
  10433. (name (plist-get plist :name)))
  10434. (insert "#+BEGIN: " name)
  10435. (while plist
  10436. (if (eq (car plist) :name)
  10437. (setq plist (cddr plist))
  10438. (insert " " (prin1-to-string (pop plist)))))
  10439. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10440. (beginning-of-line -2)))
  10441. (defun org-prepare-dblock ()
  10442. "Prepare dynamic block for refresh.
  10443. This empties the block, puts the cursor at the insert position and returns
  10444. the property list including an extra property :name with the block name."
  10445. (unless (looking-at org-dblock-start-re)
  10446. (error "Not at a dynamic block"))
  10447. (let* ((begdel (1+ (match-end 0)))
  10448. (name (org-no-properties (match-string 1)))
  10449. (params (append (list :name name)
  10450. (read (concat "(" (match-string 3) ")")))))
  10451. (save-excursion
  10452. (beginning-of-line 1)
  10453. (skip-chars-forward " \t")
  10454. (setq params (plist-put params :indentation-column (current-column))))
  10455. (unless (re-search-forward org-dblock-end-re nil t)
  10456. (error "Dynamic block not terminated"))
  10457. (setq params
  10458. (append params
  10459. (list :content (buffer-substring
  10460. begdel (match-beginning 0)))))
  10461. (delete-region begdel (match-beginning 0))
  10462. (goto-char begdel)
  10463. (open-line 1)
  10464. params))
  10465. (defun org-map-dblocks (&optional command)
  10466. "Apply COMMAND to all dynamic blocks in the current buffer.
  10467. If COMMAND is not given, use `org-update-dblock'."
  10468. (let ((cmd (or command 'org-update-dblock)))
  10469. (save-excursion
  10470. (goto-char (point-min))
  10471. (while (re-search-forward org-dblock-start-re nil t)
  10472. (goto-char (match-beginning 0))
  10473. (save-excursion
  10474. (condition-case nil
  10475. (funcall cmd)
  10476. (error (message "Error during update of dynamic block"))))
  10477. (unless (re-search-forward org-dblock-end-re nil t)
  10478. (error "Dynamic block not terminated"))))))
  10479. (defun org-dblock-update (&optional arg)
  10480. "User command for updating dynamic blocks.
  10481. Update the dynamic block at point. With prefix ARG, update all dynamic
  10482. blocks in the buffer."
  10483. (interactive "P")
  10484. (if arg
  10485. (org-update-all-dblocks)
  10486. (or (looking-at org-dblock-start-re)
  10487. (org-beginning-of-dblock))
  10488. (org-update-dblock)))
  10489. (defun org-update-dblock ()
  10490. "Update the dynamic block at point.
  10491. This means to empty the block, parse for parameters and then call
  10492. the correct writing function."
  10493. (interactive)
  10494. (save-window-excursion
  10495. (let* ((pos (point))
  10496. (line (org-current-line))
  10497. (params (org-prepare-dblock))
  10498. (name (plist-get params :name))
  10499. (indent (plist-get params :indentation-column))
  10500. (cmd (intern (concat "org-dblock-write:" name))))
  10501. (message "Updating dynamic block `%s' at line %d..." name line)
  10502. (funcall cmd params)
  10503. (message "Updating dynamic block `%s' at line %d...done" name line)
  10504. (goto-char pos)
  10505. (when (and indent (> indent 0))
  10506. (setq indent (make-string indent ?\ ))
  10507. (save-excursion
  10508. (org-beginning-of-dblock)
  10509. (forward-line 1)
  10510. (while (not (looking-at org-dblock-end-re))
  10511. (insert indent)
  10512. (beginning-of-line 2))
  10513. (when (looking-at org-dblock-end-re)
  10514. (and (looking-at "[ \t]+")
  10515. (replace-match ""))
  10516. (insert indent)))))))
  10517. (defun org-beginning-of-dblock ()
  10518. "Find the beginning of the dynamic block at point.
  10519. Error if there is no such block at point."
  10520. (let ((pos (point))
  10521. beg)
  10522. (end-of-line 1)
  10523. (if (and (re-search-backward org-dblock-start-re nil t)
  10524. (setq beg (match-beginning 0))
  10525. (re-search-forward org-dblock-end-re nil t)
  10526. (> (match-end 0) pos))
  10527. (goto-char beg)
  10528. (goto-char pos)
  10529. (error "Not in a dynamic block"))))
  10530. (defun org-update-all-dblocks ()
  10531. "Update all dynamic blocks in the buffer.
  10532. This function can be used in a hook."
  10533. (interactive)
  10534. (when (derived-mode-p 'org-mode)
  10535. (org-map-dblocks 'org-update-dblock)))
  10536. ;;;; Completion
  10537. (defun org-get-export-keywords ()
  10538. "Return a list of all currently understood export keywords.
  10539. Export keywords include options, block names, attributes and
  10540. keywords relative to each registered export back-end."
  10541. (delq nil
  10542. (let (keywords)
  10543. (mapc
  10544. (lambda (back-end)
  10545. (let ((props (cdr back-end)))
  10546. ;; Back-end name (for keywords, like #+LATEX:)
  10547. (push (upcase (symbol-name (car back-end))) keywords)
  10548. ;; Back-end options.
  10549. (mapc (lambda (option) (push (cadr option) keywords))
  10550. (plist-get (cdr back-end) :options-alist))))
  10551. (org-bound-and-true-p org-export-registered-backends))
  10552. keywords)))
  10553. (defconst org-options-keywords
  10554. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10555. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10556. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10557. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10558. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10559. (defcustom org-structure-template-alist
  10560. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10561. "<src lang=\"?\">\n\n</src>")
  10562. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10563. "<example>\n?\n</example>")
  10564. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10565. "<quote>\n?\n</quote>")
  10566. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10567. "<verse>\n?\n</verse>")
  10568. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10569. "<verbatim>\n?\n</verbatim>")
  10570. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10571. "<center>\n?\n</center>")
  10572. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10573. "<literal style=\"latex\">\n?\n</literal>")
  10574. ("L" "#+LaTeX: "
  10575. "<literal style=\"latex\">?</literal>")
  10576. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10577. "<literal style=\"html\">\n?\n</literal>")
  10578. ("H" "#+HTML: "
  10579. "<literal style=\"html\">?</literal>")
  10580. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10581. ("A" "#+ASCII: ")
  10582. ("i" "#+INDEX: ?"
  10583. "#+INDEX: ?")
  10584. ("I" "#+INCLUDE: %file ?"
  10585. "<include file=%file markup=\"?\">"))
  10586. "Structure completion elements.
  10587. This is a list of abbreviation keys and values. The value gets inserted
  10588. if you type `<' followed by the key and then press the completion key,
  10589. usually `M-TAB'. %file will be replaced by a file name after prompting
  10590. for the file using completion. The cursor will be placed at the position
  10591. of the `?` in the template.
  10592. There are two templates for each key, the first uses the original Org syntax,
  10593. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10594. the default when the /org-mtags.el/ module has been loaded. See also the
  10595. variable `org-mtags-prefer-muse-templates'."
  10596. :group 'org-completion
  10597. :type '(repeat
  10598. (string :tag "Key")
  10599. (string :tag "Template")
  10600. (string :tag "Muse Template")))
  10601. (defun org-try-structure-completion ()
  10602. "Try to complete a structure template before point.
  10603. This looks for strings like \"<e\" on an otherwise empty line and
  10604. expands them."
  10605. (let ((l (buffer-substring (point-at-bol) (point)))
  10606. a)
  10607. (when (and (looking-at "[ \t]*$")
  10608. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10609. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10610. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10611. (match-beginning 1)) a)
  10612. t)))
  10613. (defun org-complete-expand-structure-template (start cell)
  10614. "Expand a structure template."
  10615. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10616. (rpl (nth (if musep 2 1) cell))
  10617. (ind ""))
  10618. (delete-region start (point))
  10619. (when (string-match "\\`#\\+" rpl)
  10620. (cond
  10621. ((bolp))
  10622. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10623. (setq ind (buffer-substring (point-at-bol) (point))))
  10624. (t (newline))))
  10625. (setq start (point))
  10626. (if (string-match "%file" rpl)
  10627. (setq rpl (replace-match
  10628. (concat
  10629. "\""
  10630. (save-match-data
  10631. (abbreviate-file-name (read-file-name "Include file: ")))
  10632. "\"")
  10633. t t rpl)))
  10634. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10635. (concat "\n" ind)))
  10636. (insert rpl)
  10637. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10638. ;;;; TODO, DEADLINE, Comments
  10639. (defun org-toggle-comment ()
  10640. "Change the COMMENT state of an entry."
  10641. (interactive)
  10642. (save-excursion
  10643. (org-back-to-heading)
  10644. (let (case-fold-search)
  10645. (cond
  10646. ((looking-at (format org-heading-keyword-regexp-format
  10647. org-comment-string))
  10648. (goto-char (match-end 1))
  10649. (looking-at (concat " +" org-comment-string))
  10650. (replace-match "" t t)
  10651. (when (eolp) (insert " ")))
  10652. ((looking-at org-outline-regexp)
  10653. (goto-char (match-end 0))
  10654. (insert org-comment-string " "))))))
  10655. (defvar org-last-todo-state-is-todo nil
  10656. "This is non-nil when the last TODO state change led to a TODO state.
  10657. If the last change removed the TODO tag or switched to DONE, then
  10658. this is nil.")
  10659. (defvar org-setting-tags nil) ; dynamically skipped
  10660. (defvar org-todo-setup-filter-hook nil
  10661. "Hook for functions that pre-filter todo specs.
  10662. Each function takes a todo spec and returns either nil or the spec
  10663. transformed into canonical form." )
  10664. (defvar org-todo-get-default-hook nil
  10665. "Hook for functions that get a default item for todo.
  10666. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10667. nil or a string to be used for the todo mark." )
  10668. (defvar org-agenda-headline-snapshot-before-repeat)
  10669. (defun org-current-effective-time ()
  10670. "Return current time adjusted for `org-extend-today-until' variable."
  10671. (let* ((ct (org-current-time))
  10672. (dct (decode-time ct))
  10673. (ct1
  10674. (cond
  10675. (org-use-last-clock-out-time-as-effective-time
  10676. (or (org-clock-get-last-clock-out-time) ct))
  10677. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10678. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10679. (t ct))))
  10680. ct1))
  10681. (defun org-todo-yesterday (&optional arg)
  10682. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10683. (interactive "P")
  10684. (if (eq major-mode 'org-agenda-mode)
  10685. (apply 'org-agenda-todo-yesterday arg)
  10686. (let* ((hour (third (decode-time
  10687. (org-current-time))))
  10688. (org-extend-today-until (1+ hour)))
  10689. (org-todo arg))))
  10690. (defvar org-block-entry-blocking ""
  10691. "First entry preventing the TODO state change.")
  10692. (defun org-todo (&optional arg)
  10693. "Change the TODO state of an item.
  10694. The state of an item is given by a keyword at the start of the heading,
  10695. like
  10696. *** TODO Write paper
  10697. *** DONE Call mom
  10698. The different keywords are specified in the variable `org-todo-keywords'.
  10699. By default the available states are \"TODO\" and \"DONE\".
  10700. So for this example: when the item starts with TODO, it is changed to DONE.
  10701. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10702. DONE are present, add TODO at the beginning of the heading.
  10703. With \\[universal-argument] prefix arg, use completion to determine the new \
  10704. state.
  10705. With numeric prefix arg, switch to that state.
  10706. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10707. keywords (nextset).
  10708. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10709. With a numeric prefix arg of 0, inhibit note taking for the change.
  10710. For calling through lisp, arg is also interpreted in the following way:
  10711. 'none -> empty state
  10712. \"\"(empty string) -> switch to empty state
  10713. 'done -> switch to DONE
  10714. 'nextset -> switch to the next set of keywords
  10715. 'previousset -> switch to the previous set of keywords
  10716. \"WAITING\" -> switch to the specified keyword, but only if it
  10717. really is a member of `org-todo-keywords'."
  10718. (interactive "P")
  10719. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10720. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10721. 'region-start-level 'region))
  10722. org-loop-over-headlines-in-active-region)
  10723. (org-map-entries
  10724. `(org-todo ,arg)
  10725. org-loop-over-headlines-in-active-region
  10726. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10727. (if (equal arg '(16)) (setq arg 'nextset))
  10728. (let ((org-blocker-hook org-blocker-hook)
  10729. commentp
  10730. case-fold-search)
  10731. (when (equal arg '(64))
  10732. (setq arg nil org-blocker-hook nil))
  10733. (when (and org-blocker-hook
  10734. (or org-inhibit-blocking
  10735. (org-entry-get nil "NOBLOCKING")))
  10736. (setq org-blocker-hook nil))
  10737. (save-excursion
  10738. (catch 'exit
  10739. (org-back-to-heading t)
  10740. (when (looking-at (concat "^\\*+ " org-comment-string))
  10741. (org-toggle-comment)
  10742. (setq commentp t))
  10743. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10744. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10745. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10746. (let* ((match-data (match-data))
  10747. (startpos (point-at-bol))
  10748. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10749. (org-log-done org-log-done)
  10750. (org-log-repeat org-log-repeat)
  10751. (org-todo-log-states org-todo-log-states)
  10752. (org-inhibit-logging
  10753. (if (equal arg 0)
  10754. (progn (setq arg nil) 'note) org-inhibit-logging))
  10755. (this (match-string 1))
  10756. (hl-pos (match-beginning 0))
  10757. (head (org-get-todo-sequence-head this))
  10758. (ass (assoc head org-todo-kwd-alist))
  10759. (interpret (nth 1 ass))
  10760. (done-word (nth 3 ass))
  10761. (final-done-word (nth 4 ass))
  10762. (org-last-state (or this ""))
  10763. (completion-ignore-case t)
  10764. (member (member this org-todo-keywords-1))
  10765. (tail (cdr member))
  10766. (org-state (cond
  10767. ((and org-todo-key-trigger
  10768. (or (and (equal arg '(4))
  10769. (eq org-use-fast-todo-selection 'prefix))
  10770. (and (not arg) org-use-fast-todo-selection
  10771. (not (eq org-use-fast-todo-selection
  10772. 'prefix)))))
  10773. ;; Use fast selection
  10774. (org-fast-todo-selection))
  10775. ((and (equal arg '(4))
  10776. (or (not org-use-fast-todo-selection)
  10777. (not org-todo-key-trigger)))
  10778. ;; Read a state with completion
  10779. (org-icompleting-read
  10780. "State: " (mapcar (lambda(x) (list x))
  10781. org-todo-keywords-1)
  10782. nil t))
  10783. ((eq arg 'right)
  10784. (if this
  10785. (if tail (car tail) nil)
  10786. (car org-todo-keywords-1)))
  10787. ((eq arg 'left)
  10788. (if (equal member org-todo-keywords-1)
  10789. nil
  10790. (if this
  10791. (nth (- (length org-todo-keywords-1)
  10792. (length tail) 2)
  10793. org-todo-keywords-1)
  10794. (org-last org-todo-keywords-1))))
  10795. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10796. (setq arg nil))) ; hack to fall back to cycling
  10797. (arg
  10798. ;; user or caller requests a specific state
  10799. (cond
  10800. ((equal arg "") nil)
  10801. ((eq arg 'none) nil)
  10802. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10803. ((eq arg 'nextset)
  10804. (or (car (cdr (member head org-todo-heads)))
  10805. (car org-todo-heads)))
  10806. ((eq arg 'previousset)
  10807. (let ((org-todo-heads (reverse org-todo-heads)))
  10808. (or (car (cdr (member head org-todo-heads)))
  10809. (car org-todo-heads))))
  10810. ((car (member arg org-todo-keywords-1)))
  10811. ((stringp arg)
  10812. (error "State `%s' not valid in this file" arg))
  10813. ((nth (1- (prefix-numeric-value arg))
  10814. org-todo-keywords-1))))
  10815. ((null member) (or head (car org-todo-keywords-1)))
  10816. ((equal this final-done-word) nil) ;; -> make empty
  10817. ((null tail) nil) ;; -> first entry
  10818. ((memq interpret '(type priority))
  10819. (if (eq this-command last-command)
  10820. (car tail)
  10821. (if (> (length tail) 0)
  10822. (or done-word (car org-done-keywords))
  10823. nil)))
  10824. (t
  10825. (car tail))))
  10826. (org-state (or
  10827. (run-hook-with-args-until-success
  10828. 'org-todo-get-default-hook org-state org-last-state)
  10829. org-state))
  10830. (next (if org-state (concat " " org-state " ") " "))
  10831. (change-plist (list :type 'todo-state-change :from this :to org-state
  10832. :position startpos))
  10833. dolog now-done-p)
  10834. (when org-blocker-hook
  10835. (setq org-last-todo-state-is-todo
  10836. (not (member this org-done-keywords)))
  10837. (unless (save-excursion
  10838. (save-match-data
  10839. (org-with-wide-buffer
  10840. (run-hook-with-args-until-failure
  10841. 'org-blocker-hook change-plist))))
  10842. (if (org-called-interactively-p 'interactive)
  10843. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10844. this org-state org-block-entry-blocking)
  10845. ;; fail silently
  10846. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10847. this org-state org-block-entry-blocking)
  10848. (throw 'exit nil))))
  10849. (store-match-data match-data)
  10850. (replace-match next t t)
  10851. (unless (pos-visible-in-window-p hl-pos)
  10852. (message "TODO state changed to %s" (org-trim next)))
  10853. (unless head
  10854. (setq head (org-get-todo-sequence-head org-state)
  10855. ass (assoc head org-todo-kwd-alist)
  10856. interpret (nth 1 ass)
  10857. done-word (nth 3 ass)
  10858. final-done-word (nth 4 ass)))
  10859. (when (memq arg '(nextset previousset))
  10860. (message "Keyword-Set %d/%d: %s"
  10861. (- (length org-todo-sets) -1
  10862. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10863. (length org-todo-sets)
  10864. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10865. (setq org-last-todo-state-is-todo
  10866. (not (member org-state org-done-keywords)))
  10867. (setq now-done-p (and (member org-state org-done-keywords)
  10868. (not (member this org-done-keywords))))
  10869. (and logging (org-local-logging logging))
  10870. (when (and (or org-todo-log-states org-log-done)
  10871. (not (eq org-inhibit-logging t))
  10872. (not (memq arg '(nextset previousset))))
  10873. ;; we need to look at recording a time and note
  10874. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10875. (nth 2 (assoc this org-todo-log-states))))
  10876. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10877. (setq dolog 'time))
  10878. (when (and org-state
  10879. (member org-state org-not-done-keywords)
  10880. (not (member this org-not-done-keywords)))
  10881. ;; This is now a todo state and was not one before
  10882. ;; If there was a CLOSED time stamp, get rid of it.
  10883. (org-add-planning-info nil nil 'closed))
  10884. (when (and now-done-p org-log-done)
  10885. ;; It is now done, and it was not done before
  10886. (org-add-planning-info 'closed (org-current-effective-time))
  10887. (if (and (not dolog) (eq 'note org-log-done))
  10888. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10889. (when (and org-state dolog)
  10890. ;; This is a non-nil state, and we need to log it
  10891. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10892. ;; Fixup tag positioning
  10893. (org-todo-trigger-tag-changes org-state)
  10894. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10895. (when org-provide-todo-statistics
  10896. (org-update-parent-todo-statistics))
  10897. (run-hooks 'org-after-todo-state-change-hook)
  10898. (if (and arg (not (member org-state org-done-keywords)))
  10899. (setq head (org-get-todo-sequence-head org-state)))
  10900. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10901. ;; Do we need to trigger a repeat?
  10902. (when now-done-p
  10903. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10904. ;; This is for the agenda, take a snapshot of the headline.
  10905. (save-match-data
  10906. (setq org-agenda-headline-snapshot-before-repeat
  10907. (org-get-heading))))
  10908. (org-auto-repeat-maybe org-state))
  10909. ;; Fixup cursor location if close to the keyword
  10910. (if (and (outline-on-heading-p)
  10911. (not (bolp))
  10912. (save-excursion (beginning-of-line 1)
  10913. (looking-at org-todo-line-regexp))
  10914. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10915. (progn
  10916. (goto-char (or (match-end 2) (match-end 1)))
  10917. (and (looking-at " ") (just-one-space))))
  10918. (when org-trigger-hook
  10919. (save-excursion
  10920. (run-hook-with-args 'org-trigger-hook change-plist)))
  10921. (when commentp (org-toggle-comment))))))))
  10922. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10923. "Block turning an entry into a TODO, using the hierarchy.
  10924. This checks whether the current task should be blocked from state
  10925. changes. Such blocking occurs when:
  10926. 1. The task has children which are not all in a completed state.
  10927. 2. A task has a parent with the property :ORDERED:, and there
  10928. are siblings prior to the current task with incomplete
  10929. status.
  10930. 3. The parent of the task is blocked because it has siblings that should
  10931. be done first, or is child of a block grandparent TODO entry."
  10932. (if (not org-enforce-todo-dependencies)
  10933. t ; if locally turned off don't block
  10934. (catch 'dont-block
  10935. ;; If this is not a todo state change, or if this entry is already DONE,
  10936. ;; do not block
  10937. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10938. (member (plist-get change-plist :from)
  10939. (cons 'done org-done-keywords))
  10940. (member (plist-get change-plist :to)
  10941. (cons 'todo org-not-done-keywords))
  10942. (not (plist-get change-plist :to)))
  10943. (throw 'dont-block t))
  10944. ;; If this task has children, and any are undone, it's blocked
  10945. (save-excursion
  10946. (org-back-to-heading t)
  10947. (let ((this-level (funcall outline-level)))
  10948. (outline-next-heading)
  10949. (let ((child-level (funcall outline-level)))
  10950. (while (and (not (eobp))
  10951. (> child-level this-level))
  10952. ;; this todo has children, check whether they are all
  10953. ;; completed
  10954. (if (and (not (org-entry-is-done-p))
  10955. (org-entry-is-todo-p))
  10956. (progn (setq org-block-entry-blocking (org-get-heading))
  10957. (throw 'dont-block nil)))
  10958. (outline-next-heading)
  10959. (setq child-level (funcall outline-level))))))
  10960. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10961. ;; any previous siblings are undone, it's blocked
  10962. (save-excursion
  10963. (org-back-to-heading t)
  10964. (let* ((pos (point))
  10965. (parent-pos (and (org-up-heading-safe) (point))))
  10966. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10967. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10968. (forward-line 1)
  10969. (re-search-forward org-not-done-heading-regexp pos t))
  10970. (setq org-block-entry-blocking (match-string 0))
  10971. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10972. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10973. (while t
  10974. (goto-char parent-pos)
  10975. (if (not (looking-at org-not-done-heading-regexp))
  10976. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10977. (setq pos (point))
  10978. (setq parent-pos (and (org-up-heading-safe) (point)))
  10979. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10980. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10981. (forward-line 1)
  10982. (re-search-forward org-not-done-heading-regexp pos t)
  10983. (setq org-block-entry-blocking (org-get-heading)))
  10984. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10985. (defcustom org-track-ordered-property-with-tag nil
  10986. "Should the ORDERED property also be shown as a tag?
  10987. The ORDERED property decides if an entry should require subtasks to be
  10988. completed in sequence. Since a property is not very visible, setting
  10989. this option means that toggling the ORDERED property with the command
  10990. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10991. not relevant for the behavior, but it makes things more visible.
  10992. Note that toggling the tag with tags commands will not change the property
  10993. and therefore not influence behavior!
  10994. This can be t, meaning the tag ORDERED should be used, It can also be a
  10995. string to select a different tag for this task."
  10996. :group 'org-todo
  10997. :type '(choice
  10998. (const :tag "No tracking" nil)
  10999. (const :tag "Track with ORDERED tag" t)
  11000. (string :tag "Use other tag")))
  11001. (defun org-toggle-ordered-property ()
  11002. "Toggle the ORDERED property of the current entry.
  11003. For better visibility, you can track the value of this property with a tag.
  11004. See variable `org-track-ordered-property-with-tag'."
  11005. (interactive)
  11006. (let* ((t1 org-track-ordered-property-with-tag)
  11007. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11008. (save-excursion
  11009. (org-back-to-heading)
  11010. (if (org-entry-get nil "ORDERED")
  11011. (progn
  11012. (org-delete-property "ORDERED" "PROPERTIES")
  11013. (and tag (org-toggle-tag tag 'off))
  11014. (message "Subtasks can be completed in arbitrary order"))
  11015. (org-entry-put nil "ORDERED" "t")
  11016. (and tag (org-toggle-tag tag 'on))
  11017. (message "Subtasks must be completed in sequence")))))
  11018. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11019. (defun org-block-todo-from-checkboxes (change-plist)
  11020. "Block turning an entry into a TODO, using checkboxes.
  11021. This checks whether the current task should be blocked from state
  11022. changes because there are unchecked boxes in this entry."
  11023. (if (not org-enforce-todo-checkbox-dependencies)
  11024. t ; if locally turned off don't block
  11025. (catch 'dont-block
  11026. ;; If this is not a todo state change, or if this entry is already DONE,
  11027. ;; do not block
  11028. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11029. (member (plist-get change-plist :from)
  11030. (cons 'done org-done-keywords))
  11031. (member (plist-get change-plist :to)
  11032. (cons 'todo org-not-done-keywords))
  11033. (not (plist-get change-plist :to)))
  11034. (throw 'dont-block t))
  11035. ;; If this task has checkboxes that are not checked, it's blocked
  11036. (save-excursion
  11037. (org-back-to-heading t)
  11038. (let ((beg (point)) end)
  11039. (outline-next-heading)
  11040. (setq end (point))
  11041. (goto-char beg)
  11042. (if (org-list-search-forward
  11043. (concat (org-item-beginning-re)
  11044. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11045. "\\[[- ]\\]")
  11046. end t)
  11047. (progn
  11048. (if (boundp 'org-blocked-by-checkboxes)
  11049. (setq org-blocked-by-checkboxes t))
  11050. (throw 'dont-block nil)))))
  11051. t))) ; do not block
  11052. (defun org-entry-blocked-p ()
  11053. "Is the current entry blocked?"
  11054. (org-with-silent-modifications
  11055. (if (org-entry-get nil "NOBLOCKING")
  11056. nil ;; Never block this entry
  11057. (not (run-hook-with-args-until-failure
  11058. 'org-blocker-hook
  11059. (list :type 'todo-state-change
  11060. :position (point)
  11061. :from 'todo
  11062. :to 'done))))))
  11063. (defun org-update-statistics-cookies (all)
  11064. "Update the statistics cookie, either from TODO or from checkboxes.
  11065. This should be called with the cursor in a line with a statistics cookie."
  11066. (interactive "P")
  11067. (if all
  11068. (progn
  11069. (org-update-checkbox-count 'all)
  11070. (org-map-entries 'org-update-parent-todo-statistics))
  11071. (if (not (org-at-heading-p))
  11072. (org-update-checkbox-count)
  11073. (let ((pos (point-marker))
  11074. end l1 l2)
  11075. (ignore-errors (org-back-to-heading t))
  11076. (if (not (org-at-heading-p))
  11077. (org-update-checkbox-count)
  11078. (setq l1 (org-outline-level))
  11079. (setq end (save-excursion
  11080. (outline-next-heading)
  11081. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11082. (point)))
  11083. (if (and (save-excursion
  11084. (re-search-forward
  11085. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11086. (not (save-excursion (re-search-forward
  11087. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11088. (org-update-checkbox-count)
  11089. (if (and l2 (> l2 l1))
  11090. (progn
  11091. (goto-char end)
  11092. (org-update-parent-todo-statistics))
  11093. (goto-char pos)
  11094. (beginning-of-line 1)
  11095. (while (re-search-forward
  11096. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11097. (point-at-eol) t)
  11098. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11099. (goto-char pos)
  11100. (move-marker pos nil)))))
  11101. (defvar org-entry-property-inherited-from) ;; defined below
  11102. (defun org-update-parent-todo-statistics ()
  11103. "Update any statistics cookie in the parent of the current headline.
  11104. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11105. the entire subtree and this will travel up the hierarchy and update
  11106. statistics everywhere."
  11107. (let* ((prop (save-excursion (org-up-heading-safe)
  11108. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11109. (recursive (or (not org-hierarchical-todo-statistics)
  11110. (and prop (string-match "\\<recursive\\>" prop))))
  11111. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11112. 0))
  11113. (first t)
  11114. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11115. level ltoggle l1 new ndel
  11116. (cnt-all 0) (cnt-done 0) is-percent kwd
  11117. checkbox-beg ov ovs ove cookie-present)
  11118. (catch 'exit
  11119. (save-excursion
  11120. (beginning-of-line 1)
  11121. (setq ltoggle (funcall outline-level))
  11122. ;; Three situations are to consider:
  11123. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11124. ;; to the top-level ancestor on the headline;
  11125. ;; 2. If parent has "recursive" property, repeat up to the
  11126. ;; headline setting that property, taking inheritance into
  11127. ;; account;
  11128. ;; 3. Else, move up to direct parent and proceed only once.
  11129. (while (and (setq level (org-up-heading-safe))
  11130. (or recursive first)
  11131. (>= (point) lim))
  11132. (setq first nil cookie-present nil)
  11133. (unless (and level
  11134. (not (string-match
  11135. "\\<checkbox\\>"
  11136. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11137. "")))))
  11138. (throw 'exit nil))
  11139. (while (re-search-forward box-re (point-at-eol) t)
  11140. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11141. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11142. (save-match-data
  11143. (unless (outline-next-heading) (throw 'exit nil))
  11144. (while (and (looking-at org-complex-heading-regexp)
  11145. (> (setq l1 (length (match-string 1))) level))
  11146. (setq kwd (and (or recursive (= l1 ltoggle))
  11147. (match-string 2)))
  11148. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11149. (and (listp org-provide-todo-statistics)
  11150. (or (member kwd org-provide-todo-statistics)
  11151. (member kwd org-done-keywords))))
  11152. (setq cnt-all (1+ cnt-all))
  11153. (if (eq org-provide-todo-statistics t)
  11154. (and kwd (setq cnt-all (1+ cnt-all)))))
  11155. (and (member kwd org-done-keywords)
  11156. (setq cnt-done (1+ cnt-done)))
  11157. (outline-next-heading)))
  11158. (setq new
  11159. (if is-percent
  11160. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11161. (format "[%d/%d]" cnt-done cnt-all))
  11162. ndel (- (match-end 0) checkbox-beg))
  11163. ;; handle overlays when updating cookie from column view
  11164. (when (setq ov (car (overlays-at checkbox-beg)))
  11165. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11166. (delete-overlay ov))
  11167. (goto-char checkbox-beg)
  11168. (insert new)
  11169. (delete-region (point) (+ (point) ndel))
  11170. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11171. (when ov (move-overlay ov ovs ove)))
  11172. (when cookie-present
  11173. (run-hook-with-args 'org-after-todo-statistics-hook
  11174. cnt-done (- cnt-all cnt-done))))))
  11175. (run-hooks 'org-todo-statistics-hook)))
  11176. (defvar org-after-todo-statistics-hook nil
  11177. "Hook that is called after a TODO statistics cookie has been updated.
  11178. Each function is called with two arguments: the number of not-done entries
  11179. and the number of done entries.
  11180. For example, the following function, when added to this hook, will switch
  11181. an entry to DONE when all children are done, and back to TODO when new
  11182. entries are set to a TODO status. Note that this hook is only called
  11183. when there is a statistics cookie in the headline!
  11184. (defun org-summary-todo (n-done n-not-done)
  11185. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11186. (let (org-log-done org-log-states) ; turn off logging
  11187. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11188. ")
  11189. (defvar org-todo-statistics-hook nil
  11190. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11191. This hook runs even if there is no statistics cookie present, in which case
  11192. `org-after-todo-statistics-hook' would not run.")
  11193. (defun org-todo-trigger-tag-changes (state)
  11194. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11195. (let ((l org-todo-state-tags-triggers)
  11196. changes)
  11197. (when (or (not state) (equal state ""))
  11198. (setq changes (append changes (cdr (assoc "" l)))))
  11199. (when (and (stringp state) (> (length state) 0))
  11200. (setq changes (append changes (cdr (assoc state l)))))
  11201. (when (member state org-not-done-keywords)
  11202. (setq changes (append changes (cdr (assoc 'todo l)))))
  11203. (when (member state org-done-keywords)
  11204. (setq changes (append changes (cdr (assoc 'done l)))))
  11205. (dolist (c changes)
  11206. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11207. (defun org-local-logging (value)
  11208. "Get logging settings from a property VALUE."
  11209. (let* (words w a)
  11210. ;; directly set the variables, they are already local.
  11211. (setq org-log-done nil
  11212. org-log-repeat nil
  11213. org-todo-log-states nil)
  11214. (setq words (org-split-string value))
  11215. (while (setq w (pop words))
  11216. (cond
  11217. ((setq a (assoc w org-startup-options))
  11218. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11219. (set (nth 1 a) (nth 2 a))))
  11220. ((setq a (org-extract-log-state-settings w))
  11221. (and (member (car a) org-todo-keywords-1)
  11222. (push a org-todo-log-states)))))))
  11223. (defun org-get-todo-sequence-head (kwd)
  11224. "Return the head of the TODO sequence to which KWD belongs.
  11225. If KWD is not set, check if there is a text property remembering the
  11226. right sequence."
  11227. (let (p)
  11228. (cond
  11229. ((not kwd)
  11230. (or (get-text-property (point-at-bol) 'org-todo-head)
  11231. (progn
  11232. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11233. nil (point-at-eol)))
  11234. (get-text-property p 'org-todo-head))))
  11235. ((not (member kwd org-todo-keywords-1))
  11236. (car org-todo-keywords-1))
  11237. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11238. (defun org-fast-todo-selection ()
  11239. "Fast TODO keyword selection with single keys.
  11240. Returns the new TODO keyword, or nil if no state change should occur."
  11241. (let* ((fulltable org-todo-key-alist)
  11242. (done-keywords org-done-keywords) ;; needed for the faces.
  11243. (maxlen (apply 'max (mapcar
  11244. (lambda (x)
  11245. (if (stringp (car x)) (string-width (car x)) 0))
  11246. fulltable)))
  11247. (expert nil)
  11248. (fwidth (+ maxlen 3 1 3))
  11249. (ncol (/ (- (window-width) 4) fwidth))
  11250. tg cnt e c tbl
  11251. groups ingroup)
  11252. (save-excursion
  11253. (save-window-excursion
  11254. (if expert
  11255. (set-buffer (get-buffer-create " *Org todo*"))
  11256. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11257. (erase-buffer)
  11258. (org-set-local 'org-done-keywords done-keywords)
  11259. (setq tbl fulltable cnt 0)
  11260. (while (setq e (pop tbl))
  11261. (cond
  11262. ((equal e '(:startgroup))
  11263. (push '() groups) (setq ingroup t)
  11264. (when (not (= cnt 0))
  11265. (setq cnt 0)
  11266. (insert "\n"))
  11267. (insert "{ "))
  11268. ((equal e '(:endgroup))
  11269. (setq ingroup nil cnt 0)
  11270. (insert "}\n"))
  11271. ((equal e '(:newline))
  11272. (when (not (= cnt 0))
  11273. (setq cnt 0)
  11274. (insert "\n")
  11275. (setq e (car tbl))
  11276. (while (equal (car tbl) '(:newline))
  11277. (insert "\n")
  11278. (setq tbl (cdr tbl)))))
  11279. (t
  11280. (setq tg (car e) c (cdr e))
  11281. (if ingroup (push tg (car groups)))
  11282. (setq tg (org-add-props tg nil 'face
  11283. (org-get-todo-face tg)))
  11284. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11285. (insert "[" c "] " tg (make-string
  11286. (- fwidth 4 (length tg)) ?\ ))
  11287. (when (= (setq cnt (1+ cnt)) ncol)
  11288. (insert "\n")
  11289. (if ingroup (insert " "))
  11290. (setq cnt 0)))))
  11291. (insert "\n")
  11292. (goto-char (point-min))
  11293. (if (not expert) (org-fit-window-to-buffer))
  11294. (message "[a-z..]:Set [SPC]:clear")
  11295. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11296. (cond
  11297. ((or (= c ?\C-g)
  11298. (and (= c ?q) (not (rassoc c fulltable))))
  11299. (setq quit-flag t))
  11300. ((= c ?\ ) nil)
  11301. ((setq e (rassoc c fulltable) tg (car e))
  11302. tg)
  11303. (t (setq quit-flag t)))))))
  11304. (defun org-entry-is-todo-p ()
  11305. (member (org-get-todo-state) org-not-done-keywords))
  11306. (defun org-entry-is-done-p ()
  11307. (member (org-get-todo-state) org-done-keywords))
  11308. (defun org-get-todo-state ()
  11309. (save-excursion
  11310. (org-back-to-heading t)
  11311. (and (looking-at org-todo-line-regexp)
  11312. (match-end 2)
  11313. (match-string 2))))
  11314. (defun org-at-date-range-p (&optional inactive-ok)
  11315. "Is the cursor inside a date range?"
  11316. (interactive)
  11317. (save-excursion
  11318. (catch 'exit
  11319. (let ((pos (point)))
  11320. (skip-chars-backward "^[<\r\n")
  11321. (skip-chars-backward "<[")
  11322. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11323. (>= (match-end 0) pos)
  11324. (throw 'exit t))
  11325. (skip-chars-backward "^<[\r\n")
  11326. (skip-chars-backward "<[")
  11327. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11328. (>= (match-end 0) pos)
  11329. (throw 'exit t)))
  11330. nil)))
  11331. (defun org-get-repeat (&optional tagline)
  11332. "Check if there is a deadline/schedule with repeater in this entry."
  11333. (save-match-data
  11334. (save-excursion
  11335. (org-back-to-heading t)
  11336. (and (re-search-forward (if tagline
  11337. (concat tagline "\\s-*" org-repeat-re)
  11338. org-repeat-re)
  11339. (org-entry-end-position) t)
  11340. (match-string-no-properties 1)))))
  11341. (defvar org-last-changed-timestamp)
  11342. (defvar org-last-inserted-timestamp)
  11343. (defvar org-log-post-message)
  11344. (defvar org-log-note-purpose)
  11345. (defvar org-log-note-how)
  11346. (defvar org-log-note-extra)
  11347. (defun org-auto-repeat-maybe (done-word)
  11348. "Check if the current headline contains a repeated deadline/schedule.
  11349. If yes, set TODO state back to what it was and change the base date
  11350. of repeating deadline/scheduled time stamps to new date.
  11351. This function is run automatically after each state change to a DONE state."
  11352. ;; last-state is dynamically scoped into this function
  11353. (let* ((repeat (org-get-repeat))
  11354. (aa (assoc org-last-state org-todo-kwd-alist))
  11355. (interpret (nth 1 aa))
  11356. (head (nth 2 aa))
  11357. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11358. (msg "Entry repeats: ")
  11359. (org-log-done nil)
  11360. (org-todo-log-states nil)
  11361. re type n what ts time to-state)
  11362. (when repeat
  11363. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11364. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11365. org-todo-repeat-to-state))
  11366. (unless (and to-state (member to-state org-todo-keywords-1))
  11367. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11368. (org-todo to-state)
  11369. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11370. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11371. (org-time-stamp-format t t))))
  11372. (when org-log-repeat
  11373. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11374. (memq 'org-add-log-note post-command-hook))
  11375. ;; OK, we are already setup for some record
  11376. (if (eq org-log-repeat 'note)
  11377. ;; make sure we take a note, not only a time stamp
  11378. (setq org-log-note-how 'note))
  11379. ;; Set up for taking a record
  11380. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11381. org-last-state
  11382. 'findpos org-log-repeat)))
  11383. (org-back-to-heading t)
  11384. (org-add-planning-info nil nil 'closed)
  11385. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11386. org-deadline-time-regexp "\\)\\|\\("
  11387. org-ts-regexp "\\)"))
  11388. (while (re-search-forward
  11389. re (save-excursion (outline-next-heading) (point)) t)
  11390. (setq type (if (match-end 1) org-scheduled-string
  11391. (if (match-end 3) org-deadline-string "Plain:"))
  11392. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11393. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11394. (setq n (string-to-number (match-string 2 ts))
  11395. what (match-string 3 ts))
  11396. (if (equal what "w") (setq n (* n 7) what "d"))
  11397. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11398. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11399. ;; Preparation, see if we need to modify the start date for the change
  11400. (when (match-end 1)
  11401. (setq time (save-match-data (org-time-string-to-time ts)))
  11402. (cond
  11403. ((equal (match-string 1 ts) ".")
  11404. ;; Shift starting date to today
  11405. (org-timestamp-change
  11406. (- (org-today) (time-to-days time))
  11407. 'day))
  11408. ((equal (match-string 1 ts) "+")
  11409. (let ((nshiftmax 10) (nshift 0))
  11410. (while (or (= nshift 0)
  11411. (<= (time-to-days time)
  11412. (time-to-days (current-time))))
  11413. (when (= (incf nshift) nshiftmax)
  11414. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11415. (error "Abort")))
  11416. (org-timestamp-change n (cdr (assoc what whata)))
  11417. (org-at-timestamp-p t)
  11418. (setq ts (match-string 1))
  11419. (setq time (save-match-data (org-time-string-to-time ts)))))
  11420. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11421. ;; rematch, so that we have everything in place for the real shift
  11422. (org-at-timestamp-p t)
  11423. (setq ts (match-string 1))
  11424. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11425. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11426. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11427. (setq org-log-post-message msg)
  11428. (message "%s" msg))))
  11429. (defun org-show-todo-tree (arg)
  11430. "Make a compact tree which shows all headlines marked with TODO.
  11431. The tree will show the lines where the regexp matches, and all higher
  11432. headlines above the match.
  11433. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11434. With a numeric prefix N, construct a sparse tree for the Nth element
  11435. of `org-todo-keywords-1'."
  11436. (interactive "P")
  11437. (let ((case-fold-search nil)
  11438. (kwd-re
  11439. (cond ((null arg) org-not-done-regexp)
  11440. ((equal arg '(4))
  11441. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11442. (mapcar 'list org-todo-keywords-1))))
  11443. (concat "\\("
  11444. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11445. "\\)\\>")))
  11446. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11447. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11448. org-todo-keywords-1)))
  11449. (t (error "Invalid prefix argument: %s" arg)))))
  11450. (message "%d TODO entries found"
  11451. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11452. (defun org-deadline (arg &optional time)
  11453. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11454. With one universal prefix argument, remove any deadline from the item.
  11455. With two universal prefix arguments, prompt for a warning delay.
  11456. With argument TIME, set the deadline at the corresponding date. TIME
  11457. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11458. (interactive "P")
  11459. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11460. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11461. 'region-start-level 'region))
  11462. org-loop-over-headlines-in-active-region)
  11463. (org-map-entries
  11464. `(org-deadline ',arg ,time)
  11465. org-loop-over-headlines-in-active-region
  11466. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11467. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11468. (old-date-time (org-time-string-to-time old-date))
  11469. (repeater (and old-date
  11470. (string-match
  11471. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11472. old-date)
  11473. (match-string 1 old-date))))
  11474. (cond
  11475. ((equal arg '(4))
  11476. (when (and old-date org-log-redeadline)
  11477. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11478. org-log-redeadline))
  11479. (org-remove-timestamp-with-keyword org-deadline-string)
  11480. (message "Item no longer has a deadline."))
  11481. ((equal arg '(16))
  11482. (save-excursion
  11483. (if (re-search-forward
  11484. org-deadline-time-regexp
  11485. (save-excursion (outline-next-heading) (point)) t)
  11486. (let* ((rpl0 (match-string 1))
  11487. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11488. (replace-match
  11489. (concat org-deadline-string
  11490. " <" rpl
  11491. (format " -%dd"
  11492. (abs
  11493. (- (time-to-days
  11494. (save-match-data
  11495. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11496. (time-to-days old-date-time))))
  11497. ">") t t))
  11498. (user-error "No deadline information to update"))))
  11499. (t
  11500. (org-add-planning-info 'deadline time 'closed)
  11501. (when (and old-date org-log-redeadline
  11502. (not (equal old-date
  11503. (substring org-last-inserted-timestamp 1 -1))))
  11504. (org-add-log-setup 'redeadline nil old-date 'findpos
  11505. org-log-redeadline))
  11506. (when repeater
  11507. (save-excursion
  11508. (org-back-to-heading t)
  11509. (when (re-search-forward (concat org-deadline-string " "
  11510. org-last-inserted-timestamp)
  11511. (save-excursion
  11512. (outline-next-heading) (point)) t)
  11513. (goto-char (1- (match-end 0)))
  11514. (insert " " repeater)
  11515. (setq org-last-inserted-timestamp
  11516. (concat (substring org-last-inserted-timestamp 0 -1)
  11517. " " repeater
  11518. (substring org-last-inserted-timestamp -1))))))
  11519. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11520. (defun org-schedule (arg &optional time)
  11521. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11522. With one universal prefix argument, remove any scheduling date from the item.
  11523. With two universal prefix arguments, prompt for a delay cookie.
  11524. With argument TIME, scheduled at the corresponding date. TIME can
  11525. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11526. (interactive "P")
  11527. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11528. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11529. 'region-start-level 'region))
  11530. org-loop-over-headlines-in-active-region)
  11531. (org-map-entries
  11532. `(org-schedule ',arg ,time)
  11533. org-loop-over-headlines-in-active-region
  11534. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11535. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11536. (old-date-time (org-time-string-to-time old-date))
  11537. (repeater (and old-date
  11538. (string-match
  11539. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11540. old-date)
  11541. (match-string 1 old-date))))
  11542. (cond
  11543. ((equal arg '(4))
  11544. (progn
  11545. (when (and old-date org-log-reschedule)
  11546. (org-add-log-setup 'delschedule nil old-date 'findpos
  11547. org-log-reschedule))
  11548. (org-remove-timestamp-with-keyword org-scheduled-string)
  11549. (message "Item is no longer scheduled.")))
  11550. ((equal arg '(16))
  11551. (save-excursion
  11552. (if (re-search-forward
  11553. org-scheduled-time-regexp
  11554. (save-excursion (outline-next-heading) (point)) t)
  11555. (let* ((rpl0 (match-string 1))
  11556. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11557. (replace-match
  11558. (concat org-scheduled-string
  11559. " <" rpl
  11560. (format " -%dd"
  11561. (abs
  11562. (- (time-to-days
  11563. (save-match-data
  11564. (org-read-date nil t nil "Delay until" old-date-time)))
  11565. (time-to-days old-date-time))))
  11566. ">") t t))
  11567. (user-error "No scheduled information to update"))))
  11568. (t
  11569. (org-add-planning-info 'scheduled time 'closed)
  11570. (when (and old-date org-log-reschedule
  11571. (not (equal old-date
  11572. (substring org-last-inserted-timestamp 1 -1))))
  11573. (org-add-log-setup 'reschedule nil old-date 'findpos
  11574. org-log-reschedule))
  11575. (when repeater
  11576. (save-excursion
  11577. (org-back-to-heading t)
  11578. (when (re-search-forward (concat org-scheduled-string " "
  11579. org-last-inserted-timestamp)
  11580. (save-excursion
  11581. (outline-next-heading) (point)) t)
  11582. (goto-char (1- (match-end 0)))
  11583. (insert " " repeater)
  11584. (setq org-last-inserted-timestamp
  11585. (concat (substring org-last-inserted-timestamp 0 -1)
  11586. " " repeater
  11587. (substring org-last-inserted-timestamp -1))))))
  11588. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11589. (defun org-get-scheduled-time (pom &optional inherit)
  11590. "Get the scheduled time as a time tuple, of a format suitable
  11591. for calling org-schedule with, or if there is no scheduling,
  11592. returns nil."
  11593. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11594. (when time
  11595. (apply 'encode-time (org-parse-time-string time)))))
  11596. (defun org-get-deadline-time (pom &optional inherit)
  11597. "Get the deadline as a time tuple, of a format suitable for
  11598. calling org-deadline with, or if there is no scheduling, returns
  11599. nil."
  11600. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11601. (when time
  11602. (apply 'encode-time (org-parse-time-string time)))))
  11603. (defun org-remove-timestamp-with-keyword (keyword)
  11604. "Remove all time stamps with KEYWORD in the current entry."
  11605. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11606. beg)
  11607. (save-excursion
  11608. (org-back-to-heading t)
  11609. (setq beg (point))
  11610. (outline-next-heading)
  11611. (while (re-search-backward re beg t)
  11612. (replace-match "")
  11613. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11614. (equal (char-before) ?\ ))
  11615. (backward-delete-char 1)
  11616. (if (string-match "^[ \t]*$" (buffer-substring
  11617. (point-at-bol) (point-at-eol)))
  11618. (delete-region (point-at-bol)
  11619. (min (point-max) (1+ (point-at-eol))))))))))
  11620. (defun org-add-planning-info (what &optional time &rest remove)
  11621. "Insert new timestamp with keyword in the line directly after the headline.
  11622. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11623. If non is given, the user is prompted for a date.
  11624. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11625. be removed."
  11626. (interactive)
  11627. (let (org-time-was-given org-end-time-was-given ts
  11628. end default-time default-input)
  11629. (catch 'exit
  11630. (when (and (memq what '(scheduled deadline))
  11631. (or (not time)
  11632. (and (stringp time)
  11633. (string-match "^[-+]+[0-9]" time))))
  11634. ;; Try to get a default date/time from existing timestamp
  11635. (save-excursion
  11636. (org-back-to-heading t)
  11637. (setq end (save-excursion (outline-next-heading) (point)))
  11638. (when (re-search-forward (if (eq what 'scheduled)
  11639. org-scheduled-time-regexp
  11640. org-deadline-time-regexp)
  11641. end t)
  11642. (setq ts (match-string 1)
  11643. default-time
  11644. (apply 'encode-time (org-parse-time-string ts))
  11645. default-input (and ts (org-get-compact-tod ts))))))
  11646. (when what
  11647. (setq time
  11648. (if (stringp time)
  11649. ;; This is a string (relative or absolute), set proper date
  11650. (apply 'encode-time
  11651. (org-read-date-analyze
  11652. time default-time (decode-time default-time)))
  11653. ;; If necessary, get the time from the user
  11654. (or time (org-read-date nil 'to-time nil nil
  11655. default-time default-input)))))
  11656. (when (and org-insert-labeled-timestamps-at-point
  11657. (member what '(scheduled deadline)))
  11658. (insert
  11659. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11660. (org-insert-time-stamp time org-time-was-given
  11661. nil nil nil (list org-end-time-was-given))
  11662. (setq what nil))
  11663. (save-excursion
  11664. (save-restriction
  11665. (let (col list elt ts buffer-invisibility-spec)
  11666. (org-back-to-heading t)
  11667. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11668. (goto-char (match-end 1))
  11669. (setq col (current-column))
  11670. (goto-char (match-end 0))
  11671. (if (eobp) (insert "\n") (forward-char 1))
  11672. (when (and (not what)
  11673. (not (looking-at
  11674. (concat "[ \t]*"
  11675. org-keyword-time-not-clock-regexp))))
  11676. ;; Nothing to add, nothing to remove...... :-)
  11677. (throw 'exit nil))
  11678. (if (and (not (looking-at org-outline-regexp))
  11679. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11680. "[^\r\n]*"))
  11681. (not (equal (match-string 1) org-clock-string)))
  11682. (narrow-to-region (match-beginning 0) (match-end 0))
  11683. (insert-before-markers "\n")
  11684. (backward-char 1)
  11685. (narrow-to-region (point) (point))
  11686. (and org-adapt-indentation (org-indent-to-column col)))
  11687. ;; Check if we have to remove something.
  11688. (setq list (cons what remove))
  11689. (while list
  11690. (setq elt (pop list))
  11691. (when (or (and (eq elt 'scheduled)
  11692. (re-search-forward org-scheduled-time-regexp nil t))
  11693. (and (eq elt 'deadline)
  11694. (re-search-forward org-deadline-time-regexp nil t))
  11695. (and (eq elt 'closed)
  11696. (re-search-forward org-closed-time-regexp nil t)))
  11697. (replace-match "")
  11698. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11699. (and (looking-at "[ \t]+") (replace-match ""))
  11700. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11701. (when what
  11702. (insert
  11703. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11704. (cond ((eq what 'scheduled) org-scheduled-string)
  11705. ((eq what 'deadline) org-deadline-string)
  11706. ((eq what 'closed) org-closed-string))
  11707. " ")
  11708. (setq ts (org-insert-time-stamp
  11709. time
  11710. (or org-time-was-given
  11711. (and (eq what 'closed) org-log-done-with-time))
  11712. (eq what 'closed)
  11713. nil nil (list org-end-time-was-given)))
  11714. (insert
  11715. (if (not (or (bolp) (eq (char-before) ?\ )
  11716. (memq (char-after) '(32 10))
  11717. (eobp))) " " ""))
  11718. (end-of-line 1))
  11719. (goto-char (point-min))
  11720. (widen)
  11721. (if (and (looking-at "[ \t]*\n")
  11722. (equal (char-before) ?\n))
  11723. (delete-region (1- (point)) (point-at-eol)))
  11724. ts))))))
  11725. (defvar org-log-note-marker (make-marker))
  11726. (defvar org-log-note-purpose nil)
  11727. (defvar org-log-note-state nil)
  11728. (defvar org-log-note-previous-state nil)
  11729. (defvar org-log-note-how nil)
  11730. (defvar org-log-note-extra nil)
  11731. (defvar org-log-note-window-configuration nil)
  11732. (defvar org-log-note-return-to (make-marker))
  11733. (defvar org-log-note-effective-time nil
  11734. "Remembered current time so that dynamically scoped
  11735. `org-extend-today-until' affects tha timestamps in state change
  11736. log")
  11737. (defvar org-log-post-message nil
  11738. "Message to be displayed after a log note has been stored.
  11739. The auto-repeater uses this.")
  11740. (defun org-add-note ()
  11741. "Add a note to the current entry.
  11742. This is done in the same way as adding a state change note."
  11743. (interactive)
  11744. (org-add-log-setup 'note nil nil 'findpos nil))
  11745. (defvar org-property-end-re)
  11746. (defun org-add-log-setup (&optional purpose state prev-state
  11747. findpos how extra)
  11748. "Set up the post command hook to take a note.
  11749. If this is about to TODO state change, the new state is expected in STATE.
  11750. When FINDPOS is non-nil, find the correct position for the note in
  11751. the current entry. If not, assume that it can be inserted at point.
  11752. HOW is an indicator what kind of note should be created.
  11753. EXTRA is additional text that will be inserted into the notes buffer."
  11754. (let* ((org-log-into-drawer (org-log-into-drawer))
  11755. (drawer (cond ((stringp org-log-into-drawer)
  11756. org-log-into-drawer)
  11757. (org-log-into-drawer "LOGBOOK"))))
  11758. (save-restriction
  11759. (save-excursion
  11760. (when findpos
  11761. (org-back-to-heading t)
  11762. (narrow-to-region (point) (save-excursion
  11763. (outline-next-heading) (point)))
  11764. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11765. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11766. "[^\r\n]*\\)?"))
  11767. (goto-char (match-end 0))
  11768. (cond
  11769. (drawer
  11770. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11771. nil t)
  11772. (progn
  11773. (goto-char (match-end 0))
  11774. (or org-log-states-order-reversed
  11775. (and (re-search-forward org-property-end-re nil t)
  11776. (goto-char (1- (match-beginning 0))))))
  11777. (insert "\n:" drawer ":\n:END:")
  11778. (beginning-of-line 0)
  11779. (org-indent-line)
  11780. (beginning-of-line 2)
  11781. (org-indent-line)
  11782. (end-of-line 0)))
  11783. ((and org-log-state-notes-insert-after-drawers
  11784. (save-excursion
  11785. (forward-line) (looking-at org-drawer-regexp)))
  11786. (forward-line)
  11787. (while (looking-at org-drawer-regexp)
  11788. (goto-char (match-end 0))
  11789. (re-search-forward org-property-end-re (point-max) t)
  11790. (forward-line))
  11791. (forward-line -1)))
  11792. (unless org-log-states-order-reversed
  11793. (and (= (char-after) ?\n) (forward-char 1))
  11794. (org-skip-over-state-notes)
  11795. (skip-chars-backward " \t\n\r")))
  11796. (move-marker org-log-note-marker (point))
  11797. (setq org-log-note-purpose purpose
  11798. org-log-note-state state
  11799. org-log-note-previous-state prev-state
  11800. org-log-note-how how
  11801. org-log-note-extra extra
  11802. org-log-note-effective-time (org-current-effective-time))
  11803. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11804. (defun org-skip-over-state-notes ()
  11805. "Skip past the list of State notes in an entry."
  11806. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11807. (when (ignore-errors (goto-char (org-in-item-p)))
  11808. (let* ((struct (org-list-struct))
  11809. (prevs (org-list-prevs-alist struct)))
  11810. (while (looking-at "[ \t]*- State")
  11811. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11812. (org-list-get-item-end (point) struct)))))))
  11813. (defun org-add-log-note (&optional purpose)
  11814. "Pop up a window for taking a note, and add this note later at point."
  11815. (remove-hook 'post-command-hook 'org-add-log-note)
  11816. (setq org-log-note-window-configuration (current-window-configuration))
  11817. (delete-other-windows)
  11818. (move-marker org-log-note-return-to (point))
  11819. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11820. (goto-char org-log-note-marker)
  11821. (org-switch-to-buffer-other-window "*Org Note*")
  11822. (erase-buffer)
  11823. (if (memq org-log-note-how '(time state))
  11824. (let (current-prefix-arg) (org-store-log-note))
  11825. (let ((org-inhibit-startup t)) (org-mode))
  11826. (insert (format "# Insert note for %s.
  11827. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11828. (cond
  11829. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11830. ((eq org-log-note-purpose 'done) "closed todo item")
  11831. ((eq org-log-note-purpose 'state)
  11832. (format "state change from \"%s\" to \"%s\""
  11833. (or org-log-note-previous-state "")
  11834. (or org-log-note-state "")))
  11835. ((eq org-log-note-purpose 'reschedule)
  11836. "rescheduling")
  11837. ((eq org-log-note-purpose 'delschedule)
  11838. "no longer scheduled")
  11839. ((eq org-log-note-purpose 'redeadline)
  11840. "changing deadline")
  11841. ((eq org-log-note-purpose 'deldeadline)
  11842. "removing deadline")
  11843. ((eq org-log-note-purpose 'refile)
  11844. "refiling")
  11845. ((eq org-log-note-purpose 'note)
  11846. "this entry")
  11847. (t (error "This should not happen")))))
  11848. (if org-log-note-extra (insert org-log-note-extra))
  11849. (org-set-local 'org-finish-function 'org-store-log-note)
  11850. (run-hooks 'org-log-buffer-setup-hook)))
  11851. (defvar org-note-abort nil) ; dynamically scoped
  11852. (defun org-store-log-note ()
  11853. "Finish taking a log note, and insert it to where it belongs."
  11854. (let ((txt (buffer-string)))
  11855. (kill-buffer (current-buffer))
  11856. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11857. lines ind bul)
  11858. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11859. (setq txt (replace-match "" t t txt)))
  11860. (if (string-match "\\s-+\\'" txt)
  11861. (setq txt (replace-match "" t t txt)))
  11862. (setq lines (org-split-string txt "\n"))
  11863. (when (and note (string-match "\\S-" note))
  11864. (setq note
  11865. (org-replace-escapes
  11866. note
  11867. (list (cons "%u" (user-login-name))
  11868. (cons "%U" user-full-name)
  11869. (cons "%t" (format-time-string
  11870. (org-time-stamp-format 'long 'inactive)
  11871. org-log-note-effective-time))
  11872. (cons "%T" (format-time-string
  11873. (org-time-stamp-format 'long nil)
  11874. org-log-note-effective-time))
  11875. (cons "%d" (format-time-string
  11876. (org-time-stamp-format nil 'inactive)
  11877. org-log-note-effective-time))
  11878. (cons "%D" (format-time-string
  11879. (org-time-stamp-format nil nil)
  11880. org-log-note-effective-time))
  11881. (cons "%s" (if org-log-note-state
  11882. (concat "\"" org-log-note-state "\"")
  11883. ""))
  11884. (cons "%S" (if org-log-note-previous-state
  11885. (concat "\"" org-log-note-previous-state "\"")
  11886. "\"\"")))))
  11887. (if lines (setq note (concat note " \\\\")))
  11888. (push note lines))
  11889. (when (or current-prefix-arg org-note-abort)
  11890. (when org-log-into-drawer
  11891. (org-remove-empty-drawer-at
  11892. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11893. org-log-note-marker))
  11894. (setq lines nil))
  11895. (when lines
  11896. (with-current-buffer (marker-buffer org-log-note-marker)
  11897. (save-excursion
  11898. (goto-char org-log-note-marker)
  11899. (move-marker org-log-note-marker nil)
  11900. (end-of-line 1)
  11901. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11902. (setq ind (save-excursion
  11903. (if (ignore-errors (goto-char (org-in-item-p)))
  11904. (let ((struct (org-list-struct)))
  11905. (org-list-get-ind
  11906. (org-list-get-top-point struct) struct))
  11907. (skip-chars-backward " \r\t\n")
  11908. (cond
  11909. ((and (org-at-heading-p)
  11910. org-adapt-indentation)
  11911. (1+ (org-current-level)))
  11912. ((org-at-heading-p) 0)
  11913. (t (org-get-indentation))))))
  11914. (setq bul (org-list-bullet-string "-"))
  11915. (org-indent-line-to ind)
  11916. (insert bul (pop lines))
  11917. (let ((ind-body (+ (length bul) ind)))
  11918. (while lines
  11919. (insert "\n")
  11920. (org-indent-line-to ind-body)
  11921. (insert (pop lines))))
  11922. (message "Note stored")
  11923. (org-back-to-heading t)
  11924. (org-cycle-hide-drawers 'children))))))
  11925. (set-window-configuration org-log-note-window-configuration)
  11926. (with-current-buffer (marker-buffer org-log-note-return-to)
  11927. (goto-char org-log-note-return-to))
  11928. (move-marker org-log-note-return-to nil)
  11929. (and org-log-post-message (message "%s" org-log-post-message)))
  11930. (defun org-remove-empty-drawer-at (drawer pos)
  11931. "Remove an empty drawer DRAWER at position POS.
  11932. POS may also be a marker."
  11933. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11934. (save-excursion
  11935. (save-restriction
  11936. (widen)
  11937. (goto-char pos)
  11938. (if (org-in-regexp
  11939. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11940. (replace-match ""))))))
  11941. (defvar org-ts-type nil)
  11942. (defun org-sparse-tree (&optional arg type)
  11943. "Create a sparse tree, prompt for the details.
  11944. This command can create sparse trees. You first need to select the type
  11945. of match used to create the tree:
  11946. t Show all TODO entries.
  11947. T Show entries with a specific TODO keyword.
  11948. m Show entries selected by a tags/property match.
  11949. p Enter a property name and its value (both with completion on existing
  11950. names/values) and show entries with that property.
  11951. r Show entries matching a regular expression (`/' can be used as well).
  11952. b Show deadlines and scheduled items before a date.
  11953. a Show deadlines and scheduled items after a date.
  11954. d Show deadlines due within `org-deadline-warning-days'.
  11955. D Show deadlines and scheduled items between a date range."
  11956. (interactive "P")
  11957. (let (ans kwd value ts-type)
  11958. (setq type (or type org-sparse-tree-default-date-type))
  11959. (setq org-ts-type type)
  11960. (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"
  11961. (cond ((eq type 'all) "all timestamps")
  11962. ((eq type 'scheduled) "only scheduled")
  11963. ((eq type 'deadline) "only deadline")
  11964. ((eq type 'active) "only active timestamps")
  11965. ((eq type 'inactive) "only inactive timestamps")
  11966. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11967. (t "scheduled/deadline")))
  11968. (setq ans (read-char-exclusive))
  11969. (cond
  11970. ((equal ans ?c)
  11971. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11972. ((equal ans ?d)
  11973. (call-interactively 'org-check-deadlines))
  11974. ((equal ans ?b)
  11975. (call-interactively 'org-check-before-date))
  11976. ((equal ans ?a)
  11977. (call-interactively 'org-check-after-date))
  11978. ((equal ans ?D)
  11979. (call-interactively 'org-check-dates-range))
  11980. ((equal ans ?t)
  11981. (call-interactively 'org-show-todo-tree))
  11982. ((equal ans ?T)
  11983. (org-show-todo-tree '(4)))
  11984. ((member ans '(?T ?m))
  11985. (call-interactively 'org-match-sparse-tree))
  11986. ((member ans '(?p ?P))
  11987. (setq kwd (org-icompleting-read "Property: "
  11988. (mapcar 'list (org-buffer-property-keys))))
  11989. (setq value (org-icompleting-read "Value: "
  11990. (mapcar 'list (org-property-values kwd))))
  11991. (unless (string-match "\\`{.*}\\'" value)
  11992. (setq value (concat "\"" value "\"")))
  11993. (org-match-sparse-tree arg (concat kwd "=" value)))
  11994. ((member ans '(?r ?R ?/))
  11995. (call-interactively 'org-occur))
  11996. (t (error "No such sparse tree command \"%c\"" ans)))))
  11997. (defvar org-occur-highlights nil
  11998. "List of overlays used for occur matches.")
  11999. (make-variable-buffer-local 'org-occur-highlights)
  12000. (defvar org-occur-parameters nil
  12001. "Parameters of the active org-occur calls.
  12002. This is a list, each call to org-occur pushes as cons cell,
  12003. containing the regular expression and the callback, onto the list.
  12004. The list can contain several entries if `org-occur' has been called
  12005. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12006. will only contain one set of parameters. When the highlights are
  12007. removed (for example with `C-c C-c', or with the next edit (depending
  12008. on `org-remove-highlights-with-change'), this variable is emptied
  12009. as well.")
  12010. (make-variable-buffer-local 'org-occur-parameters)
  12011. (defun org-occur (regexp &optional keep-previous callback)
  12012. "Make a compact tree which shows all matches of REGEXP.
  12013. The tree will show the lines where the regexp matches, and all higher
  12014. headlines above the match. It will also show the heading after the match,
  12015. to make sure editing the matching entry is easy.
  12016. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12017. call to `org-occur' will be kept, to allow stacking of calls to this
  12018. command.
  12019. If CALLBACK is non-nil, it is a function which is called to confirm
  12020. that the match should indeed be shown."
  12021. (interactive "sRegexp: \nP")
  12022. (when (equal regexp "")
  12023. (error "Regexp cannot be empty"))
  12024. (unless keep-previous
  12025. (org-remove-occur-highlights nil nil t))
  12026. (push (cons regexp callback) org-occur-parameters)
  12027. (let ((cnt 0))
  12028. (save-excursion
  12029. (goto-char (point-min))
  12030. (if (or (not keep-previous) ; do not want to keep
  12031. (not org-occur-highlights)) ; no previous matches
  12032. ;; hide everything
  12033. (org-overview))
  12034. (while (re-search-forward regexp nil t)
  12035. (when (or (not callback)
  12036. (save-match-data (funcall callback)))
  12037. (setq cnt (1+ cnt))
  12038. (when org-highlight-sparse-tree-matches
  12039. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12040. (org-show-context 'occur-tree))))
  12041. (when org-remove-highlights-with-change
  12042. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12043. nil 'local))
  12044. (unless org-sparse-tree-open-archived-trees
  12045. (org-hide-archived-subtrees (point-min) (point-max)))
  12046. (run-hooks 'org-occur-hook)
  12047. (if (org-called-interactively-p 'interactive)
  12048. (message "%d match(es) for regexp %s" cnt regexp))
  12049. cnt))
  12050. (defun org-occur-next-match (&optional n reset)
  12051. "Function for `next-error-function' to find sparse tree matches.
  12052. N is the number of matches to move, when negative move backwards.
  12053. RESET is entirely ignored - this function always goes back to the
  12054. starting point when no match is found."
  12055. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12056. (search-func (if (< n 0)
  12057. 'previous-single-char-property-change
  12058. 'next-single-char-property-change))
  12059. (n (abs n))
  12060. (pos (point))
  12061. p1)
  12062. (catch 'exit
  12063. (while (setq p1 (funcall search-func (point) 'org-type))
  12064. (when (equal p1 limit)
  12065. (goto-char pos)
  12066. (error "No more matches"))
  12067. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12068. (setq n (1- n))
  12069. (when (= n 0)
  12070. (goto-char p1)
  12071. (throw 'exit (point))))
  12072. (goto-char p1))
  12073. (goto-char p1)
  12074. (error "No more matches"))))
  12075. (defun org-show-context (&optional key)
  12076. "Make sure point and context are visible.
  12077. How much context is shown depends upon the variables
  12078. `org-show-hierarchy-above', `org-show-following-heading',
  12079. `org-show-entry-below' and `org-show-siblings'."
  12080. (let ((heading-p (org-at-heading-p t))
  12081. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12082. (following-p (org-get-alist-option org-show-following-heading key))
  12083. (entry-p (org-get-alist-option org-show-entry-below key))
  12084. (siblings-p (org-get-alist-option org-show-siblings key)))
  12085. ;; Show heading or entry text
  12086. (if (and heading-p (not entry-p))
  12087. (org-flag-heading nil) ; only show the heading
  12088. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12089. (org-show-hidden-entry))) ; show entire entry
  12090. (when following-p
  12091. ;; Show next sibling, or heading below text
  12092. (save-excursion
  12093. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12094. (org-flag-heading nil))))
  12095. (when siblings-p (org-show-siblings))
  12096. (when hierarchy-p
  12097. ;; show all higher headings, possibly with siblings
  12098. (save-excursion
  12099. (while (and (condition-case nil
  12100. (progn (org-up-heading-all 1) t)
  12101. (error nil))
  12102. (not (bobp)))
  12103. (org-flag-heading nil)
  12104. (when siblings-p (org-show-siblings)))))
  12105. (org-fix-ellipsis-at-bol)))
  12106. (defvar org-reveal-start-hook nil
  12107. "Hook run before revealing a location.")
  12108. (defun org-reveal (&optional siblings)
  12109. "Show current entry, hierarchy above it, and the following headline.
  12110. This can be used to show a consistent set of context around locations
  12111. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12112. not t for the search context.
  12113. With optional argument SIBLINGS, on each level of the hierarchy all
  12114. siblings are shown. This repairs the tree structure to what it would
  12115. look like when opened with hierarchical calls to `org-cycle'.
  12116. With double optional argument \\[universal-argument] \\[universal-argument], \
  12117. go to the parent and show the
  12118. entire tree."
  12119. (interactive "P")
  12120. (run-hooks 'org-reveal-start-hook)
  12121. (let ((org-show-hierarchy-above t)
  12122. (org-show-following-heading t)
  12123. (org-show-siblings (if siblings t org-show-siblings)))
  12124. (org-show-context nil))
  12125. (when (equal siblings '(16))
  12126. (save-excursion
  12127. (when (org-up-heading-safe)
  12128. (org-show-subtree)
  12129. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12130. (defun org-highlight-new-match (beg end)
  12131. "Highlight from BEG to END and mark the highlight is an occur headline."
  12132. (let ((ov (make-overlay beg end)))
  12133. (overlay-put ov 'face 'secondary-selection)
  12134. (overlay-put ov 'org-type 'org-occur)
  12135. (push ov org-occur-highlights)))
  12136. (defun org-remove-occur-highlights (&optional beg end noremove)
  12137. "Remove the occur highlights from the buffer.
  12138. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12139. from the `before-change-functions' in the current buffer."
  12140. (interactive)
  12141. (unless org-inhibit-highlight-removal
  12142. (mapc 'delete-overlay org-occur-highlights)
  12143. (setq org-occur-highlights nil)
  12144. (setq org-occur-parameters nil)
  12145. (unless noremove
  12146. (remove-hook 'before-change-functions
  12147. 'org-remove-occur-highlights 'local))))
  12148. ;;;; Priorities
  12149. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12150. "Regular expression matching the priority indicator.")
  12151. (defvar org-remove-priority-next-time nil)
  12152. (defun org-priority-up ()
  12153. "Increase the priority of the current item."
  12154. (interactive)
  12155. (org-priority 'up))
  12156. (defun org-priority-down ()
  12157. "Decrease the priority of the current item."
  12158. (interactive)
  12159. (org-priority 'down))
  12160. (defun org-priority (&optional action show)
  12161. "Change the priority of an item.
  12162. ACTION can be `set', `up', `down', or a character."
  12163. (interactive "P")
  12164. (if (equal action '(4))
  12165. (org-show-priority)
  12166. (unless org-enable-priority-commands
  12167. (error "Priority commands are disabled"))
  12168. (setq action (or action 'set))
  12169. (let (current new news have remove)
  12170. (save-excursion
  12171. (org-back-to-heading t)
  12172. (if (looking-at org-priority-regexp)
  12173. (setq current (string-to-char (match-string 2))
  12174. have t))
  12175. (cond
  12176. ((eq action 'remove)
  12177. (setq remove t new ?\ ))
  12178. ((or (eq action 'set)
  12179. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12180. (if (not (eq action 'set))
  12181. (setq new action)
  12182. (message "Priority %c-%c, SPC to remove: "
  12183. org-highest-priority org-lowest-priority)
  12184. (save-match-data
  12185. (setq new (read-char-exclusive))))
  12186. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12187. (= (upcase org-lowest-priority) org-lowest-priority))
  12188. (setq new (upcase new)))
  12189. (cond ((equal new ?\ ) (setq remove t))
  12190. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12191. (error "Priority must be between `%c' and `%c'"
  12192. org-highest-priority org-lowest-priority))))
  12193. ((eq action 'up)
  12194. (setq new (if have
  12195. (1- current) ; normal cycling
  12196. ;; last priority was empty
  12197. (if (eq last-command this-command)
  12198. org-lowest-priority ; wrap around empty to lowest
  12199. ;; default
  12200. (if org-priority-start-cycle-with-default
  12201. org-default-priority
  12202. (1- org-default-priority))))))
  12203. ((eq action 'down)
  12204. (setq new (if have
  12205. (1+ current) ; normal cycling
  12206. ;; last priority was empty
  12207. (if (eq last-command this-command)
  12208. org-highest-priority ; wrap around empty to highest
  12209. ;; default
  12210. (if org-priority-start-cycle-with-default
  12211. org-default-priority
  12212. (1+ org-default-priority))))))
  12213. (t (error "Invalid action")))
  12214. (if (or (< (upcase new) org-highest-priority)
  12215. (> (upcase new) org-lowest-priority))
  12216. (if (and (memq action '(up down))
  12217. (not have) (not (eq last-command this-command)))
  12218. ;; `new' is from default priority
  12219. (error
  12220. "The default can not be set, see `org-default-priority' why")
  12221. ;; normal cycling: `new' is beyond highest/lowest priority
  12222. ;; and is wrapped around to the empty priority
  12223. (setq remove t)))
  12224. (setq news (format "%c" new))
  12225. (if have
  12226. (if remove
  12227. (replace-match "" t t nil 1)
  12228. (replace-match news t t nil 2))
  12229. (if remove
  12230. (error "No priority cookie found in line")
  12231. (let ((case-fold-search nil))
  12232. (looking-at org-todo-line-regexp))
  12233. (if (match-end 2)
  12234. (progn
  12235. (goto-char (match-end 2))
  12236. (insert " [#" news "]"))
  12237. (goto-char (match-beginning 3))
  12238. (insert "[#" news "] "))))
  12239. (org-preserve-lc (org-set-tags nil 'align)))
  12240. (if remove
  12241. (message "Priority removed")
  12242. (message "Priority of current item set to %s" news)))))
  12243. (defun org-show-priority ()
  12244. "Show the priority of the current item.
  12245. This priority is composed of the main priority given with the [#A] cookies,
  12246. and by additional input from the age of a schedules or deadline entry."
  12247. (interactive)
  12248. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12249. (org-get-at-bol 'priority)
  12250. (save-excursion
  12251. (save-match-data
  12252. (beginning-of-line)
  12253. (and (looking-at org-heading-regexp)
  12254. (org-get-priority (match-string 0))))))))
  12255. (message "Priority is %d" (if pri pri -1000))))
  12256. (defun org-get-priority (s)
  12257. "Find priority cookie and return priority."
  12258. (save-match-data
  12259. (if (functionp org-get-priority-function)
  12260. (funcall org-get-priority-function)
  12261. (if (not (string-match org-priority-regexp s))
  12262. (* 1000 (- org-lowest-priority org-default-priority))
  12263. (* 1000 (- org-lowest-priority
  12264. (string-to-char (match-string 2 s))))))))
  12265. ;;;; Tags
  12266. (defvar org-agenda-archives-mode)
  12267. (defvar org-map-continue-from nil
  12268. "Position from where mapping should continue.
  12269. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12270. (defvar org-scanner-tags nil
  12271. "The current tag list while the tags scanner is running.")
  12272. (defvar org-trust-scanner-tags nil
  12273. "Should `org-get-tags-at' use the tags for the scanner.
  12274. This is for internal dynamical scoping only.
  12275. When this is non-nil, the function `org-get-tags-at' will return the value
  12276. of `org-scanner-tags' instead of building the list by itself. This
  12277. can lead to large speed-ups when the tags scanner is used in a file with
  12278. many entries, and when the list of tags is retrieved, for example to
  12279. obtain a list of properties. Building the tags list for each entry in such
  12280. a file becomes an N^2 operation - but with this variable set, it scales
  12281. as N.")
  12282. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12283. "Sca headline tags with inheritance and produce output ACTION.
  12284. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12285. or `agenda' to produce an entry list for an agenda view. It can also be
  12286. a Lisp form or a function that should be called at each matched headline, in
  12287. this case the return value is a list of all return values from these calls.
  12288. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12289. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12290. only lines with a not-done TODO keyword are included in the output.
  12291. This should be the same variable that was scoped into
  12292. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12293. START-LEVEL can be a string with asterisks, reducing the scope to
  12294. headlines matching this string."
  12295. (require 'org-agenda)
  12296. (let* ((re (concat "^"
  12297. (if start-level
  12298. ;; Get the correct level to match
  12299. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12300. org-outline-regexp)
  12301. " *\\(\\<\\("
  12302. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12303. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12304. (props (list 'face 'default
  12305. 'done-face 'org-agenda-done
  12306. 'undone-face 'default
  12307. 'mouse-face 'highlight
  12308. 'org-not-done-regexp org-not-done-regexp
  12309. 'org-todo-regexp org-todo-regexp
  12310. 'org-complex-heading-regexp org-complex-heading-regexp
  12311. 'help-echo
  12312. (format "mouse-2 or RET jump to org file %s"
  12313. (abbreviate-file-name
  12314. (or (buffer-file-name (buffer-base-buffer))
  12315. (buffer-name (buffer-base-buffer)))))))
  12316. (case-fold-search nil)
  12317. (org-map-continue-from nil)
  12318. lspos tags tags-list
  12319. (tags-alist (list (cons 0 org-file-tags)))
  12320. (llast 0) rtn rtn1 level category i txt
  12321. todo marker entry priority)
  12322. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12323. (setq action (list 'lambda nil action)))
  12324. (save-excursion
  12325. (goto-char (point-min))
  12326. (when (eq action 'sparse-tree)
  12327. (org-overview)
  12328. (org-remove-occur-highlights))
  12329. (while (re-search-forward re nil t)
  12330. (setq org-map-continue-from nil)
  12331. (catch :skip
  12332. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12333. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12334. (goto-char (setq lspos (match-beginning 0)))
  12335. (setq level (org-reduced-level (org-outline-level))
  12336. category (org-get-category))
  12337. (setq i llast llast level)
  12338. ;; remove tag lists from same and sublevels
  12339. (while (>= i level)
  12340. (when (setq entry (assoc i tags-alist))
  12341. (setq tags-alist (delete entry tags-alist)))
  12342. (setq i (1- i)))
  12343. ;; add the next tags
  12344. (when tags
  12345. (setq tags (org-split-string tags ":")
  12346. tags-alist
  12347. (cons (cons level tags) tags-alist)))
  12348. ;; compile tags for current headline
  12349. (setq tags-list
  12350. (if org-use-tag-inheritance
  12351. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12352. tags)
  12353. org-scanner-tags tags-list)
  12354. (when org-use-tag-inheritance
  12355. (setcdr (car tags-alist)
  12356. (mapcar (lambda (x)
  12357. (setq x (copy-sequence x))
  12358. (org-add-prop-inherited x))
  12359. (cdar tags-alist))))
  12360. (when (and tags org-use-tag-inheritance
  12361. (or (not (eq t org-use-tag-inheritance))
  12362. org-tags-exclude-from-inheritance))
  12363. ;; selective inheritance, remove uninherited ones
  12364. (setcdr (car tags-alist)
  12365. (org-remove-uninherited-tags (cdar tags-alist))))
  12366. (when (and
  12367. ;; eval matcher only when the todo condition is OK
  12368. (and (or (not todo-only) (member todo org-not-done-keywords))
  12369. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12370. (eval matcher)))
  12371. ;; Call the skipper, but return t if it does not skip,
  12372. ;; so that the `and' form continues evaluating
  12373. (progn
  12374. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12375. t)
  12376. ;; Check if timestamps are deselecting this entry
  12377. (or (not todo-only)
  12378. (and (member todo org-not-done-keywords)
  12379. (or (not org-agenda-tags-todo-honor-ignore-options)
  12380. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12381. ;; select this headline
  12382. (cond
  12383. ((eq action 'sparse-tree)
  12384. (and org-highlight-sparse-tree-matches
  12385. (org-get-heading) (match-end 0)
  12386. (org-highlight-new-match
  12387. (match-beginning 1) (match-end 1)))
  12388. (org-show-context 'tags-tree))
  12389. ((eq action 'agenda)
  12390. (setq txt (org-agenda-format-item
  12391. ""
  12392. (concat
  12393. (if (eq org-tags-match-list-sublevels 'indented)
  12394. (make-string (1- level) ?.) "")
  12395. (org-get-heading))
  12396. level category
  12397. tags-list)
  12398. priority (org-get-priority txt))
  12399. (goto-char lspos)
  12400. (setq marker (org-agenda-new-marker))
  12401. (org-add-props txt props
  12402. 'org-marker marker 'org-hd-marker marker 'org-category category
  12403. 'todo-state todo
  12404. 'priority priority 'type "tagsmatch")
  12405. (push txt rtn))
  12406. ((functionp action)
  12407. (setq org-map-continue-from nil)
  12408. (save-excursion
  12409. (setq rtn1 (funcall action))
  12410. (push rtn1 rtn)))
  12411. (t (error "Invalid action")))
  12412. ;; if we are to skip sublevels, jump to end of subtree
  12413. (unless org-tags-match-list-sublevels
  12414. (org-end-of-subtree t)
  12415. (backward-char 1))))
  12416. ;; Get the correct position from where to continue
  12417. (if org-map-continue-from
  12418. (goto-char org-map-continue-from)
  12419. (and (= (point) lspos) (end-of-line 1)))))
  12420. (when (and (eq action 'sparse-tree)
  12421. (not org-sparse-tree-open-archived-trees))
  12422. (org-hide-archived-subtrees (point-min) (point-max)))
  12423. (nreverse rtn)))
  12424. (defun org-remove-uninherited-tags (tags)
  12425. "Remove all tags that are not inherited from the list TAGS."
  12426. (cond
  12427. ((eq org-use-tag-inheritance t)
  12428. (if org-tags-exclude-from-inheritance
  12429. (org-delete-all org-tags-exclude-from-inheritance tags)
  12430. tags))
  12431. ((not org-use-tag-inheritance) nil)
  12432. ((stringp org-use-tag-inheritance)
  12433. (delq nil (mapcar
  12434. (lambda (x)
  12435. (if (and (string-match org-use-tag-inheritance x)
  12436. (not (member x org-tags-exclude-from-inheritance)))
  12437. x nil))
  12438. tags)))
  12439. ((listp org-use-tag-inheritance)
  12440. (delq nil (mapcar
  12441. (lambda (x)
  12442. (if (member x org-use-tag-inheritance) x nil))
  12443. tags)))))
  12444. (defun org-match-sparse-tree (&optional todo-only match)
  12445. "Create a sparse tree according to tags string MATCH.
  12446. MATCH can contain positive and negative selection of tags, like
  12447. \"+WORK+URGENT-WITHBOSS\".
  12448. If optional argument TODO-ONLY is non-nil, only select lines that are
  12449. also TODO lines."
  12450. (interactive "P")
  12451. (org-agenda-prepare-buffers (list (current-buffer)))
  12452. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12453. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12454. (defvar org-cached-props nil)
  12455. (defun org-cached-entry-get (pom property)
  12456. (if (or (eq t org-use-property-inheritance)
  12457. (and (stringp org-use-property-inheritance)
  12458. (string-match org-use-property-inheritance property))
  12459. (and (listp org-use-property-inheritance)
  12460. (member property org-use-property-inheritance)))
  12461. ;; Caching is not possible, check it directly
  12462. (org-entry-get pom property 'inherit)
  12463. ;; Get all properties, so that we can do complicated checks easily
  12464. (cdr (assoc property (or org-cached-props
  12465. (setq org-cached-props
  12466. (org-entry-properties pom)))))))
  12467. (defun org-global-tags-completion-table (&optional files)
  12468. "Return the list of all tags in all agenda buffer/files.
  12469. Optional FILES argument is a list of files which can be used
  12470. instead of the agenda files."
  12471. (save-excursion
  12472. (org-uniquify
  12473. (delq nil
  12474. (apply 'append
  12475. (mapcar
  12476. (lambda (file)
  12477. (set-buffer (find-file-noselect file))
  12478. (append (org-get-buffer-tags)
  12479. (mapcar (lambda (x) (if (stringp (car-safe x))
  12480. (list (car-safe x)) nil))
  12481. org-tag-alist)))
  12482. (if (and files (car files))
  12483. files
  12484. (org-agenda-files))))))))
  12485. (defun org-make-tags-matcher (match)
  12486. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12487. The variable `todo-only' is scoped dynamically into this function.
  12488. It will be set to t if the matcher restricts matching to TODO entries,
  12489. otherwise will not be touched.
  12490. Returns a cons of the selection string MATCH and the constructed
  12491. lisp form implementing the matcher. The matcher is to be evaluated
  12492. at an Org entry, with point on the headline, and returns t if the
  12493. entry matches the selection string MATCH. The returned lisp form
  12494. references two variables with information about the entry, which
  12495. must be bound around the form's evaluation: todo, the TODO keyword
  12496. at the entry (or nil of none); and tags-list, the list of all tags
  12497. at the entry including inherited ones. Additionally, the category
  12498. of the entry (if any) must be specified as the text property
  12499. 'org-category on the headline.
  12500. See also `org-scan-tags'.
  12501. "
  12502. (declare (special todo-only))
  12503. (unless (boundp 'todo-only)
  12504. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12505. (unless match
  12506. ;; Get a new match request, with completion
  12507. (let ((org-last-tags-completion-table
  12508. (org-global-tags-completion-table)))
  12509. (setq match (org-completing-read-no-i
  12510. "Match: " 'org-tags-completion-function nil nil nil
  12511. 'org-tags-history))))
  12512. ;; Parse the string and create a lisp form
  12513. (let ((match0 match)
  12514. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12515. minus tag mm
  12516. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12517. orterms term orlist re-p str-p level-p level-op time-p
  12518. prop-p pn pv po gv rest)
  12519. (if (string-match "/+" match)
  12520. ;; match contains also a todo-matching request
  12521. (progn
  12522. (setq tagsmatch (substring match 0 (match-beginning 0))
  12523. todomatch (substring match (match-end 0)))
  12524. (if (string-match "^!" todomatch)
  12525. (setq todo-only t todomatch (substring todomatch 1)))
  12526. (if (string-match "^\\s-*$" todomatch)
  12527. (setq todomatch nil)))
  12528. ;; only matching tags
  12529. (setq tagsmatch match todomatch nil))
  12530. ;; Make the tags matcher
  12531. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12532. (setq tagsmatcher t)
  12533. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12534. (while (setq term (pop orterms))
  12535. (while (and (equal (substring term -1) "\\") orterms)
  12536. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12537. (while (string-match re term)
  12538. (setq rest (substring term (match-end 0))
  12539. minus (and (match-end 1)
  12540. (equal (match-string 1 term) "-"))
  12541. tag (save-match-data (replace-regexp-in-string
  12542. "\\\\-" "-"
  12543. (match-string 2 term)))
  12544. re-p (equal (string-to-char tag) ?{)
  12545. level-p (match-end 4)
  12546. prop-p (match-end 5)
  12547. mm (cond
  12548. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12549. (level-p
  12550. (setq level-op (org-op-to-function (match-string 3 term)))
  12551. `(,level-op level ,(string-to-number
  12552. (match-string 4 term))))
  12553. (prop-p
  12554. (setq pn (match-string 5 term)
  12555. po (match-string 6 term)
  12556. pv (match-string 7 term)
  12557. re-p (equal (string-to-char pv) ?{)
  12558. str-p (equal (string-to-char pv) ?\")
  12559. time-p (save-match-data
  12560. (string-match "^\"[[<].*[]>]\"$" pv))
  12561. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12562. (if time-p (setq pv (org-matcher-time pv)))
  12563. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12564. (cond
  12565. ((equal pn "CATEGORY")
  12566. (setq gv '(get-text-property (point) 'org-category)))
  12567. ((equal pn "TODO")
  12568. (setq gv 'todo))
  12569. (t
  12570. (setq gv `(org-cached-entry-get nil ,pn))))
  12571. (if re-p
  12572. (if (eq po 'org<>)
  12573. `(not (string-match ,pv (or ,gv "")))
  12574. `(string-match ,pv (or ,gv "")))
  12575. (if str-p
  12576. `(,po (or ,gv "") ,pv)
  12577. `(,po (string-to-number (or ,gv ""))
  12578. ,(string-to-number pv) ))))
  12579. (t `(member ,tag tags-list)))
  12580. mm (if minus (list 'not mm) mm)
  12581. term rest)
  12582. (push mm tagsmatcher))
  12583. (push (if (> (length tagsmatcher) 1)
  12584. (cons 'and tagsmatcher)
  12585. (car tagsmatcher))
  12586. orlist)
  12587. (setq tagsmatcher nil))
  12588. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12589. (setq tagsmatcher
  12590. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12591. ;; Make the todo matcher
  12592. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12593. (setq todomatcher t)
  12594. (setq orterms (org-split-string todomatch "|") orlist nil)
  12595. (while (setq term (pop orterms))
  12596. (while (string-match re term)
  12597. (setq minus (and (match-end 1)
  12598. (equal (match-string 1 term) "-"))
  12599. kwd (match-string 2 term)
  12600. re-p (equal (string-to-char kwd) ?{)
  12601. term (substring term (match-end 0))
  12602. mm (if re-p
  12603. `(string-match ,(substring kwd 1 -1) todo)
  12604. (list 'equal 'todo kwd))
  12605. mm (if minus (list 'not mm) mm))
  12606. (push mm todomatcher))
  12607. (push (if (> (length todomatcher) 1)
  12608. (cons 'and todomatcher)
  12609. (car todomatcher))
  12610. orlist)
  12611. (setq todomatcher nil))
  12612. (setq todomatcher (if (> (length orlist) 1)
  12613. (cons 'or orlist) (car orlist))))
  12614. ;; Return the string and lisp forms of the matcher
  12615. (setq matcher (if todomatcher
  12616. (list 'and tagsmatcher todomatcher)
  12617. tagsmatcher))
  12618. (when todo-only
  12619. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12620. matcher)))
  12621. (cons match0 matcher)))
  12622. (defun org-op-to-function (op &optional stringp)
  12623. "Turn an operator into the appropriate function."
  12624. (setq op
  12625. (cond
  12626. ((equal op "<" ) '(< string< org-time<))
  12627. ((equal op ">" ) '(> org-string> org-time>))
  12628. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12629. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12630. ((member op '("=" "==")) '(= string= org-time=))
  12631. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12632. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12633. (defun org<> (a b) (not (= a b)))
  12634. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12635. (defun org-string>= (a b) (not (string< a b)))
  12636. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12637. (defun org-string<> (a b) (not (string= a b)))
  12638. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12639. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12640. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12641. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12642. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12643. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12644. (defun org-2ft (s)
  12645. "Convert S to a floating point time.
  12646. If S is already a number, just return it. If it is a string, parse
  12647. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12648. (cond
  12649. ((numberp s) s)
  12650. ((stringp s)
  12651. (condition-case nil
  12652. (float-time (apply 'encode-time (org-parse-time-string s)))
  12653. (error 0.)))
  12654. (t 0.)))
  12655. (defun org-time-today ()
  12656. "Time in seconds today at 0:00.
  12657. Returns the float number of seconds since the beginning of the
  12658. epoch to the beginning of today (00:00)."
  12659. (float-time (apply 'encode-time
  12660. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12661. (defun org-matcher-time (s)
  12662. "Interpret a time comparison value."
  12663. (save-match-data
  12664. (cond
  12665. ((string= s "<now>") (float-time))
  12666. ((string= s "<today>") (org-time-today))
  12667. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12668. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12669. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12670. (+ (org-time-today)
  12671. (* (string-to-number (match-string 1 s))
  12672. (cdr (assoc (match-string 2 s)
  12673. '(("d" . 86400.0) ("w" . 604800.0)
  12674. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12675. (t (org-2ft s)))))
  12676. (defun org-match-any-p (re list)
  12677. "Does re match any element of list?"
  12678. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12679. (delq nil list))
  12680. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12681. (defvar org-tags-overlay (make-overlay 1 1))
  12682. (org-detach-overlay org-tags-overlay)
  12683. (defun org-get-local-tags-at (&optional pos)
  12684. "Get a list of tags defined in the current headline."
  12685. (org-get-tags-at pos 'local))
  12686. (defun org-get-local-tags ()
  12687. "Get a list of tags defined in the current headline."
  12688. (org-get-tags-at nil 'local))
  12689. (defun org-get-tags-at (&optional pos local)
  12690. "Get a list of all headline tags applicable at POS.
  12691. POS defaults to point. If tags are inherited, the list contains
  12692. the targets in the same sequence as the headlines appear, i.e.
  12693. the tags of the current headline come last.
  12694. When LOCAL is non-nil, only return tags from the current headline,
  12695. ignore inherited ones."
  12696. (interactive)
  12697. (if (and org-trust-scanner-tags
  12698. (or (not pos) (equal pos (point)))
  12699. (not local))
  12700. org-scanner-tags
  12701. (let (tags ltags lastpos parent)
  12702. (save-excursion
  12703. (save-restriction
  12704. (widen)
  12705. (goto-char (or pos (point)))
  12706. (save-match-data
  12707. (catch 'done
  12708. (condition-case nil
  12709. (progn
  12710. (org-back-to-heading t)
  12711. (while (not (equal lastpos (point)))
  12712. (setq lastpos (point))
  12713. (when (looking-at
  12714. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12715. (setq ltags (org-split-string
  12716. (org-match-string-no-properties 1) ":"))
  12717. (when parent
  12718. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12719. (setq tags (append
  12720. (if parent
  12721. (org-remove-uninherited-tags ltags)
  12722. ltags)
  12723. tags)))
  12724. (or org-use-tag-inheritance (throw 'done t))
  12725. (if local (throw 'done t))
  12726. (or (org-up-heading-safe) (error nil))
  12727. (setq parent t)))
  12728. (error nil)))))
  12729. (if local
  12730. tags
  12731. (reverse (delete-dups
  12732. (reverse (append
  12733. (org-remove-uninherited-tags
  12734. org-file-tags) tags)))))))))
  12735. (defun org-add-prop-inherited (s)
  12736. (add-text-properties 0 (length s) '(inherited t) s)
  12737. s)
  12738. (defun org-toggle-tag (tag &optional onoff)
  12739. "Toggle the tag TAG for the current line.
  12740. If ONOFF is `on' or `off', don't toggle but set to this state."
  12741. (let (res current)
  12742. (save-excursion
  12743. (org-back-to-heading t)
  12744. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12745. (point-at-eol) t)
  12746. (progn
  12747. (setq current (match-string 1))
  12748. (replace-match ""))
  12749. (setq current ""))
  12750. (setq current (nreverse (org-split-string current ":")))
  12751. (cond
  12752. ((eq onoff 'on)
  12753. (setq res t)
  12754. (or (member tag current) (push tag current)))
  12755. ((eq onoff 'off)
  12756. (or (not (member tag current)) (setq current (delete tag current))))
  12757. (t (if (member tag current)
  12758. (setq current (delete tag current))
  12759. (setq res t)
  12760. (push tag current))))
  12761. (end-of-line 1)
  12762. (if current
  12763. (progn
  12764. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12765. (org-set-tags nil t))
  12766. (delete-horizontal-space))
  12767. (run-hooks 'org-after-tags-change-hook))
  12768. res))
  12769. (defun org-align-tags-here (to-col)
  12770. ;; Assumes that this is a headline
  12771. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12772. (beginning-of-line 1)
  12773. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12774. (< pos (match-beginning 2)))
  12775. (progn
  12776. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12777. (goto-char (match-beginning 1))
  12778. (insert " ")
  12779. (delete-region (point) (1+ (match-beginning 2)))
  12780. (setq ncol (max (current-column)
  12781. (1+ col)
  12782. (if (> to-col 0)
  12783. to-col
  12784. (- (abs to-col) tags-l))))
  12785. (setq p (point))
  12786. (insert (make-string (- ncol (current-column)) ?\ ))
  12787. (setq ncol (current-column))
  12788. (when indent-tabs-mode (tabify p (point-at-eol)))
  12789. (org-move-to-column (min ncol col) t))
  12790. (goto-char pos))))
  12791. (defun org-set-tags-command (&optional arg just-align)
  12792. "Call the set-tags command for the current entry."
  12793. (interactive "P")
  12794. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12795. (org-set-tags arg just-align)
  12796. (save-excursion
  12797. (unless (and (org-region-active-p)
  12798. org-loop-over-headlines-in-active-region)
  12799. (org-back-to-heading t))
  12800. (org-set-tags arg just-align))))
  12801. (defun org-set-tags-to (data)
  12802. "Set the tags of the current entry to DATA, replacing the current tags.
  12803. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12804. If DATA is nil or the empty string, any tags will be removed."
  12805. (interactive "sTags: ")
  12806. (setq data
  12807. (cond
  12808. ((eq data nil) "")
  12809. ((equal data "") "")
  12810. ((stringp data)
  12811. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12812. ":"))
  12813. ((listp data)
  12814. (concat ":" (mapconcat 'identity data ":") ":"))))
  12815. (when data
  12816. (save-excursion
  12817. (org-back-to-heading t)
  12818. (when (looking-at org-complex-heading-regexp)
  12819. (if (match-end 5)
  12820. (progn
  12821. (goto-char (match-beginning 5))
  12822. (insert data)
  12823. (delete-region (point) (point-at-eol))
  12824. (org-set-tags nil 'align))
  12825. (goto-char (point-at-eol))
  12826. (insert " " data)
  12827. (org-set-tags nil 'align)))
  12828. (beginning-of-line 1)
  12829. (if (looking-at ".*?\\([ \t]+\\)$")
  12830. (delete-region (match-beginning 1) (match-end 1))))))
  12831. (defun org-align-all-tags ()
  12832. "Align the tags i all headings."
  12833. (interactive)
  12834. (save-excursion
  12835. (or (ignore-errors (org-back-to-heading t))
  12836. (outline-next-heading))
  12837. (if (org-at-heading-p)
  12838. (org-set-tags t)
  12839. (message "No headings"))))
  12840. (defvar org-indent-indentation-per-level)
  12841. (defun org-set-tags (&optional arg just-align)
  12842. "Set the tags for the current headline.
  12843. With prefix ARG, realign all tags in headings in the current buffer."
  12844. (interactive "P")
  12845. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12846. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12847. 'region-start-level 'region))
  12848. org-loop-over-headlines-in-active-region)
  12849. (org-map-entries
  12850. ;; We don't use ARG and JUST-ALIGN here these args are not
  12851. ;; useful when looping over headlines
  12852. `(org-set-tags)
  12853. org-loop-over-headlines-in-active-region
  12854. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12855. (let* ((re org-outline-regexp-bol)
  12856. (current (unless arg (org-get-tags-string)))
  12857. (col (current-column))
  12858. (org-setting-tags t)
  12859. table current-tags inherited-tags ; computed below when needed
  12860. tags p0 c0 c1 rpl di tc level)
  12861. (if arg
  12862. (save-excursion
  12863. (goto-char (point-min))
  12864. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12865. (while (re-search-forward re nil t)
  12866. (org-set-tags nil t)
  12867. (end-of-line 1)))
  12868. (message "All tags realigned to column %d" org-tags-column))
  12869. (if just-align
  12870. (setq tags current)
  12871. ;; Get a new set of tags from the user
  12872. (save-excursion
  12873. (setq table (append org-tag-persistent-alist
  12874. (or org-tag-alist (org-get-buffer-tags))
  12875. (and
  12876. org-complete-tags-always-offer-all-agenda-tags
  12877. (org-global-tags-completion-table
  12878. (org-agenda-files))))
  12879. org-last-tags-completion-table table
  12880. current-tags (org-split-string current ":")
  12881. inherited-tags (nreverse
  12882. (nthcdr (length current-tags)
  12883. (nreverse (org-get-tags-at))))
  12884. tags
  12885. (if (or (eq t org-use-fast-tag-selection)
  12886. (and org-use-fast-tag-selection
  12887. (delq nil (mapcar 'cdr table))))
  12888. (org-fast-tag-selection
  12889. current-tags inherited-tags table
  12890. (if org-fast-tag-selection-include-todo
  12891. org-todo-key-alist))
  12892. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12893. (org-trim
  12894. (org-icompleting-read "Tags: "
  12895. 'org-tags-completion-function
  12896. nil nil current 'org-tags-history))))))
  12897. (while (string-match "[-+&]+" tags)
  12898. ;; No boolean logic, just a list
  12899. (setq tags (replace-match ":" t t tags))))
  12900. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12901. (if org-tags-sort-function
  12902. (setq tags (mapconcat 'identity
  12903. (sort (org-split-string
  12904. tags (org-re "[^[:alnum:]_@#%]+"))
  12905. org-tags-sort-function) ":")))
  12906. (if (string-match "\\`[\t ]*\\'" tags)
  12907. (setq tags "")
  12908. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12909. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12910. ;; Insert new tags at the correct column
  12911. (beginning-of-line 1)
  12912. (setq level (or (and (looking-at org-outline-regexp)
  12913. (- (match-end 0) (point) 1))
  12914. 1))
  12915. (cond
  12916. ((and (equal current "") (equal tags "")))
  12917. ((re-search-forward
  12918. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12919. (point-at-eol) t)
  12920. (if (equal tags "")
  12921. (setq rpl "")
  12922. (goto-char (match-beginning 0))
  12923. (setq c0 (current-column)
  12924. ;; compute offset for the case of org-indent-mode active
  12925. di (if org-indent-mode
  12926. (* (1- org-indent-indentation-per-level) (1- level))
  12927. 0)
  12928. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12929. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12930. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12931. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12932. (replace-match rpl t t)
  12933. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12934. tags)
  12935. (t (error "Tags alignment failed")))
  12936. (org-move-to-column col)
  12937. (unless just-align
  12938. (run-hooks 'org-after-tags-change-hook))))))
  12939. (defun org-change-tag-in-region (beg end tag off)
  12940. "Add or remove TAG for each entry in the region.
  12941. This works in the agenda, and also in an org-mode buffer."
  12942. (interactive
  12943. (list (region-beginning) (region-end)
  12944. (let ((org-last-tags-completion-table
  12945. (if (derived-mode-p 'org-mode)
  12946. (org-get-buffer-tags)
  12947. (org-global-tags-completion-table))))
  12948. (org-icompleting-read
  12949. "Tag: " 'org-tags-completion-function nil nil nil
  12950. 'org-tags-history))
  12951. (progn
  12952. (message "[s]et or [r]emove? ")
  12953. (equal (read-char-exclusive) ?r))))
  12954. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12955. (let ((agendap (equal major-mode 'org-agenda-mode))
  12956. l1 l2 m buf pos newhead (cnt 0))
  12957. (goto-char end)
  12958. (setq l2 (1- (org-current-line)))
  12959. (goto-char beg)
  12960. (setq l1 (org-current-line))
  12961. (loop for l from l1 to l2 do
  12962. (org-goto-line l)
  12963. (setq m (get-text-property (point) 'org-hd-marker))
  12964. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12965. (and agendap m))
  12966. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12967. pos (if agendap m (point)))
  12968. (with-current-buffer buf
  12969. (save-excursion
  12970. (save-restriction
  12971. (goto-char pos)
  12972. (setq cnt (1+ cnt))
  12973. (org-toggle-tag tag (if off 'off 'on))
  12974. (setq newhead (org-get-heading)))))
  12975. (and agendap (org-agenda-change-all-lines newhead m))))
  12976. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12977. (defun org-tags-completion-function (string predicate &optional flag)
  12978. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12979. (confirm (lambda (x) (stringp (car x)))))
  12980. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12981. (setq s1 (match-string 1 string)
  12982. s2 (match-string 2 string))
  12983. (setq s1 "" s2 string))
  12984. (cond
  12985. ((eq flag nil)
  12986. ;; try completion
  12987. (setq rtn (try-completion s2 ctable confirm))
  12988. (if (stringp rtn)
  12989. (setq rtn
  12990. (concat s1 s2 (substring rtn (length s2))
  12991. (if (and org-add-colon-after-tag-completion
  12992. (assoc rtn ctable))
  12993. ":" ""))))
  12994. rtn)
  12995. ((eq flag t)
  12996. ;; all-completions
  12997. (all-completions s2 ctable confirm)
  12998. )
  12999. ((eq flag 'lambda)
  13000. ;; exact match?
  13001. (assoc s2 ctable)))
  13002. ))
  13003. (defun org-fast-tag-insert (kwd tags face &optional end)
  13004. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  13005. (insert (format "%-12s" (concat kwd ":"))
  13006. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13007. (or end "")))
  13008. (defun org-fast-tag-show-exit (flag)
  13009. (save-excursion
  13010. (org-goto-line 3)
  13011. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13012. (replace-match ""))
  13013. (when flag
  13014. (end-of-line 1)
  13015. (org-move-to-column (- (window-width) 19) t)
  13016. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13017. (defun org-set-current-tags-overlay (current prefix)
  13018. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13019. (if (featurep 'xemacs)
  13020. (org-overlay-display org-tags-overlay (concat prefix s)
  13021. 'secondary-selection)
  13022. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13023. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13024. (defvar org-last-tag-selection-key nil)
  13025. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13026. "Fast tag selection with single keys.
  13027. CURRENT is the current list of tags in the headline, INHERITED is the
  13028. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13029. possibly with grouping information. TODO-TABLE is a similar table with
  13030. TODO keywords, should these have keys assigned to them.
  13031. If the keys are nil, a-z are automatically assigned.
  13032. Returns the new tags string, or nil to not change the current settings."
  13033. (let* ((fulltable (append table todo-table))
  13034. (maxlen (apply 'max (mapcar
  13035. (lambda (x)
  13036. (if (stringp (car x)) (string-width (car x)) 0))
  13037. fulltable)))
  13038. (buf (current-buffer))
  13039. (expert (eq org-fast-tag-selection-single-key 'expert))
  13040. (buffer-tags nil)
  13041. (fwidth (+ maxlen 3 1 3))
  13042. (ncol (/ (- (window-width) 4) fwidth))
  13043. (i-face 'org-done)
  13044. (c-face 'org-todo)
  13045. tg cnt e c char c1 c2 ntable tbl rtn
  13046. ov-start ov-end ov-prefix
  13047. (exit-after-next org-fast-tag-selection-single-key)
  13048. (done-keywords org-done-keywords)
  13049. groups ingroup)
  13050. (save-excursion
  13051. (beginning-of-line 1)
  13052. (if (looking-at
  13053. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13054. (setq ov-start (match-beginning 1)
  13055. ov-end (match-end 1)
  13056. ov-prefix "")
  13057. (setq ov-start (1- (point-at-eol))
  13058. ov-end (1+ ov-start))
  13059. (skip-chars-forward "^\n\r")
  13060. (setq ov-prefix
  13061. (concat
  13062. (buffer-substring (1- (point)) (point))
  13063. (if (> (current-column) org-tags-column)
  13064. " "
  13065. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13066. (move-overlay org-tags-overlay ov-start ov-end)
  13067. (save-window-excursion
  13068. (if expert
  13069. (set-buffer (get-buffer-create " *Org tags*"))
  13070. (delete-other-windows)
  13071. (split-window-vertically)
  13072. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13073. (erase-buffer)
  13074. (org-set-local 'org-done-keywords done-keywords)
  13075. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13076. (org-fast-tag-insert "Current" current c-face "\n\n")
  13077. (org-fast-tag-show-exit exit-after-next)
  13078. (org-set-current-tags-overlay current ov-prefix)
  13079. (setq tbl fulltable char ?a cnt 0)
  13080. (while (setq e (pop tbl))
  13081. (cond
  13082. ((equal (car e) :startgroup)
  13083. (push '() groups) (setq ingroup t)
  13084. (when (not (= cnt 0))
  13085. (setq cnt 0)
  13086. (insert "\n"))
  13087. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13088. ((equal (car e) :endgroup)
  13089. (setq ingroup nil cnt 0)
  13090. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13091. ((equal e '(:newline))
  13092. (when (not (= cnt 0))
  13093. (setq cnt 0)
  13094. (insert "\n")
  13095. (setq e (car tbl))
  13096. (while (equal (car tbl) '(:newline))
  13097. (insert "\n")
  13098. (setq tbl (cdr tbl)))))
  13099. (t
  13100. (setq tg (copy-sequence (car e)) c2 nil)
  13101. (if (cdr e)
  13102. (setq c (cdr e))
  13103. ;; automatically assign a character.
  13104. (setq c1 (string-to-char
  13105. (downcase (substring
  13106. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13107. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13108. (while (or (rassoc char ntable) (rassoc char table))
  13109. (setq char (1+ char)))
  13110. (setq c2 c1))
  13111. (setq c (or c2 char)))
  13112. (if ingroup (push tg (car groups)))
  13113. (setq tg (org-add-props tg nil 'face
  13114. (cond
  13115. ((not (assoc tg table))
  13116. (org-get-todo-face tg))
  13117. ((member tg current) c-face)
  13118. ((member tg inherited) i-face))))
  13119. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13120. (insert "[" c "] " tg (make-string
  13121. (- fwidth 4 (length tg)) ?\ ))
  13122. (push (cons tg c) ntable)
  13123. (when (= (setq cnt (1+ cnt)) ncol)
  13124. (insert "\n")
  13125. (if ingroup (insert " "))
  13126. (setq cnt 0)))))
  13127. (setq ntable (nreverse ntable))
  13128. (insert "\n")
  13129. (goto-char (point-min))
  13130. (if (not expert) (org-fit-window-to-buffer))
  13131. (setq rtn
  13132. (catch 'exit
  13133. (while t
  13134. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13135. (if (not groups) "no " "")
  13136. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13137. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13138. (setq org-last-tag-selection-key c)
  13139. (cond
  13140. ((= c ?\r) (throw 'exit t))
  13141. ((= c ?!)
  13142. (setq groups (not groups))
  13143. (goto-char (point-min))
  13144. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13145. ((= c ?\C-c)
  13146. (if (not expert)
  13147. (org-fast-tag-show-exit
  13148. (setq exit-after-next (not exit-after-next)))
  13149. (setq expert nil)
  13150. (delete-other-windows)
  13151. (set-window-buffer (split-window-vertically) " *Org tags*")
  13152. (org-switch-to-buffer-other-window " *Org tags*")
  13153. (org-fit-window-to-buffer)))
  13154. ((or (= c ?\C-g)
  13155. (and (= c ?q) (not (rassoc c ntable))))
  13156. (org-detach-overlay org-tags-overlay)
  13157. (setq quit-flag t))
  13158. ((= c ?\ )
  13159. (setq current nil)
  13160. (if exit-after-next (setq exit-after-next 'now)))
  13161. ((= c ?\t)
  13162. (condition-case nil
  13163. (setq tg (org-icompleting-read
  13164. "Tag: "
  13165. (or buffer-tags
  13166. (with-current-buffer buf
  13167. (org-get-buffer-tags)))))
  13168. (quit (setq tg "")))
  13169. (when (string-match "\\S-" tg)
  13170. (add-to-list 'buffer-tags (list tg))
  13171. (if (member tg current)
  13172. (setq current (delete tg current))
  13173. (push tg current)))
  13174. (if exit-after-next (setq exit-after-next 'now)))
  13175. ((setq e (rassoc c todo-table) tg (car e))
  13176. (with-current-buffer buf
  13177. (save-excursion (org-todo tg)))
  13178. (if exit-after-next (setq exit-after-next 'now)))
  13179. ((setq e (rassoc c ntable) tg (car e))
  13180. (if (member tg current)
  13181. (setq current (delete tg current))
  13182. (loop for g in groups do
  13183. (if (member tg g)
  13184. (mapc (lambda (x)
  13185. (setq current (delete x current)))
  13186. g)))
  13187. (push tg current))
  13188. (if exit-after-next (setq exit-after-next 'now))))
  13189. ;; Create a sorted list
  13190. (setq current
  13191. (sort current
  13192. (lambda (a b)
  13193. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13194. (if (eq exit-after-next 'now) (throw 'exit t))
  13195. (goto-char (point-min))
  13196. (beginning-of-line 2)
  13197. (delete-region (point) (point-at-eol))
  13198. (org-fast-tag-insert "Current" current c-face)
  13199. (org-set-current-tags-overlay current ov-prefix)
  13200. (while (re-search-forward
  13201. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13202. (setq tg (match-string 1))
  13203. (add-text-properties
  13204. (match-beginning 1) (match-end 1)
  13205. (list 'face
  13206. (cond
  13207. ((member tg current) c-face)
  13208. ((member tg inherited) i-face)
  13209. (t (get-text-property (match-beginning 1) 'face))))))
  13210. (goto-char (point-min)))))
  13211. (org-detach-overlay org-tags-overlay)
  13212. (if rtn
  13213. (mapconcat 'identity current ":")
  13214. nil))))
  13215. (defun org-get-tags-string ()
  13216. "Get the TAGS string in the current headline."
  13217. (unless (org-at-heading-p t)
  13218. (error "Not on a heading"))
  13219. (save-excursion
  13220. (beginning-of-line 1)
  13221. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13222. (org-match-string-no-properties 1)
  13223. "")))
  13224. (defun org-get-tags ()
  13225. "Get the list of tags specified in the current headline."
  13226. (org-split-string (org-get-tags-string) ":"))
  13227. (defun org-get-buffer-tags ()
  13228. "Get a table of all tags used in the buffer, for completion."
  13229. (let (tags)
  13230. (save-excursion
  13231. (goto-char (point-min))
  13232. (while (re-search-forward
  13233. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13234. (when (equal (char-after (point-at-bol 0)) ?*)
  13235. (mapc (lambda (x) (add-to-list 'tags x))
  13236. (org-split-string (org-match-string-no-properties 1) ":")))))
  13237. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13238. (mapcar 'list tags)))
  13239. ;;;; The mapping API
  13240. (defun org-map-entries (func &optional match scope &rest skip)
  13241. "Call FUNC at each headline selected by MATCH in SCOPE.
  13242. FUNC is a function or a lisp form. The function will be called without
  13243. arguments, with the cursor positioned at the beginning of the headline.
  13244. The return values of all calls to the function will be collected and
  13245. returned as a list.
  13246. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13247. does not need to preserve point. After evaluation, the cursor will be
  13248. moved to the end of the line (presumably of the headline of the
  13249. processed entry) and search continues from there. Under some
  13250. circumstances, this may not produce the wanted results. For example,
  13251. if you have removed (e.g. archived) the current (sub)tree it could
  13252. mean that the next entry will be skipped entirely. In such cases, you
  13253. can specify the position from where search should continue by making
  13254. FUNC set the variable `org-map-continue-from' to the desired buffer
  13255. position.
  13256. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13257. Only headlines that are matched by this query will be considered during
  13258. the iteration. When MATCH is nil or t, all headlines will be
  13259. visited by the iteration.
  13260. SCOPE determines the scope of this command. It can be any of:
  13261. nil The current buffer, respecting the restriction if any
  13262. tree The subtree started with the entry at point
  13263. region The entries within the active region, if any
  13264. region-start-level
  13265. The entries within the active region, but only those at
  13266. the same level than the first one.
  13267. file The current buffer, without restriction
  13268. file-with-archives
  13269. The current buffer, and any archives associated with it
  13270. agenda All agenda files
  13271. agenda-with-archives
  13272. All agenda files with any archive files associated with them
  13273. \(file1 file2 ...)
  13274. If this is a list, all files in the list will be scanned
  13275. The remaining args are treated as settings for the skipping facilities of
  13276. the scanner. The following items can be given here:
  13277. archive skip trees with the archive tag
  13278. comment skip trees with the COMMENT keyword
  13279. function or Emacs Lisp form:
  13280. will be used as value for `org-agenda-skip-function', so
  13281. whenever the function returns a position, FUNC will not be
  13282. called for that entry and search will continue from the
  13283. position returned
  13284. If your function needs to retrieve the tags including inherited tags
  13285. at the *current* entry, you can use the value of the variable
  13286. `org-scanner-tags' which will be much faster than getting the value
  13287. with `org-get-tags-at'. If your function gets properties with
  13288. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13289. to t around the call to `org-entry-properties' to get the same speedup.
  13290. Note that if your function moves around to retrieve tags and properties at
  13291. a *different* entry, you cannot use these techniques."
  13292. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13293. (not (org-region-active-p)))
  13294. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13295. (org-agenda-skip-archived-trees (memq 'archive skip))
  13296. (org-agenda-skip-comment-trees (memq 'comment skip))
  13297. (org-agenda-skip-function
  13298. (car (org-delete-all '(comment archive) skip)))
  13299. (org-tags-match-list-sublevels t)
  13300. (start-level (eq scope 'region-start-level))
  13301. matcher file res
  13302. org-todo-keywords-for-agenda
  13303. org-done-keywords-for-agenda
  13304. org-todo-keyword-alist-for-agenda
  13305. org-drawers-for-agenda
  13306. org-tag-alist-for-agenda
  13307. todo-only)
  13308. (cond
  13309. ((eq match t) (setq matcher t))
  13310. ((eq match nil) (setq matcher t))
  13311. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13312. (save-excursion
  13313. (save-restriction
  13314. (cond ((eq scope 'tree)
  13315. (org-back-to-heading t)
  13316. (org-narrow-to-subtree)
  13317. (setq scope nil))
  13318. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13319. (org-region-active-p))
  13320. ;; If needed, set start-level to a string like "2"
  13321. (when start-level
  13322. (save-excursion
  13323. (goto-char (region-beginning))
  13324. (unless (org-at-heading-p) (outline-next-heading))
  13325. (setq start-level (org-current-level))))
  13326. (narrow-to-region (region-beginning)
  13327. (save-excursion
  13328. (goto-char (region-end))
  13329. (unless (and (bolp) (org-at-heading-p))
  13330. (outline-next-heading))
  13331. (point)))
  13332. (setq scope nil)))
  13333. (if (not scope)
  13334. (progn
  13335. (org-agenda-prepare-buffers
  13336. (list (buffer-file-name (current-buffer))))
  13337. (setq res (org-scan-tags func matcher todo-only start-level)))
  13338. ;; Get the right scope
  13339. (cond
  13340. ((and scope (listp scope) (symbolp (car scope)))
  13341. (setq scope (eval scope)))
  13342. ((eq scope 'agenda)
  13343. (setq scope (org-agenda-files t)))
  13344. ((eq scope 'agenda-with-archives)
  13345. (setq scope (org-agenda-files t))
  13346. (setq scope (org-add-archive-files scope)))
  13347. ((eq scope 'file)
  13348. (setq scope (list (buffer-file-name))))
  13349. ((eq scope 'file-with-archives)
  13350. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13351. (org-agenda-prepare-buffers scope)
  13352. (while (setq file (pop scope))
  13353. (with-current-buffer (org-find-base-buffer-visiting file)
  13354. (save-excursion
  13355. (save-restriction
  13356. (widen)
  13357. (goto-char (point-min))
  13358. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13359. res)))
  13360. ;;;; Properties
  13361. ;;; Setting and retrieving properties
  13362. (defconst org-special-properties
  13363. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13364. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13365. "The special properties valid in Org-mode.
  13366. These are properties that are not defined in the property drawer,
  13367. but in some other way.")
  13368. (defconst org-default-properties
  13369. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13370. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13371. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13372. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13373. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13374. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13375. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13376. "Some properties that are used by Org-mode for various purposes.
  13377. Being in this list makes sure that they are offered for completion.")
  13378. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13379. "Regular expression matching the first line of a property drawer.")
  13380. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13381. "Regular expression matching the last line of a property drawer.")
  13382. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13383. "Regular expression matching the first line of a property drawer.")
  13384. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13385. "Regular expression matching the first line of a property drawer.")
  13386. (defconst org-property-drawer-re
  13387. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13388. org-property-end-re "\\)\n?")
  13389. "Matches an entire property drawer.")
  13390. (defconst org-clock-drawer-re
  13391. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13392. org-property-end-re "\\)\n?")
  13393. "Matches an entire clock drawer.")
  13394. (defsubst org-re-property (property)
  13395. "Return a regexp matching a PROPERTY line.
  13396. Match group 1 will be set to the value."
  13397. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13398. (defsubst org-re-property-keyword (property)
  13399. "Return a regexp matching a PROPERTY line, possibly with no
  13400. value for the property."
  13401. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13402. (defun org-property-action ()
  13403. "Do an action on properties."
  13404. (interactive)
  13405. (let (c)
  13406. (org-at-property-p)
  13407. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13408. (setq c (read-char-exclusive))
  13409. (cond
  13410. ((equal c ?s)
  13411. (call-interactively 'org-set-property))
  13412. ((equal c ?d)
  13413. (call-interactively 'org-delete-property))
  13414. ((equal c ?D)
  13415. (call-interactively 'org-delete-property-globally))
  13416. ((equal c ?c)
  13417. (call-interactively 'org-compute-property-at-point))
  13418. (t (error "No such property action %c" c)))))
  13419. (defun org-inc-effort ()
  13420. "Increment the value of the effort property in the current entry."
  13421. (interactive)
  13422. (org-set-effort nil t))
  13423. (defvar org-clock-effort) ;; Defined in org-clock.el
  13424. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13425. (defun org-set-effort (&optional value increment)
  13426. "Set the effort property of the current entry.
  13427. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13428. 10th allowed value.
  13429. When INCREMENT is non-nil, set the property to the next allowed value."
  13430. (interactive "P")
  13431. (if (equal value 0) (setq value 10))
  13432. (let* ((completion-ignore-case t)
  13433. (prop org-effort-property)
  13434. (cur (org-entry-get nil prop))
  13435. (allowed (org-property-get-allowed-values nil prop 'table))
  13436. (existing (mapcar 'list (org-property-values prop)))
  13437. (heading (nth 4 (org-heading-components)))
  13438. rpl
  13439. (val (cond
  13440. ((stringp value) value)
  13441. ((and allowed (integerp value))
  13442. (or (car (nth (1- value) allowed))
  13443. (car (org-last allowed))))
  13444. ((and allowed increment)
  13445. (or (caadr (member (list cur) allowed))
  13446. (error "Allowed effort values are not set")))
  13447. (allowed
  13448. (message "Select 1-9,0, [RET%s]: %s"
  13449. (if cur (concat "=" cur) "")
  13450. (mapconcat 'car allowed " "))
  13451. (setq rpl (read-char-exclusive))
  13452. (if (equal rpl ?\r)
  13453. cur
  13454. (setq rpl (- rpl ?0))
  13455. (if (equal rpl 0) (setq rpl 10))
  13456. (if (and (> rpl 0) (<= rpl (length allowed)))
  13457. (car (nth (1- rpl) allowed))
  13458. (org-completing-read "Effort: " allowed nil))))
  13459. (t
  13460. (let (org-completion-use-ido org-completion-use-iswitchb)
  13461. (org-completing-read
  13462. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13463. (concat "[" cur "]") "")
  13464. ": ")
  13465. existing nil nil "" nil cur))))))
  13466. (unless (equal (org-entry-get nil prop) val)
  13467. (org-entry-put nil prop val))
  13468. (save-excursion
  13469. (org-back-to-heading t)
  13470. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13471. (when (string= heading org-clock-current-task)
  13472. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13473. (org-clock-update-mode-line))
  13474. (message "%s is now %s" prop val)))
  13475. (defun org-at-property-p ()
  13476. "Is cursor inside a property drawer?"
  13477. (save-excursion
  13478. (beginning-of-line 1)
  13479. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13480. (save-match-data ;; Used by calling procedures
  13481. (let ((p (point))
  13482. (range (unless (org-before-first-heading-p)
  13483. (org-get-property-block))))
  13484. (and range (<= (car range) p) (< p (cdr range))))))))
  13485. (defun org-get-property-block (&optional beg end force)
  13486. "Return the (beg . end) range of the body of the property drawer.
  13487. BEG and END are the beginning and end of the current subtree, or of
  13488. the part before the first headline. If they are not given, they will
  13489. be found. If the drawer does not exist and FORCE is non-nil, create
  13490. the drawer."
  13491. (catch 'exit
  13492. (save-excursion
  13493. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13494. (progn (org-back-to-heading t) (point))))
  13495. (end (or end (and (not (outline-next-heading)) (point-max))
  13496. (point))))
  13497. (goto-char beg)
  13498. (if (re-search-forward org-property-start-re end t)
  13499. (setq beg (1+ (match-end 0)))
  13500. (if force
  13501. (save-excursion
  13502. (org-insert-property-drawer)
  13503. (setq end (progn (outline-next-heading) (point))))
  13504. (throw 'exit nil))
  13505. (goto-char beg)
  13506. (if (re-search-forward org-property-start-re end t)
  13507. (setq beg (1+ (match-end 0)))))
  13508. (if (re-search-forward org-property-end-re end t)
  13509. (setq end (match-beginning 0))
  13510. (or force (throw 'exit nil))
  13511. (goto-char beg)
  13512. (setq end beg)
  13513. (org-indent-line)
  13514. (insert ":END:\n"))
  13515. (cons beg end)))))
  13516. (defun org-entry-properties (&optional pom which specific)
  13517. "Get all properties of the entry at point-or-marker POM.
  13518. This includes the TODO keyword, the tags, time strings for deadline,
  13519. scheduled, and clocking, and any additional properties defined in the
  13520. entry. The return value is an alist, keys may occur multiple times
  13521. if the property key was used several times.
  13522. POM may also be nil, in which case the current entry is used.
  13523. If WHICH is nil or `all', get all properties. If WHICH is
  13524. `special' or `standard', only get that subclass. If WHICH
  13525. is a string only get exactly this property. SPECIFIC can be a string, the
  13526. specific property we are interested in. Specifying it can speed
  13527. things up because then unnecessary parsing is avoided."
  13528. (setq which (or which 'all))
  13529. (org-with-point-at pom
  13530. (let ((clockstr (substring org-clock-string 0 -1))
  13531. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13532. (case-fold-search nil)
  13533. beg end range props sum-props key key1 value string clocksum clocksumt)
  13534. (save-excursion
  13535. (when (condition-case nil
  13536. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13537. (error nil))
  13538. (setq beg (point))
  13539. (setq sum-props (get-text-property (point) 'org-summaries))
  13540. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13541. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13542. (outline-next-heading)
  13543. (setq end (point))
  13544. (when (memq which '(all special))
  13545. ;; Get the special properties, like TODO and tags
  13546. (goto-char beg)
  13547. (when (and (or (not specific) (string= specific "TODO"))
  13548. (looking-at org-todo-line-regexp) (match-end 2))
  13549. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13550. (when (and (or (not specific) (string= specific "PRIORITY"))
  13551. (looking-at org-priority-regexp))
  13552. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13553. (when (or (not specific) (string= specific "FILE"))
  13554. (push (cons "FILE" buffer-file-name) props))
  13555. (when (and (or (not specific) (string= specific "TAGS"))
  13556. (setq value (org-get-tags-string))
  13557. (string-match "\\S-" value))
  13558. (push (cons "TAGS" value) props))
  13559. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13560. (setq value (org-get-tags-at)))
  13561. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13562. ":"))
  13563. props))
  13564. (when (or (not specific) (string= specific "BLOCKED"))
  13565. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13566. (when (or (not specific)
  13567. (member specific
  13568. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13569. "TIMESTAMP" "TIMESTAMP_IA")))
  13570. (catch 'match
  13571. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13572. (setq key (if (match-end 1)
  13573. (substring (org-match-string-no-properties 1)
  13574. 0 -1))
  13575. string (if (equal key clockstr)
  13576. (org-trim
  13577. (buffer-substring-no-properties
  13578. (match-beginning 3) (goto-char
  13579. (point-at-eol))))
  13580. (substring (org-match-string-no-properties 3)
  13581. 1 -1)))
  13582. ;; Get the correct property name from the key. This is
  13583. ;; necessary if the user has configured time keywords.
  13584. (setq key1 (concat key ":"))
  13585. (cond
  13586. ((not key)
  13587. (setq key
  13588. (if (= (char-after (match-beginning 3)) ?\[)
  13589. "TIMESTAMP_IA" "TIMESTAMP")))
  13590. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13591. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13592. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13593. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13594. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13595. (progn
  13596. (push (cons key string) props)
  13597. ;; no need to search further if match is found
  13598. (throw 'match t))
  13599. (when (or (equal key "CLOCK") (not (assoc key props)))
  13600. (push (cons key string) props)))))))
  13601. (when (memq which '(all standard))
  13602. ;; Get the standard properties, like :PROP: ...
  13603. (setq range (org-get-property-block beg end))
  13604. (when range
  13605. (goto-char (car range))
  13606. (while (re-search-forward
  13607. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13608. (cdr range) t)
  13609. (setq key (org-match-string-no-properties 1)
  13610. value (org-trim (or (org-match-string-no-properties 2) "")))
  13611. (unless (member key excluded)
  13612. (push (cons key (or value "")) props)))))
  13613. (if clocksum
  13614. (push (cons "CLOCKSUM"
  13615. (org-columns-number-to-string (/ (float clocksum) 60.)
  13616. 'add_times))
  13617. props))
  13618. (if clocksumt
  13619. (push (cons "CLOCKSUM_T"
  13620. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13621. 'add_times))
  13622. props))
  13623. (unless (assoc "CATEGORY" props)
  13624. (push (cons "CATEGORY" (org-get-category)) props))
  13625. (append sum-props (nreverse props)))))))
  13626. (defun org-entry-get (pom property &optional inherit literal-nil)
  13627. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13628. If INHERIT is non-nil and the entry does not have the property,
  13629. then also check higher levels of the hierarchy.
  13630. If INHERIT is the symbol `selective', use inheritance only if the setting
  13631. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13632. If the property is present but empty, the return value is the empty string.
  13633. If the property is not present at all, nil is returned.
  13634. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13635. do not interpret it as the list atom nil. This is used for inheritance
  13636. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13637. (org-with-point-at pom
  13638. (if (and inherit (if (eq inherit 'selective)
  13639. (org-property-inherit-p property)
  13640. t))
  13641. (org-entry-get-with-inheritance property literal-nil)
  13642. (if (member property org-special-properties)
  13643. ;; We need a special property. Use `org-entry-properties' to
  13644. ;; retrieve it, but specify the wanted property
  13645. (cdr (assoc property (org-entry-properties nil 'special property)))
  13646. (let ((range (org-get-property-block)))
  13647. (when (and range (not (eq (car range) (cdr range))))
  13648. (let* ((props (list (or (assoc property org-file-properties)
  13649. (assoc property org-global-properties)
  13650. (assoc property org-global-properties-fixed))))
  13651. (ap (lambda (key)
  13652. (when (re-search-forward
  13653. (org-re-property key) (cdr range) t)
  13654. (setq props
  13655. (org-update-property-plist
  13656. key
  13657. (if (match-end 1)
  13658. (org-match-string-no-properties 1) "")
  13659. props)))))
  13660. val)
  13661. (goto-char (car range))
  13662. (funcall ap property)
  13663. (goto-char (car range))
  13664. (while (funcall ap (concat property "+")))
  13665. (setq val (cdr (assoc property props)))
  13666. (when val (if literal-nil val (org-not-nil val))))))))))
  13667. (defun org-property-or-variable-value (var &optional inherit)
  13668. "Check if there is a property fixing the value of VAR.
  13669. If yes, return this value. If not, return the current value of the variable."
  13670. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13671. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13672. (read prop)
  13673. (symbol-value var))))
  13674. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13675. "Delete the property PROPERTY from entry at point-or-marker POM.
  13676. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13677. an empty drawer to delete."
  13678. (org-with-point-at pom
  13679. (if (member property org-special-properties)
  13680. nil ; cannot delete these properties.
  13681. (let ((range (org-get-property-block)))
  13682. (if (and range
  13683. (goto-char (car range))
  13684. (re-search-forward
  13685. (org-re-property property)
  13686. (cdr range) t))
  13687. (progn
  13688. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13689. (and delete-empty-drawer
  13690. (org-remove-empty-drawer-at
  13691. delete-empty-drawer (car range)))
  13692. t)
  13693. nil)))))
  13694. ;; Multi-values properties are properties that contain multiple values
  13695. ;; These values are assumed to be single words, separated by whitespace.
  13696. (defun org-entry-add-to-multivalued-property (pom property value)
  13697. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13698. (let* ((old (org-entry-get pom property))
  13699. (values (and old (org-split-string old "[ \t]"))))
  13700. (setq value (org-entry-protect-space value))
  13701. (unless (member value values)
  13702. (setq values (cons value values))
  13703. (org-entry-put pom property
  13704. (mapconcat 'identity values " ")))))
  13705. (defun org-entry-remove-from-multivalued-property (pom property value)
  13706. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13707. (let* ((old (org-entry-get pom property))
  13708. (values (and old (org-split-string old "[ \t]"))))
  13709. (setq value (org-entry-protect-space value))
  13710. (when (member value values)
  13711. (setq values (delete value values))
  13712. (org-entry-put pom property
  13713. (mapconcat 'identity values " ")))))
  13714. (defun org-entry-member-in-multivalued-property (pom property value)
  13715. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13716. (let* ((old (org-entry-get pom property))
  13717. (values (and old (org-split-string old "[ \t]"))))
  13718. (setq value (org-entry-protect-space value))
  13719. (member value values)))
  13720. (defun org-entry-get-multivalued-property (pom property)
  13721. "Return a list of values in a multivalued property."
  13722. (let* ((value (org-entry-get pom property))
  13723. (values (and value (org-split-string value "[ \t]"))))
  13724. (mapcar 'org-entry-restore-space values)))
  13725. (defun org-entry-put-multivalued-property (pom property &rest values)
  13726. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13727. VALUES should be a list of strings. Spaces will be protected."
  13728. (org-entry-put pom property
  13729. (mapconcat 'org-entry-protect-space values " "))
  13730. (let* ((value (org-entry-get pom property))
  13731. (values (and value (org-split-string value "[ \t]"))))
  13732. (mapcar 'org-entry-restore-space values)))
  13733. (defun org-entry-protect-space (s)
  13734. "Protect spaces and newline in string S."
  13735. (while (string-match " " s)
  13736. (setq s (replace-match "%20" t t s)))
  13737. (while (string-match "\n" s)
  13738. (setq s (replace-match "%0A" t t s)))
  13739. s)
  13740. (defun org-entry-restore-space (s)
  13741. "Restore spaces and newline in string S."
  13742. (while (string-match "%20" s)
  13743. (setq s (replace-match " " t t s)))
  13744. (while (string-match "%0A" s)
  13745. (setq s (replace-match "\n" t t s)))
  13746. s)
  13747. (defvar org-entry-property-inherited-from (make-marker)
  13748. "Marker pointing to the entry from where a property was inherited.
  13749. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13750. location of the entry where the inheritance search matched. If there was
  13751. no match, the marker will point nowhere.
  13752. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13753. is set.")
  13754. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13755. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13756. The search will stop at the first ancestor which has the property defined.
  13757. If the value found is \"nil\", return nil to show that the property
  13758. should be considered as undefined (this is the meaning of nil here).
  13759. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13760. (move-marker org-entry-property-inherited-from nil)
  13761. (let (tmp)
  13762. (save-excursion
  13763. (save-restriction
  13764. (widen)
  13765. (catch 'ex
  13766. (while t
  13767. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13768. (or (ignore-errors (org-back-to-heading t))
  13769. (goto-char (point-min)))
  13770. (move-marker org-entry-property-inherited-from (point))
  13771. (throw 'ex tmp))
  13772. (or (ignore-errors (org-up-heading-safe))
  13773. (throw 'ex nil))))))
  13774. (setq tmp (or tmp
  13775. (cdr (assoc property org-file-properties))
  13776. (cdr (assoc property org-global-properties))
  13777. (cdr (assoc property org-global-properties-fixed))))
  13778. (if literal-nil tmp (org-not-nil tmp))))
  13779. (defvar org-property-changed-functions nil
  13780. "Hook called when the value of a property has changed.
  13781. Each hook function should accept two arguments, the name of the property
  13782. and the new value.")
  13783. (defun org-entry-put (pom property value)
  13784. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13785. (org-with-point-at pom
  13786. (org-back-to-heading t)
  13787. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13788. range)
  13789. (cond
  13790. ((equal property "TODO")
  13791. (when (and (stringp value) (string-match "\\S-" value)
  13792. (not (member value org-todo-keywords-1)))
  13793. (error "\"%s\" is not a valid TODO state" value))
  13794. (if (or (not value)
  13795. (not (string-match "\\S-" value)))
  13796. (setq value 'none))
  13797. (org-todo value)
  13798. (org-set-tags nil 'align))
  13799. ((equal property "PRIORITY")
  13800. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13801. (string-to-char value) ?\ ))
  13802. (org-set-tags nil 'align))
  13803. ((equal property "CLOCKSUM")
  13804. (if (not (re-search-forward
  13805. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13806. (error "Cannot find a clock log")
  13807. (goto-char (- (match-end 1) 2))
  13808. (cond
  13809. ((eq value 'earlier) (org-timestamp-down))
  13810. ((eq value 'later) (org-timestamp-up)))
  13811. (org-clock-sum-current-item)))
  13812. ((equal property "SCHEDULED")
  13813. (if (re-search-forward org-scheduled-time-regexp end t)
  13814. (cond
  13815. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13816. ((eq value 'later) (org-timestamp-change 1 'day))
  13817. (t (call-interactively 'org-schedule)))
  13818. (call-interactively 'org-schedule)))
  13819. ((equal property "DEADLINE")
  13820. (if (re-search-forward org-deadline-time-regexp end t)
  13821. (cond
  13822. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13823. ((eq value 'later) (org-timestamp-change 1 'day))
  13824. (t (call-interactively 'org-deadline)))
  13825. (call-interactively 'org-deadline)))
  13826. ((member property org-special-properties)
  13827. (error "The %s property can not yet be set with `org-entry-put'"
  13828. property))
  13829. (t ; a non-special property
  13830. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13831. (setq range (org-get-property-block beg end 'force))
  13832. (goto-char (car range))
  13833. (if (re-search-forward
  13834. (org-re-property-keyword property) (cdr range) t)
  13835. (progn
  13836. (delete-region (match-beginning 0) (match-end 0))
  13837. (goto-char (match-beginning 0)))
  13838. (goto-char (cdr range))
  13839. (insert "\n")
  13840. (backward-char 1)
  13841. (org-indent-line))
  13842. (insert ":" property ":")
  13843. (and value (insert " " value))
  13844. (org-indent-line)))))
  13845. (run-hook-with-args 'org-property-changed-functions property value)))
  13846. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13847. "Get all property keys in the current buffer.
  13848. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13849. like tags and TODO state.
  13850. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13851. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13852. and others.
  13853. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13854. formats in the current buffer."
  13855. (let (rtn range cfmt s p)
  13856. (save-excursion
  13857. (save-restriction
  13858. (widen)
  13859. (goto-char (point-min))
  13860. (while (re-search-forward org-property-start-re nil t)
  13861. (setq range (org-get-property-block))
  13862. (goto-char (car range))
  13863. (while (re-search-forward
  13864. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13865. (cdr range) t)
  13866. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13867. (outline-next-heading))))
  13868. (when include-specials
  13869. (setq rtn (append org-special-properties rtn)))
  13870. (when include-defaults
  13871. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13872. (add-to-list 'rtn org-effort-property))
  13873. (when include-columns
  13874. (save-excursion
  13875. (save-restriction
  13876. (widen)
  13877. (goto-char (point-min))
  13878. (while (re-search-forward
  13879. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13880. nil t)
  13881. (setq cfmt (match-string 2) s 0)
  13882. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13883. cfmt s)
  13884. (setq s (match-end 0)
  13885. p (match-string 1 cfmt))
  13886. (unless (or (equal p "ITEM")
  13887. (member p org-special-properties))
  13888. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13889. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13890. (defun org-property-values (key)
  13891. "Return a list of all values of property KEY in the current buffer."
  13892. (save-excursion
  13893. (save-restriction
  13894. (widen)
  13895. (goto-char (point-min))
  13896. (let ((re (org-re-property key))
  13897. values)
  13898. (while (re-search-forward re nil t)
  13899. (add-to-list 'values (org-trim (match-string 1))))
  13900. (delete "" values)))))
  13901. (defun org-insert-property-drawer ()
  13902. "Insert a property drawer into the current entry."
  13903. (org-back-to-heading t)
  13904. (looking-at org-outline-regexp)
  13905. (let ((indent (if org-adapt-indentation
  13906. (- (match-end 0) (match-beginning 0))
  13907. 0))
  13908. (beg (point))
  13909. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13910. end hiddenp)
  13911. (outline-next-heading)
  13912. (setq end (point))
  13913. (goto-char beg)
  13914. (while (re-search-forward re end t))
  13915. (setq hiddenp (outline-invisible-p))
  13916. (end-of-line 1)
  13917. (and (equal (char-after) ?\n) (forward-char 1))
  13918. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13919. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13920. ;; just skip this line
  13921. (beginning-of-line 2)
  13922. ;; Drawer start, find the end
  13923. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13924. (beginning-of-line 1)))
  13925. (org-skip-over-state-notes)
  13926. (skip-chars-backward " \t\n\r")
  13927. (if (eq (char-before) ?*) (forward-char 1))
  13928. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13929. (beginning-of-line 0)
  13930. (org-indent-to-column indent)
  13931. (beginning-of-line 2)
  13932. (org-indent-to-column indent)
  13933. (beginning-of-line 0)
  13934. (if hiddenp
  13935. (save-excursion
  13936. (org-back-to-heading t)
  13937. (hide-entry))
  13938. (org-flag-drawer t))))
  13939. (defun org-insert-drawer (&optional arg drawer)
  13940. "Insert a drawer at point.
  13941. Optional argument DRAWER, when non-nil, is a string representing
  13942. drawer's name. Otherwise, the user is prompted for a name.
  13943. If a region is active, insert the drawer around that region
  13944. instead.
  13945. Point is left between drawer's boundaries."
  13946. (interactive "P")
  13947. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13948. "LOGBOOK"))
  13949. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13950. ;; internally by Org. They are meant to be inserted
  13951. ;; automatically.
  13952. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13953. ;; Remove system drawers from list. Note: For some reason,
  13954. ;; `org-completing-read' ignores the predicate while
  13955. ;; `completing-read' handles it fine.
  13956. (drawer (if arg "PROPERTIES"
  13957. (or drawer
  13958. (completing-read
  13959. "Drawer: " org-drawers
  13960. (lambda (d) (not (member d system-drawers))))))))
  13961. (cond
  13962. ;; With C-u, fall back on `org-insert-property-drawer'
  13963. (arg (org-insert-property-drawer))
  13964. ;; With an active region, insert a drawer at point.
  13965. ((not (org-region-active-p))
  13966. (progn
  13967. (unless (bolp) (insert "\n"))
  13968. (insert (format ":%s:\n\n:END:\n" drawer))
  13969. (forward-line -2)))
  13970. ;; Otherwise, insert the drawer at point
  13971. (t
  13972. (let ((rbeg (region-beginning))
  13973. (rend (copy-marker (region-end))))
  13974. (unwind-protect
  13975. (progn
  13976. (goto-char rbeg)
  13977. (beginning-of-line)
  13978. (when (save-excursion
  13979. (re-search-forward org-outline-regexp-bol rend t))
  13980. (error "Drawers cannot contain headlines"))
  13981. ;; Position point at the beginning of the first
  13982. ;; non-blank line in region. Insert drawer's opening
  13983. ;; there, then indent it.
  13984. (org-skip-whitespace)
  13985. (beginning-of-line)
  13986. (insert ":" drawer ":\n")
  13987. (forward-line -1)
  13988. (indent-for-tab-command)
  13989. ;; Move point to the beginning of the first blank line
  13990. ;; after the last non-blank line in region. Insert
  13991. ;; drawer's closing, then indent it.
  13992. (goto-char rend)
  13993. (skip-chars-backward " \r\t\n")
  13994. (insert "\n:END:")
  13995. (deactivate-mark t)
  13996. (indent-for-tab-command)
  13997. (unless (eolp) (insert "\n")))
  13998. ;; Clear marker, whatever the outcome of insertion is.
  13999. (set-marker rend nil)))))))
  14000. (defvar org-property-set-functions-alist nil
  14001. "Property set function alist.
  14002. Each entry should have the following format:
  14003. (PROPERTY . READ-FUNCTION)
  14004. The read function will be called with the same argument as
  14005. `org-completing-read'.")
  14006. (defun org-set-property-function (property)
  14007. "Get the function that should be used to set PROPERTY.
  14008. This is computed according to `org-property-set-functions-alist'."
  14009. (or (cdr (assoc property org-property-set-functions-alist))
  14010. 'org-completing-read))
  14011. (defun org-read-property-value (property)
  14012. "Read PROPERTY value from user."
  14013. (let* ((completion-ignore-case t)
  14014. (allowed (org-property-get-allowed-values nil property 'table))
  14015. (cur (org-entry-get nil property))
  14016. (prompt (concat property " value"
  14017. (if (and cur (string-match "\\S-" cur))
  14018. (concat " [" cur "]") "") ": "))
  14019. (set-function (org-set-property-function property))
  14020. (val (if allowed
  14021. (funcall set-function prompt allowed nil
  14022. (not (get-text-property 0 'org-unrestricted
  14023. (caar allowed))))
  14024. (let (org-completion-use-ido org-completion-use-iswitchb)
  14025. (funcall set-function prompt
  14026. (mapcar 'list (org-property-values property))
  14027. nil nil "" nil cur)))))
  14028. (if (equal val "")
  14029. cur
  14030. val)))
  14031. (defvar org-last-set-property nil)
  14032. (defvar org-last-set-property-value nil)
  14033. (defun org-read-property-name ()
  14034. "Read a property name."
  14035. (let* ((completion-ignore-case t)
  14036. (keys (org-buffer-property-keys nil t t))
  14037. (default-prop (or (save-excursion
  14038. (save-match-data
  14039. (beginning-of-line)
  14040. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14041. (null (string= (match-string 1) "END"))
  14042. (match-string 1))))
  14043. org-last-set-property))
  14044. (property (org-icompleting-read
  14045. (concat "Property"
  14046. (if default-prop (concat " [" default-prop "]") "")
  14047. ": ")
  14048. (mapcar 'list keys)
  14049. nil nil nil nil
  14050. default-prop)))
  14051. (if (member property keys)
  14052. property
  14053. (or (cdr (assoc (downcase property)
  14054. (mapcar (lambda (x) (cons (downcase x) x))
  14055. keys)))
  14056. property))))
  14057. (defun org-set-property-and-value (use-last)
  14058. "Allow to set [PROPERTY]: [value] direction from prompt.
  14059. When use-default, don't even ask, just use the last
  14060. \"[PROPERTY]: [value]\" string from the history."
  14061. (interactive "P")
  14062. (let* ((completion-ignore-case t)
  14063. (pv (or (and use-last org-last-set-property-value)
  14064. (org-completing-read
  14065. "Enter a \"[Property]: [value]\" pair: "
  14066. nil nil nil nil nil
  14067. org-last-set-property-value)))
  14068. prop val)
  14069. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14070. (setq prop (match-string 1 pv)
  14071. val (match-string 2 pv))
  14072. (org-set-property prop val))))
  14073. (defun org-set-property (property value)
  14074. "In the current entry, set PROPERTY to VALUE.
  14075. When called interactively, this will prompt for a property name, offering
  14076. completion on existing and default properties. And then it will prompt
  14077. for a value, offering completion either on allowed values (via an inherited
  14078. xxx_ALL property) or on existing values in other instances of this property
  14079. in the current file."
  14080. (interactive (list nil nil))
  14081. (let* ((property (or property (org-read-property-name)))
  14082. (value (or value (org-read-property-value property)))
  14083. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14084. (setq org-last-set-property property)
  14085. (setq org-last-set-property-value (concat property ": " value))
  14086. ;; Possibly postprocess the inserted value:
  14087. (when fn (setq value (funcall fn value)))
  14088. (unless (equal (org-entry-get nil property) value)
  14089. (org-entry-put nil property value))))
  14090. (defun org-delete-property (property &optional delete-empty-drawer)
  14091. "In the current entry, delete PROPERTY.
  14092. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14093. an empty drawer to delete."
  14094. (interactive
  14095. (let* ((completion-ignore-case t)
  14096. (prop (org-icompleting-read "Property: "
  14097. (org-entry-properties nil 'standard))))
  14098. (list prop)))
  14099. (message "Property %s %s" property
  14100. (if (org-entry-delete nil property delete-empty-drawer)
  14101. "deleted"
  14102. "was not present in the entry")))
  14103. (defun org-delete-property-globally (property)
  14104. "Remove PROPERTY globally, from all entries."
  14105. (interactive
  14106. (let* ((completion-ignore-case t)
  14107. (prop (org-icompleting-read
  14108. "Globally remove property: "
  14109. (mapcar 'list (org-buffer-property-keys)))))
  14110. (list prop)))
  14111. (save-excursion
  14112. (save-restriction
  14113. (widen)
  14114. (goto-char (point-min))
  14115. (let ((cnt 0))
  14116. (while (re-search-forward
  14117. (org-re-property property)
  14118. nil t)
  14119. (setq cnt (1+ cnt))
  14120. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14121. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14122. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14123. (defun org-compute-property-at-point ()
  14124. "Compute the property at point.
  14125. This looks for an enclosing column format, extracts the operator and
  14126. then applies it to the property in the column format's scope."
  14127. (interactive)
  14128. (unless (org-at-property-p)
  14129. (error "Not at a property"))
  14130. (let ((prop (org-match-string-no-properties 2)))
  14131. (org-columns-get-format-and-top-level)
  14132. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14133. (error "No operator defined for property %s" prop))
  14134. (org-columns-compute prop)))
  14135. (defvar org-property-allowed-value-functions nil
  14136. "Hook for functions supplying allowed values for a specific property.
  14137. The functions must take a single argument, the name of the property, and
  14138. return a flat list of allowed values. If \":ETC\" is one of
  14139. the values, this means that these values are intended as defaults for
  14140. completion, but that other values should be allowed too.
  14141. The functions must return nil if they are not responsible for this
  14142. property.")
  14143. (defun org-property-get-allowed-values (pom property &optional table)
  14144. "Get allowed values for the property PROPERTY.
  14145. When TABLE is non-nil, return an alist that can directly be used for
  14146. completion."
  14147. (let (vals)
  14148. (cond
  14149. ((equal property "TODO")
  14150. (setq vals (org-with-point-at pom
  14151. (append org-todo-keywords-1 '("")))))
  14152. ((equal property "PRIORITY")
  14153. (let ((n org-lowest-priority))
  14154. (while (>= n org-highest-priority)
  14155. (push (char-to-string n) vals)
  14156. (setq n (1- n)))))
  14157. ((member property org-special-properties))
  14158. ((setq vals (run-hook-with-args-until-success
  14159. 'org-property-allowed-value-functions property)))
  14160. (t
  14161. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14162. (when (and vals (string-match "\\S-" vals))
  14163. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14164. (setq vals (mapcar (lambda (x)
  14165. (cond ((stringp x) x)
  14166. ((numberp x) (number-to-string x))
  14167. ((symbolp x) (symbol-name x))
  14168. (t "???")))
  14169. vals)))))
  14170. (when (member ":ETC" vals)
  14171. (setq vals (remove ":ETC" vals))
  14172. (org-add-props (car vals) '(org-unrestricted t)))
  14173. (if table (mapcar 'list vals) vals)))
  14174. (defun org-property-previous-allowed-value (&optional previous)
  14175. "Switch to the next allowed value for this property."
  14176. (interactive)
  14177. (org-property-next-allowed-value t))
  14178. (defun org-property-next-allowed-value (&optional previous)
  14179. "Switch to the next allowed value for this property."
  14180. (interactive)
  14181. (unless (org-at-property-p)
  14182. (error "Not at a property"))
  14183. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14184. (key (match-string 2))
  14185. (value (match-string 3))
  14186. (allowed (or (org-property-get-allowed-values (point) key)
  14187. (and (member value '("[ ]" "[-]" "[X]"))
  14188. '("[ ]" "[X]"))))
  14189. (heading (save-match-data (nth 4 (org-heading-components))))
  14190. nval)
  14191. (unless allowed
  14192. (error "Allowed values for this property have not been defined"))
  14193. (if previous (setq allowed (reverse allowed)))
  14194. (if (member value allowed)
  14195. (setq nval (car (cdr (member value allowed)))))
  14196. (setq nval (or nval (car allowed)))
  14197. (if (equal nval value)
  14198. (error "Only one allowed value for this property"))
  14199. (org-at-property-p)
  14200. (replace-match (concat " :" key ": " nval) t t)
  14201. (org-indent-line)
  14202. (beginning-of-line 1)
  14203. (skip-chars-forward " \t")
  14204. (when (equal prop org-effort-property)
  14205. (save-excursion
  14206. (org-back-to-heading t)
  14207. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14208. (when (string= org-clock-current-task heading)
  14209. (setq org-clock-effort nval)
  14210. (org-clock-update-mode-line)))
  14211. (run-hook-with-args 'org-property-changed-functions key nval)))
  14212. (defun org-find-olp (path &optional this-buffer)
  14213. "Return a marker pointing to the entry at outline path OLP.
  14214. If anything goes wrong, throw an error.
  14215. You can wrap this call to catch the error like this:
  14216. (condition-case msg
  14217. (org-mobile-locate-entry (match-string 4))
  14218. (error (nth 1 msg)))
  14219. The return value will then be either a string with the error message,
  14220. or a marker if everything is OK.
  14221. If THIS-BUFFER is set, the outline path does not contain a file,
  14222. only headings."
  14223. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14224. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14225. (level 1)
  14226. (lmin 1)
  14227. (lmax 1)
  14228. limit re end found pos heading cnt flevel)
  14229. (unless buffer (error "File not found :%s" file))
  14230. (with-current-buffer buffer
  14231. (save-excursion
  14232. (save-restriction
  14233. (widen)
  14234. (setq limit (point-max))
  14235. (goto-char (point-min))
  14236. (while (setq heading (pop path))
  14237. (setq re (format org-complex-heading-regexp-format
  14238. (regexp-quote heading)))
  14239. (setq cnt 0 pos (point))
  14240. (while (re-search-forward re end t)
  14241. (setq level (- (match-end 1) (match-beginning 1)))
  14242. (if (and (>= level lmin) (<= level lmax))
  14243. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14244. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14245. lmax heading))
  14246. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14247. lmax heading))
  14248. (goto-char found)
  14249. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14250. (setq end (save-excursion (org-end-of-subtree t t))))
  14251. (when (org-at-heading-p)
  14252. (point-marker)))))))
  14253. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14254. "Find node HEADING in BUFFER.
  14255. Return a marker to the heading if it was found, or nil if not.
  14256. If POS-ONLY is set, return just the position instead of a marker.
  14257. The heading text must match exact, but it may have a TODO keyword,
  14258. a priority cookie and tags in the standard locations."
  14259. (with-current-buffer (or buffer (current-buffer))
  14260. (save-excursion
  14261. (save-restriction
  14262. (widen)
  14263. (goto-char (point-min))
  14264. (let (case-fold-search)
  14265. (if (re-search-forward
  14266. (format org-complex-heading-regexp-format
  14267. (regexp-quote heading)) nil t)
  14268. (if pos-only
  14269. (match-beginning 0)
  14270. (move-marker (make-marker) (match-beginning 0)))))))))
  14271. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14272. "Find Org node headline HEADING in all .org files in directory DIR.
  14273. When the target headline is found, return a marker to this location."
  14274. (let ((files (directory-files (or dir default-directory)
  14275. nil "\\`[^.#].*\\.org\\'"))
  14276. file visiting m buffer)
  14277. (catch 'found
  14278. (while (setq file (pop files))
  14279. (message "trying %s" file)
  14280. (setq visiting (org-find-base-buffer-visiting file))
  14281. (setq buffer (or visiting (find-file-noselect file)))
  14282. (setq m (org-find-exact-headline-in-buffer
  14283. heading buffer))
  14284. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14285. (and m (throw 'found m))))))
  14286. (defun org-find-entry-with-id (ident)
  14287. "Locate the entry that contains the ID property with exact value IDENT.
  14288. IDENT can be a string, a symbol or a number, this function will search for
  14289. the string representation of it.
  14290. Return the position where this entry starts, or nil if there is no such entry."
  14291. (interactive "sID: ")
  14292. (let ((id (cond
  14293. ((stringp ident) ident)
  14294. ((symbol-name ident) (symbol-name ident))
  14295. ((numberp ident) (number-to-string ident))
  14296. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14297. (case-fold-search nil))
  14298. (save-excursion
  14299. (save-restriction
  14300. (widen)
  14301. (goto-char (point-min))
  14302. (when (re-search-forward
  14303. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14304. nil t)
  14305. (org-back-to-heading t)
  14306. (point))))))
  14307. ;;;; Timestamps
  14308. (defvar org-last-changed-timestamp nil)
  14309. (defvar org-last-inserted-timestamp nil
  14310. "The last time stamp inserted with `org-insert-time-stamp'.")
  14311. (defvar org-time-was-given) ; dynamically scoped parameter
  14312. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14313. (defvar org-ts-what) ; dynamically scoped parameter
  14314. (defun org-time-stamp (arg &optional inactive)
  14315. "Prompt for a date/time and insert a time stamp.
  14316. If the user specifies a time like HH:MM or if this command is
  14317. called with at least one prefix argument, the time stamp contains
  14318. the date and the time. Otherwise, only the date is be included.
  14319. All parts of a date not specified by the user is filled in from
  14320. the current date/time. So if you just press return without
  14321. typing anything, the time stamp will represent the current
  14322. date/time.
  14323. If there is already a timestamp at the cursor, it will be
  14324. modified.
  14325. With two universal prefix arguments, insert an active timestamp
  14326. with the current time without prompting the user."
  14327. (interactive "P")
  14328. (let* ((ts nil)
  14329. (default-time
  14330. ;; Default time is either today, or, when entering a range,
  14331. ;; the range start.
  14332. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14333. (save-excursion
  14334. (re-search-backward
  14335. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14336. (- (point) 20) t)))
  14337. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14338. (current-time)))
  14339. (default-input (and ts (org-get-compact-tod ts)))
  14340. (repeater (save-excursion
  14341. (save-match-data
  14342. (beginning-of-line)
  14343. (when (re-search-forward
  14344. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14345. (save-excursion (progn (end-of-line) (point))) t)
  14346. (match-string 0)))))
  14347. org-time-was-given org-end-time-was-given time)
  14348. (cond
  14349. ((and (org-at-timestamp-p t)
  14350. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14351. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14352. (insert "--")
  14353. (setq time (let ((this-command this-command))
  14354. (org-read-date arg 'totime nil nil
  14355. default-time default-input inactive)))
  14356. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14357. ((org-at-timestamp-p t)
  14358. (setq time (let ((this-command this-command))
  14359. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14360. (when (org-at-timestamp-p t) ; just to get the match data
  14361. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14362. (replace-match "")
  14363. (setq org-last-changed-timestamp
  14364. (org-insert-time-stamp
  14365. time (or org-time-was-given arg)
  14366. inactive nil nil (list org-end-time-was-given)))
  14367. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14368. (setq org-last-changed-timestamp
  14369. (concat (substring org-last-inserted-timestamp 0 -1)
  14370. " " repeater ">"))))
  14371. (message "Timestamp updated"))
  14372. ((equal arg '(16))
  14373. (org-insert-time-stamp (current-time) t))
  14374. (t
  14375. (setq time (let ((this-command this-command))
  14376. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14377. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14378. nil nil (list org-end-time-was-given))))))
  14379. ;; FIXME: can we use this for something else, like computing time differences?
  14380. (defun org-get-compact-tod (s)
  14381. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14382. (let* ((t1 (match-string 1 s))
  14383. (h1 (string-to-number (match-string 2 s)))
  14384. (m1 (string-to-number (match-string 3 s)))
  14385. (t2 (and (match-end 4) (match-string 5 s)))
  14386. (h2 (and t2 (string-to-number (match-string 6 s))))
  14387. (m2 (and t2 (string-to-number (match-string 7 s))))
  14388. dh dm)
  14389. (if (not t2)
  14390. t1
  14391. (setq dh (- h2 h1) dm (- m2 m1))
  14392. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14393. (concat t1 "+" (number-to-string dh)
  14394. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14395. (defun org-time-stamp-inactive (&optional arg)
  14396. "Insert an inactive time stamp.
  14397. An inactive time stamp is enclosed in square brackets instead of angle
  14398. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14399. does not link to the calendar and cannot be changed with the S-cursor keys.
  14400. So these are more for recording a certain time/date."
  14401. (interactive "P")
  14402. (org-time-stamp arg 'inactive))
  14403. (defvar org-date-ovl (make-overlay 1 1))
  14404. (overlay-put org-date-ovl 'face 'org-date-selected)
  14405. (org-detach-overlay org-date-ovl)
  14406. (defvar org-ans1) ; dynamically scoped parameter
  14407. (defvar org-ans2) ; dynamically scoped parameter
  14408. (defvar org-plain-time-of-day-regexp) ; defined below
  14409. (defvar org-overriding-default-time nil) ; dynamically scoped
  14410. (defvar org-read-date-overlay nil)
  14411. (defvar org-dcst nil) ; dynamically scoped
  14412. (defvar org-read-date-history nil)
  14413. (defvar org-read-date-final-answer nil)
  14414. (defvar org-read-date-analyze-futurep nil)
  14415. (defvar org-read-date-analyze-forced-year nil)
  14416. (defvar org-read-date-inactive)
  14417. (defvar org-read-date-minibuffer-local-map
  14418. (let ((map (make-sparse-keymap)))
  14419. (set-keymap-parent map minibuffer-local-map)
  14420. (org-defkey map (kbd ".")
  14421. (lambda () (interactive)
  14422. (org-eval-in-calendar '(calendar-goto-today))))
  14423. (org-defkey map [(meta shift left)]
  14424. (lambda () (interactive)
  14425. (org-eval-in-calendar '(calendar-backward-month 1))))
  14426. (org-defkey map [(meta shift right)]
  14427. (lambda () (interactive)
  14428. (org-eval-in-calendar '(calendar-forward-month 1))))
  14429. (org-defkey map [(meta shift up)]
  14430. (lambda () (interactive)
  14431. (org-eval-in-calendar '(calendar-backward-year 1))))
  14432. (org-defkey map [(meta shift down)]
  14433. (lambda () (interactive)
  14434. (org-eval-in-calendar '(calendar-forward-year 1))))
  14435. (org-defkey map [?\e (shift left)]
  14436. (lambda () (interactive)
  14437. (org-eval-in-calendar '(calendar-backward-month 1))))
  14438. (org-defkey map [?\e (shift right)]
  14439. (lambda () (interactive)
  14440. (org-eval-in-calendar '(calendar-forward-month 1))))
  14441. (org-defkey map [?\e (shift up)]
  14442. (lambda () (interactive)
  14443. (org-eval-in-calendar '(calendar-backward-year 1))))
  14444. (org-defkey map [?\e (shift down)]
  14445. (lambda () (interactive)
  14446. (org-eval-in-calendar '(calendar-forward-year 1))))
  14447. (org-defkey map [(shift up)]
  14448. (lambda () (interactive)
  14449. (org-eval-in-calendar '(calendar-backward-week 1))))
  14450. (org-defkey map [(shift down)]
  14451. (lambda () (interactive)
  14452. (org-eval-in-calendar '(calendar-forward-week 1))))
  14453. (org-defkey map [(shift left)]
  14454. (lambda () (interactive)
  14455. (org-eval-in-calendar '(calendar-backward-day 1))))
  14456. (org-defkey map [(shift right)]
  14457. (lambda () (interactive)
  14458. (org-eval-in-calendar '(calendar-forward-day 1))))
  14459. (org-defkey map "!"
  14460. (lambda () (interactive)
  14461. (org-eval-in-calendar '(diary-view-entries))
  14462. (message "")))
  14463. (org-defkey map ">"
  14464. (lambda () (interactive)
  14465. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14466. (org-defkey map "<"
  14467. (lambda () (interactive)
  14468. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14469. (org-defkey map "\C-v"
  14470. (lambda () (interactive)
  14471. (org-eval-in-calendar
  14472. '(calendar-scroll-left-three-months 1))))
  14473. (org-defkey map "\M-v"
  14474. (lambda () (interactive)
  14475. (org-eval-in-calendar
  14476. '(calendar-scroll-right-three-months 1))))
  14477. map)
  14478. "Keymap for minibuffer commands when using `org-read-date'.")
  14479. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14480. default-time default-input inactive)
  14481. "Read a date, possibly a time, and make things smooth for the user.
  14482. The prompt will suggest to enter an ISO date, but you can also enter anything
  14483. which will at least partially be understood by `parse-time-string'.
  14484. Unrecognized parts of the date will default to the current day, month, year,
  14485. hour and minute. If this command is called to replace a timestamp at point,
  14486. or to enter the second timestamp of a range, the default time is taken
  14487. from the existing stamp. Furthermore, the command prefers the future,
  14488. so if you are giving a date where the year is not given, and the day-month
  14489. combination is already past in the current year, it will assume you
  14490. mean next year. For details, see the manual. A few examples:
  14491. 3-2-5 --> 2003-02-05
  14492. feb 15 --> currentyear-02-15
  14493. 2/15 --> currentyear-02-15
  14494. sep 12 9 --> 2009-09-12
  14495. 12:45 --> today 12:45
  14496. 22 sept 0:34 --> currentyear-09-22 0:34
  14497. 12 --> currentyear-currentmonth-12
  14498. Fri --> nearest Friday (today or later)
  14499. etc.
  14500. Furthermore you can specify a relative date by giving, as the *first* thing
  14501. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14502. change in days weeks, months, years.
  14503. With a single plus or minus, the date is relative to today. With a double
  14504. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14505. +4d --> four days from today
  14506. +4 --> same as above
  14507. +2w --> two weeks from today
  14508. ++5 --> five days from default date
  14509. The function understands only English month and weekday abbreviations.
  14510. While prompting, a calendar is popped up - you can also select the
  14511. date with the mouse (button 1). The calendar shows a period of three
  14512. months. To scroll it to other months, use the keys `>' and `<'.
  14513. If you don't like the calendar, turn it off with
  14514. \(setq org-read-date-popup-calendar nil)
  14515. With optional argument TO-TIME, the date will immediately be converted
  14516. to an internal time.
  14517. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14518. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14519. still enter a time, and this function will inform the calling routine
  14520. about this change. The calling routine may then choose to change the
  14521. format used to insert the time stamp into the buffer to include the time.
  14522. With optional argument FROM-STRING, read from this string instead from
  14523. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14524. the time/date that is used for everything that is not specified by the
  14525. user."
  14526. (require 'parse-time)
  14527. (let* ((org-time-stamp-rounding-minutes
  14528. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14529. (org-dcst org-display-custom-times)
  14530. (ct (org-current-time))
  14531. (org-def (or org-overriding-default-time default-time ct))
  14532. (org-defdecode (decode-time org-def))
  14533. (dummy (progn
  14534. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14535. (setcar (nthcdr 2 org-defdecode) -1)
  14536. (setcar (nthcdr 1 org-defdecode) 59)
  14537. (setq org-def (apply 'encode-time org-defdecode)
  14538. org-defdecode (decode-time org-def)))))
  14539. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14540. (calendar-frame-setup nil)
  14541. (calendar-setup nil)
  14542. (calendar-move-hook nil)
  14543. (calendar-view-diary-initially-flag nil)
  14544. (calendar-view-holidays-initially-flag nil)
  14545. (timestr (format-time-string
  14546. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14547. (prompt (concat (if prompt (concat prompt " ") "")
  14548. (format "Date+time [%s]: " timestr)))
  14549. ans (org-ans0 "") org-ans1 org-ans2 final)
  14550. (cond
  14551. (from-string (setq ans from-string))
  14552. (org-read-date-popup-calendar
  14553. (save-excursion
  14554. (save-window-excursion
  14555. (calendar)
  14556. (org-eval-in-calendar '(setq cursor-type nil) t)
  14557. (unwind-protect
  14558. (progn
  14559. (calendar-forward-day (- (time-to-days org-def)
  14560. (calendar-absolute-from-gregorian
  14561. (calendar-current-date))))
  14562. (org-eval-in-calendar nil t)
  14563. (let* ((old-map (current-local-map))
  14564. (map (copy-keymap calendar-mode-map))
  14565. (minibuffer-local-map
  14566. (copy-keymap org-read-date-minibuffer-local-map)))
  14567. (org-defkey map (kbd "RET") 'org-calendar-select)
  14568. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14569. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14570. (unwind-protect
  14571. (progn
  14572. (use-local-map map)
  14573. (setq org-read-date-inactive inactive)
  14574. (add-hook 'post-command-hook 'org-read-date-display)
  14575. (setq org-ans0 (read-string prompt default-input
  14576. 'org-read-date-history nil))
  14577. ;; org-ans0: from prompt
  14578. ;; org-ans1: from mouse click
  14579. ;; org-ans2: from calendar motion
  14580. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14581. (remove-hook 'post-command-hook 'org-read-date-display)
  14582. (use-local-map old-map)
  14583. (when org-read-date-overlay
  14584. (delete-overlay org-read-date-overlay)
  14585. (setq org-read-date-overlay nil)))))
  14586. (bury-buffer "*Calendar*")))))
  14587. (t ; Naked prompt only
  14588. (unwind-protect
  14589. (setq ans (read-string prompt default-input
  14590. 'org-read-date-history timestr))
  14591. (when org-read-date-overlay
  14592. (delete-overlay org-read-date-overlay)
  14593. (setq org-read-date-overlay nil)))))
  14594. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14595. (when org-read-date-analyze-forced-year
  14596. (message "Year was forced into %s"
  14597. (if org-read-date-force-compatible-dates
  14598. "compatible range (1970-2037)"
  14599. "range representable on this machine"))
  14600. (ding))
  14601. ;; One round trip to get rid of 34th of August and stuff like that....
  14602. (setq final (decode-time (apply 'encode-time final)))
  14603. (setq org-read-date-final-answer ans)
  14604. (if to-time
  14605. (apply 'encode-time final)
  14606. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14607. (format "%04d-%02d-%02d %02d:%02d"
  14608. (nth 5 final) (nth 4 final) (nth 3 final)
  14609. (nth 2 final) (nth 1 final))
  14610. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14611. (defvar org-def)
  14612. (defvar org-defdecode)
  14613. (defvar org-with-time)
  14614. (defun org-read-date-display ()
  14615. "Display the current date prompt interpretation in the minibuffer."
  14616. (when org-read-date-display-live
  14617. (when org-read-date-overlay
  14618. (delete-overlay org-read-date-overlay))
  14619. (when (minibufferp (current-buffer))
  14620. (save-excursion
  14621. (end-of-line 1)
  14622. (while (not (equal (buffer-substring
  14623. (max (point-min) (- (point) 4)) (point))
  14624. " "))
  14625. (insert " ")))
  14626. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14627. " " (or org-ans1 org-ans2)))
  14628. (org-end-time-was-given nil)
  14629. (f (org-read-date-analyze ans org-def org-defdecode))
  14630. (fmts (if org-dcst
  14631. org-time-stamp-custom-formats
  14632. org-time-stamp-formats))
  14633. (fmt (if (or org-with-time
  14634. (and (boundp 'org-time-was-given) org-time-was-given))
  14635. (cdr fmts)
  14636. (car fmts)))
  14637. (txt (format-time-string fmt (apply 'encode-time f)))
  14638. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14639. (txt (concat "=> " txt)))
  14640. (when (and org-end-time-was-given
  14641. (string-match org-plain-time-of-day-regexp txt))
  14642. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14643. org-end-time-was-given
  14644. (substring txt (match-end 0)))))
  14645. (when org-read-date-analyze-futurep
  14646. (setq txt (concat txt " (=>F)")))
  14647. (setq org-read-date-overlay
  14648. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14649. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14650. (defun org-read-date-analyze (ans org-def org-defdecode)
  14651. "Analyze the combined answer of the date prompt."
  14652. ;; FIXME: cleanup and comment
  14653. (let ((nowdecode (decode-time (current-time)))
  14654. delta deltan deltaw deltadef year month day
  14655. hour minute second wday pm h2 m2 tl wday1
  14656. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14657. (setq org-read-date-analyze-futurep nil
  14658. org-read-date-analyze-forced-year nil)
  14659. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14660. (setq ans "+0"))
  14661. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14662. (setq ans (replace-match "" t t ans)
  14663. deltan (car delta)
  14664. deltaw (nth 1 delta)
  14665. deltadef (nth 2 delta)))
  14666. ;; Check if there is an iso week date in there. If yes, store the
  14667. ;; info and postpone interpreting it until the rest of the parsing
  14668. ;; is done.
  14669. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14670. (setq iso-year (if (match-end 1)
  14671. (org-small-year-to-year
  14672. (string-to-number (match-string 1 ans))))
  14673. iso-weekday (if (match-end 3)
  14674. (string-to-number (match-string 3 ans)))
  14675. iso-week (string-to-number (match-string 2 ans)))
  14676. (setq ans (replace-match "" t t ans)))
  14677. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14678. (when (string-match
  14679. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14680. (setq year (if (match-end 2)
  14681. (string-to-number (match-string 2 ans))
  14682. (progn (setq kill-year t)
  14683. (string-to-number (format-time-string "%Y"))))
  14684. month (string-to-number (match-string 3 ans))
  14685. day (string-to-number (match-string 4 ans)))
  14686. (if (< year 100) (setq year (+ 2000 year)))
  14687. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14688. t nil ans)))
  14689. ;; Help matching dotted european dates
  14690. (when (string-match
  14691. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14692. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14693. (setq kill-year t)
  14694. (string-to-number (format-time-string "%Y")))
  14695. day (string-to-number (match-string 1 ans))
  14696. month (string-to-number (match-string 2 ans))
  14697. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14698. t nil ans)))
  14699. ;; Help matching american dates, like 5/30 or 5/30/7
  14700. (when (string-match
  14701. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14702. (setq year (if (match-end 4)
  14703. (string-to-number (match-string 4 ans))
  14704. (progn (setq kill-year t)
  14705. (string-to-number (format-time-string "%Y"))))
  14706. month (string-to-number (match-string 1 ans))
  14707. day (string-to-number (match-string 2 ans)))
  14708. (if (< year 100) (setq year (+ 2000 year)))
  14709. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14710. t nil ans)))
  14711. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14712. ;; If there is a time with am/pm, and *no* time without it, we convert
  14713. ;; so that matching will be successful.
  14714. (loop for i from 1 to 2 do ; twice, for end time as well
  14715. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14716. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14717. (setq hour (string-to-number (match-string 1 ans))
  14718. minute (if (match-end 3)
  14719. (string-to-number (match-string 3 ans))
  14720. 0)
  14721. pm (equal ?p
  14722. (string-to-char (downcase (match-string 4 ans)))))
  14723. (if (and (= hour 12) (not pm))
  14724. (setq hour 0)
  14725. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14726. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14727. t t ans))))
  14728. ;; Check if a time range is given as a duration
  14729. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14730. (setq hour (string-to-number (match-string 1 ans))
  14731. h2 (+ hour (string-to-number (match-string 3 ans)))
  14732. minute (string-to-number (match-string 2 ans))
  14733. m2 (+ minute (if (match-end 5) (string-to-number
  14734. (match-string 5 ans))0)))
  14735. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14736. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14737. t t ans)))
  14738. ;; Check if there is a time range
  14739. (when (boundp 'org-end-time-was-given)
  14740. (setq org-time-was-given nil)
  14741. (when (and (string-match org-plain-time-of-day-regexp ans)
  14742. (match-end 8))
  14743. (setq org-end-time-was-given (match-string 8 ans))
  14744. (setq ans (concat (substring ans 0 (match-beginning 7))
  14745. (substring ans (match-end 7))))))
  14746. (setq tl (parse-time-string ans)
  14747. day (or (nth 3 tl) (nth 3 org-defdecode))
  14748. month (or (nth 4 tl)
  14749. (if (and org-read-date-prefer-future
  14750. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14751. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14752. (nth 4 org-defdecode)))
  14753. year (or (and (not kill-year) (nth 5 tl))
  14754. (if (and org-read-date-prefer-future
  14755. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14756. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14757. (nth 5 org-defdecode)))
  14758. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14759. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14760. second (or (nth 0 tl) 0)
  14761. wday (nth 6 tl))
  14762. (when (and (eq org-read-date-prefer-future 'time)
  14763. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14764. (equal day (nth 3 nowdecode))
  14765. (equal month (nth 4 nowdecode))
  14766. (equal year (nth 5 nowdecode))
  14767. (nth 2 tl)
  14768. (or (< (nth 2 tl) (nth 2 nowdecode))
  14769. (and (= (nth 2 tl) (nth 2 nowdecode))
  14770. (nth 1 tl)
  14771. (< (nth 1 tl) (nth 1 nowdecode)))))
  14772. (setq day (1+ day)
  14773. futurep t))
  14774. ;; Special date definitions below
  14775. (cond
  14776. (iso-week
  14777. ;; There was an iso week
  14778. (require 'cal-iso)
  14779. (setq futurep nil)
  14780. (setq year (or iso-year year)
  14781. day (or iso-weekday wday 1)
  14782. wday nil ; to make sure that the trigger below does not match
  14783. iso-date (calendar-gregorian-from-absolute
  14784. (calendar-absolute-from-iso
  14785. (list iso-week day year))))
  14786. ; FIXME: Should we also push ISO weeks into the future?
  14787. ; (when (and org-read-date-prefer-future
  14788. ; (not iso-year)
  14789. ; (< (calendar-absolute-from-gregorian iso-date)
  14790. ; (time-to-days (current-time))))
  14791. ; (setq year (1+ year)
  14792. ; iso-date (calendar-gregorian-from-absolute
  14793. ; (calendar-absolute-from-iso
  14794. ; (list iso-week day year)))))
  14795. (setq month (car iso-date)
  14796. year (nth 2 iso-date)
  14797. day (nth 1 iso-date)))
  14798. (deltan
  14799. (setq futurep nil)
  14800. (unless deltadef
  14801. (let ((now (decode-time (current-time))))
  14802. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14803. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14804. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14805. ((equal deltaw "m") (setq month (+ month deltan)))
  14806. ((equal deltaw "y") (setq year (+ year deltan)))))
  14807. ((and wday (not (nth 3 tl)))
  14808. ;; Weekday was given, but no day, so pick that day in the week
  14809. ;; on or after the derived date.
  14810. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14811. (unless (equal wday wday1)
  14812. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14813. (if (and (boundp 'org-time-was-given)
  14814. (nth 2 tl))
  14815. (setq org-time-was-given t))
  14816. (if (< year 100) (setq year (+ 2000 year)))
  14817. ;; Check of the date is representable
  14818. (if org-read-date-force-compatible-dates
  14819. (progn
  14820. (if (< year 1970)
  14821. (setq year 1970 org-read-date-analyze-forced-year t))
  14822. (if (> year 2037)
  14823. (setq year 2037 org-read-date-analyze-forced-year t)))
  14824. (condition-case nil
  14825. (ignore (encode-time second minute hour day month year))
  14826. (error
  14827. (setq year (nth 5 org-defdecode))
  14828. (setq org-read-date-analyze-forced-year t))))
  14829. (setq org-read-date-analyze-futurep futurep)
  14830. (list second minute hour day month year)))
  14831. (defvar parse-time-weekdays)
  14832. (defun org-read-date-get-relative (s today default)
  14833. "Check string S for special relative date string.
  14834. TODAY and DEFAULT are internal times, for today and for a default.
  14835. Return shift list (N what def-flag)
  14836. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14837. N is the number of WHATs to shift.
  14838. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14839. the DEFAULT date rather than TODAY."
  14840. (require 'parse-time)
  14841. (when (and
  14842. (string-match
  14843. (concat
  14844. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14845. "\\([0-9]+\\)?"
  14846. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14847. "\\([ \t]\\|$\\)") s)
  14848. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14849. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14850. (string-to-char (substring (match-string 1 s) -1))
  14851. ?+))
  14852. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14853. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14854. (what (if (match-end 3) (match-string 3 s) "d"))
  14855. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14856. (date (if rel default today))
  14857. (wday (nth 6 (decode-time date)))
  14858. delta)
  14859. (if wday1
  14860. (progn
  14861. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14862. (if (= dir ?-) (setq delta (- delta 7)))
  14863. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14864. (list delta "d" rel))
  14865. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14866. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14867. "Turn a user-specified date into the internal representation.
  14868. The internal representation needed by the calendar is (month day year).
  14869. This is a wrapper to handle the brain-dead convention in calendar that
  14870. user function argument order change dependent on argument order."
  14871. (if (boundp 'calendar-date-style)
  14872. (cond
  14873. ((eq calendar-date-style 'american)
  14874. (list arg1 arg2 arg3))
  14875. ((eq calendar-date-style 'european)
  14876. (list arg2 arg1 arg3))
  14877. ((eq calendar-date-style 'iso)
  14878. (list arg2 arg3 arg1)))
  14879. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14880. (if (org-bound-and-true-p european-calendar-style)
  14881. (list arg2 arg1 arg3)
  14882. (list arg1 arg2 arg3)))))
  14883. (defun org-eval-in-calendar (form &optional keepdate)
  14884. "Eval FORM in the calendar window and return to current window.
  14885. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14886. otherwise stick to the current value of `org-ans2'."
  14887. (let ((sf (selected-frame))
  14888. (sw (selected-window)))
  14889. (select-window (get-buffer-window "*Calendar*" t))
  14890. (eval form)
  14891. (when (and (not keepdate) (calendar-cursor-to-date))
  14892. (let* ((date (calendar-cursor-to-date))
  14893. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14894. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14895. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14896. (select-window sw)
  14897. (org-select-frame-set-input-focus sf)))
  14898. (defun org-calendar-select ()
  14899. "Return to `org-read-date' with the date currently selected.
  14900. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14901. (interactive)
  14902. (when (calendar-cursor-to-date)
  14903. (let* ((date (calendar-cursor-to-date))
  14904. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14905. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14906. (if (active-minibuffer-window) (exit-minibuffer))))
  14907. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14908. "Insert a date stamp for the date given by the internal TIME.
  14909. WITH-HM means use the stamp format that includes the time of the day.
  14910. INACTIVE means use square brackets instead of angular ones, so that the
  14911. stamp will not contribute to the agenda.
  14912. PRE and POST are optional strings to be inserted before and after the
  14913. stamp.
  14914. The command returns the inserted time stamp."
  14915. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14916. stamp)
  14917. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14918. (insert-before-markers (or pre ""))
  14919. (when (listp extra)
  14920. (setq extra (car extra))
  14921. (if (and (stringp extra)
  14922. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14923. (setq extra (format "-%02d:%02d"
  14924. (string-to-number (match-string 1 extra))
  14925. (string-to-number (match-string 2 extra))))
  14926. (setq extra nil)))
  14927. (when extra
  14928. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14929. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14930. (insert-before-markers (or post ""))
  14931. (setq org-last-inserted-timestamp stamp)))
  14932. (defun org-toggle-time-stamp-overlays ()
  14933. "Toggle the use of custom time stamp formats."
  14934. (interactive)
  14935. (setq org-display-custom-times (not org-display-custom-times))
  14936. (unless org-display-custom-times
  14937. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14938. (while (setq p (next-single-property-change p 'display))
  14939. (if (and (get-text-property p 'display)
  14940. (eq (get-text-property p 'face) 'org-date))
  14941. (remove-text-properties
  14942. p (setq p (next-single-property-change p 'display))
  14943. '(display t))))
  14944. (set-buffer-modified-p bmp)))
  14945. (if (featurep 'xemacs)
  14946. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14947. (org-restart-font-lock)
  14948. (setq org-table-may-need-update t)
  14949. (if org-display-custom-times
  14950. (message "Time stamps are overlaid with custom format")
  14951. (message "Time stamp overlays removed")))
  14952. (defun org-display-custom-time (beg end)
  14953. "Overlay modified time stamp format over timestamp between BEG and END."
  14954. (let* ((ts (buffer-substring beg end))
  14955. t1 w1 with-hm tf time str w2 (off 0))
  14956. (save-match-data
  14957. (setq t1 (org-parse-time-string ts t))
  14958. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14959. (setq off (- (match-end 0) (match-beginning 0)))))
  14960. (setq end (- end off))
  14961. (setq w1 (- end beg)
  14962. with-hm (and (nth 1 t1) (nth 2 t1))
  14963. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14964. time (org-fix-decoded-time t1)
  14965. str (org-add-props
  14966. (format-time-string
  14967. (substring tf 1 -1) (apply 'encode-time time))
  14968. nil 'mouse-face 'highlight)
  14969. w2 (length str))
  14970. (if (not (= w2 w1))
  14971. (add-text-properties (1+ beg) (+ 2 beg)
  14972. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14973. (if (featurep 'xemacs)
  14974. (progn
  14975. (put-text-property beg end 'invisible t)
  14976. (put-text-property beg end 'end-glyph (make-glyph str)))
  14977. (put-text-property beg end 'display str))))
  14978. (defun org-translate-time (string)
  14979. "Translate all timestamps in STRING to custom format.
  14980. But do this only if the variable `org-display-custom-times' is set."
  14981. (when org-display-custom-times
  14982. (save-match-data
  14983. (let* ((start 0)
  14984. (re org-ts-regexp-both)
  14985. t1 with-hm inactive tf time str beg end)
  14986. (while (setq start (string-match re string start))
  14987. (setq beg (match-beginning 0)
  14988. end (match-end 0)
  14989. t1 (save-match-data
  14990. (org-parse-time-string (substring string beg end) t))
  14991. with-hm (and (nth 1 t1) (nth 2 t1))
  14992. inactive (equal (substring string beg (1+ beg)) "[")
  14993. tf (funcall (if with-hm 'cdr 'car)
  14994. org-time-stamp-custom-formats)
  14995. time (org-fix-decoded-time t1)
  14996. str (format-time-string
  14997. (concat
  14998. (if inactive "[" "<") (substring tf 1 -1)
  14999. (if inactive "]" ">"))
  15000. (apply 'encode-time time))
  15001. string (replace-match str t t string)
  15002. start (+ start (length str)))))))
  15003. string)
  15004. (defun org-fix-decoded-time (time)
  15005. "Set 0 instead of nil for the first 6 elements of time.
  15006. Don't touch the rest."
  15007. (let ((n 0))
  15008. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15009. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15010. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15011. "Difference between TIMESTAMP-STRING and now in days.
  15012. If SECONDS is non-nil, return the difference in seconds."
  15013. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15014. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15015. (funcall fdiff (current-time)))))
  15016. (defun org-deadline-close (timestamp-string &optional ndays)
  15017. "Is the time in TIMESTAMP-STRING close to the current date?"
  15018. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15019. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15020. (not (org-entry-is-done-p))))
  15021. (defun org-get-wdays (ts &optional delay zero-delay)
  15022. "Get the deadline lead time appropriate for timestring TS.
  15023. When DELAY is non-nil, get the delay time for scheduled items
  15024. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15025. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15026. don't try to find the delay cookie in the scheduled timestamp."
  15027. (let ((tv (if delay org-scheduled-delay-days
  15028. org-deadline-warning-days)))
  15029. (cond
  15030. ((or (and delay (< tv 0))
  15031. (and delay zero-delay (<= tv 0))
  15032. (and (not delay) (<= tv 0)))
  15033. ;; Enforce this value no matter what
  15034. (- tv))
  15035. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15036. ;; lead time is specified.
  15037. (floor (* (string-to-number (match-string 1 ts))
  15038. (cdr (assoc (match-string 2 ts)
  15039. '(("d" . 1) ("w" . 7)
  15040. ("m" . 30.4) ("y" . 365.25)
  15041. ("h" . 0.041667)))))))
  15042. ;; go for the default.
  15043. (t tv))))
  15044. (defun org-calendar-select-mouse (ev)
  15045. "Return to `org-read-date' with the date currently selected.
  15046. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15047. (interactive "e")
  15048. (mouse-set-point ev)
  15049. (when (calendar-cursor-to-date)
  15050. (let* ((date (calendar-cursor-to-date))
  15051. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15052. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15053. (if (active-minibuffer-window) (exit-minibuffer))))
  15054. (defun org-check-deadlines (ndays)
  15055. "Check if there are any deadlines due or past due.
  15056. A deadline is considered due if it happens within `org-deadline-warning-days'
  15057. days from today's date. If the deadline appears in an entry marked DONE,
  15058. it is not shown. The prefix arg NDAYS can be used to test that many
  15059. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15060. (interactive "P")
  15061. (let* ((org-warn-days
  15062. (cond
  15063. ((equal ndays '(4)) 100000)
  15064. (ndays (prefix-numeric-value ndays))
  15065. (t (abs org-deadline-warning-days))))
  15066. (case-fold-search nil)
  15067. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15068. (callback
  15069. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15070. (message "%d deadlines past-due or due within %d days"
  15071. (org-occur regexp nil callback)
  15072. org-warn-days)))
  15073. (defsubst org-re-timestamp (type)
  15074. "Return a regexp for timestamp TYPE.
  15075. Allowed values for TYPE are:
  15076. all: all timestamps
  15077. active: only active timestamps (<...>)
  15078. inactive: only inactive timestamps ([...])
  15079. scheduled: only scheduled timestamps
  15080. deadline: only deadline timestamps
  15081. When TYPE is nil, fall back on returning a regexp that matches
  15082. both scheduled and deadline timestamps."
  15083. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15084. ((eq type 'active) org-ts-regexp)
  15085. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15086. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15087. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15088. ((eq type 'scheduled-or-deadline)
  15089. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15090. (defun org-check-before-date (date)
  15091. "Check if there are deadlines or scheduled entries before DATE."
  15092. (interactive (list (org-read-date)))
  15093. (let ((case-fold-search nil)
  15094. (regexp (org-re-timestamp org-ts-type))
  15095. (callback
  15096. (lambda () (time-less-p
  15097. (org-time-string-to-time (match-string 1))
  15098. (org-time-string-to-time date)))))
  15099. (message "%d entries before %s"
  15100. (org-occur regexp nil callback) date)))
  15101. (defun org-check-after-date (date)
  15102. "Check if there are deadlines or scheduled entries after DATE."
  15103. (interactive (list (org-read-date)))
  15104. (let ((case-fold-search nil)
  15105. (regexp (org-re-timestamp org-ts-type))
  15106. (callback
  15107. (lambda () (not
  15108. (time-less-p
  15109. (org-time-string-to-time (match-string 1))
  15110. (org-time-string-to-time date))))))
  15111. (message "%d entries after %s"
  15112. (org-occur regexp nil callback) date)))
  15113. (defun org-check-dates-range (start-date end-date)
  15114. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15115. (interactive (list (org-read-date nil nil nil "Range starts")
  15116. (org-read-date nil nil nil "Range end")))
  15117. (let ((case-fold-search nil)
  15118. (regexp (org-re-timestamp org-ts-type))
  15119. (callback
  15120. (lambda ()
  15121. (let ((match (match-string 1)))
  15122. (and
  15123. (not (time-less-p
  15124. (org-time-string-to-time match)
  15125. (org-time-string-to-time start-date)))
  15126. (time-less-p
  15127. (org-time-string-to-time match)
  15128. (org-time-string-to-time end-date)))))))
  15129. (message "%d entries between %s and %s"
  15130. (org-occur regexp nil callback) start-date end-date)))
  15131. (defun org-evaluate-time-range (&optional to-buffer)
  15132. "Evaluate a time range by computing the difference between start and end.
  15133. Normally the result is just printed in the echo area, but with prefix arg
  15134. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15135. If the time range is actually in a table, the result is inserted into the
  15136. next column.
  15137. For time difference computation, a year is assumed to be exactly 365
  15138. days in order to avoid rounding problems."
  15139. (interactive "P")
  15140. (or
  15141. (org-clock-update-time-maybe)
  15142. (save-excursion
  15143. (unless (org-at-date-range-p t)
  15144. (goto-char (point-at-bol))
  15145. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15146. (if (not (org-at-date-range-p t))
  15147. (error "Not at a time-stamp range, and none found in current line")))
  15148. (let* ((ts1 (match-string 1))
  15149. (ts2 (match-string 2))
  15150. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15151. (match-end (match-end 0))
  15152. (time1 (org-time-string-to-time ts1))
  15153. (time2 (org-time-string-to-time ts2))
  15154. (t1 (org-float-time time1))
  15155. (t2 (org-float-time time2))
  15156. (diff (abs (- t2 t1)))
  15157. (negative (< (- t2 t1) 0))
  15158. ;; (ys (floor (* 365 24 60 60)))
  15159. (ds (* 24 60 60))
  15160. (hs (* 60 60))
  15161. (fy "%dy %dd %02d:%02d")
  15162. (fy1 "%dy %dd")
  15163. (fd "%dd %02d:%02d")
  15164. (fd1 "%dd")
  15165. (fh "%02d:%02d")
  15166. y d h m align)
  15167. (if havetime
  15168. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15169. y 0
  15170. d (floor (/ diff ds)) diff (mod diff ds)
  15171. h (floor (/ diff hs)) diff (mod diff hs)
  15172. m (floor (/ diff 60)))
  15173. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15174. y 0
  15175. d (floor (+ (/ diff ds) 0.5))
  15176. h 0 m 0))
  15177. (if (not to-buffer)
  15178. (message "%s" (org-make-tdiff-string y d h m))
  15179. (if (org-at-table-p)
  15180. (progn
  15181. (goto-char match-end)
  15182. (setq align t)
  15183. (and (looking-at " *|") (goto-char (match-end 0))))
  15184. (goto-char match-end))
  15185. (if (looking-at
  15186. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15187. (replace-match ""))
  15188. (if negative (insert " -"))
  15189. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15190. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15191. (insert " " (format fh h m))))
  15192. (if align (org-table-align))
  15193. (message "Time difference inserted")))))
  15194. (defun org-make-tdiff-string (y d h m)
  15195. (let ((fmt "")
  15196. (l nil))
  15197. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15198. l (push y l)))
  15199. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15200. l (push d l)))
  15201. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15202. l (push h l)))
  15203. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15204. l (push m l)))
  15205. (apply 'format fmt (nreverse l))))
  15206. (defun org-time-string-to-time (s &optional buffer pos)
  15207. "Convert a timestamp string into internal time."
  15208. (condition-case errdata
  15209. (apply 'encode-time (org-parse-time-string s))
  15210. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15211. s (if (not (and buffer pos))
  15212. ""
  15213. (format " at %d in buffer `%s'" pos buffer))
  15214. (cdr errdata)))))
  15215. (defun org-time-string-to-seconds (s)
  15216. "Convert a timestamp string to a number of seconds."
  15217. (org-float-time (org-time-string-to-time s)))
  15218. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15219. "Convert a time stamp to an absolute day number.
  15220. If there is a specifier for a cyclic time stamp, get the closest
  15221. date to DAYNR.
  15222. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15223. The variable date is bound by the calendar when this is called."
  15224. (cond
  15225. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15226. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15227. daynr
  15228. (+ daynr 1000)))
  15229. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15230. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15231. (time-to-days (current-time))) (match-string 0 s)
  15232. prefer show-all))
  15233. (t (time-to-days
  15234. (condition-case errdata
  15235. (apply 'encode-time (org-parse-time-string s))
  15236. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15237. s (if (not (and buffer pos))
  15238. ""
  15239. (format " at %d in buffer `%s'" pos buffer))
  15240. (cdr errdata))))))))
  15241. (defun org-days-to-iso-week (days)
  15242. "Return the iso week number."
  15243. (require 'cal-iso)
  15244. (car (calendar-iso-from-absolute days)))
  15245. (defun org-small-year-to-year (year)
  15246. "Convert 2-digit years into 4-digit years.
  15247. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15248. The year 2000 cannot be abbreviated. Any year larger than 99
  15249. is returned unchanged."
  15250. (if (< year 38)
  15251. (setq year (+ 2000 year))
  15252. (if (< year 100)
  15253. (setq year (+ 1900 year))))
  15254. year)
  15255. (defun org-time-from-absolute (d)
  15256. "Return the time corresponding to date D.
  15257. D may be an absolute day number, or a calendar-type list (month day year)."
  15258. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15259. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15260. (defun org-calendar-holiday ()
  15261. "List of holidays, for Diary display in Org-mode."
  15262. (require 'holidays)
  15263. (let ((hl (funcall
  15264. (if (fboundp 'calendar-check-holidays)
  15265. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15266. (if hl (mapconcat 'identity hl "; "))))
  15267. (defun org-diary-sexp-entry (sexp entry date)
  15268. "Process a SEXP diary ENTRY for DATE."
  15269. (require 'diary-lib)
  15270. (let ((result (if calendar-debug-sexp
  15271. (let ((stack-trace-on-error t))
  15272. (eval (car (read-from-string sexp))))
  15273. (condition-case nil
  15274. (eval (car (read-from-string sexp)))
  15275. (error
  15276. (beep)
  15277. (message "Bad sexp at line %d in %s: %s"
  15278. (org-current-line)
  15279. (buffer-file-name) sexp)
  15280. (sleep-for 2))))))
  15281. (cond ((stringp result) (split-string result "; "))
  15282. ((and (consp result)
  15283. (not (consp (cdr result)))
  15284. (stringp (cdr result))) (cdr result))
  15285. ((and (consp result)
  15286. (stringp (car result))) result)
  15287. (result entry))))
  15288. (defun org-diary-to-ical-string (frombuf)
  15289. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15290. This uses the icalendar.el library."
  15291. (let* ((tmpdir (if (featurep 'xemacs)
  15292. (temp-directory)
  15293. temporary-file-directory))
  15294. (tmpfile (make-temp-name
  15295. (expand-file-name "orgics" tmpdir)))
  15296. buf rtn b e)
  15297. (with-current-buffer frombuf
  15298. (icalendar-export-region (point-min) (point-max) tmpfile)
  15299. (setq buf (find-buffer-visiting tmpfile))
  15300. (set-buffer buf)
  15301. (goto-char (point-min))
  15302. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15303. (setq b (match-beginning 0)))
  15304. (goto-char (point-max))
  15305. (if (re-search-backward "^END:VEVENT" nil t)
  15306. (setq e (match-end 0)))
  15307. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15308. (kill-buffer buf)
  15309. (delete-file tmpfile)
  15310. rtn))
  15311. (defun org-closest-date (start current change prefer show-all)
  15312. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15313. When PREFER is `past', return a date that is either CURRENT or past.
  15314. When PREFER is `future', return a date that is either CURRENT or future.
  15315. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15316. ;; Make the proper lists from the dates
  15317. (catch 'exit
  15318. (let ((a1 '(("h" . hour)
  15319. ("d" . day)
  15320. ("w" . week)
  15321. ("m" . month)
  15322. ("y" . year)))
  15323. (shour (nth 2 (org-parse-time-string start)))
  15324. dn dw sday cday n1 n2 n0
  15325. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15326. (setq start (org-date-to-gregorian start)
  15327. current (org-date-to-gregorian
  15328. (if show-all
  15329. current
  15330. (time-to-days (current-time))))
  15331. sday (calendar-absolute-from-gregorian start)
  15332. cday (calendar-absolute-from-gregorian current))
  15333. (if (<= cday sday) (throw 'exit sday))
  15334. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15335. (setq dn (string-to-number (match-string 1 change))
  15336. dw (cdr (assoc (match-string 2 change) a1)))
  15337. (error "Invalid change specifier: %s" change))
  15338. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15339. (cond
  15340. ((eq dw 'hour)
  15341. (let ((missing-hours
  15342. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15343. dn)))
  15344. (setq n1 (if (zerop missing-hours) cday
  15345. (- cday (1+ (floor (/ missing-hours 24)))))
  15346. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15347. ((eq dw 'day)
  15348. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15349. n2 (+ n1 dn)))
  15350. ((eq dw 'year)
  15351. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15352. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15353. (setq date1 (list m d y1)
  15354. n1 (calendar-absolute-from-gregorian date1)
  15355. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15356. n2 (calendar-absolute-from-gregorian date2)))
  15357. ((eq dw 'month)
  15358. ;; approx number of month between the two dates
  15359. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15360. ;; How often does dn fit in there?
  15361. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15362. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15363. m (+ m nm)
  15364. ny (floor (/ m 12))
  15365. y (+ y ny)
  15366. m (- m (* ny 12)))
  15367. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15368. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15369. (setq m2 (+ m dn) y2 y)
  15370. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15371. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15372. (while (<= n2 cday)
  15373. (setq n1 n2 m m2 y y2)
  15374. (setq m2 (+ m dn) y2 y)
  15375. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15376. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15377. ;; Make sure n1 is the earlier date
  15378. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15379. (if show-all
  15380. (cond
  15381. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15382. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15383. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15384. (cond
  15385. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15386. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15387. (t (if (= cday n1) n1 n2)))))))
  15388. (defun org-date-to-gregorian (date)
  15389. "Turn any specification of DATE into a Gregorian date for the calendar."
  15390. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15391. ((and (listp date) (= (length date) 3)) date)
  15392. ((stringp date)
  15393. (setq date (org-parse-time-string date))
  15394. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15395. ((listp date)
  15396. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15397. (defun org-parse-time-string (s &optional nodefault)
  15398. "Parse the standard Org-mode time string.
  15399. This should be a lot faster than the normal `parse-time-string'.
  15400. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15401. hour and minute fields will be nil if not given."
  15402. (cond ((string-match org-ts-regexp0 s)
  15403. (list 0
  15404. (if (or (match-beginning 8) (not nodefault))
  15405. (string-to-number (or (match-string 8 s) "0")))
  15406. (if (or (match-beginning 7) (not nodefault))
  15407. (string-to-number (or (match-string 7 s) "0")))
  15408. (string-to-number (match-string 4 s))
  15409. (string-to-number (match-string 3 s))
  15410. (string-to-number (match-string 2 s))
  15411. nil nil nil))
  15412. ((string-match "^<[^>]+>$" s)
  15413. (decode-time (seconds-to-time (org-matcher-time s))))
  15414. (t (error "Not a standard Org-mode time string: %s" s))))
  15415. (defun org-timestamp-up (&optional arg)
  15416. "Increase the date item at the cursor by one.
  15417. If the cursor is on the year, change the year. If it is on the month,
  15418. the day or the time, change that.
  15419. With prefix ARG, change by that many units."
  15420. (interactive "p")
  15421. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15422. (defun org-timestamp-down (&optional arg)
  15423. "Decrease the date item at the cursor by one.
  15424. If the cursor is on the year, change the year. If it is on the month,
  15425. the day or the time, change that.
  15426. With prefix ARG, change by that many units."
  15427. (interactive "p")
  15428. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15429. (defun org-timestamp-up-day (&optional arg)
  15430. "Increase the date in the time stamp by one day.
  15431. With prefix ARG, change that many days."
  15432. (interactive "p")
  15433. (if (and (not (org-at-timestamp-p t))
  15434. (org-at-heading-p))
  15435. (org-todo 'up)
  15436. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15437. (defun org-timestamp-down-day (&optional arg)
  15438. "Decrease the date in the time stamp by one day.
  15439. With prefix ARG, change that many days."
  15440. (interactive "p")
  15441. (if (and (not (org-at-timestamp-p t))
  15442. (org-at-heading-p))
  15443. (org-todo 'down)
  15444. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15445. (defun org-at-timestamp-p (&optional inactive-ok)
  15446. "Determine if the cursor is in or at a timestamp."
  15447. (interactive)
  15448. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15449. (pos (point))
  15450. (ans (or (looking-at tsr)
  15451. (save-excursion
  15452. (skip-chars-backward "^[<\n\r\t")
  15453. (if (> (point) (point-min)) (backward-char 1))
  15454. (and (looking-at tsr)
  15455. (> (- (match-end 0) pos) -1))))))
  15456. (and ans
  15457. (boundp 'org-ts-what)
  15458. (setq org-ts-what
  15459. (cond
  15460. ((= pos (match-beginning 0)) 'bracket)
  15461. ;; Point is considered to be "on the bracket" whether
  15462. ;; it's really on it or right after it.
  15463. ((= pos (1- (match-end 0))) 'bracket)
  15464. ((= pos (match-end 0)) 'after)
  15465. ((org-pos-in-match-range pos 2) 'year)
  15466. ((org-pos-in-match-range pos 3) 'month)
  15467. ((org-pos-in-match-range pos 7) 'hour)
  15468. ((org-pos-in-match-range pos 8) 'minute)
  15469. ((or (org-pos-in-match-range pos 4)
  15470. (org-pos-in-match-range pos 5)) 'day)
  15471. ((and (> pos (or (match-end 8) (match-end 5)))
  15472. (< pos (match-end 0)))
  15473. (- pos (or (match-end 8) (match-end 5))))
  15474. (t 'day))))
  15475. ans))
  15476. (defun org-toggle-timestamp-type ()
  15477. "Toggle the type (<active> or [inactive]) of a time stamp."
  15478. (interactive)
  15479. (when (org-at-timestamp-p t)
  15480. (let ((beg (match-beginning 0)) (end (match-end 0))
  15481. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15482. (save-excursion
  15483. (goto-char beg)
  15484. (while (re-search-forward "[][<>]" end t)
  15485. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15486. t t)))
  15487. (message "Timestamp is now %sactive"
  15488. (if (equal (char-after beg) ?<) "" "in")))))
  15489. (defun org-at-clock-log-p nil
  15490. "Is the cursor on the clock log line?"
  15491. (save-excursion
  15492. (move-beginning-of-line 1)
  15493. (looking-at "^[ \t]*CLOCK:")))
  15494. (defvar org-clock-history) ; defined in org-clock.el
  15495. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15496. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15497. "Change the date in the time stamp at point.
  15498. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15499. `year', `minute', `second'. If WHAT is not given, the cursor position
  15500. in the timestamp determines what will be changed.
  15501. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15502. (let ((origin (point)) origin-cat
  15503. with-hm inactive
  15504. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15505. org-ts-what
  15506. extra rem
  15507. ts time time0 fixnext clrgx)
  15508. (if (not (org-at-timestamp-p t))
  15509. (error "Not at a timestamp"))
  15510. (if (and (not what) (eq org-ts-what 'bracket))
  15511. (org-toggle-timestamp-type)
  15512. ;; Point isn't on brackets. Remember the part of the time-stamp
  15513. ;; the point was in. Indeed, size of time-stamps may change,
  15514. ;; but point must be kept in the same category nonetheless.
  15515. (setq origin-cat org-ts-what)
  15516. (if (and (not what) (not (eq org-ts-what 'day))
  15517. org-display-custom-times
  15518. (get-text-property (point) 'display)
  15519. (not (get-text-property (1- (point)) 'display)))
  15520. (setq org-ts-what 'day))
  15521. (setq org-ts-what (or what org-ts-what)
  15522. inactive (= (char-after (match-beginning 0)) ?\[)
  15523. ts (match-string 0))
  15524. (replace-match "")
  15525. (when (string-match
  15526. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15527. ts)
  15528. (setq extra (match-string 1 ts))
  15529. (if suppress-tmp-delay
  15530. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15531. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15532. (setq with-hm t))
  15533. (setq time0 (org-parse-time-string ts))
  15534. (when (and updown
  15535. (eq org-ts-what 'minute)
  15536. (not current-prefix-arg))
  15537. ;; This looks like s-up and s-down. Change by one rounding step.
  15538. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15539. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15540. (setcar (cdr time0) (+ (nth 1 time0)
  15541. (if (> n 0) (- rem) (- dm rem))))))
  15542. (setq time
  15543. (encode-time (or (car time0) 0)
  15544. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15545. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15546. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15547. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15548. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15549. (nthcdr 6 time0)))
  15550. (when (and (member org-ts-what '(hour minute))
  15551. extra
  15552. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15553. (setq extra (org-modify-ts-extra
  15554. extra
  15555. (if (eq org-ts-what 'hour) 2 5)
  15556. n dm)))
  15557. (when (integerp org-ts-what)
  15558. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15559. (if (eq what 'calendar)
  15560. (let ((cal-date (org-get-date-from-calendar)))
  15561. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15562. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15563. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15564. (setcar time0 (or (car time0) 0))
  15565. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15566. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15567. (setq time (apply 'encode-time time0))))
  15568. ;; Insert the new time-stamp, and ensure point stays in the same
  15569. ;; category as before (i.e. not after the last position in that
  15570. ;; category).
  15571. (let ((pos (point)))
  15572. ;; Stay before inserted string. `save-excursion' is of no use.
  15573. (setq org-last-changed-timestamp
  15574. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15575. (goto-char pos))
  15576. (save-match-data
  15577. (looking-at org-ts-regexp3)
  15578. (goto-char (cond
  15579. ;; `day' category ends before `hour' if any, or at
  15580. ;; the end of the day name.
  15581. ((eq origin-cat 'day)
  15582. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15583. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15584. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15585. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15586. ;; `year' and `month' have both fixed size: point
  15587. ;; couldn't have moved into another part.
  15588. (t origin))))
  15589. ;; Update clock if on a CLOCK line.
  15590. (org-clock-update-time-maybe)
  15591. ;; Maybe adjust the closest clock in `org-clock-history'
  15592. (when org-clock-adjust-closest
  15593. (if (not (and (org-at-clock-log-p)
  15594. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15595. org-clock-history))))))
  15596. (message "No clock to adjust")
  15597. (cond ((save-excursion ; fix previous clock?
  15598. (re-search-backward org-ts-regexp0 nil t)
  15599. (org-looking-back (concat org-clock-string " \\[")))
  15600. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15601. ((save-excursion ; fix next clock?
  15602. (re-search-backward org-ts-regexp0 nil t)
  15603. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15604. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15605. (save-window-excursion
  15606. ;; Find closest clock to point, adjust the previous/next one in history
  15607. (let* ((p (save-excursion (org-back-to-heading t)))
  15608. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15609. (clfixnth
  15610. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15611. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15612. (if (not clfixpos)
  15613. (message "No clock to adjust")
  15614. (save-excursion
  15615. (org-goto-marker-or-bmk clfixpos)
  15616. (org-show-subtree)
  15617. (when (re-search-forward clrgx nil t)
  15618. (goto-char (match-beginning 1))
  15619. (let (org-clock-adjust-closest)
  15620. (org-timestamp-change n org-ts-what updown))
  15621. (message "Clock adjusted in %s for heading: %s"
  15622. (file-name-nondirectory (buffer-file-name))
  15623. (org-get-heading t t)))))))))
  15624. ;; Try to recenter the calendar window, if any.
  15625. (if (and org-calendar-follow-timestamp-change
  15626. (get-buffer-window "*Calendar*" t)
  15627. (memq org-ts-what '(day month year)))
  15628. (org-recenter-calendar (time-to-days time))))))
  15629. (defun org-modify-ts-extra (s pos n dm)
  15630. "Change the different parts of the lead-time and repeat fields in timestamp."
  15631. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15632. ng h m new rem)
  15633. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15634. (cond
  15635. ((or (org-pos-in-match-range pos 2)
  15636. (org-pos-in-match-range pos 3))
  15637. (setq m (string-to-number (match-string 3 s))
  15638. h (string-to-number (match-string 2 s)))
  15639. (if (org-pos-in-match-range pos 2)
  15640. (setq h (+ h n))
  15641. (setq n (* dm (org-no-warnings (signum n))))
  15642. (when (not (= 0 (setq rem (% m dm))))
  15643. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15644. (setq m (+ m n)))
  15645. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15646. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15647. (setq h (min 24 (max 0 h)))
  15648. (setq ng 1 new (format "-%02d:%02d" h m)))
  15649. ((org-pos-in-match-range pos 6)
  15650. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15651. ((org-pos-in-match-range pos 5)
  15652. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15653. ((org-pos-in-match-range pos 9)
  15654. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15655. ((org-pos-in-match-range pos 8)
  15656. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15657. (when ng
  15658. (setq s (concat
  15659. (substring s 0 (match-beginning ng))
  15660. new
  15661. (substring s (match-end ng))))))
  15662. s))
  15663. (defun org-recenter-calendar (date)
  15664. "If the calendar is visible, recenter it to DATE."
  15665. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15666. (when cwin
  15667. (let ((calendar-move-hook nil))
  15668. (with-selected-window cwin
  15669. (calendar-goto-date (if (listp date) date
  15670. (calendar-gregorian-from-absolute date))))))))
  15671. (defun org-goto-calendar (&optional arg)
  15672. "Go to the Emacs calendar at the current date.
  15673. If there is a time stamp in the current line, go to that date.
  15674. A prefix ARG can be used to force the current date."
  15675. (interactive "P")
  15676. (let ((tsr org-ts-regexp) diff
  15677. (calendar-move-hook nil)
  15678. (calendar-view-holidays-initially-flag nil)
  15679. (calendar-view-diary-initially-flag nil))
  15680. (if (or (org-at-timestamp-p)
  15681. (save-excursion
  15682. (beginning-of-line 1)
  15683. (looking-at (concat ".*" tsr))))
  15684. (let ((d1 (time-to-days (current-time)))
  15685. (d2 (time-to-days
  15686. (org-time-string-to-time (match-string 1)))))
  15687. (setq diff (- d2 d1))))
  15688. (calendar)
  15689. (calendar-goto-today)
  15690. (if (and diff (not arg)) (calendar-forward-day diff))))
  15691. (defun org-get-date-from-calendar ()
  15692. "Return a list (month day year) of date at point in calendar."
  15693. (with-current-buffer "*Calendar*"
  15694. (save-match-data
  15695. (calendar-cursor-to-date))))
  15696. (defun org-date-from-calendar ()
  15697. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15698. If there is already a time stamp at the cursor position, update it."
  15699. (interactive)
  15700. (if (org-at-timestamp-p t)
  15701. (org-timestamp-change 0 'calendar)
  15702. (let ((cal-date (org-get-date-from-calendar)))
  15703. (org-insert-time-stamp
  15704. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15705. (defcustom org-effort-durations
  15706. `(("h" . 60)
  15707. ("d" . ,(* 60 8))
  15708. ("w" . ,(* 60 8 5))
  15709. ("m" . ,(* 60 8 5 4))
  15710. ("y" . ,(* 60 8 5 40)))
  15711. "Conversion factor to minutes for an effort modifier.
  15712. Each entry has the form (MODIFIER . MINUTES).
  15713. In an effort string, a number followed by MODIFIER is multiplied
  15714. by the specified number of MINUTES to obtain an effort in
  15715. minutes.
  15716. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15717. effort string \"2hours\" is equivalent to 120 minutes."
  15718. :group 'org-agenda
  15719. :version "24.1"
  15720. :type '(alist :key-type (string :tag "Modifier")
  15721. :value-type (number :tag "Minutes")))
  15722. (defun org-minutes-to-clocksum-string (m)
  15723. "Format number of minutes as a clocksum string.
  15724. The format is determined by `org-time-clocksum-format',
  15725. `org-time-clocksum-use-fractional' and
  15726. `org-time-clocksum-fractional-format' and
  15727. `org-time-clocksum-use-effort-durations'."
  15728. (let ((clocksum "") h d w mo y fmt n)
  15729. (setq h (if org-time-clocksum-use-effort-durations
  15730. (cdr (assoc "h" org-effort-durations)) 60)
  15731. d (if org-time-clocksum-use-effort-durations
  15732. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15733. w (if org-time-clocksum-use-effort-durations
  15734. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15735. mo (if org-time-clocksum-use-effort-durations
  15736. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15737. y (if org-time-clocksum-use-effort-durations
  15738. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15739. ;; fractional format
  15740. (if org-time-clocksum-use-fractional
  15741. (cond
  15742. ;; single format string
  15743. ((stringp org-time-clocksum-fractional-format)
  15744. (format org-time-clocksum-fractional-format (/ m (float h))))
  15745. ;; choice of fractional formats for different time units
  15746. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15747. (> (/ (truncate m) (* y d h)) 0))
  15748. (format fmt (/ m (* y d (float h)))))
  15749. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15750. (> (/ (truncate m) (* mo d h)) 0))
  15751. (format fmt (/ m (* mo d (float h)))))
  15752. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15753. (> (/ (truncate m) (* w d h)) 0))
  15754. (format fmt (/ m (* w d (float h)))))
  15755. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15756. (> (/ (truncate m) (* d h)) 0))
  15757. (format fmt (/ m (* d (float h)))))
  15758. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15759. (> (/ (truncate m) h) 0))
  15760. (format fmt (/ m (float h))))
  15761. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15762. (format fmt m))
  15763. ;; fall back to smallest time unit with a format
  15764. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15765. (format fmt (/ m (float h))))
  15766. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15767. (format fmt (/ m (* d (float h)))))
  15768. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15769. (format fmt (/ m (* w d (float h)))))
  15770. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15771. (format fmt (/ m (* mo d (float h)))))
  15772. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15773. (format fmt (/ m (* y d (float h))))))
  15774. ;; standard (non-fractional) format, with single format string
  15775. (if (stringp org-time-clocksum-format)
  15776. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15777. ;; separate formats components
  15778. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15779. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15780. (plist-get org-time-clocksum-format :require-years))
  15781. (setq clocksum (concat clocksum (format fmt n))
  15782. m (- m (* n y d h))))
  15783. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15784. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15785. (plist-get org-time-clocksum-format :require-months))
  15786. (setq clocksum (concat clocksum (format fmt n))
  15787. m (- m (* n mo d h))))
  15788. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15789. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15790. (plist-get org-time-clocksum-format :require-weeks))
  15791. (setq clocksum (concat clocksum (format fmt n))
  15792. m (- m (* n w d h))))
  15793. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15794. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15795. (plist-get org-time-clocksum-format :require-days))
  15796. (setq clocksum (concat clocksum (format fmt n))
  15797. m (- m (* n d h))))
  15798. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15799. (or (> (setq n (/ (truncate m) h)) 0)
  15800. (plist-get org-time-clocksum-format :require-hours))
  15801. (setq clocksum (concat clocksum (format fmt n))
  15802. m (- m (* n h))))
  15803. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15804. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15805. (setq clocksum (concat clocksum (format fmt m))))
  15806. ;; return formatted time duration
  15807. clocksum))))
  15808. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15809. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15810. "Org mode version 8.0")
  15811. (defun org-hours-to-clocksum-string (n)
  15812. (org-minutes-to-clocksum-string (* n 60)))
  15813. (defun org-hh:mm-string-to-minutes (s)
  15814. "Convert a string H:MM to a number of minutes.
  15815. If the string is just a number, interpret it as minutes.
  15816. In fact, the first hh:mm or number in the string will be taken,
  15817. there can be extra stuff in the string.
  15818. If no number is found, the return value is 0."
  15819. (cond
  15820. ((integerp s) s)
  15821. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15822. (+ (* (string-to-number (match-string 1 s)) 60)
  15823. (string-to-number (match-string 2 s))))
  15824. ((string-match "\\([0-9]+\\)" s)
  15825. (string-to-number (match-string 1 s)))
  15826. (t 0)))
  15827. (defcustom org-image-actual-width t
  15828. "Should we use the actual width of images when inlining them?
  15829. When set to `t', always use the image width.
  15830. When set to a number, use imagemagick (when available) to set
  15831. the image's width to this value.
  15832. When set to a number in a list, try to get the width from the
  15833. #+ATTR.* keyword if it matches a width specification like
  15834. width=\"[0-9]+\", and fall back on that number if none is found.
  15835. When set to nil, try to get the width from an #+ATTR.* keyword
  15836. and fall back on the original width if none is found.
  15837. This requires Emacs >= 24.1, build with imagemagick support."
  15838. :group 'org-appearance
  15839. :version "24.4"
  15840. :package-version '(Org . "8.0")
  15841. :type '(choice
  15842. (const :tag "Use the image width" t)
  15843. (integer :tag "Use a number of pixels")
  15844. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15845. (const :tag "Use #+ATTR* or don't resize" nil)))
  15846. (defcustom org-agenda-inhibit-startup t
  15847. "Inhibit startup when preparing agenda buffers.
  15848. When this variable is `t' (the default), the initialization of
  15849. the Org agenda buffers is inhibited: e.g. the visibility state
  15850. is not set, the tables are not re-aligned, etc."
  15851. :type 'boolean
  15852. :version "24.3"
  15853. :group 'org-agenda)
  15854. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15855. "Convert a duration string S to minutes.
  15856. A bare number is interpreted as minutes, modifiers can be set by
  15857. customizing `org-effort-durations' (which see).
  15858. Entries containing a colon are interpreted as H:MM by
  15859. `org-hh:mm-string-to-minutes'."
  15860. (let ((result 0)
  15861. (re (concat "\\([0-9.]+\\) *\\("
  15862. (regexp-opt (mapcar 'car org-effort-durations))
  15863. "\\)")))
  15864. (while (string-match re s)
  15865. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15866. (string-to-number (match-string 1 s))))
  15867. (setq s (replace-match "" nil t s)))
  15868. (setq result (floor result))
  15869. (incf result (org-hh:mm-string-to-minutes s))
  15870. (if output-to-string (number-to-string result) result)))
  15871. ;;;; Files
  15872. (defun org-save-all-org-buffers ()
  15873. "Save all Org-mode buffers without user confirmation."
  15874. (interactive)
  15875. (message "Saving all Org-mode buffers...")
  15876. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15877. (when (featurep 'org-id) (org-id-locations-save))
  15878. (message "Saving all Org-mode buffers... done"))
  15879. (defun org-revert-all-org-buffers ()
  15880. "Revert all Org-mode buffers.
  15881. Prompt for confirmation when there are unsaved changes.
  15882. Be sure you know what you are doing before letting this function
  15883. overwrite your changes.
  15884. This function is useful in a setup where one tracks org files
  15885. with a version control system, to revert on one machine after pulling
  15886. changes from another. I believe the procedure must be like this:
  15887. 1. M-x org-save-all-org-buffers
  15888. 2. Pull changes from the other machine, resolve conflicts
  15889. 3. M-x org-revert-all-org-buffers"
  15890. (interactive)
  15891. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15892. (error "Abort"))
  15893. (save-excursion
  15894. (save-window-excursion
  15895. (mapc
  15896. (lambda (b)
  15897. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15898. (with-current-buffer b buffer-file-name))
  15899. (org-pop-to-buffer-same-window b)
  15900. (revert-buffer t 'no-confirm)))
  15901. (buffer-list))
  15902. (when (and (featurep 'org-id) org-id-track-globally)
  15903. (org-id-locations-load)))))
  15904. ;;;; Agenda files
  15905. ;;;###autoload
  15906. (defun org-switchb (&optional arg)
  15907. "Switch between Org buffers.
  15908. With one prefix argument, restrict available buffers to files.
  15909. With two prefix arguments, restrict available buffers to agenda files.
  15910. Defaults to `iswitchb' for buffer name completion.
  15911. Set `org-completion-use-ido' to make it use ido instead."
  15912. (interactive "P")
  15913. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15914. ((equal arg '(16)) (org-buffer-list 'agenda))
  15915. (t (org-buffer-list))))
  15916. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15917. (org-completion-use-ido org-completion-use-ido))
  15918. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15919. (setq org-completion-use-iswitchb t))
  15920. (org-pop-to-buffer-same-window
  15921. (org-icompleting-read "Org buffer: "
  15922. (mapcar 'list (mapcar 'buffer-name blist))
  15923. nil t))))
  15924. ;;; Define some older names previously used for this functionality
  15925. ;;;###autoload
  15926. (defalias 'org-ido-switchb 'org-switchb)
  15927. ;;;###autoload
  15928. (defalias 'org-iswitchb 'org-switchb)
  15929. (defun org-buffer-list (&optional predicate exclude-tmp)
  15930. "Return a list of Org buffers.
  15931. PREDICATE can be `export', `files' or `agenda'.
  15932. export restrict the list to Export buffers.
  15933. files restrict the list to buffers visiting Org files.
  15934. agenda restrict the list to buffers visiting agenda files.
  15935. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15936. (let* ((bfn nil)
  15937. (agenda-files (and (eq predicate 'agenda)
  15938. (mapcar 'file-truename (org-agenda-files t))))
  15939. (filter
  15940. (cond
  15941. ((eq predicate 'files)
  15942. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15943. ((eq predicate 'export)
  15944. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15945. ((eq predicate 'agenda)
  15946. (lambda (b)
  15947. (with-current-buffer b
  15948. (and (derived-mode-p 'org-mode)
  15949. (setq bfn (buffer-file-name b))
  15950. (member (file-truename bfn) agenda-files)))))
  15951. (t (lambda (b) (with-current-buffer b
  15952. (or (derived-mode-p 'org-mode)
  15953. (string-match "\*Org .*Export"
  15954. (buffer-name b)))))))))
  15955. (delq nil
  15956. (mapcar
  15957. (lambda(b)
  15958. (if (and (funcall filter b)
  15959. (or (not exclude-tmp)
  15960. (not (string-match "tmp" (buffer-name b)))))
  15961. b
  15962. nil))
  15963. (buffer-list)))))
  15964. (defun org-agenda-files (&optional unrestricted archives)
  15965. "Get the list of agenda files.
  15966. Optional UNRESTRICTED means return the full list even if a restriction
  15967. is currently in place.
  15968. When ARCHIVES is t, include all archive files that are really being
  15969. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15970. `org-agenda-archives-mode' is t."
  15971. (let ((files
  15972. (cond
  15973. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15974. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15975. ((listp org-agenda-files) org-agenda-files)
  15976. (t (error "Invalid value of `org-agenda-files'")))))
  15977. (setq files (apply 'append
  15978. (mapcar (lambda (f)
  15979. (if (file-directory-p f)
  15980. (directory-files
  15981. f t org-agenda-file-regexp)
  15982. (list f)))
  15983. files)))
  15984. (when org-agenda-skip-unavailable-files
  15985. (setq files (delq nil
  15986. (mapcar (function
  15987. (lambda (file)
  15988. (and (file-readable-p file) file)))
  15989. files))))
  15990. (when (or (eq archives t)
  15991. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15992. (setq files (org-add-archive-files files)))
  15993. files))
  15994. (defun org-agenda-file-p (&optional file)
  15995. "Return non-nil, if FILE is an agenda file.
  15996. If FILE is omitted, use the file associated with the current
  15997. buffer."
  15998. (member (or file (buffer-file-name))
  15999. (org-agenda-files t)))
  16000. (defun org-edit-agenda-file-list ()
  16001. "Edit the list of agenda files.
  16002. Depending on setup, this either uses customize to edit the variable
  16003. `org-agenda-files', or it visits the file that is holding the list. In the
  16004. latter case, the buffer is set up in a way that saving it automatically kills
  16005. the buffer and restores the previous window configuration."
  16006. (interactive)
  16007. (if (stringp org-agenda-files)
  16008. (let ((cw (current-window-configuration)))
  16009. (find-file org-agenda-files)
  16010. (org-set-local 'org-window-configuration cw)
  16011. (org-add-hook 'after-save-hook
  16012. (lambda ()
  16013. (set-window-configuration
  16014. (prog1 org-window-configuration
  16015. (kill-buffer (current-buffer))))
  16016. (org-install-agenda-files-menu)
  16017. (message "New agenda file list installed"))
  16018. nil 'local)
  16019. (message "%s" (substitute-command-keys
  16020. "Edit list and finish with \\[save-buffer]")))
  16021. (customize-variable 'org-agenda-files)))
  16022. (defun org-store-new-agenda-file-list (list)
  16023. "Set new value for the agenda file list and save it correctly."
  16024. (if (stringp org-agenda-files)
  16025. (let ((fe (org-read-agenda-file-list t)) b u)
  16026. (while (setq b (find-buffer-visiting org-agenda-files))
  16027. (kill-buffer b))
  16028. (with-temp-file org-agenda-files
  16029. (insert
  16030. (mapconcat
  16031. (lambda (f) ;; Keep un-expanded entries.
  16032. (if (setq u (assoc f fe))
  16033. (cdr u)
  16034. f))
  16035. list "\n")
  16036. "\n")))
  16037. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16038. (org-insert-mode-line-in-empty-file nil))
  16039. (setq org-agenda-files list)
  16040. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16041. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16042. "Read the list of agenda files from a file.
  16043. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16044. filenames, used by `org-store-new-agenda-file-list' to write back
  16045. un-expanded file names."
  16046. (when (file-directory-p org-agenda-files)
  16047. (error "`org-agenda-files' cannot be a single directory"))
  16048. (when (stringp org-agenda-files)
  16049. (with-temp-buffer
  16050. (insert-file-contents org-agenda-files)
  16051. (mapcar
  16052. (lambda (f)
  16053. (let ((e (expand-file-name (substitute-in-file-name f)
  16054. org-directory)))
  16055. (if pair-with-expansion
  16056. (cons e f)
  16057. e)))
  16058. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16059. ;;;###autoload
  16060. (defun org-cycle-agenda-files ()
  16061. "Cycle through the files in `org-agenda-files'.
  16062. If the current buffer visits an agenda file, find the next one in the list.
  16063. If the current buffer does not, find the first agenda file."
  16064. (interactive)
  16065. (let* ((fs (org-agenda-files t))
  16066. (files (append fs (list (car fs))))
  16067. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16068. file)
  16069. (unless files (error "No agenda files"))
  16070. (catch 'exit
  16071. (while (setq file (pop files))
  16072. (if (equal (file-truename file) tcf)
  16073. (when (car files)
  16074. (find-file (car files))
  16075. (throw 'exit t))))
  16076. (find-file (car fs)))
  16077. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16078. (defun org-agenda-file-to-front (&optional to-end)
  16079. "Move/add the current file to the top of the agenda file list.
  16080. If the file is not present in the list, it is added to the front. If it is
  16081. present, it is moved there. With optional argument TO-END, add/move to the
  16082. end of the list."
  16083. (interactive "P")
  16084. (let ((org-agenda-skip-unavailable-files nil)
  16085. (file-alist (mapcar (lambda (x)
  16086. (cons (file-truename x) x))
  16087. (org-agenda-files t)))
  16088. (ctf (file-truename
  16089. (or buffer-file-name
  16090. (error "Please save the current buffer to a file"))))
  16091. x had)
  16092. (setq x (assoc ctf file-alist) had x)
  16093. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16094. (if to-end
  16095. (setq file-alist (append (delq x file-alist) (list x)))
  16096. (setq file-alist (cons x (delq x file-alist))))
  16097. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16098. (org-install-agenda-files-menu)
  16099. (message "File %s to %s of agenda file list"
  16100. (if had "moved" "added") (if to-end "end" "front"))))
  16101. (defun org-remove-file (&optional file)
  16102. "Remove current file from the list of files in variable `org-agenda-files'.
  16103. These are the files which are being checked for agenda entries.
  16104. Optional argument FILE means use this file instead of the current."
  16105. (interactive)
  16106. (let* ((org-agenda-skip-unavailable-files nil)
  16107. (file (or file buffer-file-name
  16108. (error "Current buffer does not visit a file")))
  16109. (true-file (file-truename file))
  16110. (afile (abbreviate-file-name file))
  16111. (files (delq nil (mapcar
  16112. (lambda (x)
  16113. (if (equal true-file
  16114. (file-truename x))
  16115. nil x))
  16116. (org-agenda-files t)))))
  16117. (if (not (= (length files) (length (org-agenda-files t))))
  16118. (progn
  16119. (org-store-new-agenda-file-list files)
  16120. (org-install-agenda-files-menu)
  16121. (message "Removed file: %s" afile))
  16122. (message "File was not in list: %s (not removed)" afile))))
  16123. (defun org-file-menu-entry (file)
  16124. (vector file (list 'find-file file) t))
  16125. (defun org-check-agenda-file (file)
  16126. "Make sure FILE exists. If not, ask user what to do."
  16127. (when (not (file-exists-p file))
  16128. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16129. (abbreviate-file-name file))
  16130. (let ((r (downcase (read-char-exclusive))))
  16131. (cond
  16132. ((equal r ?r)
  16133. (org-remove-file file)
  16134. (throw 'nextfile t))
  16135. (t (error "Abort"))))))
  16136. (defun org-get-agenda-file-buffer (file)
  16137. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16138. it to the list of buffers which might be released later."
  16139. (let ((buf (org-find-base-buffer-visiting file)))
  16140. (if buf
  16141. buf ; just return it
  16142. ;; Make a new buffer and remember it
  16143. (setq buf (find-file-noselect file))
  16144. (if buf (push buf org-agenda-new-buffers))
  16145. buf)))
  16146. (defun org-release-buffers (blist)
  16147. "Release all buffers in list, asking the user for confirmation when needed.
  16148. When a buffer is unmodified, it is just killed. When modified, it is saved
  16149. \(if the user agrees) and then killed."
  16150. (let (buf file)
  16151. (while (setq buf (pop blist))
  16152. (setq file (buffer-file-name buf))
  16153. (when (and (buffer-modified-p buf)
  16154. file
  16155. (y-or-n-p (format "Save file %s? " file)))
  16156. (with-current-buffer buf (save-buffer)))
  16157. (kill-buffer buf))))
  16158. (defun org-agenda-prepare-buffers (files)
  16159. "Create buffers for all agenda files, protect archived trees and comments."
  16160. (interactive)
  16161. (let ((pa '(:org-archived t))
  16162. (pc '(:org-comment t))
  16163. (pall '(:org-archived t :org-comment t))
  16164. (inhibit-read-only t)
  16165. (org-inhibit-startup org-agenda-inhibit-startup)
  16166. (rea (concat ":" org-archive-tag ":"))
  16167. file re)
  16168. (save-excursion
  16169. (save-restriction
  16170. (while (setq file (pop files))
  16171. (catch 'nextfile
  16172. (if (bufferp file)
  16173. (set-buffer file)
  16174. (org-check-agenda-file file)
  16175. (set-buffer (org-get-agenda-file-buffer file)))
  16176. (widen)
  16177. (org-refresh-category-properties)
  16178. (org-refresh-properties org-effort-property 'org-effort)
  16179. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16180. (setq org-todo-keywords-for-agenda
  16181. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16182. (setq org-done-keywords-for-agenda
  16183. (append org-done-keywords-for-agenda org-done-keywords))
  16184. (setq org-todo-keyword-alist-for-agenda
  16185. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16186. (setq org-drawers-for-agenda
  16187. (append org-drawers-for-agenda org-drawers))
  16188. (setq org-tag-alist-for-agenda
  16189. (append org-tag-alist-for-agenda org-tag-alist))
  16190. (org-with-silent-modifications
  16191. (save-excursion
  16192. (remove-text-properties (point-min) (point-max) pall)
  16193. (when org-agenda-skip-archived-trees
  16194. (goto-char (point-min))
  16195. (while (re-search-forward rea nil t)
  16196. (if (org-at-heading-p t)
  16197. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16198. (goto-char (point-min))
  16199. (setq re (format org-heading-keyword-regexp-format
  16200. org-comment-string))
  16201. (while (re-search-forward re nil t)
  16202. (add-text-properties
  16203. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16204. (setq org-todo-keywords-for-agenda
  16205. (org-uniquify org-todo-keywords-for-agenda))
  16206. (setq org-todo-keyword-alist-for-agenda
  16207. (org-uniquify org-todo-keyword-alist-for-agenda)
  16208. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16209. ;;;; CDLaTeX minor mode
  16210. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16211. "Keymap for the minor `org-cdlatex-mode'.")
  16212. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16213. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16214. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16215. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16216. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16217. (defvar org-cdlatex-texmathp-advice-is-done nil
  16218. "Flag remembering if we have applied the advice to texmathp already.")
  16219. (define-minor-mode org-cdlatex-mode
  16220. "Toggle the minor `org-cdlatex-mode'.
  16221. This mode supports entering LaTeX environment and math in LaTeX fragments
  16222. in Org-mode.
  16223. \\{org-cdlatex-mode-map}"
  16224. nil " OCDL" nil
  16225. (when org-cdlatex-mode
  16226. (require 'cdlatex)
  16227. (run-hooks 'cdlatex-mode-hook)
  16228. (cdlatex-compute-tables))
  16229. (unless org-cdlatex-texmathp-advice-is-done
  16230. (setq org-cdlatex-texmathp-advice-is-done t)
  16231. (defadvice texmathp (around org-math-always-on activate)
  16232. "Always return t in org-mode buffers.
  16233. This is because we want to insert math symbols without dollars even outside
  16234. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16235. an embedded LaTeX fragment, let texmathp do its job.
  16236. \\[org-cdlatex-mode-map]"
  16237. (interactive)
  16238. (let (p)
  16239. (cond
  16240. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16241. ((eq this-command 'cdlatex-math-symbol)
  16242. (setq ad-return-value t
  16243. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16244. (t
  16245. (let ((p (org-inside-LaTeX-fragment-p)))
  16246. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16247. (setq ad-return-value t
  16248. texmathp-why '("Org-mode embedded math" . 0))
  16249. (if p ad-do-it)))))))))
  16250. (defun turn-on-org-cdlatex ()
  16251. "Unconditionally turn on `org-cdlatex-mode'."
  16252. (org-cdlatex-mode 1))
  16253. (defun org-try-cdlatex-tab ()
  16254. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16255. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16256. - inside a LaTeX fragment, or
  16257. - after the first word in a line, where an abbreviation expansion could
  16258. insert a LaTeX environment."
  16259. (when org-cdlatex-mode
  16260. (cond
  16261. ;; Before any word on the line: No expansion possible.
  16262. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16263. ;; Just after first word on the line: Expand it. Make sure it
  16264. ;; cannot happen on headlines, though.
  16265. ((save-excursion
  16266. (skip-chars-backward "a-zA-Z0-9*")
  16267. (skip-chars-backward " \t")
  16268. (and (bolp) (not (org-at-heading-p))))
  16269. (cdlatex-tab) t)
  16270. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16271. (defun org-cdlatex-underscore-caret (&optional arg)
  16272. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16273. Revert to the normal definition outside of these fragments."
  16274. (interactive "P")
  16275. (if (org-inside-LaTeX-fragment-p)
  16276. (call-interactively 'cdlatex-sub-superscript)
  16277. (let (org-cdlatex-mode)
  16278. (call-interactively (key-binding (vector last-input-event))))))
  16279. (defun org-cdlatex-math-modify (&optional arg)
  16280. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16281. Revert to the normal definition outside of these fragments."
  16282. (interactive "P")
  16283. (if (org-inside-LaTeX-fragment-p)
  16284. (call-interactively 'cdlatex-math-modify)
  16285. (let (org-cdlatex-mode)
  16286. (call-interactively (key-binding (vector last-input-event))))))
  16287. ;;;; LaTeX fragments
  16288. (defvar org-latex-regexps
  16289. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16290. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16291. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16292. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16293. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16294. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16295. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16296. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16297. "Regular expressions for matching embedded LaTeX.")
  16298. (defun org-inside-LaTeX-fragment-p ()
  16299. "Test if point is inside a LaTeX fragment.
  16300. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16301. sequence appearing also before point.
  16302. Even though the matchers for math are configurable, this function assumes
  16303. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16304. delimiters are skipped when they have been removed by customization.
  16305. The return value is nil, or a cons cell with the delimiter and the
  16306. position of this delimiter.
  16307. This function does a reasonably good job, but can locally be fooled by
  16308. for example currency specifications. For example it will assume being in
  16309. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16310. fragments that are properly closed, but during editing, we have to live
  16311. with the uncertainty caused by missing closing delimiters. This function
  16312. looks only before point, not after."
  16313. (catch 'exit
  16314. (let ((pos (point))
  16315. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16316. (lim (progn
  16317. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16318. (point)))
  16319. dd-on str (start 0) m re)
  16320. (goto-char pos)
  16321. (when dodollar
  16322. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16323. re (nth 1 (assoc "$" org-latex-regexps)))
  16324. (while (string-match re str start)
  16325. (cond
  16326. ((= (match-end 0) (length str))
  16327. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16328. ((= (match-end 0) (- (length str) 5))
  16329. (throw 'exit nil))
  16330. (t (setq start (match-end 0))))))
  16331. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16332. (goto-char pos)
  16333. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16334. (and (match-beginning 2) (throw 'exit nil))
  16335. ;; count $$
  16336. (while (re-search-backward "\\$\\$" lim t)
  16337. (setq dd-on (not dd-on)))
  16338. (goto-char pos)
  16339. (if dd-on (cons "$$" m))))))
  16340. (defun org-inside-latex-macro-p ()
  16341. "Is point inside a LaTeX macro or its arguments?"
  16342. (save-match-data
  16343. (org-in-regexp
  16344. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16345. (defvar org-latex-fragment-image-overlays nil
  16346. "List of overlays carrying the images of latex fragments.")
  16347. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16348. (defun org-remove-latex-fragment-image-overlays ()
  16349. "Remove all overlays with LaTeX fragment images in current buffer."
  16350. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16351. (setq org-latex-fragment-image-overlays nil))
  16352. (defun org-preview-latex-fragment (&optional subtree)
  16353. "Preview the LaTeX fragment at point, or all locally or globally.
  16354. If the cursor is in a LaTeX fragment, create the image and overlay
  16355. it over the source code. If there is no fragment at point, display
  16356. all fragments in the current text, from one headline to the next. With
  16357. prefix SUBTREE, display all fragments in the current subtree. With a
  16358. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16359. the cursor is before the first headline,
  16360. display all fragments in the buffer.
  16361. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16362. (interactive "P")
  16363. (unless buffer-file-name
  16364. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16365. (org-remove-latex-fragment-image-overlays)
  16366. (save-excursion
  16367. (save-restriction
  16368. (let (beg end at msg)
  16369. (cond
  16370. ((or (equal subtree '(16))
  16371. (not (save-excursion
  16372. (re-search-backward org-outline-regexp-bol nil t))))
  16373. (setq beg (point-min) end (point-max)
  16374. msg "Creating images for buffer...%s"))
  16375. ((equal subtree '(4))
  16376. (org-back-to-heading)
  16377. (setq beg (point) end (org-end-of-subtree t)
  16378. msg "Creating images for subtree...%s"))
  16379. (t
  16380. (if (setq at (org-inside-LaTeX-fragment-p))
  16381. (goto-char (max (point-min) (- (cdr at) 2)))
  16382. (org-back-to-heading))
  16383. (setq beg (point) end (progn (outline-next-heading) (point))
  16384. msg (if at "Creating image...%s"
  16385. "Creating images for entry...%s"))))
  16386. (message msg "")
  16387. (narrow-to-region beg end)
  16388. (goto-char beg)
  16389. (org-format-latex
  16390. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16391. (file-name-nondirectory
  16392. buffer-file-name)))
  16393. default-directory 'overlays msg at 'forbuffer
  16394. org-latex-create-formula-image-program)
  16395. (message msg "done. Use `C-c C-c' to remove images.")))))
  16396. (defun org-format-latex (prefix &optional dir overlays msg at
  16397. forbuffer processing-type)
  16398. "Replace LaTeX fragments with links to an image, and produce images.
  16399. Some of the options can be changed using the variable
  16400. `org-format-latex-options'."
  16401. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16402. (let* ((prefixnodir (file-name-nondirectory prefix))
  16403. (absprefix (expand-file-name prefix dir))
  16404. (todir (file-name-directory absprefix))
  16405. (opt org-format-latex-options)
  16406. (optnew org-format-latex-options)
  16407. (matchers (plist-get opt :matchers))
  16408. (re-list org-latex-regexps)
  16409. (cnt 0) txt hash link beg end re e checkdir
  16410. string
  16411. m n block-type block linkfile movefile ov)
  16412. ;; Check the different regular expressions
  16413. (while (setq e (pop re-list))
  16414. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16415. block (if block-type "\n\n" ""))
  16416. (when (member m matchers)
  16417. (goto-char (point-min))
  16418. (while (re-search-forward re nil t)
  16419. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16420. (or (not overlays)
  16421. (not (eq (get-char-property (match-beginning n)
  16422. 'org-overlay-type)
  16423. 'org-latex-overlay))))
  16424. (cond
  16425. ((eq processing-type 'verbatim))
  16426. ((eq processing-type 'mathjax)
  16427. ;; Prepare for MathJax processing.
  16428. (setq string (match-string n))
  16429. (when (member m '("$" "$1"))
  16430. (save-excursion
  16431. (delete-region (match-beginning n) (match-end n))
  16432. (goto-char (match-beginning n))
  16433. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16434. ((or (eq processing-type 'dvipng)
  16435. (eq processing-type 'imagemagick))
  16436. ;; Process to an image.
  16437. (setq txt (match-string n)
  16438. beg (match-beginning n) end (match-end n)
  16439. cnt (1+ cnt))
  16440. (let ((face (face-at-point))
  16441. (fg (plist-get opt :foreground))
  16442. (bg (plist-get opt :background))
  16443. ;; Ensure full list is printed.
  16444. print-length print-level)
  16445. (when forbuffer
  16446. ;; Get the colors from the face at point.
  16447. (goto-char beg)
  16448. (when (eq fg 'auto)
  16449. (setq fg (face-attribute face :foreground nil 'default)))
  16450. (when (eq bg 'auto)
  16451. (setq bg (face-attribute face :background nil 'default)))
  16452. (setq optnew (copy-sequence opt))
  16453. (plist-put optnew :foreground fg)
  16454. (plist-put optnew :background bg))
  16455. (setq hash (sha1 (prin1-to-string
  16456. (list org-format-latex-header
  16457. org-latex-default-packages-alist
  16458. org-latex-packages-alist
  16459. org-format-latex-options
  16460. forbuffer txt fg bg)))
  16461. linkfile (format "%s_%s.png" prefix hash)
  16462. movefile (format "%s_%s.png" absprefix hash)))
  16463. (setq link (concat block "[[file:" linkfile "]]" block))
  16464. (if msg (message msg cnt))
  16465. (goto-char beg)
  16466. (unless checkdir ; Ensure the directory exists.
  16467. (setq checkdir t)
  16468. (or (file-directory-p todir) (make-directory todir t)))
  16469. (unless (file-exists-p movefile)
  16470. (org-create-formula-image
  16471. txt movefile optnew forbuffer processing-type))
  16472. (if overlays
  16473. (progn
  16474. (mapc (lambda (o)
  16475. (if (eq (overlay-get o 'org-overlay-type)
  16476. 'org-latex-overlay)
  16477. (delete-overlay o)))
  16478. (overlays-in beg end))
  16479. (setq ov (make-overlay beg end))
  16480. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16481. (if (featurep 'xemacs)
  16482. (progn
  16483. (overlay-put ov 'invisible t)
  16484. (overlay-put
  16485. ov 'end-glyph
  16486. (make-glyph (vector 'png :file movefile))))
  16487. (overlay-put
  16488. ov 'display
  16489. (list 'image :type 'png :file movefile :ascent 'center)))
  16490. (push ov org-latex-fragment-image-overlays)
  16491. (goto-char end))
  16492. (delete-region beg end)
  16493. (insert (org-add-props link
  16494. (list 'org-latex-src
  16495. (replace-regexp-in-string
  16496. "\"" "" txt)
  16497. 'org-latex-src-embed-type
  16498. (if block-type 'paragraph 'character))))))
  16499. ((eq processing-type 'mathml)
  16500. ;; Process to MathML
  16501. (unless (save-match-data (org-format-latex-mathml-available-p))
  16502. (error "LaTeX to MathML converter not configured"))
  16503. (setq txt (match-string n)
  16504. beg (match-beginning n) end (match-end n)
  16505. cnt (1+ cnt))
  16506. (if msg (message msg cnt))
  16507. (goto-char beg)
  16508. (delete-region beg end)
  16509. (insert (org-format-latex-as-mathml
  16510. txt block-type prefix dir)))
  16511. (t
  16512. (error "Unknown conversion type %s for latex fragments"
  16513. processing-type)))))))))
  16514. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16515. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16516. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16517. sucessful, return the portion between \"<math...> </math>\"
  16518. elements otherwise return nil. When MATHML-FILE is specified,
  16519. write the results in to that file. When invoked as an
  16520. interactive command, prompt for LATEX-FRAG, with initial value
  16521. set to the current active region and echo the results for user
  16522. inspection."
  16523. (interactive (list (let ((frag (when (org-region-active-p)
  16524. (buffer-substring-no-properties
  16525. (region-beginning) (region-end)))))
  16526. (read-string "LaTeX Fragment: " frag nil frag))))
  16527. (unless latex-frag (error "Invalid latex-frag"))
  16528. (let* ((tmp-in-file (file-relative-name
  16529. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16530. (ignore (write-region latex-frag nil tmp-in-file))
  16531. (tmp-out-file (file-relative-name
  16532. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16533. (cmd (format-spec
  16534. org-latex-to-mathml-convert-command
  16535. `((?j . ,(shell-quote-argument
  16536. (expand-file-name org-latex-to-mathml-jar-file)))
  16537. (?I . ,(shell-quote-argument tmp-in-file))
  16538. (?o . ,(shell-quote-argument tmp-out-file)))))
  16539. mathml shell-command-output)
  16540. (when (org-called-interactively-p 'any)
  16541. (unless (org-format-latex-mathml-available-p)
  16542. (error "LaTeX to MathML converter not configured")))
  16543. (message "Running %s" cmd)
  16544. (setq shell-command-output (shell-command-to-string cmd))
  16545. (setq mathml
  16546. (when (file-readable-p tmp-out-file)
  16547. (with-current-buffer (find-file-noselect tmp-out-file t)
  16548. (goto-char (point-min))
  16549. (when (re-search-forward
  16550. (concat
  16551. (regexp-quote
  16552. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16553. "\\(.\\|\n\\)*"
  16554. (regexp-quote "</math>")) nil t)
  16555. (prog1 (match-string 0) (kill-buffer))))))
  16556. (cond
  16557. (mathml
  16558. (setq mathml
  16559. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16560. (when mathml-file
  16561. (write-region mathml nil mathml-file))
  16562. (when (org-called-interactively-p 'any)
  16563. (message mathml)))
  16564. ((message "LaTeX to MathML conversion failed")
  16565. (message shell-command-output)))
  16566. (delete-file tmp-in-file)
  16567. (when (file-exists-p tmp-out-file)
  16568. (delete-file tmp-out-file))
  16569. mathml))
  16570. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16571. prefix &optional dir)
  16572. "Use `org-create-math-formula' but check local cache first."
  16573. (let* ((absprefix (expand-file-name prefix dir))
  16574. (print-length nil) (print-level nil)
  16575. (formula-id (concat
  16576. "formula-"
  16577. (sha1
  16578. (prin1-to-string
  16579. (list latex-frag
  16580. org-latex-to-mathml-convert-command)))))
  16581. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16582. (formula-cache-dir (file-name-directory formula-cache)))
  16583. (unless (file-directory-p formula-cache-dir)
  16584. (make-directory formula-cache-dir t))
  16585. (unless (file-exists-p formula-cache)
  16586. (org-create-math-formula latex-frag formula-cache))
  16587. (if (file-exists-p formula-cache)
  16588. ;; Successful conversion. Return the link to MathML file.
  16589. (org-add-props
  16590. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16591. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16592. 'org-latex-src-embed-type (if latex-frag-type
  16593. 'paragraph 'character)))
  16594. ;; Failed conversion. Return the LaTeX fragment verbatim
  16595. latex-frag)))
  16596. (defun org-create-formula-image (string tofile options buffer &optional type)
  16597. "Create an image from LaTeX source using dvipng or convert.
  16598. This function calls either `org-create-formula-image-with-dvipng'
  16599. or `org-create-formula-image-with-imagemagick' depending on the
  16600. value of `org-latex-create-formula-image-program' or on the value
  16601. of the optional TYPE variable.
  16602. Note: ultimately these two function should be combined as they
  16603. share a good deal of logic."
  16604. (org-check-external-command
  16605. "latex" "needed to convert LaTeX fragments to images")
  16606. (funcall
  16607. (case (or type org-latex-create-formula-image-program)
  16608. ('dvipng
  16609. (org-check-external-command
  16610. "dvipng" "needed to convert LaTeX fragments to images")
  16611. #'org-create-formula-image-with-dvipng)
  16612. ('imagemagick
  16613. (org-check-external-command
  16614. "convert" "you need to install imagemagick")
  16615. #'org-create-formula-image-with-imagemagick)
  16616. (t (error
  16617. "invalid value of `org-latex-create-formula-image-program'")))
  16618. string tofile options buffer))
  16619. (declare-function org-export--get-global-options "ox" (&optional backend))
  16620. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16621. (defun org-create-formula--latex-header ()
  16622. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16623. (org-latex-guess-inputenc
  16624. (org-splice-latex-header
  16625. org-format-latex-header
  16626. org-latex-default-packages-alist
  16627. org-latex-packages-alist t
  16628. (plist-get
  16629. (org-combine-plists
  16630. (org-export--get-global-options 'latex)
  16631. (org-export--get-inbuffer-options 'latex))
  16632. :latex-header-extra))))
  16633. ;; This function borrows from Ganesh Swami's latex2png.el
  16634. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16635. "This calls dvipng."
  16636. (require 'ox-latex)
  16637. (let* ((tmpdir (if (featurep 'xemacs)
  16638. (temp-directory)
  16639. temporary-file-directory))
  16640. (texfilebase (make-temp-name
  16641. (expand-file-name "orgtex" tmpdir)))
  16642. (texfile (concat texfilebase ".tex"))
  16643. (dvifile (concat texfilebase ".dvi"))
  16644. (pngfile (concat texfilebase ".png"))
  16645. (fnh (if (featurep 'xemacs)
  16646. (font-height (face-font 'default))
  16647. (face-attribute 'default :height nil)))
  16648. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16649. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16650. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16651. "Black"))
  16652. (bg (or (plist-get options (if buffer :background :html-background))
  16653. "Transparent")))
  16654. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16655. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16656. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16657. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16658. (let ((latex-header (org-create-formula--latex-header)))
  16659. (with-temp-file texfile
  16660. (insert latex-header)
  16661. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16662. (let ((dir default-directory))
  16663. (condition-case nil
  16664. (progn
  16665. (cd tmpdir)
  16666. (call-process "latex" nil nil nil texfile))
  16667. (error nil))
  16668. (cd dir))
  16669. (if (not (file-exists-p dvifile))
  16670. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16671. (condition-case nil
  16672. (if (featurep 'xemacs)
  16673. (call-process "dvipng" nil nil nil
  16674. "-fg" fg "-bg" bg
  16675. "-T" "tight"
  16676. "-o" pngfile
  16677. dvifile)
  16678. (call-process "dvipng" nil nil nil
  16679. "-fg" fg "-bg" bg
  16680. "-D" dpi
  16681. ;;"-x" scale "-y" scale
  16682. "-T" "tight"
  16683. "-o" pngfile
  16684. dvifile))
  16685. (error nil))
  16686. (if (not (file-exists-p pngfile))
  16687. (if org-format-latex-signal-error
  16688. (error "Failed to create png file from %s" texfile)
  16689. (message "Failed to create png file from %s" texfile)
  16690. nil)
  16691. ;; Use the requested file name and clean up
  16692. (copy-file pngfile tofile 'replace)
  16693. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16694. (if (file-exists-p (concat texfilebase e))
  16695. (delete-file (concat texfilebase e))))
  16696. pngfile))))
  16697. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16698. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16699. "This calls convert, which is included into imagemagick."
  16700. (require 'ox-latex)
  16701. (let* ((tmpdir (if (featurep 'xemacs)
  16702. (temp-directory)
  16703. temporary-file-directory))
  16704. (texfilebase (make-temp-name
  16705. (expand-file-name "orgtex" tmpdir)))
  16706. (texfile (concat texfilebase ".tex"))
  16707. (pdffile (concat texfilebase ".pdf"))
  16708. (pngfile (concat texfilebase ".png"))
  16709. (fnh (if (featurep 'xemacs)
  16710. (font-height (face-font 'default))
  16711. (face-attribute 'default :height nil)))
  16712. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16713. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16714. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16715. "black"))
  16716. (bg (or (plist-get options (if buffer :background :html-background))
  16717. "white")))
  16718. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16719. (setq fg (org-latex-color-format fg)))
  16720. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16721. (setq bg (org-latex-color-format
  16722. (if (string= bg "Transparent") "white" bg))))
  16723. (let ((latex-header (org-create-formula--latex-header)))
  16724. (with-temp-file texfile
  16725. (insert latex-header)
  16726. (insert "\n\\begin{document}\n"
  16727. "\\definecolor{fg}{rgb}{" fg "}\n"
  16728. "\\definecolor{bg}{rgb}{" bg "}\n"
  16729. "\n\\pagecolor{bg}\n"
  16730. "\n{\\color{fg}\n"
  16731. string
  16732. "\n}\n"
  16733. "\n\\end{document}\n")))
  16734. (org-latex-compile texfile t)
  16735. (if (not (file-exists-p pdffile))
  16736. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16737. (condition-case nil
  16738. (if (featurep 'xemacs)
  16739. (call-process "convert" nil nil nil
  16740. "-density" "96"
  16741. "-trim"
  16742. "-antialias"
  16743. pdffile
  16744. "-quality" "100"
  16745. ;; "-sharpen" "0x1.0"
  16746. pngfile)
  16747. (call-process "convert" nil nil nil
  16748. "-density" dpi
  16749. "-trim"
  16750. "-antialias"
  16751. pdffile
  16752. "-quality" "100"
  16753. ;; "-sharpen" "0x1.0"
  16754. pngfile))
  16755. (error nil))
  16756. (if (not (file-exists-p pngfile))
  16757. (if org-format-latex-signal-error
  16758. (error "Failed to create png file from %s" texfile)
  16759. (message "Failed to create png file from %s" texfile)
  16760. nil)
  16761. ;; Use the requested file name and clean up
  16762. (copy-file pngfile tofile 'replace)
  16763. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16764. (if (file-exists-p (concat texfilebase e))
  16765. (delete-file (concat texfilebase e))))
  16766. pngfile))))
  16767. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16768. "Fill a LaTeX header template TPL.
  16769. In the template, the following place holders will be recognized:
  16770. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16771. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16772. [PACKAGES] \\usepackage statements for PKG
  16773. [NO-PACKAGES] do not include PKG
  16774. [EXTRA] the string EXTRA
  16775. [NO-EXTRA] do not include EXTRA
  16776. For backward compatibility, if both the positive and the negative place
  16777. holder is missing, the positive one (without the \"NO-\") will be
  16778. assumed to be present at the end of the template.
  16779. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16780. EXTRA is a string.
  16781. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16782. (let (rpl (end ""))
  16783. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16784. (setq rpl (if (or (match-end 1) (not def-pkg))
  16785. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16786. tpl (replace-match rpl t t tpl))
  16787. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16788. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16789. (setq rpl (if (or (match-end 1) (not pkg))
  16790. "" (org-latex-packages-to-string pkg snippets-p t))
  16791. tpl (replace-match rpl t t tpl))
  16792. (if pkg (setq end
  16793. (concat end "\n"
  16794. (org-latex-packages-to-string pkg snippets-p)))))
  16795. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16796. (setq rpl (if (or (match-end 1) (not extra))
  16797. "" (concat extra "\n"))
  16798. tpl (replace-match rpl t t tpl))
  16799. (if (and extra (string-match "\\S-" extra))
  16800. (setq end (concat end "\n" extra))))
  16801. (if (string-match "\\S-" end)
  16802. (concat tpl "\n" end)
  16803. tpl)))
  16804. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16805. "Turn an alist of packages into a string with the \\usepackage macros."
  16806. (setq pkg (mapconcat (lambda(p)
  16807. (cond
  16808. ((stringp p) p)
  16809. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16810. (format "%% Package %s omitted" (cadr p)))
  16811. ((equal "" (car p))
  16812. (format "\\usepackage{%s}" (cadr p)))
  16813. (t
  16814. (format "\\usepackage[%s]{%s}"
  16815. (car p) (cadr p)))))
  16816. pkg
  16817. "\n"))
  16818. (if newline (concat pkg "\n") pkg))
  16819. (defun org-dvipng-color (attr)
  16820. "Return a RGB color specification for dvipng."
  16821. (apply 'format "rgb %s %s %s"
  16822. (mapcar 'org-normalize-color
  16823. (if (featurep 'xemacs)
  16824. (color-rgb-components
  16825. (face-property 'default
  16826. (cond ((eq attr :foreground) 'foreground)
  16827. ((eq attr :background) 'background))))
  16828. (color-values (face-attribute 'default attr nil))))))
  16829. (defun org-dvipng-color-format (color-name)
  16830. "Convert COLOR-NAME to a RGB color value for dvipng."
  16831. (apply 'format "rgb %s %s %s"
  16832. (mapcar 'org-normalize-color
  16833. (color-values color-name))))
  16834. (defun org-latex-color (attr)
  16835. "Return a RGB color for the LaTeX color package."
  16836. (apply 'format "%s,%s,%s"
  16837. (mapcar 'org-normalize-color
  16838. (if (featurep 'xemacs)
  16839. (color-rgb-components
  16840. (face-property 'default
  16841. (cond ((eq attr :foreground) 'foreground)
  16842. ((eq attr :background) 'background))))
  16843. (color-values (face-attribute 'default attr nil))))))
  16844. (defun org-latex-color-format (color-name)
  16845. "Convert COLOR-NAME to a RGB color value."
  16846. (apply 'format "%s,%s,%s"
  16847. (mapcar 'org-normalize-color
  16848. (color-values color-name))))
  16849. (defun org-normalize-color (value)
  16850. "Return string to be used as color value for an RGB component."
  16851. (format "%g" (/ value 65535.0)))
  16852. ;; Image display
  16853. (defvar org-inline-image-overlays nil)
  16854. (make-variable-buffer-local 'org-inline-image-overlays)
  16855. (defun org-toggle-inline-images (&optional include-linked)
  16856. "Toggle the display of inline images.
  16857. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16858. (interactive "P")
  16859. (if org-inline-image-overlays
  16860. (progn
  16861. (org-remove-inline-images)
  16862. (message "Inline image display turned off"))
  16863. (org-display-inline-images include-linked)
  16864. (if (and (org-called-interactively-p)
  16865. org-inline-image-overlays)
  16866. (message "%d images displayed inline"
  16867. (length org-inline-image-overlays))
  16868. (message "No images to display inline"))))
  16869. (defun org-redisplay-inline-images ()
  16870. "Refresh the display of inline images."
  16871. (interactive)
  16872. (if (not org-inline-image-overlays)
  16873. (org-toggle-inline-images)
  16874. (org-toggle-inline-images)
  16875. (org-toggle-inline-images)))
  16876. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16877. "Display inline images.
  16878. Normally only links without a description part are inlined, because this
  16879. is how it will work for export. When INCLUDE-LINKED is set, also links
  16880. with a description part will be inlined. This can be nice for a quick
  16881. look at those images, but it does not reflect what exported files will look
  16882. like.
  16883. When REFRESH is set, refresh existing images between BEG and END.
  16884. This will create new image displays only if necessary.
  16885. BEG and END default to the buffer boundaries."
  16886. (interactive "P")
  16887. (unless refresh
  16888. (org-remove-inline-images)
  16889. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16890. (save-excursion
  16891. (save-restriction
  16892. (widen)
  16893. (setq beg (or beg (point-min)) end (or end (point-max)))
  16894. (goto-char beg)
  16895. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16896. (substring (org-image-file-name-regexp) 0 -2)
  16897. "\\)\\]" (if include-linked "" "\\]")))
  16898. old file ov img type attrwidth width)
  16899. (while (re-search-forward re end t)
  16900. (setq old (get-char-property-and-overlay (match-beginning 1)
  16901. 'org-image-overlay)
  16902. file (expand-file-name
  16903. (concat (or (match-string 3) "") (match-string 4))))
  16904. (when (image-type-available-p 'imagemagick)
  16905. (setq attrwidth (if (or (listp org-image-actual-width)
  16906. (null org-image-actual-width))
  16907. (save-excursion
  16908. (save-match-data
  16909. (when (re-search-backward
  16910. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16911. (save-excursion
  16912. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16913. (string-to-number (match-string 1))))))
  16914. width (cond ((eq org-image-actual-width t) nil)
  16915. ((null org-image-actual-width) attrwidth)
  16916. ((numberp org-image-actual-width)
  16917. org-image-actual-width)
  16918. ((listp org-image-actual-width)
  16919. (or attrwidth (car org-image-actual-width))))
  16920. type (if width 'imagemagick)))
  16921. (when (file-exists-p file)
  16922. (if (and (car-safe old) refresh)
  16923. (image-refresh (overlay-get (cdr old) 'display))
  16924. (setq img (save-match-data (create-image file type nil :width width)))
  16925. (when img
  16926. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16927. (overlay-put ov 'display img)
  16928. (overlay-put ov 'face 'default)
  16929. (overlay-put ov 'org-image-overlay t)
  16930. (overlay-put ov 'modification-hooks
  16931. (list 'org-display-inline-remove-overlay))
  16932. (push ov org-inline-image-overlays)))))))))
  16933. (define-obsolete-function-alias
  16934. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16935. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16936. "Remove inline-display overlay if a corresponding region is modified."
  16937. (let ((inhibit-modification-hooks t))
  16938. (when (and ov after)
  16939. (delete ov org-inline-image-overlays)
  16940. (delete-overlay ov))))
  16941. (defun org-remove-inline-images ()
  16942. "Remove inline display of images."
  16943. (interactive)
  16944. (mapc 'delete-overlay org-inline-image-overlays)
  16945. (setq org-inline-image-overlays nil))
  16946. ;;;; Key bindings
  16947. ;; Outline functions from `outline-mode-prefix-map'
  16948. ;; that can be remapped in Org:
  16949. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16950. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16951. (define-key org-mode-map [remap outline-forward-same-level]
  16952. 'org-forward-heading-same-level)
  16953. (define-key org-mode-map [remap outline-backward-same-level]
  16954. 'org-backward-heading-same-level)
  16955. (define-key org-mode-map [remap show-branches]
  16956. 'org-kill-note-or-show-branches)
  16957. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16958. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16959. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16960. ;; Outline functions from `outline-mode-prefix-map' that can not
  16961. ;; be remapped in Org:
  16962. ;;
  16963. ;; - the column "key binding" shows whether the Outline function is still
  16964. ;; available in Org mode on the same key that it has been bound to in
  16965. ;; Outline mode:
  16966. ;; - "overridden": key used for a different functionality in Org mode
  16967. ;; - else: key still bound to the same Outline function in Org mode
  16968. ;;
  16969. ;; | Outline function | key binding | Org replacement |
  16970. ;; |------------------------------------+-------------+-----------------------|
  16971. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16972. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16973. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16974. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16975. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16976. ;; | `show-entry' | overridden | no replacement |
  16977. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16978. ;; | `show-branches' | `C-c C-k' | still same function |
  16979. ;; | `show-subtree' | overridden | visibility cycling |
  16980. ;; | `show-all' | overridden | no replacement |
  16981. ;; | `hide-subtree' | overridden | visibility cycling |
  16982. ;; | `hide-body' | overridden | no replacement |
  16983. ;; | `hide-entry' | overridden | visibility cycling |
  16984. ;; | `hide-leaves' | overridden | no replacement |
  16985. ;; | `hide-sublevels' | overridden | no replacement |
  16986. ;; | `hide-other' | overridden | no replacement |
  16987. ;; Make `C-c C-x' a prefix key
  16988. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16989. ;; TAB key with modifiers
  16990. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16991. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16992. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16993. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16994. ;; The following line is necessary under Suse GNU/Linux
  16995. (unless (featurep 'xemacs)
  16996. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16997. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16998. (define-key org-mode-map [backtab] 'org-shifttab)
  16999. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17000. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17001. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17002. ;; Cursor keys with modifiers
  17003. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17004. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17005. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17006. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17007. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17008. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17009. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17010. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17011. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17012. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17013. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17014. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17015. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17016. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17017. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17018. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17019. ;; Babel keys
  17020. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17021. (mapc (lambda (pair)
  17022. (define-key org-babel-map (car pair) (cdr pair)))
  17023. org-babel-key-bindings)
  17024. ;;; Extra keys for tty access.
  17025. ;; We only set them when really needed because otherwise the
  17026. ;; menus don't show the simple keys
  17027. (when (or org-use-extra-keys
  17028. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17029. (not window-system))
  17030. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17031. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17032. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17033. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17034. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17035. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17036. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17037. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17038. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17039. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17040. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17041. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17042. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17043. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17044. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17045. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17046. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17047. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17048. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17049. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17050. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17051. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17052. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17053. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17054. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17055. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17056. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17057. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17058. ;; All the other keys
  17059. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17060. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17061. (if (boundp 'narrow-map)
  17062. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17063. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17064. (if (boundp 'narrow-map)
  17065. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17066. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17067. (if (boundp 'narrow-map)
  17068. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17069. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17070. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17071. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17072. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17073. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17074. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17075. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17076. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17077. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17078. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17079. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17080. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17081. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17082. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17083. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17084. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17085. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17086. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17087. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17088. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17089. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17090. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17091. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17092. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17093. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17094. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17095. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17096. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17097. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17098. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17099. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17100. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17101. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17102. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17103. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17104. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17105. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17106. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17107. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17108. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17109. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17110. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17111. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17112. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17113. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17114. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17115. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17116. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17117. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17118. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17119. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17120. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17121. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17122. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17123. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17124. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17125. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17126. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17127. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17128. (org-defkey org-mode-map "\C-m" 'org-return)
  17129. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17130. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17131. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17132. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17133. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17134. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17135. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17136. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17137. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17138. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17139. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17140. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17141. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17142. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17143. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17144. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17145. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17146. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17147. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17148. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17149. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17150. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17151. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17152. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17153. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17154. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17155. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17156. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17157. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17158. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17159. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17160. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17161. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17162. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17163. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17164. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17165. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17166. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17167. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17168. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17169. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17170. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17171. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17172. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17173. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17174. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17175. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17176. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17177. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17178. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17179. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17180. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17181. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17182. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17183. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17184. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17185. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17186. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17187. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17188. (when (featurep 'xemacs)
  17189. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17190. (defconst org-speed-commands-default
  17191. '(
  17192. ("Outline Navigation")
  17193. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17194. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17195. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17196. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17197. ("F" . org-next-block)
  17198. ("B" . org-previous-block)
  17199. ("u" . (org-speed-move-safe 'outline-up-heading))
  17200. ("j" . org-goto)
  17201. ("g" . (org-refile t))
  17202. ("Outline Visibility")
  17203. ("c" . org-cycle)
  17204. ("C" . org-shifttab)
  17205. (" " . org-display-outline-path)
  17206. ("=" . org-columns)
  17207. ("Outline Structure Editing")
  17208. ("U" . org-shiftmetaup)
  17209. ("D" . org-shiftmetadown)
  17210. ("r" . org-metaright)
  17211. ("l" . org-metaleft)
  17212. ("R" . org-shiftmetaright)
  17213. ("L" . org-shiftmetaleft)
  17214. ("i" . (progn (forward-char 1) (call-interactively
  17215. 'org-insert-heading-respect-content)))
  17216. ("^" . org-sort)
  17217. ("w" . org-refile)
  17218. ("a" . org-archive-subtree-default-with-confirmation)
  17219. ("@" . org-mark-subtree)
  17220. ("#" . org-toggle-comment)
  17221. ("Clock Commands")
  17222. ("I" . org-clock-in)
  17223. ("O" . org-clock-out)
  17224. ("Meta Data Editing")
  17225. ("t" . org-todo)
  17226. ("," . (org-priority))
  17227. ("0" . (org-priority ?\ ))
  17228. ("1" . (org-priority ?A))
  17229. ("2" . (org-priority ?B))
  17230. ("3" . (org-priority ?C))
  17231. (":" . org-set-tags-command)
  17232. ("e" . org-set-effort)
  17233. ("E" . org-inc-effort)
  17234. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17235. (org-entry-put (point) "APPT_WARNTIME" m)))
  17236. ("Agenda Views etc")
  17237. ("v" . org-agenda)
  17238. ("/" . org-sparse-tree)
  17239. ("Misc")
  17240. ("o" . org-open-at-point)
  17241. ("?" . org-speed-command-help)
  17242. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17243. (">" . (org-agenda-remove-restriction-lock))
  17244. )
  17245. "The default speed commands.")
  17246. (defun org-print-speed-command (e)
  17247. (if (> (length (car e)) 1)
  17248. (progn
  17249. (princ "\n")
  17250. (princ (car e))
  17251. (princ "\n")
  17252. (princ (make-string (length (car e)) ?-))
  17253. (princ "\n"))
  17254. (princ (car e))
  17255. (princ " ")
  17256. (if (symbolp (cdr e))
  17257. (princ (symbol-name (cdr e)))
  17258. (prin1 (cdr e)))
  17259. (princ "\n")))
  17260. (defun org-speed-command-help ()
  17261. "Show the available speed commands."
  17262. (interactive)
  17263. (if (not org-use-speed-commands)
  17264. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17265. (with-output-to-temp-buffer "*Help*"
  17266. (princ "User-defined Speed commands\n===========================\n")
  17267. (mapc 'org-print-speed-command org-speed-commands-user)
  17268. (princ "\n")
  17269. (princ "Built-in Speed commands\n=======================\n")
  17270. (mapc 'org-print-speed-command org-speed-commands-default))
  17271. (with-current-buffer "*Help*"
  17272. (setq truncate-lines t))))
  17273. (defun org-speed-move-safe (cmd)
  17274. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17275. If not, return to the original position and throw an error."
  17276. (interactive)
  17277. (let ((pos (point)))
  17278. (call-interactively cmd)
  17279. (unless (and (bolp) (org-at-heading-p))
  17280. (goto-char pos)
  17281. (error "Boundary reached while executing %s" cmd))))
  17282. (defvar org-self-insert-command-undo-counter 0)
  17283. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17284. (defvar org-speed-command nil)
  17285. (define-obsolete-function-alias
  17286. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17287. (defun org-speed-command-activate (keys)
  17288. "Hook for activating single-letter speed commands.
  17289. `org-speed-commands-default' specifies a minimal command set.
  17290. Use `org-speed-commands-user' for further customization."
  17291. (when (or (and (bolp) (looking-at org-outline-regexp))
  17292. (and (functionp org-use-speed-commands)
  17293. (funcall org-use-speed-commands)))
  17294. (cdr (assoc keys (append org-speed-commands-user
  17295. org-speed-commands-default)))))
  17296. (define-obsolete-function-alias
  17297. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17298. (defun org-babel-speed-command-activate (keys)
  17299. "Hook for activating single-letter code block commands."
  17300. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17301. (cdr (assoc keys org-babel-key-bindings))))
  17302. (defcustom org-speed-command-hook
  17303. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17304. "Hook for activating speed commands at strategic locations.
  17305. Hook functions are called in sequence until a valid handler is
  17306. found.
  17307. Each hook takes a single argument, a user-pressed command key
  17308. which is also a `self-insert-command' from the global map.
  17309. Within the hook, examine the cursor position and the command key
  17310. and return nil or a valid handler as appropriate. Handler could
  17311. be one of an interactive command, a function, or a form.
  17312. Set `org-use-speed-commands' to non-nil value to enable this
  17313. hook. The default setting is `org-speed-command-activate'."
  17314. :group 'org-structure
  17315. :version "24.1"
  17316. :type 'hook)
  17317. (defun org-self-insert-command (N)
  17318. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17319. If the cursor is in a table looking at whitespace, the whitespace is
  17320. overwritten, and the table is not marked as requiring realignment."
  17321. (interactive "p")
  17322. (org-check-before-invisible-edit 'insert)
  17323. (cond
  17324. ((and org-use-speed-commands
  17325. (setq org-speed-command
  17326. (run-hook-with-args-until-success
  17327. 'org-speed-command-hook (this-command-keys))))
  17328. (cond
  17329. ((commandp org-speed-command)
  17330. (setq this-command org-speed-command)
  17331. (call-interactively org-speed-command))
  17332. ((functionp org-speed-command)
  17333. (funcall org-speed-command))
  17334. ((and org-speed-command (listp org-speed-command))
  17335. (eval org-speed-command))
  17336. (t (let (org-use-speed-commands)
  17337. (call-interactively 'org-self-insert-command)))))
  17338. ((and
  17339. (org-table-p)
  17340. (progn
  17341. ;; check if we blank the field, and if that triggers align
  17342. (and (featurep 'org-table) org-table-auto-blank-field
  17343. (member last-command
  17344. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17345. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17346. ;; got extra space, this field does not determine column width
  17347. (let (org-table-may-need-update) (org-table-blank-field))
  17348. ;; no extra space, this field may determine column width
  17349. (org-table-blank-field)))
  17350. t)
  17351. (eq N 1)
  17352. (looking-at "[^|\n]* |"))
  17353. (let (org-table-may-need-update)
  17354. (goto-char (1- (match-end 0)))
  17355. (backward-delete-char 1)
  17356. (goto-char (match-beginning 0))
  17357. (self-insert-command N)))
  17358. (t
  17359. (setq org-table-may-need-update t)
  17360. (self-insert-command N)
  17361. (org-fix-tags-on-the-fly)
  17362. (if org-self-insert-cluster-for-undo
  17363. (if (not (eq last-command 'org-self-insert-command))
  17364. (setq org-self-insert-command-undo-counter 1)
  17365. (if (>= org-self-insert-command-undo-counter 20)
  17366. (setq org-self-insert-command-undo-counter 1)
  17367. (and (> org-self-insert-command-undo-counter 0)
  17368. buffer-undo-list (listp buffer-undo-list)
  17369. (not (cadr buffer-undo-list)) ; remove nil entry
  17370. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17371. (setq org-self-insert-command-undo-counter
  17372. (1+ org-self-insert-command-undo-counter))))))))
  17373. (defun org-check-before-invisible-edit (kind)
  17374. "Check is editing if kind KIND would be dangerous with invisible text around.
  17375. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17376. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17377. (if (and org-catch-invisible-edits
  17378. (or (not (boundp 'visible-mode)) (not visible-mode))
  17379. (or (get-char-property (point) 'invisible)
  17380. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17381. ;; OK, we need to take a closer look
  17382. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17383. (invisible-before-point (if (bobp) nil (get-char-property
  17384. (1- (point)) 'invisible)))
  17385. (border-and-ok-direction
  17386. (or
  17387. ;; Check if we are acting predictably before invisible text
  17388. (and invisible-at-point (not invisible-before-point)
  17389. (memq kind '(insert delete-backward)))
  17390. ;; Check if we are acting predictably after invisible text
  17391. ;; This works not well, and I have turned it off. It seems
  17392. ;; better to always show and stop after invisible text.
  17393. ;; (and (not invisible-at-point) invisible-before-point
  17394. ;; (memq kind '(insert delete)))
  17395. )))
  17396. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17397. (memq invisible-before-point '(outline org-hide-block t)))
  17398. (if (eq org-catch-invisible-edits 'error)
  17399. (error "Editing in invisible areas is prohibited - make visible first"))
  17400. (if (and org-custom-properties-overlays
  17401. (y-or-n-p "Display invisible properties in this buffer? "))
  17402. (org-toggle-custom-properties-visibility)
  17403. ;; Make the area visible
  17404. (save-excursion
  17405. (if invisible-before-point
  17406. (goto-char (previous-single-char-property-change
  17407. (point) 'invisible)))
  17408. (org-cycle))
  17409. (cond
  17410. ((eq org-catch-invisible-edits 'show)
  17411. ;; That's it, we do the edit after showing
  17412. (message
  17413. "Unfolding invisible region around point before editing")
  17414. (sit-for 1))
  17415. ((and (eq org-catch-invisible-edits 'smart)
  17416. border-and-ok-direction)
  17417. (message "Unfolding invisible region around point before editing"))
  17418. (t
  17419. ;; Don't do the edit, make the user repeat it in full visibility
  17420. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17421. (defun org-fix-tags-on-the-fly ()
  17422. (when (and (equal (char-after (point-at-bol)) ?*)
  17423. (org-at-heading-p))
  17424. (org-align-tags-here org-tags-column)))
  17425. (defun org-delete-backward-char (N)
  17426. "Like `delete-backward-char', insert whitespace at field end in tables.
  17427. When deleting backwards, in tables this function will insert whitespace in
  17428. front of the next \"|\" separator, to keep the table aligned. The table will
  17429. still be marked for re-alignment if the field did fill the entire column,
  17430. because, in this case the deletion might narrow the column."
  17431. (interactive "p")
  17432. (save-match-data
  17433. (org-check-before-invisible-edit 'delete-backward)
  17434. (if (and (org-table-p)
  17435. (eq N 1)
  17436. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17437. (looking-at ".*?|"))
  17438. (let ((pos (point))
  17439. (noalign (looking-at "[^|\n\r]* |"))
  17440. (c org-table-may-need-update))
  17441. (backward-delete-char N)
  17442. (if (not overwrite-mode)
  17443. (progn
  17444. (skip-chars-forward "^|")
  17445. (insert " ")
  17446. (goto-char (1- pos))))
  17447. ;; noalign: if there were two spaces at the end, this field
  17448. ;; does not determine the width of the column.
  17449. (if noalign (setq org-table-may-need-update c)))
  17450. (backward-delete-char N)
  17451. (org-fix-tags-on-the-fly))))
  17452. (defun org-delete-char (N)
  17453. "Like `delete-char', but insert whitespace at field end in tables.
  17454. When deleting characters, in tables this function will insert whitespace in
  17455. front of the next \"|\" separator, to keep the table aligned. The table will
  17456. still be marked for re-alignment if the field did fill the entire column,
  17457. because, in this case the deletion might narrow the column."
  17458. (interactive "p")
  17459. (save-match-data
  17460. (org-check-before-invisible-edit 'delete)
  17461. (if (and (org-table-p)
  17462. (not (bolp))
  17463. (not (= (char-after) ?|))
  17464. (eq N 1))
  17465. (if (looking-at ".*?|")
  17466. (let ((pos (point))
  17467. (noalign (looking-at "[^|\n\r]* |"))
  17468. (c org-table-may-need-update))
  17469. (replace-match (concat
  17470. (substring (match-string 0) 1 -1)
  17471. " |"))
  17472. (goto-char pos)
  17473. ;; noalign: if there were two spaces at the end, this field
  17474. ;; does not determine the width of the column.
  17475. (if noalign (setq org-table-may-need-update c)))
  17476. (delete-char N))
  17477. (delete-char N)
  17478. (org-fix-tags-on-the-fly))))
  17479. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17480. (put 'org-self-insert-command 'delete-selection t)
  17481. (put 'orgtbl-self-insert-command 'delete-selection t)
  17482. (put 'org-delete-char 'delete-selection 'supersede)
  17483. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17484. (put 'org-yank 'delete-selection 'yank)
  17485. ;; Make `flyspell-mode' delay after some commands
  17486. (put 'org-self-insert-command 'flyspell-delayed t)
  17487. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17488. (put 'org-delete-char 'flyspell-delayed t)
  17489. (put 'org-delete-backward-char 'flyspell-delayed t)
  17490. ;; Make pabbrev-mode expand after org-mode commands
  17491. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17492. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17493. ;; How to do this: Measure non-white length of current string
  17494. ;; If equal to column width, we should realign.
  17495. (defun org-remap (map &rest commands)
  17496. "In MAP, remap the functions given in COMMANDS.
  17497. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17498. (let (new old)
  17499. (while commands
  17500. (setq old (pop commands) new (pop commands))
  17501. (if (fboundp 'command-remapping)
  17502. (org-defkey map (vector 'remap old) new)
  17503. (substitute-key-definition old new map global-map)))))
  17504. (defun org-transpose-words ()
  17505. "Transpose words for Org.
  17506. This uses the `org-mode-transpose-word-syntax-table' syntax
  17507. table, which interprets characters in `org-emphasis-alist' as
  17508. word constituants."
  17509. (interactive)
  17510. (with-syntax-table org-mode-transpose-word-syntax-table
  17511. (call-interactively 'transpose-words)))
  17512. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17513. (when (eq org-enable-table-editor 'optimized)
  17514. ;; If the user wants maximum table support, we need to hijack
  17515. ;; some standard editing functions
  17516. (org-remap org-mode-map
  17517. 'self-insert-command 'org-self-insert-command
  17518. 'delete-char 'org-delete-char
  17519. 'delete-backward-char 'org-delete-backward-char)
  17520. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17521. (defvar org-ctrl-c-ctrl-c-hook nil
  17522. "Hook for functions attaching themselves to `C-c C-c'.
  17523. This can be used to add additional functionality to the C-c C-c
  17524. key which executes context-dependent commands. This hook is run
  17525. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17526. run after the last test.
  17527. Each function will be called with no arguments. The function
  17528. must check if the context is appropriate for it to act. If yes,
  17529. it should do its thing and then return a non-nil value. If the
  17530. context is wrong, just do nothing and return nil.")
  17531. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17532. "Hook for functions attaching themselves to `C-c C-c'.
  17533. This can be used to add additional functionality to the C-c C-c
  17534. key which executes context-dependent commands. This hook is run
  17535. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17536. before the first test.
  17537. Each function will be called with no arguments. The function
  17538. must check if the context is appropriate for it to act. If yes,
  17539. it should do its thing and then return a non-nil value. If the
  17540. context is wrong, just do nothing and return nil.")
  17541. (defvar org-tab-first-hook nil
  17542. "Hook for functions to attach themselves to TAB.
  17543. See `org-ctrl-c-ctrl-c-hook' for more information.
  17544. This hook runs as the first action when TAB is pressed, even before
  17545. `org-cycle' messes around with the `outline-regexp' to cater for
  17546. inline tasks and plain list item folding.
  17547. If any function in this hook returns t, any other actions that
  17548. would have been caused by TAB (such as table field motion or visibility
  17549. cycling) will not occur.")
  17550. (defvar org-tab-after-check-for-table-hook nil
  17551. "Hook for functions to attach themselves to TAB.
  17552. See `org-ctrl-c-ctrl-c-hook' for more information.
  17553. This hook runs after it has been established that the cursor is not in a
  17554. table, but before checking if the cursor is in a headline or if global cycling
  17555. should be done.
  17556. If any function in this hook returns t, not other actions like visibility
  17557. cycling will be done.")
  17558. (defvar org-tab-after-check-for-cycling-hook nil
  17559. "Hook for functions to attach themselves to TAB.
  17560. See `org-ctrl-c-ctrl-c-hook' for more information.
  17561. This hook runs after it has been established that not table field motion and
  17562. not visibility should be done because of current context. This is probably
  17563. the place where a package like yasnippets can hook in.")
  17564. (defvar org-tab-before-tab-emulation-hook nil
  17565. "Hook for functions to attach themselves to TAB.
  17566. See `org-ctrl-c-ctrl-c-hook' for more information.
  17567. This hook runs after every other options for TAB have been exhausted, but
  17568. before indentation and \t insertion takes place.")
  17569. (defvar org-metaleft-hook nil
  17570. "Hook for functions attaching themselves to `M-left'.
  17571. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17572. (defvar org-metaright-hook nil
  17573. "Hook for functions attaching themselves to `M-right'.
  17574. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17575. (defvar org-metaup-hook nil
  17576. "Hook for functions attaching themselves to `M-up'.
  17577. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17578. (defvar org-metadown-hook nil
  17579. "Hook for functions attaching themselves to `M-down'.
  17580. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17581. (defvar org-shiftmetaleft-hook nil
  17582. "Hook for functions attaching themselves to `M-S-left'.
  17583. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17584. (defvar org-shiftmetaright-hook nil
  17585. "Hook for functions attaching themselves to `M-S-right'.
  17586. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17587. (defvar org-shiftmetaup-hook nil
  17588. "Hook for functions attaching themselves to `M-S-up'.
  17589. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17590. (defvar org-shiftmetadown-hook nil
  17591. "Hook for functions attaching themselves to `M-S-down'.
  17592. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17593. (defvar org-metareturn-hook nil
  17594. "Hook for functions attaching themselves to `M-RET'.
  17595. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17596. (defvar org-shiftup-hook nil
  17597. "Hook for functions attaching themselves to `S-up'.
  17598. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17599. (defvar org-shiftup-final-hook nil
  17600. "Hook for functions attaching themselves to `S-up'.
  17601. This one runs after all other options except shift-select have been excluded.
  17602. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17603. (defvar org-shiftdown-hook nil
  17604. "Hook for functions attaching themselves to `S-down'.
  17605. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17606. (defvar org-shiftdown-final-hook nil
  17607. "Hook for functions attaching themselves to `S-down'.
  17608. This one runs after all other options except shift-select have been excluded.
  17609. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17610. (defvar org-shiftleft-hook nil
  17611. "Hook for functions attaching themselves to `S-left'.
  17612. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17613. (defvar org-shiftleft-final-hook nil
  17614. "Hook for functions attaching themselves to `S-left'.
  17615. This one runs after all other options except shift-select have been excluded.
  17616. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17617. (defvar org-shiftright-hook nil
  17618. "Hook for functions attaching themselves to `S-right'.
  17619. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17620. (defvar org-shiftright-final-hook nil
  17621. "Hook for functions attaching themselves to `S-right'.
  17622. This one runs after all other options except shift-select have been excluded.
  17623. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17624. (defun org-modifier-cursor-error ()
  17625. "Throw an error, a modified cursor command was applied in wrong context."
  17626. (error "This command is active in special context like tables, headlines or items"))
  17627. (defun org-shiftselect-error ()
  17628. "Throw an error because Shift-Cursor command was applied in wrong context."
  17629. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17630. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17631. (error "This command works only in special context like headlines or timestamps")))
  17632. (defun org-call-for-shift-select (cmd)
  17633. (let ((this-command-keys-shift-translated t))
  17634. (call-interactively cmd)))
  17635. (defun org-shifttab (&optional arg)
  17636. "Global visibility cycling or move to previous table field.
  17637. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17638. on context.
  17639. See the individual commands for more information."
  17640. (interactive "P")
  17641. (cond
  17642. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17643. ((integerp arg)
  17644. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17645. (message "Content view to level: %d" arg)
  17646. (org-content (prefix-numeric-value arg2))
  17647. (setq org-cycle-global-status 'overview)))
  17648. (t (call-interactively 'org-global-cycle))))
  17649. (defun org-shiftmetaleft ()
  17650. "Promote subtree or delete table column.
  17651. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17652. `org-table-delete-column', depending on context. See the
  17653. individual commands for more information."
  17654. (interactive)
  17655. (cond
  17656. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17657. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17658. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17659. ((if (not (org-region-active-p)) (org-at-item-p)
  17660. (save-excursion (goto-char (region-beginning))
  17661. (org-at-item-p)))
  17662. (call-interactively 'org-outdent-item-tree))
  17663. (t (org-modifier-cursor-error))))
  17664. (defun org-shiftmetaright ()
  17665. "Demote subtree or insert table column.
  17666. Calls `org-demote-subtree', `org-indent-item-tree', or
  17667. `org-table-insert-column', depending on context. See the
  17668. individual commands for more information."
  17669. (interactive)
  17670. (cond
  17671. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17672. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17673. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17674. ((if (not (org-region-active-p)) (org-at-item-p)
  17675. (save-excursion (goto-char (region-beginning))
  17676. (org-at-item-p)))
  17677. (call-interactively 'org-indent-item-tree))
  17678. (t (org-modifier-cursor-error))))
  17679. (defun org-shiftmetaup (&optional arg)
  17680. "Move subtree up or kill table row.
  17681. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17682. `org-move-item-up' or `org-timestamp-up', depending on context.
  17683. See the individual commands for more information."
  17684. (interactive "P")
  17685. (cond
  17686. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17687. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17688. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17689. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17690. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17691. (call-interactively 'org-timestamp-up)))
  17692. (t (call-interactively 'org-drag-line-backward))))
  17693. (defun org-shiftmetadown (&optional arg)
  17694. "Move subtree down or insert table row.
  17695. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17696. `org-move-item-down' or `org-timestamp-up', depending on context.
  17697. See the individual commands for more information."
  17698. (interactive "P")
  17699. (cond
  17700. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17701. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17702. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17703. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17704. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17705. (call-interactively 'org-timestamp-down)))
  17706. (t (call-interactively 'org-drag-line-forward))))
  17707. (defsubst org-hidden-tree-error ()
  17708. (error
  17709. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17710. (defun org-metaleft (&optional arg)
  17711. "Promote heading or move table column to left.
  17712. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17713. With no specific context, calls the Emacs default `backward-word'.
  17714. See the individual commands for more information."
  17715. (interactive "P")
  17716. (cond
  17717. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17718. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17719. ((org-with-limited-levels
  17720. (or (org-at-heading-p)
  17721. (and (org-region-active-p)
  17722. (save-excursion
  17723. (goto-char (region-beginning))
  17724. (org-at-heading-p)))))
  17725. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17726. (call-interactively 'org-do-promote))
  17727. ;; At an inline task.
  17728. ((org-at-heading-p)
  17729. (call-interactively 'org-inlinetask-promote))
  17730. ((or (org-at-item-p)
  17731. (and (org-region-active-p)
  17732. (save-excursion
  17733. (goto-char (region-beginning))
  17734. (org-at-item-p))))
  17735. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17736. (call-interactively 'org-outdent-item))
  17737. (t (call-interactively 'backward-word))))
  17738. (defun org-metaright (&optional arg)
  17739. "Demote a subtree, a list item or move table column to right.
  17740. In front of a drawer or a block keyword, indent it correctly.
  17741. With no specific context, calls the Emacs default `forward-word'.
  17742. See the individual commands for more information."
  17743. (interactive "P")
  17744. (cond
  17745. ((run-hook-with-args-until-success 'org-metaright-hook))
  17746. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17747. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17748. ((org-at-block-p) (call-interactively 'org-indent-block))
  17749. ((org-with-limited-levels
  17750. (or (org-at-heading-p)
  17751. (and (org-region-active-p)
  17752. (save-excursion
  17753. (goto-char (region-beginning))
  17754. (org-at-heading-p)))))
  17755. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17756. (call-interactively 'org-do-demote))
  17757. ;; At an inline task.
  17758. ((org-at-heading-p)
  17759. (call-interactively 'org-inlinetask-demote))
  17760. ((or (org-at-item-p)
  17761. (and (org-region-active-p)
  17762. (save-excursion
  17763. (goto-char (region-beginning))
  17764. (org-at-item-p))))
  17765. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17766. (call-interactively 'org-indent-item))
  17767. (t (call-interactively 'forward-word))))
  17768. (defun org-check-for-hidden (what)
  17769. "Check if there are hidden headlines/items in the current visual line.
  17770. WHAT can be either `headlines' or `items'. If the current line is
  17771. an outline or item heading and it has a folded subtree below it,
  17772. this function returns t, nil otherwise."
  17773. (let ((re (cond
  17774. ((eq what 'headlines) org-outline-regexp-bol)
  17775. ((eq what 'items) (org-item-beginning-re))
  17776. (t (error "This should not happen"))))
  17777. beg end)
  17778. (save-excursion
  17779. (catch 'exit
  17780. (unless (org-region-active-p)
  17781. (setq beg (point-at-bol))
  17782. (beginning-of-line 2)
  17783. (while (and (not (eobp)) ;; this is like `next-line'
  17784. (get-char-property (1- (point)) 'invisible))
  17785. (beginning-of-line 2))
  17786. (setq end (point))
  17787. (goto-char beg)
  17788. (goto-char (point-at-eol))
  17789. (setq end (max end (point)))
  17790. (while (re-search-forward re end t)
  17791. (if (get-char-property (match-beginning 0) 'invisible)
  17792. (throw 'exit t))))
  17793. nil))))
  17794. (defun org-metaup (&optional arg)
  17795. "Move subtree up or move table row up.
  17796. Calls `org-move-subtree-up' or `org-table-move-row' or
  17797. `org-move-item-up', depending on context. See the individual commands
  17798. for more information."
  17799. (interactive "P")
  17800. (cond
  17801. ((run-hook-with-args-until-success 'org-metaup-hook))
  17802. ((org-region-active-p)
  17803. (let* ((a (min (region-beginning) (region-end)))
  17804. (b (1- (max (region-beginning) (region-end))))
  17805. (c (save-excursion (goto-char a)
  17806. (move-beginning-of-line 0)))
  17807. (d (save-excursion (goto-char a)
  17808. (move-end-of-line 0) (point))))
  17809. (transpose-regions a b c d)
  17810. (goto-char c)))
  17811. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17812. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17813. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17814. (t (org-drag-element-backward))))
  17815. (defun org-metadown (&optional arg)
  17816. "Move subtree down or move table row down.
  17817. Calls `org-move-subtree-down' or `org-table-move-row' or
  17818. `org-move-item-down', depending on context. See the individual
  17819. commands for more information."
  17820. (interactive "P")
  17821. (cond
  17822. ((run-hook-with-args-until-success 'org-metadown-hook))
  17823. ((org-region-active-p)
  17824. (let* ((a (min (region-beginning) (region-end)))
  17825. (b (max (region-beginning) (region-end)))
  17826. (c (save-excursion (goto-char b)
  17827. (move-beginning-of-line 1)))
  17828. (d (save-excursion (goto-char b)
  17829. (move-end-of-line 1) (1+ (point)))))
  17830. (transpose-regions a b c d)
  17831. (goto-char d)))
  17832. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17833. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17834. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17835. (t (org-drag-element-forward))))
  17836. (defun org-shiftup (&optional arg)
  17837. "Increase item in timestamp or increase priority of current headline.
  17838. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17839. depending on context. See the individual commands for more information."
  17840. (interactive "P")
  17841. (cond
  17842. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17843. ((and org-support-shift-select (org-region-active-p))
  17844. (org-call-for-shift-select 'previous-line))
  17845. ((org-at-timestamp-p t)
  17846. (call-interactively (if org-edit-timestamp-down-means-later
  17847. 'org-timestamp-down 'org-timestamp-up)))
  17848. ((and (not (eq org-support-shift-select 'always))
  17849. org-enable-priority-commands
  17850. (org-at-heading-p))
  17851. (call-interactively 'org-priority-up))
  17852. ((and (not org-support-shift-select) (org-at-item-p))
  17853. (call-interactively 'org-previous-item))
  17854. ((org-clocktable-try-shift 'up arg))
  17855. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17856. (org-support-shift-select
  17857. (org-call-for-shift-select 'previous-line))
  17858. (t (org-shiftselect-error))))
  17859. (defun org-shiftdown (&optional arg)
  17860. "Decrease item in timestamp or decrease priority of current headline.
  17861. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17862. depending on context. See the individual commands for more information."
  17863. (interactive "P")
  17864. (cond
  17865. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17866. ((and org-support-shift-select (org-region-active-p))
  17867. (org-call-for-shift-select 'next-line))
  17868. ((org-at-timestamp-p t)
  17869. (call-interactively (if org-edit-timestamp-down-means-later
  17870. 'org-timestamp-up 'org-timestamp-down)))
  17871. ((and (not (eq org-support-shift-select 'always))
  17872. org-enable-priority-commands
  17873. (org-at-heading-p))
  17874. (call-interactively 'org-priority-down))
  17875. ((and (not org-support-shift-select) (org-at-item-p))
  17876. (call-interactively 'org-next-item))
  17877. ((org-clocktable-try-shift 'down arg))
  17878. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17879. (org-support-shift-select
  17880. (org-call-for-shift-select 'next-line))
  17881. (t (org-shiftselect-error))))
  17882. (defun org-shiftright (&optional arg)
  17883. "Cycle the thing at point or in the current line, depending on context.
  17884. Depending on context, this does one of the following:
  17885. - switch a timestamp at point one day into the future
  17886. - on a headline, switch to the next TODO keyword.
  17887. - on an item, switch entire list to the next bullet type
  17888. - on a property line, switch to the next allowed value
  17889. - on a clocktable definition line, move time block into the future"
  17890. (interactive "P")
  17891. (cond
  17892. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17893. ((and org-support-shift-select (org-region-active-p))
  17894. (org-call-for-shift-select 'forward-char))
  17895. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17896. ((and (not (eq org-support-shift-select 'always))
  17897. (org-at-heading-p))
  17898. (let ((org-inhibit-logging
  17899. (not org-treat-S-cursor-todo-selection-as-state-change))
  17900. (org-inhibit-blocking
  17901. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17902. (org-call-with-arg 'org-todo 'right)))
  17903. ((or (and org-support-shift-select
  17904. (not (eq org-support-shift-select 'always))
  17905. (org-at-item-bullet-p))
  17906. (and (not org-support-shift-select) (org-at-item-p)))
  17907. (org-call-with-arg 'org-cycle-list-bullet nil))
  17908. ((and (not (eq org-support-shift-select 'always))
  17909. (org-at-property-p))
  17910. (call-interactively 'org-property-next-allowed-value))
  17911. ((org-clocktable-try-shift 'right arg))
  17912. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17913. (org-support-shift-select
  17914. (org-call-for-shift-select 'forward-char))
  17915. (t (org-shiftselect-error))))
  17916. (defun org-shiftleft (&optional arg)
  17917. "Cycle the thing at point or in the current line, depending on context.
  17918. Depending on context, this does one of the following:
  17919. - switch a timestamp at point one day into the past
  17920. - on a headline, switch to the previous TODO keyword.
  17921. - on an item, switch entire list to the previous bullet type
  17922. - on a property line, switch to the previous allowed value
  17923. - on a clocktable definition line, move time block into the past"
  17924. (interactive "P")
  17925. (cond
  17926. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17927. ((and org-support-shift-select (org-region-active-p))
  17928. (org-call-for-shift-select 'backward-char))
  17929. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17930. ((and (not (eq org-support-shift-select 'always))
  17931. (org-at-heading-p))
  17932. (let ((org-inhibit-logging
  17933. (not org-treat-S-cursor-todo-selection-as-state-change))
  17934. (org-inhibit-blocking
  17935. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17936. (org-call-with-arg 'org-todo 'left)))
  17937. ((or (and org-support-shift-select
  17938. (not (eq org-support-shift-select 'always))
  17939. (org-at-item-bullet-p))
  17940. (and (not org-support-shift-select) (org-at-item-p)))
  17941. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17942. ((and (not (eq org-support-shift-select 'always))
  17943. (org-at-property-p))
  17944. (call-interactively 'org-property-previous-allowed-value))
  17945. ((org-clocktable-try-shift 'left arg))
  17946. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17947. (org-support-shift-select
  17948. (org-call-for-shift-select 'backward-char))
  17949. (t (org-shiftselect-error))))
  17950. (defun org-shiftcontrolright ()
  17951. "Switch to next TODO set."
  17952. (interactive)
  17953. (cond
  17954. ((and org-support-shift-select (org-region-active-p))
  17955. (org-call-for-shift-select 'forward-word))
  17956. ((and (not (eq org-support-shift-select 'always))
  17957. (org-at-heading-p))
  17958. (org-call-with-arg 'org-todo 'nextset))
  17959. (org-support-shift-select
  17960. (org-call-for-shift-select 'forward-word))
  17961. (t (org-shiftselect-error))))
  17962. (defun org-shiftcontrolleft ()
  17963. "Switch to previous TODO set."
  17964. (interactive)
  17965. (cond
  17966. ((and org-support-shift-select (org-region-active-p))
  17967. (org-call-for-shift-select 'backward-word))
  17968. ((and (not (eq org-support-shift-select 'always))
  17969. (org-at-heading-p))
  17970. (org-call-with-arg 'org-todo 'previousset))
  17971. (org-support-shift-select
  17972. (org-call-for-shift-select 'backward-word))
  17973. (t (org-shiftselect-error))))
  17974. (defun org-shiftcontrolup (&optional n)
  17975. "Change timestamps synchronously up in CLOCK log lines.
  17976. Optional argument N tells to change by that many units."
  17977. (interactive "P")
  17978. (cond ((and (not org-support-shift-select)
  17979. (org-at-clock-log-p)
  17980. (org-at-timestamp-p t))
  17981. (org-clock-timestamps-up n))
  17982. (t (org-shiftselect-error))))
  17983. (defun org-shiftcontroldown (&optional n)
  17984. "Change timestamps synchronously down in CLOCK log lines.
  17985. Optional argument N tells to change by that many units."
  17986. (interactive "P")
  17987. (cond ((and (not org-support-shift-select)
  17988. (org-at-clock-log-p)
  17989. (org-at-timestamp-p t))
  17990. (org-clock-timestamps-down n))
  17991. (t (org-shiftselect-error))))
  17992. (defun org-ctrl-c-ret ()
  17993. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17994. (interactive)
  17995. (cond
  17996. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17997. (t (call-interactively 'org-insert-heading))))
  17998. (defun org-find-visible ()
  17999. (let ((s (point)))
  18000. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18001. (get-char-property s 'invisible)))
  18002. s))
  18003. (defun org-find-invisible ()
  18004. (let ((s (point)))
  18005. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18006. (not (get-char-property s 'invisible))))
  18007. s))
  18008. (defun org-copy-visible (beg end)
  18009. "Copy the visible parts of the region."
  18010. (interactive "r")
  18011. (let (snippets s)
  18012. (save-excursion
  18013. (save-restriction
  18014. (narrow-to-region beg end)
  18015. (setq s (goto-char (point-min)))
  18016. (while (not (= (point) (point-max)))
  18017. (goto-char (org-find-invisible))
  18018. (push (buffer-substring s (point)) snippets)
  18019. (setq s (goto-char (org-find-visible))))))
  18020. (kill-new (apply 'concat (nreverse snippets)))))
  18021. (defun org-copy-special ()
  18022. "Copy region in table or copy current subtree.
  18023. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18024. See the individual commands for more information."
  18025. (interactive)
  18026. (call-interactively
  18027. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18028. (defun org-cut-special ()
  18029. "Cut region in table or cut current subtree.
  18030. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18031. See the individual commands for more information."
  18032. (interactive)
  18033. (call-interactively
  18034. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18035. (defun org-paste-special (arg)
  18036. "Paste rectangular region into table, or past subtree relative to level.
  18037. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18038. See the individual commands for more information."
  18039. (interactive "P")
  18040. (if (org-at-table-p)
  18041. (org-table-paste-rectangle)
  18042. (org-paste-subtree arg)))
  18043. (defsubst org-in-fixed-width-region-p ()
  18044. "Is point in a fixed-width region?"
  18045. (save-match-data
  18046. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18047. (defun org-edit-special (&optional arg)
  18048. "Call a special editor for the element at point.
  18049. When at a table, call the formula editor with `org-table-edit-formulas'.
  18050. When in a source code block, call `org-edit-src-code'.
  18051. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18052. When at an #+INCLUDE keyword, visit the included file.
  18053. On a link, call `ffap' to visit the link at point.
  18054. Otherwise, return a user error."
  18055. (interactive)
  18056. (let ((element (org-element-at-point)))
  18057. (assert (not buffer-read-only) nil
  18058. "Buffer is read-only: %s" (buffer-name))
  18059. (case (org-element-type element)
  18060. (src-block
  18061. (if (not arg) (org-edit-src-code)
  18062. (let* ((info (org-babel-get-src-block-info))
  18063. (lang (nth 0 info))
  18064. (params (nth 2 info))
  18065. (session (cdr (assq :session params))))
  18066. (if (not session) (org-edit-src-code)
  18067. ;; At a src-block with a session and function called with
  18068. ;; an ARG: switch to the buffer related to the inferior
  18069. ;; process.
  18070. (funcall (intern (concat "org-babel-prep-session:" lang))
  18071. session params)))))
  18072. (keyword
  18073. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18074. (find-file
  18075. (org-remove-double-quotes
  18076. (car (org-split-string (org-element-property :value element)))))
  18077. (user-error "No special environment to edit here")))
  18078. (table
  18079. (if (eq (org-element-property :type element) 'table.el)
  18080. (org-edit-src-code)
  18081. (call-interactively 'org-table-edit-formulas)))
  18082. ;; Only Org tables contain `table-row' type elements.
  18083. (table-row (call-interactively 'org-table-edit-formulas))
  18084. ((example-block export-block) (org-edit-src-code))
  18085. (fixed-width (org-edit-fixed-width-region))
  18086. (otherwise
  18087. ;; No notable element at point. Though, we may be at a link,
  18088. ;; which is an object. Thus, scan deeper.
  18089. (if (eq (org-element-type (org-element-context element)) 'link)
  18090. (call-interactively 'ffap)
  18091. (user-error "No special environment to edit here"))))))
  18092. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18093. (defun org-ctrl-c-ctrl-c (&optional arg)
  18094. "Set tags in headline, or update according to changed information at point.
  18095. This command does many different things, depending on context:
  18096. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18097. this is what we do.
  18098. - If the cursor is on a statistics cookie, update it.
  18099. - If the cursor is in a headline, prompt for tags and insert them
  18100. into the current line, aligned to `org-tags-column'. When called
  18101. with prefix arg, realign all tags in the current buffer.
  18102. - If the cursor is in one of the special #+KEYWORD lines, this
  18103. triggers scanning the buffer for these lines and updating the
  18104. information.
  18105. - If the cursor is inside a table, realign the table. This command
  18106. works even if the automatic table editor has been turned off.
  18107. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18108. the entire table.
  18109. - If the cursor is at a footnote reference or definition, jump to
  18110. the corresponding definition or references, respectively.
  18111. - If the cursor is a the beginning of a dynamic block, update it.
  18112. - If the current buffer is a capture buffer, close note and file it.
  18113. - If the cursor is on a <<<target>>>, update radio targets and
  18114. corresponding links in this buffer.
  18115. - If the cursor is on a numbered item in a plain list, renumber the
  18116. ordered list.
  18117. - If the cursor is on a checkbox, toggle it.
  18118. - If the cursor is on a code block, evaluate it. The variable
  18119. `org-confirm-babel-evaluate' can be used to control prompting
  18120. before code block evaluation, by default every code block
  18121. evaluation requires confirmation. Code block evaluation can be
  18122. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18123. (interactive "P")
  18124. (cond
  18125. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18126. org-occur-highlights
  18127. org-latex-fragment-image-overlays)
  18128. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18129. (org-remove-occur-highlights)
  18130. (org-remove-latex-fragment-image-overlays)
  18131. (message "Temporary highlights/overlays removed from current buffer"))
  18132. ((and (local-variable-p 'org-finish-function (current-buffer))
  18133. (fboundp org-finish-function))
  18134. (funcall org-finish-function))
  18135. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18136. (t
  18137. (let* ((context (org-element-context)) (type (org-element-type context)))
  18138. ;; Test if point is within a blank line.
  18139. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18140. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18141. (user-error "C-c C-c can do nothing useful at this location"))
  18142. ;; For convenience: at the first line of a paragraph on the
  18143. ;; same line as an item, apply function on that item instead.
  18144. (when (eq type 'paragraph)
  18145. (let ((parent (org-element-property :parent context)))
  18146. (when (and (eq (org-element-type parent) 'item)
  18147. (= (point-at-bol) (org-element-property :begin parent)))
  18148. (setq context parent type 'item))))
  18149. ;; Act according to type of element or object at point.
  18150. (case type
  18151. (clock (org-clock-update-time-maybe))
  18152. (dynamic-block
  18153. (save-excursion
  18154. (goto-char (org-element-property :post-affiliated context))
  18155. (org-update-dblock)))
  18156. (footnote-definition
  18157. (goto-char (org-element-property :post-affiliated context))
  18158. (call-interactively 'org-footnote-action))
  18159. (footnote-reference (call-interactively 'org-footnote-action))
  18160. ((headline inlinetask)
  18161. (save-excursion (goto-char (org-element-property :begin context))
  18162. (call-interactively 'org-set-tags)))
  18163. (item
  18164. ;; At an item: a double C-u set checkbox to "[-]"
  18165. ;; unconditionally, whereas a single one will toggle its
  18166. ;; presence. Without an universal argument, if the item
  18167. ;; has a checkbox, toggle it. Otherwise repair the list.
  18168. (let* ((box (org-element-property :checkbox context))
  18169. (struct (org-element-property :structure context))
  18170. (old-struct (copy-tree struct))
  18171. (parents (org-list-parents-alist struct))
  18172. (prevs (org-list-prevs-alist struct))
  18173. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18174. (org-list-set-checkbox
  18175. (org-element-property :begin context) struct
  18176. (cond ((equal arg '(16)) "[-]")
  18177. ((and (not box) (equal arg '(4))) "[ ]")
  18178. ((or (not box) (equal arg '(4))) nil)
  18179. ((eq box 'on) "[ ]")
  18180. (t "[X]")))
  18181. ;; Mimic `org-list-write-struct' but with grabbing
  18182. ;; a return value from `org-list-struct-fix-box'.
  18183. (org-list-struct-fix-ind struct parents 2)
  18184. (org-list-struct-fix-item-end struct)
  18185. (org-list-struct-fix-bul struct prevs)
  18186. (org-list-struct-fix-ind struct parents)
  18187. (let ((block-item
  18188. (org-list-struct-fix-box struct parents prevs orderedp)))
  18189. (if (and box (equal struct old-struct))
  18190. (if (equal arg '(16))
  18191. (message "Checkboxes already reset")
  18192. (user-error "Cannot toggle this checkbox: %s"
  18193. (if (eq box 'on)
  18194. "all subitems checked"
  18195. "unchecked subitems")))
  18196. (org-list-struct-apply-struct struct old-struct)
  18197. (org-update-checkbox-count-maybe))
  18198. (when block-item
  18199. (message "Checkboxes were removed due to empty box at line %d"
  18200. (org-current-line block-item))))))
  18201. (keyword
  18202. (let ((org-inhibit-startup-visibility-stuff t)
  18203. (org-startup-align-all-tables nil))
  18204. (when (boundp 'org-table-coordinate-overlays)
  18205. (mapc 'delete-overlay org-table-coordinate-overlays)
  18206. (setq org-table-coordinate-overlays nil))
  18207. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18208. (message "Local setup has been refreshed"))
  18209. (plain-list
  18210. ;; At a plain list, with a double C-u argument, set
  18211. ;; checkboxes of each item to "[-]", whereas a single one
  18212. ;; will toggle their presence according to the state of the
  18213. ;; first item in the list. Without an argument, repair the
  18214. ;; list.
  18215. (let* ((begin (org-element-property :contents-begin context))
  18216. (struct (org-element-property :structure context))
  18217. (old-struct (copy-tree struct))
  18218. (first-box (save-excursion
  18219. (goto-char begin)
  18220. (looking-at org-list-full-item-re)
  18221. (match-string-no-properties 3)))
  18222. (new-box (cond ((equal arg '(16)) "[-]")
  18223. ((equal arg '(4)) (unless first-box "[ ]"))
  18224. ((equal first-box "[X]") "[ ]")
  18225. (t "[X]"))))
  18226. (cond
  18227. (arg
  18228. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18229. (org-list-get-all-items
  18230. begin struct (org-list-prevs-alist struct))))
  18231. ((and first-box (eq (point) begin))
  18232. ;; For convenience, when point is at bol on the first
  18233. ;; item of the list and no argument is provided, simply
  18234. ;; toggle checkbox of that item, if any.
  18235. (org-list-set-checkbox begin struct new-box)))
  18236. (org-list-write-struct
  18237. struct (org-list-parents-alist struct) old-struct)
  18238. (org-update-checkbox-count-maybe)
  18239. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18240. ((property-drawer node-property)
  18241. (call-interactively 'org-property-action))
  18242. ((radio-target target)
  18243. (call-interactively 'org-update-radio-target-regexp))
  18244. (statistics-cookie
  18245. (call-interactively 'org-update-statistics-cookies))
  18246. ((table table-cell table-row)
  18247. ;; At a table, recalculate every field and align it. Also
  18248. ;; send the table if necessary. If the table has
  18249. ;; a `table.el' type, just give up. At a table row or
  18250. ;; cell, maybe recalculate line but always align table.
  18251. (if (eq (org-element-property :type context) 'table.el)
  18252. (message "Use C-c ' to edit table.el tables")
  18253. (let ((org-enable-table-editor t))
  18254. (if (or (eq type 'table)
  18255. ;; Check if point is at a TBLFM line.
  18256. (and (eq type 'table-row)
  18257. (= (point) (org-element-property :end context))))
  18258. (save-excursion
  18259. (goto-char (org-element-property :contents-begin context))
  18260. (org-call-with-arg 'org-table-recalculate (or arg t))
  18261. (orgtbl-send-table 'maybe))
  18262. (org-table-maybe-eval-formula)
  18263. (cond (arg (call-interactively 'org-table-recalculate))
  18264. ((org-table-maybe-recalculate-line))
  18265. (t (org-table-align)))))))
  18266. (timestamp (org-timestamp-change 0 'day))
  18267. (otherwise
  18268. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18269. (user-error
  18270. "C-c C-c can do nothing useful at this location")))))))))
  18271. (defun org-mode-restart ()
  18272. "Restart Org-mode, to scan again for special lines.
  18273. Also updates the keyword regular expressions."
  18274. (interactive)
  18275. (org-mode)
  18276. (message "Org-mode restarted"))
  18277. (defun org-kill-note-or-show-branches ()
  18278. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18279. (interactive)
  18280. (if (not org-finish-function)
  18281. (progn
  18282. (hide-subtree)
  18283. (call-interactively 'show-branches))
  18284. (let ((org-note-abort t))
  18285. (funcall org-finish-function))))
  18286. (defun org-return (&optional indent)
  18287. "Goto next table row or insert a newline.
  18288. Calls `org-table-next-row' or `newline', depending on context.
  18289. See the individual commands for more information."
  18290. (interactive)
  18291. (let (org-ts-what)
  18292. (cond
  18293. ((or (bobp) (org-in-src-block-p))
  18294. (if indent (newline-and-indent) (newline)))
  18295. ((org-at-table-p)
  18296. (org-table-justify-field-maybe)
  18297. (call-interactively 'org-table-next-row))
  18298. ;; when `newline-and-indent' is called within a list, make sure
  18299. ;; text moved stays inside the item.
  18300. ((and (org-in-item-p) indent)
  18301. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18302. (progn
  18303. (save-match-data (newline))
  18304. (org-indent-line-to (length (match-string 0))))
  18305. (let ((ind (org-get-indentation)))
  18306. (newline)
  18307. (if (org-looking-back org-list-end-re)
  18308. (org-indent-line)
  18309. (org-indent-line-to ind)))))
  18310. ((and org-return-follows-link
  18311. (org-at-timestamp-p t)
  18312. (not (eq org-ts-what 'after)))
  18313. (org-follow-timestamp-link))
  18314. ((and org-return-follows-link
  18315. (let ((tprop (get-text-property (point) 'face)))
  18316. (or (eq tprop 'org-link)
  18317. (and (listp tprop) (memq 'org-link tprop)))))
  18318. (call-interactively 'org-open-at-point))
  18319. ((and (org-at-heading-p)
  18320. (looking-at
  18321. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18322. (org-show-entry)
  18323. (end-of-line 1)
  18324. (newline))
  18325. (t (if indent (newline-and-indent) (newline))))))
  18326. (defun org-return-indent ()
  18327. "Goto next table row or insert a newline and indent.
  18328. Calls `org-table-next-row' or `newline-and-indent', depending on
  18329. context. See the individual commands for more information."
  18330. (interactive)
  18331. (org-return t))
  18332. (defun org-ctrl-c-star ()
  18333. "Compute table, or change heading status of lines.
  18334. Calls `org-table-recalculate' or `org-toggle-heading',
  18335. depending on context."
  18336. (interactive)
  18337. (cond
  18338. ((org-at-table-p)
  18339. (call-interactively 'org-table-recalculate))
  18340. (t
  18341. ;; Convert all lines in region to list items
  18342. (call-interactively 'org-toggle-heading))))
  18343. (defun org-ctrl-c-minus ()
  18344. "Insert separator line in table or modify bullet status of line.
  18345. Also turns a plain line or a region of lines into list items.
  18346. Calls `org-table-insert-hline', `org-toggle-item', or
  18347. `org-cycle-list-bullet', depending on context."
  18348. (interactive)
  18349. (cond
  18350. ((org-at-table-p)
  18351. (call-interactively 'org-table-insert-hline))
  18352. ((org-region-active-p)
  18353. (call-interactively 'org-toggle-item))
  18354. ((org-in-item-p)
  18355. (call-interactively 'org-cycle-list-bullet))
  18356. (t
  18357. (call-interactively 'org-toggle-item))))
  18358. (defun org-toggle-item (arg)
  18359. "Convert headings or normal lines to items, items to normal lines.
  18360. If there is no active region, only the current line is considered.
  18361. If the first non blank line in the region is a headline, convert
  18362. all headlines to items, shifting text accordingly.
  18363. If it is an item, convert all items to normal lines.
  18364. If it is normal text, change region into an item. With a prefix
  18365. argument ARG, change each line in region into an item."
  18366. (interactive "P")
  18367. (let ((shift-text
  18368. (function
  18369. ;; Shift text in current section to IND, from point to END.
  18370. ;; The function leaves point to END line.
  18371. (lambda (ind end)
  18372. (let ((min-i 1000) (end (copy-marker end)))
  18373. ;; First determine the minimum indentation (MIN-I) of
  18374. ;; the text.
  18375. (save-excursion
  18376. (catch 'exit
  18377. (while (< (point) end)
  18378. (let ((i (org-get-indentation)))
  18379. (cond
  18380. ;; Skip blank lines and inline tasks.
  18381. ((looking-at "^[ \t]*$"))
  18382. ((looking-at org-outline-regexp-bol))
  18383. ;; We can't find less than 0 indentation.
  18384. ((zerop i) (throw 'exit (setq min-i 0)))
  18385. ((< i min-i) (setq min-i i))))
  18386. (forward-line))))
  18387. ;; Then indent each line so that a line indented to
  18388. ;; MIN-I becomes indented to IND. Ignore blank lines
  18389. ;; and inline tasks in the process.
  18390. (let ((delta (- ind min-i)))
  18391. (while (< (point) end)
  18392. (unless (or (looking-at "^[ \t]*$")
  18393. (looking-at org-outline-regexp-bol))
  18394. (org-indent-line-to (+ (org-get-indentation) delta)))
  18395. (forward-line)))))))
  18396. (skip-blanks
  18397. (function
  18398. ;; Return beginning of first non-blank line, starting from
  18399. ;; line at POS.
  18400. (lambda (pos)
  18401. (save-excursion
  18402. (goto-char pos)
  18403. (skip-chars-forward " \r\t\n")
  18404. (point-at-bol)))))
  18405. beg end)
  18406. ;; Determine boundaries of changes.
  18407. (if (org-region-active-p)
  18408. (setq beg (funcall skip-blanks (region-beginning))
  18409. end (copy-marker (region-end)))
  18410. (setq beg (funcall skip-blanks (point-at-bol))
  18411. end (copy-marker (point-at-eol))))
  18412. ;; Depending on the starting line, choose an action on the text
  18413. ;; between BEG and END.
  18414. (org-with-limited-levels
  18415. (save-excursion
  18416. (goto-char beg)
  18417. (cond
  18418. ;; Case 1. Start at an item: de-itemize. Note that it only
  18419. ;; happens when a region is active: `org-ctrl-c-minus'
  18420. ;; would call `org-cycle-list-bullet' otherwise.
  18421. ((org-at-item-p)
  18422. (while (< (point) end)
  18423. (when (org-at-item-p)
  18424. (skip-chars-forward " \t")
  18425. (delete-region (point) (match-end 0)))
  18426. (forward-line)))
  18427. ;; Case 2. Start at an heading: convert to items.
  18428. ((org-at-heading-p)
  18429. (let* ((bul (org-list-bullet-string "-"))
  18430. (bul-len (length bul))
  18431. ;; Indentation of the first heading. It should be
  18432. ;; relative to the indentation of its parent, if any.
  18433. (start-ind (save-excursion
  18434. (cond
  18435. ((not org-adapt-indentation) 0)
  18436. ((not (outline-previous-heading)) 0)
  18437. (t (length (match-string 0))))))
  18438. ;; Level of first heading. Further headings will be
  18439. ;; compared to it to determine hierarchy in the list.
  18440. (ref-level (org-reduced-level (org-outline-level))))
  18441. (while (< (point) end)
  18442. (let* ((level (org-reduced-level (org-outline-level)))
  18443. (delta (max 0 (- level ref-level))))
  18444. ;; If current headline is less indented than the first
  18445. ;; one, set it as reference, in order to preserve
  18446. ;; subtrees.
  18447. (when (< level ref-level) (setq ref-level level))
  18448. (replace-match bul t t)
  18449. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18450. ;; Ensure all text down to END (or SECTION-END) belongs
  18451. ;; to the newly created item.
  18452. (let ((section-end (save-excursion
  18453. (or (outline-next-heading) (point)))))
  18454. (forward-line)
  18455. (funcall shift-text
  18456. (+ start-ind (* (1+ delta) bul-len))
  18457. (min end section-end)))))))
  18458. ;; Case 3. Normal line with ARG: turn each non-item line into
  18459. ;; an item.
  18460. (arg
  18461. (while (< (point) end)
  18462. (unless (or (org-at-heading-p) (org-at-item-p))
  18463. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18464. (replace-match
  18465. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18466. (forward-line)))
  18467. ;; Case 4. Normal line without ARG: make the first line of
  18468. ;; region an item, and shift indentation of others
  18469. ;; lines to set them as item's body.
  18470. (t (let* ((bul (org-list-bullet-string "-"))
  18471. (bul-len (length bul))
  18472. (ref-ind (org-get-indentation)))
  18473. (skip-chars-forward " \t")
  18474. (insert bul)
  18475. (forward-line)
  18476. (while (< (point) end)
  18477. ;; Ensure that lines less indented than first one
  18478. ;; still get included in item body.
  18479. (funcall shift-text
  18480. (+ ref-ind bul-len)
  18481. (min end (save-excursion (or (outline-next-heading)
  18482. (point)))))
  18483. (forward-line)))))))))
  18484. (defun org-toggle-heading (&optional nstars)
  18485. "Convert headings to normal text, or items or text to headings.
  18486. If there is no active region, only the current line is considered.
  18487. With a \\[universal-argument] prefix, convert the whole list at
  18488. point into heading.
  18489. In a region:
  18490. - If the first non blank line is a headline, remove the stars
  18491. from all headlines in the region.
  18492. - If it is a normal line turn each and every normal line (i.e. not an
  18493. heading or an item) in the region into a heading.
  18494. - If it is a plain list item, turn all plain list items into headings.
  18495. When converting a line into a heading, the number of stars is chosen
  18496. such that the lines become children of the current entry. However,
  18497. when a prefix argument is given, its value determines the number of
  18498. stars to add."
  18499. (interactive "P")
  18500. (let ((skip-blanks
  18501. (function
  18502. ;; Return beginning of first non-blank line, starting from
  18503. ;; line at POS.
  18504. (lambda (pos)
  18505. (save-excursion
  18506. (goto-char pos)
  18507. (while (org-at-comment-p) (forward-line))
  18508. (skip-chars-forward " \r\t\n")
  18509. (point-at-bol)))))
  18510. beg end toggled)
  18511. ;; Determine boundaries of changes. If a universal prefix has
  18512. ;; been given, put the list in a region. If region ends at a bol,
  18513. ;; do not consider the last line to be in the region.
  18514. (when (and current-prefix-arg (org-at-item-p))
  18515. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18516. (org-mark-element))
  18517. (if (org-region-active-p)
  18518. (setq beg (funcall skip-blanks (region-beginning))
  18519. end (copy-marker (save-excursion
  18520. (goto-char (region-end))
  18521. (if (bolp) (point) (point-at-eol)))))
  18522. (setq beg (funcall skip-blanks (point-at-bol))
  18523. end (copy-marker (point-at-eol))))
  18524. ;; Ensure inline tasks don't count as headings.
  18525. (org-with-limited-levels
  18526. (save-excursion
  18527. (goto-char beg)
  18528. (cond
  18529. ;; Case 1. Started at an heading: de-star headings.
  18530. ((org-at-heading-p)
  18531. (while (< (point) end)
  18532. (when (org-at-heading-p t)
  18533. (looking-at org-outline-regexp) (replace-match "")
  18534. (setq toggled t))
  18535. (forward-line)))
  18536. ;; Case 2. Started at an item: change items into headlines.
  18537. ;; One star will be added by `org-list-to-subtree'.
  18538. ((org-at-item-p)
  18539. (let* ((stars (make-string
  18540. (if nstars
  18541. ;; subtract the star that will be added again by
  18542. ;; `org-list-to-subtree'
  18543. (1- (prefix-numeric-value current-prefix-arg))
  18544. (or (org-current-level) 0))
  18545. ?*))
  18546. (add-stars
  18547. (cond (nstars "") ; stars from prefix only
  18548. ((equal stars "") "") ; before first heading
  18549. (org-odd-levels-only "*") ; inside heading, odd
  18550. (t "")))) ; inside heading, oddeven
  18551. (while (< (point) end)
  18552. (when (org-at-item-p)
  18553. ;; Pay attention to cases when region ends before list.
  18554. (let* ((struct (org-list-struct))
  18555. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18556. (save-restriction
  18557. (narrow-to-region (point) list-end)
  18558. (insert
  18559. (org-list-to-subtree
  18560. (org-list-parse-list t)
  18561. '(:istart (concat stars add-stars (funcall get-stars depth))
  18562. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18563. (setq toggled t))
  18564. (forward-line))))
  18565. ;; Case 3. Started at normal text: make every line an heading,
  18566. ;; skipping headlines and items.
  18567. (t (let* ((stars (make-string
  18568. (if nstars
  18569. (prefix-numeric-value current-prefix-arg)
  18570. (or (org-current-level) 0))
  18571. ?*))
  18572. (add-stars
  18573. (cond (nstars "") ; stars from prefix only
  18574. ((equal stars "") "*") ; before first heading
  18575. (org-odd-levels-only "**") ; inside heading, odd
  18576. (t "*"))) ; inside heading, oddeven
  18577. (rpl (concat stars add-stars " ")))
  18578. (while (< (point) end)
  18579. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18580. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18581. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18582. (forward-line)))))))
  18583. (unless toggled (message "Cannot toggle heading from here"))))
  18584. (defun org-meta-return (&optional arg)
  18585. "Insert a new heading or wrap a region in a table.
  18586. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18587. See the individual commands for more information."
  18588. (interactive "P")
  18589. (cond
  18590. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18591. ((or (org-at-drawer-p) (org-at-property-p))
  18592. (newline-and-indent))
  18593. ((org-at-table-p)
  18594. (call-interactively 'org-table-wrap-region))
  18595. (t (call-interactively 'org-insert-heading))))
  18596. ;;; Menu entries
  18597. (defsubst org-in-subtree-not-table-p ()
  18598. "Are we in a subtree and not in a table?"
  18599. (and (not (org-before-first-heading-p))
  18600. (not (org-at-table-p))))
  18601. ;; Define the Org-mode menus
  18602. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18603. '("Tbl"
  18604. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18605. ["Next Field" org-cycle (org-at-table-p)]
  18606. ["Previous Field" org-shifttab (org-at-table-p)]
  18607. ["Next Row" org-return (org-at-table-p)]
  18608. "--"
  18609. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18610. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18611. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18612. "--"
  18613. ("Column"
  18614. ["Move Column Left" org-metaleft (org-at-table-p)]
  18615. ["Move Column Right" org-metaright (org-at-table-p)]
  18616. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18617. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18618. ("Row"
  18619. ["Move Row Up" org-metaup (org-at-table-p)]
  18620. ["Move Row Down" org-metadown (org-at-table-p)]
  18621. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18622. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18623. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18624. "--"
  18625. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18626. ("Rectangle"
  18627. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18628. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18629. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18630. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18631. "--"
  18632. ("Calculate"
  18633. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18634. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18635. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18636. "--"
  18637. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18638. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18639. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18640. "--"
  18641. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18642. "--"
  18643. ["Sum Column/Rectangle" org-table-sum
  18644. (or (org-at-table-p) (org-region-active-p))]
  18645. ["Which Column?" org-table-current-column (org-at-table-p)])
  18646. ["Debug Formulas"
  18647. org-table-toggle-formula-debugger
  18648. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18649. ["Show Col/Row Numbers"
  18650. org-table-toggle-coordinate-overlays
  18651. :style toggle
  18652. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18653. "--"
  18654. ["Create" org-table-create (and (not (org-at-table-p))
  18655. org-enable-table-editor)]
  18656. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18657. ["Import from File" org-table-import (not (org-at-table-p))]
  18658. ["Export to File" org-table-export (org-at-table-p)]
  18659. "--"
  18660. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18661. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18662. '("Org"
  18663. ("Show/Hide"
  18664. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18665. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18666. ["Sparse Tree..." org-sparse-tree t]
  18667. ["Reveal Context" org-reveal t]
  18668. ["Show All" show-all t]
  18669. "--"
  18670. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18671. "--"
  18672. ["New Heading" org-insert-heading t]
  18673. ("Navigate Headings"
  18674. ["Up" outline-up-heading t]
  18675. ["Next" outline-next-visible-heading t]
  18676. ["Previous" outline-previous-visible-heading t]
  18677. ["Next Same Level" outline-forward-same-level t]
  18678. ["Previous Same Level" outline-backward-same-level t]
  18679. "--"
  18680. ["Jump" org-goto t])
  18681. ("Edit Structure"
  18682. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18683. "--"
  18684. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18685. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18686. "--"
  18687. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18688. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18689. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18690. "--"
  18691. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18692. "--"
  18693. ["Copy visible text" org-copy-visible t]
  18694. "--"
  18695. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18696. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18697. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18698. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18699. "--"
  18700. ["Sort Region/Children" org-sort t]
  18701. "--"
  18702. ["Convert to odd levels" org-convert-to-odd-levels t]
  18703. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18704. ("Editing"
  18705. ["Emphasis..." org-emphasize t]
  18706. ["Edit Source Example" org-edit-special t]
  18707. "--"
  18708. ["Footnote new/jump" org-footnote-action t]
  18709. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18710. ("Archive"
  18711. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18712. "--"
  18713. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18714. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18715. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18716. )
  18717. "--"
  18718. ("Hyperlinks"
  18719. ["Store Link (Global)" org-store-link t]
  18720. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18721. ["Insert Link" org-insert-link t]
  18722. ["Follow Link" org-open-at-point t]
  18723. "--"
  18724. ["Next link" org-next-link t]
  18725. ["Previous link" org-previous-link t]
  18726. "--"
  18727. ["Descriptive Links"
  18728. org-toggle-link-display
  18729. :style radio
  18730. :selected org-descriptive-links
  18731. ]
  18732. ["Literal Links"
  18733. org-toggle-link-display
  18734. :style radio
  18735. :selected (not org-descriptive-links)])
  18736. "--"
  18737. ("TODO Lists"
  18738. ["TODO/DONE/-" org-todo t]
  18739. ("Select keyword"
  18740. ["Next keyword" org-shiftright (org-at-heading-p)]
  18741. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18742. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18743. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18744. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18745. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18746. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18747. "--"
  18748. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18749. :selected org-enforce-todo-dependencies :style toggle :active t]
  18750. "Settings for tree at point"
  18751. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18752. :selected (org-entry-get nil "ORDERED")
  18753. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18754. ["Do Children parallel" org-toggle-ordered-property :style radio
  18755. :selected (not (org-entry-get nil "ORDERED"))
  18756. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18757. "--"
  18758. ["Set Priority" org-priority t]
  18759. ["Priority Up" org-shiftup t]
  18760. ["Priority Down" org-shiftdown t]
  18761. "--"
  18762. ["Get news from all feeds" org-feed-update-all t]
  18763. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18764. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18765. ("TAGS and Properties"
  18766. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18767. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18768. "--"
  18769. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18770. ["Column view of properties" org-columns t]
  18771. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18772. ("Dates and Scheduling"
  18773. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18774. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18775. ("Change Date"
  18776. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18777. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18778. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18779. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18780. ["Compute Time Range" org-evaluate-time-range t]
  18781. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18782. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18783. "--"
  18784. ["Custom time format" org-toggle-time-stamp-overlays
  18785. :style radio :selected org-display-custom-times]
  18786. "--"
  18787. ["Goto Calendar" org-goto-calendar t]
  18788. ["Date from Calendar" org-date-from-calendar t]
  18789. "--"
  18790. ["Start/Restart Timer" org-timer-start t]
  18791. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18792. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18793. ["Insert Timer String" org-timer t]
  18794. ["Insert Timer Item" org-timer-item t])
  18795. ("Logging work"
  18796. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18797. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18798. ["Clock out" org-clock-out t]
  18799. ["Clock cancel" org-clock-cancel t]
  18800. "--"
  18801. ["Mark as default task" org-clock-mark-default-task t]
  18802. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18803. ["Goto running clock" org-clock-goto t]
  18804. "--"
  18805. ["Display times" org-clock-display t]
  18806. ["Create clock table" org-clock-report t]
  18807. "--"
  18808. ["Record DONE time"
  18809. (progn (setq org-log-done (not org-log-done))
  18810. (message "Switching to %s will %s record a timestamp"
  18811. (car org-done-keywords)
  18812. (if org-log-done "automatically" "not")))
  18813. :style toggle :selected org-log-done])
  18814. "--"
  18815. ["Agenda Command..." org-agenda t]
  18816. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18817. ("File List for Agenda")
  18818. ("Special views current file"
  18819. ["TODO Tree" org-show-todo-tree t]
  18820. ["Check Deadlines" org-check-deadlines t]
  18821. ["Timeline" org-timeline t]
  18822. ["Tags/Property tree" org-match-sparse-tree t])
  18823. "--"
  18824. ["Export/Publish..." org-export-dispatch t]
  18825. ("LaTeX"
  18826. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18827. :selected org-cdlatex-mode]
  18828. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18829. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18830. ["Modify math symbol" org-cdlatex-math-modify
  18831. (org-inside-LaTeX-fragment-p)]
  18832. ["Insert citation" org-reftex-citation t]
  18833. "--"
  18834. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18835. "--"
  18836. ("MobileOrg"
  18837. ["Push Files and Views" org-mobile-push t]
  18838. ["Get Captured and Flagged" org-mobile-pull t]
  18839. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18840. "--"
  18841. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18842. "--"
  18843. ("Documentation"
  18844. ["Show Version" org-version t]
  18845. ["Info Documentation" org-info t])
  18846. ("Customize"
  18847. ["Browse Org Group" org-customize t]
  18848. "--"
  18849. ["Expand This Menu" org-create-customize-menu
  18850. (fboundp 'customize-menu-create)])
  18851. ["Send bug report" org-submit-bug-report t]
  18852. "--"
  18853. ("Refresh/Reload"
  18854. ["Refresh setup current buffer" org-mode-restart t]
  18855. ["Reload Org (after update)" org-reload t]
  18856. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18857. ))
  18858. (defun org-info (&optional node)
  18859. "Read documentation for Org-mode in the info system.
  18860. With optional NODE, go directly to that node."
  18861. (interactive)
  18862. (info (format "(org)%s" (or node ""))))
  18863. ;;;###autoload
  18864. (defun org-submit-bug-report ()
  18865. "Submit a bug report on Org-mode via mail.
  18866. Don't hesitate to report any problems or inaccurate documentation.
  18867. If you don't have setup sending mail from (X)Emacs, please copy the
  18868. output buffer into your mail program, as it gives us important
  18869. information about your Org-mode version and configuration."
  18870. (interactive)
  18871. (require 'reporter)
  18872. (org-load-modules-maybe)
  18873. (org-require-autoloaded-modules)
  18874. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18875. (reporter-submit-bug-report
  18876. "emacs-orgmode@gnu.org"
  18877. (org-version nil 'full)
  18878. (let (list)
  18879. (save-window-excursion
  18880. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18881. (delete-other-windows)
  18882. (erase-buffer)
  18883. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18884. We would like to add your full Org-mode and Outline configuration to the
  18885. bug report. This greatly simplifies the work of the maintainer and
  18886. other experts on the mailing list.
  18887. HOWEVER, some variables you have customized may contain private
  18888. information. The names of customers, colleagues, or friends, might
  18889. appear in the form of file names, tags, todo states, or search strings.
  18890. If you answer yes to the prompt, you might want to check and remove
  18891. such private information before sending the email.")
  18892. (add-text-properties (point-min) (point-max) '(face org-warning))
  18893. (when (yes-or-no-p "Include your Org-mode configuration ")
  18894. (mapatoms
  18895. (lambda (v)
  18896. (and (boundp v)
  18897. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18898. (or (and (symbol-value v)
  18899. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18900. (and
  18901. (get v 'custom-type) (get v 'standard-value)
  18902. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18903. (push v list)))))
  18904. (kill-buffer (get-buffer "*Warn about privacy*"))
  18905. list))
  18906. nil nil
  18907. "Remember to cover the basics, that is, what you expected to happen and
  18908. what in fact did happen. You don't know how to make a good report? See
  18909. http://orgmode.org/manual/Feedback.html#Feedback
  18910. Your bug report will be posted to the Org-mode mailing list.
  18911. ------------------------------------------------------------------------")
  18912. (save-excursion
  18913. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18914. (replace-match "\\1Bug: \\3 [\\2]")))))
  18915. (defun org-install-agenda-files-menu ()
  18916. (let ((bl (buffer-list)))
  18917. (save-excursion
  18918. (while bl
  18919. (set-buffer (pop bl))
  18920. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18921. (when (derived-mode-p 'org-mode)
  18922. (easy-menu-change
  18923. '("Org") "File List for Agenda"
  18924. (append
  18925. (list
  18926. ["Edit File List" (org-edit-agenda-file-list) t]
  18927. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18928. ["Remove Current File from List" org-remove-file t]
  18929. ["Cycle through agenda files" org-cycle-agenda-files t]
  18930. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18931. "--")
  18932. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18933. ;;;; Documentation
  18934. (defun org-require-autoloaded-modules ()
  18935. (interactive)
  18936. (mapc 'require
  18937. '(org-agenda org-archive org-attach org-clock org-colview org-id
  18938. org-remember org-table org-timer)))
  18939. ;;;###autoload
  18940. (defun org-reload (&optional uncompiled)
  18941. "Reload all org lisp files.
  18942. With prefix arg UNCOMPILED, load the uncompiled versions."
  18943. (interactive "P")
  18944. (require 'loadhist)
  18945. (let* ((org-dir (org-find-library-dir "org"))
  18946. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18947. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  18948. (remove-re (mapconcat 'identity
  18949. (mapcar (lambda (f) (concat "^" f "$"))
  18950. (list (if (featurep 'xemacs)
  18951. "org-colview"
  18952. "org-colview-xemacs")
  18953. "org" "org-loaddefs" "org-version"))
  18954. "\\|"))
  18955. (feats (delete-dups
  18956. (mapcar 'file-name-sans-extension
  18957. (mapcar 'file-name-nondirectory
  18958. (delq nil
  18959. (mapcar 'feature-file
  18960. features))))))
  18961. (lfeat (append
  18962. (sort
  18963. (setq feats
  18964. (delq nil (mapcar
  18965. (lambda (f)
  18966. (if (and (string-match feature-re f)
  18967. (not (string-match remove-re f)))
  18968. f nil))
  18969. feats)))
  18970. 'string-lessp)
  18971. (list "org-version" "org")))
  18972. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  18973. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18974. load-uncore load-misses)
  18975. (setq load-misses
  18976. (delq 't
  18977. (mapcar (lambda (f)
  18978. (or (org-load-noerror-mustsuffix (concat org-dir f))
  18979. (and (string= org-dir contrib-dir)
  18980. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  18981. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  18982. (add-to-list 'load-uncore f 'append)
  18983. 't)
  18984. f))
  18985. lfeat)))
  18986. (if load-uncore
  18987. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18988. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18989. (if load-misses
  18990. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18991. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18992. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18993. ;;;###autoload
  18994. (defun org-customize ()
  18995. "Call the customize function with org as argument."
  18996. (interactive)
  18997. (org-load-modules-maybe)
  18998. (org-require-autoloaded-modules)
  18999. (customize-browse 'org))
  19000. (defun org-create-customize-menu ()
  19001. "Create a full customization menu for Org-mode, insert it into the menu."
  19002. (interactive)
  19003. (org-load-modules-maybe)
  19004. (org-require-autoloaded-modules)
  19005. (if (fboundp 'customize-menu-create)
  19006. (progn
  19007. (easy-menu-change
  19008. '("Org") "Customize"
  19009. `(["Browse Org group" org-customize t]
  19010. "--"
  19011. ,(customize-menu-create 'org)
  19012. ["Set" Custom-set t]
  19013. ["Save" Custom-save t]
  19014. ["Reset to Current" Custom-reset-current t]
  19015. ["Reset to Saved" Custom-reset-saved t]
  19016. ["Reset to Standard Settings" Custom-reset-standard t]))
  19017. (message "\"Org\"-menu now contains full customization menu"))
  19018. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19019. ;;;; Miscellaneous stuff
  19020. ;;; Generally useful functions
  19021. (defun org-get-at-bol (property)
  19022. "Get text property PROPERTY at beginning of line."
  19023. (get-text-property (point-at-bol) property))
  19024. (defun org-find-text-property-in-string (prop s)
  19025. "Return the first non-nil value of property PROP in string S."
  19026. (or (get-text-property 0 prop s)
  19027. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19028. prop s)))
  19029. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19030. "Display the given MESSAGE as a warning."
  19031. (if (fboundp 'display-warning)
  19032. (display-warning 'org message
  19033. (if (featurep 'xemacs) 'warning :warning))
  19034. (let ((buf (get-buffer-create "*Org warnings*")))
  19035. (with-current-buffer buf
  19036. (goto-char (point-max))
  19037. (insert "Warning (Org): " message)
  19038. (unless (bolp)
  19039. (newline)))
  19040. (display-buffer buf)
  19041. (sit-for 0))))
  19042. (defun org-eval (form)
  19043. "Eval FORM and return result."
  19044. (condition-case error
  19045. (eval form)
  19046. (error (format "%%![Error: %s]" error))))
  19047. (defun org-in-clocktable-p ()
  19048. "Check if the cursor is in a clocktable."
  19049. (let ((pos (point)) start)
  19050. (save-excursion
  19051. (end-of-line 1)
  19052. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19053. (setq start (match-beginning 0))
  19054. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19055. (>= (match-end 0) pos)
  19056. start))))
  19057. (defun org-in-commented-line ()
  19058. "Is point in a line starting with `#'?"
  19059. (equal (char-after (point-at-bol)) ?#))
  19060. (defun org-in-indented-comment-line ()
  19061. "Is point in a line starting with `#' after some white space?"
  19062. (save-excursion
  19063. (save-match-data
  19064. (goto-char (point-at-bol))
  19065. (looking-at "[ \t]*#"))))
  19066. (defun org-in-verbatim-emphasis ()
  19067. (save-match-data
  19068. (and (org-in-regexp org-emph-re 2)
  19069. (>= (point) (match-beginning 3))
  19070. (<= (point) (match-end 4))
  19071. (member (match-string 3) '("=" "~")))))
  19072. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19073. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19074. (if (and marker (marker-buffer marker)
  19075. (buffer-live-p (marker-buffer marker)))
  19076. (progn
  19077. (org-pop-to-buffer-same-window (marker-buffer marker))
  19078. (if (or (> marker (point-max)) (< marker (point-min)))
  19079. (widen))
  19080. (goto-char marker)
  19081. (org-show-context 'org-goto))
  19082. (if bookmark
  19083. (bookmark-jump bookmark)
  19084. (error "Cannot find location"))))
  19085. (defun org-quote-csv-field (s)
  19086. "Quote field for inclusion in CSV material."
  19087. (if (string-match "[\",]" s)
  19088. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19089. s))
  19090. (defun org-force-self-insert (N)
  19091. "Needed to enforce self-insert under remapping."
  19092. (interactive "p")
  19093. (self-insert-command N))
  19094. (defun org-string-width (s)
  19095. "Compute width of string, ignoring invisible characters.
  19096. This ignores character with invisibility property `org-link', and also
  19097. characters with property `org-cwidth', because these will become invisible
  19098. upon the next fontification round."
  19099. (let (b l)
  19100. (when (or (eq t buffer-invisibility-spec)
  19101. (assq 'org-link buffer-invisibility-spec))
  19102. (while (setq b (text-property-any 0 (length s)
  19103. 'invisible 'org-link s))
  19104. (setq s (concat (substring s 0 b)
  19105. (substring s (or (next-single-property-change
  19106. b 'invisible s) (length s)))))))
  19107. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19108. (setq s (concat (substring s 0 b)
  19109. (substring s (or (next-single-property-change
  19110. b 'org-cwidth s) (length s))))))
  19111. (setq l (string-width s) b -1)
  19112. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19113. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19114. l))
  19115. (defun org-shorten-string (s maxlength)
  19116. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19117. If the string is shorter or has length MAXLENGTH, just return the
  19118. original string. If it is longer, the functions finds a space in the
  19119. string, breaks this string off at that locations and adds three dots
  19120. as ellipsis. Including the ellipsis, the string will not be longer
  19121. than MAXLENGTH. If finding a good breaking point in the string does
  19122. not work, the string is just chopped off in the middle of a word
  19123. if necessary."
  19124. (if (<= (length s) maxlength)
  19125. s
  19126. (let* ((n (max (- maxlength 4) 1))
  19127. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19128. (if (string-match re s)
  19129. (concat (match-string 1 s) "...")
  19130. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19131. (defun org-get-indentation (&optional line)
  19132. "Get the indentation of the current line, interpreting tabs.
  19133. When LINE is given, assume it represents a line and compute its indentation."
  19134. (if line
  19135. (if (string-match "^ *" (org-remove-tabs line))
  19136. (match-end 0))
  19137. (save-excursion
  19138. (beginning-of-line 1)
  19139. (skip-chars-forward " \t")
  19140. (current-column))))
  19141. (defun org-get-string-indentation (s)
  19142. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19143. (let ((n -1) (i 0) (w tab-width) c)
  19144. (catch 'exit
  19145. (while (< (setq n (1+ n)) (length s))
  19146. (setq c (aref s n))
  19147. (cond ((= c ?\ ) (setq i (1+ i)))
  19148. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19149. (t (throw 'exit t)))))
  19150. i))
  19151. (defun org-remove-tabs (s &optional width)
  19152. "Replace tabulators in S with spaces.
  19153. Assumes that s is a single line, starting in column 0."
  19154. (setq width (or width tab-width))
  19155. (while (string-match "\t" s)
  19156. (setq s (replace-match
  19157. (make-string
  19158. (- (* width (/ (+ (match-beginning 0) width) width))
  19159. (match-beginning 0)) ?\ )
  19160. t t s)))
  19161. s)
  19162. (defun org-fix-indentation (line ind)
  19163. "Fix indentation in LINE.
  19164. IND is a cons cell with target and minimum indentation.
  19165. If the current indentation in LINE is smaller than the minimum,
  19166. leave it alone. If it is larger than ind, set it to the target."
  19167. (let* ((l (org-remove-tabs line))
  19168. (i (org-get-indentation l))
  19169. (i1 (car ind)) (i2 (cdr ind)))
  19170. (if (>= i i2) (setq l (substring line i2)))
  19171. (if (> i1 0)
  19172. (concat (make-string i1 ?\ ) l)
  19173. l)))
  19174. (defun org-remove-indentation (code &optional n)
  19175. "Remove the maximum common indentation from the lines in CODE.
  19176. N may optionally be the number of spaces to remove."
  19177. (with-temp-buffer
  19178. (insert code)
  19179. (org-do-remove-indentation n)
  19180. (buffer-string)))
  19181. (defun org-do-remove-indentation (&optional n)
  19182. "Remove the maximum common indentation from the buffer."
  19183. (untabify (point-min) (point-max))
  19184. (let ((min 10000) re)
  19185. (if n
  19186. (setq min n)
  19187. (goto-char (point-min))
  19188. (while (re-search-forward "^ *[^ \n]" nil t)
  19189. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19190. (unless (or (= min 0) (= min 10000))
  19191. (setq re (format "^ \\{%d\\}" min))
  19192. (goto-char (point-min))
  19193. (while (re-search-forward re nil t)
  19194. (replace-match "")
  19195. (end-of-line 1))
  19196. min)))
  19197. (defun org-fill-template (template alist)
  19198. "Find each %key of ALIST in TEMPLATE and replace it."
  19199. (let ((case-fold-search nil)
  19200. entry key value)
  19201. (setq alist (sort (copy-sequence alist)
  19202. (lambda (a b) (< (length (car a)) (length (car b))))))
  19203. (while (setq entry (pop alist))
  19204. (setq template
  19205. (replace-regexp-in-string
  19206. (concat "%" (regexp-quote (car entry)))
  19207. (or (cdr entry) "") template t t)))
  19208. template))
  19209. (defun org-base-buffer (buffer)
  19210. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19211. (if (not buffer)
  19212. buffer
  19213. (or (buffer-base-buffer buffer)
  19214. buffer)))
  19215. (defun org-trim (s)
  19216. "Remove whitespace at beginning and end of string."
  19217. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19218. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19219. s)
  19220. (defun org-wrap (string &optional width lines)
  19221. "Wrap string to either a number of lines, or a width in characters.
  19222. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19223. that costs. If there is a word longer than WIDTH, the text is actually
  19224. wrapped to the length of that word.
  19225. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19226. many lines, whatever width that takes.
  19227. The return value is a list of lines, without newlines at the end."
  19228. (let* ((words (org-split-string string "[ \t\n]+"))
  19229. (maxword (apply 'max (mapcar 'org-string-width words)))
  19230. w ll)
  19231. (cond (width
  19232. (org-do-wrap words (max maxword width)))
  19233. (lines
  19234. (setq w maxword)
  19235. (setq ll (org-do-wrap words maxword))
  19236. (if (<= (length ll) lines)
  19237. ll
  19238. (setq ll words)
  19239. (while (> (length ll) lines)
  19240. (setq w (1+ w))
  19241. (setq ll (org-do-wrap words w)))
  19242. ll))
  19243. (t (error "Cannot wrap this")))))
  19244. (defun org-do-wrap (words width)
  19245. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19246. (let (lines line)
  19247. (while words
  19248. (setq line (pop words))
  19249. (while (and words (< (+ (length line) (length (car words))) width))
  19250. (setq line (concat line " " (pop words))))
  19251. (setq lines (push line lines)))
  19252. (nreverse lines)))
  19253. (defun org-split-string (string &optional separators)
  19254. "Splits STRING into substrings at SEPARATORS.
  19255. No empty strings are returned if there are matches at the beginning
  19256. and end of string."
  19257. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19258. (start 0)
  19259. notfirst
  19260. (list nil))
  19261. (while (and (string-match rexp string
  19262. (if (and notfirst
  19263. (= start (match-beginning 0))
  19264. (< start (length string)))
  19265. (1+ start) start))
  19266. (< (match-beginning 0) (length string)))
  19267. (setq notfirst t)
  19268. (or (eq (match-beginning 0) 0)
  19269. (and (eq (match-beginning 0) (match-end 0))
  19270. (eq (match-beginning 0) start))
  19271. (setq list
  19272. (cons (substring string start (match-beginning 0))
  19273. list)))
  19274. (setq start (match-end 0)))
  19275. (or (eq start (length string))
  19276. (setq list
  19277. (cons (substring string start)
  19278. list)))
  19279. (nreverse list)))
  19280. (defun org-quote-vert (s)
  19281. "Replace \"|\" with \"\\vert\"."
  19282. (while (string-match "|" s)
  19283. (setq s (replace-match "\\vert" t t s)))
  19284. s)
  19285. (defun org-uuidgen-p (s)
  19286. "Is S an ID created by UUIDGEN?"
  19287. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19288. (defun org-in-src-block-p (&optional inside)
  19289. "Whether point is in a code source block.
  19290. When INSIDE is non-nil, don't consider we are within a src block
  19291. when point is at #+BEGIN_SRC or #+END_SRC."
  19292. (let ((case-fold-search t) ov)
  19293. (or (and (setq ov (overlays-at (point)))
  19294. (memq 'org-block-background
  19295. (overlay-properties (car ov))))
  19296. (and (not inside)
  19297. (save-match-data
  19298. (save-excursion
  19299. (beginning-of-line)
  19300. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19301. (defun org-context ()
  19302. "Return a list of contexts of the current cursor position.
  19303. If several contexts apply, all are returned.
  19304. Each context entry is a list with a symbol naming the context, and
  19305. two positions indicating start and end of the context. Possible
  19306. contexts are:
  19307. :headline anywhere in a headline
  19308. :headline-stars on the leading stars in a headline
  19309. :todo-keyword on a TODO keyword (including DONE) in a headline
  19310. :tags on the TAGS in a headline
  19311. :priority on the priority cookie in a headline
  19312. :item on the first line of a plain list item
  19313. :item-bullet on the bullet/number of a plain list item
  19314. :checkbox on the checkbox in a plain list item
  19315. :table in an org-mode table
  19316. :table-special on a special filed in a table
  19317. :table-table in a table.el table
  19318. :clocktable in a clocktable
  19319. :src-block in a source block
  19320. :link on a hyperlink
  19321. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19322. :target on a <<target>>
  19323. :radio-target on a <<<radio-target>>>
  19324. :latex-fragment on a LaTeX fragment
  19325. :latex-preview on a LaTeX fragment with overlaid preview image
  19326. This function expects the position to be visible because it uses font-lock
  19327. faces as a help to recognize the following contexts: :table-special, :link,
  19328. and :keyword."
  19329. (let* ((f (get-text-property (point) 'face))
  19330. (faces (if (listp f) f (list f)))
  19331. (case-fold-search t)
  19332. (p (point)) clist o)
  19333. ;; First the large context
  19334. (cond
  19335. ((org-at-heading-p t)
  19336. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19337. (when (progn
  19338. (beginning-of-line 1)
  19339. (looking-at org-todo-line-tags-regexp))
  19340. (push (org-point-in-group p 1 :headline-stars) clist)
  19341. (push (org-point-in-group p 2 :todo-keyword) clist)
  19342. (push (org-point-in-group p 4 :tags) clist))
  19343. (goto-char p)
  19344. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19345. (if (looking-at "\\[#[A-Z0-9]\\]")
  19346. (push (org-point-in-group p 0 :priority) clist)))
  19347. ((org-at-item-p)
  19348. (push (org-point-in-group p 2 :item-bullet) clist)
  19349. (push (list :item (point-at-bol)
  19350. (save-excursion (org-end-of-item) (point)))
  19351. clist)
  19352. (and (org-at-item-checkbox-p)
  19353. (push (org-point-in-group p 0 :checkbox) clist)))
  19354. ((org-at-table-p)
  19355. (push (list :table (org-table-begin) (org-table-end)) clist)
  19356. (if (memq 'org-formula faces)
  19357. (push (list :table-special
  19358. (previous-single-property-change p 'face)
  19359. (next-single-property-change p 'face)) clist)))
  19360. ((org-at-table-p 'any)
  19361. (push (list :table-table) clist)))
  19362. (goto-char p)
  19363. (let ((case-fold-search t))
  19364. ;; New the "medium" contexts: clocktables, source blocks
  19365. (cond ((org-in-clocktable-p)
  19366. (push (list :clocktable
  19367. (and (or (looking-at "#\\+BEGIN: clocktable")
  19368. (search-backward "#+BEGIN: clocktable" nil t))
  19369. (match-beginning 0))
  19370. (and (re-search-forward "#\\+END:?" nil t)
  19371. (match-end 0))) clist))
  19372. ((org-in-src-block-p)
  19373. (push (list :src-block
  19374. (and (or (looking-at "#\\+BEGIN_SRC")
  19375. (search-backward "#+BEGIN_SRC" nil t))
  19376. (match-beginning 0))
  19377. (and (search-forward "#+END_SRC" nil t)
  19378. (match-beginning 0))) clist))))
  19379. (goto-char p)
  19380. ;; Now the small context
  19381. (cond
  19382. ((org-at-timestamp-p)
  19383. (push (org-point-in-group p 0 :timestamp) clist))
  19384. ((memq 'org-link faces)
  19385. (push (list :link
  19386. (previous-single-property-change p 'face)
  19387. (next-single-property-change p 'face)) clist))
  19388. ((memq 'org-special-keyword faces)
  19389. (push (list :keyword
  19390. (previous-single-property-change p 'face)
  19391. (next-single-property-change p 'face)) clist))
  19392. ((org-at-target-p)
  19393. (push (org-point-in-group p 0 :target) clist)
  19394. (goto-char (1- (match-beginning 0)))
  19395. (if (looking-at org-radio-target-regexp)
  19396. (push (org-point-in-group p 0 :radio-target) clist))
  19397. (goto-char p))
  19398. ((setq o (car (delq nil
  19399. (mapcar
  19400. (lambda (x)
  19401. (if (memq x org-latex-fragment-image-overlays) x))
  19402. (overlays-at (point))))))
  19403. (push (list :latex-fragment
  19404. (overlay-start o) (overlay-end o)) clist)
  19405. (push (list :latex-preview
  19406. (overlay-start o) (overlay-end o)) clist))
  19407. ((org-inside-LaTeX-fragment-p)
  19408. ;; FIXME: positions wrong.
  19409. (push (list :latex-fragment (point) (point)) clist)))
  19410. (setq clist (nreverse (delq nil clist)))
  19411. clist))
  19412. ;; FIXME: Compare with at-regexp-p Do we need both?
  19413. (defun org-in-regexp (re &optional nlines visually)
  19414. "Check if point is inside a match of regexp.
  19415. Normally only the current line is checked, but you can include NLINES extra
  19416. lines both before and after point into the search.
  19417. If VISUALLY is set, require that the cursor is not after the match but
  19418. really on, so that the block visually is on the match."
  19419. (catch 'exit
  19420. (let ((pos (point))
  19421. (eol (point-at-eol (+ 1 (or nlines 0))))
  19422. (inc (if visually 1 0)))
  19423. (save-excursion
  19424. (beginning-of-line (- 1 (or nlines 0)))
  19425. (while (re-search-forward re eol t)
  19426. (if (and (<= (match-beginning 0) pos)
  19427. (>= (+ inc (match-end 0)) pos))
  19428. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19429. (defun org-at-regexp-p (regexp)
  19430. "Is point inside a match of REGEXP in the current line?"
  19431. (catch 'exit
  19432. (save-excursion
  19433. (let ((pos (point)) (end (point-at-eol)))
  19434. (beginning-of-line 1)
  19435. (while (re-search-forward regexp end t)
  19436. (if (and (<= (match-beginning 0) pos)
  19437. (>= (match-end 0) pos))
  19438. (throw 'exit t)))
  19439. nil))))
  19440. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19441. "Non-nil when point is between matches of START-RE and END-RE.
  19442. Also return a non-nil value when point is on one of the matches.
  19443. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19444. buffer positions. Default values are the positions of headlines
  19445. surrounding the point.
  19446. The functions returns a cons cell whose car (resp. cdr) is the
  19447. position before START-RE (resp. after END-RE)."
  19448. (save-match-data
  19449. (let ((pos (point))
  19450. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19451. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19452. beg end)
  19453. (save-excursion
  19454. ;; Point is on a block when on START-RE or if START-RE can be
  19455. ;; found before it...
  19456. (and (or (org-at-regexp-p start-re)
  19457. (re-search-backward start-re limit-up t))
  19458. (setq beg (match-beginning 0))
  19459. ;; ... and END-RE after it...
  19460. (goto-char (match-end 0))
  19461. (re-search-forward end-re limit-down t)
  19462. (> (setq end (match-end 0)) pos)
  19463. ;; ... without another START-RE in-between.
  19464. (goto-char (match-beginning 0))
  19465. (not (re-search-backward start-re (1+ beg) t))
  19466. ;; Return value.
  19467. (cons beg end))))))
  19468. (defun org-in-block-p (names)
  19469. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19470. NAMES is a list of strings containing names of blocks.
  19471. Return first block name matched, or nil. Beware that in case of
  19472. nested blocks, the returned name may not belong to the closest
  19473. block from point."
  19474. (save-match-data
  19475. (catch 'exit
  19476. (let ((case-fold-search t)
  19477. (lim-up (save-excursion (outline-previous-heading)))
  19478. (lim-down (save-excursion (outline-next-heading))))
  19479. (mapc (lambda (name)
  19480. (let ((n (regexp-quote name)))
  19481. (when (org-between-regexps-p
  19482. (concat "^[ \t]*#\\+begin_" n)
  19483. (concat "^[ \t]*#\\+end_" n)
  19484. lim-up lim-down)
  19485. (throw 'exit n))))
  19486. names))
  19487. nil)))
  19488. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19489. "Call `multi-occur' with buffers for all agenda files."
  19490. (interactive "sOrg-files matching: \np")
  19491. (let* ((files (org-agenda-files))
  19492. (tnames (mapcar 'file-truename files))
  19493. (extra org-agenda-text-search-extra-files)
  19494. f)
  19495. (when (eq (car extra) 'agenda-archives)
  19496. (setq extra (cdr extra))
  19497. (setq files (org-add-archive-files files)))
  19498. (while (setq f (pop extra))
  19499. (unless (member (file-truename f) tnames)
  19500. (add-to-list 'files f 'append)
  19501. (add-to-list 'tnames (file-truename f) 'append)))
  19502. (multi-occur
  19503. (mapcar (lambda (x)
  19504. (with-current-buffer
  19505. (or (get-file-buffer x) (find-file-noselect x))
  19506. (widen)
  19507. (current-buffer)))
  19508. files)
  19509. regexp)))
  19510. (if (boundp 'occur-mode-find-occurrence-hook)
  19511. ;; Emacs 23
  19512. (add-hook 'occur-mode-find-occurrence-hook
  19513. (lambda ()
  19514. (when (derived-mode-p 'org-mode)
  19515. (org-reveal))))
  19516. ;; Emacs 22
  19517. (defadvice occur-mode-goto-occurrence
  19518. (after org-occur-reveal activate)
  19519. (and (derived-mode-p 'org-mode) (org-reveal)))
  19520. (defadvice occur-mode-goto-occurrence-other-window
  19521. (after org-occur-reveal activate)
  19522. (and (derived-mode-p 'org-mode) (org-reveal)))
  19523. (defadvice occur-mode-display-occurrence
  19524. (after org-occur-reveal activate)
  19525. (when (derived-mode-p 'org-mode)
  19526. (let ((pos (occur-mode-find-occurrence)))
  19527. (with-current-buffer (marker-buffer pos)
  19528. (save-excursion
  19529. (goto-char pos)
  19530. (org-reveal)))))))
  19531. (defun org-occur-link-in-agenda-files ()
  19532. "Create a link and search for it in the agendas.
  19533. The link is not stored in `org-stored-links', it is just created
  19534. for the search purpose."
  19535. (interactive)
  19536. (let ((link (condition-case nil
  19537. (org-store-link nil)
  19538. (error "Unable to create a link to here"))))
  19539. (org-occur-in-agenda-files (regexp-quote link))))
  19540. (defun org-reverse-string (string)
  19541. "Return the reverse of STRING."
  19542. (apply 'string (reverse (string-to-list string))))
  19543. (defun org-uniquify (list)
  19544. "Remove duplicate elements from LIST."
  19545. (let (res)
  19546. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19547. res))
  19548. (defun org-delete-all (elts list)
  19549. "Remove all elements in ELTS from LIST."
  19550. (while elts
  19551. (setq list (delete (pop elts) list)))
  19552. list)
  19553. (defun org-count (cl-item cl-seq)
  19554. "Count the number of occurrences of ITEM in SEQ.
  19555. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19556. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19557. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19558. (while (< cl-start cl-end)
  19559. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19560. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19561. (setq cl-start (1+ cl-start)))
  19562. cl-count))
  19563. (defun org-remove-if (predicate seq)
  19564. "Remove everything from SEQ that fulfills PREDICATE."
  19565. (let (res e)
  19566. (while seq
  19567. (setq e (pop seq))
  19568. (if (not (funcall predicate e)) (push e res)))
  19569. (nreverse res)))
  19570. (defun org-remove-if-not (predicate seq)
  19571. "Remove everything from SEQ that does not fulfill PREDICATE."
  19572. (let (res e)
  19573. (while seq
  19574. (setq e (pop seq))
  19575. (if (funcall predicate e) (push e res)))
  19576. (nreverse res)))
  19577. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19578. "Reduce two-argument FUNCTION across SEQ.
  19579. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19580. \":initial-value\" removed."
  19581. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19582. (cadr (memq :initial-value cl-keys)))
  19583. (cl-seq (pop cl-seq))
  19584. (t (funcall cl-func)))))
  19585. (while cl-seq
  19586. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19587. cl-accum))
  19588. (defun org-back-over-empty-lines ()
  19589. "Move backwards over whitespace, to the beginning of the first empty line.
  19590. Returns the number of empty lines passed."
  19591. (let ((pos (point)))
  19592. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19593. (skip-chars-backward " \t\n\r")
  19594. (unless (eobp)
  19595. (forward-line -1)))
  19596. (beginning-of-line 2)
  19597. (goto-char (min (point) pos))
  19598. (count-lines (point) pos)))
  19599. (defun org-skip-whitespace ()
  19600. (skip-chars-forward " \t\n\r"))
  19601. (defun org-point-in-group (point group &optional context)
  19602. "Check if POINT is in match-group GROUP.
  19603. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19604. match. If the match group does not exist or point is not inside it,
  19605. return nil."
  19606. (and (match-beginning group)
  19607. (>= point (match-beginning group))
  19608. (<= point (match-end group))
  19609. (if context
  19610. (list context (match-beginning group) (match-end group))
  19611. t)))
  19612. (defun org-switch-to-buffer-other-window (&rest args)
  19613. "Switch to buffer in a second window on the current frame.
  19614. In particular, do not allow pop-up frames.
  19615. Returns the newly created buffer."
  19616. (org-no-popups
  19617. (apply 'switch-to-buffer-other-window args)))
  19618. (defun org-combine-plists (&rest plists)
  19619. "Create a single property list from all plists in PLISTS.
  19620. The process starts by copying the first list, and then setting properties
  19621. from the other lists. Settings in the last list are the most significant
  19622. ones and overrule settings in the other lists."
  19623. (let ((rtn (copy-sequence (pop plists)))
  19624. p v ls)
  19625. (while plists
  19626. (setq ls (pop plists))
  19627. (while ls
  19628. (setq p (pop ls) v (pop ls))
  19629. (setq rtn (plist-put rtn p v))))
  19630. rtn))
  19631. (defun org-replace-escapes (string table)
  19632. "Replace %-escapes in STRING with values in TABLE.
  19633. TABLE is an association list with keys like \"%a\" and string values.
  19634. The sequences in STRING may contain normal field width and padding information,
  19635. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19636. so values can contain further %-escapes if they are define later in TABLE."
  19637. (let ((tbl (copy-alist table))
  19638. (case-fold-search nil)
  19639. (pchg 0)
  19640. e re rpl)
  19641. (while (setq e (pop tbl))
  19642. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19643. (when (and (cdr e) (string-match re (cdr e)))
  19644. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19645. (safe "SREF"))
  19646. (add-text-properties 0 3 (list 'sref sref) safe)
  19647. (setcdr e (replace-match safe t t (cdr e)))))
  19648. (while (string-match re string)
  19649. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19650. (cdr e)))
  19651. (setq string (replace-match rpl t t string))))
  19652. (while (setq pchg (next-property-change pchg string))
  19653. (let ((sref (get-text-property pchg 'sref string)))
  19654. (when (and sref (string-match "SREF" string pchg))
  19655. (setq string (replace-match sref t t string)))))
  19656. string))
  19657. (defun org-sublist (list start end)
  19658. "Return a section of LIST, from START to END.
  19659. Counting starts at 1."
  19660. (let (rtn (c start))
  19661. (setq list (nthcdr (1- start) list))
  19662. (while (and list (<= c end))
  19663. (push (pop list) rtn)
  19664. (setq c (1+ c)))
  19665. (nreverse rtn)))
  19666. (defun org-find-base-buffer-visiting (file)
  19667. "Like `find-buffer-visiting' but always return the base buffer and
  19668. not an indirect buffer."
  19669. (let ((buf (or (get-file-buffer file)
  19670. (find-buffer-visiting file))))
  19671. (if buf
  19672. (or (buffer-base-buffer buf) buf)
  19673. nil)))
  19674. (defun org-image-file-name-regexp (&optional extensions)
  19675. "Return regexp matching the file names of images.
  19676. If EXTENSIONS is given, only match these."
  19677. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19678. (image-file-name-regexp)
  19679. (let ((image-file-name-extensions
  19680. (or extensions
  19681. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19682. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19683. (concat "\\."
  19684. (regexp-opt (nconc (mapcar 'upcase
  19685. image-file-name-extensions)
  19686. image-file-name-extensions)
  19687. t)
  19688. "\\'"))))
  19689. (defun org-file-image-p (file &optional extensions)
  19690. "Return non-nil if FILE is an image."
  19691. (save-match-data
  19692. (string-match (org-image-file-name-regexp extensions) file)))
  19693. (defun org-get-cursor-date (&optional with-time)
  19694. "Return the date at cursor in as a time.
  19695. This works in the calendar and in the agenda, anywhere else it just
  19696. returns the current time.
  19697. If WITH-TIME is non-nil, returns the time of the event at point (in
  19698. the agenda) or the current time of the day."
  19699. (let (date day defd tp tm hod mod)
  19700. (when with-time
  19701. (setq tp (get-text-property (point) 'time))
  19702. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19703. (setq hod (string-to-number (match-string 1 tp))
  19704. mod (string-to-number (match-string 2 tp))))
  19705. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19706. mod (nth 1 (decode-time (current-time))))))
  19707. (cond
  19708. ((eq major-mode 'calendar-mode)
  19709. (setq date (calendar-cursor-to-date)
  19710. defd (encode-time 0 (or mod 0) (or hod 0)
  19711. (nth 1 date) (nth 0 date) (nth 2 date))))
  19712. ((eq major-mode 'org-agenda-mode)
  19713. (setq day (get-text-property (point) 'day))
  19714. (if day
  19715. (setq date (calendar-gregorian-from-absolute day)
  19716. defd (encode-time 0 (or mod 0) (or hod 0)
  19717. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19718. (or defd (current-time))))
  19719. (defun org-mark-subtree (&optional up)
  19720. "Mark the current subtree.
  19721. This puts point at the start of the current subtree, and mark at
  19722. the end. If a numeric prefix UP is given, move up into the
  19723. hierarchy of headlines by UP levels before marking the subtree."
  19724. (interactive "P")
  19725. (org-with-limited-levels
  19726. (cond ((org-at-heading-p) (beginning-of-line))
  19727. ((org-before-first-heading-p) (error "Not in a subtree"))
  19728. (t (outline-previous-visible-heading 1))))
  19729. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19730. (if (org-called-interactively-p 'any)
  19731. (call-interactively 'org-mark-element)
  19732. (org-mark-element)))
  19733. ;;; Indentation
  19734. (defun org-indent-line ()
  19735. "Indent line depending on context."
  19736. (interactive)
  19737. (let* ((pos (point))
  19738. (itemp (org-at-item-p))
  19739. (case-fold-search t)
  19740. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19741. (inline-task-p (and (featurep 'org-inlinetask)
  19742. (org-inlinetask-in-task-p)))
  19743. (inline-re (and inline-task-p
  19744. (org-inlinetask-outline-regexp)))
  19745. column)
  19746. (if (and orgstruct-is-++ (eq pos (point)))
  19747. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19748. (indent-according-to-mode))
  19749. (beginning-of-line 1)
  19750. (cond
  19751. ;; Headings
  19752. ((looking-at org-outline-regexp) (setq column 0))
  19753. ;; Footnote definition
  19754. ((looking-at org-footnote-definition-re) (setq column 0))
  19755. ;; Literal examples
  19756. ((looking-at "[ \t]*:\\( \\|$\\)")
  19757. (setq column (org-get-indentation))) ; do nothing
  19758. ;; Lists
  19759. ((ignore-errors (goto-char (org-in-item-p)))
  19760. (setq column (if itemp
  19761. (org-get-indentation)
  19762. (org-list-item-body-column (point))))
  19763. (goto-char pos))
  19764. ;; Drawers
  19765. ((and (looking-at "[ \t]*:END:")
  19766. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19767. (save-excursion
  19768. (goto-char (1- (match-beginning 1)))
  19769. (setq column (current-column))))
  19770. ;; Special blocks
  19771. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19772. (save-excursion
  19773. (re-search-backward
  19774. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19775. (setq column (org-get-indentation (match-string 0))))
  19776. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19777. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19778. (save-excursion
  19779. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19780. (setq column
  19781. (cond ((equal (downcase (match-string 1)) "src")
  19782. ;; src blocks: let `org-edit-src-exit' handle them
  19783. (org-get-indentation))
  19784. ((equal (downcase (match-string 1)) "example")
  19785. (max (org-get-indentation)
  19786. (org-get-indentation (match-string 0))))
  19787. (t
  19788. (org-get-indentation (match-string 0))))))
  19789. ;; This line has nothing special, look at the previous relevant
  19790. ;; line to compute indentation
  19791. (t
  19792. (beginning-of-line 0)
  19793. (while (and (not (bobp))
  19794. (not (looking-at org-table-line-regexp))
  19795. (not (looking-at org-drawer-regexp))
  19796. ;; When point started in an inline task, do not move
  19797. ;; above task starting line.
  19798. (not (and inline-task-p (looking-at inline-re)))
  19799. ;; Skip drawers, blocks, empty lines, verbatim,
  19800. ;; comments, tables, footnotes definitions, lists,
  19801. ;; inline tasks.
  19802. (or (and (looking-at "[ \t]*:END:")
  19803. (re-search-backward org-drawer-regexp nil t))
  19804. (and (looking-at "[ \t]*#\\+end_")
  19805. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19806. (looking-at "[ \t]*[\n:#|]")
  19807. (looking-at org-footnote-definition-re)
  19808. (and (ignore-errors (goto-char (org-in-item-p)))
  19809. (goto-char
  19810. (org-list-get-top-point (org-list-struct))))
  19811. (and (not inline-task-p)
  19812. (featurep 'org-inlinetask)
  19813. (org-inlinetask-in-task-p)
  19814. (or (org-inlinetask-goto-beginning) t))))
  19815. (beginning-of-line 0))
  19816. (cond
  19817. ;; There was an heading above.
  19818. ((looking-at "\\*+[ \t]+")
  19819. (if (not org-adapt-indentation)
  19820. (setq column 0)
  19821. (goto-char (match-end 0))
  19822. (setq column (current-column))))
  19823. ;; A drawer had started and is unfinished
  19824. ((looking-at org-drawer-regexp)
  19825. (goto-char (1- (match-beginning 1)))
  19826. (setq column (current-column)))
  19827. ;; Else, nothing noticeable found: get indentation and go on.
  19828. (t (setq column (org-get-indentation))))))
  19829. ;; Now apply indentation and move cursor accordingly
  19830. (goto-char pos)
  19831. (if (<= (current-column) (current-indentation))
  19832. (org-indent-line-to column)
  19833. (save-excursion (org-indent-line-to column)))
  19834. ;; Special polishing for properties, see `org-property-format'
  19835. (setq column (current-column))
  19836. (beginning-of-line 1)
  19837. (if (looking-at
  19838. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19839. (replace-match (concat (match-string 1)
  19840. (format org-property-format
  19841. (match-string 2) (match-string 3)))
  19842. t t))
  19843. (org-move-to-column column))))
  19844. (defun org-indent-drawer ()
  19845. "Indent the drawer at point."
  19846. (interactive)
  19847. (let ((p (point))
  19848. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19849. (match-end 0)))
  19850. (folded
  19851. (save-excursion
  19852. (end-of-line)
  19853. (when (overlays-at (point))
  19854. (member 'invisible (overlay-properties
  19855. (car (overlays-at (point)))))))))
  19856. (when folded (org-cycle))
  19857. (indent-for-tab-command)
  19858. (while (and (move-beginning-of-line 2) (< (point) e))
  19859. (indent-for-tab-command))
  19860. (goto-char p)
  19861. (when folded (org-cycle)))
  19862. (message "Drawer at point indented"))
  19863. (defun org-indent-block ()
  19864. "Indent the block at point."
  19865. (interactive)
  19866. (let ((p (point))
  19867. (case-fold-search t)
  19868. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19869. (match-end 0)))
  19870. (folded
  19871. (save-excursion
  19872. (end-of-line)
  19873. (when (overlays-at (point))
  19874. (member 'invisible (overlay-properties
  19875. (car (overlays-at (point)))))))))
  19876. (when folded (org-cycle))
  19877. (indent-for-tab-command)
  19878. (while (and (move-beginning-of-line 2) (< (point) e))
  19879. (indent-for-tab-command))
  19880. (goto-char p)
  19881. (when folded (org-cycle)))
  19882. (message "Block at point indented"))
  19883. (defun org-indent-region (start end)
  19884. "Indent region."
  19885. (interactive "r")
  19886. (save-excursion
  19887. (let ((line-end (org-current-line end)))
  19888. (goto-char start)
  19889. (while (< (org-current-line) line-end)
  19890. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19891. (t (call-interactively 'org-indent-line)))
  19892. (move-beginning-of-line 2)))))
  19893. ;;; Filling
  19894. ;; We use our own fill-paragraph and auto-fill functions.
  19895. ;; `org-fill-paragraph' relies on adaptive filling and context
  19896. ;; checking. Appropriate `fill-prefix' is computed with
  19897. ;; `org-adaptive-fill-function'.
  19898. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19899. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19900. ;; `org-adaptive-fill-function' value. Internally,
  19901. ;; `org-comment-line-break-function' breaks the line.
  19902. ;; `org-setup-filling' installs filling and auto-filling related
  19903. ;; variables during `org-mode' initialization.
  19904. (defun org-setup-filling ()
  19905. (interactive)
  19906. ;; Prevent auto-fill from inserting unwanted new items.
  19907. (when (boundp 'fill-nobreak-predicate)
  19908. (org-set-local
  19909. 'fill-nobreak-predicate
  19910. (org-uniquify
  19911. (append fill-nobreak-predicate
  19912. '(org-fill-paragraph-separate-nobreak-p
  19913. org-fill-line-break-nobreak-p
  19914. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19915. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19916. (org-set-local 'auto-fill-inhibit-regexp nil)
  19917. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19918. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19919. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19920. (defvar org-element-paragraph-separate) ; org-element.el
  19921. (defun org-fill-paragraph-separate-nobreak-p ()
  19922. "Non-nil when a line break at point would insert a new item."
  19923. (looking-at (substring org-element-paragraph-separate 1)))
  19924. (defun org-fill-line-break-nobreak-p ()
  19925. "Non-nil when a line break at point would create an Org line break."
  19926. (save-excursion
  19927. (skip-chars-backward "[ \t]")
  19928. (skip-chars-backward "\\\\")
  19929. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19930. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  19931. "Non-nil when a line break at point would insert a new item."
  19932. (and (org-at-timestamp-p t)
  19933. (not (looking-at org-ts-regexp-both))))
  19934. (declare-function message-in-body-p "message" ())
  19935. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19936. (defun org-adaptive-fill-function ()
  19937. "Compute a fill prefix for the current line.
  19938. Return fill prefix, as a string, or nil if current line isn't
  19939. meant to be filled."
  19940. (let (prefix)
  19941. (catch 'exit
  19942. (when (derived-mode-p 'message-mode)
  19943. (save-excursion
  19944. (beginning-of-line)
  19945. (cond ((or (not (message-in-body-p))
  19946. (looking-at orgtbl-line-start-regexp))
  19947. (throw 'exit nil))
  19948. ((looking-at message-cite-prefix-regexp)
  19949. (throw 'exit (match-string-no-properties 0)))
  19950. ((looking-at org-outline-regexp)
  19951. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19952. (org-with-wide-buffer
  19953. (let* ((p (line-beginning-position))
  19954. (element (save-excursion (beginning-of-line)
  19955. (org-element-at-point)))
  19956. (type (org-element-type element))
  19957. (post-affiliated (org-element-property :post-affiliated element)))
  19958. (unless (and post-affiliated (< p post-affiliated))
  19959. (case type
  19960. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19961. (footnote-definition "")
  19962. ((item plain-list)
  19963. (make-string (org-list-item-body-column
  19964. (or post-affiliated
  19965. (org-element-property :begin element)))
  19966. ? ))
  19967. (paragraph
  19968. ;; Fill prefix is usually the same as the current line,
  19969. ;; except if the paragraph is at the beginning of an item.
  19970. (let ((parent (org-element-property :parent element)))
  19971. (cond ((eq (org-element-type parent) 'item)
  19972. (make-string (org-list-item-body-column
  19973. (org-element-property :begin parent))
  19974. ? ))
  19975. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19976. (match-string 0))
  19977. (t ""))))
  19978. (comment-block
  19979. ;; Only fill contents if P is within block boundaries.
  19980. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19981. (forward-line)
  19982. (point)))
  19983. (cend (save-excursion
  19984. (goto-char (org-element-property :end element))
  19985. (skip-chars-backward " \r\t\n")
  19986. (line-beginning-position))))
  19987. (when (and (>= p cbeg) (< p cend))
  19988. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19989. (match-string 0)
  19990. "")))))))))))
  19991. (declare-function message-goto-body "message" ())
  19992. (defvar message-cite-prefix-regexp) ; From message.el
  19993. (defvar org-element-all-successors) ; From org-element.el
  19994. (defun org-fill-paragraph (&optional justify)
  19995. "Fill element at point, when applicable.
  19996. This function only applies to comment blocks, comments, example
  19997. blocks and paragraphs. Also, as a special case, re-align table
  19998. when point is at one.
  19999. If JUSTIFY is non-nil (interactively, with prefix argument),
  20000. justify as well. If `sentence-end-double-space' is non-nil, then
  20001. period followed by one space does not end a sentence, so don't
  20002. break a line there. The variable `fill-column' controls the
  20003. width for filling.
  20004. For convenience, when point is at a plain list, an item or
  20005. a footnote definition, try to fill the first paragraph within."
  20006. (interactive)
  20007. (if (and (derived-mode-p 'message-mode)
  20008. (or (not (message-in-body-p))
  20009. (save-excursion (move-beginning-of-line 1)
  20010. (looking-at message-cite-prefix-regexp))))
  20011. ;; First ensure filling is correct in message-mode.
  20012. (let ((fill-paragraph-function
  20013. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20014. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20015. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20016. (paragraph-separate
  20017. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20018. (fill-paragraph nil))
  20019. (with-syntax-table org-mode-transpose-word-syntax-table
  20020. ;; Move to end of line in order to get the first paragraph
  20021. ;; within a plain list or a footnote definition.
  20022. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20023. ;; First check if point is in a blank line at the beginning of
  20024. ;; the buffer. In that case, ignore filling.
  20025. (case (org-element-type element)
  20026. ;; Use major mode filling function is src blocks.
  20027. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20028. ;; Align Org tables, leave table.el tables as-is.
  20029. (table-row (org-table-align) t)
  20030. (table
  20031. (when (eq (org-element-property :type element) 'org)
  20032. (org-table-align))
  20033. t)
  20034. (paragraph
  20035. ;; Paragraphs may contain `line-break' type objects.
  20036. (let ((beg (max (point-min)
  20037. (org-element-property :contents-begin element)))
  20038. (end (min (point-max)
  20039. (org-element-property :contents-end element))))
  20040. ;; Do nothing if point is at an affiliated keyword.
  20041. (if (< (line-end-position) beg) t
  20042. (when (derived-mode-p 'message-mode)
  20043. ;; In `message-mode', do not fill following citation
  20044. ;; in current paragraph nor text before message body.
  20045. (let ((body-start (save-excursion (message-goto-body))))
  20046. (when body-start (setq beg (max body-start beg))))
  20047. (when (save-excursion
  20048. (re-search-forward
  20049. (concat "^" message-cite-prefix-regexp) end t))
  20050. (setq end (match-beginning 0))))
  20051. ;; Fill paragraph, taking line breaks into account.
  20052. ;; For that, slice the paragraph using line breaks as
  20053. ;; separators, and fill the parts in reverse order to
  20054. ;; avoid messing with markers.
  20055. (save-excursion
  20056. (goto-char end)
  20057. (mapc
  20058. (lambda (pos)
  20059. (fill-region-as-paragraph pos (point) justify)
  20060. (goto-char pos))
  20061. ;; Find the list of ending positions for line breaks
  20062. ;; in the current paragraph. Add paragraph
  20063. ;; beginning to include first slice.
  20064. (nreverse
  20065. (cons beg
  20066. (org-element-map
  20067. (org-element--parse-objects
  20068. beg end nil org-element-all-successors)
  20069. 'line-break
  20070. (lambda (lb) (org-element-property :end lb)))))))
  20071. t)))
  20072. ;; Contents of `comment-block' type elements should be
  20073. ;; filled as plain text, but only if point is within block
  20074. ;; markers.
  20075. (comment-block
  20076. (let* ((case-fold-search t)
  20077. (beg (save-excursion
  20078. (goto-char (org-element-property :begin element))
  20079. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20080. (forward-line)
  20081. (point)))
  20082. (end (save-excursion
  20083. (goto-char (org-element-property :end element))
  20084. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20085. (line-beginning-position))))
  20086. (when (and (>= (point) beg) (< (point) end))
  20087. (fill-region-as-paragraph
  20088. (save-excursion
  20089. (end-of-line)
  20090. (re-search-backward "^[ \t]*$" beg 'move)
  20091. (line-beginning-position))
  20092. (save-excursion
  20093. (beginning-of-line)
  20094. (re-search-forward "^[ \t]*$" end 'move)
  20095. (line-beginning-position))
  20096. justify)))
  20097. t)
  20098. ;; Fill comments.
  20099. (comment (fill-comment-paragraph justify))
  20100. ;; Ignore every other element.
  20101. (otherwise t))))))
  20102. (defun org-auto-fill-function ()
  20103. "Auto-fill function."
  20104. ;; Check if auto-filling is meaningful.
  20105. (let ((fc (current-fill-column)))
  20106. (when (and fc (> (current-column) fc))
  20107. (let* ((fill-prefix (org-adaptive-fill-function))
  20108. ;; Enforce empty fill prefix, if required. Otherwise, it
  20109. ;; will be computed again.
  20110. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20111. (when fill-prefix (do-auto-fill))))))
  20112. (defun org-comment-line-break-function (&optional soft)
  20113. "Break line at point and indent, continuing comment if within one.
  20114. The inserted newline is marked hard if variable
  20115. `use-hard-newlines' is true, unless optional argument SOFT is
  20116. non-nil."
  20117. (if soft (insert-and-inherit ?\n) (newline 1))
  20118. (save-excursion (forward-char -1) (delete-horizontal-space))
  20119. (delete-horizontal-space)
  20120. (indent-to-left-margin)
  20121. (insert-before-markers-and-inherit fill-prefix))
  20122. ;;; Comments
  20123. ;; Org comments syntax is quite complex. It requires the entire line
  20124. ;; to be just a comment. Also, even with the right syntax at the
  20125. ;; beginning of line, some some elements (i.e. verse-block or
  20126. ;; example-block) don't accept comments. Usual Emacs comment commands
  20127. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20128. ;; Org still relies on `comment-dwim', but cannot trust
  20129. ;; `comment-only-p'. So, `comment-region-function' and
  20130. ;; `uncomment-region-function' both point
  20131. ;; to`org-comment-or-uncomment-region'. Eventually,
  20132. ;; `org-insert-comment' takes care of insertion of comments at the
  20133. ;; beginning of line.
  20134. ;; `org-setup-comments-handling' install comments related variables
  20135. ;; during `org-mode' initialization.
  20136. (defun org-setup-comments-handling ()
  20137. (interactive)
  20138. (org-set-local 'comment-use-syntax nil)
  20139. (org-set-local 'comment-start "# ")
  20140. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20141. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20142. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20143. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20144. (defun org-insert-comment ()
  20145. "Insert an empty comment above current line.
  20146. If the line is empty, insert comment at its beginning."
  20147. (beginning-of-line)
  20148. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20149. (org-indent-line)
  20150. (insert "# "))
  20151. (defvar comment-empty-lines) ; From newcomment.el.
  20152. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20153. "Comment or uncomment each non-blank line in the region.
  20154. Uncomment each non-blank line between BEG and END if it only
  20155. contains commented lines. Otherwise, comment them."
  20156. (save-restriction
  20157. ;; Restrict region
  20158. (narrow-to-region (save-excursion (goto-char beg)
  20159. (skip-chars-forward " \r\t\n" end)
  20160. (line-beginning-position))
  20161. (save-excursion (goto-char end)
  20162. (skip-chars-backward " \r\t\n" beg)
  20163. (line-end-position)))
  20164. (let ((uncommentp
  20165. ;; UNCOMMENTP is non-nil when every non blank line between
  20166. ;; BEG and END is a comment.
  20167. (save-excursion
  20168. (goto-char (point-min))
  20169. (while (and (not (eobp))
  20170. (let ((element (org-element-at-point)))
  20171. (and (eq (org-element-type element) 'comment)
  20172. (goto-char (min (point-max)
  20173. (org-element-property
  20174. :end element)))))))
  20175. (eobp))))
  20176. (if uncommentp
  20177. ;; Only blank lines and comments in region: uncomment it.
  20178. (save-excursion
  20179. (goto-char (point-min))
  20180. (while (not (eobp))
  20181. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20182. (replace-match "" nil nil nil 1))
  20183. (forward-line)))
  20184. ;; Comment each line in region.
  20185. (let ((min-indent (point-max)))
  20186. ;; First find the minimum indentation across all lines.
  20187. (save-excursion
  20188. (goto-char (point-min))
  20189. (while (and (not (eobp)) (not (zerop min-indent)))
  20190. (unless (looking-at "[ \t]*$")
  20191. (setq min-indent (min min-indent (current-indentation))))
  20192. (forward-line)))
  20193. ;; Then loop over all lines.
  20194. (save-excursion
  20195. (goto-char (point-min))
  20196. (while (not (eobp))
  20197. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20198. (org-move-to-column min-indent t)
  20199. (insert comment-start))
  20200. (forward-line))))))))
  20201. ;;; Planning
  20202. ;; This section contains tools to operate on timestamp objects, as
  20203. ;; returned by, e.g. `org-element-context'.
  20204. (defun org-timestamp-has-time-p (timestamp)
  20205. "Non-nil when TIMESTAMP has a time specified."
  20206. (org-element-property :hour-start timestamp))
  20207. (defun org-timestamp-format (timestamp format &optional end utc)
  20208. "Format a TIMESTAMP element into a string.
  20209. FORMAT is a format specifier to be passed to
  20210. `format-time-string'.
  20211. When optional argument END is non-nil, use end of date-range or
  20212. time-range, if possible.
  20213. When optional argument UTC is non-nil, time will be expressed as
  20214. Universal Time."
  20215. (format-time-string
  20216. format
  20217. (apply 'encode-time
  20218. (cons 0
  20219. (mapcar
  20220. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20221. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20222. '(:minute-start :hour-start :day-start :month-start
  20223. :year-start)))))
  20224. utc))
  20225. (defun org-timestamp-split-range (timestamp &optional end)
  20226. "Extract a timestamp object from a date or time range.
  20227. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20228. the end of the range. Otherwise, extract its start.
  20229. Return a new timestamp object sharing the same parent as
  20230. TIMESTAMP."
  20231. (let ((type (org-element-property :type timestamp)))
  20232. (if (memq type '(active inactive diary)) timestamp
  20233. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20234. ;; Set new type.
  20235. (org-element-put-property
  20236. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20237. ;; Copy start properties over end properties if END is
  20238. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20239. (let ((p-alist '((:minute-start . :minute-end)
  20240. (:hour-start . :hour-end)
  20241. (:day-start . :day-end)
  20242. (:month-start . :month-end)
  20243. (:year-start . :year-end))))
  20244. (dolist (p-cell p-alist)
  20245. (org-element-put-property
  20246. split-ts
  20247. (funcall (if end 'car 'cdr) p-cell)
  20248. (org-element-property
  20249. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20250. ;; Eventually refresh `:raw-value'.
  20251. (org-element-put-property split-ts :raw-value nil)
  20252. (org-element-put-property
  20253. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20254. (defun org-timestamp-translate (timestamp &optional boundary)
  20255. "Apply `org-translate-time' on a TIMESTAMP object.
  20256. When optional argument BOUNDARY is non-nil, it is either the
  20257. symbol `start' or `end'. In this case, only translate the
  20258. starting or ending part of TIMESTAMP if it is a date or time
  20259. range. Otherwise, translate both parts."
  20260. (if (and (not boundary)
  20261. (memq (org-element-property :type timestamp)
  20262. '(active-range inactive-range)))
  20263. (concat
  20264. (org-translate-time
  20265. (org-element-property :raw-value
  20266. (org-timestamp-split-range timestamp)))
  20267. "--"
  20268. (org-translate-time
  20269. (org-element-property :raw-value
  20270. (org-timestamp-split-range timestamp t))))
  20271. (org-translate-time
  20272. (org-element-property
  20273. :raw-value
  20274. (if (not boundary) timestamp
  20275. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20276. ;;; Other stuff.
  20277. (defun org-toggle-fixed-width-section (arg)
  20278. "Toggle the fixed-width export.
  20279. If there is no active region, the QUOTE keyword at the current headline is
  20280. inserted or removed. When present, it causes the text between this headline
  20281. and the next to be exported as fixed-width text, and unmodified.
  20282. If there is an active region, this command adds or removes a colon as the
  20283. first character of this line. If the first character of a line is a colon,
  20284. this line is also exported in fixed-width font."
  20285. (interactive "P")
  20286. (let* ((cc 0)
  20287. (regionp (org-region-active-p))
  20288. (beg (if regionp (region-beginning) (point)))
  20289. (end (if regionp (region-end)))
  20290. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20291. (case-fold-search nil)
  20292. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20293. off)
  20294. (if regionp
  20295. (save-excursion
  20296. (goto-char beg)
  20297. (setq cc (current-column))
  20298. (beginning-of-line 1)
  20299. (setq off (looking-at re))
  20300. (while (> nlines 0)
  20301. (setq nlines (1- nlines))
  20302. (beginning-of-line 1)
  20303. (cond
  20304. (arg
  20305. (org-move-to-column cc t)
  20306. (insert ": \n")
  20307. (forward-line -1))
  20308. ((and off (looking-at re))
  20309. (replace-match "" t t nil 1))
  20310. ((not off) (org-move-to-column cc t) (insert ": ")))
  20311. (forward-line 1)))
  20312. (save-excursion
  20313. (org-back-to-heading)
  20314. (cond
  20315. ((looking-at (format org-heading-keyword-regexp-format
  20316. org-quote-string))
  20317. (goto-char (match-end 1))
  20318. (looking-at (concat " +" org-quote-string))
  20319. (replace-match "" t t)
  20320. (when (eolp) (insert " ")))
  20321. ((looking-at org-outline-regexp)
  20322. (goto-char (match-end 0))
  20323. (insert org-quote-string " ")))))))
  20324. (defun org-reftex-citation ()
  20325. "Use reftex-citation to insert a citation into the buffer.
  20326. This looks for a line like
  20327. #+BIBLIOGRAPHY: foo plain option:-d
  20328. and derives from it that foo.bib is the bibliography file relevant
  20329. for this document. It then installs the necessary environment for RefTeX
  20330. to work in this buffer and calls `reftex-citation' to insert a citation
  20331. into the buffer.
  20332. Export of such citations to both LaTeX and HTML is handled by the contributed
  20333. package org-exp-bibtex by Taru Karttunen."
  20334. (interactive)
  20335. (let ((reftex-docstruct-symbol 'rds)
  20336. (reftex-cite-format "\\cite{%l}")
  20337. rds bib)
  20338. (save-excursion
  20339. (save-restriction
  20340. (widen)
  20341. (let ((case-fold-search t)
  20342. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20343. (if (not (save-excursion
  20344. (or (re-search-forward re nil t)
  20345. (re-search-backward re nil t))))
  20346. (error "No bibliography defined in file")
  20347. (setq bib (concat (match-string 1) ".bib")
  20348. rds (list (list 'bib bib)))))))
  20349. (call-interactively 'reftex-citation)))
  20350. ;;;; Functions extending outline functionality
  20351. (defun org-beginning-of-line (&optional arg)
  20352. "Go to the beginning of the current line. If that is invisible, continue
  20353. to a visible line beginning. This makes the function of C-a more intuitive.
  20354. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20355. first attempt, and only move to after the tags when the cursor is already
  20356. beyond the end of the headline."
  20357. (interactive "P")
  20358. (let ((pos (point))
  20359. (special (if (consp org-special-ctrl-a/e)
  20360. (car org-special-ctrl-a/e)
  20361. org-special-ctrl-a/e))
  20362. refpos)
  20363. (if (org-bound-and-true-p visual-line-mode)
  20364. (beginning-of-visual-line 1)
  20365. (beginning-of-line 1))
  20366. (if (and arg (fboundp 'move-beginning-of-line))
  20367. (call-interactively 'move-beginning-of-line)
  20368. (if (bobp)
  20369. nil
  20370. (backward-char 1)
  20371. (if (org-truely-invisible-p)
  20372. (while (and (not (bobp)) (org-truely-invisible-p))
  20373. (backward-char 1)
  20374. (beginning-of-line 1))
  20375. (forward-char 1))))
  20376. (when special
  20377. (cond
  20378. ((and (looking-at org-complex-heading-regexp)
  20379. (= (char-after (match-end 1)) ?\ ))
  20380. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20381. (point-at-eol)))
  20382. (goto-char
  20383. (if (eq special t)
  20384. (cond ((> pos refpos) refpos)
  20385. ((= pos (point)) refpos)
  20386. (t (point)))
  20387. (cond ((> pos (point)) (point))
  20388. ((not (eq last-command this-command)) (point))
  20389. (t refpos)))))
  20390. ((org-at-item-p)
  20391. ;; Being at an item and not looking at an the item means point
  20392. ;; was previously moved to beginning of a visual line, which
  20393. ;; doesn't contain the item. Therefore, do nothing special,
  20394. ;; just stay here.
  20395. (when (looking-at org-list-full-item-re)
  20396. ;; Set special position at first white space character after
  20397. ;; bullet, and check-box, if any.
  20398. (let ((after-bullet
  20399. (let ((box (match-end 3)))
  20400. (if (not box) (match-end 1)
  20401. (let ((after (char-after box)))
  20402. (if (and after (= after ? )) (1+ box) box))))))
  20403. ;; Special case: Move point to special position when
  20404. ;; currently after it or at beginning of line.
  20405. (if (eq special t)
  20406. (when (or (> pos after-bullet) (= (point) pos))
  20407. (goto-char after-bullet))
  20408. ;; Reversed case: Move point to special position when
  20409. ;; point was already at beginning of line and command is
  20410. ;; repeated.
  20411. (when (and (= (point) pos) (eq last-command this-command))
  20412. (goto-char after-bullet))))))))
  20413. (org-no-warnings
  20414. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20415. (defun org-end-of-line (&optional arg)
  20416. "Go to the end of the line.
  20417. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20418. tags on the first attempt, and only move to after the tags when
  20419. the cursor is already beyond the end of the headline."
  20420. (interactive "P")
  20421. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20422. org-special-ctrl-a/e))
  20423. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20424. 'end-of-visual-line)
  20425. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20426. (t 'end-of-line))))
  20427. (if (or (not special) arg) (call-interactively move-fun)
  20428. (let* ((element (save-excursion (beginning-of-line)
  20429. (org-element-at-point)))
  20430. (type (org-element-type element)))
  20431. (cond
  20432. ((memq type '(headline inlinetask))
  20433. (let ((pos (point)))
  20434. (beginning-of-line 1)
  20435. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20436. (if (eq special t)
  20437. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20438. (goto-char (match-beginning 1))
  20439. (goto-char (match-end 0)))
  20440. (if (or (< pos (match-end 0))
  20441. (not (eq this-command last-command)))
  20442. (goto-char (match-end 0))
  20443. (goto-char (match-beginning 1))))
  20444. (call-interactively move-fun))))
  20445. ((org-element-property :hiddenp element)
  20446. ;; If element is hidden, `move-end-of-line' would put point
  20447. ;; after it. Use `end-of-line' to stay on current line.
  20448. (call-interactively 'end-of-line))
  20449. (t (call-interactively move-fun)))))
  20450. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20451. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20452. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20453. (defun org-backward-sentence (&optional arg)
  20454. "Go to beginning of sentence, or beginning of table field.
  20455. This will call `backward-sentence' or `org-table-beginning-of-field',
  20456. depending on context."
  20457. (interactive "P")
  20458. (cond
  20459. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20460. (t (call-interactively 'backward-sentence))))
  20461. (defun org-forward-sentence (&optional arg)
  20462. "Go to end of sentence, or end of table field.
  20463. This will call `forward-sentence' or `org-table-end-of-field',
  20464. depending on context."
  20465. (interactive "P")
  20466. (cond
  20467. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20468. (t (call-interactively 'forward-sentence))))
  20469. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20470. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20471. (defun org-kill-line (&optional arg)
  20472. "Kill line, to tags or end of line."
  20473. (interactive "P")
  20474. (cond
  20475. ((or (not org-special-ctrl-k)
  20476. (bolp)
  20477. (not (org-at-heading-p)))
  20478. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20479. org-ctrl-k-protect-subtree)
  20480. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20481. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20482. (error "C-k aborted - would kill hidden subtree")))
  20483. (call-interactively
  20484. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20485. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20486. (kill-region (point) (match-beginning 1))
  20487. (org-set-tags nil t))
  20488. (t (kill-region (point) (point-at-eol)))))
  20489. (define-key org-mode-map "\C-k" 'org-kill-line)
  20490. (defun org-yank (&optional arg)
  20491. "Yank. If the kill is a subtree, treat it specially.
  20492. This command will look at the current kill and check if is a single
  20493. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20494. cursor is at the beginning of a line or after the stars of a currently
  20495. empty headline, then the yank is handled specially. How exactly depends
  20496. on the value of the following variables, both set by default.
  20497. org-yank-folded-subtrees
  20498. When set, the subtree(s) will be folded after insertion, but only
  20499. if doing so would now swallow text after the yanked text.
  20500. org-yank-adjusted-subtrees
  20501. When set, the subtree will be promoted or demoted in order to
  20502. fit into the local outline tree structure, which means that the level
  20503. will be adjusted so that it becomes the smaller one of the two
  20504. *visible* surrounding headings.
  20505. Any prefix to this command will cause `yank' to be called directly with
  20506. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20507. will just
  20508. plainly yank the text as it is.
  20509. \[1] The test checks if the first non-white line is a heading
  20510. and if there are no other headings with fewer stars."
  20511. (interactive "P")
  20512. (org-yank-generic 'yank arg))
  20513. (defun org-yank-generic (command arg)
  20514. "Perform some yank-like command.
  20515. This function implements the behavior described in the `org-yank'
  20516. documentation. However, it has been generalized to work for any
  20517. interactive command with similar behavior."
  20518. ;; pretend to be command COMMAND
  20519. (setq this-command command)
  20520. (if arg
  20521. (call-interactively command)
  20522. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20523. (and (org-kill-is-subtree-p)
  20524. (or (bolp)
  20525. (and (looking-at "[ \t]*$")
  20526. (string-match
  20527. "\\`\\*+\\'"
  20528. (buffer-substring (point-at-bol) (point)))))))
  20529. swallowp)
  20530. (cond
  20531. ((and subtreep org-yank-folded-subtrees)
  20532. (let ((beg (point))
  20533. end)
  20534. (if (and subtreep org-yank-adjusted-subtrees)
  20535. (org-paste-subtree nil nil 'for-yank)
  20536. (call-interactively command))
  20537. (setq end (point))
  20538. (goto-char beg)
  20539. (when (and (bolp) subtreep
  20540. (not (setq swallowp
  20541. (org-yank-folding-would-swallow-text beg end))))
  20542. (org-with-limited-levels
  20543. (or (looking-at org-outline-regexp)
  20544. (re-search-forward org-outline-regexp-bol end t))
  20545. (while (and (< (point) end) (looking-at org-outline-regexp))
  20546. (hide-subtree)
  20547. (org-cycle-show-empty-lines 'folded)
  20548. (condition-case nil
  20549. (outline-forward-same-level 1)
  20550. (error (goto-char end))))))
  20551. (when swallowp
  20552. (message
  20553. "Inserted text not folded because that would swallow text"))
  20554. (goto-char end)
  20555. (skip-chars-forward " \t\n\r")
  20556. (beginning-of-line 1)
  20557. (push-mark beg 'nomsg)))
  20558. ((and subtreep org-yank-adjusted-subtrees)
  20559. (let ((beg (point-at-bol)))
  20560. (org-paste-subtree nil nil 'for-yank)
  20561. (push-mark beg 'nomsg)))
  20562. (t
  20563. (call-interactively command))))))
  20564. (defun org-yank-folding-would-swallow-text (beg end)
  20565. "Would hide-subtree at BEG swallow any text after END?"
  20566. (let (level)
  20567. (org-with-limited-levels
  20568. (save-excursion
  20569. (goto-char beg)
  20570. (when (or (looking-at org-outline-regexp)
  20571. (re-search-forward org-outline-regexp-bol end t))
  20572. (setq level (org-outline-level)))
  20573. (goto-char end)
  20574. (skip-chars-forward " \t\r\n\v\f")
  20575. (if (or (eobp)
  20576. (and (bolp) (looking-at org-outline-regexp)
  20577. (<= (org-outline-level) level)))
  20578. nil ; Nothing would be swallowed
  20579. t))))) ; something would swallow
  20580. (define-key org-mode-map "\C-y" 'org-yank)
  20581. (defun org-truely-invisible-p ()
  20582. "Check if point is at a character currently not visible.
  20583. This version does not only check the character property, but also
  20584. `visible-mode'."
  20585. ;; Early versions of noutline don't have `outline-invisible-p'.
  20586. (if (org-bound-and-true-p visible-mode)
  20587. nil
  20588. (outline-invisible-p)))
  20589. (defun org-invisible-p2 ()
  20590. "Check if point is at a character currently not visible."
  20591. (save-excursion
  20592. (if (and (eolp) (not (bobp))) (backward-char 1))
  20593. ;; Early versions of noutline don't have `outline-invisible-p'.
  20594. (outline-invisible-p)))
  20595. (defun org-back-to-heading (&optional invisible-ok)
  20596. "Call `outline-back-to-heading', but provide a better error message."
  20597. (condition-case nil
  20598. (outline-back-to-heading invisible-ok)
  20599. (error (error "Before first headline at position %d in buffer %s"
  20600. (point) (current-buffer)))))
  20601. (defun org-before-first-heading-p ()
  20602. "Before first heading?"
  20603. (save-excursion
  20604. (end-of-line)
  20605. (null (re-search-backward org-outline-regexp-bol nil t))))
  20606. (defun org-at-heading-p (&optional ignored)
  20607. (outline-on-heading-p t))
  20608. ;; Compatibility alias with Org versions < 7.8.03
  20609. (defalias 'org-on-heading-p 'org-at-heading-p)
  20610. (defun org-at-comment-p nil
  20611. "Is cursor in a line starting with a # character?"
  20612. (save-excursion
  20613. (beginning-of-line)
  20614. (looking-at "^#")))
  20615. (defun org-at-drawer-p nil
  20616. "Is cursor at a drawer keyword?"
  20617. (save-excursion
  20618. (move-beginning-of-line 1)
  20619. (looking-at org-drawer-regexp)))
  20620. (defun org-at-block-p nil
  20621. "Is cursor at a block keyword?"
  20622. (save-excursion
  20623. (move-beginning-of-line 1)
  20624. (looking-at org-block-regexp)))
  20625. (defun org-point-at-end-of-empty-headline ()
  20626. "If point is at the end of an empty headline, return t, else nil.
  20627. If the heading only contains a TODO keyword, it is still still considered
  20628. empty."
  20629. (and (looking-at "[ \t]*$")
  20630. (when org-todo-line-regexp
  20631. (save-excursion
  20632. (beginning-of-line 1)
  20633. (let ((case-fold-search nil))
  20634. (looking-at org-todo-line-regexp)
  20635. (string= (match-string 3) ""))))))
  20636. (defun org-at-heading-or-item-p ()
  20637. (or (org-at-heading-p) (org-at-item-p)))
  20638. (defun org-at-target-p ()
  20639. (or (org-in-regexp org-radio-target-regexp)
  20640. (org-in-regexp org-target-regexp)))
  20641. ;; Compatibility alias with Org versions < 7.8.03
  20642. (defalias 'org-on-target-p 'org-at-target-p)
  20643. (defun org-up-heading-all (arg)
  20644. "Move to the heading line of which the present line is a subheading.
  20645. This function considers both visible and invisible heading lines.
  20646. With argument, move up ARG levels."
  20647. (if (fboundp 'outline-up-heading-all)
  20648. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20649. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20650. (defun org-up-heading-safe ()
  20651. "Move to the heading line of which the present line is a subheading.
  20652. This version will not throw an error. It will return the level of the
  20653. headline found, or nil if no higher level is found.
  20654. Also, this function will be a lot faster than `outline-up-heading',
  20655. because it relies on stars being the outline starters. This can really
  20656. make a significant difference in outlines with very many siblings."
  20657. (let (start-level re)
  20658. (org-back-to-heading t)
  20659. (setq start-level (funcall outline-level))
  20660. (if (equal start-level 1)
  20661. nil
  20662. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20663. (if (re-search-backward re nil t)
  20664. (funcall outline-level)))))
  20665. (defun org-first-sibling-p ()
  20666. "Is this heading the first child of its parents?"
  20667. (interactive)
  20668. (let ((re org-outline-regexp-bol)
  20669. level l)
  20670. (unless (org-at-heading-p t)
  20671. (error "Not at a heading"))
  20672. (setq level (funcall outline-level))
  20673. (save-excursion
  20674. (if (not (re-search-backward re nil t))
  20675. t
  20676. (setq l (funcall outline-level))
  20677. (< l level)))))
  20678. (defun org-goto-sibling (&optional previous)
  20679. "Goto the next sibling, even if it is invisible.
  20680. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20681. when a sibling was found. When none is found, return nil and don't
  20682. move point."
  20683. (let ((fun (if previous 're-search-backward 're-search-forward))
  20684. (pos (point))
  20685. (re org-outline-regexp-bol)
  20686. level l)
  20687. (when (condition-case nil (org-back-to-heading t) (error nil))
  20688. (setq level (funcall outline-level))
  20689. (catch 'exit
  20690. (or previous (forward-char 1))
  20691. (while (funcall fun re nil t)
  20692. (setq l (funcall outline-level))
  20693. (when (< l level) (goto-char pos) (throw 'exit nil))
  20694. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20695. (goto-char pos)
  20696. nil))))
  20697. (defun org-show-siblings ()
  20698. "Show all siblings of the current headline."
  20699. (save-excursion
  20700. (while (org-goto-sibling) (org-flag-heading nil)))
  20701. (save-excursion
  20702. (while (org-goto-sibling 'previous)
  20703. (org-flag-heading nil))))
  20704. (defun org-goto-first-child ()
  20705. "Goto the first child, even if it is invisible.
  20706. Return t when a child was found. Otherwise don't move point and
  20707. return nil."
  20708. (let (level (pos (point)) (re org-outline-regexp-bol))
  20709. (when (condition-case nil (org-back-to-heading t) (error nil))
  20710. (setq level (outline-level))
  20711. (forward-char 1)
  20712. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20713. (progn (goto-char (match-beginning 0)) t)
  20714. (goto-char pos) nil))))
  20715. (defun org-show-hidden-entry ()
  20716. "Show an entry where even the heading is hidden."
  20717. (save-excursion
  20718. (org-show-entry)))
  20719. (defun org-flag-heading (flag &optional entry)
  20720. "Flag the current heading. FLAG non-nil means make invisible.
  20721. When ENTRY is non-nil, show the entire entry."
  20722. (save-excursion
  20723. (org-back-to-heading t)
  20724. ;; Check if we should show the entire entry
  20725. (if entry
  20726. (progn
  20727. (org-show-entry)
  20728. (save-excursion
  20729. (and (outline-next-heading)
  20730. (org-flag-heading nil))))
  20731. (outline-flag-region (max (point-min) (1- (point)))
  20732. (save-excursion (outline-end-of-heading) (point))
  20733. flag))))
  20734. (defun org-get-next-sibling ()
  20735. "Move to next heading of the same level, and return point.
  20736. If there is no such heading, return nil.
  20737. This is like outline-next-sibling, but invisible headings are ok."
  20738. (let ((level (funcall outline-level)))
  20739. (outline-next-heading)
  20740. (while (and (not (eobp)) (> (funcall outline-level) level))
  20741. (outline-next-heading))
  20742. (if (or (eobp) (< (funcall outline-level) level))
  20743. nil
  20744. (point))))
  20745. (defun org-get-last-sibling ()
  20746. "Move to previous heading of the same level, and return point.
  20747. If there is no such heading, return nil."
  20748. (let ((opoint (point))
  20749. (level (funcall outline-level)))
  20750. (outline-previous-heading)
  20751. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20752. (while (and (> (funcall outline-level) level)
  20753. (not (bobp)))
  20754. (outline-previous-heading))
  20755. (if (< (funcall outline-level) level)
  20756. nil
  20757. (point)))))
  20758. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20759. "Goto to the end of a subtree."
  20760. ;; This contains an exact copy of the original function, but it uses
  20761. ;; `org-back-to-heading', to make it work also in invisible
  20762. ;; trees. And is uses an invisible-ok argument.
  20763. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20764. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20765. ;; with many children and grandchildren etc, this can be much faster
  20766. ;; than the outline version.
  20767. (org-back-to-heading invisible-ok)
  20768. (let ((first t)
  20769. (level (funcall outline-level)))
  20770. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20771. ;; A true heading (not a plain list item), in Org-mode
  20772. ;; This means we can easily find the end by looking
  20773. ;; only for the right number of stars. Using a regexp to do
  20774. ;; this is so much faster than using a Lisp loop.
  20775. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20776. (forward-char 1)
  20777. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20778. ;; something else, do it the slow way
  20779. (while (and (not (eobp))
  20780. (or first (> (funcall outline-level) level)))
  20781. (setq first nil)
  20782. (outline-next-heading)))
  20783. (unless to-heading
  20784. (if (memq (preceding-char) '(?\n ?\^M))
  20785. (progn
  20786. ;; Go to end of line before heading
  20787. (forward-char -1)
  20788. (if (memq (preceding-char) '(?\n ?\^M))
  20789. ;; leave blank line before heading
  20790. (forward-char -1))))))
  20791. (point))
  20792. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20793. "Use Org version in org-mode, for dramatic speed-up."
  20794. (if (derived-mode-p 'org-mode)
  20795. (progn
  20796. (org-end-of-subtree nil t)
  20797. (unless (eobp) (backward-char 1)))
  20798. ad-do-it))
  20799. (defun org-end-of-meta-data-and-drawers ()
  20800. "Jump to the first text after meta data and drawers in the current entry.
  20801. This will move over empty lines, lines with planning time stamps,
  20802. clocking lines, and drawers."
  20803. (org-back-to-heading t)
  20804. (let ((end (save-excursion (outline-next-heading) (point)))
  20805. (re (concat "\\(" org-drawer-regexp "\\)"
  20806. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20807. (forward-line 1)
  20808. (while (re-search-forward re end t)
  20809. (if (not (match-end 1))
  20810. ;; empty or planning line
  20811. (forward-line 1)
  20812. ;; a drawer, find the end
  20813. (re-search-forward "^[ \t]*:END:" end 'move)
  20814. (forward-line 1)))
  20815. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20816. (point)))
  20817. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20818. "Move forward to the ARG'th subheading at same level as this one.
  20819. Stop at the first and last subheadings of a superior heading.
  20820. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20821. non-nil it will also look at invisible ones."
  20822. (interactive "p")
  20823. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20824. (if (and arg (< arg 0))
  20825. (goto-char (point-min))
  20826. (outline-next-heading))
  20827. (org-at-heading-p)
  20828. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20829. (f (if (and arg (< arg 0))
  20830. 're-search-backward
  20831. 're-search-forward))
  20832. (count (if arg (abs arg) 1))
  20833. (result (point)))
  20834. (forward-char (if (and arg (< arg 0)) -1 1))
  20835. (while (and (> count 0)
  20836. (funcall f org-outline-regexp-bol nil 'move))
  20837. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20838. (cond ((< l level) (setq count 0))
  20839. ((and (= l level)
  20840. (or invisible-ok
  20841. (progn
  20842. (goto-char (line-beginning-position))
  20843. (not (outline-invisible-p)))))
  20844. (setq count (1- count))
  20845. (when (eq l level)
  20846. (setq result (point)))))))
  20847. (goto-char result))
  20848. (beginning-of-line 1)))
  20849. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20850. "Move backward to the ARG'th subheading at same level as this one.
  20851. Stop at the first and last subheadings of a superior heading."
  20852. (interactive "p")
  20853. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20854. (defun org-next-block (arg &optional backward block-regexp)
  20855. "Jump to the next block.
  20856. With a prefix argument ARG, jump forward ARG many source blocks.
  20857. When BACKWARD is non-nil, jump to the previous block.
  20858. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20859. (interactive "p")
  20860. (let ((re (or block-regexp org-block-regexp))
  20861. (re-search-fn (or (and backward 're-search-backward)
  20862. 're-search-forward)))
  20863. (if (looking-at re) (forward-char 1))
  20864. (condition-case nil
  20865. (funcall re-search-fn re nil nil arg)
  20866. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20867. (goto-char (match-beginning 0)) (org-show-context)))
  20868. (defun org-previous-block (arg &optional block-regexp)
  20869. "Jump to the previous block.
  20870. With a prefix argument ARG, jump backward ARG many source blocks.
  20871. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20872. (interactive "p")
  20873. (org-next-block arg t block-regexp))
  20874. (defun org-forward-element ()
  20875. "Move forward by one element.
  20876. Move to the next element at the same level, when possible."
  20877. (interactive)
  20878. (cond ((eobp) (user-error "Cannot move further down"))
  20879. ((org-with-limited-levels (org-at-heading-p))
  20880. (let ((origin (point)))
  20881. (goto-char (org-end-of-subtree nil t))
  20882. (unless (org-with-limited-levels (org-at-heading-p))
  20883. (goto-char origin)
  20884. (user-error "Cannot move further down"))))
  20885. (t
  20886. (let* ((elem (org-element-at-point))
  20887. (end (org-element-property :end elem))
  20888. (parent (org-element-property :parent elem)))
  20889. (if (and parent (= (org-element-property :contents-end parent) end))
  20890. (goto-char (org-element-property :end parent))
  20891. (goto-char end))))))
  20892. (defun org-backward-element ()
  20893. "Move backward by one element.
  20894. Move to the previous element at the same level, when possible."
  20895. (interactive)
  20896. (cond ((bobp) (user-error "Cannot move further up"))
  20897. ((org-with-limited-levels (org-at-heading-p))
  20898. ;; At a headline, move to the previous one, if any, or stay
  20899. ;; here.
  20900. (let ((origin (point)))
  20901. (org-with-limited-levels (org-backward-heading-same-level 1))
  20902. ;; When current headline has no sibling above, move to its
  20903. ;; parent.
  20904. (when (= (point) origin)
  20905. (or (org-with-limited-levels (org-up-heading-safe))
  20906. (progn (goto-char origin)
  20907. (user-error "Cannot move further up"))))))
  20908. (t
  20909. (let* ((trail (org-element-at-point 'keep-trail))
  20910. (elem (car trail))
  20911. (prev-elem (nth 1 trail))
  20912. (beg (org-element-property :begin elem)))
  20913. (cond
  20914. ;; Move to beginning of current element if point isn't
  20915. ;; there already.
  20916. ((/= (point) beg) (goto-char beg))
  20917. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20918. ((org-before-first-heading-p) (goto-char (point-min)))
  20919. (t (org-back-to-heading)))))))
  20920. (defun org-up-element ()
  20921. "Move to upper element."
  20922. (interactive)
  20923. (if (org-with-limited-levels (org-at-heading-p))
  20924. (unless (org-up-heading-safe) (error "No surrounding element"))
  20925. (let* ((elem (org-element-at-point))
  20926. (parent (org-element-property :parent elem)))
  20927. (if parent (goto-char (org-element-property :begin parent))
  20928. (if (org-with-limited-levels (org-before-first-heading-p))
  20929. (error "No surrounding element")
  20930. (org-with-limited-levels (org-back-to-heading)))))))
  20931. (defvar org-element-greater-elements)
  20932. (defun org-down-element ()
  20933. "Move to inner element."
  20934. (interactive)
  20935. (let ((element (org-element-at-point)))
  20936. (cond
  20937. ((memq (org-element-type element) '(plain-list table))
  20938. (goto-char (org-element-property :contents-begin element))
  20939. (forward-char))
  20940. ((memq (org-element-type element) org-element-greater-elements)
  20941. ;; If contents are hidden, first disclose them.
  20942. (when (org-element-property :hiddenp element) (org-cycle))
  20943. (goto-char (or (org-element-property :contents-begin element)
  20944. (error "No content for this element"))))
  20945. (t (error "No inner element")))))
  20946. (defun org-drag-element-backward ()
  20947. "Move backward element at point."
  20948. (interactive)
  20949. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20950. (let* ((trail (org-element-at-point 'keep-trail))
  20951. (elem (car trail))
  20952. (prev-elem (nth 1 trail)))
  20953. ;; Error out if no previous element or previous element is
  20954. ;; a parent of the current one.
  20955. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20956. (error "Cannot drag element backward")
  20957. (let ((pos (point)))
  20958. (org-element-swap-A-B prev-elem elem)
  20959. (goto-char (+ (org-element-property :begin prev-elem)
  20960. (- pos (org-element-property :begin elem)))))))))
  20961. (defun org-drag-element-forward ()
  20962. "Move forward element at point."
  20963. (interactive)
  20964. (let* ((pos (point))
  20965. (elem (org-element-at-point)))
  20966. (when (= (point-max) (org-element-property :end elem))
  20967. (error "Cannot drag element forward"))
  20968. (goto-char (org-element-property :end elem))
  20969. (let ((next-elem (org-element-at-point)))
  20970. (when (or (org-element-nested-p elem next-elem)
  20971. (and (eq (org-element-type next-elem) 'headline)
  20972. (not (eq (org-element-type elem) 'headline))))
  20973. (goto-char pos)
  20974. (error "Cannot drag element forward"))
  20975. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20976. ;; body's length (without final blanks) and by the length of
  20977. ;; blanks between ELEM and NEXT-ELEM.
  20978. (let ((size-next (- (save-excursion
  20979. (goto-char (org-element-property :end next-elem))
  20980. (skip-chars-backward " \r\t\n")
  20981. (forward-line)
  20982. ;; Small correction if buffer doesn't end
  20983. ;; with a newline character.
  20984. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20985. (org-element-property :begin next-elem)))
  20986. (size-blank (- (org-element-property :end elem)
  20987. (save-excursion
  20988. (goto-char (org-element-property :end elem))
  20989. (skip-chars-backward " \r\t\n")
  20990. (forward-line)
  20991. (point)))))
  20992. (org-element-swap-A-B elem next-elem)
  20993. (goto-char (+ pos size-next size-blank))))))
  20994. (defun org-drag-line-forward (arg)
  20995. "Drag the line at point ARG lines forward."
  20996. (interactive "p")
  20997. (dotimes (n (abs arg))
  20998. (let ((c (current-column)))
  20999. (if (< 0 arg)
  21000. (progn
  21001. (beginning-of-line 2)
  21002. (transpose-lines 1)
  21003. (beginning-of-line 0))
  21004. (transpose-lines 1)
  21005. (beginning-of-line -1))
  21006. (org-move-to-column c))))
  21007. (defun org-drag-line-backward (arg)
  21008. "Drag the line at point ARG lines backward."
  21009. (interactive "p")
  21010. (org-drag-line-forward (- arg)))
  21011. (defun org-mark-element ()
  21012. "Put point at beginning of this element, mark at end.
  21013. Interactively, if this command is repeated or (in Transient Mark
  21014. mode) if the mark is active, it marks the next element after the
  21015. ones already marked."
  21016. (interactive)
  21017. (let (deactivate-mark)
  21018. (if (and (org-called-interactively-p 'any)
  21019. (or (and (eq last-command this-command) (mark t))
  21020. (and transient-mark-mode mark-active)))
  21021. (set-mark
  21022. (save-excursion
  21023. (goto-char (mark))
  21024. (goto-char (org-element-property :end (org-element-at-point)))))
  21025. (let ((element (org-element-at-point)))
  21026. (end-of-line)
  21027. (push-mark (org-element-property :end element) t t)
  21028. (goto-char (org-element-property :begin element))))))
  21029. (defun org-narrow-to-element ()
  21030. "Narrow buffer to current element."
  21031. (interactive)
  21032. (let ((elem (org-element-at-point)))
  21033. (cond
  21034. ((eq (car elem) 'headline)
  21035. (narrow-to-region
  21036. (org-element-property :begin elem)
  21037. (org-element-property :end elem)))
  21038. ((memq (car elem) org-element-greater-elements)
  21039. (narrow-to-region
  21040. (org-element-property :contents-begin elem)
  21041. (org-element-property :contents-end elem)))
  21042. (t
  21043. (narrow-to-region
  21044. (org-element-property :begin elem)
  21045. (org-element-property :end elem))))))
  21046. (defun org-transpose-element ()
  21047. "Transpose current and previous elements, keeping blank lines between.
  21048. Point is moved after both elements."
  21049. (interactive)
  21050. (org-skip-whitespace)
  21051. (let ((end (org-element-property :end (org-element-at-point))))
  21052. (org-drag-element-backward)
  21053. (goto-char end)))
  21054. (defun org-unindent-buffer ()
  21055. "Un-indent the visible part of the buffer.
  21056. Relative indentation (between items, inside blocks, etc.) isn't
  21057. modified."
  21058. (interactive)
  21059. (unless (eq major-mode 'org-mode)
  21060. (error "Cannot un-indent a buffer not in Org mode"))
  21061. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21062. unindent-tree ; For byte-compiler.
  21063. (unindent-tree
  21064. (function
  21065. (lambda (contents)
  21066. (mapc
  21067. (lambda (element)
  21068. (if (memq (org-element-type element) '(headline section))
  21069. (funcall unindent-tree (org-element-contents element))
  21070. (save-excursion
  21071. (save-restriction
  21072. (narrow-to-region
  21073. (org-element-property :begin element)
  21074. (org-element-property :end element))
  21075. (org-do-remove-indentation)))))
  21076. (reverse contents))))))
  21077. (funcall unindent-tree (org-element-contents parse-tree))))
  21078. (defun org-show-subtree ()
  21079. "Show everything after this heading at deeper levels."
  21080. (interactive)
  21081. (outline-flag-region
  21082. (point)
  21083. (save-excursion
  21084. (org-end-of-subtree t t))
  21085. nil))
  21086. (defun org-show-entry ()
  21087. "Show the body directly following this heading.
  21088. Show the heading too, if it is currently invisible."
  21089. (interactive)
  21090. (save-excursion
  21091. (condition-case nil
  21092. (progn
  21093. (org-back-to-heading t)
  21094. (outline-flag-region
  21095. (max (point-min) (1- (point)))
  21096. (save-excursion
  21097. (if (re-search-forward
  21098. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21099. (match-beginning 1)
  21100. (point-max)))
  21101. nil)
  21102. (org-cycle-hide-drawers 'children))
  21103. (error nil))))
  21104. (defun org-make-options-regexp (kwds &optional extra)
  21105. "Make a regular expression for keyword lines."
  21106. (concat
  21107. "^#\\+\\("
  21108. (mapconcat 'regexp-quote kwds "\\|")
  21109. (if extra (concat "\\|" extra))
  21110. "\\):[ \t]*\\(.*\\)"))
  21111. ;; Make isearch reveal the necessary context
  21112. (defun org-isearch-end ()
  21113. "Reveal context after isearch exits."
  21114. (when isearch-success ; only if search was successful
  21115. (if (featurep 'xemacs)
  21116. ;; Under XEmacs, the hook is run in the correct place,
  21117. ;; we directly show the context.
  21118. (org-show-context 'isearch)
  21119. ;; In Emacs the hook runs *before* restoring the overlays.
  21120. ;; So we have to use a one-time post-command-hook to do this.
  21121. ;; (Emacs 22 has a special variable, see function `org-mode')
  21122. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21123. isearch-mode-end-hook-quit)
  21124. ;; Only when the isearch was not quitted.
  21125. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21126. 'append 'local)))
  21127. (org-fix-ellipsis-at-bol)))
  21128. (defun org-isearch-post-command ()
  21129. "Remove self from hook, and show context."
  21130. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21131. (org-show-context 'isearch))
  21132. ;;;; Integration with and fixes for other packages
  21133. ;;; Imenu support
  21134. (defvar org-imenu-markers nil
  21135. "All markers currently used by Imenu.")
  21136. (make-variable-buffer-local 'org-imenu-markers)
  21137. (defun org-imenu-new-marker (&optional pos)
  21138. "Return a new marker for use by Imenu, and remember the marker."
  21139. (let ((m (make-marker)))
  21140. (move-marker m (or pos (point)))
  21141. (push m org-imenu-markers)
  21142. m))
  21143. (defun org-imenu-get-tree ()
  21144. "Produce the index for Imenu."
  21145. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21146. (setq org-imenu-markers nil)
  21147. (let* ((n org-imenu-depth)
  21148. (re (concat "^" (org-get-limited-outline-regexp)))
  21149. (subs (make-vector (1+ n) nil))
  21150. (last-level 0)
  21151. m level head0 head)
  21152. (save-excursion
  21153. (save-restriction
  21154. (widen)
  21155. (goto-char (point-max))
  21156. (while (re-search-backward re nil t)
  21157. (setq level (org-reduced-level (funcall outline-level)))
  21158. (when (and (<= level n)
  21159. (looking-at org-complex-heading-regexp)
  21160. (setq head0 (org-match-string-no-properties 4)))
  21161. (setq head (org-link-display-format head0)
  21162. m (org-imenu-new-marker))
  21163. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21164. (if (>= level last-level)
  21165. (push (cons head m) (aref subs level))
  21166. (push (cons head (aref subs (1+ level))) (aref subs level))
  21167. (loop for i from (1+ level) to n do (aset subs i nil)))
  21168. (setq last-level level)))))
  21169. (aref subs 1)))
  21170. (eval-after-load "imenu"
  21171. '(progn
  21172. (add-hook 'imenu-after-jump-hook
  21173. (lambda ()
  21174. (if (derived-mode-p 'org-mode)
  21175. (org-show-context 'org-goto))))))
  21176. (defun org-link-display-format (link)
  21177. "Replace a link with either the description, or the link target
  21178. if no description is present"
  21179. (save-match-data
  21180. (if (string-match org-bracket-link-analytic-regexp link)
  21181. (replace-match (if (match-end 5)
  21182. (match-string 5 link)
  21183. (concat (match-string 1 link)
  21184. (match-string 3 link)))
  21185. nil t link)
  21186. link)))
  21187. (defun org-toggle-link-display ()
  21188. "Toggle the literal or descriptive display of links."
  21189. (interactive)
  21190. (if org-descriptive-links
  21191. (progn (org-remove-from-invisibility-spec '(org-link))
  21192. (org-restart-font-lock)
  21193. (setq org-descriptive-links nil))
  21194. (progn (add-to-invisibility-spec '(org-link))
  21195. (org-restart-font-lock)
  21196. (setq org-descriptive-links t))))
  21197. ;; Speedbar support
  21198. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21199. "Overlay marking the agenda restriction line in speedbar.")
  21200. (overlay-put org-speedbar-restriction-lock-overlay
  21201. 'face 'org-agenda-restriction-lock)
  21202. (overlay-put org-speedbar-restriction-lock-overlay
  21203. 'help-echo "Agendas are currently limited to this item.")
  21204. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21205. (defun org-speedbar-set-agenda-restriction ()
  21206. "Restrict future agenda commands to the location at point in speedbar.
  21207. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21208. (interactive)
  21209. (require 'org-agenda)
  21210. (let (p m tp np dir txt)
  21211. (cond
  21212. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21213. 'org-imenu t))
  21214. (setq m (get-text-property p 'org-imenu-marker))
  21215. (with-current-buffer (marker-buffer m)
  21216. (goto-char m)
  21217. (org-agenda-set-restriction-lock 'subtree)))
  21218. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21219. 'speedbar-function 'speedbar-find-file))
  21220. (setq tp (previous-single-property-change
  21221. (1+ p) 'speedbar-function)
  21222. np (next-single-property-change
  21223. tp 'speedbar-function)
  21224. dir (speedbar-line-directory)
  21225. txt (buffer-substring-no-properties (or tp (point-min))
  21226. (or np (point-max))))
  21227. (with-current-buffer (find-file-noselect
  21228. (let ((default-directory dir))
  21229. (expand-file-name txt)))
  21230. (unless (derived-mode-p 'org-mode)
  21231. (error "Cannot restrict to non-Org-mode file"))
  21232. (org-agenda-set-restriction-lock 'file)))
  21233. (t (error "Don't know how to restrict Org-mode's agenda")))
  21234. (move-overlay org-speedbar-restriction-lock-overlay
  21235. (point-at-bol) (point-at-eol))
  21236. (setq current-prefix-arg nil)
  21237. (org-agenda-maybe-redo)))
  21238. (eval-after-load "speedbar"
  21239. '(progn
  21240. (speedbar-add-supported-extension ".org")
  21241. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21242. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21243. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21244. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21245. (add-hook 'speedbar-visiting-tag-hook
  21246. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21247. ;;; Fixes and Hacks for problems with other packages
  21248. ;; Make flyspell not check words in links, to not mess up our keymap
  21249. (defvar org-element-affiliated-keywords) ; From org-element.el
  21250. (defvar org-element-block-name-alist) ; From org-element.el
  21251. (defun org-mode-flyspell-verify ()
  21252. "Don't let flyspell put overlays at active buttons, or on
  21253. {todo,all-time,additional-option-like}-keywords."
  21254. (let ((pos (max (1- (point)) (point-min)))
  21255. (word (thing-at-point 'word)))
  21256. (and (not (get-text-property pos 'keymap))
  21257. (not (get-text-property pos 'org-no-flyspell))
  21258. (not (member word org-todo-keywords-1))
  21259. (not (member word org-all-time-keywords))
  21260. (not (member word org-options-keywords))
  21261. (not (member word (mapcar 'car org-startup-options)))
  21262. (not (member-ignore-case word org-element-affiliated-keywords))
  21263. (not (member-ignore-case word (org-get-export-keywords)))
  21264. (not (member-ignore-case
  21265. word (mapcar 'car org-element-block-name-alist)))
  21266. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21267. (defun org-remove-flyspell-overlays-in (beg end)
  21268. "Remove flyspell overlays in region."
  21269. (and (org-bound-and-true-p flyspell-mode)
  21270. (fboundp 'flyspell-delete-region-overlays)
  21271. (flyspell-delete-region-overlays beg end))
  21272. (add-text-properties beg end '(org-no-flyspell t)))
  21273. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21274. (eval-after-load "bookmark"
  21275. '(if (boundp 'bookmark-after-jump-hook)
  21276. ;; We can use the hook
  21277. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21278. ;; Hook not available, use advice
  21279. (defadvice bookmark-jump (after org-make-visible activate)
  21280. "Make the position visible."
  21281. (org-bookmark-jump-unhide))))
  21282. ;; Make sure saveplace shows the location if it was hidden
  21283. (eval-after-load "saveplace"
  21284. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21285. "Make the position visible."
  21286. (org-bookmark-jump-unhide)))
  21287. ;; Make sure ecb shows the location if it was hidden
  21288. (eval-after-load "ecb"
  21289. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21290. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21291. (if (derived-mode-p 'org-mode)
  21292. (org-show-context))))
  21293. (defun org-bookmark-jump-unhide ()
  21294. "Unhide the current position, to show the bookmark location."
  21295. (and (derived-mode-p 'org-mode)
  21296. (or (outline-invisible-p)
  21297. (save-excursion (goto-char (max (point-min) (1- (point))))
  21298. (outline-invisible-p)))
  21299. (org-show-context 'bookmark-jump)))
  21300. ;; Make session.el ignore our circular variable
  21301. (eval-after-load "session"
  21302. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21303. ;;;; Experimental code
  21304. (defun org-closed-in-range ()
  21305. "Sparse tree of items closed in a certain time range.
  21306. Still experimental, may disappear in the future."
  21307. (interactive)
  21308. ;; Get the time interval from the user.
  21309. (let* ((time1 (org-float-time
  21310. (org-read-date nil 'to-time nil "Starting date: ")))
  21311. (time2 (org-float-time
  21312. (org-read-date nil 'to-time nil "End date:")))
  21313. ;; callback function
  21314. (callback (lambda ()
  21315. (let ((time
  21316. (org-float-time
  21317. (apply 'encode-time
  21318. (org-parse-time-string
  21319. (match-string 1))))))
  21320. ;; check if time in interval
  21321. (and (>= time time1) (<= time time2))))))
  21322. ;; make tree, check each match with the callback
  21323. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21324. ;;;; Finish up
  21325. (provide 'org)
  21326. (run-hooks 'org-load-hook)
  21327. ;;; org.el ends here