org.el 842 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t)
  74. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  75. ;; some places -- e.g. see the macro org-with-limited-levels.
  76. ;;
  77. ;; In Org buffers, the value of `outline-regexp' is that of
  78. ;; `org-outline-regexp'. The only function still directly relying on
  79. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  80. ;; job when `orgstruct-mode' is active.
  81. (defvar org-outline-regexp "\\*+ "
  82. "Regexp to match Org headlines.")
  83. (defvar org-outline-regexp-bol "^\\*+ "
  84. "Regexp to match Org headlines.
  85. This is similar to `org-outline-regexp' but additionally makes
  86. sure that we are at the beginning of the line.")
  87. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  88. "Matches an headline, putting stars and text into groups.
  89. Stars are put in group 1 and the trimmed body in group 2.")
  90. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  91. (when (fboundp 'defvaralias)
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-at-clock-log-p "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" ())
  106. (declare-function org-clock-timestamps-down "org-clock" ())
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "org-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-id-get-create "org-id" (&optional force))
  114. (declare-function org-id-find-id-file "org-id" (id))
  115. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  116. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  117. (declare-function org-table-align "org-table" ())
  118. (declare-function org-table-paste-rectangle "org-table" ())
  119. (declare-function org-table-maybe-eval-formula "org-table" ())
  120. (declare-function org-table-maybe-recalculate-line "org-table" ())
  121. ;; load languages based on value of `org-babel-load-languages'
  122. (defvar org-babel-load-languages)
  123. ;;;###autoload
  124. (defun org-babel-do-load-languages (sym value)
  125. "Load the languages defined in `org-babel-load-languages'."
  126. (set-default sym value)
  127. (mapc (lambda (pair)
  128. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  129. (if active
  130. (progn
  131. (require (intern (concat "ob-" lang))))
  132. (progn
  133. (funcall 'fmakunbound
  134. (intern (concat "org-babel-execute:" lang)))
  135. (funcall 'fmakunbound
  136. (intern (concat "org-babel-expand-body:" lang)))))))
  137. org-babel-load-languages))
  138. (defcustom org-babel-load-languages '((emacs-lisp . t))
  139. "Languages which can be evaluated in Org-mode buffers.
  140. This list can be used to load support for any of the languages
  141. below, note that each language will depend on a different set of
  142. system executables and/or Emacs modes. When a language is
  143. \"loaded\", then code blocks in that language can be evaluated
  144. with `org-babel-execute-src-block' bound by default to C-c
  145. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  146. be set to remove code block evaluation from the C-c C-c
  147. keybinding. By default only Emacs Lisp (which has no
  148. requirements) is loaded."
  149. :group 'org-babel
  150. :set 'org-babel-do-load-languages
  151. :version "24.1"
  152. :type '(alist :tag "Babel Languages"
  153. :key-type
  154. (choice
  155. (const :tag "Awk" awk)
  156. (const :tag "C" C)
  157. (const :tag "R" R)
  158. (const :tag "Asymptote" asymptote)
  159. (const :tag "Calc" calc)
  160. (const :tag "Clojure" clojure)
  161. (const :tag "CSS" css)
  162. (const :tag "Ditaa" ditaa)
  163. (const :tag "Dot" dot)
  164. (const :tag "Emacs Lisp" emacs-lisp)
  165. (const :tag "Fortran" fortran)
  166. (const :tag "Gnuplot" gnuplot)
  167. (const :tag "Haskell" haskell)
  168. (const :tag "IO" io)
  169. (const :tag "Java" java)
  170. (const :tag "Javascript" js)
  171. (const :tag "LaTeX" latex)
  172. (const :tag "Ledger" ledger)
  173. (const :tag "Lilypond" lilypond)
  174. (const :tag "Lisp" lisp)
  175. (const :tag "Maxima" maxima)
  176. (const :tag "Matlab" matlab)
  177. (const :tag "Mscgen" mscgen)
  178. (const :tag "Ocaml" ocaml)
  179. (const :tag "Octave" octave)
  180. (const :tag "Org" org)
  181. (const :tag "Perl" perl)
  182. (const :tag "Pico Lisp" picolisp)
  183. (const :tag "PlantUML" plantuml)
  184. (const :tag "Python" python)
  185. (const :tag "Ruby" ruby)
  186. (const :tag "Sass" sass)
  187. (const :tag "Scala" scala)
  188. (const :tag "Scheme" scheme)
  189. (const :tag "Screen" screen)
  190. (const :tag "Shell Script" sh)
  191. (const :tag "Shen" shen)
  192. (const :tag "Sql" sql)
  193. (const :tag "Sqlite" sqlite))
  194. :value-type (boolean :tag "Activate" :value t)))
  195. ;;;; Customization variables
  196. (defcustom org-clone-delete-id nil
  197. "Remove ID property of clones of a subtree.
  198. When non-nil, clones of a subtree don't inherit the ID property.
  199. Otherwise they inherit the ID property with a new unique
  200. identifier."
  201. :type 'boolean
  202. :version "24.1"
  203. :group 'org-id)
  204. ;;; Version
  205. (require 'org-compat)
  206. (org-check-version)
  207. ;;;###autoload
  208. (defun org-version (&optional here full message)
  209. "Show the org-mode version in the echo area.
  210. With prefix argument HERE, insert it at point.
  211. When FULL is non-nil, use a verbose version string.
  212. When MESSAGE is non-nil, display a message with the version."
  213. (interactive "P")
  214. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  215. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs.el")))
  216. (org-trash (or
  217. (and (fboundp 'org-release) (fboundp 'org-git-version))
  218. (load (concat org-dir "org-version.el")
  219. 'noerror 'nomessage 'nosuffix)))
  220. (org-version (org-release))
  221. (git-version (org-git-version))
  222. (version (format "Org-mode version %s (%s @ %s)"
  223. org-version
  224. git-version
  225. (if org-install-dir
  226. (if (string= org-dir org-install-dir)
  227. org-install-dir
  228. (concat "mixed installation! " org-install-dir " and " org-dir))
  229. "org-loaddefs.el can not be found!")))
  230. (_version (if full version org-version)))
  231. (if (org-called-interactively-p 'interactive)
  232. (if here
  233. (insert version)
  234. (message version))
  235. (if message (message _version))
  236. _version)))
  237. (defconst org-version (org-version))
  238. ;;; Compatibility constants
  239. ;;; The custom variables
  240. (defgroup org nil
  241. "Outline-based notes management and organizer."
  242. :tag "Org"
  243. :group 'outlines
  244. :group 'calendar)
  245. (defcustom org-mode-hook nil
  246. "Mode hook for Org-mode, run after the mode was turned on."
  247. :group 'org
  248. :type 'hook)
  249. (defcustom org-load-hook nil
  250. "Hook that is run after org.el has been loaded."
  251. :group 'org
  252. :type 'hook)
  253. (defcustom org-log-buffer-setup-hook nil
  254. "Hook that is run after an Org log buffer is created."
  255. :group 'org
  256. :version "24.1"
  257. :type 'hook)
  258. (defvar org-modules) ; defined below
  259. (defvar org-modules-loaded nil
  260. "Have the modules been loaded already?")
  261. (defun org-load-modules-maybe (&optional force)
  262. "Load all extensions listed in `org-modules'."
  263. (when (or force (not org-modules-loaded))
  264. (mapc (lambda (ext)
  265. (condition-case nil (require ext)
  266. (error (message "Problems while trying to load feature `%s'" ext))))
  267. org-modules)
  268. (setq org-modules-loaded t)))
  269. (defun org-set-modules (var value)
  270. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  271. (set var value)
  272. (when (featurep 'org)
  273. (org-load-modules-maybe 'force)))
  274. (when (org-bound-and-true-p org-modules)
  275. (let ((a (member 'org-infojs org-modules)))
  276. (and a (setcar a 'org-jsinfo))))
  277. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  278. "Modules that should always be loaded together with org.el.
  279. If a description starts with <C>, the file is not part of Emacs
  280. and loading it will require that you have downloaded and properly installed
  281. the org-mode distribution.
  282. You can also use this system to load external packages (i.e. neither Org
  283. core modules, nor modules from the CONTRIB directory). Just add symbols
  284. to the end of the list. If the package is called org-xyz.el, then you need
  285. to add the symbol `xyz', and the package must have a call to
  286. (provide 'org-xyz)"
  287. :group 'org
  288. :set 'org-set-modules
  289. :type
  290. '(set :greedy t
  291. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  292. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  293. (const :tag " crypt: Encryption of subtrees" org-crypt)
  294. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  295. (const :tag " docview: Links to doc-view buffers" org-docview)
  296. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  297. (const :tag " id: Global IDs for identifying entries" org-id)
  298. (const :tag " info: Links to Info nodes" org-info)
  299. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  300. (const :tag " habit: Track your consistency with habits" org-habit)
  301. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  302. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  303. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  304. (const :tag " mew Links to Mew folders/messages" org-mew)
  305. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  306. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  307. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  308. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  309. (const :tag " vm: Links to VM folders/messages" org-vm)
  310. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  311. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  312. (const :tag " mouse: Additional mouse support" org-mouse)
  313. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  314. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  315. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  316. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  317. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  318. (const :tag "C collector: Collect properties into tables" org-collector)
  319. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  320. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  321. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  322. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  323. (const :tag "C eval: Include command output as text" org-eval)
  324. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  325. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  326. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  327. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  328. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  329. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  330. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  331. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  332. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  333. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  334. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  335. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  336. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  337. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  338. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  339. (const :tag "C registry: A registry for Org-mode links" org-registry)
  340. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  341. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  342. (const :tag "C secretary: Team management with org-mode" org-secretary)
  343. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  344. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  345. (const :tag "C track: Keep up with Org-mode development" org-track)
  346. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  347. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  348. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  349. (defcustom org-support-shift-select nil
  350. "Non-nil means make shift-cursor commands select text when possible.
  351. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  352. start selecting a region, or enlarge regions started in this way.
  353. In Org-mode, in special contexts, these same keys are used for
  354. other purposes, important enough to compete with shift selection.
  355. Org tries to balance these needs by supporting `shift-select-mode'
  356. outside these special contexts, under control of this variable.
  357. The default of this variable is nil, to avoid confusing behavior. Shifted
  358. cursor keys will then execute Org commands in the following contexts:
  359. - on a headline, changing TODO state (left/right) and priority (up/down)
  360. - on a time stamp, changing the time
  361. - in a plain list item, changing the bullet type
  362. - in a property definition line, switching between allowed values
  363. - in the BEGIN line of a clock table (changing the time block).
  364. Outside these contexts, the commands will throw an error.
  365. When this variable is t and the cursor is not in a special
  366. context, Org-mode will support shift-selection for making and
  367. enlarging regions. To make this more effective, the bullet
  368. cycling will no longer happen anywhere in an item line, but only
  369. if the cursor is exactly on the bullet.
  370. If you set this variable to the symbol `always', then the keys
  371. will not be special in headlines, property lines, and item lines,
  372. to make shift selection work there as well. If this is what you
  373. want, you can use the following alternative commands: `C-c C-t'
  374. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  375. can be used to switch TODO sets, `C-c -' to cycle item bullet
  376. types, and properties can be edited by hand or in column view.
  377. However, when the cursor is on a timestamp, shift-cursor commands
  378. will still edit the time stamp - this is just too good to give up.
  379. XEmacs user should have this variable set to nil, because
  380. `shift-select-mode' is in Emacs 23 or later only."
  381. :group 'org
  382. :type '(choice
  383. (const :tag "Never" nil)
  384. (const :tag "When outside special context" t)
  385. (const :tag "Everywhere except timestamps" always)))
  386. (defcustom org-loop-over-headlines-in-active-region nil
  387. "Shall some commands act upon headlines in the active region?
  388. When set to `t', some commands will be performed in all headlines
  389. within the active region.
  390. When set to `start-level', some commands will be performed in all
  391. headlines within the active region, provided that these headlines
  392. are of the same level than the first one.
  393. When set to a string, those commands will be performed on the
  394. matching headlines within the active region. Such string must be
  395. a tags/property/todo match as it is used in the agenda tags view.
  396. The list of commands is: `org-schedule', `org-deadline',
  397. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  398. `org-archive-to-archive-sibling'. The archiving commands skip
  399. already archived entries."
  400. :type '(choice (const :tag "Don't loop" nil)
  401. (const :tag "All headlines in active region" t)
  402. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  403. (string :tag "Tags/Property/Todo matcher"))
  404. :version "24.1"
  405. :group 'org-todo
  406. :group 'org-archive)
  407. (defgroup org-startup nil
  408. "Options concerning startup of Org-mode."
  409. :tag "Org Startup"
  410. :group 'org)
  411. (defcustom org-startup-folded t
  412. "Non-nil means entering Org-mode will switch to OVERVIEW.
  413. This can also be configured on a per-file basis by adding one of
  414. the following lines anywhere in the buffer:
  415. #+STARTUP: fold (or `overview', this is equivalent)
  416. #+STARTUP: nofold (or `showall', this is equivalent)
  417. #+STARTUP: content
  418. #+STARTUP: showeverything"
  419. :group 'org-startup
  420. :type '(choice
  421. (const :tag "nofold: show all" nil)
  422. (const :tag "fold: overview" t)
  423. (const :tag "content: all headlines" content)
  424. (const :tag "show everything, even drawers" showeverything)))
  425. (defcustom org-startup-truncated t
  426. "Non-nil means entering Org-mode will set `truncate-lines'.
  427. This is useful since some lines containing links can be very long and
  428. uninteresting. Also tables look terrible when wrapped."
  429. :group 'org-startup
  430. :type 'boolean)
  431. (defcustom org-startup-indented nil
  432. "Non-nil means turn on `org-indent-mode' on startup.
  433. This can also be configured on a per-file basis by adding one of
  434. the following lines anywhere in the buffer:
  435. #+STARTUP: indent
  436. #+STARTUP: noindent"
  437. :group 'org-structure
  438. :type '(choice
  439. (const :tag "Not" nil)
  440. (const :tag "Globally (slow on startup in large files)" t)))
  441. (defcustom org-use-sub-superscripts t
  442. "Non-nil means interpret \"_\" and \"^\" for export.
  443. When this option is turned on, you can use TeX-like syntax for sub- and
  444. superscripts. Several characters after \"_\" or \"^\" will be
  445. considered as a single item - so grouping with {} is normally not
  446. needed. For example, the following things will be parsed as single
  447. sub- or superscripts.
  448. 10^24 or 10^tau several digits will be considered 1 item.
  449. 10^-12 or 10^-tau a leading sign with digits or a word
  450. x^2-y^3 will be read as x^2 - y^3, because items are
  451. terminated by almost any nonword/nondigit char.
  452. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  453. Still, ambiguity is possible - so when in doubt use {} to enclose the
  454. sub/superscript. If you set this variable to the symbol `{}',
  455. the braces are *required* in order to trigger interpretations as
  456. sub/superscript. This can be helpful in documents that need \"_\"
  457. frequently in plain text.
  458. Not all export backends support this, but HTML does.
  459. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  460. :group 'org-startup
  461. :group 'org-export-translation
  462. :version "24.1"
  463. :type '(choice
  464. (const :tag "Always interpret" t)
  465. (const :tag "Only with braces" {})
  466. (const :tag "Never interpret" nil)))
  467. (if (fboundp 'defvaralias)
  468. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  469. (defcustom org-startup-with-beamer-mode nil
  470. "Non-nil means turn on `org-beamer-mode' on startup.
  471. This can also be configured on a per-file basis by adding one of
  472. the following lines anywhere in the buffer:
  473. #+STARTUP: beamer"
  474. :group 'org-startup
  475. :version "24.1"
  476. :type 'boolean)
  477. (defcustom org-startup-align-all-tables nil
  478. "Non-nil means align all tables when visiting a file.
  479. This is useful when the column width in tables is forced with <N> cookies
  480. in table fields. Such tables will look correct only after the first re-align.
  481. This can also be configured on a per-file basis by adding one of
  482. the following lines anywhere in the buffer:
  483. #+STARTUP: align
  484. #+STARTUP: noalign"
  485. :group 'org-startup
  486. :type 'boolean)
  487. (defcustom org-startup-with-inline-images nil
  488. "Non-nil means show inline images when loading a new Org file.
  489. This can also be configured on a per-file basis by adding one of
  490. the following lines anywhere in the buffer:
  491. #+STARTUP: inlineimages
  492. #+STARTUP: noinlineimages"
  493. :group 'org-startup
  494. :version "24.1"
  495. :type 'boolean)
  496. (defcustom org-insert-mode-line-in-empty-file nil
  497. "Non-nil means insert the first line setting Org-mode in empty files.
  498. When the function `org-mode' is called interactively in an empty file, this
  499. normally means that the file name does not automatically trigger Org-mode.
  500. To ensure that the file will always be in Org-mode in the future, a
  501. line enforcing Org-mode will be inserted into the buffer, if this option
  502. has been set."
  503. :group 'org-startup
  504. :type 'boolean)
  505. (defcustom org-replace-disputed-keys nil
  506. "Non-nil means use alternative key bindings for some keys.
  507. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  508. These keys are also used by other packages like shift-selection-mode'
  509. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  510. If you want to use Org-mode together with one of these other modes,
  511. or more generally if you would like to move some Org-mode commands to
  512. other keys, set this variable and configure the keys with the variable
  513. `org-disputed-keys'.
  514. This option is only relevant at load-time of Org-mode, and must be set
  515. *before* org.el is loaded. Changing it requires a restart of Emacs to
  516. become effective."
  517. :group 'org-startup
  518. :type 'boolean)
  519. (defcustom org-use-extra-keys nil
  520. "Non-nil means use extra key sequence definitions for certain commands.
  521. This happens automatically if you run XEmacs or if `window-system'
  522. is nil. This variable lets you do the same manually. You must
  523. set it before loading org.
  524. Example: on Carbon Emacs 22 running graphically, with an external
  525. keyboard on a Powerbook, the default way of setting M-left might
  526. not work for either Alt or ESC. Setting this variable will make
  527. it work for ESC."
  528. :group 'org-startup
  529. :type 'boolean)
  530. (if (fboundp 'defvaralias)
  531. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  532. (defcustom org-disputed-keys
  533. '(([(shift up)] . [(meta p)])
  534. ([(shift down)] . [(meta n)])
  535. ([(shift left)] . [(meta -)])
  536. ([(shift right)] . [(meta +)])
  537. ([(control shift right)] . [(meta shift +)])
  538. ([(control shift left)] . [(meta shift -)]))
  539. "Keys for which Org-mode and other modes compete.
  540. This is an alist, cars are the default keys, second element specifies
  541. the alternative to use when `org-replace-disputed-keys' is t.
  542. Keys can be specified in any syntax supported by `define-key'.
  543. The value of this option takes effect only at Org-mode's startup,
  544. therefore you'll have to restart Emacs to apply it after changing."
  545. :group 'org-startup
  546. :type 'alist)
  547. (defun org-key (key)
  548. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  549. Or return the original if not disputed.
  550. Also apply the translations defined in `org-xemacs-key-equivalents'."
  551. (when org-replace-disputed-keys
  552. (let* ((nkey (key-description key))
  553. (x (org-find-if (lambda (x)
  554. (equal (key-description (car x)) nkey))
  555. org-disputed-keys)))
  556. (setq key (if x (cdr x) key))))
  557. (when (featurep 'xemacs)
  558. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  559. key)
  560. (defun org-find-if (predicate seq)
  561. (catch 'exit
  562. (while seq
  563. (if (funcall predicate (car seq))
  564. (throw 'exit (car seq))
  565. (pop seq)))))
  566. (defun org-defkey (keymap key def)
  567. "Define a key, possibly translated, as returned by `org-key'."
  568. (define-key keymap (org-key key) def))
  569. (defcustom org-ellipsis nil
  570. "The ellipsis to use in the Org-mode outline.
  571. When nil, just use the standard three dots. When a string, use that instead,
  572. When a face, use the standard 3 dots, but with the specified face.
  573. The change affects only Org-mode (which will then use its own display table).
  574. Changing this requires executing `M-x org-mode' in a buffer to become
  575. effective."
  576. :group 'org-startup
  577. :type '(choice (const :tag "Default" nil)
  578. (face :tag "Face" :value org-warning)
  579. (string :tag "String" :value "...#")))
  580. (defvar org-display-table nil
  581. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  582. (defgroup org-keywords nil
  583. "Keywords in Org-mode."
  584. :tag "Org Keywords"
  585. :group 'org)
  586. (defcustom org-deadline-string "DEADLINE:"
  587. "String to mark deadline entries.
  588. A deadline is this string, followed by a time stamp. Should be a word,
  589. terminated by a colon. You can insert a schedule keyword and
  590. a timestamp with \\[org-deadline].
  591. Changes become only effective after restarting Emacs."
  592. :group 'org-keywords
  593. :type 'string)
  594. (defcustom org-scheduled-string "SCHEDULED:"
  595. "String to mark scheduled TODO entries.
  596. A schedule is this string, followed by a time stamp. Should be a word,
  597. terminated by a colon. You can insert a schedule keyword and
  598. a timestamp with \\[org-schedule].
  599. Changes become only effective after restarting Emacs."
  600. :group 'org-keywords
  601. :type 'string)
  602. (defcustom org-closed-string "CLOSED:"
  603. "String used as the prefix for timestamps logging closing a TODO entry."
  604. :group 'org-keywords
  605. :type 'string)
  606. (defcustom org-clock-string "CLOCK:"
  607. "String used as prefix for timestamps clocking work hours on an item."
  608. :group 'org-keywords
  609. :type 'string)
  610. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  611. org-scheduled-string "\\|"
  612. org-deadline-string "\\|"
  613. org-closed-string "\\|"
  614. org-clock-string "\\)")
  615. "Matches a line with planning or clock info.")
  616. (defcustom org-comment-string "COMMENT"
  617. "Entries starting with this keyword will never be exported.
  618. An entry can be toggled between COMMENT and normal with
  619. \\[org-toggle-comment].
  620. Changes become only effective after restarting Emacs."
  621. :group 'org-keywords
  622. :type 'string)
  623. (defcustom org-quote-string "QUOTE"
  624. "Entries starting with this keyword will be exported in fixed-width font.
  625. Quoting applies only to the text in the entry following the headline, and does
  626. not extend beyond the next headline, even if that is lower level.
  627. An entry can be toggled between QUOTE and normal with
  628. \\[org-toggle-fixed-width-section]."
  629. :group 'org-keywords
  630. :type 'string)
  631. (defconst org-repeat-re
  632. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  633. "Regular expression for specifying repeated events.
  634. After a match, group 1 contains the repeat expression.")
  635. (defgroup org-structure nil
  636. "Options concerning the general structure of Org-mode files."
  637. :tag "Org Structure"
  638. :group 'org)
  639. (defgroup org-reveal-location nil
  640. "Options about how to make context of a location visible."
  641. :tag "Org Reveal Location"
  642. :group 'org-structure)
  643. (defconst org-context-choice
  644. '(choice
  645. (const :tag "Always" t)
  646. (const :tag "Never" nil)
  647. (repeat :greedy t :tag "Individual contexts"
  648. (cons
  649. (choice :tag "Context"
  650. (const agenda)
  651. (const org-goto)
  652. (const occur-tree)
  653. (const tags-tree)
  654. (const link-search)
  655. (const mark-goto)
  656. (const bookmark-jump)
  657. (const isearch)
  658. (const default))
  659. (boolean))))
  660. "Contexts for the reveal options.")
  661. (defcustom org-show-hierarchy-above '((default . t))
  662. "Non-nil means show full hierarchy when revealing a location.
  663. Org-mode often shows locations in an org-mode file which might have
  664. been invisible before. When this is set, the hierarchy of headings
  665. above the exposed location is shown.
  666. Turning this off for example for sparse trees makes them very compact.
  667. Instead of t, this can also be an alist specifying this option for different
  668. contexts. Valid contexts are
  669. agenda when exposing an entry from the agenda
  670. org-goto when using the command `org-goto' on key C-c C-j
  671. occur-tree when using the command `org-occur' on key C-c /
  672. tags-tree when constructing a sparse tree based on tags matches
  673. link-search when exposing search matches associated with a link
  674. mark-goto when exposing the jump goal of a mark
  675. bookmark-jump when exposing a bookmark location
  676. isearch when exiting from an incremental search
  677. default default for all contexts not set explicitly"
  678. :group 'org-reveal-location
  679. :type org-context-choice)
  680. (defcustom org-show-following-heading '((default . nil))
  681. "Non-nil means show following heading when revealing a location.
  682. Org-mode often shows locations in an org-mode file which might have
  683. been invisible before. When this is set, the heading following the
  684. match is shown.
  685. Turning this off for example for sparse trees makes them very compact,
  686. but makes it harder to edit the location of the match. In such a case,
  687. use the command \\[org-reveal] to show more context.
  688. Instead of t, this can also be an alist specifying this option for different
  689. contexts. See `org-show-hierarchy-above' for valid contexts."
  690. :group 'org-reveal-location
  691. :type org-context-choice)
  692. (defcustom org-show-siblings '((default . nil) (isearch t))
  693. "Non-nil means show all sibling heading when revealing a location.
  694. Org-mode often shows locations in an org-mode file which might have
  695. been invisible before. When this is set, the sibling of the current entry
  696. heading are all made visible. If `org-show-hierarchy-above' is t,
  697. the same happens on each level of the hierarchy above the current entry.
  698. By default this is on for the isearch context, off for all other contexts.
  699. Turning this off for example for sparse trees makes them very compact,
  700. but makes it harder to edit the location of the match. In such a case,
  701. use the command \\[org-reveal] to show more context.
  702. Instead of t, this can also be an alist specifying this option for different
  703. contexts. See `org-show-hierarchy-above' for valid contexts."
  704. :group 'org-reveal-location
  705. :type org-context-choice)
  706. (defcustom org-show-entry-below '((default . nil))
  707. "Non-nil means show the entry below a headline when revealing a location.
  708. Org-mode often shows locations in an org-mode file which might have
  709. been invisible before. When this is set, the text below the headline that is
  710. exposed is also shown.
  711. By default this is off for all contexts.
  712. Instead of t, this can also be an alist specifying this option for different
  713. contexts. See `org-show-hierarchy-above' for valid contexts."
  714. :group 'org-reveal-location
  715. :type org-context-choice)
  716. (defcustom org-indirect-buffer-display 'other-window
  717. "How should indirect tree buffers be displayed?
  718. This applies to indirect buffers created with the commands
  719. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  720. Valid values are:
  721. current-window Display in the current window
  722. other-window Just display in another window.
  723. dedicated-frame Create one new frame, and re-use it each time.
  724. new-frame Make a new frame each time. Note that in this case
  725. previously-made indirect buffers are kept, and you need to
  726. kill these buffers yourself."
  727. :group 'org-structure
  728. :group 'org-agenda-windows
  729. :type '(choice
  730. (const :tag "In current window" current-window)
  731. (const :tag "In current frame, other window" other-window)
  732. (const :tag "Each time a new frame" new-frame)
  733. (const :tag "One dedicated frame" dedicated-frame)))
  734. (defcustom org-use-speed-commands nil
  735. "Non-nil means activate single letter commands at beginning of a headline.
  736. This may also be a function to test for appropriate locations where speed
  737. commands should be active."
  738. :group 'org-structure
  739. :type '(choice
  740. (const :tag "Never" nil)
  741. (const :tag "At beginning of headline stars" t)
  742. (function)))
  743. (defcustom org-speed-commands-user nil
  744. "Alist of additional speed commands.
  745. This list will be checked before `org-speed-commands-default'
  746. when the variable `org-use-speed-commands' is non-nil
  747. and when the cursor is at the beginning of a headline.
  748. The car if each entry is a string with a single letter, which must
  749. be assigned to `self-insert-command' in the global map.
  750. The cdr is either a command to be called interactively, a function
  751. to be called, or a form to be evaluated.
  752. An entry that is just a list with a single string will be interpreted
  753. as a descriptive headline that will be added when listing the speed
  754. commands in the Help buffer using the `?' speed command."
  755. :group 'org-structure
  756. :type '(repeat :value ("k" . ignore)
  757. (choice :value ("k" . ignore)
  758. (list :tag "Descriptive Headline" (string :tag "Headline"))
  759. (cons :tag "Letter and Command"
  760. (string :tag "Command letter")
  761. (choice
  762. (function)
  763. (sexp))))))
  764. (defgroup org-cycle nil
  765. "Options concerning visibility cycling in Org-mode."
  766. :tag "Org Cycle"
  767. :group 'org-structure)
  768. (defcustom org-cycle-skip-children-state-if-no-children t
  769. "Non-nil means skip CHILDREN state in entries that don't have any."
  770. :group 'org-cycle
  771. :type 'boolean)
  772. (defcustom org-cycle-max-level nil
  773. "Maximum level which should still be subject to visibility cycling.
  774. Levels higher than this will, for cycling, be treated as text, not a headline.
  775. When `org-odd-levels-only' is set, a value of N in this variable actually
  776. means 2N-1 stars as the limiting headline.
  777. When nil, cycle all levels.
  778. Note that the limiting level of cycling is also influenced by
  779. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  780. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  781. than its value."
  782. :group 'org-cycle
  783. :type '(choice
  784. (const :tag "No limit" nil)
  785. (integer :tag "Maximum level")))
  786. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  787. "Names of drawers. Drawers are not opened by cycling on the headline above.
  788. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  789. this:
  790. :DRAWERNAME:
  791. .....
  792. :END:
  793. The drawer \"PROPERTIES\" is special for capturing properties through
  794. the property API.
  795. Drawers can be defined on the per-file basis with a line like:
  796. #+DRAWERS: HIDDEN STATE PROPERTIES"
  797. :group 'org-structure
  798. :group 'org-cycle
  799. :type '(repeat (string :tag "Drawer Name")))
  800. (defcustom org-hide-block-startup nil
  801. "Non-nil means entering Org-mode will fold all blocks.
  802. This can also be set in on a per-file basis with
  803. #+STARTUP: hideblocks
  804. #+STARTUP: showblocks"
  805. :group 'org-startup
  806. :group 'org-cycle
  807. :type 'boolean)
  808. (defcustom org-cycle-global-at-bob nil
  809. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  810. This makes it possible to do global cycling without having to use S-TAB or
  811. \\[universal-argument] TAB. For this special case to work, the first line
  812. of the buffer must not be a headline -- it may be empty or some other text.
  813. When used in this way, `org-cycle-hook' is disabled temporarily to make
  814. sure the cursor stays at the beginning of the buffer. When this option is
  815. nil, don't do anything special at the beginning of the buffer."
  816. :group 'org-cycle
  817. :type 'boolean)
  818. (defcustom org-cycle-level-after-item/entry-creation t
  819. "Non-nil means cycle entry level or item indentation in new empty entries.
  820. When the cursor is at the end of an empty headline, i.e with only stars
  821. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  822. and then all possible ancestor states, before returning to the original state.
  823. This makes data entry extremely fast: M-RET to create a new headline,
  824. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  825. When the cursor is at the end of an empty plain list item, one TAB will
  826. make it a subitem, two or more tabs will back up to make this an item
  827. higher up in the item hierarchy."
  828. :group 'org-cycle
  829. :type 'boolean)
  830. (defcustom org-cycle-emulate-tab t
  831. "Where should `org-cycle' emulate TAB.
  832. nil Never
  833. white Only in completely white lines
  834. whitestart Only at the beginning of lines, before the first non-white char
  835. t Everywhere except in headlines
  836. exc-hl-bol Everywhere except at the start of a headline
  837. If TAB is used in a place where it does not emulate TAB, the current subtree
  838. visibility is cycled."
  839. :group 'org-cycle
  840. :type '(choice (const :tag "Never" nil)
  841. (const :tag "Only in completely white lines" white)
  842. (const :tag "Before first char in a line" whitestart)
  843. (const :tag "Everywhere except in headlines" t)
  844. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  845. ))
  846. (defcustom org-cycle-separator-lines 2
  847. "Number of empty lines needed to keep an empty line between collapsed trees.
  848. If you leave an empty line between the end of a subtree and the following
  849. headline, this empty line is hidden when the subtree is folded.
  850. Org-mode will leave (exactly) one empty line visible if the number of
  851. empty lines is equal or larger to the number given in this variable.
  852. So the default 2 means at least 2 empty lines after the end of a subtree
  853. are needed to produce free space between a collapsed subtree and the
  854. following headline.
  855. If the number is negative, and the number of empty lines is at least -N,
  856. all empty lines are shown.
  857. Special case: when 0, never leave empty lines in collapsed view."
  858. :group 'org-cycle
  859. :type 'integer)
  860. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  861. (defcustom org-pre-cycle-hook nil
  862. "Hook that is run before visibility cycling is happening.
  863. The function(s) in this hook must accept a single argument which indicates
  864. the new state that will be set right after running this hook. The
  865. argument is a symbol. Before a global state change, it can have the values
  866. `overview', `content', or `all'. Before a local state change, it can have
  867. the values `folded', `children', or `subtree'."
  868. :group 'org-cycle
  869. :type 'hook)
  870. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  871. org-cycle-hide-drawers
  872. org-cycle-show-empty-lines
  873. org-optimize-window-after-visibility-change)
  874. "Hook that is run after `org-cycle' has changed the buffer visibility.
  875. The function(s) in this hook must accept a single argument which indicates
  876. the new state that was set by the most recent `org-cycle' command. The
  877. argument is a symbol. After a global state change, it can have the values
  878. `overview', `contents', or `all'. After a local state change, it can have
  879. the values `folded', `children', or `subtree'."
  880. :group 'org-cycle
  881. :type 'hook)
  882. (defgroup org-edit-structure nil
  883. "Options concerning structure editing in Org-mode."
  884. :tag "Org Edit Structure"
  885. :group 'org-structure)
  886. (defcustom org-odd-levels-only nil
  887. "Non-nil means skip even levels and only use odd levels for the outline.
  888. This has the effect that two stars are being added/taken away in
  889. promotion/demotion commands. It also influences how levels are
  890. handled by the exporters.
  891. Changing it requires restart of `font-lock-mode' to become effective
  892. for fontification also in regions already fontified.
  893. You may also set this on a per-file basis by adding one of the following
  894. lines to the buffer:
  895. #+STARTUP: odd
  896. #+STARTUP: oddeven"
  897. :group 'org-edit-structure
  898. :group 'org-appearance
  899. :type 'boolean)
  900. (defcustom org-adapt-indentation t
  901. "Non-nil means adapt indentation to outline node level.
  902. When this variable is set, Org assumes that you write outlines by
  903. indenting text in each node to align with the headline (after the stars).
  904. The following issues are influenced by this variable:
  905. - When this is set and the *entire* text in an entry is indented, the
  906. indentation is increased by one space in a demotion command, and
  907. decreased by one in a promotion command. If any line in the entry
  908. body starts with text at column 0, indentation is not changed at all.
  909. - Property drawers and planning information is inserted indented when
  910. this variable s set. When nil, they will not be indented.
  911. - TAB indents a line relative to context. The lines below a headline
  912. will be indented when this variable is set.
  913. Note that this is all about true indentation, by adding and removing
  914. space characters. See also `org-indent.el' which does level-dependent
  915. indentation in a virtual way, i.e. at display time in Emacs."
  916. :group 'org-edit-structure
  917. :type 'boolean)
  918. (defcustom org-special-ctrl-a/e nil
  919. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  920. When t, `C-a' will bring back the cursor to the beginning of the
  921. headline text, i.e. after the stars and after a possible TODO
  922. keyword. In an item, this will be the position after bullet and
  923. check-box, if any. When the cursor is already at that position,
  924. another `C-a' will bring it to the beginning of the line.
  925. `C-e' will jump to the end of the headline, ignoring the presence
  926. of tags in the headline. A second `C-e' will then jump to the
  927. true end of the line, after any tags. This also means that, when
  928. this variable is non-nil, `C-e' also will never jump beyond the
  929. end of the heading of a folded section, i.e. not after the
  930. ellipses.
  931. When set to the symbol `reversed', the first `C-a' or `C-e' works
  932. normally, going to the true line boundary first. Only a directly
  933. following, identical keypress will bring the cursor to the
  934. special positions.
  935. This may also be a cons cell where the behavior for `C-a' and
  936. `C-e' is set separately."
  937. :group 'org-edit-structure
  938. :type '(choice
  939. (const :tag "off" nil)
  940. (const :tag "on: after stars/bullet and before tags first" t)
  941. (const :tag "reversed: true line boundary first" reversed)
  942. (cons :tag "Set C-a and C-e separately"
  943. (choice :tag "Special C-a"
  944. (const :tag "off" nil)
  945. (const :tag "on: after stars/bullet first" t)
  946. (const :tag "reversed: before stars/bullet first" reversed))
  947. (choice :tag "Special C-e"
  948. (const :tag "off" nil)
  949. (const :tag "on: before tags first" t)
  950. (const :tag "reversed: after tags first" reversed)))))
  951. (if (fboundp 'defvaralias)
  952. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  953. (defcustom org-special-ctrl-k nil
  954. "Non-nil means `C-k' will behave specially in headlines.
  955. When nil, `C-k' will call the default `kill-line' command.
  956. When t, the following will happen while the cursor is in the headline:
  957. - When the cursor is at the beginning of a headline, kill the entire
  958. line and possible the folded subtree below the line.
  959. - When in the middle of the headline text, kill the headline up to the tags.
  960. - When after the headline text, kill the tags."
  961. :group 'org-edit-structure
  962. :type 'boolean)
  963. (defcustom org-ctrl-k-protect-subtree nil
  964. "Non-nil means, do not delete a hidden subtree with C-k.
  965. When set to the symbol `error', simply throw an error when C-k is
  966. used to kill (part-of) a headline that has hidden text behind it.
  967. Any other non-nil value will result in a query to the user, if it is
  968. OK to kill that hidden subtree. When nil, kill without remorse."
  969. :group 'org-edit-structure
  970. :version "24.1"
  971. :type '(choice
  972. (const :tag "Do not protect hidden subtrees" nil)
  973. (const :tag "Protect hidden subtrees with a security query" t)
  974. (const :tag "Never kill a hidden subtree with C-k" error)))
  975. (defcustom org-catch-invisible-edits nil
  976. "Check if in invisible region before inserting or deleting a character.
  977. Valid values are:
  978. nil Do not check, so just do invisible edits.
  979. error Throw an error and do nothing.
  980. show Make point visible, and do the requested edit.
  981. show-and-error Make point visible, then throw an error and abort the edit.
  982. smart Make point visible, and do insertion/deletion if it is
  983. adjacent to visible text and the change feels predictable.
  984. Never delete a previously invisible character or add in the
  985. middle or right after an invisible region. Basically, this
  986. allows insertion and backward-delete right before ellipses.
  987. FIXME: maybe in this case we should not even show?"
  988. :group 'org-edit-structure
  989. :version "24.1"
  990. :type '(choice
  991. (const :tag "Do not check" nil)
  992. (const :tag "Throw error when trying to edit" error)
  993. (const :tag "Unhide, but do not do the edit" show-and-error)
  994. (const :tag "Show invisible part and do the edit" show)
  995. (const :tag "Be smart and do the right thing" smart)))
  996. (defcustom org-yank-folded-subtrees t
  997. "Non-nil means when yanking subtrees, fold them.
  998. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  999. it starts with a heading and all other headings in it are either children
  1000. or siblings, then fold all the subtrees. However, do this only if no
  1001. text after the yank would be swallowed into a folded tree by this action."
  1002. :group 'org-edit-structure
  1003. :type 'boolean)
  1004. (defcustom org-yank-adjusted-subtrees nil
  1005. "Non-nil means when yanking subtrees, adjust the level.
  1006. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1007. this function for details."
  1008. :group 'org-edit-structure
  1009. :type 'boolean)
  1010. (defcustom org-M-RET-may-split-line '((default . t))
  1011. "Non-nil means M-RET will split the line at the cursor position.
  1012. When nil, it will go to the end of the line before making a
  1013. new line.
  1014. You may also set this option in a different way for different
  1015. contexts. Valid contexts are:
  1016. headline when creating a new headline
  1017. item when creating a new item
  1018. table in a table field
  1019. default the value to be used for all contexts not explicitly
  1020. customized"
  1021. :group 'org-structure
  1022. :group 'org-table
  1023. :type '(choice
  1024. (const :tag "Always" t)
  1025. (const :tag "Never" nil)
  1026. (repeat :greedy t :tag "Individual contexts"
  1027. (cons
  1028. (choice :tag "Context"
  1029. (const headline)
  1030. (const item)
  1031. (const table)
  1032. (const default))
  1033. (boolean)))))
  1034. (defcustom org-insert-heading-respect-content nil
  1035. "Non-nil means insert new headings after the current subtree.
  1036. When nil, the new heading is created directly after the current line.
  1037. The commands \\[org-insert-heading-respect-content] and
  1038. \\[org-insert-todo-heading-respect-content] turn this variable on
  1039. for the duration of the command."
  1040. :group 'org-structure
  1041. :type 'boolean)
  1042. (defcustom org-blank-before-new-entry '((heading . auto)
  1043. (plain-list-item . auto))
  1044. "Should `org-insert-heading' leave a blank line before new heading/item?
  1045. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1046. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1047. which case Org will look at the surrounding headings/items and try to
  1048. make an intelligent decision whether to insert a blank line or not.
  1049. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1050. set, the setting here is ignored and no empty line is inserted, to avoid
  1051. breaking the list structure."
  1052. :group 'org-edit-structure
  1053. :type '(list
  1054. (cons (const heading)
  1055. (choice (const :tag "Never" nil)
  1056. (const :tag "Always" t)
  1057. (const :tag "Auto" auto)))
  1058. (cons (const plain-list-item)
  1059. (choice (const :tag "Never" nil)
  1060. (const :tag "Always" t)
  1061. (const :tag "Auto" auto)))))
  1062. (defcustom org-insert-heading-hook nil
  1063. "Hook being run after inserting a new heading."
  1064. :group 'org-edit-structure
  1065. :type 'hook)
  1066. (defcustom org-enable-fixed-width-editor t
  1067. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1068. This currently only means they are never auto-wrapped.
  1069. When nil, such lines will be treated like ordinary lines.
  1070. See also the QUOTE keyword."
  1071. :group 'org-edit-structure
  1072. :type 'boolean)
  1073. (defcustom org-goto-auto-isearch t
  1074. "Non-nil means typing characters in `org-goto' starts incremental search."
  1075. :group 'org-edit-structure
  1076. :type 'boolean)
  1077. (defgroup org-sparse-trees nil
  1078. "Options concerning sparse trees in Org-mode."
  1079. :tag "Org Sparse Trees"
  1080. :group 'org-structure)
  1081. (defcustom org-highlight-sparse-tree-matches t
  1082. "Non-nil means highlight all matches that define a sparse tree.
  1083. The highlights will automatically disappear the next time the buffer is
  1084. changed by an edit command."
  1085. :group 'org-sparse-trees
  1086. :type 'boolean)
  1087. (defcustom org-remove-highlights-with-change t
  1088. "Non-nil means any change to the buffer will remove temporary highlights.
  1089. Such highlights are created by `org-occur' and `org-clock-display'.
  1090. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1091. The highlights created by `org-preview-latex-fragment' always need
  1092. `C-c C-c' to be removed."
  1093. :group 'org-sparse-trees
  1094. :group 'org-time
  1095. :type 'boolean)
  1096. (defcustom org-occur-hook '(org-first-headline-recenter)
  1097. "Hook that is run after `org-occur' has constructed a sparse tree.
  1098. This can be used to recenter the window to show as much of the structure
  1099. as possible."
  1100. :group 'org-sparse-trees
  1101. :type 'hook)
  1102. (defgroup org-imenu-and-speedbar nil
  1103. "Options concerning imenu and speedbar in Org-mode."
  1104. :tag "Org Imenu and Speedbar"
  1105. :group 'org-structure)
  1106. (defcustom org-imenu-depth 2
  1107. "The maximum level for Imenu access to Org-mode headlines.
  1108. This also applied for speedbar access."
  1109. :group 'org-imenu-and-speedbar
  1110. :type 'integer)
  1111. (defgroup org-table nil
  1112. "Options concerning tables in Org-mode."
  1113. :tag "Org Table"
  1114. :group 'org)
  1115. (defcustom org-enable-table-editor 'optimized
  1116. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1117. When nil, such lines will be treated like ordinary lines.
  1118. When equal to the symbol `optimized', the table editor will be optimized to
  1119. do the following:
  1120. - Automatic overwrite mode in front of whitespace in table fields.
  1121. This makes the structure of the table stay in tact as long as the edited
  1122. field does not exceed the column width.
  1123. - Minimize the number of realigns. Normally, the table is aligned each time
  1124. TAB or RET are pressed to move to another field. With optimization this
  1125. happens only if changes to a field might have changed the column width.
  1126. Optimization requires replacing the functions `self-insert-command',
  1127. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1128. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1129. very good at guessing when a re-align will be necessary, but you can always
  1130. force one with \\[org-ctrl-c-ctrl-c].
  1131. If you would like to use the optimized version in Org-mode, but the
  1132. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1133. This variable can be used to turn on and off the table editor during a session,
  1134. but in order to toggle optimization, a restart is required.
  1135. See also the variable `org-table-auto-blank-field'."
  1136. :group 'org-table
  1137. :type '(choice
  1138. (const :tag "off" nil)
  1139. (const :tag "on" t)
  1140. (const :tag "on, optimized" optimized)))
  1141. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1142. (version<= emacs-version "24.1"))
  1143. "Non-nil means cluster self-insert commands for undo when possible.
  1144. If this is set, then, like in the Emacs command loop, 20 consecutive
  1145. characters will be undone together.
  1146. This is configurable, because there is some impact on typing performance."
  1147. :group 'org-table
  1148. :type 'boolean)
  1149. (defcustom org-table-tab-recognizes-table.el t
  1150. "Non-nil means TAB will automatically notice a table.el table.
  1151. When it sees such a table, it moves point into it and - if necessary -
  1152. calls `table-recognize-table'."
  1153. :group 'org-table-editing
  1154. :type 'boolean)
  1155. (defgroup org-link nil
  1156. "Options concerning links in Org-mode."
  1157. :tag "Org Link"
  1158. :group 'org)
  1159. (defvar org-link-abbrev-alist-local nil
  1160. "Buffer-local version of `org-link-abbrev-alist', which see.
  1161. The value of this is taken from the #+LINK lines.")
  1162. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1163. (defcustom org-link-abbrev-alist nil
  1164. "Alist of link abbreviations.
  1165. The car of each element is a string, to be replaced at the start of a link.
  1166. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1167. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1168. [[linkkey:tag][description]]
  1169. The 'linkkey' must be a word word, starting with a letter, followed
  1170. by letters, numbers, '-' or '_'.
  1171. If REPLACE is a string, the tag will simply be appended to create the link.
  1172. If the string contains \"%s\", the tag will be inserted there. If the string
  1173. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1174. at that point (see the function `url-hexify-string'). If the string contains
  1175. the specifier \"%(my-function)\", then the custom function `my-function' will
  1176. be invoked: this function takes the tag as its only argument and must return
  1177. a string.
  1178. REPLACE may also be a function that will be called with the tag as the
  1179. only argument to create the link, which should be returned as a string.
  1180. See the manual for examples."
  1181. :group 'org-link
  1182. :type '(repeat
  1183. (cons
  1184. (string :tag "Protocol")
  1185. (choice
  1186. (string :tag "Format")
  1187. (function)))))
  1188. (defcustom org-descriptive-links t
  1189. "Non-nil means Org will display descriptive links.
  1190. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1191. \"Org Website\", hiding the link itself and just displaying its
  1192. description. When set to `nil', Org will display the full links
  1193. literally.
  1194. You can interactively set the value of this variable by calling
  1195. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1196. :group 'org-link
  1197. :type 'boolean)
  1198. (defcustom org-link-file-path-type 'adaptive
  1199. "How the path name in file links should be stored.
  1200. Valid values are:
  1201. relative Relative to the current directory, i.e. the directory of the file
  1202. into which the link is being inserted.
  1203. absolute Absolute path, if possible with ~ for home directory.
  1204. noabbrev Absolute path, no abbreviation of home directory.
  1205. adaptive Use relative path for files in the current directory and sub-
  1206. directories of it. For other files, use an absolute path."
  1207. :group 'org-link
  1208. :type '(choice
  1209. (const relative)
  1210. (const absolute)
  1211. (const noabbrev)
  1212. (const adaptive)))
  1213. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1214. "Types of links that should be activated in Org-mode files.
  1215. This is a list of symbols, each leading to the activation of a certain link
  1216. type. In principle, it does not hurt to turn on most link types - there may
  1217. be a small gain when turning off unused link types. The types are:
  1218. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1219. angle Links in angular brackets that may contain whitespace like
  1220. <bbdb:Carsten Dominik>.
  1221. plain Plain links in normal text, no whitespace, like http://google.com.
  1222. radio Text that is matched by a radio target, see manual for details.
  1223. tag Tag settings in a headline (link to tag search).
  1224. date Time stamps (link to calendar).
  1225. footnote Footnote labels.
  1226. Changing this variable requires a restart of Emacs to become effective."
  1227. :group 'org-link
  1228. :type '(set :greedy t
  1229. (const :tag "Double bracket links" bracket)
  1230. (const :tag "Angular bracket links" angle)
  1231. (const :tag "Plain text links" plain)
  1232. (const :tag "Radio target matches" radio)
  1233. (const :tag "Tags" tag)
  1234. (const :tag "Timestamps" date)
  1235. (const :tag "Footnotes" footnote)))
  1236. (defcustom org-make-link-description-function nil
  1237. "Function to use for generating link descriptions from links.
  1238. When nil, the link location will be used. This function must take
  1239. two parameters: the first one is the link, the second one is the
  1240. description generated by `org-insert-link'. The function should
  1241. return the description to use."
  1242. :group 'org-link
  1243. :type 'function)
  1244. (defgroup org-link-store nil
  1245. "Options concerning storing links in Org-mode."
  1246. :tag "Org Store Link"
  1247. :group 'org-link)
  1248. (defcustom org-url-hexify-p t
  1249. "When non-nil, hexify URL when creating a link."
  1250. :type 'boolean
  1251. :version "24.3"
  1252. :group 'org-link-store)
  1253. (defcustom org-email-link-description-format "Email %c: %.30s"
  1254. "Format of the description part of a link to an email or usenet message.
  1255. The following %-escapes will be replaced by corresponding information:
  1256. %F full \"From\" field
  1257. %f name, taken from \"From\" field, address if no name
  1258. %T full \"To\" field
  1259. %t first name in \"To\" field, address if no name
  1260. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1261. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1262. %s subject
  1263. %d date
  1264. %m message-id.
  1265. You may use normal field width specification between the % and the letter.
  1266. This is for example useful to limit the length of the subject.
  1267. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1268. :group 'org-link-store
  1269. :type 'string)
  1270. (defcustom org-from-is-user-regexp
  1271. (let (r1 r2)
  1272. (when (and user-mail-address (not (string= user-mail-address "")))
  1273. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1274. (when (and user-full-name (not (string= user-full-name "")))
  1275. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1276. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1277. "Regexp matched against the \"From:\" header of an email or usenet message.
  1278. It should match if the message is from the user him/herself."
  1279. :group 'org-link-store
  1280. :type 'regexp)
  1281. (defcustom org-context-in-file-links t
  1282. "Non-nil means file links from `org-store-link' contain context.
  1283. A search string will be added to the file name with :: as separator and
  1284. used to find the context when the link is activated by the command
  1285. `org-open-at-point'. When this option is t, the entire active region
  1286. will be placed in the search string of the file link. If set to a
  1287. positive integer, only the first n lines of context will be stored.
  1288. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1289. negates this setting for the duration of the command."
  1290. :group 'org-link-store
  1291. :type '(choice boolean integer))
  1292. (defcustom org-keep-stored-link-after-insertion nil
  1293. "Non-nil means keep link in list for entire session.
  1294. The command `org-store-link' adds a link pointing to the current
  1295. location to an internal list. These links accumulate during a session.
  1296. The command `org-insert-link' can be used to insert links into any
  1297. Org-mode file (offering completion for all stored links). When this
  1298. option is nil, every link which has been inserted once using \\[org-insert-link]
  1299. will be removed from the list, to make completing the unused links
  1300. more efficient."
  1301. :group 'org-link-store
  1302. :type 'boolean)
  1303. (defgroup org-link-follow nil
  1304. "Options concerning following links in Org-mode."
  1305. :tag "Org Follow Link"
  1306. :group 'org-link)
  1307. (defcustom org-link-translation-function nil
  1308. "Function to translate links with different syntax to Org syntax.
  1309. This can be used to translate links created for example by the Planner
  1310. or emacs-wiki packages to Org syntax.
  1311. The function must accept two parameters, a TYPE containing the link
  1312. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1313. which is everything after the link protocol. It should return a cons
  1314. with possibly modified values of type and path.
  1315. Org contains a function for this, so if you set this variable to
  1316. `org-translate-link-from-planner', you should be able follow many
  1317. links created by planner."
  1318. :group 'org-link-follow
  1319. :type 'function)
  1320. (defcustom org-follow-link-hook nil
  1321. "Hook that is run after a link has been followed."
  1322. :group 'org-link-follow
  1323. :type 'hook)
  1324. (defcustom org-tab-follows-link nil
  1325. "Non-nil means on links TAB will follow the link.
  1326. Needs to be set before org.el is loaded.
  1327. This really should not be used, it does not make sense, and the
  1328. implementation is bad."
  1329. :group 'org-link-follow
  1330. :type 'boolean)
  1331. (defcustom org-return-follows-link nil
  1332. "Non-nil means on links RET will follow the link."
  1333. :group 'org-link-follow
  1334. :type 'boolean)
  1335. (defcustom org-mouse-1-follows-link
  1336. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1337. "Non-nil means mouse-1 on a link will follow the link.
  1338. A longer mouse click will still set point. Does not work on XEmacs.
  1339. Needs to be set before org.el is loaded."
  1340. :group 'org-link-follow
  1341. :type 'boolean)
  1342. (defcustom org-mark-ring-length 4
  1343. "Number of different positions to be recorded in the ring.
  1344. Changing this requires a restart of Emacs to work correctly."
  1345. :group 'org-link-follow
  1346. :type 'integer)
  1347. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1348. "Non-nil means internal links in Org files must exactly match a headline.
  1349. When nil, the link search tries to match a phrase with all words
  1350. in the search text."
  1351. :group 'org-link-follow
  1352. :version "24.1"
  1353. :type '(choice
  1354. (const :tag "Use fuzzy text search" nil)
  1355. (const :tag "Match only exact headline" t)
  1356. (const :tag "Match exact headline or query to create it"
  1357. query-to-create)))
  1358. (defcustom org-link-frame-setup
  1359. '((vm . vm-visit-folder-other-frame)
  1360. (vm-imap . vm-visit-imap-folder-other-frame)
  1361. (gnus . org-gnus-no-new-news)
  1362. (file . find-file-other-window)
  1363. (wl . wl-other-frame))
  1364. "Setup the frame configuration for following links.
  1365. When following a link with Emacs, it may often be useful to display
  1366. this link in another window or frame. This variable can be used to
  1367. set this up for the different types of links.
  1368. For VM, use any of
  1369. `vm-visit-folder'
  1370. `vm-visit-folder-other-window'
  1371. `vm-visit-folder-other-frame'
  1372. For Gnus, use any of
  1373. `gnus'
  1374. `gnus-other-frame'
  1375. `org-gnus-no-new-news'
  1376. For FILE, use any of
  1377. `find-file'
  1378. `find-file-other-window'
  1379. `find-file-other-frame'
  1380. For Wanderlust use any of
  1381. `wl'
  1382. `wl-other-frame'
  1383. For the calendar, use the variable `calendar-setup'.
  1384. For BBDB, it is currently only possible to display the matches in
  1385. another window."
  1386. :group 'org-link-follow
  1387. :type '(list
  1388. (cons (const vm)
  1389. (choice
  1390. (const vm-visit-folder)
  1391. (const vm-visit-folder-other-window)
  1392. (const vm-visit-folder-other-frame)))
  1393. (cons (const gnus)
  1394. (choice
  1395. (const gnus)
  1396. (const gnus-other-frame)
  1397. (const org-gnus-no-new-news)))
  1398. (cons (const file)
  1399. (choice
  1400. (const find-file)
  1401. (const find-file-other-window)
  1402. (const find-file-other-frame)))
  1403. (cons (const wl)
  1404. (choice
  1405. (const wl)
  1406. (const wl-other-frame)))))
  1407. (defcustom org-display-internal-link-with-indirect-buffer nil
  1408. "Non-nil means use indirect buffer to display infile links.
  1409. Activating internal links (from one location in a file to another location
  1410. in the same file) normally just jumps to the location. When the link is
  1411. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1412. is displayed in
  1413. another window. When this option is set, the other window actually displays
  1414. an indirect buffer clone of the current buffer, to avoid any visibility
  1415. changes to the current buffer."
  1416. :group 'org-link-follow
  1417. :type 'boolean)
  1418. (defcustom org-open-non-existing-files nil
  1419. "Non-nil means `org-open-file' will open non-existing files.
  1420. When nil, an error will be generated.
  1421. This variable applies only to external applications because they
  1422. might choke on non-existing files. If the link is to a file that
  1423. will be opened in Emacs, the variable is ignored."
  1424. :group 'org-link-follow
  1425. :type 'boolean)
  1426. (defcustom org-open-directory-means-index-dot-org nil
  1427. "Non-nil means a link to a directory really means to index.org.
  1428. When nil, following a directory link will run dired or open a finder/explorer
  1429. window on that directory."
  1430. :group 'org-link-follow
  1431. :type 'boolean)
  1432. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1433. "Function and arguments to call for following mailto links.
  1434. This is a list with the first element being a Lisp function, and the
  1435. remaining elements being arguments to the function. In string arguments,
  1436. %a will be replaced by the address, and %s will be replaced by the subject
  1437. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1438. :group 'org-link-follow
  1439. :type '(choice
  1440. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1441. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1442. (const :tag "message-mail" (message-mail "%a" "%s"))
  1443. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1444. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1445. "Non-nil means ask for confirmation before executing shell links.
  1446. Shell links can be dangerous: just think about a link
  1447. [[shell:rm -rf ~/*][Google Search]]
  1448. This link would show up in your Org-mode document as \"Google Search\",
  1449. but really it would remove your entire home directory.
  1450. Therefore we advise against setting this variable to nil.
  1451. Just change it to `y-or-n-p' if you want to confirm with a
  1452. single keystroke rather than having to type \"yes\"."
  1453. :group 'org-link-follow
  1454. :type '(choice
  1455. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1456. (const :tag "with y-or-n (faster)" y-or-n-p)
  1457. (const :tag "no confirmation (dangerous)" nil)))
  1458. (put 'org-confirm-shell-link-function
  1459. 'safe-local-variable
  1460. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1461. (defcustom org-confirm-shell-link-not-regexp ""
  1462. "A regexp to skip confirmation for shell links."
  1463. :group 'org-link-follow
  1464. :version "24.1"
  1465. :type 'regexp)
  1466. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1467. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1468. Elisp links can be dangerous: just think about a link
  1469. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1470. This link would show up in your Org-mode document as \"Google Search\",
  1471. but really it would remove your entire home directory.
  1472. Therefore we advise against setting this variable to nil.
  1473. Just change it to `y-or-n-p' if you want to confirm with a
  1474. single keystroke rather than having to type \"yes\"."
  1475. :group 'org-link-follow
  1476. :type '(choice
  1477. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1478. (const :tag "with y-or-n (faster)" y-or-n-p)
  1479. (const :tag "no confirmation (dangerous)" nil)))
  1480. (put 'org-confirm-shell-link-function
  1481. 'safe-local-variable
  1482. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1483. (defcustom org-confirm-elisp-link-not-regexp ""
  1484. "A regexp to skip confirmation for Elisp links."
  1485. :group 'org-link-follow
  1486. :version "24.1"
  1487. :type 'regexp)
  1488. (defconst org-file-apps-defaults-gnu
  1489. '((remote . emacs)
  1490. (system . mailcap)
  1491. (t . mailcap))
  1492. "Default file applications on a UNIX or GNU/Linux system.
  1493. See `org-file-apps'.")
  1494. (defconst org-file-apps-defaults-macosx
  1495. '((remote . emacs)
  1496. (t . "open %s")
  1497. (system . "open %s")
  1498. ("ps.gz" . "gv %s")
  1499. ("eps.gz" . "gv %s")
  1500. ("dvi" . "xdvi %s")
  1501. ("fig" . "xfig %s"))
  1502. "Default file applications on a MacOS X system.
  1503. The system \"open\" is known as a default, but we use X11 applications
  1504. for some files for which the OS does not have a good default.
  1505. See `org-file-apps'.")
  1506. (defconst org-file-apps-defaults-windowsnt
  1507. (list
  1508. '(remote . emacs)
  1509. (cons t
  1510. (list (if (featurep 'xemacs)
  1511. 'mswindows-shell-execute
  1512. 'w32-shell-execute)
  1513. "open" 'file))
  1514. (cons 'system
  1515. (list (if (featurep 'xemacs)
  1516. 'mswindows-shell-execute
  1517. 'w32-shell-execute)
  1518. "open" 'file)))
  1519. "Default file applications on a Windows NT system.
  1520. The system \"open\" is used for most files.
  1521. See `org-file-apps'.")
  1522. (defcustom org-file-apps
  1523. '(
  1524. (auto-mode . emacs)
  1525. ("\\.mm\\'" . default)
  1526. ("\\.x?html?\\'" . default)
  1527. ("\\.pdf\\'" . default)
  1528. )
  1529. "External applications for opening `file:path' items in a document.
  1530. Org-mode uses system defaults for different file types, but
  1531. you can use this variable to set the application for a given file
  1532. extension. The entries in this list are cons cells where the car identifies
  1533. files and the cdr the corresponding command. Possible values for the
  1534. file identifier are
  1535. \"string\" A string as a file identifier can be interpreted in different
  1536. ways, depending on its contents:
  1537. - Alphanumeric characters only:
  1538. Match links with this file extension.
  1539. Example: (\"pdf\" . \"evince %s\")
  1540. to open PDFs with evince.
  1541. - Regular expression: Match links where the
  1542. filename matches the regexp. If you want to
  1543. use groups here, use shy groups.
  1544. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1545. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1546. to open *.html and *.xhtml with firefox.
  1547. - Regular expression which contains (non-shy) groups:
  1548. Match links where the whole link, including \"::\", and
  1549. anything after that, matches the regexp.
  1550. In a custom command string, %1, %2, etc. are replaced with
  1551. the parts of the link that were matched by the groups.
  1552. For backwards compatibility, if a command string is given
  1553. that does not use any of the group matches, this case is
  1554. handled identically to the second one (i.e. match against
  1555. file name only).
  1556. In a custom lisp form, you can access the group matches with
  1557. (match-string n link).
  1558. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1559. to open [[file:document.pdf::5]] with evince at page 5.
  1560. `directory' Matches a directory
  1561. `remote' Matches a remote file, accessible through tramp or efs.
  1562. Remote files most likely should be visited through Emacs
  1563. because external applications cannot handle such paths.
  1564. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1565. so all files Emacs knows how to handle. Using this with
  1566. command `emacs' will open most files in Emacs. Beware that this
  1567. will also open html files inside Emacs, unless you add
  1568. (\"html\" . default) to the list as well.
  1569. t Default for files not matched by any of the other options.
  1570. `system' The system command to open files, like `open' on Windows
  1571. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1572. that will be selected if you call `C-c C-o' with a double
  1573. \\[universal-argument] \\[universal-argument] prefix.
  1574. Possible values for the command are:
  1575. `emacs' The file will be visited by the current Emacs process.
  1576. `default' Use the default application for this file type, which is the
  1577. association for t in the list, most likely in the system-specific
  1578. part.
  1579. This can be used to overrule an unwanted setting in the
  1580. system-specific variable.
  1581. `system' Use the system command for opening files, like \"open\".
  1582. This command is specified by the entry whose car is `system'.
  1583. Most likely, the system-specific version of this variable
  1584. does define this command, but you can overrule/replace it
  1585. here.
  1586. string A command to be executed by a shell; %s will be replaced
  1587. by the path to the file.
  1588. sexp A Lisp form which will be evaluated. The file path will
  1589. be available in the Lisp variable `file'.
  1590. For more examples, see the system specific constants
  1591. `org-file-apps-defaults-macosx'
  1592. `org-file-apps-defaults-windowsnt'
  1593. `org-file-apps-defaults-gnu'."
  1594. :group 'org-link-follow
  1595. :type '(repeat
  1596. (cons (choice :value ""
  1597. (string :tag "Extension")
  1598. (const :tag "System command to open files" system)
  1599. (const :tag "Default for unrecognized files" t)
  1600. (const :tag "Remote file" remote)
  1601. (const :tag "Links to a directory" directory)
  1602. (const :tag "Any files that have Emacs modes"
  1603. auto-mode))
  1604. (choice :value ""
  1605. (const :tag "Visit with Emacs" emacs)
  1606. (const :tag "Use default" default)
  1607. (const :tag "Use the system command" system)
  1608. (string :tag "Command")
  1609. (sexp :tag "Lisp form")))))
  1610. (defcustom org-doi-server-url "http://dx.doi.org/"
  1611. "The URL of the DOI server."
  1612. :type 'string
  1613. :version "24.3"
  1614. :group 'org-link-follow)
  1615. (defgroup org-refile nil
  1616. "Options concerning refiling entries in Org-mode."
  1617. :tag "Org Refile"
  1618. :group 'org)
  1619. (defcustom org-directory "~/org"
  1620. "Directory with org files.
  1621. This is just a default location to look for Org files. There is no need
  1622. at all to put your files into this directory. It is only used in the
  1623. following situations:
  1624. 1. When a capture template specifies a target file that is not an
  1625. absolute path. The path will then be interpreted relative to
  1626. `org-directory'
  1627. 2. When a capture note is filed away in an interactive way (when exiting the
  1628. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1629. with `org-directory' as the default path."
  1630. :group 'org-refile
  1631. :group 'org-remember
  1632. :group 'org-capture
  1633. :type 'directory)
  1634. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1635. "Default target for storing notes.
  1636. Used as a fall back file for org-remember.el and org-capture.el, for
  1637. templates that do not specify a target file."
  1638. :group 'org-refile
  1639. :group 'org-remember
  1640. :group 'org-capture
  1641. :type '(choice
  1642. (const :tag "Default from remember-data-file" nil)
  1643. file))
  1644. (defcustom org-goto-interface 'outline
  1645. "The default interface to be used for `org-goto'.
  1646. Allowed values are:
  1647. outline The interface shows an outline of the relevant file
  1648. and the correct heading is found by moving through
  1649. the outline or by searching with incremental search.
  1650. outline-path-completion Headlines in the current buffer are offered via
  1651. completion. This is the interface also used by
  1652. the refile command."
  1653. :group 'org-refile
  1654. :type '(choice
  1655. (const :tag "Outline" outline)
  1656. (const :tag "Outline-path-completion" outline-path-completion)))
  1657. (defcustom org-goto-max-level 5
  1658. "Maximum target level when running `org-goto' with refile interface."
  1659. :group 'org-refile
  1660. :type 'integer)
  1661. (defcustom org-reverse-note-order nil
  1662. "Non-nil means store new notes at the beginning of a file or entry.
  1663. When nil, new notes will be filed to the end of a file or entry.
  1664. This can also be a list with cons cells of regular expressions that
  1665. are matched against file names, and values."
  1666. :group 'org-remember
  1667. :group 'org-capture
  1668. :group 'org-refile
  1669. :type '(choice
  1670. (const :tag "Reverse always" t)
  1671. (const :tag "Reverse never" nil)
  1672. (repeat :tag "By file name regexp"
  1673. (cons regexp boolean))))
  1674. (defcustom org-log-refile nil
  1675. "Information to record when a task is refiled.
  1676. Possible values are:
  1677. nil Don't add anything
  1678. time Add a time stamp to the task
  1679. note Prompt for a note and add it with template `org-log-note-headings'
  1680. This option can also be set with on a per-file-basis with
  1681. #+STARTUP: nologrefile
  1682. #+STARTUP: logrefile
  1683. #+STARTUP: lognoterefile
  1684. You can have local logging settings for a subtree by setting the LOGGING
  1685. property to one or more of these keywords.
  1686. When bulk-refiling from the agenda, the value `note' is forbidden and
  1687. will temporarily be changed to `time'."
  1688. :group 'org-refile
  1689. :group 'org-progress
  1690. :version "24.1"
  1691. :type '(choice
  1692. (const :tag "No logging" nil)
  1693. (const :tag "Record timestamp" time)
  1694. (const :tag "Record timestamp with note." note)))
  1695. (defcustom org-refile-targets nil
  1696. "Targets for refiling entries with \\[org-refile].
  1697. This is a list of cons cells. Each cell contains:
  1698. - a specification of the files to be considered, either a list of files,
  1699. or a symbol whose function or variable value will be used to retrieve
  1700. a file name or a list of file names. If you use `org-agenda-files' for
  1701. that, all agenda files will be scanned for targets. Nil means consider
  1702. headings in the current buffer.
  1703. - A specification of how to find candidate refile targets. This may be
  1704. any of:
  1705. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1706. This tag has to be present in all target headlines, inheritance will
  1707. not be considered.
  1708. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1709. todo keyword.
  1710. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1711. headlines that are refiling targets.
  1712. - a cons cell (:level . N). Any headline of level N is considered a target.
  1713. Note that, when `org-odd-levels-only' is set, level corresponds to
  1714. order in hierarchy, not to the number of stars.
  1715. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1716. Note that, when `org-odd-levels-only' is set, level corresponds to
  1717. order in hierarchy, not to the number of stars.
  1718. Each element of this list generates a set of possible targets.
  1719. The union of these sets is presented (with completion) to
  1720. the user by `org-refile'.
  1721. You can set the variable `org-refile-target-verify-function' to a function
  1722. to verify each headline found by the simple criteria above.
  1723. When this variable is nil, all top-level headlines in the current buffer
  1724. are used, equivalent to the value `((nil . (:level . 1))'."
  1725. :group 'org-refile
  1726. :type '(repeat
  1727. (cons
  1728. (choice :value org-agenda-files
  1729. (const :tag "All agenda files" org-agenda-files)
  1730. (const :tag "Current buffer" nil)
  1731. (function) (variable) (file))
  1732. (choice :tag "Identify target headline by"
  1733. (cons :tag "Specific tag" (const :value :tag) (string))
  1734. (cons :tag "TODO keyword" (const :value :todo) (string))
  1735. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1736. (cons :tag "Level number" (const :value :level) (integer))
  1737. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1738. (defcustom org-refile-target-verify-function nil
  1739. "Function to verify if the headline at point should be a refile target.
  1740. The function will be called without arguments, with point at the
  1741. beginning of the headline. It should return t and leave point
  1742. where it is if the headline is a valid target for refiling.
  1743. If the target should not be selected, the function must return nil.
  1744. In addition to this, it may move point to a place from where the search
  1745. should be continued. For example, the function may decide that the entire
  1746. subtree of the current entry should be excluded and move point to the end
  1747. of the subtree."
  1748. :group 'org-refile
  1749. :type 'function)
  1750. (defcustom org-refile-use-cache nil
  1751. "Non-nil means cache refile targets to speed up the process.
  1752. The cache for a particular file will be updated automatically when
  1753. the buffer has been killed, or when any of the marker used for flagging
  1754. refile targets no longer points at a live buffer.
  1755. If you have added new entries to a buffer that might themselves be targets,
  1756. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1757. find that easier, `C-u C-u C-u C-c C-w'."
  1758. :group 'org-refile
  1759. :version "24.1"
  1760. :type 'boolean)
  1761. (defcustom org-refile-use-outline-path nil
  1762. "Non-nil means provide refile targets as paths.
  1763. So a level 3 headline will be available as level1/level2/level3.
  1764. When the value is `file', also include the file name (without directory)
  1765. into the path. In this case, you can also stop the completion after
  1766. the file name, to get entries inserted as top level in the file.
  1767. When `full-file-path', include the full file path."
  1768. :group 'org-refile
  1769. :type '(choice
  1770. (const :tag "Not" nil)
  1771. (const :tag "Yes" t)
  1772. (const :tag "Start with file name" file)
  1773. (const :tag "Start with full file path" full-file-path)))
  1774. (defcustom org-outline-path-complete-in-steps t
  1775. "Non-nil means complete the outline path in hierarchical steps.
  1776. When Org-mode uses the refile interface to select an outline path
  1777. \(see variable `org-refile-use-outline-path'), the completion of
  1778. the path can be done is a single go, or if can be done in steps down
  1779. the headline hierarchy. Going in steps is probably the best if you
  1780. do not use a special completion package like `ido' or `icicles'.
  1781. However, when using these packages, going in one step can be very
  1782. fast, while still showing the whole path to the entry."
  1783. :group 'org-refile
  1784. :type 'boolean)
  1785. (defcustom org-refile-allow-creating-parent-nodes nil
  1786. "Non-nil means allow to create new nodes as refile targets.
  1787. New nodes are then created by adding \"/new node name\" to the completion
  1788. of an existing node. When the value of this variable is `confirm',
  1789. new node creation must be confirmed by the user (recommended)
  1790. When nil, the completion must match an existing entry.
  1791. Note that, if the new heading is not seen by the criteria
  1792. listed in `org-refile-targets', multiple instances of the same
  1793. heading would be created by trying again to file under the new
  1794. heading."
  1795. :group 'org-refile
  1796. :type '(choice
  1797. (const :tag "Never" nil)
  1798. (const :tag "Always" t)
  1799. (const :tag "Prompt for confirmation" confirm)))
  1800. (defcustom org-refile-active-region-within-subtree nil
  1801. "Non-nil means also refile active region within a subtree.
  1802. By default `org-refile' doesn't allow refiling regions if they
  1803. don't contain a set of subtrees, but it might be convenient to
  1804. do so sometimes: in that case, the first line of the region is
  1805. converted to a headline before refiling."
  1806. :group 'org-refile
  1807. :version "24.1"
  1808. :type 'boolean)
  1809. (defgroup org-todo nil
  1810. "Options concerning TODO items in Org-mode."
  1811. :tag "Org TODO"
  1812. :group 'org)
  1813. (defgroup org-progress nil
  1814. "Options concerning Progress logging in Org-mode."
  1815. :tag "Org Progress"
  1816. :group 'org-time)
  1817. (defvar org-todo-interpretation-widgets
  1818. '((:tag "Sequence (cycling hits every state)" sequence)
  1819. (:tag "Type (cycling directly to DONE)" type))
  1820. "The available interpretation symbols for customizing `org-todo-keywords'.
  1821. Interested libraries should add to this list.")
  1822. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1823. "List of TODO entry keyword sequences and their interpretation.
  1824. \\<org-mode-map>This is a list of sequences.
  1825. Each sequence starts with a symbol, either `sequence' or `type',
  1826. indicating if the keywords should be interpreted as a sequence of
  1827. action steps, or as different types of TODO items. The first
  1828. keywords are states requiring action - these states will select a headline
  1829. for inclusion into the global TODO list Org-mode produces. If one of
  1830. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1831. signify that no further action is necessary. If \"|\" is not found,
  1832. the last keyword is treated as the only DONE state of the sequence.
  1833. The command \\[org-todo] cycles an entry through these states, and one
  1834. additional state where no keyword is present. For details about this
  1835. cycling, see the manual.
  1836. TODO keywords and interpretation can also be set on a per-file basis with
  1837. the special #+SEQ_TODO and #+TYP_TODO lines.
  1838. Each keyword can optionally specify a character for fast state selection
  1839. \(in combination with the variable `org-use-fast-todo-selection')
  1840. and specifiers for state change logging, using the same syntax that
  1841. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1842. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1843. indicates to record a time stamp each time this state is selected.
  1844. Each keyword may also specify if a timestamp or a note should be
  1845. recorded when entering or leaving the state, by adding additional
  1846. characters in the parenthesis after the keyword. This looks like this:
  1847. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1848. record only the time of the state change. With X and Y being either
  1849. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1850. Y when leaving the state if and only if the *target* state does not
  1851. define X. You may omit any of the fast-selection key or X or /Y,
  1852. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1853. For backward compatibility, this variable may also be just a list
  1854. of keywords. In this case the interpretation (sequence or type) will be
  1855. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1856. :group 'org-todo
  1857. :group 'org-keywords
  1858. :type '(choice
  1859. (repeat :tag "Old syntax, just keywords"
  1860. (string :tag "Keyword"))
  1861. (repeat :tag "New syntax"
  1862. (cons
  1863. (choice
  1864. :tag "Interpretation"
  1865. ;;Quick and dirty way to see
  1866. ;;`org-todo-interpretations'. This takes the
  1867. ;;place of item arguments
  1868. :convert-widget
  1869. (lambda (widget)
  1870. (widget-put widget
  1871. :args (mapcar
  1872. #'(lambda (x)
  1873. (widget-convert
  1874. (cons 'const x)))
  1875. org-todo-interpretation-widgets))
  1876. widget))
  1877. (repeat
  1878. (string :tag "Keyword"))))))
  1879. (defvar org-todo-keywords-1 nil
  1880. "All TODO and DONE keywords active in a buffer.")
  1881. (make-variable-buffer-local 'org-todo-keywords-1)
  1882. (defvar org-todo-keywords-for-agenda nil)
  1883. (defvar org-done-keywords-for-agenda nil)
  1884. (defvar org-drawers-for-agenda nil)
  1885. (defvar org-todo-keyword-alist-for-agenda nil)
  1886. (defvar org-tag-alist-for-agenda nil)
  1887. (defvar org-agenda-contributing-files nil)
  1888. (defvar org-not-done-keywords nil)
  1889. (make-variable-buffer-local 'org-not-done-keywords)
  1890. (defvar org-done-keywords nil)
  1891. (make-variable-buffer-local 'org-done-keywords)
  1892. (defvar org-todo-heads nil)
  1893. (make-variable-buffer-local 'org-todo-heads)
  1894. (defvar org-todo-sets nil)
  1895. (make-variable-buffer-local 'org-todo-sets)
  1896. (defvar org-todo-log-states nil)
  1897. (make-variable-buffer-local 'org-todo-log-states)
  1898. (defvar org-todo-kwd-alist nil)
  1899. (make-variable-buffer-local 'org-todo-kwd-alist)
  1900. (defvar org-todo-key-alist nil)
  1901. (make-variable-buffer-local 'org-todo-key-alist)
  1902. (defvar org-todo-key-trigger nil)
  1903. (make-variable-buffer-local 'org-todo-key-trigger)
  1904. (defcustom org-todo-interpretation 'sequence
  1905. "Controls how TODO keywords are interpreted.
  1906. This variable is in principle obsolete and is only used for
  1907. backward compatibility, if the interpretation of todo keywords is
  1908. not given already in `org-todo-keywords'. See that variable for
  1909. more information."
  1910. :group 'org-todo
  1911. :group 'org-keywords
  1912. :type '(choice (const sequence)
  1913. (const type)))
  1914. (defcustom org-use-fast-todo-selection t
  1915. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1916. This variable describes if and under what circumstances the cycling
  1917. mechanism for TODO keywords will be replaced by a single-key, direct
  1918. selection scheme.
  1919. When nil, fast selection is never used.
  1920. When the symbol `prefix', it will be used when `org-todo' is called
  1921. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1922. `C-u t' in an agenda buffer.
  1923. When t, fast selection is used by default. In this case, the prefix
  1924. argument forces cycling instead.
  1925. In all cases, the special interface is only used if access keys have
  1926. actually been assigned by the user, i.e. if keywords in the configuration
  1927. are followed by a letter in parenthesis, like TODO(t)."
  1928. :group 'org-todo
  1929. :type '(choice
  1930. (const :tag "Never" nil)
  1931. (const :tag "By default" t)
  1932. (const :tag "Only with C-u C-c C-t" prefix)))
  1933. (defcustom org-provide-todo-statistics t
  1934. "Non-nil means update todo statistics after insert and toggle.
  1935. ALL-HEADLINES means update todo statistics by including headlines
  1936. with no TODO keyword as well, counting them as not done.
  1937. A list of TODO keywords means the same, but skip keywords that are
  1938. not in this list.
  1939. When this is set, todo statistics is updated in the parent of the
  1940. current entry each time a todo state is changed."
  1941. :group 'org-todo
  1942. :type '(choice
  1943. (const :tag "Yes, only for TODO entries" t)
  1944. (const :tag "Yes, including all entries" 'all-headlines)
  1945. (repeat :tag "Yes, for TODOs in this list"
  1946. (string :tag "TODO keyword"))
  1947. (other :tag "No TODO statistics" nil)))
  1948. (defcustom org-hierarchical-todo-statistics t
  1949. "Non-nil means TODO statistics covers just direct children.
  1950. When nil, all entries in the subtree are considered.
  1951. This has only an effect if `org-provide-todo-statistics' is set.
  1952. To set this to nil for only a single subtree, use a COOKIE_DATA
  1953. property and include the word \"recursive\" into the value."
  1954. :group 'org-todo
  1955. :type 'boolean)
  1956. (defcustom org-after-todo-state-change-hook nil
  1957. "Hook which is run after the state of a TODO item was changed.
  1958. The new state (a string with a TODO keyword, or nil) is available in the
  1959. Lisp variable `org-state'."
  1960. :group 'org-todo
  1961. :type 'hook)
  1962. (defvar org-blocker-hook nil
  1963. "Hook for functions that are allowed to block a state change.
  1964. Functions in this hook should not modify the buffer.
  1965. Each function gets as its single argument a property list,
  1966. see `org-trigger-hook' for more information about this list.
  1967. If any of the functions in this hook returns nil, the state change
  1968. is blocked.")
  1969. (defvar org-trigger-hook nil
  1970. "Hook for functions that are triggered by a state change.
  1971. Each function gets as its single argument a property list with at
  1972. least the following elements:
  1973. (:type type-of-change :position pos-at-entry-start
  1974. :from old-state :to new-state)
  1975. Depending on the type, more properties may be present.
  1976. This mechanism is currently implemented for:
  1977. TODO state changes
  1978. ------------------
  1979. :type todo-state-change
  1980. :from previous state (keyword as a string), or nil, or a symbol
  1981. 'todo' or 'done', to indicate the general type of state.
  1982. :to new state, like in :from")
  1983. (defcustom org-enforce-todo-dependencies nil
  1984. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1985. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1986. be blocked if any prior sibling is not yet done.
  1987. Finally, if the parent is blocked because of ordered siblings of its own,
  1988. the child will also be blocked."
  1989. :set (lambda (var val)
  1990. (set var val)
  1991. (if val
  1992. (add-hook 'org-blocker-hook
  1993. 'org-block-todo-from-children-or-siblings-or-parent)
  1994. (remove-hook 'org-blocker-hook
  1995. 'org-block-todo-from-children-or-siblings-or-parent)))
  1996. :group 'org-todo
  1997. :type 'boolean)
  1998. (defcustom org-enforce-todo-checkbox-dependencies nil
  1999. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2000. When this is nil, checkboxes have no influence on switching TODO states.
  2001. When non-nil, you first need to check off all check boxes before the TODO
  2002. entry can be switched to DONE.
  2003. This variable needs to be set before org.el is loaded, and you need to
  2004. restart Emacs after a change to make the change effective. The only way
  2005. to change is while Emacs is running is through the customize interface."
  2006. :set (lambda (var val)
  2007. (set var val)
  2008. (if val
  2009. (add-hook 'org-blocker-hook
  2010. 'org-block-todo-from-checkboxes)
  2011. (remove-hook 'org-blocker-hook
  2012. 'org-block-todo-from-checkboxes)))
  2013. :group 'org-todo
  2014. :type 'boolean)
  2015. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2016. "Non-nil means inserting a TODO heading is treated as state change.
  2017. So when the command \\[org-insert-todo-heading] is used, state change
  2018. logging will apply if appropriate. When nil, the new TODO item will
  2019. be inserted directly, and no logging will take place."
  2020. :group 'org-todo
  2021. :type 'boolean)
  2022. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2023. "Non-nil means switching TODO states with S-cursor counts as state change.
  2024. This is the default behavior. However, setting this to nil allows a
  2025. convenient way to select a TODO state and bypass any logging associated
  2026. with that."
  2027. :group 'org-todo
  2028. :type 'boolean)
  2029. (defcustom org-todo-state-tags-triggers nil
  2030. "Tag changes that should be triggered by TODO state changes.
  2031. This is a list. Each entry is
  2032. (state-change (tag . flag) .......)
  2033. State-change can be a string with a state, and empty string to indicate the
  2034. state that has no TODO keyword, or it can be one of the symbols `todo'
  2035. or `done', meaning any not-done or done state, respectively."
  2036. :group 'org-todo
  2037. :group 'org-tags
  2038. :type '(repeat
  2039. (cons (choice :tag "When changing to"
  2040. (const :tag "Not-done state" todo)
  2041. (const :tag "Done state" done)
  2042. (string :tag "State"))
  2043. (repeat
  2044. (cons :tag "Tag action"
  2045. (string :tag "Tag")
  2046. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2047. (defcustom org-log-done nil
  2048. "Information to record when a task moves to the DONE state.
  2049. Possible values are:
  2050. nil Don't add anything, just change the keyword
  2051. time Add a time stamp to the task
  2052. note Prompt for a note and add it with template `org-log-note-headings'
  2053. This option can also be set with on a per-file-basis with
  2054. #+STARTUP: nologdone
  2055. #+STARTUP: logdone
  2056. #+STARTUP: lognotedone
  2057. You can have local logging settings for a subtree by setting the LOGGING
  2058. property to one or more of these keywords."
  2059. :group 'org-todo
  2060. :group 'org-progress
  2061. :type '(choice
  2062. (const :tag "No logging" nil)
  2063. (const :tag "Record CLOSED timestamp" time)
  2064. (const :tag "Record CLOSED timestamp with note." note)))
  2065. ;; Normalize old uses of org-log-done.
  2066. (cond
  2067. ((eq org-log-done t) (setq org-log-done 'time))
  2068. ((and (listp org-log-done) (memq 'done org-log-done))
  2069. (setq org-log-done 'note)))
  2070. (defcustom org-log-reschedule nil
  2071. "Information to record when the scheduling date of a tasks is modified.
  2072. Possible values are:
  2073. nil Don't add anything, just change the date
  2074. time Add a time stamp to the task
  2075. note Prompt for a note and add it with template `org-log-note-headings'
  2076. This option can also be set with on a per-file-basis with
  2077. #+STARTUP: nologreschedule
  2078. #+STARTUP: logreschedule
  2079. #+STARTUP: lognotereschedule"
  2080. :group 'org-todo
  2081. :group 'org-progress
  2082. :type '(choice
  2083. (const :tag "No logging" nil)
  2084. (const :tag "Record timestamp" time)
  2085. (const :tag "Record timestamp with note." note)))
  2086. (defcustom org-log-redeadline nil
  2087. "Information to record when the deadline date of a tasks is modified.
  2088. Possible values are:
  2089. nil Don't add anything, just change the date
  2090. time Add a time stamp to the task
  2091. note Prompt for a note and add it with template `org-log-note-headings'
  2092. This option can also be set with on a per-file-basis with
  2093. #+STARTUP: nologredeadline
  2094. #+STARTUP: logredeadline
  2095. #+STARTUP: lognoteredeadline
  2096. You can have local logging settings for a subtree by setting the LOGGING
  2097. property to one or more of these keywords."
  2098. :group 'org-todo
  2099. :group 'org-progress
  2100. :type '(choice
  2101. (const :tag "No logging" nil)
  2102. (const :tag "Record timestamp" time)
  2103. (const :tag "Record timestamp with note." note)))
  2104. (defcustom org-log-note-clock-out nil
  2105. "Non-nil means record a note when clocking out of an item.
  2106. This can also be configured on a per-file basis by adding one of
  2107. the following lines anywhere in the buffer:
  2108. #+STARTUP: lognoteclock-out
  2109. #+STARTUP: nolognoteclock-out"
  2110. :group 'org-todo
  2111. :group 'org-progress
  2112. :type 'boolean)
  2113. (defcustom org-log-done-with-time t
  2114. "Non-nil means the CLOSED time stamp will contain date and time.
  2115. When nil, only the date will be recorded."
  2116. :group 'org-progress
  2117. :type 'boolean)
  2118. (defcustom org-log-note-headings
  2119. '((done . "CLOSING NOTE %t")
  2120. (state . "State %-12s from %-12S %t")
  2121. (note . "Note taken on %t")
  2122. (reschedule . "Rescheduled from %S on %t")
  2123. (delschedule . "Not scheduled, was %S on %t")
  2124. (redeadline . "New deadline from %S on %t")
  2125. (deldeadline . "Removed deadline, was %S on %t")
  2126. (refile . "Refiled on %t")
  2127. (clock-out . ""))
  2128. "Headings for notes added to entries.
  2129. The value is an alist, with the car being a symbol indicating the note
  2130. context, and the cdr is the heading to be used. The heading may also be the
  2131. empty string.
  2132. %t in the heading will be replaced by a time stamp.
  2133. %T will be an active time stamp instead the default inactive one
  2134. %d will be replaced by a short-format time stamp.
  2135. %D will be replaced by an active short-format time stamp.
  2136. %s will be replaced by the new TODO state, in double quotes.
  2137. %S will be replaced by the old TODO state, in double quotes.
  2138. %u will be replaced by the user name.
  2139. %U will be replaced by the full user name.
  2140. In fact, it is not a good idea to change the `state' entry, because
  2141. agenda log mode depends on the format of these entries."
  2142. :group 'org-todo
  2143. :group 'org-progress
  2144. :type '(list :greedy t
  2145. (cons (const :tag "Heading when closing an item" done) string)
  2146. (cons (const :tag
  2147. "Heading when changing todo state (todo sequence only)"
  2148. state) string)
  2149. (cons (const :tag "Heading when just taking a note" note) string)
  2150. (cons (const :tag "Heading when clocking out" clock-out) string)
  2151. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2152. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2153. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2154. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2155. (cons (const :tag "Heading when refiling" refile) string)))
  2156. (unless (assq 'note org-log-note-headings)
  2157. (push '(note . "%t") org-log-note-headings))
  2158. (defcustom org-log-into-drawer nil
  2159. "Non-nil means insert state change notes and time stamps into a drawer.
  2160. When nil, state changes notes will be inserted after the headline and
  2161. any scheduling and clock lines, but not inside a drawer.
  2162. The value of this variable should be the name of the drawer to use.
  2163. LOGBOOK is proposed as the default drawer for this purpose, you can
  2164. also set this to a string to define the drawer of your choice.
  2165. A value of t is also allowed, representing \"LOGBOOK\".
  2166. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2167. will be ignored.
  2168. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2169. a subtree."
  2170. :group 'org-todo
  2171. :group 'org-progress
  2172. :type '(choice
  2173. (const :tag "Not into a drawer" nil)
  2174. (const :tag "LOGBOOK" t)
  2175. (string :tag "Other")))
  2176. (if (fboundp 'defvaralias)
  2177. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2178. (defun org-log-into-drawer ()
  2179. "Return the value of `org-log-into-drawer', but let properties overrule.
  2180. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2181. used instead of the default value."
  2182. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2183. (cond
  2184. ((not p) org-log-into-drawer)
  2185. ((equal p "nil") nil)
  2186. ((equal p "t") "LOGBOOK")
  2187. (t p))))
  2188. (defcustom org-log-state-notes-insert-after-drawers nil
  2189. "Non-nil means insert state change notes after any drawers in entry.
  2190. Only the drawers that *immediately* follow the headline and the
  2191. deadline/scheduled line are skipped.
  2192. When nil, insert notes right after the heading and perhaps the line
  2193. with deadline/scheduling if present.
  2194. This variable will have no effect if `org-log-into-drawer' is
  2195. set."
  2196. :group 'org-todo
  2197. :group 'org-progress
  2198. :type 'boolean)
  2199. (defcustom org-log-states-order-reversed t
  2200. "Non-nil means the latest state note will be directly after heading.
  2201. When nil, the state change notes will be ordered according to time."
  2202. :group 'org-todo
  2203. :group 'org-progress
  2204. :type 'boolean)
  2205. (defcustom org-todo-repeat-to-state nil
  2206. "The TODO state to which a repeater should return the repeating task.
  2207. By default this is the first task in a TODO sequence, or the previous state
  2208. in a TODO_TYP set. But you can specify another task here.
  2209. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2210. :group 'org-todo
  2211. :version "24.1"
  2212. :type '(choice (const :tag "Head of sequence" nil)
  2213. (string :tag "Specific state")))
  2214. (defcustom org-log-repeat 'time
  2215. "Non-nil means record moving through the DONE state when triggering repeat.
  2216. An auto-repeating task is immediately switched back to TODO when
  2217. marked DONE. If you are not logging state changes (by adding \"@\"
  2218. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2219. record a closing note, there will be no record of the task moving
  2220. through DONE. This variable forces taking a note anyway.
  2221. nil Don't force a record
  2222. time Record a time stamp
  2223. note Prompt for a note and add it with template `org-log-note-headings'
  2224. This option can also be set with on a per-file-basis with
  2225. #+STARTUP: nologrepeat
  2226. #+STARTUP: logrepeat
  2227. #+STARTUP: lognoterepeat
  2228. You can have local logging settings for a subtree by setting the LOGGING
  2229. property to one or more of these keywords."
  2230. :group 'org-todo
  2231. :group 'org-progress
  2232. :type '(choice
  2233. (const :tag "Don't force a record" nil)
  2234. (const :tag "Force recording the DONE state" time)
  2235. (const :tag "Force recording a note with the DONE state" note)))
  2236. (defgroup org-priorities nil
  2237. "Priorities in Org-mode."
  2238. :tag "Org Priorities"
  2239. :group 'org-todo)
  2240. (defcustom org-enable-priority-commands t
  2241. "Non-nil means priority commands are active.
  2242. When nil, these commands will be disabled, so that you never accidentally
  2243. set a priority."
  2244. :group 'org-priorities
  2245. :type 'boolean)
  2246. (defcustom org-highest-priority ?A
  2247. "The highest priority of TODO items. A character like ?A, ?B etc.
  2248. Must have a smaller ASCII number than `org-lowest-priority'."
  2249. :group 'org-priorities
  2250. :type 'character)
  2251. (defcustom org-lowest-priority ?C
  2252. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2253. Must have a larger ASCII number than `org-highest-priority'."
  2254. :group 'org-priorities
  2255. :type 'character)
  2256. (defcustom org-default-priority ?B
  2257. "The default priority of TODO items.
  2258. This is the priority an item gets if no explicit priority is given.
  2259. When starting to cycle on an empty priority the first step in the cycle
  2260. depends on `org-priority-start-cycle-with-default'. The resulting first
  2261. step priority must not exceed the range from `org-highest-priority' to
  2262. `org-lowest-priority' which means that `org-default-priority' has to be
  2263. in this range exclusive or inclusive the range boundaries. Else the
  2264. first step refuses to set the default and the second will fall back
  2265. to (depending on the command used) the highest or lowest priority."
  2266. :group 'org-priorities
  2267. :type 'character)
  2268. (defcustom org-priority-start-cycle-with-default t
  2269. "Non-nil means start with default priority when starting to cycle.
  2270. When this is nil, the first step in the cycle will be (depending on the
  2271. command used) one higher or lower than the default priority.
  2272. See also `org-default-priority'."
  2273. :group 'org-priorities
  2274. :type 'boolean)
  2275. (defcustom org-get-priority-function nil
  2276. "Function to extract the priority from a string.
  2277. The string is normally the headline. If this is nil Org computes the
  2278. priority from the priority cookie like [#A] in the headline. It returns
  2279. an integer, increasing by 1000 for each priority level.
  2280. The user can set a different function here, which should take a string
  2281. as an argument and return the numeric priority."
  2282. :group 'org-priorities
  2283. :version "24.1"
  2284. :type 'function)
  2285. (defgroup org-time nil
  2286. "Options concerning time stamps and deadlines in Org-mode."
  2287. :tag "Org Time"
  2288. :group 'org)
  2289. (defcustom org-insert-labeled-timestamps-at-point nil
  2290. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2291. When nil, these labeled time stamps are forces into the second line of an
  2292. entry, just after the headline. When scheduling from the global TODO list,
  2293. the time stamp will always be forced into the second line."
  2294. :group 'org-time
  2295. :type 'boolean)
  2296. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2297. "Formats for `format-time-string' which are used for time stamps.
  2298. It is not recommended to change this constant.")
  2299. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2300. "Number of minutes to round time stamps to.
  2301. These are two values, the first applies when first creating a time stamp.
  2302. The second applies when changing it with the commands `S-up' and `S-down'.
  2303. When changing the time stamp, this means that it will change in steps
  2304. of N minutes, as given by the second value.
  2305. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2306. numbers should be factors of 60, so for example 5, 10, 15.
  2307. When this is larger than 1, you can still force an exact time stamp by using
  2308. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2309. and by using a prefix arg to `S-up/down' to specify the exact number
  2310. of minutes to shift."
  2311. :group 'org-time
  2312. :get #'(lambda (var) ; Make sure both elements are there
  2313. (if (integerp (default-value var))
  2314. (list (default-value var) 5)
  2315. (default-value var)))
  2316. :type '(list
  2317. (integer :tag "when inserting times")
  2318. (integer :tag "when modifying times")))
  2319. ;; Normalize old customizations of this variable.
  2320. (when (integerp org-time-stamp-rounding-minutes)
  2321. (setq org-time-stamp-rounding-minutes
  2322. (list org-time-stamp-rounding-minutes
  2323. org-time-stamp-rounding-minutes)))
  2324. (defcustom org-display-custom-times nil
  2325. "Non-nil means overlay custom formats over all time stamps.
  2326. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2327. To turn this on on a per-file basis, insert anywhere in the file:
  2328. #+STARTUP: customtime"
  2329. :group 'org-time
  2330. :set 'set-default
  2331. :type 'sexp)
  2332. (make-variable-buffer-local 'org-display-custom-times)
  2333. (defcustom org-time-stamp-custom-formats
  2334. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2335. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2336. These are overlaid over the default ISO format if the variable
  2337. `org-display-custom-times' is set. Time like %H:%M should be at the
  2338. end of the second format. The custom formats are also honored by export
  2339. commands, if custom time display is turned on at the time of export."
  2340. :group 'org-time
  2341. :type 'sexp)
  2342. (defun org-time-stamp-format (&optional long inactive)
  2343. "Get the right format for a time string."
  2344. (let ((f (if long (cdr org-time-stamp-formats)
  2345. (car org-time-stamp-formats))))
  2346. (if inactive
  2347. (concat "[" (substring f 1 -1) "]")
  2348. f)))
  2349. (defcustom org-time-clocksum-format "%d:%02d"
  2350. "The format string used when creating CLOCKSUM lines.
  2351. This is also used when org-mode generates a time duration."
  2352. :group 'org-time
  2353. :type 'string)
  2354. (defcustom org-time-clocksum-use-fractional nil
  2355. "If non-nil, \\[org-clock-display] uses fractional times.
  2356. org-mode generates a time duration."
  2357. :group 'org-time
  2358. :type 'boolean)
  2359. (defcustom org-time-clocksum-fractional-format "%.2f"
  2360. "The format string used when creating CLOCKSUM lines, or when
  2361. org-mode generates a time duration."
  2362. :group 'org-time
  2363. :type 'string)
  2364. (defcustom org-deadline-warning-days 14
  2365. "No. of days before expiration during which a deadline becomes active.
  2366. This variable governs the display in sparse trees and in the agenda.
  2367. When 0 or negative, it means use this number (the absolute value of it)
  2368. even if a deadline has a different individual lead time specified.
  2369. Custom commands can set this variable in the options section."
  2370. :group 'org-time
  2371. :group 'org-agenda-daily/weekly
  2372. :type 'integer)
  2373. (defcustom org-read-date-prefer-future t
  2374. "Non-nil means assume future for incomplete date input from user.
  2375. This affects the following situations:
  2376. 1. The user gives a month but not a year.
  2377. For example, if it is April and you enter \"feb 2\", this will be read
  2378. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2379. 2. The user gives a day, but no month.
  2380. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2381. read this as the third of *next* month. However, if you enter \"17\",
  2382. it will be considered as *this* month.
  2383. If you set this variable to the symbol `time', then also the following
  2384. will work:
  2385. 3. If the user gives a time.
  2386. If the time is before now, it will be interpreted as tomorrow.
  2387. Currently none of this works for ISO week specifications.
  2388. When this option is nil, the current day, month and year will always be
  2389. used as defaults.
  2390. See also `org-agenda-jump-prefer-future'."
  2391. :group 'org-time
  2392. :type '(choice
  2393. (const :tag "Never" nil)
  2394. (const :tag "Check month and day" t)
  2395. (const :tag "Check month, day, and time" time)))
  2396. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2397. "Should the agenda jump command prefer the future for incomplete dates?
  2398. The default is to do the same as configured in `org-read-date-prefer-future'.
  2399. But you can also set a deviating value here.
  2400. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2401. :group 'org-agenda
  2402. :group 'org-time
  2403. :version "24.1"
  2404. :type '(choice
  2405. (const :tag "Use org-read-date-prefer-future"
  2406. org-read-date-prefer-future)
  2407. (const :tag "Never" nil)
  2408. (const :tag "Always" t)))
  2409. (defcustom org-read-date-force-compatible-dates t
  2410. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2411. Depending on the system Emacs is running on, certain dates cannot
  2412. be represented with the type used internally to represent time.
  2413. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2414. correctly. Some systems allow for earlier dates, some for later,
  2415. some for both. One way to find out it to insert any date into an
  2416. Org buffer, putting the cursor on the year and hitting S-up and
  2417. S-down to test the range.
  2418. When this variable is set to t, the date/time prompt will not let
  2419. you specify dates outside the 1970-2037 range, so it is certain that
  2420. these dates will work in whatever version of Emacs you are
  2421. running, and also that you can move a file from one Emacs implementation
  2422. to another. WHenever Org is forcing the year for you, it will display
  2423. a message and beep.
  2424. When this variable is nil, Org will check if the date is
  2425. representable in the specific Emacs implementation you are using.
  2426. If not, it will force a year, usually the current year, and beep
  2427. to remind you. Currently this setting is not recommended because
  2428. the likelihood that you will open your Org files in an Emacs that
  2429. has limited date range is not negligible.
  2430. A workaround for this problem is to use diary sexp dates for time
  2431. stamps outside of this range."
  2432. :group 'org-time
  2433. :version "24.1"
  2434. :type 'boolean)
  2435. (defcustom org-read-date-display-live t
  2436. "Non-nil means display current interpretation of date prompt live.
  2437. This display will be in an overlay, in the minibuffer."
  2438. :group 'org-time
  2439. :type 'boolean)
  2440. (defcustom org-read-date-popup-calendar t
  2441. "Non-nil means pop up a calendar when prompting for a date.
  2442. In the calendar, the date can be selected with mouse-1. However, the
  2443. minibuffer will also be active, and you can simply enter the date as well.
  2444. When nil, only the minibuffer will be available."
  2445. :group 'org-time
  2446. :type 'boolean)
  2447. (if (fboundp 'defvaralias)
  2448. (defvaralias 'org-popup-calendar-for-date-prompt
  2449. 'org-read-date-popup-calendar))
  2450. (defcustom org-read-date-minibuffer-setup-hook nil
  2451. "Hook to be used to set up keys for the date/time interface.
  2452. Add key definitions to `minibuffer-local-map', which will be a temporary
  2453. copy."
  2454. :group 'org-time
  2455. :type 'hook)
  2456. (defcustom org-extend-today-until 0
  2457. "The hour when your day really ends. Must be an integer.
  2458. This has influence for the following applications:
  2459. - When switching the agenda to \"today\". It it is still earlier than
  2460. the time given here, the day recognized as TODAY is actually yesterday.
  2461. - When a date is read from the user and it is still before the time given
  2462. here, the current date and time will be assumed to be yesterday, 23:59.
  2463. Also, timestamps inserted in capture templates follow this rule.
  2464. IMPORTANT: This is a feature whose implementation is and likely will
  2465. remain incomplete. Really, it is only here because past midnight seems to
  2466. be the favorite working time of John Wiegley :-)"
  2467. :group 'org-time
  2468. :type 'integer)
  2469. (defcustom org-use-effective-time nil
  2470. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2471. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2472. \"effective time\" of any timestamps between midnight and 8am will be
  2473. 23:59 of the previous day."
  2474. :group 'org-time
  2475. :version "24.1"
  2476. :type 'boolean)
  2477. (defcustom org-edit-timestamp-down-means-later nil
  2478. "Non-nil means S-down will increase the time in a time stamp.
  2479. When nil, S-up will increase."
  2480. :group 'org-time
  2481. :type 'boolean)
  2482. (defcustom org-calendar-follow-timestamp-change t
  2483. "Non-nil means make the calendar window follow timestamp changes.
  2484. When a timestamp is modified and the calendar window is visible, it will be
  2485. moved to the new date."
  2486. :group 'org-time
  2487. :type 'boolean)
  2488. (defgroup org-tags nil
  2489. "Options concerning tags in Org-mode."
  2490. :tag "Org Tags"
  2491. :group 'org)
  2492. (defcustom org-tag-alist nil
  2493. "List of tags allowed in Org-mode files.
  2494. When this list is nil, Org-mode will base TAG input on what is already in the
  2495. buffer.
  2496. The value of this variable is an alist, the car of each entry must be a
  2497. keyword as a string, the cdr may be a character that is used to select
  2498. that tag through the fast-tag-selection interface.
  2499. See the manual for details."
  2500. :group 'org-tags
  2501. :type '(repeat
  2502. (choice
  2503. (cons (string :tag "Tag name")
  2504. (character :tag "Access char"))
  2505. (list :tag "Start radio group"
  2506. (const :startgroup)
  2507. (option (string :tag "Group description")))
  2508. (list :tag "End radio group"
  2509. (const :endgroup)
  2510. (option (string :tag "Group description")))
  2511. (const :tag "New line" (:newline)))))
  2512. (defcustom org-tag-persistent-alist nil
  2513. "List of tags that will always appear in all Org-mode files.
  2514. This is in addition to any in buffer settings or customizations
  2515. of `org-tag-alist'.
  2516. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2517. The value of this variable is an alist, the car of each entry must be a
  2518. keyword as a string, the cdr may be a character that is used to select
  2519. that tag through the fast-tag-selection interface.
  2520. See the manual for details.
  2521. To disable these tags on a per-file basis, insert anywhere in the file:
  2522. #+STARTUP: noptag"
  2523. :group 'org-tags
  2524. :type '(repeat
  2525. (choice
  2526. (cons (string :tag "Tag name")
  2527. (character :tag "Access char"))
  2528. (const :tag "Start radio group" (:startgroup))
  2529. (const :tag "End radio group" (:endgroup))
  2530. (const :tag "New line" (:newline)))))
  2531. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2532. "If non-nil, always offer completion for all tags of all agenda files.
  2533. Instead of customizing this variable directly, you might want to
  2534. set it locally for capture buffers, because there no list of
  2535. tags in that file can be created dynamically (there are none).
  2536. (add-hook 'org-capture-mode-hook
  2537. (lambda ()
  2538. (set (make-local-variable
  2539. 'org-complete-tags-always-offer-all-agenda-tags)
  2540. t)))"
  2541. :group 'org-tags
  2542. :version "24.1"
  2543. :type 'boolean)
  2544. (defvar org-file-tags nil
  2545. "List of tags that can be inherited by all entries in the file.
  2546. The tags will be inherited if the variable `org-use-tag-inheritance'
  2547. says they should be.
  2548. This variable is populated from #+FILETAGS lines.")
  2549. (defcustom org-use-fast-tag-selection 'auto
  2550. "Non-nil means use fast tag selection scheme.
  2551. This is a special interface to select and deselect tags with single keys.
  2552. When nil, fast selection is never used.
  2553. When the symbol `auto', fast selection is used if and only if selection
  2554. characters for tags have been configured, either through the variable
  2555. `org-tag-alist' or through a #+TAGS line in the buffer.
  2556. When t, fast selection is always used and selection keys are assigned
  2557. automatically if necessary."
  2558. :group 'org-tags
  2559. :type '(choice
  2560. (const :tag "Always" t)
  2561. (const :tag "Never" nil)
  2562. (const :tag "When selection characters are configured" 'auto)))
  2563. (defcustom org-fast-tag-selection-single-key nil
  2564. "Non-nil means fast tag selection exits after first change.
  2565. When nil, you have to press RET to exit it.
  2566. During fast tag selection, you can toggle this flag with `C-c'.
  2567. This variable can also have the value `expert'. In this case, the window
  2568. displaying the tags menu is not even shown, until you press C-c again."
  2569. :group 'org-tags
  2570. :type '(choice
  2571. (const :tag "No" nil)
  2572. (const :tag "Yes" t)
  2573. (const :tag "Expert" expert)))
  2574. (defvar org-fast-tag-selection-include-todo nil
  2575. "Non-nil means fast tags selection interface will also offer TODO states.
  2576. This is an undocumented feature, you should not rely on it.")
  2577. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2578. "The column to which tags should be indented in a headline.
  2579. If this number is positive, it specifies the column. If it is negative,
  2580. it means that the tags should be flushright to that column. For example,
  2581. -80 works well for a normal 80 character screen.
  2582. When 0, place tags directly after headline text, with only one space in
  2583. between."
  2584. :group 'org-tags
  2585. :type 'integer)
  2586. (defcustom org-auto-align-tags t
  2587. "Non-nil keeps tags aligned when modifying headlines.
  2588. Some operations (i.e. demoting) change the length of a headline and
  2589. therefore shift the tags around. With this option turned on, after
  2590. each such operation the tags are again aligned to `org-tags-column'."
  2591. :group 'org-tags
  2592. :type 'boolean)
  2593. (defcustom org-use-tag-inheritance t
  2594. "Non-nil means tags in levels apply also for sublevels.
  2595. When nil, only the tags directly given in a specific line apply there.
  2596. This may also be a list of tags that should be inherited, or a regexp that
  2597. matches tags that should be inherited. Additional control is possible
  2598. with the variable `org-tags-exclude-from-inheritance' which gives an
  2599. explicit list of tags to be excluded from inheritance., even if the value of
  2600. `org-use-tag-inheritance' would select it for inheritance.
  2601. If this option is t, a match early-on in a tree can lead to a large
  2602. number of matches in the subtree when constructing the agenda or creating
  2603. a sparse tree. If you only want to see the first match in a tree during
  2604. a search, check out the variable `org-tags-match-list-sublevels'."
  2605. :group 'org-tags
  2606. :type '(choice
  2607. (const :tag "Not" nil)
  2608. (const :tag "Always" t)
  2609. (repeat :tag "Specific tags" (string :tag "Tag"))
  2610. (regexp :tag "Tags matched by regexp")))
  2611. (defcustom org-tags-exclude-from-inheritance nil
  2612. "List of tags that should never be inherited.
  2613. This is a way to exclude a few tags from inheritance. For way to do
  2614. the opposite, to actively allow inheritance for selected tags,
  2615. see the variable `org-use-tag-inheritance'."
  2616. :group 'org-tags
  2617. :type '(repeat (string :tag "Tag")))
  2618. (defun org-tag-inherit-p (tag)
  2619. "Check if TAG is one that should be inherited."
  2620. (cond
  2621. ((member tag org-tags-exclude-from-inheritance) nil)
  2622. ((eq org-use-tag-inheritance t) t)
  2623. ((not org-use-tag-inheritance) nil)
  2624. ((stringp org-use-tag-inheritance)
  2625. (string-match org-use-tag-inheritance tag))
  2626. ((listp org-use-tag-inheritance)
  2627. (member tag org-use-tag-inheritance))
  2628. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2629. (defcustom org-tags-match-list-sublevels t
  2630. "Non-nil means list also sublevels of headlines matching a search.
  2631. This variable applies to tags/property searches, and also to stuck
  2632. projects because this search is based on a tags match as well.
  2633. When set to the symbol `indented', sublevels are indented with
  2634. leading dots.
  2635. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2636. the sublevels of a headline matching a tag search often also match
  2637. the same search. Listing all of them can create very long lists.
  2638. Setting this variable to nil causes subtrees of a match to be skipped.
  2639. This variable is semi-obsolete and probably should always be true. It
  2640. is better to limit inheritance to certain tags using the variables
  2641. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2642. :group 'org-tags
  2643. :type '(choice
  2644. (const :tag "No, don't list them" nil)
  2645. (const :tag "Yes, do list them" t)
  2646. (const :tag "List them, indented with leading dots" indented)))
  2647. (defcustom org-tags-sort-function nil
  2648. "When set, tags are sorted using this function as a comparator."
  2649. :group 'org-tags
  2650. :type '(choice
  2651. (const :tag "No sorting" nil)
  2652. (const :tag "Alphabetical" string<)
  2653. (const :tag "Reverse alphabetical" string>)
  2654. (function :tag "Custom function" nil)))
  2655. (defvar org-tags-history nil
  2656. "History of minibuffer reads for tags.")
  2657. (defvar org-last-tags-completion-table nil
  2658. "The last used completion table for tags.")
  2659. (defvar org-after-tags-change-hook nil
  2660. "Hook that is run after the tags in a line have changed.")
  2661. (defgroup org-properties nil
  2662. "Options concerning properties in Org-mode."
  2663. :tag "Org Properties"
  2664. :group 'org)
  2665. (defcustom org-property-format "%-10s %s"
  2666. "How property key/value pairs should be formatted by `indent-line'.
  2667. When `indent-line' hits a property definition, it will format the line
  2668. according to this format, mainly to make sure that the values are
  2669. lined-up with respect to each other."
  2670. :group 'org-properties
  2671. :type 'string)
  2672. (defcustom org-properties-postprocess-alist nil
  2673. "Alist of properties and functions to adjust inserted values.
  2674. Elements of this alist must be of the form
  2675. ([string] [function])
  2676. where [string] must be a property name and [function] must be a
  2677. lambda expression: this lambda expression must take one argument,
  2678. the value to adjust, and return the new value as a string.
  2679. For example, this element will allow the property \"Remaining\"
  2680. to be updated wrt the relation between the \"Effort\" property
  2681. and the clock summary:
  2682. ((\"Remaining\" (lambda(value)
  2683. (let ((clocksum (org-clock-sum-current-item))
  2684. (effort (org-duration-string-to-minutes
  2685. (org-entry-get (point) \"Effort\"))))
  2686. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2687. :group 'org-properties
  2688. :version "24.1"
  2689. :type '(alist :key-type (string :tag "Property")
  2690. :value-type (function :tag "Function")))
  2691. (defcustom org-use-property-inheritance nil
  2692. "Non-nil means properties apply also for sublevels.
  2693. This setting is chiefly used during property searches. Turning it on can
  2694. cause significant overhead when doing a search, which is why it is not
  2695. on by default.
  2696. When nil, only the properties directly given in the current entry count.
  2697. When t, every property is inherited. The value may also be a list of
  2698. properties that should have inheritance, or a regular expression matching
  2699. properties that should be inherited.
  2700. However, note that some special properties use inheritance under special
  2701. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2702. and the properties ending in \"_ALL\" when they are used as descriptor
  2703. for valid values of a property.
  2704. Note for programmers:
  2705. When querying an entry with `org-entry-get', you can control if inheritance
  2706. should be used. By default, `org-entry-get' looks only at the local
  2707. properties. You can request inheritance by setting the inherit argument
  2708. to t (to force inheritance) or to `selective' (to respect the setting
  2709. in this variable)."
  2710. :group 'org-properties
  2711. :type '(choice
  2712. (const :tag "Not" nil)
  2713. (const :tag "Always" t)
  2714. (repeat :tag "Specific properties" (string :tag "Property"))
  2715. (regexp :tag "Properties matched by regexp")))
  2716. (defun org-property-inherit-p (property)
  2717. "Check if PROPERTY is one that should be inherited."
  2718. (cond
  2719. ((eq org-use-property-inheritance t) t)
  2720. ((not org-use-property-inheritance) nil)
  2721. ((stringp org-use-property-inheritance)
  2722. (string-match org-use-property-inheritance property))
  2723. ((listp org-use-property-inheritance)
  2724. (member property org-use-property-inheritance))
  2725. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2726. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2727. "The default column format, if no other format has been defined.
  2728. This variable can be set on the per-file basis by inserting a line
  2729. #+COLUMNS: %25ITEM ....."
  2730. :group 'org-properties
  2731. :type 'string)
  2732. (defcustom org-columns-ellipses ".."
  2733. "The ellipses to be used when a field in column view is truncated.
  2734. When this is the empty string, as many characters as possible are shown,
  2735. but then there will be no visual indication that the field has been truncated.
  2736. When this is a string of length N, the last N characters of a truncated
  2737. field are replaced by this string. If the column is narrower than the
  2738. ellipses string, only part of the ellipses string will be shown."
  2739. :group 'org-properties
  2740. :type 'string)
  2741. (defcustom org-columns-modify-value-for-display-function nil
  2742. "Function that modifies values for display in column view.
  2743. For example, it can be used to cut out a certain part from a time stamp.
  2744. The function must take 2 arguments:
  2745. column-title The title of the column (*not* the property name)
  2746. value The value that should be modified.
  2747. The function should return the value that should be displayed,
  2748. or nil if the normal value should be used."
  2749. :group 'org-properties
  2750. :type 'function)
  2751. (defcustom org-effort-property "Effort"
  2752. "The property that is being used to keep track of effort estimates.
  2753. Effort estimates given in this property need to have the format H:MM."
  2754. :group 'org-properties
  2755. :group 'org-progress
  2756. :type '(string :tag "Property"))
  2757. (defconst org-global-properties-fixed
  2758. '(("VISIBILITY_ALL" . "folded children content all")
  2759. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2760. "List of property/value pairs that can be inherited by any entry.
  2761. These are fixed values, for the preset properties. The user variable
  2762. that can be used to add to this list is `org-global-properties'.
  2763. The entries in this list are cons cells where the car is a property
  2764. name and cdr is a string with the value. If the value represents
  2765. multiple items like an \"_ALL\" property, separate the items by
  2766. spaces.")
  2767. (defcustom org-global-properties nil
  2768. "List of property/value pairs that can be inherited by any entry.
  2769. This list will be combined with the constant `org-global-properties-fixed'.
  2770. The entries in this list are cons cells where the car is a property
  2771. name and cdr is a string with the value.
  2772. You can set buffer-local values for the same purpose in the variable
  2773. `org-file-properties' this by adding lines like
  2774. #+PROPERTY: NAME VALUE"
  2775. :group 'org-properties
  2776. :type '(repeat
  2777. (cons (string :tag "Property")
  2778. (string :tag "Value"))))
  2779. (defvar org-file-properties nil
  2780. "List of property/value pairs that can be inherited by any entry.
  2781. Valid for the current buffer.
  2782. This variable is populated from #+PROPERTY lines.")
  2783. (make-variable-buffer-local 'org-file-properties)
  2784. (defgroup org-agenda nil
  2785. "Options concerning agenda views in Org-mode."
  2786. :tag "Org Agenda"
  2787. :group 'org)
  2788. (defvar org-category nil
  2789. "Variable used by org files to set a category for agenda display.
  2790. Such files should use a file variable to set it, for example
  2791. # -*- mode: org; org-category: \"ELisp\"
  2792. or contain a special line
  2793. #+CATEGORY: ELisp
  2794. If the file does not specify a category, then file's base name
  2795. is used instead.")
  2796. (make-variable-buffer-local 'org-category)
  2797. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2798. (defcustom org-agenda-files nil
  2799. "The files to be used for agenda display.
  2800. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2801. \\[org-remove-file]. You can also use customize to edit the list.
  2802. If an entry is a directory, all files in that directory that are matched by
  2803. `org-agenda-file-regexp' will be part of the file list.
  2804. If the value of the variable is not a list but a single file name, then
  2805. the list of agenda files is actually stored and maintained in that file, one
  2806. agenda file per line. In this file paths can be given relative to
  2807. `org-directory'. Tilde expansion and environment variable substitution
  2808. are also made."
  2809. :group 'org-agenda
  2810. :type '(choice
  2811. (repeat :tag "List of files and directories" file)
  2812. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2813. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2814. "Regular expression to match files for `org-agenda-files'.
  2815. If any element in the list in that variable contains a directory instead
  2816. of a normal file, all files in that directory that are matched by this
  2817. regular expression will be included."
  2818. :group 'org-agenda
  2819. :type 'regexp)
  2820. (defcustom org-agenda-text-search-extra-files nil
  2821. "List of extra files to be searched by text search commands.
  2822. These files will be search in addition to the agenda files by the
  2823. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2824. Note that these files will only be searched for text search commands,
  2825. not for the other agenda views like todo lists, tag searches or the weekly
  2826. agenda. This variable is intended to list notes and possibly archive files
  2827. that should also be searched by these two commands.
  2828. In fact, if the first element in the list is the symbol `agenda-archives',
  2829. than all archive files of all agenda files will be added to the search
  2830. scope."
  2831. :group 'org-agenda
  2832. :type '(set :greedy t
  2833. (const :tag "Agenda Archives" agenda-archives)
  2834. (repeat :inline t (file))))
  2835. (if (fboundp 'defvaralias)
  2836. (defvaralias 'org-agenda-multi-occur-extra-files
  2837. 'org-agenda-text-search-extra-files))
  2838. (defcustom org-agenda-skip-unavailable-files nil
  2839. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2840. A nil value means to remove them, after a query, from the list."
  2841. :group 'org-agenda
  2842. :type 'boolean)
  2843. (defcustom org-calendar-to-agenda-key [?c]
  2844. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2845. The command `org-calendar-goto-agenda' will be bound to this key. The
  2846. default is the character `c' because then `c' can be used to switch back and
  2847. forth between agenda and calendar."
  2848. :group 'org-agenda
  2849. :type 'sexp)
  2850. (defcustom org-calendar-insert-diary-entry-key [?i]
  2851. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2852. This option is irrelevant until `org-agenda-diary-file' has been configured
  2853. to point to an Org-mode file. When that is the case, the command
  2854. `org-agenda-diary-entry' will be bound to the key given here, by default
  2855. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2856. if you want to continue doing this, you need to change this to a different
  2857. key."
  2858. :group 'org-agenda
  2859. :type 'sexp)
  2860. (defcustom org-agenda-diary-file 'diary-file
  2861. "File to which to add new entries with the `i' key in agenda and calendar.
  2862. When this is the symbol `diary-file', the functionality in the Emacs
  2863. calendar will be used to add entries to the `diary-file'. But when this
  2864. points to a file, `org-agenda-diary-entry' will be used instead."
  2865. :group 'org-agenda
  2866. :type '(choice
  2867. (const :tag "The standard Emacs diary file" diary-file)
  2868. (file :tag "Special Org file diary entries")))
  2869. (eval-after-load "calendar"
  2870. '(progn
  2871. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2872. 'org-calendar-goto-agenda)
  2873. (add-hook 'calendar-mode-hook
  2874. (lambda ()
  2875. (unless (eq org-agenda-diary-file 'diary-file)
  2876. (define-key calendar-mode-map
  2877. org-calendar-insert-diary-entry-key
  2878. 'org-agenda-diary-entry))))))
  2879. (defgroup org-latex nil
  2880. "Options for embedding LaTeX code into Org-mode."
  2881. :tag "Org LaTeX"
  2882. :group 'org)
  2883. (defcustom org-format-latex-options
  2884. '(:foreground default :background default :scale 1.0
  2885. :html-foreground "Black" :html-background "Transparent"
  2886. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2887. "Options for creating images from LaTeX fragments.
  2888. This is a property list with the following properties:
  2889. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2890. `default' means use the foreground of the default face.
  2891. :background the background color, or \"Transparent\".
  2892. `default' means use the background of the default face.
  2893. :scale a scaling factor for the size of the images, to get more pixels
  2894. :html-foreground, :html-background, :html-scale
  2895. the same numbers for HTML export.
  2896. :matchers a list indicating which matchers should be used to
  2897. find LaTeX fragments. Valid members of this list are:
  2898. \"begin\" find environments
  2899. \"$1\" find single characters surrounded by $.$
  2900. \"$\" find math expressions surrounded by $...$
  2901. \"$$\" find math expressions surrounded by $$....$$
  2902. \"\\(\" find math expressions surrounded by \\(...\\)
  2903. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2904. :group 'org-latex
  2905. :type 'plist)
  2906. (defcustom org-format-latex-signal-error t
  2907. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2908. When nil, just push out a message."
  2909. :group 'org-latex
  2910. :version "24.1"
  2911. :type 'boolean)
  2912. (defcustom org-latex-to-mathml-jar-file nil
  2913. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2914. Use this to specify additional executable file say a jar file.
  2915. When using MathToWeb as the converter, specify the full-path to
  2916. your mathtoweb.jar file."
  2917. :group 'org-latex
  2918. :version "24.1"
  2919. :type '(choice
  2920. (const :tag "None" nil)
  2921. (file :tag "JAR file" :must-match t)))
  2922. (defcustom org-latex-to-mathml-convert-command nil
  2923. "Command to convert LaTeX fragments to MathML.
  2924. Replace format-specifiers in the command as noted below and use
  2925. `shell-command' to convert LaTeX to MathML.
  2926. %j: Executable file in fully expanded form as specified by
  2927. `org-latex-to-mathml-jar-file'.
  2928. %I: Input LaTeX file in fully expanded form
  2929. %o: Output MathML file
  2930. This command is used by `org-create-math-formula'.
  2931. When using MathToWeb as the converter, set this to
  2932. \"java -jar %j -unicode -force -df %o %I\"."
  2933. :group 'org-latex
  2934. :version "24.1"
  2935. :type '(choice
  2936. (const :tag "None" nil)
  2937. (string :tag "\nShell command")))
  2938. (defcustom org-latex-create-formula-image-program 'dvipng
  2939. "Program to convert LaTeX fragments with.
  2940. dvipng Process the LaTeX fragments to dvi file, then convert
  2941. dvi files to png files using dvipng.
  2942. This will also include processing of non-math environments.
  2943. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2944. to convert pdf files to png files"
  2945. :group 'org-latex
  2946. :version "24.1"
  2947. :type '(choice
  2948. (const :tag "dvipng" dvipng)
  2949. (const :tag "imagemagick" imagemagick)))
  2950. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  2951. "Path to store latex preview images. A relative path here creates many
  2952. directories relative to the processed org files paths. An absolute path
  2953. puts all preview images at the same place."
  2954. :group 'org-latex
  2955. :version "24.3"
  2956. :type 'string)
  2957. (defun org-format-latex-mathml-available-p ()
  2958. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2959. (save-match-data
  2960. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2961. org-latex-to-mathml-convert-command)
  2962. (let ((executable (car (split-string
  2963. org-latex-to-mathml-convert-command))))
  2964. (when (executable-find executable)
  2965. (if (string-match
  2966. "%j" org-latex-to-mathml-convert-command)
  2967. (file-readable-p org-latex-to-mathml-jar-file)
  2968. t))))))
  2969. (defcustom org-format-latex-header "\\documentclass{article}
  2970. \\usepackage[usenames]{color}
  2971. \\usepackage{amsmath}
  2972. \\usepackage[mathscr]{eucal}
  2973. \\pagestyle{empty} % do not remove
  2974. \[PACKAGES]
  2975. \[DEFAULT-PACKAGES]
  2976. % The settings below are copied from fullpage.sty
  2977. \\setlength{\\textwidth}{\\paperwidth}
  2978. \\addtolength{\\textwidth}{-3cm}
  2979. \\setlength{\\oddsidemargin}{1.5cm}
  2980. \\addtolength{\\oddsidemargin}{-2.54cm}
  2981. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2982. \\setlength{\\textheight}{\\paperheight}
  2983. \\addtolength{\\textheight}{-\\headheight}
  2984. \\addtolength{\\textheight}{-\\headsep}
  2985. \\addtolength{\\textheight}{-\\footskip}
  2986. \\addtolength{\\textheight}{-3cm}
  2987. \\setlength{\\topmargin}{1.5cm}
  2988. \\addtolength{\\topmargin}{-2.54cm}"
  2989. "The document header used for processing LaTeX fragments.
  2990. It is imperative that this header make sure that no page number
  2991. appears on the page. The package defined in the variables
  2992. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2993. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2994. will be appended."
  2995. :group 'org-latex
  2996. :type 'string)
  2997. (defvar org-format-latex-header-extra nil)
  2998. (defun org-set-packages-alist (var val)
  2999. "Set the packages alist and make sure it has 3 elements per entry."
  3000. (set var (mapcar (lambda (x)
  3001. (if (and (consp x) (= (length x) 2))
  3002. (list (car x) (nth 1 x) t)
  3003. x))
  3004. val)))
  3005. (defun org-get-packages-alist (var)
  3006. "Get the packages alist and make sure it has 3 elements per entry."
  3007. (mapcar (lambda (x)
  3008. (if (and (consp x) (= (length x) 2))
  3009. (list (car x) (nth 1 x) t)
  3010. x))
  3011. (default-value var)))
  3012. ;; The following variables are defined here because is it also used
  3013. ;; when formatting latex fragments. Originally it was part of the
  3014. ;; LaTeX exporter, which is why the name includes "export".
  3015. (defcustom org-export-latex-default-packages-alist
  3016. '(("AUTO" "inputenc" t)
  3017. ("T1" "fontenc" t)
  3018. ("" "fixltx2e" nil)
  3019. ("" "graphicx" t)
  3020. ("" "longtable" nil)
  3021. ("" "float" nil)
  3022. ("" "wrapfig" nil)
  3023. ("" "soul" t)
  3024. ("" "textcomp" t)
  3025. ("" "marvosym" t)
  3026. ("" "wasysym" t)
  3027. ("" "latexsym" t)
  3028. ("" "amssymb" t)
  3029. ("" "hyperref" nil)
  3030. "\\tolerance=1000"
  3031. )
  3032. "Alist of default packages to be inserted in the header.
  3033. Change this only if one of the packages here causes an incompatibility
  3034. with another package you are using.
  3035. The packages in this list are needed by one part or another of Org-mode
  3036. to function properly.
  3037. - inputenc, fontenc: for basic font and character selection
  3038. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3039. for interpreting the entities in `org-entities'. You can skip some of these
  3040. packages if you don't use any of the symbols in it.
  3041. - graphicx: for including images
  3042. - float, wrapfig: for figure placement
  3043. - longtable: for long tables
  3044. - hyperref: for cross references
  3045. Therefore you should not modify this variable unless you know what you
  3046. are doing. The one reason to change it anyway is that you might be loading
  3047. some other package that conflicts with one of the default packages.
  3048. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3049. If SNIPPET-FLAG is t, the package also needs to be included when
  3050. compiling LaTeX snippets into images for inclusion into HTML."
  3051. :group 'org-export-latex
  3052. :set 'org-set-packages-alist
  3053. :get 'org-get-packages-alist
  3054. :version "24.1"
  3055. :type '(repeat
  3056. (choice
  3057. (list :tag "options/package pair"
  3058. (string :tag "options")
  3059. (string :tag "package")
  3060. (boolean :tag "Snippet"))
  3061. (string :tag "A line of LaTeX"))))
  3062. (defcustom org-export-latex-packages-alist nil
  3063. "Alist of packages to be inserted in every LaTeX header.
  3064. These will be inserted after `org-export-latex-default-packages-alist'.
  3065. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3066. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3067. turning LaTeX snippets into images for inclusion into HTML.
  3068. Make sure that you only list packages here which:
  3069. - you want in every file
  3070. - do not conflict with the default packages in
  3071. `org-export-latex-default-packages-alist'
  3072. - do not conflict with the setup in `org-format-latex-header'."
  3073. :group 'org-export-latex
  3074. :set 'org-set-packages-alist
  3075. :get 'org-get-packages-alist
  3076. :type '(repeat
  3077. (choice
  3078. (list :tag "options/package pair"
  3079. (string :tag "options")
  3080. (string :tag "package")
  3081. (boolean :tag "Snippet"))
  3082. (string :tag "A line of LaTeX"))))
  3083. (defgroup org-appearance nil
  3084. "Settings for Org-mode appearance."
  3085. :tag "Org Appearance"
  3086. :group 'org)
  3087. (defcustom org-level-color-stars-only nil
  3088. "Non-nil means fontify only the stars in each headline.
  3089. When nil, the entire headline is fontified.
  3090. Changing it requires restart of `font-lock-mode' to become effective
  3091. also in regions already fontified."
  3092. :group 'org-appearance
  3093. :type 'boolean)
  3094. (defcustom org-hide-leading-stars nil
  3095. "Non-nil means hide the first N-1 stars in a headline.
  3096. This works by using the face `org-hide' for these stars. This
  3097. face is white for a light background, and black for a dark
  3098. background. You may have to customize the face `org-hide' to
  3099. make this work.
  3100. Changing it requires restart of `font-lock-mode' to become effective
  3101. also in regions already fontified.
  3102. You may also set this on a per-file basis by adding one of the following
  3103. lines to the buffer:
  3104. #+STARTUP: hidestars
  3105. #+STARTUP: showstars"
  3106. :group 'org-appearance
  3107. :type 'boolean)
  3108. (defcustom org-hidden-keywords nil
  3109. "List of symbols corresponding to keywords to be hidden the org buffer.
  3110. For example, a value '(title) for this list will make the document's title
  3111. appear in the buffer without the initial #+TITLE: keyword."
  3112. :group 'org-appearance
  3113. :version "24.1"
  3114. :type '(set (const :tag "#+AUTHOR" author)
  3115. (const :tag "#+DATE" date)
  3116. (const :tag "#+EMAIL" email)
  3117. (const :tag "#+TITLE" title)))
  3118. (defcustom org-custom-properties nil
  3119. "List of properties (as strings) with a special meaning.
  3120. The default use of these custom properties is to let the user
  3121. hide them with `org-toggle-custom-properties-visibility'."
  3122. :group 'org-properties
  3123. :group 'org-appearance
  3124. :version "24.3"
  3125. :type '(repeat (string :tag "Property Name")))
  3126. (defcustom org-fontify-done-headline nil
  3127. "Non-nil means change the face of a headline if it is marked DONE.
  3128. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3129. When this is non-nil, the headline after the keyword is set to the
  3130. `org-headline-done' as an additional indication."
  3131. :group 'org-appearance
  3132. :type 'boolean)
  3133. (defcustom org-fontify-emphasized-text t
  3134. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3135. Changing this variable requires a restart of Emacs to take effect."
  3136. :group 'org-appearance
  3137. :type 'boolean)
  3138. (defcustom org-fontify-whole-heading-line nil
  3139. "Non-nil means fontify the whole line for headings.
  3140. This is useful when setting a background color for the
  3141. org-level-* faces."
  3142. :group 'org-appearance
  3143. :type 'boolean)
  3144. (defcustom org-highlight-latex-fragments-and-specials nil
  3145. "Non-nil means fontify what is treated specially by the exporters."
  3146. :group 'org-appearance
  3147. :type 'boolean)
  3148. (defcustom org-hide-emphasis-markers nil
  3149. "Non-nil mean font-lock should hide the emphasis marker characters."
  3150. :group 'org-appearance
  3151. :type 'boolean)
  3152. (defcustom org-pretty-entities nil
  3153. "Non-nil means show entities as UTF8 characters.
  3154. When nil, the \\name form remains in the buffer."
  3155. :group 'org-appearance
  3156. :version "24.1"
  3157. :type 'boolean)
  3158. (defcustom org-pretty-entities-include-sub-superscripts t
  3159. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3160. :group 'org-appearance
  3161. :version "24.1"
  3162. :type 'boolean)
  3163. (defvar org-emph-re nil
  3164. "Regular expression for matching emphasis.
  3165. After a match, the match groups contain these elements:
  3166. 0 The match of the full regular expression, including the characters
  3167. before and after the proper match
  3168. 1 The character before the proper match, or empty at beginning of line
  3169. 2 The proper match, including the leading and trailing markers
  3170. 3 The leading marker like * or /, indicating the type of highlighting
  3171. 4 The text between the emphasis markers, not including the markers
  3172. 5 The character after the match, empty at the end of a line")
  3173. (defvar org-verbatim-re nil
  3174. "Regular expression for matching verbatim text.")
  3175. (defvar org-emphasis-regexp-components) ; defined just below
  3176. (defvar org-emphasis-alist) ; defined just below
  3177. (defun org-set-emph-re (var val)
  3178. "Set variable and compute the emphasis regular expression."
  3179. (set var val)
  3180. (when (and (boundp 'org-emphasis-alist)
  3181. (boundp 'org-emphasis-regexp-components)
  3182. org-emphasis-alist org-emphasis-regexp-components)
  3183. (let* ((e org-emphasis-regexp-components)
  3184. (pre (car e))
  3185. (post (nth 1 e))
  3186. (border (nth 2 e))
  3187. (body (nth 3 e))
  3188. (nl (nth 4 e))
  3189. (body1 (concat body "*?"))
  3190. (markers (mapconcat 'car org-emphasis-alist ""))
  3191. (vmarkers (mapconcat
  3192. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3193. org-emphasis-alist "")))
  3194. ;; make sure special characters appear at the right position in the class
  3195. (if (string-match "\\^" markers)
  3196. (setq markers (concat (replace-match "" t t markers) "^")))
  3197. (if (string-match "-" markers)
  3198. (setq markers (concat (replace-match "" t t markers) "-")))
  3199. (if (string-match "\\^" vmarkers)
  3200. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3201. (if (string-match "-" vmarkers)
  3202. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3203. (if (> nl 0)
  3204. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3205. (int-to-string nl) "\\}")))
  3206. ;; Make the regexp
  3207. (setq org-emph-re
  3208. (concat "\\([" pre "]\\|^\\)"
  3209. "\\("
  3210. "\\([" markers "]\\)"
  3211. "\\("
  3212. "[^" border "]\\|"
  3213. "[^" border "]"
  3214. body1
  3215. "[^" border "]"
  3216. "\\)"
  3217. "\\3\\)"
  3218. "\\([" post "]\\|$\\)"))
  3219. (setq org-verbatim-re
  3220. (concat "\\([" pre "]\\|^\\)"
  3221. "\\("
  3222. "\\([" vmarkers "]\\)"
  3223. "\\("
  3224. "[^" border "]\\|"
  3225. "[^" border "]"
  3226. body1
  3227. "[^" border "]"
  3228. "\\)"
  3229. "\\3\\)"
  3230. "\\([" post "]\\|$\\)")))))
  3231. (defcustom org-emphasis-regexp-components
  3232. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3233. "Components used to build the regular expression for emphasis.
  3234. This is a list with five entries. Terminology: In an emphasis string
  3235. like \" *strong word* \", we call the initial space PREMATCH, the final
  3236. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3237. and \"trong wor\" is the body. The different components in this variable
  3238. specify what is allowed/forbidden in each part:
  3239. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3240. post Chars allowed as postmatch. End of line will be allowed too.
  3241. border The chars *forbidden* as border characters.
  3242. body-regexp A regexp like \".\" to match a body character. Don't use
  3243. non-shy groups here, and don't allow newline here.
  3244. newline The maximum number of newlines allowed in an emphasis exp.
  3245. Use customize to modify this, or restart Emacs after changing it."
  3246. :group 'org-appearance
  3247. :set 'org-set-emph-re
  3248. :type '(list
  3249. (sexp :tag "Allowed chars in pre ")
  3250. (sexp :tag "Allowed chars in post ")
  3251. (sexp :tag "Forbidden chars in border ")
  3252. (sexp :tag "Regexp for body ")
  3253. (integer :tag "number of newlines allowed")
  3254. (option (boolean :tag "Please ignore this button"))))
  3255. (defcustom org-emphasis-alist
  3256. `(("*" bold "<b>" "</b>")
  3257. ("/" italic "<i>" "</i>")
  3258. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3259. ("=" org-code "<code>" "</code>" verbatim)
  3260. ("~" org-verbatim "<code>" "</code>" verbatim)
  3261. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3262. "<del>" "</del>")
  3263. )
  3264. "Special syntax for emphasized text.
  3265. Text starting and ending with a special character will be emphasized, for
  3266. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3267. characters, the face to be used by font-lock for highlighting in Org-mode
  3268. Emacs buffers, and the HTML tags to be used for this.
  3269. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3270. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3271. Use customize to modify this, or restart Emacs after changing it."
  3272. :group 'org-appearance
  3273. :set 'org-set-emph-re
  3274. :type '(repeat
  3275. (list
  3276. (string :tag "Marker character")
  3277. (choice
  3278. (face :tag "Font-lock-face")
  3279. (plist :tag "Face property list"))
  3280. (string :tag "HTML start tag")
  3281. (string :tag "HTML end tag")
  3282. (option (const verbatim)))))
  3283. (defvar org-protecting-blocks
  3284. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3285. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3286. This is needed for font-lock setup.")
  3287. ;;; Miscellaneous options
  3288. (defgroup org-completion nil
  3289. "Completion in Org-mode."
  3290. :tag "Org Completion"
  3291. :group 'org)
  3292. (defcustom org-completion-use-ido nil
  3293. "Non-nil means use ido completion wherever possible.
  3294. Note that `ido-mode' must be active for this variable to be relevant.
  3295. If you decide to turn this variable on, you might well want to turn off
  3296. `org-outline-path-complete-in-steps'.
  3297. See also `org-completion-use-iswitchb'."
  3298. :group 'org-completion
  3299. :type 'boolean)
  3300. (defcustom org-completion-use-iswitchb nil
  3301. "Non-nil means use iswitchb completion wherever possible.
  3302. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3303. If you decide to turn this variable on, you might well want to turn off
  3304. `org-outline-path-complete-in-steps'.
  3305. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3306. :group 'org-completion
  3307. :type 'boolean)
  3308. (defcustom org-completion-fallback-command 'hippie-expand
  3309. "The expansion command called by \\[pcomplete] in normal context.
  3310. Normal means, no org-mode-specific context."
  3311. :group 'org-completion
  3312. :type 'function)
  3313. ;;; Functions and variables from their packages
  3314. ;; Declared here to avoid compiler warnings
  3315. ;; XEmacs only
  3316. (defvar outline-mode-menu-heading)
  3317. (defvar outline-mode-menu-show)
  3318. (defvar outline-mode-menu-hide)
  3319. (defvar zmacs-regions) ; XEmacs regions
  3320. ;; Emacs only
  3321. (defvar mark-active)
  3322. ;; Various packages
  3323. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3324. (declare-function calendar-forward-day "cal-move" (arg))
  3325. (declare-function calendar-goto-date "cal-move" (date))
  3326. (declare-function calendar-goto-today "cal-move" ())
  3327. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3328. (defvar calc-embedded-close-formula)
  3329. (defvar calc-embedded-open-formula)
  3330. (declare-function cdlatex-tab "ext:cdlatex" ())
  3331. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3332. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3333. (defvar font-lock-unfontify-region-function)
  3334. (declare-function iswitchb-read-buffer "iswitchb"
  3335. (prompt &optional default require-match start matches-set))
  3336. (defvar iswitchb-temp-buflist)
  3337. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3338. (defvar org-agenda-tags-todo-honor-ignore-options)
  3339. (declare-function org-agenda-skip "org-agenda" ())
  3340. (declare-function
  3341. org-agenda-format-item "org-agenda"
  3342. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3343. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3344. (declare-function org-agenda-change-all-lines "org-agenda"
  3345. (newhead hdmarker &optional fixface just-this))
  3346. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3347. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3348. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3349. (beg end))
  3350. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3351. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3352. "org-agenda" (&optional end))
  3353. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3354. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3355. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3356. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3357. (declare-function org-indent-mode "org-indent" (&optional arg))
  3358. (declare-function parse-time-string "parse-time" (string))
  3359. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3360. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3361. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3362. (defvar remember-data-file)
  3363. (defvar texmathp-why)
  3364. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3365. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3366. (defvar w3m-current-url)
  3367. (defvar w3m-current-title)
  3368. (defvar org-latex-regexps)
  3369. ;;; Autoload and prepare some org modules
  3370. ;; Some table stuff that needs to be defined here, because it is used
  3371. ;; by the functions setting up org-mode or checking for table context.
  3372. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3373. "Detect an org-type or table-type table.")
  3374. (defconst org-table-line-regexp "^[ \t]*|"
  3375. "Detect an org-type table line.")
  3376. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3377. "Detect an org-type table line.")
  3378. (defconst org-table-hline-regexp "^[ \t]*|-"
  3379. "Detect an org-type table hline.")
  3380. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3381. "Detect a table-type table hline.")
  3382. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3383. "Detect the first line outside a table when searching from within it.
  3384. This works for both table types.")
  3385. ;; Autoload the functions in org-table.el that are needed by functions here.
  3386. (eval-and-compile
  3387. (org-autoload "org-table"
  3388. '(org-table-begin org-table-blank-field org-table-end)))
  3389. ;;;###autoload
  3390. (defun turn-on-orgtbl ()
  3391. "Unconditionally turn on `orgtbl-mode'."
  3392. (require 'org-table)
  3393. (orgtbl-mode 1))
  3394. (defun org-at-table-p (&optional table-type)
  3395. "Return t if the cursor is inside an org-type table.
  3396. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3397. (if org-enable-table-editor
  3398. (save-excursion
  3399. (beginning-of-line 1)
  3400. (looking-at (if table-type org-table-any-line-regexp
  3401. org-table-line-regexp)))
  3402. nil))
  3403. (defsubst org-table-p () (org-at-table-p))
  3404. (defun org-at-table.el-p ()
  3405. "Return t if and only if we are at a table.el table."
  3406. (and (org-at-table-p 'any)
  3407. (save-excursion
  3408. (goto-char (org-table-begin 'any))
  3409. (looking-at org-table1-hline-regexp))))
  3410. (defun org-table-recognize-table.el ()
  3411. "If there is a table.el table nearby, recognize it and move into it."
  3412. (if org-table-tab-recognizes-table.el
  3413. (if (org-at-table.el-p)
  3414. (progn
  3415. (beginning-of-line 1)
  3416. (if (looking-at org-table-dataline-regexp)
  3417. nil
  3418. (if (looking-at org-table1-hline-regexp)
  3419. (progn
  3420. (beginning-of-line 2)
  3421. (if (looking-at org-table-any-border-regexp)
  3422. (beginning-of-line -1)))))
  3423. (if (re-search-forward "|" (org-table-end t) t)
  3424. (progn
  3425. (require 'table)
  3426. (if (table--at-cell-p (point))
  3427. t
  3428. (message "recognizing table.el table...")
  3429. (table-recognize-table)
  3430. (message "recognizing table.el table...done")))
  3431. (error "This should not happen"))
  3432. t)
  3433. nil)
  3434. nil))
  3435. (defun org-at-table-hline-p ()
  3436. "Return t if the cursor is inside a hline in a table."
  3437. (if org-enable-table-editor
  3438. (save-excursion
  3439. (beginning-of-line 1)
  3440. (looking-at org-table-hline-regexp))
  3441. nil))
  3442. (defvar org-table-clean-did-remove-column nil)
  3443. (defun org-table-map-tables (function &optional quietly)
  3444. "Apply FUNCTION to the start of all tables in the buffer."
  3445. (save-excursion
  3446. (save-restriction
  3447. (widen)
  3448. (goto-char (point-min))
  3449. (while (re-search-forward org-table-any-line-regexp nil t)
  3450. (unless quietly
  3451. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3452. (beginning-of-line 1)
  3453. (when (and (looking-at org-table-line-regexp)
  3454. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3455. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3456. (save-excursion (funcall function))
  3457. (or (looking-at org-table-line-regexp)
  3458. (forward-char 1)))
  3459. (re-search-forward org-table-any-border-regexp nil 1))))
  3460. (unless quietly (message "Mapping tables: done")))
  3461. ;; Declare and autoload functions from org-exp.el & Co
  3462. (declare-function org-default-export-plist "org-exp")
  3463. (declare-function org-infile-export-plist "org-exp")
  3464. (declare-function org-get-current-options "org-exp")
  3465. ;; Declare and autoload functions from org-agenda.el
  3466. (eval-and-compile
  3467. (org-autoload "org-agenda"
  3468. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3469. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3470. (declare-function org-clock-update-mode-line "org-clock" ())
  3471. (declare-function org-resolve-clocks "org-clock"
  3472. (&optional also-non-dangling-p prompt last-valid))
  3473. (defvar org-clock-start-time)
  3474. (defvar org-clock-marker (make-marker)
  3475. "Marker recording the last clock-in.")
  3476. (defvar org-clock-hd-marker (make-marker)
  3477. "Marker recording the last clock-in, but the headline position.")
  3478. (defvar org-clock-heading ""
  3479. "The heading of the current clock entry.")
  3480. (defun org-clock-is-active ()
  3481. "Return non-nil if clock is currently running.
  3482. The return value is actually the clock marker."
  3483. (marker-buffer org-clock-marker))
  3484. (eval-and-compile
  3485. (org-autoload "org-clock" '(org-clock-remove-overlays
  3486. org-clock-update-time-maybe
  3487. org-clocktable-shift)))
  3488. (defun org-check-running-clock ()
  3489. "Check if the current buffer contains the running clock.
  3490. If yes, offer to stop it and to save the buffer with the changes."
  3491. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3492. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3493. (buffer-name))))
  3494. (org-clock-out)
  3495. (when (y-or-n-p "Save changed buffer?")
  3496. (save-buffer))))
  3497. (defun org-clocktable-try-shift (dir n)
  3498. "Check if this line starts a clock table, if yes, shift the time block."
  3499. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3500. (org-clocktable-shift dir n)))
  3501. ;;;###autoload
  3502. (defun org-clock-persistence-insinuate ()
  3503. "Set up hooks for clock persistence."
  3504. (require 'org-clock)
  3505. (add-hook 'org-mode-hook 'org-clock-load)
  3506. (add-hook 'kill-emacs-hook 'org-clock-save))
  3507. ;; Define the variable already here, to make sure we have it.
  3508. (defvar org-indent-mode nil
  3509. "Non-nil if Org-Indent mode is enabled.
  3510. Use the command `org-indent-mode' to change this variable.")
  3511. ;; Autoload archiving code
  3512. ;; The stuff that is needed for cycling and tags has to be defined here.
  3513. (defgroup org-archive nil
  3514. "Options concerning archiving in Org-mode."
  3515. :tag "Org Archive"
  3516. :group 'org-structure)
  3517. (defcustom org-archive-location "%s_archive::"
  3518. "The location where subtrees should be archived.
  3519. The value of this variable is a string, consisting of two parts,
  3520. separated by a double-colon. The first part is a filename and
  3521. the second part is a headline.
  3522. When the filename is omitted, archiving happens in the same file.
  3523. %s in the filename will be replaced by the current file
  3524. name (without the directory part). Archiving to a different file
  3525. is useful to keep archived entries from contributing to the
  3526. Org-mode Agenda.
  3527. The archived entries will be filed as subtrees of the specified
  3528. headline. When the headline is omitted, the subtrees are simply
  3529. filed away at the end of the file, as top-level entries. Also in
  3530. the heading you can use %s to represent the file name, this can be
  3531. useful when using the same archive for a number of different files.
  3532. Here are a few examples:
  3533. \"%s_archive::\"
  3534. If the current file is Projects.org, archive in file
  3535. Projects.org_archive, as top-level trees. This is the default.
  3536. \"::* Archived Tasks\"
  3537. Archive in the current file, under the top-level headline
  3538. \"* Archived Tasks\".
  3539. \"~/org/archive.org::\"
  3540. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3541. \"~/org/archive.org::* From %s\"
  3542. Archive in file ~/org/archive.org (absolute path), under headlines
  3543. \"From FILENAME\" where file name is the current file name.
  3544. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3545. The \"datetree/\" string is special, signifying to archive
  3546. items to the datetree. Items are placed in either the CLOSED
  3547. date of the item, or the current date if there is no CLOSED date.
  3548. The heading will be a subentry to the current date. There doesn't
  3549. need to be a heading, but there always needs to be a slash after
  3550. datetree. For example, to store archived items directly in the
  3551. datetree, use \"~/org/datetree.org::datetree/\".
  3552. \"basement::** Finished Tasks\"
  3553. Archive in file ./basement (relative path), as level 3 trees
  3554. below the level 2 heading \"** Finished Tasks\".
  3555. You may set this option on a per-file basis by adding to the buffer a
  3556. line like
  3557. #+ARCHIVE: basement::** Finished Tasks
  3558. You may also define it locally for a subtree by setting an ARCHIVE property
  3559. in the entry. If such a property is found in an entry, or anywhere up
  3560. the hierarchy, it will be used."
  3561. :group 'org-archive
  3562. :type 'string)
  3563. (defcustom org-archive-tag "ARCHIVE"
  3564. "The tag that marks a subtree as archived.
  3565. An archived subtree does not open during visibility cycling, and does
  3566. not contribute to the agenda listings.
  3567. After changing this, font-lock must be restarted in the relevant buffers to
  3568. get the proper fontification."
  3569. :group 'org-archive
  3570. :group 'org-keywords
  3571. :type 'string)
  3572. (defcustom org-agenda-skip-archived-trees t
  3573. "Non-nil means the agenda will skip any items located in archived trees.
  3574. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3575. variable is no longer recommended, you should leave it at the value t.
  3576. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3577. :group 'org-archive
  3578. :group 'org-agenda-skip
  3579. :type 'boolean)
  3580. (defcustom org-columns-skip-archived-trees t
  3581. "Non-nil means ignore archived trees when creating column view."
  3582. :group 'org-archive
  3583. :group 'org-properties
  3584. :type 'boolean)
  3585. (defcustom org-cycle-open-archived-trees nil
  3586. "Non-nil means `org-cycle' will open archived trees.
  3587. An archived tree is a tree marked with the tag ARCHIVE.
  3588. When nil, archived trees will stay folded. You can still open them with
  3589. normal outline commands like `show-all', but not with the cycling commands."
  3590. :group 'org-archive
  3591. :group 'org-cycle
  3592. :type 'boolean)
  3593. (defcustom org-sparse-tree-open-archived-trees nil
  3594. "Non-nil means sparse tree construction shows matches in archived trees.
  3595. When nil, matches in these trees are highlighted, but the trees are kept in
  3596. collapsed state."
  3597. :group 'org-archive
  3598. :group 'org-sparse-trees
  3599. :type 'boolean)
  3600. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3601. "The default date type when building a sparse tree.
  3602. When this is nil, a date is a scheduled or a deadline timestamp.
  3603. Otherwise, these types are allowed:
  3604. all: all timestamps
  3605. active: only active timestamps (<...>)
  3606. inactive: only inactive timestamps (<...)
  3607. scheduled: only scheduled timestamps
  3608. deadline: only deadline timestamps"
  3609. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3610. (const :tag "All timestamps" all)
  3611. (const :tag "Only active timestamps" active)
  3612. (const :tag "Only inactive timestamps" inactive)
  3613. (const :tag "Only scheduled timestamps" scheduled)
  3614. (const :tag "Only deadline timestamps" deadline))
  3615. :version "24.3"
  3616. :group 'org-sparse-trees)
  3617. (defun org-cycle-hide-archived-subtrees (state)
  3618. "Re-hide all archived subtrees after a visibility state change."
  3619. (when (and (not org-cycle-open-archived-trees)
  3620. (not (memq state '(overview folded))))
  3621. (save-excursion
  3622. (let* ((globalp (memq state '(contents all)))
  3623. (beg (if globalp (point-min) (point)))
  3624. (end (if globalp (point-max) (org-end-of-subtree t))))
  3625. (org-hide-archived-subtrees beg end)
  3626. (goto-char beg)
  3627. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3628. (message "%s" (substitute-command-keys
  3629. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3630. (defun org-force-cycle-archived ()
  3631. "Cycle subtree even if it is archived."
  3632. (interactive)
  3633. (setq this-command 'org-cycle)
  3634. (let ((org-cycle-open-archived-trees t))
  3635. (call-interactively 'org-cycle)))
  3636. (defun org-hide-archived-subtrees (beg end)
  3637. "Re-hide all archived subtrees after a visibility state change."
  3638. (save-excursion
  3639. (let* ((re (concat ":" org-archive-tag ":")))
  3640. (goto-char beg)
  3641. (while (re-search-forward re end t)
  3642. (when (org-at-heading-p)
  3643. (org-flag-subtree t)
  3644. (org-end-of-subtree t))))))
  3645. (declare-function outline-end-of-heading "outline" ())
  3646. (declare-function outline-flag-region "outline" (from to flag))
  3647. (defun org-flag-subtree (flag)
  3648. (save-excursion
  3649. (org-back-to-heading t)
  3650. (outline-end-of-heading)
  3651. (outline-flag-region (point)
  3652. (progn (org-end-of-subtree t) (point))
  3653. flag)))
  3654. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3655. (eval-and-compile
  3656. (org-autoload "org-archive"
  3657. '(org-add-archive-files)))
  3658. ;; Autoload Column View Code
  3659. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3660. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3661. (declare-function org-columns-compute "org-colview" (property))
  3662. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3663. '(org-columns-number-to-string
  3664. org-columns-get-format-and-top-level
  3665. org-columns-compute
  3666. org-columns-remove-overlays))
  3667. ;; Autoload ID code
  3668. (declare-function org-id-store-link "org-id")
  3669. (declare-function org-id-locations-load "org-id")
  3670. (declare-function org-id-locations-save "org-id")
  3671. (defvar org-id-track-globally)
  3672. (org-autoload "org-id"
  3673. '(org-id-new
  3674. org-id-copy
  3675. org-id-get-with-outline-path-completion
  3676. org-id-get-with-outline-drilling))
  3677. ;;; Variables for pre-computed regular expressions, all buffer local
  3678. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3679. "Matches first line of a hidden block.")
  3680. (make-variable-buffer-local 'org-drawer-regexp)
  3681. (defvar org-todo-regexp nil
  3682. "Matches any of the TODO state keywords.")
  3683. (make-variable-buffer-local 'org-todo-regexp)
  3684. (defvar org-not-done-regexp nil
  3685. "Matches any of the TODO state keywords except the last one.")
  3686. (make-variable-buffer-local 'org-not-done-regexp)
  3687. (defvar org-not-done-heading-regexp nil
  3688. "Matches a TODO headline that is not done.")
  3689. (make-variable-buffer-local 'org-not-done-regexp)
  3690. (defvar org-todo-line-regexp nil
  3691. "Matches a headline and puts TODO state into group 2 if present.")
  3692. (make-variable-buffer-local 'org-todo-line-regexp)
  3693. (defvar org-complex-heading-regexp nil
  3694. "Matches a headline and puts everything into groups:
  3695. group 1: the stars
  3696. group 2: The todo keyword, maybe
  3697. group 3: Priority cookie
  3698. group 4: True headline
  3699. group 5: Tags")
  3700. (make-variable-buffer-local 'org-complex-heading-regexp)
  3701. (defvar org-complex-heading-regexp-format nil
  3702. "Printf format to make regexp to match an exact headline.
  3703. This regexp will match the headline of any node which has the
  3704. exact headline text that is put into the format, but may have any
  3705. TODO state, priority and tags.")
  3706. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3707. (defvar org-todo-line-tags-regexp nil
  3708. "Matches a headline and puts TODO state into group 2 if present.
  3709. Also put tags into group 4 if tags are present.")
  3710. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3711. (defvar org-ds-keyword-length 12
  3712. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3713. (make-variable-buffer-local 'org-ds-keyword-length)
  3714. (defvar org-deadline-regexp nil
  3715. "Matches the DEADLINE keyword.")
  3716. (make-variable-buffer-local 'org-deadline-regexp)
  3717. (defvar org-deadline-time-regexp nil
  3718. "Matches the DEADLINE keyword together with a time stamp.")
  3719. (make-variable-buffer-local 'org-deadline-time-regexp)
  3720. (defvar org-deadline-line-regexp nil
  3721. "Matches the DEADLINE keyword and the rest of the line.")
  3722. (make-variable-buffer-local 'org-deadline-line-regexp)
  3723. (defvar org-scheduled-regexp nil
  3724. "Matches the SCHEDULED keyword.")
  3725. (make-variable-buffer-local 'org-scheduled-regexp)
  3726. (defvar org-scheduled-time-regexp nil
  3727. "Matches the SCHEDULED keyword together with a time stamp.")
  3728. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3729. (defvar org-closed-time-regexp nil
  3730. "Matches the CLOSED keyword together with a time stamp.")
  3731. (make-variable-buffer-local 'org-closed-time-regexp)
  3732. (defvar org-keyword-time-regexp nil
  3733. "Matches any of the 4 keywords, together with the time stamp.")
  3734. (make-variable-buffer-local 'org-keyword-time-regexp)
  3735. (defvar org-keyword-time-not-clock-regexp nil
  3736. "Matches any of the 3 keywords, together with the time stamp.")
  3737. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3738. (defvar org-maybe-keyword-time-regexp nil
  3739. "Matches a timestamp, possibly preceded by a keyword.")
  3740. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3741. (defvar org-all-time-keywords nil
  3742. "List of time keywords.")
  3743. (make-variable-buffer-local 'org-all-time-keywords)
  3744. (defconst org-plain-time-of-day-regexp
  3745. (concat
  3746. "\\(\\<[012]?[0-9]"
  3747. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3748. "\\(--?"
  3749. "\\(\\<[012]?[0-9]"
  3750. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3751. "\\)?")
  3752. "Regular expression to match a plain time or time range.
  3753. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3754. groups carry important information:
  3755. 0 the full match
  3756. 1 the first time, range or not
  3757. 8 the second time, if it is a range.")
  3758. (defconst org-plain-time-extension-regexp
  3759. (concat
  3760. "\\(\\<[012]?[0-9]"
  3761. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3762. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3763. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3764. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3765. groups carry important information:
  3766. 0 the full match
  3767. 7 hours of duration
  3768. 9 minutes of duration")
  3769. (defconst org-stamp-time-of-day-regexp
  3770. (concat
  3771. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3772. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3773. "\\(--?"
  3774. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3775. "Regular expression to match a timestamp time or time range.
  3776. After a match, the following groups carry important information:
  3777. 0 the full match
  3778. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3779. 2 the first time, range or not
  3780. 4 the second time, if it is a range.")
  3781. (defconst org-startup-options
  3782. '(("fold" org-startup-folded t)
  3783. ("overview" org-startup-folded t)
  3784. ("nofold" org-startup-folded nil)
  3785. ("showall" org-startup-folded nil)
  3786. ("showeverything" org-startup-folded showeverything)
  3787. ("content" org-startup-folded content)
  3788. ("indent" org-startup-indented t)
  3789. ("noindent" org-startup-indented nil)
  3790. ("hidestars" org-hide-leading-stars t)
  3791. ("showstars" org-hide-leading-stars nil)
  3792. ("odd" org-odd-levels-only t)
  3793. ("oddeven" org-odd-levels-only nil)
  3794. ("align" org-startup-align-all-tables t)
  3795. ("noalign" org-startup-align-all-tables nil)
  3796. ("inlineimages" org-startup-with-inline-images t)
  3797. ("noinlineimages" org-startup-with-inline-images nil)
  3798. ("customtime" org-display-custom-times t)
  3799. ("logdone" org-log-done time)
  3800. ("lognotedone" org-log-done note)
  3801. ("nologdone" org-log-done nil)
  3802. ("lognoteclock-out" org-log-note-clock-out t)
  3803. ("nolognoteclock-out" org-log-note-clock-out nil)
  3804. ("logrepeat" org-log-repeat state)
  3805. ("lognoterepeat" org-log-repeat note)
  3806. ("nologrepeat" org-log-repeat nil)
  3807. ("logreschedule" org-log-reschedule time)
  3808. ("lognotereschedule" org-log-reschedule note)
  3809. ("nologreschedule" org-log-reschedule nil)
  3810. ("logredeadline" org-log-redeadline time)
  3811. ("lognoteredeadline" org-log-redeadline note)
  3812. ("nologredeadline" org-log-redeadline nil)
  3813. ("logrefile" org-log-refile time)
  3814. ("lognoterefile" org-log-refile note)
  3815. ("nologrefile" org-log-refile nil)
  3816. ("fninline" org-footnote-define-inline t)
  3817. ("nofninline" org-footnote-define-inline nil)
  3818. ("fnlocal" org-footnote-section nil)
  3819. ("fnauto" org-footnote-auto-label t)
  3820. ("fnprompt" org-footnote-auto-label nil)
  3821. ("fnconfirm" org-footnote-auto-label confirm)
  3822. ("fnplain" org-footnote-auto-label plain)
  3823. ("fnadjust" org-footnote-auto-adjust t)
  3824. ("nofnadjust" org-footnote-auto-adjust nil)
  3825. ("constcgs" constants-unit-system cgs)
  3826. ("constSI" constants-unit-system SI)
  3827. ("noptag" org-tag-persistent-alist nil)
  3828. ("hideblocks" org-hide-block-startup t)
  3829. ("nohideblocks" org-hide-block-startup nil)
  3830. ("beamer" org-startup-with-beamer-mode t)
  3831. ("entitiespretty" org-pretty-entities t)
  3832. ("entitiesplain" org-pretty-entities nil))
  3833. "Variable associated with STARTUP options for org-mode.
  3834. Each element is a list of three items: the startup options (as written
  3835. in the #+STARTUP line), the corresponding variable, and the value to set
  3836. this variable to if the option is found. An optional forth element PUSH
  3837. means to push this value onto the list in the variable.")
  3838. (defun org-update-property-plist (key val props)
  3839. "Update PROPS with KEY and VAL."
  3840. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3841. (key (if appending (substring key 0 (- (length key) 1)) key))
  3842. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3843. (previous (cdr (assoc key props))))
  3844. (if appending
  3845. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3846. (cons (cons key val) remainder))))
  3847. (defconst org-block-regexp
  3848. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3849. "Regular expression for hiding blocks.")
  3850. (defconst org-heading-keyword-regexp-format
  3851. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3852. "Printf format for a regexp matching an headline with some keyword.
  3853. This regexp will match the headline of any node which has the
  3854. exact keyword that is put into the format. The keyword isn't in
  3855. any group by default, but the stars and the body are.")
  3856. (defconst org-heading-keyword-maybe-regexp-format
  3857. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3858. "Printf format for a regexp matching an headline, possibly with some keyword.
  3859. This regexp can match any headline with the specified keyword, or
  3860. without a keyword. The keyword isn't in any group by default,
  3861. but the stars and the body are.")
  3862. (defun org-set-regexps-and-options ()
  3863. "Precompute regular expressions for current buffer."
  3864. (when (derived-mode-p 'org-mode)
  3865. (org-set-local 'org-todo-kwd-alist nil)
  3866. (org-set-local 'org-todo-key-alist nil)
  3867. (org-set-local 'org-todo-key-trigger nil)
  3868. (org-set-local 'org-todo-keywords-1 nil)
  3869. (org-set-local 'org-done-keywords nil)
  3870. (org-set-local 'org-todo-heads nil)
  3871. (org-set-local 'org-todo-sets nil)
  3872. (org-set-local 'org-todo-log-states nil)
  3873. (org-set-local 'org-file-properties nil)
  3874. (org-set-local 'org-file-tags nil)
  3875. (let ((re (org-make-options-regexp
  3876. '("CATEGORY" "TODO" "COLUMNS"
  3877. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3878. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3879. "OPTIONS")
  3880. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3881. (splitre "[ \t]+")
  3882. (scripts org-use-sub-superscripts)
  3883. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3884. tail sep kws1 prio props ftags drawers beamer-p
  3885. ext-setup-or-nil setup-contents (start 0))
  3886. (save-excursion
  3887. (save-restriction
  3888. (widen)
  3889. (goto-char (point-min))
  3890. (while (or (and ext-setup-or-nil
  3891. (string-match re ext-setup-or-nil start)
  3892. (setq start (match-end 0)))
  3893. (and (setq ext-setup-or-nil nil start 0)
  3894. (re-search-forward re nil t)))
  3895. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3896. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3897. (if (stringp value) (setq value (org-trim value)))
  3898. (cond
  3899. ((equal key "CATEGORY")
  3900. (setq cat value))
  3901. ((member key '("SEQ_TODO" "TODO"))
  3902. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3903. ((equal key "TYP_TODO")
  3904. (push (cons 'type (org-split-string value splitre)) kwds))
  3905. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3906. ;; general TODO-like setup
  3907. (push (cons (intern (downcase (match-string 1 key)))
  3908. (org-split-string value splitre)) kwds))
  3909. ((equal key "TAGS")
  3910. (setq tags (append tags (if tags '("\\n") nil)
  3911. (org-split-string value splitre))))
  3912. ((equal key "COLUMNS")
  3913. (org-set-local 'org-columns-default-format value))
  3914. ((equal key "LINK")
  3915. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3916. (push (cons (match-string 1 value)
  3917. (org-trim (match-string 2 value)))
  3918. links)))
  3919. ((equal key "PRIORITIES")
  3920. (setq prio (org-split-string value " +")))
  3921. ((equal key "PROPERTY")
  3922. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3923. (setq props (org-update-property-plist (match-string 1 value)
  3924. (match-string 2 value)
  3925. props))))
  3926. ((equal key "FILETAGS")
  3927. (when (string-match "\\S-" value)
  3928. (setq ftags
  3929. (append
  3930. ftags
  3931. (apply 'append
  3932. (mapcar (lambda (x) (org-split-string x ":"))
  3933. (org-split-string value)))))))
  3934. ((equal key "DRAWERS")
  3935. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  3936. ((equal key "CONSTANTS")
  3937. (setq const (append const (org-split-string value splitre))))
  3938. ((equal key "STARTUP")
  3939. (let ((opts (org-split-string value splitre))
  3940. l var val)
  3941. (while (setq l (pop opts))
  3942. (when (setq l (assoc l org-startup-options))
  3943. (setq var (nth 1 l) val (nth 2 l))
  3944. (if (not (nth 3 l))
  3945. (set (make-local-variable var) val)
  3946. (if (not (listp (symbol-value var)))
  3947. (set (make-local-variable var) nil))
  3948. (set (make-local-variable var) (symbol-value var))
  3949. (add-to-list var val))))))
  3950. ((equal key "ARCHIVE")
  3951. (setq arch value)
  3952. (remove-text-properties 0 (length arch)
  3953. '(face t fontified t) arch))
  3954. ((equal key "LATEX_CLASS")
  3955. (setq beamer-p (equal value "beamer")))
  3956. ((equal key "OPTIONS")
  3957. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3958. (setq scripts (read (match-string 2 value)))))
  3959. ((equal key "SETUPFILE")
  3960. (setq setup-contents (org-file-contents
  3961. (expand-file-name
  3962. (org-remove-double-quotes value))
  3963. 'noerror))
  3964. (if (not ext-setup-or-nil)
  3965. (setq ext-setup-or-nil setup-contents start 0)
  3966. (setq ext-setup-or-nil
  3967. (concat (substring ext-setup-or-nil 0 start)
  3968. "\n" setup-contents "\n"
  3969. (substring ext-setup-or-nil start)))))))
  3970. ;; search for property blocks
  3971. (goto-char (point-min))
  3972. (while (re-search-forward org-block-regexp nil t)
  3973. (when (equal "PROPERTY" (upcase (match-string 1)))
  3974. (setq value (replace-regexp-in-string
  3975. "[\n\r]" " " (match-string 4)))
  3976. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3977. (setq props (org-update-property-plist (match-string 1 value)
  3978. (match-string 2 value)
  3979. props)))))))
  3980. (org-set-local 'org-use-sub-superscripts scripts)
  3981. (when cat
  3982. (org-set-local 'org-category (intern cat))
  3983. (push (cons "CATEGORY" cat) props))
  3984. (when prio
  3985. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3986. (setq prio (mapcar 'string-to-char prio))
  3987. (org-set-local 'org-highest-priority (nth 0 prio))
  3988. (org-set-local 'org-lowest-priority (nth 1 prio))
  3989. (org-set-local 'org-default-priority (nth 2 prio)))
  3990. (and props (org-set-local 'org-file-properties (nreverse props)))
  3991. (and ftags (org-set-local 'org-file-tags
  3992. (mapcar 'org-add-prop-inherited ftags)))
  3993. (and drawers (org-set-local 'org-drawers drawers))
  3994. (and arch (org-set-local 'org-archive-location arch))
  3995. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3996. ;; Process the TODO keywords
  3997. (unless kwds
  3998. ;; Use the global values as if they had been given locally.
  3999. (setq kwds (default-value 'org-todo-keywords))
  4000. (if (stringp (car kwds))
  4001. (setq kwds (list (cons org-todo-interpretation
  4002. (default-value 'org-todo-keywords)))))
  4003. (setq kwds (reverse kwds)))
  4004. (setq kwds (nreverse kwds))
  4005. (let (inter kws kw)
  4006. (while (setq kws (pop kwds))
  4007. (let ((kws (or
  4008. (run-hook-with-args-until-success
  4009. 'org-todo-setup-filter-hook kws)
  4010. kws)))
  4011. (setq inter (pop kws) sep (member "|" kws)
  4012. kws0 (delete "|" (copy-sequence kws))
  4013. kwsa nil
  4014. kws1 (mapcar
  4015. (lambda (x)
  4016. ;; 1 2
  4017. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4018. (progn
  4019. (setq kw (match-string 1 x)
  4020. key (and (match-end 2) (match-string 2 x))
  4021. log (org-extract-log-state-settings x))
  4022. (push (cons kw (and key (string-to-char key))) kwsa)
  4023. (and log (push log org-todo-log-states))
  4024. kw)
  4025. (error "Invalid TODO keyword %s" x)))
  4026. kws0)
  4027. kwsa (if kwsa (append '((:startgroup))
  4028. (nreverse kwsa)
  4029. '((:endgroup))))
  4030. hw (car kws1)
  4031. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4032. tail (list inter hw (car dws) (org-last dws))))
  4033. (add-to-list 'org-todo-heads hw 'append)
  4034. (push kws1 org-todo-sets)
  4035. (setq org-done-keywords (append org-done-keywords dws nil))
  4036. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4037. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4038. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4039. (setq org-todo-sets (nreverse org-todo-sets)
  4040. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4041. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4042. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4043. ;; Process the constants
  4044. (when const
  4045. (let (e cst)
  4046. (while (setq e (pop const))
  4047. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4048. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4049. (setq org-table-formula-constants-local cst)))
  4050. ;; Process the tags.
  4051. (when tags
  4052. (let (e tgs)
  4053. (while (setq e (pop tags))
  4054. (cond
  4055. ((equal e "{") (push '(:startgroup) tgs))
  4056. ((equal e "}") (push '(:endgroup) tgs))
  4057. ((equal e "\\n") (push '(:newline) tgs))
  4058. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4059. (push (cons (match-string 1 e)
  4060. (string-to-char (match-string 2 e)))
  4061. tgs))
  4062. (t (push (list e) tgs))))
  4063. (org-set-local 'org-tag-alist nil)
  4064. (while (setq e (pop tgs))
  4065. (or (and (stringp (car e))
  4066. (assoc (car e) org-tag-alist))
  4067. (push e org-tag-alist)))))
  4068. ;; Compute the regular expressions and other local variables.
  4069. ;; Using `org-outline-regexp-bol' would complicate them much,
  4070. ;; because of the fixed white space at the end of that string.
  4071. (if (not org-done-keywords)
  4072. (setq org-done-keywords (and org-todo-keywords-1
  4073. (list (org-last org-todo-keywords-1)))))
  4074. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4075. (length org-scheduled-string)
  4076. (length org-clock-string)
  4077. (length org-closed-string)))
  4078. org-drawer-regexp
  4079. (concat "^[ \t]*:\\("
  4080. (mapconcat 'regexp-quote org-drawers "\\|")
  4081. "\\):[ \t]*$")
  4082. org-not-done-keywords
  4083. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4084. org-todo-regexp
  4085. (concat "\\("
  4086. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4087. "\\)")
  4088. org-not-done-regexp
  4089. (concat "\\("
  4090. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4091. "\\)")
  4092. org-not-done-heading-regexp
  4093. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4094. org-todo-line-regexp
  4095. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4096. org-complex-heading-regexp
  4097. (concat "^\\(\\*+\\)"
  4098. "\\(?: +" org-todo-regexp "\\)?"
  4099. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4100. "\\(?: +\\(.*?\\)\\)??"
  4101. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4102. "[ \t]*$")
  4103. org-complex-heading-regexp-format
  4104. (concat "^\\(\\*+\\)"
  4105. "\\(?: +" org-todo-regexp "\\)?"
  4106. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4107. "\\(?: +"
  4108. ;; Stats cookies can be stuck to body.
  4109. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4110. "\\(%s\\)"
  4111. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4112. "\\)"
  4113. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4114. "[ \t]*$")
  4115. org-todo-line-tags-regexp
  4116. (concat "^\\(\\*+\\)"
  4117. "\\(?: +" org-todo-regexp "\\)?"
  4118. "\\(?: +\\(.*?\\)\\)??"
  4119. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4120. "[ \t]*$")
  4121. org-deadline-regexp (concat "\\<" org-deadline-string)
  4122. org-deadline-time-regexp
  4123. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4124. org-deadline-line-regexp
  4125. (concat "\\<\\(" org-deadline-string "\\).*")
  4126. org-scheduled-regexp
  4127. (concat "\\<" org-scheduled-string)
  4128. org-scheduled-time-regexp
  4129. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4130. org-closed-time-regexp
  4131. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4132. org-keyword-time-regexp
  4133. (concat "\\<\\(" org-scheduled-string
  4134. "\\|" org-deadline-string
  4135. "\\|" org-closed-string
  4136. "\\|" org-clock-string "\\)"
  4137. " *[[<]\\([^]>]+\\)[]>]")
  4138. org-keyword-time-not-clock-regexp
  4139. (concat "\\<\\(" org-scheduled-string
  4140. "\\|" org-deadline-string
  4141. "\\|" org-closed-string
  4142. "\\)"
  4143. " *[[<]\\([^]>]+\\)[]>]")
  4144. org-maybe-keyword-time-regexp
  4145. (concat "\\(\\<\\(" org-scheduled-string
  4146. "\\|" org-deadline-string
  4147. "\\|" org-closed-string
  4148. "\\|" org-clock-string "\\)\\)?"
  4149. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4150. org-all-time-keywords
  4151. (mapcar (lambda (w) (substring w 0 -1))
  4152. (list org-scheduled-string org-deadline-string
  4153. org-clock-string org-closed-string))
  4154. )
  4155. (org-compute-latex-and-specials-regexp)
  4156. (org-set-font-lock-defaults))))
  4157. (defun org-file-contents (file &optional noerror)
  4158. "Return the contents of FILE, as a string."
  4159. (if (or (not file)
  4160. (not (file-readable-p file)))
  4161. (if noerror
  4162. (progn
  4163. (message "Cannot read file \"%s\"" file)
  4164. (ding) (sit-for 2)
  4165. "")
  4166. (error "Cannot read file \"%s\"" file))
  4167. (with-temp-buffer
  4168. (insert-file-contents file)
  4169. (buffer-string))))
  4170. (defun org-extract-log-state-settings (x)
  4171. "Extract the log state setting from a TODO keyword string.
  4172. This will extract info from a string like \"WAIT(w@/!)\"."
  4173. (let (kw key log1 log2)
  4174. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4175. (setq kw (match-string 1 x)
  4176. key (and (match-end 2) (match-string 2 x))
  4177. log1 (and (match-end 3) (match-string 3 x))
  4178. log2 (and (match-end 4) (match-string 4 x)))
  4179. (and (or log1 log2)
  4180. (list kw
  4181. (and log1 (if (equal log1 "!") 'time 'note))
  4182. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4183. (defun org-remove-keyword-keys (list)
  4184. "Remove a pair of parenthesis at the end of each string in LIST."
  4185. (mapcar (lambda (x)
  4186. (if (string-match "(.*)$" x)
  4187. (substring x 0 (match-beginning 0))
  4188. x))
  4189. list))
  4190. (defun org-assign-fast-keys (alist)
  4191. "Assign fast keys to a keyword-key alist.
  4192. Respect keys that are already there."
  4193. (let (new e (alt ?0))
  4194. (while (setq e (pop alist))
  4195. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4196. (cdr e)) ;; Key already assigned.
  4197. (push e new)
  4198. (let ((clist (string-to-list (downcase (car e))))
  4199. (used (append new alist)))
  4200. (when (= (car clist) ?@)
  4201. (pop clist))
  4202. (while (and clist (rassoc (car clist) used))
  4203. (pop clist))
  4204. (unless clist
  4205. (while (rassoc alt used)
  4206. (incf alt)))
  4207. (push (cons (car e) (or (car clist) alt)) new))))
  4208. (nreverse new)))
  4209. ;;; Some variables used in various places
  4210. (defvar org-window-configuration nil
  4211. "Used in various places to store a window configuration.")
  4212. (defvar org-selected-window nil
  4213. "Used in various places to store a window configuration.")
  4214. (defvar org-finish-function nil
  4215. "Function to be called when `C-c C-c' is used.
  4216. This is for getting out of special buffers like capture.")
  4217. ;; FIXME: Occasionally check by commenting these, to make sure
  4218. ;; no other functions uses these, forgetting to let-bind them.
  4219. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4220. (defvar org-last-state)
  4221. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4222. ;; Defined somewhere in this file, but used before definition.
  4223. (defvar org-entities) ;; defined in org-entities.el
  4224. (defvar org-struct-menu)
  4225. (defvar org-org-menu)
  4226. (defvar org-tbl-menu)
  4227. ;;;; Define the Org-mode
  4228. ;; We use a before-change function to check if a table might need
  4229. ;; an update.
  4230. (defvar org-table-may-need-update t
  4231. "Indicates that a table might need an update.
  4232. This variable is set by `org-before-change-function'.
  4233. `org-table-align' sets it back to nil.")
  4234. (defun org-before-change-function (beg end)
  4235. "Every change indicates that a table might need an update."
  4236. (setq org-table-may-need-update t))
  4237. (defvar org-mode-map)
  4238. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4239. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4240. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4241. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4242. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4243. (defvar org-table-buffer-is-an nil)
  4244. (defvar bidi-paragraph-direction)
  4245. (defvar buffer-face-mode-face)
  4246. (require 'outline)
  4247. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4248. (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"))
  4249. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4250. ;; Other stuff we need.
  4251. (require 'time-date)
  4252. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4253. (require 'easymenu)
  4254. (require 'overlay)
  4255. (require 'org-macs)
  4256. (require 'org-entities)
  4257. ;; (require 'org-compat) moved higher up in the file before it is first used
  4258. (require 'org-faces)
  4259. (require 'org-list)
  4260. (require 'org-pcomplete)
  4261. (require 'org-src)
  4262. (require 'org-footnote)
  4263. ;; babel
  4264. (require 'ob)
  4265. (require 'ob-table)
  4266. (require 'ob-lob)
  4267. (require 'ob-ref)
  4268. (require 'ob-tangle)
  4269. (require 'ob-comint)
  4270. (require 'ob-keys)
  4271. ;;;###autoload
  4272. (define-derived-mode org-mode outline-mode "Org"
  4273. "Outline-based notes management and organizer, alias
  4274. \"Carsten's outline-mode for keeping track of everything.\"
  4275. Org-mode develops organizational tasks around a NOTES file which
  4276. contains information about projects as plain text. Org-mode is
  4277. implemented on top of outline-mode, which is ideal to keep the content
  4278. of large files well structured. It supports ToDo items, deadlines and
  4279. time stamps, which magically appear in the diary listing of the Emacs
  4280. calendar. Tables are easily created with a built-in table editor.
  4281. Plain text URL-like links connect to websites, emails (VM), Usenet
  4282. messages (Gnus), BBDB entries, and any files related to the project.
  4283. For printing and sharing of notes, an Org-mode file (or a part of it)
  4284. can be exported as a structured ASCII or HTML file.
  4285. The following commands are available:
  4286. \\{org-mode-map}"
  4287. ;; Get rid of Outline menus, they are not needed
  4288. ;; Need to do this here because define-derived-mode sets up
  4289. ;; the keymap so late. Still, it is a waste to call this each time
  4290. ;; we switch another buffer into org-mode.
  4291. (if (featurep 'xemacs)
  4292. (when (boundp 'outline-mode-menu-heading)
  4293. ;; Assume this is Greg's port, it uses easymenu
  4294. (easy-menu-remove outline-mode-menu-heading)
  4295. (easy-menu-remove outline-mode-menu-show)
  4296. (easy-menu-remove outline-mode-menu-hide))
  4297. (define-key org-mode-map [menu-bar headings] 'undefined)
  4298. (define-key org-mode-map [menu-bar hide] 'undefined)
  4299. (define-key org-mode-map [menu-bar show] 'undefined))
  4300. (org-load-modules-maybe)
  4301. (easy-menu-add org-org-menu)
  4302. (easy-menu-add org-tbl-menu)
  4303. (org-install-agenda-files-menu)
  4304. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4305. (add-to-invisibility-spec '(org-cwidth))
  4306. (add-to-invisibility-spec '(org-hide-block . t))
  4307. (when (featurep 'xemacs)
  4308. (org-set-local 'line-move-ignore-invisible t))
  4309. (org-set-local 'outline-regexp org-outline-regexp)
  4310. (org-set-local 'outline-level 'org-outline-level)
  4311. (setq bidi-paragraph-direction 'left-to-right)
  4312. (when (and org-ellipsis
  4313. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4314. (fboundp 'make-glyph-code))
  4315. (unless org-display-table
  4316. (setq org-display-table (make-display-table)))
  4317. (set-display-table-slot
  4318. org-display-table 4
  4319. (vconcat (mapcar
  4320. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4321. org-ellipsis)))
  4322. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4323. (setq buffer-display-table org-display-table))
  4324. (org-set-regexps-and-options)
  4325. (when (and org-tag-faces (not org-tags-special-faces-re))
  4326. ;; tag faces set outside customize.... force initialization.
  4327. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4328. ;; Calc embedded
  4329. (org-set-local 'calc-embedded-open-mode "# ")
  4330. (modify-syntax-entry ?@ "w")
  4331. (if org-startup-truncated (setq truncate-lines t))
  4332. (org-set-local 'font-lock-unfontify-region-function
  4333. 'org-unfontify-region)
  4334. ;; Activate before-change-function
  4335. (org-set-local 'org-table-may-need-update t)
  4336. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4337. 'local)
  4338. ;; Check for running clock before killing a buffer
  4339. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4340. ;; Indentation.
  4341. (org-set-local 'indent-line-function 'org-indent-line)
  4342. (org-set-local 'indent-region-function 'org-indent-region)
  4343. ;; Initialize radio targets.
  4344. (org-update-radio-target-regexp)
  4345. ;; Filling and auto-filling.
  4346. (org-setup-filling)
  4347. ;; Comments.
  4348. (org-setup-comments-handling)
  4349. ;; Beginning/end of defun
  4350. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4351. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4352. ;; Next error for sparse trees
  4353. (org-set-local 'next-error-function 'org-occur-next-match)
  4354. ;; Make sure dependence stuff works reliably, even for users who set it
  4355. ;; too late :-(
  4356. (if org-enforce-todo-dependencies
  4357. (add-hook 'org-blocker-hook
  4358. 'org-block-todo-from-children-or-siblings-or-parent)
  4359. (remove-hook 'org-blocker-hook
  4360. 'org-block-todo-from-children-or-siblings-or-parent))
  4361. (if org-enforce-todo-checkbox-dependencies
  4362. (add-hook 'org-blocker-hook
  4363. 'org-block-todo-from-checkboxes)
  4364. (remove-hook 'org-blocker-hook
  4365. 'org-block-todo-from-checkboxes))
  4366. ;; Align options lines
  4367. (org-set-local
  4368. 'align-mode-rules-list
  4369. '((org-in-buffer-settings
  4370. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4371. (modes . '(org-mode)))))
  4372. ;; Imenu
  4373. (org-set-local 'imenu-create-index-function
  4374. 'org-imenu-get-tree)
  4375. ;; Make isearch reveal context
  4376. (if (or (featurep 'xemacs)
  4377. (not (boundp 'outline-isearch-open-invisible-function)))
  4378. ;; Emacs 21 and XEmacs make use of the hook
  4379. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4380. ;; Emacs 22 deals with this through a special variable
  4381. (org-set-local 'outline-isearch-open-invisible-function
  4382. (lambda (&rest ignore) (org-show-context 'isearch))))
  4383. ;; Turn on org-beamer-mode?
  4384. (and org-startup-with-beamer-mode (org-beamer-mode))
  4385. ;; Setup the pcomplete hooks
  4386. (set (make-local-variable 'pcomplete-command-completion-function)
  4387. 'org-pcomplete-initial)
  4388. (set (make-local-variable 'pcomplete-command-name-function)
  4389. 'org-command-at-point)
  4390. (set (make-local-variable 'pcomplete-default-completion-function)
  4391. 'ignore)
  4392. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4393. 'org-parse-arguments)
  4394. (set (make-local-variable 'pcomplete-termination-string) "")
  4395. (when (>= emacs-major-version 23)
  4396. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4397. ;; If empty file that did not turn on org-mode automatically, make it to.
  4398. (if (and org-insert-mode-line-in-empty-file
  4399. (org-called-interactively-p 'any)
  4400. (= (point-min) (point-max)))
  4401. (insert "# -*- mode: org -*-\n\n"))
  4402. (unless org-inhibit-startup
  4403. (when org-startup-align-all-tables
  4404. (let ((bmp (buffer-modified-p)))
  4405. (org-table-map-tables 'org-table-align 'quietly)
  4406. (set-buffer-modified-p bmp)))
  4407. (when org-startup-with-inline-images
  4408. (org-display-inline-images))
  4409. (when org-startup-indented
  4410. (require 'org-indent)
  4411. (org-indent-mode 1))
  4412. (unless org-inhibit-startup-visibility-stuff
  4413. (org-set-startup-visibility)))
  4414. ;; Try to set org-hide correctly
  4415. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4416. (when (fboundp 'abbrev-table-put)
  4417. (abbrev-table-put org-mode-abbrev-table
  4418. :parents (list text-mode-abbrev-table)))
  4419. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4420. (defun org-find-invisible-foreground ()
  4421. (let ((candidates (remove
  4422. "unspecified-bg"
  4423. (nconc
  4424. (list (face-background 'default)
  4425. (face-background 'org-default))
  4426. (mapcar
  4427. (lambda (alist)
  4428. (when (boundp alist)
  4429. (cdr (assoc 'background-color (symbol-value alist)))))
  4430. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4431. (list (face-foreground 'org-hide))))))
  4432. (car (remove nil candidates))))
  4433. (defun org-current-time ()
  4434. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4435. (if (> (car org-time-stamp-rounding-minutes) 1)
  4436. (let ((r (car org-time-stamp-rounding-minutes))
  4437. (time (decode-time)))
  4438. (apply 'encode-time
  4439. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4440. (nthcdr 2 time))))
  4441. (current-time)))
  4442. (defun org-today ()
  4443. "Return today date, considering `org-extend-today-until'."
  4444. (time-to-days
  4445. (time-subtract (current-time)
  4446. (list 0 (* 3600 org-extend-today-until) 0))))
  4447. ;;;; Font-Lock stuff, including the activators
  4448. (defvar org-mouse-map (make-sparse-keymap))
  4449. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4450. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4451. (when org-mouse-1-follows-link
  4452. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4453. (when org-tab-follows-link
  4454. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4455. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4456. (require 'font-lock)
  4457. (defconst org-non-link-chars "]\t\n\r<>")
  4458. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4459. "shell" "elisp" "doi" "message"))
  4460. (defvar org-link-types-re nil
  4461. "Matches a link that has a url-like prefix like \"http:\"")
  4462. (defvar org-link-re-with-space nil
  4463. "Matches a link with spaces, optional angular brackets around it.")
  4464. (defvar org-link-re-with-space2 nil
  4465. "Matches a link with spaces, optional angular brackets around it.")
  4466. (defvar org-link-re-with-space3 nil
  4467. "Matches a link with spaces, only for internal part in bracket links.")
  4468. (defvar org-angle-link-re nil
  4469. "Matches link with angular brackets, spaces are allowed.")
  4470. (defvar org-plain-link-re nil
  4471. "Matches plain link, without spaces.")
  4472. (defvar org-bracket-link-regexp nil
  4473. "Matches a link in double brackets.")
  4474. (defvar org-bracket-link-analytic-regexp nil
  4475. "Regular expression used to analyze links.
  4476. Here is what the match groups contain after a match:
  4477. 1: http:
  4478. 2: http
  4479. 3: path
  4480. 4: [desc]
  4481. 5: desc")
  4482. (defvar org-bracket-link-analytic-regexp++ nil
  4483. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4484. (defvar org-any-link-re nil
  4485. "Regular expression matching any link.")
  4486. (defcustom org-match-sexp-depth 3
  4487. "Number of stacked braces for sub/superscript matching.
  4488. This has to be set before loading org.el to be effective."
  4489. :group 'org-export-translation ; ??????????????????????????/
  4490. :type 'integer)
  4491. (defun org-create-multibrace-regexp (left right n)
  4492. "Create a regular expression which will match a balanced sexp.
  4493. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4494. as single character strings.
  4495. The regexp returned will match the entire expression including the
  4496. delimiters. It will also define a single group which contains the
  4497. match except for the outermost delimiters. The maximum depth of
  4498. stacked delimiters is N. Escaping delimiters is not possible."
  4499. (let* ((nothing (concat "[^" left right "]*?"))
  4500. (or "\\|")
  4501. (re nothing)
  4502. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4503. (while (> n 1)
  4504. (setq n (1- n)
  4505. re (concat re or next)
  4506. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4507. (concat left "\\(" re "\\)" right)))
  4508. (defvar org-match-substring-regexp
  4509. (concat
  4510. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4511. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4512. "\\|"
  4513. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4514. "\\|"
  4515. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4516. "The regular expression matching a sub- or superscript.")
  4517. (defvar org-match-substring-with-braces-regexp
  4518. (concat
  4519. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4520. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4521. "\\)")
  4522. "The regular expression matching a sub- or superscript, forcing braces.")
  4523. (defun org-make-link-regexps ()
  4524. "Update the link regular expressions.
  4525. This should be called after the variable `org-link-types' has changed."
  4526. (setq org-link-types-re
  4527. (concat
  4528. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4529. org-link-re-with-space
  4530. (concat
  4531. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4532. "\\([^" org-non-link-chars " ]"
  4533. "[^" org-non-link-chars "]*"
  4534. "[^" org-non-link-chars " ]\\)>?")
  4535. org-link-re-with-space2
  4536. (concat
  4537. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4538. "\\([^" org-non-link-chars " ]"
  4539. "[^\t\n\r]*"
  4540. "[^" org-non-link-chars " ]\\)>?")
  4541. org-link-re-with-space3
  4542. (concat
  4543. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4544. "\\([^" org-non-link-chars " ]"
  4545. "[^\t\n\r]*\\)")
  4546. org-angle-link-re
  4547. (concat
  4548. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4549. "\\([^" org-non-link-chars " ]"
  4550. "[^" org-non-link-chars "]*"
  4551. "\\)>")
  4552. org-plain-link-re
  4553. (concat
  4554. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4555. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4556. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4557. org-bracket-link-regexp
  4558. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4559. org-bracket-link-analytic-regexp
  4560. (concat
  4561. "\\[\\["
  4562. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4563. "\\([^]]+\\)"
  4564. "\\]"
  4565. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4566. "\\]")
  4567. org-bracket-link-analytic-regexp++
  4568. (concat
  4569. "\\[\\["
  4570. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4571. "\\([^]]+\\)"
  4572. "\\]"
  4573. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4574. "\\]")
  4575. org-any-link-re
  4576. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4577. org-angle-link-re "\\)\\|\\("
  4578. org-plain-link-re "\\)")))
  4579. (org-make-link-regexps)
  4580. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4581. "Regular expression for fast time stamp matching.")
  4582. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4583. "Regular expression for fast time stamp matching.")
  4584. (defconst org-ts-regexp0
  4585. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4586. "Regular expression matching time strings for analysis.
  4587. This one does not require the space after the date, so it can be used
  4588. on a string that terminates immediately after the date.")
  4589. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4590. "Regular expression matching time strings for analysis.")
  4591. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4592. "Regular expression matching time stamps, with groups.")
  4593. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4594. "Regular expression matching time stamps (also [..]), with groups.")
  4595. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4596. "Regular expression matching a time stamp range.")
  4597. (defconst org-tr-regexp-both
  4598. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4599. "Regular expression matching a time stamp range.")
  4600. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4601. org-ts-regexp "\\)?")
  4602. "Regular expression matching a time stamp or time stamp range.")
  4603. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4604. org-ts-regexp-both "\\)?")
  4605. "Regular expression matching a time stamp or time stamp range.
  4606. The time stamps may be either active or inactive.")
  4607. (defvar org-emph-face nil)
  4608. (defun org-do-emphasis-faces (limit)
  4609. "Run through the buffer and add overlays to emphasized strings."
  4610. (let (rtn a)
  4611. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4612. (if (not (= (char-after (match-beginning 3))
  4613. (char-after (match-beginning 4))))
  4614. (progn
  4615. (setq rtn t)
  4616. (setq a (assoc (match-string 3) org-emphasis-alist))
  4617. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4618. 'face
  4619. (nth 1 a))
  4620. (and (nth 4 a)
  4621. (org-remove-flyspell-overlays-in
  4622. (match-beginning 0) (match-end 0)))
  4623. (add-text-properties (match-beginning 2) (match-end 2)
  4624. '(font-lock-multiline t org-emphasis t))
  4625. (when org-hide-emphasis-markers
  4626. (add-text-properties (match-end 4) (match-beginning 5)
  4627. '(invisible org-link))
  4628. (add-text-properties (match-beginning 3) (match-end 3)
  4629. '(invisible org-link)))))
  4630. (backward-char 1))
  4631. rtn))
  4632. (defun org-emphasize (&optional char)
  4633. "Insert or change an emphasis, i.e. a font like bold or italic.
  4634. If there is an active region, change that region to a new emphasis.
  4635. If there is no region, just insert the marker characters and position
  4636. the cursor between them.
  4637. CHAR should be either the marker character, or the first character of the
  4638. HTML tag associated with that emphasis. If CHAR is a space, the means
  4639. to remove the emphasis of the selected region.
  4640. If char is not given (for example in an interactive call) it
  4641. will be prompted for."
  4642. (interactive)
  4643. (let ((eal org-emphasis-alist) e det
  4644. (erc org-emphasis-regexp-components)
  4645. (prompt "")
  4646. (string "") beg end move tag c s)
  4647. (if (org-region-active-p)
  4648. (setq beg (region-beginning) end (region-end)
  4649. string (buffer-substring beg end))
  4650. (setq move t))
  4651. (while (setq e (pop eal))
  4652. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4653. c (aref tag 0))
  4654. (push (cons c (string-to-char (car e))) det)
  4655. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4656. (substring tag 1)))))
  4657. (setq det (nreverse det))
  4658. (unless char
  4659. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4660. (setq char (read-char-exclusive)))
  4661. (setq char (or (cdr (assoc char det)) char))
  4662. (if (equal char ?\ )
  4663. (setq s "" move nil)
  4664. (unless (assoc (char-to-string char) org-emphasis-alist)
  4665. (error "No such emphasis marker: \"%c\"" char))
  4666. (setq s (char-to-string char)))
  4667. (while (and (> (length string) 1)
  4668. (equal (substring string 0 1) (substring string -1))
  4669. (assoc (substring string 0 1) org-emphasis-alist))
  4670. (setq string (substring string 1 -1)))
  4671. (setq string (concat s string s))
  4672. (if beg (delete-region beg end))
  4673. (unless (or (bolp)
  4674. (string-match (concat "[" (nth 0 erc) "\n]")
  4675. (char-to-string (char-before (point)))))
  4676. (insert " "))
  4677. (unless (or (eobp)
  4678. (string-match (concat "[" (nth 1 erc) "\n]")
  4679. (char-to-string (char-after (point)))))
  4680. (insert " ") (backward-char 1))
  4681. (insert string)
  4682. (and move (backward-char 1))))
  4683. (defconst org-nonsticky-props
  4684. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4685. (defsubst org-rear-nonsticky-at (pos)
  4686. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4687. (defun org-activate-plain-links (limit)
  4688. "Run through the buffer and add overlays to links."
  4689. (catch 'exit
  4690. (let (f)
  4691. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4692. (not (org-in-src-block-p)))
  4693. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4694. (setq f (get-text-property (match-beginning 0) 'face))
  4695. (unless (or (org-in-src-block-p)
  4696. (eq f 'org-tag)
  4697. (and (listp f) (memq 'org-tag f)))
  4698. (add-text-properties (match-beginning 0) (match-end 0)
  4699. (list 'mouse-face 'highlight
  4700. 'face 'org-link
  4701. 'keymap org-mouse-map))
  4702. (org-rear-nonsticky-at (match-end 0)))
  4703. t))))
  4704. (defun org-activate-code (limit)
  4705. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4706. (progn
  4707. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4708. (remove-text-properties (match-beginning 0) (match-end 0)
  4709. '(display t invisible t intangible t))
  4710. t)))
  4711. (defcustom org-src-fontify-natively nil
  4712. "When non-nil, fontify code in code blocks."
  4713. :type 'boolean
  4714. :version "24.1"
  4715. :group 'org-appearance
  4716. :group 'org-babel)
  4717. (defcustom org-allow-promoting-top-level-subtree nil
  4718. "When non-nil, allow promoting a top level subtree.
  4719. The leading star of the top level headline will be replaced
  4720. by a #."
  4721. :type 'boolean
  4722. :version "24.1"
  4723. :group 'org-appearance)
  4724. (defun org-fontify-meta-lines-and-blocks (limit)
  4725. (condition-case nil
  4726. (org-fontify-meta-lines-and-blocks-1 limit)
  4727. (error (message "org-mode fontification error"))))
  4728. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4729. "Fontify #+ lines and blocks, in the correct ways."
  4730. (let ((case-fold-search t))
  4731. (if (re-search-forward
  4732. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4733. limit t)
  4734. (let ((beg (match-beginning 0))
  4735. (block-start (match-end 0))
  4736. (block-end nil)
  4737. (lang (match-string 7))
  4738. (beg1 (line-beginning-position 2))
  4739. (dc1 (downcase (match-string 2)))
  4740. (dc3 (downcase (match-string 3)))
  4741. end end1 quoting block-type ovl)
  4742. (cond
  4743. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4744. ;; a single line of backend-specific content
  4745. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4746. (remove-text-properties (match-beginning 0) (match-end 0)
  4747. '(display t invisible t intangible t))
  4748. (add-text-properties (match-beginning 1) (match-end 3)
  4749. '(font-lock-fontified t face org-meta-line))
  4750. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4751. '(font-lock-fontified t face org-block))
  4752. ; for backend-specific code
  4753. t)
  4754. ((and (match-end 4) (equal dc3 "+begin"))
  4755. ;; Truly a block
  4756. (setq block-type (downcase (match-string 5))
  4757. quoting (member block-type org-protecting-blocks))
  4758. (when (re-search-forward
  4759. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4760. nil t) ;; on purpose, we look further than LIMIT
  4761. (setq end (min (point-max) (match-end 0))
  4762. end1 (min (point-max) (1- (match-beginning 0))))
  4763. (setq block-end (match-beginning 0))
  4764. (when quoting
  4765. (remove-text-properties beg end
  4766. '(display t invisible t intangible t)))
  4767. (add-text-properties
  4768. beg end
  4769. '(font-lock-fontified t font-lock-multiline t))
  4770. (add-text-properties beg beg1 '(face org-meta-line))
  4771. (add-text-properties end1 (min (point-max) (1+ end))
  4772. '(face org-meta-line)) ; for end_src
  4773. (cond
  4774. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4775. (org-src-font-lock-fontify-block lang block-start block-end)
  4776. ;; remove old background overlays
  4777. (mapc (lambda (ov)
  4778. (if (eq (overlay-get ov 'face) 'org-block-background)
  4779. (delete-overlay ov)))
  4780. (overlays-at (/ (+ beg1 block-end) 2)))
  4781. ;; add a background overlay
  4782. (setq ovl (make-overlay beg1 block-end))
  4783. (overlay-put ovl 'face 'org-block-background)
  4784. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4785. (quoting
  4786. (add-text-properties beg1 (min (point-max) (1+ end1))
  4787. '(face org-block))) ; end of source block
  4788. ((not org-fontify-quote-and-verse-blocks))
  4789. ((string= block-type "quote")
  4790. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4791. ((string= block-type "verse")
  4792. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4793. (add-text-properties beg beg1 '(face org-block-begin-line))
  4794. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4795. '(face org-block-end-line))
  4796. t))
  4797. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4798. (add-text-properties
  4799. beg (match-end 3)
  4800. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4801. '(font-lock-fontified t invisible t)
  4802. '(font-lock-fontified t face org-document-info-keyword)))
  4803. (add-text-properties
  4804. (match-beginning 6) (match-end 6)
  4805. (if (string-equal dc1 "+title:")
  4806. '(font-lock-fontified t face org-document-title)
  4807. '(font-lock-fontified t face org-document-info))))
  4808. ((or (equal dc1 "+results")
  4809. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4810. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4811. "+call:" "+header:" "+headers:" "+name:"))
  4812. (and (match-end 4) (equal dc3 "+attr")))
  4813. (add-text-properties
  4814. beg (match-end 0)
  4815. '(font-lock-fontified t face org-meta-line))
  4816. t)
  4817. ((member dc3 '(" " ""))
  4818. (add-text-properties
  4819. beg (match-end 0)
  4820. '(font-lock-fontified t face font-lock-comment-face)))
  4821. ((not (member (char-after beg) '(?\ ?\t)))
  4822. ;; just any other in-buffer setting, but not indented
  4823. (add-text-properties
  4824. beg (match-end 0)
  4825. '(font-lock-fontified t face org-meta-line))
  4826. t)
  4827. (t nil))))))
  4828. (defun org-activate-angle-links (limit)
  4829. "Run through the buffer and add overlays to links."
  4830. (if (and (re-search-forward org-angle-link-re limit t)
  4831. (not (org-in-src-block-p)))
  4832. (progn
  4833. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4834. (add-text-properties (match-beginning 0) (match-end 0)
  4835. (list 'mouse-face 'highlight
  4836. 'keymap org-mouse-map))
  4837. (org-rear-nonsticky-at (match-end 0))
  4838. t)))
  4839. (defun org-activate-footnote-links (limit)
  4840. "Run through the buffer and add overlays to footnotes."
  4841. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4842. (when fn
  4843. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4844. (org-remove-flyspell-overlays-in beg end)
  4845. (add-text-properties beg end
  4846. (list 'mouse-face 'highlight
  4847. 'keymap org-mouse-map
  4848. 'help-echo
  4849. (if (= (point-at-bol) beg)
  4850. "Footnote definition"
  4851. "Footnote reference")
  4852. 'font-lock-fontified t
  4853. 'font-lock-multiline t
  4854. 'face 'org-footnote))))))
  4855. (defun org-activate-bracket-links (limit)
  4856. "Run through the buffer and add overlays to bracketed links."
  4857. (if (and (re-search-forward org-bracket-link-regexp limit t)
  4858. (not (org-in-src-block-p)))
  4859. (let* ((help (concat "LINK: "
  4860. (org-match-string-no-properties 1)))
  4861. ;; FIXME: above we should remove the escapes.
  4862. ;; but that requires another match, protecting match data,
  4863. ;; a lot of overhead for font-lock.
  4864. (ip (org-maybe-intangible
  4865. (list 'invisible 'org-link
  4866. 'keymap org-mouse-map 'mouse-face 'highlight
  4867. 'font-lock-multiline t 'help-echo help)))
  4868. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4869. 'font-lock-multiline t 'help-echo help)))
  4870. ;; We need to remove the invisible property here. Table narrowing
  4871. ;; may have made some of this invisible.
  4872. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4873. (remove-text-properties (match-beginning 0) (match-end 0)
  4874. '(invisible nil))
  4875. (if (match-end 3)
  4876. (progn
  4877. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4878. (org-rear-nonsticky-at (match-beginning 3))
  4879. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4880. (org-rear-nonsticky-at (match-end 3))
  4881. (add-text-properties (match-end 3) (match-end 0) ip)
  4882. (org-rear-nonsticky-at (match-end 0)))
  4883. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4884. (org-rear-nonsticky-at (match-beginning 1))
  4885. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4886. (org-rear-nonsticky-at (match-end 1))
  4887. (add-text-properties (match-end 1) (match-end 0) ip)
  4888. (org-rear-nonsticky-at (match-end 0)))
  4889. t)))
  4890. (defun org-activate-dates (limit)
  4891. "Run through the buffer and add overlays to dates."
  4892. (if (re-search-forward org-tsr-regexp-both limit t)
  4893. (progn
  4894. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4895. (add-text-properties (match-beginning 0) (match-end 0)
  4896. (list 'mouse-face 'highlight
  4897. 'keymap org-mouse-map))
  4898. (org-rear-nonsticky-at (match-end 0))
  4899. (when org-display-custom-times
  4900. (if (match-end 3)
  4901. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4902. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4903. t)))
  4904. (defvar org-target-link-regexp nil
  4905. "Regular expression matching radio targets in plain text.")
  4906. (make-variable-buffer-local 'org-target-link-regexp)
  4907. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4908. "Regular expression matching a link target.")
  4909. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4910. "Regular expression matching a radio target.")
  4911. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4912. "Regular expression matching any target.")
  4913. (defun org-activate-target-links (limit)
  4914. "Run through the buffer and add overlays to target matches."
  4915. (when org-target-link-regexp
  4916. (let ((case-fold-search t))
  4917. (if (re-search-forward org-target-link-regexp limit t)
  4918. (progn
  4919. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4920. (add-text-properties (match-beginning 0) (match-end 0)
  4921. (list 'mouse-face 'highlight
  4922. 'keymap org-mouse-map
  4923. 'help-echo "Radio target link"
  4924. 'org-linked-text t))
  4925. (org-rear-nonsticky-at (match-end 0))
  4926. t)))))
  4927. (defun org-update-radio-target-regexp ()
  4928. "Find all radio targets in this file and update the regular expression."
  4929. (interactive)
  4930. (when (memq 'radio org-activate-links)
  4931. (setq org-target-link-regexp
  4932. (org-make-target-link-regexp (org-all-targets 'radio)))
  4933. (org-restart-font-lock)))
  4934. (defun org-hide-wide-columns (limit)
  4935. (let (s e)
  4936. (setq s (text-property-any (point) (or limit (point-max))
  4937. 'org-cwidth t))
  4938. (when s
  4939. (setq e (next-single-property-change s 'org-cwidth))
  4940. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4941. (goto-char e)
  4942. t)))
  4943. (defvar org-latex-and-specials-regexp nil
  4944. "Regular expression for highlighting export special stuff.")
  4945. (defvar org-match-substring-regexp)
  4946. (defvar org-match-substring-with-braces-regexp)
  4947. ;; This should be with the exporter code, but we also use if for font-locking
  4948. (defconst org-export-html-special-string-regexps
  4949. '(("\\\\-" . "&shy;")
  4950. ("---\\([^-]\\)" . "&mdash;\\1")
  4951. ("--\\([^-]\\)" . "&ndash;\\1")
  4952. ("\\.\\.\\." . "&hellip;"))
  4953. "Regular expressions for special string conversion.")
  4954. (defun org-compute-latex-and-specials-regexp ()
  4955. "Compute regular expression for stuff treated specially by exporters."
  4956. (if (not org-highlight-latex-fragments-and-specials)
  4957. (org-set-local 'org-latex-and-specials-regexp nil)
  4958. (require 'org-exp)
  4959. (let*
  4960. ((matchers (plist-get org-format-latex-options :matchers))
  4961. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4962. org-latex-regexps)))
  4963. (org-export-allow-BIND nil)
  4964. (options (org-combine-plists (org-default-export-plist)
  4965. (org-infile-export-plist)))
  4966. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4967. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4968. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4969. (org-export-html-expand (plist-get options :expand-quoted-html))
  4970. (org-export-with-special-strings (plist-get options :special-strings))
  4971. (re-sub
  4972. (cond
  4973. ((equal org-export-with-sub-superscripts '{})
  4974. (list org-match-substring-with-braces-regexp))
  4975. (org-export-with-sub-superscripts
  4976. (list org-match-substring-regexp))))
  4977. (re-latex
  4978. (if org-export-with-LaTeX-fragments
  4979. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4980. (re-macros
  4981. (if org-export-with-TeX-macros
  4982. (list (concat "\\\\"
  4983. (regexp-opt
  4984. (append
  4985. (delq nil
  4986. (mapcar 'car-safe
  4987. (append org-entities-user
  4988. org-entities)))
  4989. (if (boundp 'org-latex-entities)
  4990. (mapcar (lambda (x)
  4991. (or (car-safe x) x))
  4992. org-latex-entities)
  4993. nil))
  4994. 'words))) ; FIXME
  4995. ))
  4996. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4997. (re-special (if org-export-with-special-strings
  4998. (mapcar (lambda (x) (car x))
  4999. org-export-html-special-string-regexps)))
  5000. (re-rest
  5001. (delq nil
  5002. (list
  5003. (if org-export-html-expand "@<[^>\n]+>")
  5004. ))))
  5005. (org-set-local
  5006. 'org-latex-and-specials-regexp
  5007. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5008. re-rest) "\\|")))))
  5009. (defun org-do-latex-and-special-faces (limit)
  5010. "Run through the buffer and add overlays to links."
  5011. (when org-latex-and-specials-regexp
  5012. (let (rtn d)
  5013. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5014. limit t))
  5015. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5016. 'face))
  5017. '(org-code org-verbatim underline)))
  5018. (progn
  5019. (setq rtn t
  5020. d (cond ((member (char-after (1+ (match-beginning 0)))
  5021. '(?_ ?^)) 1)
  5022. (t 0)))
  5023. (font-lock-prepend-text-property
  5024. (+ d (match-beginning 0)) (match-end 0)
  5025. 'face 'org-latex-and-export-specials)
  5026. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5027. '(font-lock-multiline t)))))
  5028. rtn)))
  5029. (defun org-restart-font-lock ()
  5030. "Restart `font-lock-mode', to force refontification."
  5031. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5032. (font-lock-mode -1)
  5033. (font-lock-mode 1)))
  5034. (defun org-all-targets (&optional radio)
  5035. "Return a list of all targets in this file.
  5036. With optional argument RADIO, only find radio targets."
  5037. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5038. rtn)
  5039. (save-excursion
  5040. (goto-char (point-min))
  5041. (while (re-search-forward re nil t)
  5042. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5043. rtn)))
  5044. (defun org-make-target-link-regexp (targets)
  5045. "Make regular expression matching all strings in TARGETS.
  5046. The regular expression finds the targets also if there is a line break
  5047. between words."
  5048. (and targets
  5049. (concat
  5050. "\\<\\("
  5051. (mapconcat
  5052. (lambda (x)
  5053. (setq x (regexp-quote x))
  5054. (while (string-match " +" x)
  5055. (setq x (replace-match "\\s-+" t t x)))
  5056. x)
  5057. targets
  5058. "\\|")
  5059. "\\)\\>")))
  5060. (defun org-activate-tags (limit)
  5061. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5062. (progn
  5063. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5064. (add-text-properties (match-beginning 1) (match-end 1)
  5065. (list 'mouse-face 'highlight
  5066. 'keymap org-mouse-map))
  5067. (org-rear-nonsticky-at (match-end 1))
  5068. t)))
  5069. (defun org-outline-level ()
  5070. "Compute the outline level of the heading at point.
  5071. This function assumes that the cursor is at the beginning of a line matched
  5072. by `outline-regexp'. Otherwise it returns garbage.
  5073. If this is called at a normal headline, the level is the number of stars.
  5074. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5075. (save-excursion
  5076. (looking-at org-outline-regexp)
  5077. (1- (- (match-end 0) (match-beginning 0)))))
  5078. (defvar org-font-lock-keywords nil)
  5079. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5080. "Regular expression matching a property line.")
  5081. (defvar org-font-lock-hook nil
  5082. "Functions to be called for special font lock stuff.")
  5083. (defvar org-font-lock-set-keywords-hook nil
  5084. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5085. This is called after `org-font-lock-extra-keywords' is defined, but before
  5086. it is installed to be used by font lock. This can be useful if something
  5087. needs to be inserted at a specific position in the font-lock sequence.")
  5088. (defun org-font-lock-hook (limit)
  5089. "Run `org-font-lock-hook' within LIMIT."
  5090. (run-hook-with-args 'org-font-lock-hook limit))
  5091. (defun org-set-font-lock-defaults ()
  5092. "Set font lock defaults for the current buffer."
  5093. (let* ((em org-fontify-emphasized-text)
  5094. (lk org-activate-links)
  5095. (org-font-lock-extra-keywords
  5096. (list
  5097. ;; Call the hook
  5098. '(org-font-lock-hook)
  5099. ;; Headlines
  5100. `(,(if org-fontify-whole-heading-line
  5101. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5102. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5103. (1 (org-get-level-face 1))
  5104. (2 (org-get-level-face 2))
  5105. (3 (org-get-level-face 3)))
  5106. ;; Table lines
  5107. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5108. (1 'org-table t))
  5109. ;; Table internals
  5110. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5111. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5112. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5113. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5114. ;; Drawers
  5115. (list org-drawer-regexp '(0 'org-special-keyword t))
  5116. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5117. ;; Properties
  5118. (list org-property-re
  5119. '(1 'org-special-keyword t)
  5120. '(3 'org-property-value t))
  5121. ;; Links
  5122. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5123. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5124. (if (memq 'plain lk) '(org-activate-plain-links))
  5125. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5126. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5127. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5128. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5129. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5130. '(org-hide-wide-columns (0 nil append))
  5131. ;; TODO keyword
  5132. (list (format org-heading-keyword-regexp-format
  5133. org-todo-regexp)
  5134. '(2 (org-get-todo-face 2) t))
  5135. ;; DONE
  5136. (if org-fontify-done-headline
  5137. (list (format org-heading-keyword-regexp-format
  5138. (concat
  5139. "\\(?:"
  5140. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5141. "\\)"))
  5142. '(2 'org-headline-done t))
  5143. nil)
  5144. ;; Priorities
  5145. '(org-font-lock-add-priority-faces)
  5146. ;; Tags
  5147. '(org-font-lock-add-tag-faces)
  5148. ;; Special keywords
  5149. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5150. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5151. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5152. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5153. ;; Emphasis
  5154. (if em
  5155. (if (featurep 'xemacs)
  5156. '(org-do-emphasis-faces (0 nil append))
  5157. '(org-do-emphasis-faces)))
  5158. ;; Checkboxes
  5159. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5160. 1 'org-checkbox prepend)
  5161. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5162. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5163. (0 (org-get-checkbox-statistics-face) t)))
  5164. ;; Description list items
  5165. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5166. 1 'org-list-dt prepend)
  5167. ;; ARCHIVEd headings
  5168. (list (concat
  5169. org-outline-regexp-bol
  5170. "\\(.*:" org-archive-tag ":.*\\)")
  5171. '(1 'org-archived prepend))
  5172. ;; Specials
  5173. '(org-do-latex-and-special-faces)
  5174. '(org-fontify-entities)
  5175. '(org-raise-scripts)
  5176. ;; Code
  5177. '(org-activate-code (1 'org-code t))
  5178. ;; COMMENT
  5179. (list (format org-heading-keyword-regexp-format
  5180. (concat "\\("
  5181. org-comment-string "\\|" org-quote-string
  5182. "\\)"))
  5183. '(2 'org-special-keyword t))
  5184. ;; Blocks and meta lines
  5185. '(org-fontify-meta-lines-and-blocks)
  5186. )))
  5187. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5188. (run-hooks 'org-font-lock-set-keywords-hook)
  5189. ;; Now set the full font-lock-keywords
  5190. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5191. (org-set-local 'font-lock-defaults
  5192. '(org-font-lock-keywords t nil nil backward-paragraph))
  5193. (kill-local-variable 'font-lock-keywords) nil))
  5194. (defun org-toggle-pretty-entities ()
  5195. "Toggle the composition display of entities as UTF8 characters."
  5196. (interactive)
  5197. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5198. (org-restart-font-lock)
  5199. (if org-pretty-entities
  5200. (message "Entities are displayed as UTF8 characters")
  5201. (save-restriction
  5202. (widen)
  5203. (org-decompose-region (point-min) (point-max))
  5204. (message "Entities are displayed plain"))))
  5205. (defvar org-custom-properties-overlays nil
  5206. "List of overlays used for custom properties.")
  5207. (make-variable-buffer-local 'org-custom-properties-overlays)
  5208. (defun org-toggle-custom-properties-visibility ()
  5209. "Display or hide properties in `org-custom-properties'."
  5210. (interactive)
  5211. (if org-custom-properties-overlays
  5212. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5213. (setq org-custom-properties-overlays nil))
  5214. (unless (not org-custom-properties)
  5215. (save-excursion
  5216. (save-restriction
  5217. (widen)
  5218. (goto-char (point-min))
  5219. (while (re-search-forward org-property-re nil t)
  5220. (mapc (lambda(p)
  5221. (when (equal p (substring (match-string 1) 1 -1))
  5222. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5223. (overlay-put o 'invisible t)
  5224. (overlay-put o 'org-custom-property t)
  5225. (push o org-custom-properties-overlays))))
  5226. org-custom-properties)))))))
  5227. (defun org-fontify-entities (limit)
  5228. "Find an entity to fontify."
  5229. (let (ee)
  5230. (when org-pretty-entities
  5231. (catch 'match
  5232. (while (re-search-forward
  5233. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5234. limit t)
  5235. (if (and (not (org-in-indented-comment-line))
  5236. (setq ee (org-entity-get (match-string 1)))
  5237. (= (length (nth 6 ee)) 1))
  5238. (let*
  5239. ((end (if (equal (match-string 2) "{}")
  5240. (match-end 2)
  5241. (match-end 1))))
  5242. (add-text-properties
  5243. (match-beginning 0) end
  5244. (list 'font-lock-fontified t))
  5245. (compose-region (match-beginning 0) end
  5246. (nth 6 ee) nil)
  5247. (backward-char 1)
  5248. (throw 'match t))))
  5249. nil))))
  5250. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5251. "Fontify string S like in Org-mode."
  5252. (with-temp-buffer
  5253. (insert s)
  5254. (let ((org-odd-levels-only odd-levels))
  5255. (org-mode)
  5256. (font-lock-fontify-buffer)
  5257. (buffer-string))))
  5258. (defvar org-m nil)
  5259. (defvar org-l nil)
  5260. (defvar org-f nil)
  5261. (defun org-get-level-face (n)
  5262. "Get the right face for match N in font-lock matching of headlines."
  5263. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5264. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5265. (if org-cycle-level-faces
  5266. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5267. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5268. (cond
  5269. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5270. ((eq n 2) org-f)
  5271. (t (if org-level-color-stars-only nil org-f))))
  5272. (defun org-get-todo-face (kwd)
  5273. "Get the right face for a TODO keyword KWD.
  5274. If KWD is a number, get the corresponding match group."
  5275. (if (numberp kwd) (setq kwd (match-string kwd)))
  5276. (or (org-face-from-face-or-color
  5277. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5278. (and (member kwd org-done-keywords) 'org-done)
  5279. 'org-todo))
  5280. (defun org-face-from-face-or-color (context inherit face-or-color)
  5281. "Create a face list that inherits INHERIT, but sets the foreground color.
  5282. When FACE-OR-COLOR is not a string, just return it."
  5283. (if (stringp face-or-color)
  5284. (list :inherit inherit
  5285. (cdr (assoc context org-faces-easy-properties))
  5286. face-or-color)
  5287. face-or-color))
  5288. (defun org-font-lock-add-tag-faces (limit)
  5289. "Add the special tag faces."
  5290. (when (and org-tag-faces org-tags-special-faces-re)
  5291. (while (re-search-forward org-tags-special-faces-re limit t)
  5292. (add-text-properties (match-beginning 1) (match-end 1)
  5293. (list 'face (org-get-tag-face 1)
  5294. 'font-lock-fontified t))
  5295. (backward-char 1))))
  5296. (defun org-font-lock-add-priority-faces (limit)
  5297. "Add the special priority faces."
  5298. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5299. (when (save-match-data (org-at-heading-p))
  5300. (add-text-properties
  5301. (match-beginning 0) (match-end 0)
  5302. (list 'face (or (org-face-from-face-or-color
  5303. 'priority 'org-special-keyword
  5304. (cdr (assoc (char-after (match-beginning 1))
  5305. org-priority-faces)))
  5306. 'org-special-keyword)
  5307. 'font-lock-fontified t)))))
  5308. (defun org-get-tag-face (kwd)
  5309. "Get the right face for a TODO keyword KWD.
  5310. If KWD is a number, get the corresponding match group."
  5311. (if (numberp kwd) (setq kwd (match-string kwd)))
  5312. (or (org-face-from-face-or-color
  5313. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5314. 'org-tag))
  5315. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5316. "Remove fontification and activation overlays from links."
  5317. (font-lock-default-unfontify-region beg end)
  5318. (let* ((buffer-undo-list t)
  5319. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5320. (inhibit-modification-hooks t)
  5321. deactivate-mark buffer-file-name buffer-file-truename)
  5322. (org-decompose-region beg end)
  5323. (remove-text-properties beg end
  5324. '(mouse-face t keymap t org-linked-text t
  5325. invisible t intangible t
  5326. org-no-flyspell t org-emphasis t))
  5327. (org-remove-font-lock-display-properties beg end)))
  5328. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5329. ((raise 0.3) (height 0.7))
  5330. ((raise -0.5))
  5331. ((raise 0.5)))
  5332. "Display properties for showing superscripts and subscripts.")
  5333. (defun org-remove-font-lock-display-properties (beg end)
  5334. "Remove specific display properties that have been added by font lock.
  5335. The will remove the raise properties that are used to show superscripts
  5336. and subscripts."
  5337. (let (next prop)
  5338. (while (< beg end)
  5339. (setq next (next-single-property-change beg 'display nil end)
  5340. prop (get-text-property beg 'display))
  5341. (if (member prop org-script-display)
  5342. (put-text-property beg next 'display nil))
  5343. (setq beg next))))
  5344. (defun org-raise-scripts (limit)
  5345. "Add raise properties to sub/superscripts."
  5346. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5347. (if (re-search-forward
  5348. (if (eq org-use-sub-superscripts t)
  5349. org-match-substring-regexp
  5350. org-match-substring-with-braces-regexp)
  5351. limit t)
  5352. (let* ((pos (point)) table-p comment-p
  5353. (mpos (match-beginning 3))
  5354. (emph-p (get-text-property mpos 'org-emphasis))
  5355. (link-p (get-text-property mpos 'mouse-face))
  5356. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5357. (goto-char (point-at-bol))
  5358. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5359. comment-p (org-looking-at-p "[ \t]*#"))
  5360. (goto-char pos)
  5361. ;; FIXME: Should we go back one character here, for a_b^c
  5362. ;; (goto-char (1- pos)) ;????????????????????
  5363. (if (or comment-p emph-p link-p keyw-p)
  5364. t
  5365. (put-text-property (match-beginning 3) (match-end 0)
  5366. 'display
  5367. (if (equal (char-after (match-beginning 2)) ?^)
  5368. (nth (if table-p 3 1) org-script-display)
  5369. (nth (if table-p 2 0) org-script-display)))
  5370. (add-text-properties (match-beginning 2) (match-end 2)
  5371. (list 'invisible t
  5372. 'org-dwidth t 'org-dwidth-n 1))
  5373. (if (and (eq (char-after (match-beginning 3)) ?{)
  5374. (eq (char-before (match-end 3)) ?}))
  5375. (progn
  5376. (add-text-properties
  5377. (match-beginning 3) (1+ (match-beginning 3))
  5378. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5379. (add-text-properties
  5380. (1- (match-end 3)) (match-end 3)
  5381. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5382. t)))))
  5383. ;;;; Visibility cycling, including org-goto and indirect buffer
  5384. ;;; Cycling
  5385. (defvar org-cycle-global-status nil)
  5386. (make-variable-buffer-local 'org-cycle-global-status)
  5387. (defvar org-cycle-subtree-status nil)
  5388. (make-variable-buffer-local 'org-cycle-subtree-status)
  5389. (defvar org-inlinetask-min-level)
  5390. ;;;###autoload
  5391. (defun org-cycle (&optional arg)
  5392. "TAB-action and visibility cycling for Org-mode.
  5393. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5394. is outline visibility cycling, but it also invokes other actions
  5395. in special contexts.
  5396. - When this function is called with a prefix argument, rotate the entire
  5397. buffer through 3 states (global cycling)
  5398. 1. OVERVIEW: Show only top-level headlines.
  5399. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5400. 3. SHOW ALL: Show everything.
  5401. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5402. determined by the variable `org-startup-folded', and by any VISIBILITY
  5403. properties in the buffer.
  5404. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5405. including any drawers.
  5406. - When inside a table, re-align the table and move to the next field.
  5407. - When point is at the beginning of a headline, rotate the subtree started
  5408. by this line through 3 different states (local cycling)
  5409. 1. FOLDED: Only the main headline is shown.
  5410. 2. CHILDREN: The main headline and the direct children are shown.
  5411. From this state, you can move to one of the children
  5412. and zoom in further.
  5413. 3. SUBTREE: Show the entire subtree, including body text.
  5414. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5415. - When point is at the beginning of an empty headline and the variable
  5416. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5417. of the headline by demoting and promoting it to likely levels. This
  5418. speeds up creation document structure by pressing TAB once or several
  5419. times right after creating a new headline.
  5420. - When there is a numeric prefix, go up to a heading with level ARG, do
  5421. a `show-subtree' and return to the previous cursor position. If ARG
  5422. is negative, go up that many levels.
  5423. - When point is not at the beginning of a headline, execute the global
  5424. binding for TAB, which is re-indenting the line. See the option
  5425. `org-cycle-emulate-tab' for details.
  5426. - Special case: if point is at the beginning of the buffer and there is
  5427. no headline in line 1, this function will act as if called with prefix arg
  5428. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5429. But only if also the variable `org-cycle-global-at-bob' is t."
  5430. (interactive "P")
  5431. (org-load-modules-maybe)
  5432. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5433. (and org-cycle-level-after-item/entry-creation
  5434. (or (org-cycle-level)
  5435. (org-cycle-item-indentation))))
  5436. (let* ((limit-level
  5437. (or org-cycle-max-level
  5438. (and (boundp 'org-inlinetask-min-level)
  5439. org-inlinetask-min-level
  5440. (1- org-inlinetask-min-level))))
  5441. (nstars (and limit-level
  5442. (if org-odd-levels-only
  5443. (and limit-level (1- (* limit-level 2)))
  5444. limit-level)))
  5445. (org-outline-regexp
  5446. (if (not (derived-mode-p 'org-mode))
  5447. outline-regexp
  5448. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5449. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5450. (not (looking-at org-outline-regexp))))
  5451. (org-cycle-hook
  5452. (if bob-special
  5453. (delq 'org-optimize-window-after-visibility-change
  5454. (copy-sequence org-cycle-hook))
  5455. org-cycle-hook))
  5456. (pos (point)))
  5457. (if (or bob-special (equal arg '(4)))
  5458. ;; special case: use global cycling
  5459. (setq arg t))
  5460. (cond
  5461. ((equal arg '(16))
  5462. (setq last-command 'dummy)
  5463. (org-set-startup-visibility)
  5464. (message "Startup visibility, plus VISIBILITY properties"))
  5465. ((equal arg '(64))
  5466. (show-all)
  5467. (message "Entire buffer visible, including drawers"))
  5468. ;; Table: enter it or move to the next field.
  5469. ((org-at-table-p 'any)
  5470. (if (org-at-table.el-p)
  5471. (message "Use C-c ' to edit table.el tables")
  5472. (if arg (org-table-edit-field t)
  5473. (org-table-justify-field-maybe)
  5474. (call-interactively 'org-table-next-field))))
  5475. ((run-hook-with-args-until-success
  5476. 'org-tab-after-check-for-table-hook))
  5477. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5478. ((eq arg t) (org-cycle-internal-global))
  5479. ;; Drawers: delegate to `org-flag-drawer'.
  5480. ((and org-drawers org-drawer-regexp
  5481. (save-excursion
  5482. (beginning-of-line 1)
  5483. (looking-at org-drawer-regexp)))
  5484. (org-flag-drawer ; toggle block visibility
  5485. (not (get-char-property (match-end 0) 'invisible))))
  5486. ;; Show-subtree, ARG levels up from here.
  5487. ((integerp arg)
  5488. (save-excursion
  5489. (org-back-to-heading)
  5490. (outline-up-heading (if (< arg 0) (- arg)
  5491. (- (funcall outline-level) arg)))
  5492. (org-show-subtree)))
  5493. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5494. ((and (featurep 'org-inlinetask)
  5495. (org-inlinetask-at-task-p)
  5496. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5497. (org-inlinetask-toggle-visibility))
  5498. ((org-try-cdlatex-tab))
  5499. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5500. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5501. (save-excursion (beginning-of-line 1)
  5502. (looking-at org-outline-regexp)))
  5503. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5504. (org-cycle-internal-local))
  5505. ;; From there: TAB emulation and template completion.
  5506. (buffer-read-only (org-back-to-heading))
  5507. ((run-hook-with-args-until-success
  5508. 'org-tab-after-check-for-cycling-hook))
  5509. ((org-try-structure-completion))
  5510. ((run-hook-with-args-until-success
  5511. 'org-tab-before-tab-emulation-hook))
  5512. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5513. (or (not (bolp))
  5514. (not (looking-at org-outline-regexp))))
  5515. (call-interactively (global-key-binding "\t")))
  5516. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5517. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5518. (or (and (eq org-cycle-emulate-tab 'white)
  5519. (= (match-end 0) (point-at-eol)))
  5520. (and (eq org-cycle-emulate-tab 'whitestart)
  5521. (>= (match-end 0) pos))))
  5522. t
  5523. (eq org-cycle-emulate-tab t))
  5524. (call-interactively (global-key-binding "\t")))
  5525. (t (save-excursion
  5526. (org-back-to-heading)
  5527. (org-cycle)))))))
  5528. (defun org-cycle-internal-global ()
  5529. "Do the global cycling action."
  5530. ;; Hack to avoid display of messages for .org attachments in Gnus
  5531. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5532. (cond
  5533. ((and (eq last-command this-command)
  5534. (eq org-cycle-global-status 'overview))
  5535. ;; We just created the overview - now do table of contents
  5536. ;; This can be slow in very large buffers, so indicate action
  5537. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5538. (unless ga (message "CONTENTS..."))
  5539. (org-content)
  5540. (unless ga (message "CONTENTS...done"))
  5541. (setq org-cycle-global-status 'contents)
  5542. (run-hook-with-args 'org-cycle-hook 'contents))
  5543. ((and (eq last-command this-command)
  5544. (eq org-cycle-global-status 'contents))
  5545. ;; We just showed the table of contents - now show everything
  5546. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5547. (show-all)
  5548. (unless ga (message "SHOW ALL"))
  5549. (setq org-cycle-global-status 'all)
  5550. (run-hook-with-args 'org-cycle-hook 'all))
  5551. (t
  5552. ;; Default action: go to overview
  5553. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5554. (org-overview)
  5555. (unless ga (message "OVERVIEW"))
  5556. (setq org-cycle-global-status 'overview)
  5557. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5558. (defun org-cycle-internal-local ()
  5559. "Do the local cycling action."
  5560. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5561. ;; First, determine end of headline (EOH), end of subtree or item
  5562. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5563. (save-excursion
  5564. (if (org-at-item-p)
  5565. (progn
  5566. (beginning-of-line)
  5567. (setq struct (org-list-struct))
  5568. (setq eoh (point-at-eol))
  5569. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5570. (setq has-children (org-list-has-child-p (point) struct)))
  5571. (org-back-to-heading)
  5572. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5573. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5574. (setq has-children
  5575. (or (save-excursion
  5576. (let ((level (funcall outline-level)))
  5577. (outline-next-heading)
  5578. (and (org-at-heading-p t)
  5579. (> (funcall outline-level) level))))
  5580. (save-excursion
  5581. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5582. ;; Determine end invisible part of buffer (EOL)
  5583. (beginning-of-line 2)
  5584. ;; XEmacs doesn't have `next-single-char-property-change'
  5585. (if (featurep 'xemacs)
  5586. (while (and (not (eobp)) ;; this is like `next-line'
  5587. (get-char-property (1- (point)) 'invisible))
  5588. (beginning-of-line 2))
  5589. (while (and (not (eobp)) ;; this is like `next-line'
  5590. (get-char-property (1- (point)) 'invisible))
  5591. (goto-char (next-single-char-property-change (point) 'invisible))
  5592. (and (eolp) (beginning-of-line 2))))
  5593. (setq eol (point)))
  5594. ;; Find out what to do next and set `this-command'
  5595. (cond
  5596. ((= eos eoh)
  5597. ;; Nothing is hidden behind this heading
  5598. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5599. (message "EMPTY ENTRY")
  5600. (setq org-cycle-subtree-status nil)
  5601. (save-excursion
  5602. (goto-char eos)
  5603. (outline-next-heading)
  5604. (if (outline-invisible-p) (org-flag-heading nil))))
  5605. ((and (or (>= eol eos)
  5606. (not (string-match "\\S-" (buffer-substring eol eos))))
  5607. (or has-children
  5608. (not (setq children-skipped
  5609. org-cycle-skip-children-state-if-no-children))))
  5610. ;; Entire subtree is hidden in one line: children view
  5611. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5612. (if (org-at-item-p)
  5613. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5614. (org-show-entry)
  5615. (org-with-limited-levels (show-children))
  5616. ;; FIXME: This slows down the func way too much.
  5617. ;; How keep drawers hidden in subtree anyway?
  5618. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5619. ;; (org-cycle-hide-drawers 'subtree))
  5620. ;; Fold every list in subtree to top-level items.
  5621. (when (eq org-cycle-include-plain-lists 'integrate)
  5622. (save-excursion
  5623. (org-back-to-heading)
  5624. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5625. (beginning-of-line 1)
  5626. (let* ((struct (org-list-struct))
  5627. (prevs (org-list-prevs-alist struct))
  5628. (end (org-list-get-bottom-point struct)))
  5629. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5630. (org-list-get-all-items (point) struct prevs))
  5631. (goto-char end))))))
  5632. (message "CHILDREN")
  5633. (save-excursion
  5634. (goto-char eos)
  5635. (outline-next-heading)
  5636. (if (outline-invisible-p) (org-flag-heading nil)))
  5637. (setq org-cycle-subtree-status 'children)
  5638. (run-hook-with-args 'org-cycle-hook 'children))
  5639. ((or children-skipped
  5640. (and (eq last-command this-command)
  5641. (eq org-cycle-subtree-status 'children)))
  5642. ;; We just showed the children, or no children are there,
  5643. ;; now show everything.
  5644. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5645. (outline-flag-region eoh eos nil)
  5646. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5647. (setq org-cycle-subtree-status 'subtree)
  5648. (run-hook-with-args 'org-cycle-hook 'subtree))
  5649. (t
  5650. ;; Default action: hide the subtree.
  5651. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5652. (outline-flag-region eoh eos t)
  5653. (message "FOLDED")
  5654. (setq org-cycle-subtree-status 'folded)
  5655. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5656. ;;;###autoload
  5657. (defun org-global-cycle (&optional arg)
  5658. "Cycle the global visibility. For details see `org-cycle'.
  5659. With \\[universal-argument] prefix arg, switch to startup visibility.
  5660. With a numeric prefix, show all headlines up to that level."
  5661. (interactive "P")
  5662. (let ((org-cycle-include-plain-lists
  5663. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5664. (cond
  5665. ((integerp arg)
  5666. (show-all)
  5667. (hide-sublevels arg)
  5668. (setq org-cycle-global-status 'contents))
  5669. ((equal arg '(4))
  5670. (org-set-startup-visibility)
  5671. (message "Startup visibility, plus VISIBILITY properties."))
  5672. (t
  5673. (org-cycle '(4))))))
  5674. (defun org-set-startup-visibility ()
  5675. "Set the visibility required by startup options and properties."
  5676. (cond
  5677. ((eq org-startup-folded t)
  5678. (org-cycle '(4)))
  5679. ((eq org-startup-folded 'content)
  5680. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5681. (org-cycle '(4)) (org-cycle '(4)))))
  5682. (unless (eq org-startup-folded 'showeverything)
  5683. (if org-hide-block-startup (org-hide-block-all))
  5684. (org-set-visibility-according-to-property 'no-cleanup)
  5685. (org-cycle-hide-archived-subtrees 'all)
  5686. (org-cycle-hide-drawers 'all)
  5687. (org-cycle-show-empty-lines t)))
  5688. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5689. "Switch subtree visibilities according to :VISIBILITY: property."
  5690. (interactive)
  5691. (let (org-show-entry-below state)
  5692. (save-excursion
  5693. (goto-char (point-min))
  5694. (while (re-search-forward
  5695. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5696. nil t)
  5697. (setq state (match-string 1))
  5698. (save-excursion
  5699. (org-back-to-heading t)
  5700. (hide-subtree)
  5701. (org-reveal)
  5702. (cond
  5703. ((equal state '("fold" "folded"))
  5704. (hide-subtree))
  5705. ((equal state "children")
  5706. (org-show-hidden-entry)
  5707. (show-children))
  5708. ((equal state "content")
  5709. (save-excursion
  5710. (save-restriction
  5711. (org-narrow-to-subtree)
  5712. (org-content))))
  5713. ((member state '("all" "showall"))
  5714. (show-subtree)))))
  5715. (unless no-cleanup
  5716. (org-cycle-hide-archived-subtrees 'all)
  5717. (org-cycle-hide-drawers 'all)
  5718. (org-cycle-show-empty-lines 'all)))))
  5719. ;; This function uses outline-regexp instead of the more fundamental
  5720. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5721. ;; buffers, where outline-regexp is needed.
  5722. (defun org-overview ()
  5723. "Switch to overview mode, showing only top-level headlines.
  5724. Really, this shows all headlines with level equal or greater than the level
  5725. of the first headline in the buffer. This is important, because if the
  5726. first headline is not level one, then (hide-sublevels 1) gives confusing
  5727. results."
  5728. (interactive)
  5729. (let ((level (save-excursion
  5730. (goto-char (point-min))
  5731. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5732. (progn
  5733. (goto-char (match-beginning 0))
  5734. (funcall outline-level))))))
  5735. (and level (hide-sublevels level))))
  5736. (defun org-content (&optional arg)
  5737. "Show all headlines in the buffer, like a table of contents.
  5738. With numerical argument N, show content up to level N."
  5739. (interactive "P")
  5740. (save-excursion
  5741. ;; Visit all headings and show their offspring
  5742. (and (integerp arg) (org-overview))
  5743. (goto-char (point-max))
  5744. (catch 'exit
  5745. (while (and (progn (condition-case nil
  5746. (outline-previous-visible-heading 1)
  5747. (error (goto-char (point-min))))
  5748. t)
  5749. (looking-at org-outline-regexp))
  5750. (if (integerp arg)
  5751. (show-children (1- arg))
  5752. (show-branches))
  5753. (if (bobp) (throw 'exit nil))))))
  5754. (defun org-optimize-window-after-visibility-change (state)
  5755. "Adjust the window after a change in outline visibility.
  5756. This function is the default value of the hook `org-cycle-hook'."
  5757. (when (get-buffer-window (current-buffer))
  5758. (cond
  5759. ((eq state 'content) nil)
  5760. ((eq state 'all) nil)
  5761. ((eq state 'folded) nil)
  5762. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5763. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5764. (defun org-remove-empty-overlays-at (pos)
  5765. "Remove outline overlays that do not contain non-white stuff."
  5766. (mapc
  5767. (lambda (o)
  5768. (and (eq 'outline (overlay-get o 'invisible))
  5769. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5770. (overlay-end o))))
  5771. (delete-overlay o)))
  5772. (overlays-at pos)))
  5773. (defun org-clean-visibility-after-subtree-move ()
  5774. "Fix visibility issues after moving a subtree."
  5775. ;; First, find a reasonable region to look at:
  5776. ;; Start two siblings above, end three below
  5777. (let* ((beg (save-excursion
  5778. (and (org-get-last-sibling)
  5779. (org-get-last-sibling))
  5780. (point)))
  5781. (end (save-excursion
  5782. (and (org-get-next-sibling)
  5783. (org-get-next-sibling)
  5784. (org-get-next-sibling))
  5785. (if (org-at-heading-p)
  5786. (point-at-eol)
  5787. (point))))
  5788. (level (looking-at "\\*+"))
  5789. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5790. (save-excursion
  5791. (save-restriction
  5792. (narrow-to-region beg end)
  5793. (when re
  5794. ;; Properly fold already folded siblings
  5795. (goto-char (point-min))
  5796. (while (re-search-forward re nil t)
  5797. (if (and (not (outline-invisible-p))
  5798. (save-excursion
  5799. (goto-char (point-at-eol)) (outline-invisible-p)))
  5800. (hide-entry))))
  5801. (org-cycle-show-empty-lines 'overview)
  5802. (org-cycle-hide-drawers 'overview)))))
  5803. (defun org-cycle-show-empty-lines (state)
  5804. "Show empty lines above all visible headlines.
  5805. The region to be covered depends on STATE when called through
  5806. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5807. entire buffer covered. Note that an empty line is only shown if there
  5808. are at least `org-cycle-separator-lines' empty lines before the headline."
  5809. (when (not (= org-cycle-separator-lines 0))
  5810. (save-excursion
  5811. (let* ((n (abs org-cycle-separator-lines))
  5812. (re (cond
  5813. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5814. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5815. (t (let ((ns (number-to-string (- n 2))))
  5816. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5817. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5818. beg end b e)
  5819. (cond
  5820. ((memq state '(overview contents t))
  5821. (setq beg (point-min) end (point-max)))
  5822. ((memq state '(children folded))
  5823. (setq beg (point) end (progn (org-end-of-subtree t t)
  5824. (beginning-of-line 2)
  5825. (point)))))
  5826. (when beg
  5827. (goto-char beg)
  5828. (while (re-search-forward re end t)
  5829. (unless (get-char-property (match-end 1) 'invisible)
  5830. (setq e (match-end 1))
  5831. (if (< org-cycle-separator-lines 0)
  5832. (setq b (save-excursion
  5833. (goto-char (match-beginning 0))
  5834. (org-back-over-empty-lines)
  5835. (if (save-excursion
  5836. (goto-char (max (point-min) (1- (point))))
  5837. (org-at-heading-p))
  5838. (1- (point))
  5839. (point))))
  5840. (setq b (match-beginning 1)))
  5841. (outline-flag-region b e nil)))))))
  5842. ;; Never hide empty lines at the end of the file.
  5843. (save-excursion
  5844. (goto-char (point-max))
  5845. (outline-previous-heading)
  5846. (outline-end-of-heading)
  5847. (if (and (looking-at "[ \t\n]+")
  5848. (= (match-end 0) (point-max)))
  5849. (outline-flag-region (point) (match-end 0) nil))))
  5850. (defun org-show-empty-lines-in-parent ()
  5851. "Move to the parent and re-show empty lines before visible headlines."
  5852. (save-excursion
  5853. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5854. (org-cycle-show-empty-lines context))))
  5855. (defun org-files-list ()
  5856. "Return `org-agenda-files' list, plus all open org-mode files.
  5857. This is useful for operations that need to scan all of a user's
  5858. open and agenda-wise Org files."
  5859. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5860. (dolist (buf (buffer-list))
  5861. (with-current-buffer buf
  5862. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5863. (let ((file (expand-file-name (buffer-file-name))))
  5864. (unless (member file files)
  5865. (push file files))))))
  5866. files))
  5867. (defsubst org-entry-beginning-position ()
  5868. "Return the beginning position of the current entry."
  5869. (save-excursion (outline-back-to-heading t) (point)))
  5870. (defsubst org-entry-end-position ()
  5871. "Return the end position of the current entry."
  5872. (save-excursion (outline-next-heading) (point)))
  5873. (defun org-cycle-hide-drawers (state)
  5874. "Re-hide all drawers after a visibility state change."
  5875. (when (and (derived-mode-p 'org-mode)
  5876. (not (memq state '(overview folded contents))))
  5877. (save-excursion
  5878. (let* ((globalp (memq state '(contents all)))
  5879. (beg (if globalp (point-min) (point)))
  5880. (end (if globalp (point-max)
  5881. (if (eq state 'children)
  5882. (save-excursion (outline-next-heading) (point))
  5883. (org-end-of-subtree t)))))
  5884. (goto-char beg)
  5885. (while (re-search-forward org-drawer-regexp end t)
  5886. (org-flag-drawer t))))))
  5887. (defun org-flag-drawer (flag)
  5888. "When FLAG is non-nil, hide the drawer we are within.
  5889. Otherwise make it visible."
  5890. (save-excursion
  5891. (beginning-of-line 1)
  5892. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5893. (let ((b (match-end 0)))
  5894. (if (re-search-forward
  5895. "^[ \t]*:END:"
  5896. (save-excursion (outline-next-heading) (point)) t)
  5897. (outline-flag-region b (point-at-eol) flag)
  5898. (error ":END: line missing at position %s" b))))))
  5899. (defun org-subtree-end-visible-p ()
  5900. "Is the end of the current subtree visible?"
  5901. (pos-visible-in-window-p
  5902. (save-excursion (org-end-of-subtree t) (point))))
  5903. (defun org-first-headline-recenter (&optional N)
  5904. "Move cursor to the first headline and recenter the headline.
  5905. Optional argument N means put the headline into the Nth line of the window."
  5906. (goto-char (point-min))
  5907. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5908. (beginning-of-line)
  5909. (recenter (prefix-numeric-value N))))
  5910. ;;; Saving and restoring visibility
  5911. (defun org-outline-overlay-data (&optional use-markers)
  5912. "Return a list of the locations of all outline overlays.
  5913. These are overlays with the `invisible' property value `outline'.
  5914. The return value is a list of cons cells, with start and stop
  5915. positions for each overlay.
  5916. If USE-MARKERS is set, return the positions as markers."
  5917. (let (beg end)
  5918. (save-excursion
  5919. (save-restriction
  5920. (widen)
  5921. (delq nil
  5922. (mapcar (lambda (o)
  5923. (when (eq (overlay-get o 'invisible) 'outline)
  5924. (setq beg (overlay-start o)
  5925. end (overlay-end o))
  5926. (and beg end (> end beg)
  5927. (if use-markers
  5928. (cons (move-marker (make-marker) beg)
  5929. (move-marker (make-marker) end))
  5930. (cons beg end)))))
  5931. (overlays-in (point-min) (point-max))))))))
  5932. (defun org-set-outline-overlay-data (data)
  5933. "Create visibility overlays for all positions in DATA.
  5934. DATA should have been made by `org-outline-overlay-data'."
  5935. (let (o)
  5936. (save-excursion
  5937. (save-restriction
  5938. (widen)
  5939. (show-all)
  5940. (mapc (lambda (c)
  5941. (outline-flag-region (car c) (cdr c) t))
  5942. data)))))
  5943. ;;; Folding of blocks
  5944. (defvar org-hide-block-overlays nil
  5945. "Overlays hiding blocks.")
  5946. (make-variable-buffer-local 'org-hide-block-overlays)
  5947. (defun org-block-map (function &optional start end)
  5948. "Call FUNCTION at the head of all source blocks in the current buffer.
  5949. Optional arguments START and END can be used to limit the range."
  5950. (let ((start (or start (point-min)))
  5951. (end (or end (point-max))))
  5952. (save-excursion
  5953. (goto-char start)
  5954. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5955. (save-excursion
  5956. (save-match-data
  5957. (goto-char (match-beginning 0))
  5958. (funcall function)))))))
  5959. (defun org-hide-block-toggle-all ()
  5960. "Toggle the visibility of all blocks in the current buffer."
  5961. (org-block-map #'org-hide-block-toggle))
  5962. (defun org-hide-block-all ()
  5963. "Fold all blocks in the current buffer."
  5964. (interactive)
  5965. (org-show-block-all)
  5966. (org-block-map #'org-hide-block-toggle-maybe))
  5967. (defun org-show-block-all ()
  5968. "Unfold all blocks in the current buffer."
  5969. (interactive)
  5970. (mapc 'delete-overlay org-hide-block-overlays)
  5971. (setq org-hide-block-overlays nil))
  5972. (defun org-hide-block-toggle-maybe ()
  5973. "Toggle visibility of block at point."
  5974. (interactive)
  5975. (let ((case-fold-search t))
  5976. (if (save-excursion
  5977. (beginning-of-line 1)
  5978. (looking-at org-block-regexp))
  5979. (progn (org-hide-block-toggle)
  5980. t) ;; to signal that we took action
  5981. nil))) ;; to signal that we did not
  5982. (defun org-hide-block-toggle (&optional force)
  5983. "Toggle the visibility of the current block."
  5984. (interactive)
  5985. (save-excursion
  5986. (beginning-of-line)
  5987. (if (re-search-forward org-block-regexp nil t)
  5988. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5989. (end (match-end 0)) ;; end of entire body
  5990. ov)
  5991. (if (memq t (mapcar (lambda (overlay)
  5992. (eq (overlay-get overlay 'invisible)
  5993. 'org-hide-block))
  5994. (overlays-at start)))
  5995. (if (or (not force) (eq force 'off))
  5996. (mapc (lambda (ov)
  5997. (when (member ov org-hide-block-overlays)
  5998. (setq org-hide-block-overlays
  5999. (delq ov org-hide-block-overlays)))
  6000. (when (eq (overlay-get ov 'invisible)
  6001. 'org-hide-block)
  6002. (delete-overlay ov)))
  6003. (overlays-at start)))
  6004. (setq ov (make-overlay start end))
  6005. (overlay-put ov 'invisible 'org-hide-block)
  6006. ;; make the block accessible to isearch
  6007. (overlay-put
  6008. ov 'isearch-open-invisible
  6009. (lambda (ov)
  6010. (when (member ov org-hide-block-overlays)
  6011. (setq org-hide-block-overlays
  6012. (delq ov org-hide-block-overlays)))
  6013. (when (eq (overlay-get ov 'invisible)
  6014. 'org-hide-block)
  6015. (delete-overlay ov))))
  6016. (push ov org-hide-block-overlays)))
  6017. (error "Not looking at a source block"))))
  6018. ;; org-tab-after-check-for-cycling-hook
  6019. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6020. ;; Remove overlays when changing major mode
  6021. (add-hook 'org-mode-hook
  6022. (lambda () (org-add-hook 'change-major-mode-hook
  6023. 'org-show-block-all 'append 'local)))
  6024. ;;; Org-goto
  6025. (defvar org-goto-window-configuration nil)
  6026. (defvar org-goto-marker nil)
  6027. (defvar org-goto-map
  6028. (let ((map (make-sparse-keymap)))
  6029. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6030. (while (setq cmd (pop cmds))
  6031. (substitute-key-definition cmd cmd map global-map)))
  6032. (suppress-keymap map)
  6033. (org-defkey map "\C-m" 'org-goto-ret)
  6034. (org-defkey map [(return)] 'org-goto-ret)
  6035. (org-defkey map [(left)] 'org-goto-left)
  6036. (org-defkey map [(right)] 'org-goto-right)
  6037. (org-defkey map [(control ?g)] 'org-goto-quit)
  6038. (org-defkey map "\C-i" 'org-cycle)
  6039. (org-defkey map [(tab)] 'org-cycle)
  6040. (org-defkey map [(down)] 'outline-next-visible-heading)
  6041. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6042. (if org-goto-auto-isearch
  6043. (if (fboundp 'define-key-after)
  6044. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6045. nil)
  6046. (org-defkey map "q" 'org-goto-quit)
  6047. (org-defkey map "n" 'outline-next-visible-heading)
  6048. (org-defkey map "p" 'outline-previous-visible-heading)
  6049. (org-defkey map "f" 'outline-forward-same-level)
  6050. (org-defkey map "b" 'outline-backward-same-level)
  6051. (org-defkey map "u" 'outline-up-heading))
  6052. (org-defkey map "/" 'org-occur)
  6053. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6054. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6055. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6056. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6057. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6058. map))
  6059. (defconst org-goto-help
  6060. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6061. RET=jump to location [Q]uit and return to previous location
  6062. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6063. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6064. ;; FIXME: Docstring does not mention both interfaces
  6065. (defun org-goto (&optional alternative-interface)
  6066. "Look up a different location in the current file, keeping current visibility.
  6067. When you want look-up or go to a different location in a
  6068. document, the fastest way is often to fold the entire buffer and
  6069. then dive into the tree. This method has the disadvantage, that
  6070. the previous location will be folded, which may not be what you
  6071. want.
  6072. This command works around this by showing a copy of the current
  6073. buffer in an indirect buffer, in overview mode. You can dive
  6074. into the tree in that copy, use org-occur and incremental search
  6075. to find a location. When pressing RET or `Q', the command
  6076. returns to the original buffer in which the visibility is still
  6077. unchanged. After RET it will also jump to the location selected
  6078. in the indirect buffer and expose the headline hierarchy above.
  6079. With a prefix argument, use the alternative interface: e.g. if
  6080. `org-goto-interface' is 'outline use 'outline-path-completion."
  6081. (interactive "P")
  6082. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6083. (org-refile-use-outline-path t)
  6084. (org-refile-target-verify-function nil)
  6085. (interface
  6086. (if (not alternative-interface)
  6087. org-goto-interface
  6088. (if (eq org-goto-interface 'outline)
  6089. 'outline-path-completion
  6090. 'outline)))
  6091. (org-goto-start-pos (point))
  6092. (selected-point
  6093. (if (eq interface 'outline)
  6094. (car (org-get-location (current-buffer) org-goto-help))
  6095. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6096. (org-refile-check-position pa)
  6097. (nth 3 pa)))))
  6098. (if selected-point
  6099. (progn
  6100. (org-mark-ring-push org-goto-start-pos)
  6101. (goto-char selected-point)
  6102. (if (or (outline-invisible-p) (org-invisible-p2))
  6103. (org-show-context 'org-goto)))
  6104. (message "Quit"))))
  6105. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6106. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6107. (defvar org-goto-local-auto-isearch-map) ; defined below
  6108. (defun org-get-location (buf help)
  6109. "Let the user select a location in the Org-mode buffer BUF.
  6110. This function uses a recursive edit. It returns the selected position
  6111. or nil."
  6112. (org-no-popups
  6113. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6114. (isearch-hide-immediately nil)
  6115. (isearch-search-fun-function
  6116. (lambda () 'org-goto-local-search-headings))
  6117. (org-goto-selected-point org-goto-exit-command))
  6118. (save-excursion
  6119. (save-window-excursion
  6120. (delete-other-windows)
  6121. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6122. (org-pop-to-buffer-same-window
  6123. (condition-case nil
  6124. (make-indirect-buffer (current-buffer) "*org-goto*")
  6125. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6126. (with-output-to-temp-buffer "*Help*"
  6127. (princ help))
  6128. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6129. (setq buffer-read-only nil)
  6130. (let ((org-startup-truncated t)
  6131. (org-startup-folded nil)
  6132. (org-startup-align-all-tables nil))
  6133. (org-mode)
  6134. (org-overview))
  6135. (setq buffer-read-only t)
  6136. (if (and (boundp 'org-goto-start-pos)
  6137. (integer-or-marker-p org-goto-start-pos))
  6138. (let ((org-show-hierarchy-above t)
  6139. (org-show-siblings t)
  6140. (org-show-following-heading t))
  6141. (goto-char org-goto-start-pos)
  6142. (and (outline-invisible-p) (org-show-context)))
  6143. (goto-char (point-min)))
  6144. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6145. (message "Select location and press RET")
  6146. (use-local-map org-goto-map)
  6147. (recursive-edit)
  6148. ))
  6149. (kill-buffer "*org-goto*")
  6150. (cons org-goto-selected-point org-goto-exit-command))))
  6151. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6152. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6153. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6154. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6155. (defun org-goto-local-search-headings (string bound noerror)
  6156. "Search and make sure that any matches are in headlines."
  6157. (catch 'return
  6158. (while (if isearch-forward
  6159. (search-forward string bound noerror)
  6160. (search-backward string bound noerror))
  6161. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6162. (and (member :headline context)
  6163. (not (member :tags context))))
  6164. (throw 'return (point))))))
  6165. (defun org-goto-local-auto-isearch ()
  6166. "Start isearch."
  6167. (interactive)
  6168. (goto-char (point-min))
  6169. (let ((keys (this-command-keys)))
  6170. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6171. (isearch-mode t)
  6172. (isearch-process-search-char (string-to-char keys)))))
  6173. (defun org-goto-ret (&optional arg)
  6174. "Finish `org-goto' by going to the new location."
  6175. (interactive "P")
  6176. (setq org-goto-selected-point (point)
  6177. org-goto-exit-command 'return)
  6178. (throw 'exit nil))
  6179. (defun org-goto-left ()
  6180. "Finish `org-goto' by going to the new location."
  6181. (interactive)
  6182. (if (org-at-heading-p)
  6183. (progn
  6184. (beginning-of-line 1)
  6185. (setq org-goto-selected-point (point)
  6186. org-goto-exit-command 'left)
  6187. (throw 'exit nil))
  6188. (error "Not on a heading")))
  6189. (defun org-goto-right ()
  6190. "Finish `org-goto' by going to the new location."
  6191. (interactive)
  6192. (if (org-at-heading-p)
  6193. (progn
  6194. (setq org-goto-selected-point (point)
  6195. org-goto-exit-command 'right)
  6196. (throw 'exit nil))
  6197. (error "Not on a heading")))
  6198. (defun org-goto-quit ()
  6199. "Finish `org-goto' without cursor motion."
  6200. (interactive)
  6201. (setq org-goto-selected-point nil)
  6202. (setq org-goto-exit-command 'quit)
  6203. (throw 'exit nil))
  6204. ;;; Indirect buffer display of subtrees
  6205. (defvar org-indirect-dedicated-frame nil
  6206. "This is the frame being used for indirect tree display.")
  6207. (defvar org-last-indirect-buffer nil)
  6208. (defun org-tree-to-indirect-buffer (&optional arg)
  6209. "Create indirect buffer and narrow it to current subtree.
  6210. With a numerical prefix ARG, go up to this level and then take that tree.
  6211. If ARG is negative, go up that many levels.
  6212. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6213. indirect buffer previously made with this command, to avoid proliferation of
  6214. indirect buffers. However, when you call the command with a \
  6215. \\[universal-argument] prefix, or
  6216. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6217. is kept so that you can work with several indirect buffers at the same time.
  6218. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6219. \\[universal-argument] prefix also
  6220. requests that a new frame be made for the new buffer, so that the dedicated
  6221. frame is not changed."
  6222. (interactive "P")
  6223. (let ((cbuf (current-buffer))
  6224. (cwin (selected-window))
  6225. (pos (point))
  6226. beg end level heading ibuf)
  6227. (save-excursion
  6228. (org-back-to-heading t)
  6229. (when (numberp arg)
  6230. (setq level (org-outline-level))
  6231. (if (< arg 0) (setq arg (+ level arg)))
  6232. (while (> (setq level (org-outline-level)) arg)
  6233. (org-up-heading-safe)))
  6234. (setq beg (point)
  6235. heading (org-get-heading))
  6236. (org-end-of-subtree t t)
  6237. (if (org-at-heading-p) (backward-char 1))
  6238. (setq end (point)))
  6239. (if (and (buffer-live-p org-last-indirect-buffer)
  6240. (not (eq org-indirect-buffer-display 'new-frame))
  6241. (not arg))
  6242. (kill-buffer org-last-indirect-buffer))
  6243. (setq ibuf (org-get-indirect-buffer cbuf)
  6244. org-last-indirect-buffer ibuf)
  6245. (cond
  6246. ((or (eq org-indirect-buffer-display 'new-frame)
  6247. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6248. (select-frame (make-frame))
  6249. (delete-other-windows)
  6250. (org-pop-to-buffer-same-window ibuf)
  6251. (org-set-frame-title heading))
  6252. ((eq org-indirect-buffer-display 'dedicated-frame)
  6253. (raise-frame
  6254. (select-frame (or (and org-indirect-dedicated-frame
  6255. (frame-live-p org-indirect-dedicated-frame)
  6256. org-indirect-dedicated-frame)
  6257. (setq org-indirect-dedicated-frame (make-frame)))))
  6258. (delete-other-windows)
  6259. (org-pop-to-buffer-same-window ibuf)
  6260. (org-set-frame-title (concat "Indirect: " heading)))
  6261. ((eq org-indirect-buffer-display 'current-window)
  6262. (org-pop-to-buffer-same-window ibuf))
  6263. ((eq org-indirect-buffer-display 'other-window)
  6264. (pop-to-buffer ibuf))
  6265. (t (error "Invalid value")))
  6266. (if (featurep 'xemacs)
  6267. (save-excursion (org-mode) (turn-on-font-lock)))
  6268. (narrow-to-region beg end)
  6269. (show-all)
  6270. (goto-char pos)
  6271. (run-hook-with-args 'org-cycle-hook 'all)
  6272. (and (window-live-p cwin) (select-window cwin))))
  6273. (defun org-get-indirect-buffer (&optional buffer)
  6274. (setq buffer (or buffer (current-buffer)))
  6275. (let ((n 1) (base (buffer-name buffer)) bname)
  6276. (while (buffer-live-p
  6277. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6278. (setq n (1+ n)))
  6279. (condition-case nil
  6280. (make-indirect-buffer buffer bname 'clone)
  6281. (error (make-indirect-buffer buffer bname)))))
  6282. (defun org-set-frame-title (title)
  6283. "Set the title of the current frame to the string TITLE."
  6284. ;; FIXME: how to name a single frame in XEmacs???
  6285. (unless (featurep 'xemacs)
  6286. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6287. ;;;; Structure editing
  6288. ;;; Inserting headlines
  6289. (defun org-previous-line-empty-p ()
  6290. (save-excursion
  6291. (and (not (bobp))
  6292. (or (beginning-of-line 0) t)
  6293. (save-match-data
  6294. (looking-at "[ \t]*$")))))
  6295. (defun org-insert-heading (&optional force-heading invisible-ok)
  6296. "Insert a new heading or item with same depth at point.
  6297. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6298. If point is at the beginning of a headline, insert a sibling before the
  6299. current headline. If point is not at the beginning, split the line,
  6300. create the new headline with the text in the current line after point
  6301. \(but see also the variable `org-M-RET-may-split-line').
  6302. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6303. This is important for non-interactive uses of the command."
  6304. (interactive "P")
  6305. (if (or (= (buffer-size) 0)
  6306. (and (not (save-excursion
  6307. (and (ignore-errors (org-back-to-heading invisible-ok))
  6308. (org-at-heading-p))))
  6309. (or force-heading (not (org-in-item-p)))))
  6310. (progn
  6311. (insert "\n* ")
  6312. (run-hooks 'org-insert-heading-hook))
  6313. (when (or force-heading (not (org-insert-item)))
  6314. (let* ((empty-line-p nil)
  6315. (level nil)
  6316. (on-heading (org-at-heading-p))
  6317. (head (save-excursion
  6318. (condition-case nil
  6319. (progn
  6320. (org-back-to-heading invisible-ok)
  6321. (when (and (not on-heading)
  6322. (featurep 'org-inlinetask)
  6323. (integerp org-inlinetask-min-level)
  6324. (>= (length (match-string 0))
  6325. org-inlinetask-min-level))
  6326. ;; Find a heading level before the inline task
  6327. (while (and (setq level (org-up-heading-safe))
  6328. (>= level org-inlinetask-min-level)))
  6329. (if (org-at-heading-p)
  6330. (org-back-to-heading invisible-ok)
  6331. (error "This should not happen")))
  6332. (setq empty-line-p (org-previous-line-empty-p))
  6333. (match-string 0))
  6334. (error "*"))))
  6335. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6336. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6337. pos hide-previous previous-pos)
  6338. (cond
  6339. ((and (org-at-heading-p) (bolp)
  6340. (or (bobp)
  6341. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6342. ;; insert before the current line
  6343. (open-line (if blank 2 1)))
  6344. ((and (bolp)
  6345. (not org-insert-heading-respect-content)
  6346. (or (bobp)
  6347. (save-excursion
  6348. (backward-char 1) (not (outline-invisible-p)))))
  6349. ;; insert right here
  6350. nil)
  6351. (t
  6352. ;; somewhere in the line
  6353. (save-excursion
  6354. (setq previous-pos (point-at-bol))
  6355. (end-of-line)
  6356. (setq hide-previous (outline-invisible-p)))
  6357. (and org-insert-heading-respect-content (org-show-subtree))
  6358. (let ((split
  6359. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6360. (save-excursion
  6361. (let ((p (point)))
  6362. (goto-char (point-at-bol))
  6363. (and (looking-at org-complex-heading-regexp)
  6364. (match-beginning 4)
  6365. (> p (match-beginning 4)))))))
  6366. tags pos)
  6367. (cond
  6368. (org-insert-heading-respect-content
  6369. (org-end-of-subtree nil t)
  6370. (when (featurep 'org-inlinetask)
  6371. (while (and (not (eobp))
  6372. (looking-at "\\(\\*+\\)[ \t]+")
  6373. (>= (length (match-string 1))
  6374. org-inlinetask-min-level))
  6375. (org-end-of-subtree nil t)))
  6376. (or (bolp) (newline))
  6377. (or (org-previous-line-empty-p)
  6378. (and blank (newline)))
  6379. (open-line 1))
  6380. ((org-at-heading-p)
  6381. (when hide-previous
  6382. (show-children)
  6383. (org-show-entry))
  6384. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6385. (setq tags (and (match-end 2) (match-string 2)))
  6386. (and (match-end 1)
  6387. (delete-region (match-beginning 1) (match-end 1)))
  6388. (setq pos (point-at-bol))
  6389. (or split (end-of-line 1))
  6390. (delete-horizontal-space)
  6391. (if (string-match "\\`\\*+\\'"
  6392. (buffer-substring (point-at-bol) (point)))
  6393. (insert " "))
  6394. (newline (if blank 2 1))
  6395. (when tags
  6396. (save-excursion
  6397. (goto-char pos)
  6398. (end-of-line 1)
  6399. (insert " " tags)
  6400. (org-set-tags nil 'align))))
  6401. (t
  6402. (or split (end-of-line 1))
  6403. (newline (if blank 2 1)))))))
  6404. (insert head) (just-one-space)
  6405. (setq pos (point))
  6406. (end-of-line 1)
  6407. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6408. (when (and org-insert-heading-respect-content hide-previous)
  6409. (save-excursion
  6410. (goto-char previous-pos)
  6411. (hide-subtree)))
  6412. (run-hooks 'org-insert-heading-hook)))))
  6413. (defun org-get-heading (&optional no-tags no-todo)
  6414. "Return the heading of the current entry, without the stars.
  6415. When NO-TAGS is non-nil, don't include tags.
  6416. When NO-TODO is non-nil, don't include TODO keywords."
  6417. (save-excursion
  6418. (org-back-to-heading t)
  6419. (cond
  6420. ((and no-tags no-todo)
  6421. (looking-at org-complex-heading-regexp)
  6422. (match-string 4))
  6423. (no-tags
  6424. (looking-at (concat org-outline-regexp
  6425. "\\(.*?\\)"
  6426. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6427. (match-string 1))
  6428. (no-todo
  6429. (looking-at org-todo-line-regexp)
  6430. (match-string 3))
  6431. (t (looking-at org-heading-regexp)
  6432. (match-string 2)))))
  6433. (defun org-heading-components ()
  6434. "Return the components of the current heading.
  6435. This is a list with the following elements:
  6436. - the level as an integer
  6437. - the reduced level, different if `org-odd-levels-only' is set.
  6438. - the TODO keyword, or nil
  6439. - the priority character, like ?A, or nil if no priority is given
  6440. - the headline text itself, or the tags string if no headline text
  6441. - the tags string, or nil."
  6442. (save-excursion
  6443. (org-back-to-heading t)
  6444. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6445. (list (length (match-string 1))
  6446. (org-reduced-level (length (match-string 1)))
  6447. (org-match-string-no-properties 2)
  6448. (and (match-end 3) (aref (match-string 3) 2))
  6449. (org-match-string-no-properties 4)
  6450. (org-match-string-no-properties 5)))))
  6451. (defun org-get-entry ()
  6452. "Get the entry text, after heading, entire subtree."
  6453. (save-excursion
  6454. (org-back-to-heading t)
  6455. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6456. (defun org-insert-heading-after-current ()
  6457. "Insert a new heading with same level as current, after current subtree."
  6458. (interactive)
  6459. (org-back-to-heading)
  6460. (org-insert-heading)
  6461. (org-move-subtree-down)
  6462. (end-of-line 1))
  6463. (defun org-insert-heading-respect-content ()
  6464. (interactive)
  6465. (let ((org-insert-heading-respect-content t))
  6466. (org-insert-heading t)))
  6467. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6468. (interactive "P")
  6469. (let ((org-insert-heading-respect-content t))
  6470. (org-insert-todo-heading force-state t)))
  6471. (defun org-insert-todo-heading (arg &optional force-heading)
  6472. "Insert a new heading with the same level and TODO state as current heading.
  6473. If the heading has no TODO state, or if the state is DONE, use the first
  6474. state (TODO by default). Also with prefix arg, force first state."
  6475. (interactive "P")
  6476. (when (or force-heading (not (org-insert-item 'checkbox)))
  6477. (org-insert-heading force-heading)
  6478. (save-excursion
  6479. (org-back-to-heading)
  6480. (outline-previous-heading)
  6481. (looking-at org-todo-line-regexp))
  6482. (let*
  6483. ((new-mark-x
  6484. (if (or arg
  6485. (not (match-beginning 2))
  6486. (member (match-string 2) org-done-keywords))
  6487. (car org-todo-keywords-1)
  6488. (match-string 2)))
  6489. (new-mark
  6490. (or
  6491. (run-hook-with-args-until-success
  6492. 'org-todo-get-default-hook new-mark-x nil)
  6493. new-mark-x)))
  6494. (beginning-of-line 1)
  6495. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6496. (if org-treat-insert-todo-heading-as-state-change
  6497. (org-todo new-mark)
  6498. (insert new-mark " "))))
  6499. (when org-provide-todo-statistics
  6500. (org-update-parent-todo-statistics))))
  6501. (defun org-insert-subheading (arg)
  6502. "Insert a new subheading and demote it.
  6503. Works for outline headings and for plain lists alike."
  6504. (interactive "P")
  6505. (org-insert-heading arg)
  6506. (cond
  6507. ((org-at-heading-p) (org-do-demote))
  6508. ((org-at-item-p) (org-indent-item))))
  6509. (defun org-insert-todo-subheading (arg)
  6510. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6511. Works for outline headings and for plain lists alike."
  6512. (interactive "P")
  6513. (org-insert-todo-heading arg)
  6514. (cond
  6515. ((org-at-heading-p) (org-do-demote))
  6516. ((org-at-item-p) (org-indent-item))))
  6517. ;;; Promotion and Demotion
  6518. (defvar org-after-demote-entry-hook nil
  6519. "Hook run after an entry has been demoted.
  6520. The cursor will be at the beginning of the entry.
  6521. When a subtree is being demoted, the hook will be called for each node.")
  6522. (defvar org-after-promote-entry-hook nil
  6523. "Hook run after an entry has been promoted.
  6524. The cursor will be at the beginning of the entry.
  6525. When a subtree is being promoted, the hook will be called for each node.")
  6526. (defun org-promote-subtree ()
  6527. "Promote the entire subtree.
  6528. See also `org-promote'."
  6529. (interactive)
  6530. (save-excursion
  6531. (org-with-limited-levels (org-map-tree 'org-promote)))
  6532. (org-fix-position-after-promote))
  6533. (defun org-demote-subtree ()
  6534. "Demote the entire subtree. See `org-demote'.
  6535. See also `org-promote'."
  6536. (interactive)
  6537. (save-excursion
  6538. (org-with-limited-levels (org-map-tree 'org-demote)))
  6539. (org-fix-position-after-promote))
  6540. (defun org-do-promote ()
  6541. "Promote the current heading higher up the tree.
  6542. If the region is active in `transient-mark-mode', promote all headings
  6543. in the region."
  6544. (interactive)
  6545. (save-excursion
  6546. (if (org-region-active-p)
  6547. (org-map-region 'org-promote (region-beginning) (region-end))
  6548. (org-promote)))
  6549. (org-fix-position-after-promote))
  6550. (defun org-do-demote ()
  6551. "Demote the current heading lower down the tree.
  6552. If the region is active in `transient-mark-mode', demote all headings
  6553. in the region."
  6554. (interactive)
  6555. (save-excursion
  6556. (if (org-region-active-p)
  6557. (org-map-region 'org-demote (region-beginning) (region-end))
  6558. (org-demote)))
  6559. (org-fix-position-after-promote))
  6560. (defun org-fix-position-after-promote ()
  6561. "Make sure that after pro/demotion cursor position is right."
  6562. (let ((pos (point)))
  6563. (when (save-excursion
  6564. (beginning-of-line 1)
  6565. (looking-at org-todo-line-regexp)
  6566. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6567. (cond ((eobp) (insert " "))
  6568. ((eolp) (insert " "))
  6569. ((equal (char-after) ?\ ) (forward-char 1))))))
  6570. (defun org-current-level ()
  6571. "Return the level of the current entry, or nil if before the first headline.
  6572. The level is the number of stars at the beginning of the headline."
  6573. (save-excursion
  6574. (org-with-limited-levels
  6575. (if (ignore-errors (org-back-to-heading t))
  6576. (funcall outline-level)))))
  6577. (defun org-get-previous-line-level ()
  6578. "Return the outline depth of the last headline before the current line.
  6579. Returns 0 for the first headline in the buffer, and nil if before the
  6580. first headline."
  6581. (let ((current-level (org-current-level))
  6582. (prev-level (when (> (line-number-at-pos) 1)
  6583. (save-excursion
  6584. (beginning-of-line 0)
  6585. (org-current-level)))))
  6586. (cond ((null current-level) nil) ; Before first headline
  6587. ((null prev-level) 0) ; At first headline
  6588. (prev-level))))
  6589. (defun org-reduced-level (l)
  6590. "Compute the effective level of a heading.
  6591. This takes into account the setting of `org-odd-levels-only'."
  6592. (cond
  6593. ((zerop l) 0)
  6594. (org-odd-levels-only (1+ (floor (/ l 2))))
  6595. (t l)))
  6596. (defun org-level-increment ()
  6597. "Return the number of stars that will be added or removed at a
  6598. time to headlines when structure editing, based on the value of
  6599. `org-odd-levels-only'."
  6600. (if org-odd-levels-only 2 1))
  6601. (defun org-get-valid-level (level &optional change)
  6602. "Rectify a level change under the influence of `org-odd-levels-only'
  6603. LEVEL is a current level, CHANGE is by how much the level should be
  6604. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6605. even level numbers will become the next higher odd number."
  6606. (if org-odd-levels-only
  6607. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6608. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6609. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6610. (max 1 (+ level (or change 0)))))
  6611. (if (boundp 'define-obsolete-function-alias)
  6612. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6613. (define-obsolete-function-alias 'org-get-legal-level
  6614. 'org-get-valid-level)
  6615. (define-obsolete-function-alias 'org-get-legal-level
  6616. 'org-get-valid-level "23.1")))
  6617. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6618. ;; ̀org-with-limited-levels'
  6619. (defun org-promote ()
  6620. "Promote the current heading higher up the tree.
  6621. If the region is active in `transient-mark-mode', promote all headings
  6622. in the region."
  6623. (org-back-to-heading t)
  6624. (let* ((level (save-match-data (funcall outline-level)))
  6625. (after-change-functions (remove 'flyspell-after-change-function
  6626. after-change-functions))
  6627. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6628. (diff (abs (- level (length up-head) -1))))
  6629. (cond ((and (= level 1) org-called-with-limited-levels
  6630. org-allow-promoting-top-level-subtree)
  6631. (replace-match "# " nil t))
  6632. ((= level 1)
  6633. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6634. (t (replace-match up-head nil t)))
  6635. ;; Fixup tag positioning
  6636. (unless (= level 1)
  6637. (and org-auto-align-tags (org-set-tags nil t))
  6638. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6639. (run-hooks 'org-after-promote-entry-hook)))
  6640. (defun org-demote ()
  6641. "Demote the current heading lower down the tree.
  6642. If the region is active in `transient-mark-mode', demote all headings
  6643. in the region."
  6644. (org-back-to-heading t)
  6645. (let* ((level (save-match-data (funcall outline-level)))
  6646. (after-change-functions (remove 'flyspell-after-change-function
  6647. after-change-functions))
  6648. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6649. (diff (abs (- level (length down-head) -1))))
  6650. (replace-match down-head nil t)
  6651. ;; Fixup tag positioning
  6652. (and org-auto-align-tags (org-set-tags nil t))
  6653. (if org-adapt-indentation (org-fixup-indentation diff))
  6654. (run-hooks 'org-after-demote-entry-hook)))
  6655. (defun org-cycle-level ()
  6656. "Cycle the level of an empty headline through possible states.
  6657. This goes first to child, then to parent, level, then up the hierarchy.
  6658. After top level, it switches back to sibling level."
  6659. (interactive)
  6660. (let ((org-adapt-indentation nil))
  6661. (when (org-point-at-end-of-empty-headline)
  6662. (setq this-command 'org-cycle-level) ; Only needed for caching
  6663. (let ((cur-level (org-current-level))
  6664. (prev-level (org-get-previous-line-level)))
  6665. (cond
  6666. ;; If first headline in file, promote to top-level.
  6667. ((= prev-level 0)
  6668. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6669. do (org-do-promote)))
  6670. ;; If same level as prev, demote one.
  6671. ((= prev-level cur-level)
  6672. (org-do-demote))
  6673. ;; If parent is top-level, promote to top level if not already.
  6674. ((= prev-level 1)
  6675. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6676. do (org-do-promote)))
  6677. ;; If top-level, return to prev-level.
  6678. ((= cur-level 1)
  6679. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6680. do (org-do-demote)))
  6681. ;; If less than prev-level, promote one.
  6682. ((< cur-level prev-level)
  6683. (org-do-promote))
  6684. ;; If deeper than prev-level, promote until higher than
  6685. ;; prev-level.
  6686. ((> cur-level prev-level)
  6687. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6688. do (org-do-promote))))
  6689. t))))
  6690. (defun org-map-tree (fun)
  6691. "Call FUN for every heading underneath the current one."
  6692. (org-back-to-heading)
  6693. (let ((level (funcall outline-level)))
  6694. (save-excursion
  6695. (funcall fun)
  6696. (while (and (progn
  6697. (outline-next-heading)
  6698. (> (funcall outline-level) level))
  6699. (not (eobp)))
  6700. (funcall fun)))))
  6701. (defun org-map-region (fun beg end)
  6702. "Call FUN for every heading between BEG and END."
  6703. (let ((org-ignore-region t))
  6704. (save-excursion
  6705. (setq end (copy-marker end))
  6706. (goto-char beg)
  6707. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6708. (< (point) end))
  6709. (funcall fun))
  6710. (while (and (progn
  6711. (outline-next-heading)
  6712. (< (point) end))
  6713. (not (eobp)))
  6714. (funcall fun)))))
  6715. (defvar org-property-end-re) ; silence byte-compiler
  6716. (defun org-fixup-indentation (diff)
  6717. "Change the indentation in the current entry by DIFF.
  6718. However, if any line in the current entry has no indentation, or if it
  6719. would end up with no indentation after the change, nothing at all is done."
  6720. (save-excursion
  6721. (let ((end (save-excursion (outline-next-heading)
  6722. (point-marker)))
  6723. (prohibit (if (> diff 0)
  6724. "^\\S-"
  6725. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6726. col)
  6727. (unless (save-excursion (end-of-line 1)
  6728. (re-search-forward prohibit end t))
  6729. (while (and (< (point) end)
  6730. (re-search-forward "^[ \t]+" end t))
  6731. (goto-char (match-end 0))
  6732. (setq col (current-column))
  6733. (if (< diff 0) (replace-match ""))
  6734. (org-indent-to-column (+ diff col))))
  6735. (move-marker end nil))))
  6736. (defun org-convert-to-odd-levels ()
  6737. "Convert an org-mode file with all levels allowed to one with odd levels.
  6738. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6739. level 5 etc."
  6740. (interactive)
  6741. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6742. (let ((outline-level 'org-outline-level)
  6743. (org-odd-levels-only nil) n)
  6744. (save-excursion
  6745. (goto-char (point-min))
  6746. (while (re-search-forward "^\\*\\*+ " nil t)
  6747. (setq n (- (length (match-string 0)) 2))
  6748. (while (>= (setq n (1- n)) 0)
  6749. (org-demote))
  6750. (end-of-line 1))))))
  6751. (defun org-convert-to-oddeven-levels ()
  6752. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6753. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6754. file contains a section with an even level, conversion would
  6755. destroy the structure of the file. An error is signaled in this
  6756. case."
  6757. (interactive)
  6758. (goto-char (point-min))
  6759. ;; First check if there are no even levels
  6760. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6761. (org-show-context t)
  6762. (error "Not all levels are odd in this file. Conversion not possible"))
  6763. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6764. (let ((outline-regexp org-outline-regexp)
  6765. (outline-level 'org-outline-level)
  6766. (org-odd-levels-only nil) n)
  6767. (save-excursion
  6768. (goto-char (point-min))
  6769. (while (re-search-forward "^\\*\\*+ " nil t)
  6770. (setq n (/ (1- (length (match-string 0))) 2))
  6771. (while (>= (setq n (1- n)) 0)
  6772. (org-promote))
  6773. (end-of-line 1))))))
  6774. (defun org-tr-level (n)
  6775. "Make N odd if required."
  6776. (if org-odd-levels-only (1+ (/ n 2)) n))
  6777. ;;; Vertical tree motion, cutting and pasting of subtrees
  6778. (defun org-move-subtree-up (&optional arg)
  6779. "Move the current subtree up past ARG headlines of the same level."
  6780. (interactive "p")
  6781. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6782. (defun org-move-subtree-down (&optional arg)
  6783. "Move the current subtree down past ARG headlines of the same level."
  6784. (interactive "p")
  6785. (setq arg (prefix-numeric-value arg))
  6786. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6787. 'org-get-last-sibling))
  6788. (ins-point (make-marker))
  6789. (cnt (abs arg))
  6790. (col (current-column))
  6791. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6792. ;; Select the tree
  6793. (org-back-to-heading)
  6794. (setq beg0 (point))
  6795. (save-excursion
  6796. (setq ne-beg (org-back-over-empty-lines))
  6797. (setq beg (point)))
  6798. (save-match-data
  6799. (save-excursion (outline-end-of-heading)
  6800. (setq folded (outline-invisible-p)))
  6801. (outline-end-of-subtree))
  6802. (outline-next-heading)
  6803. (setq ne-end (org-back-over-empty-lines))
  6804. (setq end (point))
  6805. (goto-char beg0)
  6806. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6807. ;; include less whitespace
  6808. (save-excursion
  6809. (goto-char beg)
  6810. (forward-line (- ne-beg ne-end))
  6811. (setq beg (point))))
  6812. ;; Find insertion point, with error handling
  6813. (while (> cnt 0)
  6814. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6815. (progn (goto-char beg0)
  6816. (error "Cannot move past superior level or buffer limit")))
  6817. (setq cnt (1- cnt)))
  6818. (if (> arg 0)
  6819. ;; Moving forward - still need to move over subtree
  6820. (progn (org-end-of-subtree t t)
  6821. (save-excursion
  6822. (org-back-over-empty-lines)
  6823. (or (bolp) (newline)))))
  6824. (setq ne-ins (org-back-over-empty-lines))
  6825. (move-marker ins-point (point))
  6826. (setq txt (buffer-substring beg end))
  6827. (org-save-markers-in-region beg end)
  6828. (delete-region beg end)
  6829. (org-remove-empty-overlays-at beg)
  6830. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6831. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6832. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6833. (let ((bbb (point)))
  6834. (insert-before-markers txt)
  6835. (org-reinstall-markers-in-region bbb)
  6836. (move-marker ins-point bbb))
  6837. (or (bolp) (insert "\n"))
  6838. (setq ins-end (point))
  6839. (goto-char ins-point)
  6840. (org-skip-whitespace)
  6841. (when (and (< arg 0)
  6842. (org-first-sibling-p)
  6843. (> ne-ins ne-beg))
  6844. ;; Move whitespace back to beginning
  6845. (save-excursion
  6846. (goto-char ins-end)
  6847. (let ((kill-whole-line t))
  6848. (kill-line (- ne-ins ne-beg)) (point)))
  6849. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6850. (move-marker ins-point nil)
  6851. (if folded
  6852. (hide-subtree)
  6853. (org-show-entry)
  6854. (show-children)
  6855. (org-cycle-hide-drawers 'children))
  6856. (org-clean-visibility-after-subtree-move)
  6857. ;; move back to the initial column we were at
  6858. (move-to-column col)))
  6859. (defvar org-subtree-clip ""
  6860. "Clipboard for cut and paste of subtrees.
  6861. This is actually only a copy of the kill, because we use the normal kill
  6862. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6863. (defvar org-subtree-clip-folded nil
  6864. "Was the last copied subtree folded?
  6865. This is used to fold the tree back after pasting.")
  6866. (defun org-cut-subtree (&optional n)
  6867. "Cut the current subtree into the clipboard.
  6868. With prefix arg N, cut this many sequential subtrees.
  6869. This is a short-hand for marking the subtree and then cutting it."
  6870. (interactive "p")
  6871. (org-copy-subtree n 'cut))
  6872. (defun org-copy-subtree (&optional n cut force-store-markers)
  6873. "Cut the current subtree into the clipboard.
  6874. With prefix arg N, cut this many sequential subtrees.
  6875. This is a short-hand for marking the subtree and then copying it.
  6876. If CUT is non-nil, actually cut the subtree.
  6877. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6878. of some markers in the region, even if CUT is non-nil. This is
  6879. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6880. (interactive "p")
  6881. (let (beg end folded (beg0 (point)))
  6882. (if (org-called-interactively-p 'any)
  6883. (org-back-to-heading nil) ; take what looks like a subtree
  6884. (org-back-to-heading t)) ; take what is really there
  6885. (setq beg (point))
  6886. (skip-chars-forward " \t\r\n")
  6887. (save-match-data
  6888. (save-excursion (outline-end-of-heading)
  6889. (setq folded (outline-invisible-p)))
  6890. (condition-case nil
  6891. (org-forward-heading-same-level (1- n) t)
  6892. (error nil))
  6893. (org-end-of-subtree t t))
  6894. (setq end (point))
  6895. (goto-char beg0)
  6896. (when (> end beg)
  6897. (setq org-subtree-clip-folded folded)
  6898. (when (or cut force-store-markers)
  6899. (org-save-markers-in-region beg end))
  6900. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6901. (setq org-subtree-clip (current-kill 0))
  6902. (message "%s: Subtree(s) with %d characters"
  6903. (if cut "Cut" "Copied")
  6904. (length org-subtree-clip)))))
  6905. (defun org-paste-subtree (&optional level tree for-yank)
  6906. "Paste the clipboard as a subtree, with modification of headline level.
  6907. The entire subtree is promoted or demoted in order to match a new headline
  6908. level.
  6909. If the cursor is at the beginning of a headline, the same level as
  6910. that headline is used to paste the tree
  6911. If not, the new level is derived from the *visible* headings
  6912. before and after the insertion point, and taken to be the inferior headline
  6913. level of the two. So if the previous visible heading is level 3 and the
  6914. next is level 4 (or vice versa), level 4 will be used for insertion.
  6915. This makes sure that the subtree remains an independent subtree and does
  6916. not swallow low level entries.
  6917. You can also force a different level, either by using a numeric prefix
  6918. argument, or by inserting the heading marker by hand. For example, if the
  6919. cursor is after \"*****\", then the tree will be shifted to level 5.
  6920. If optional TREE is given, use this text instead of the kill ring.
  6921. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6922. move back over whitespace before inserting, and move point to the end of
  6923. the inserted text when done."
  6924. (interactive "P")
  6925. (setq tree (or tree (and kill-ring (current-kill 0))))
  6926. (unless (org-kill-is-subtree-p tree)
  6927. (error "%s"
  6928. (substitute-command-keys
  6929. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6930. (org-with-limited-levels
  6931. (let* ((visp (not (outline-invisible-p)))
  6932. (txt tree)
  6933. (^re_ "\\(\\*+\\)[ \t]*")
  6934. (old-level (if (string-match org-outline-regexp-bol txt)
  6935. (- (match-end 0) (match-beginning 0) 1)
  6936. -1))
  6937. (force-level (cond (level (prefix-numeric-value level))
  6938. ((and (looking-at "[ \t]*$")
  6939. (string-match
  6940. "^\\*+$" (buffer-substring
  6941. (point-at-bol) (point))))
  6942. (- (match-end 1) (match-beginning 1)))
  6943. ((and (bolp)
  6944. (looking-at org-outline-regexp))
  6945. (- (match-end 0) (point) 1))))
  6946. (previous-level (save-excursion
  6947. (condition-case nil
  6948. (progn
  6949. (outline-previous-visible-heading 1)
  6950. (if (looking-at ^re_)
  6951. (- (match-end 0) (match-beginning 0) 1)
  6952. 1))
  6953. (error 1))))
  6954. (next-level (save-excursion
  6955. (condition-case nil
  6956. (progn
  6957. (or (looking-at org-outline-regexp)
  6958. (outline-next-visible-heading 1))
  6959. (if (looking-at ^re_)
  6960. (- (match-end 0) (match-beginning 0) 1)
  6961. 1))
  6962. (error 1))))
  6963. (new-level (or force-level (max previous-level next-level)))
  6964. (shift (if (or (= old-level -1)
  6965. (= new-level -1)
  6966. (= old-level new-level))
  6967. 0
  6968. (- new-level old-level)))
  6969. (delta (if (> shift 0) -1 1))
  6970. (func (if (> shift 0) 'org-demote 'org-promote))
  6971. (org-odd-levels-only nil)
  6972. beg end newend)
  6973. ;; Remove the forced level indicator
  6974. (if force-level
  6975. (delete-region (point-at-bol) (point)))
  6976. ;; Paste
  6977. (beginning-of-line (if (bolp) 1 2))
  6978. (setq beg (point))
  6979. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6980. (insert-before-markers txt)
  6981. (unless (string-match "\n\\'" txt) (insert "\n"))
  6982. (setq newend (point))
  6983. (org-reinstall-markers-in-region beg)
  6984. (setq end (point))
  6985. (goto-char beg)
  6986. (skip-chars-forward " \t\n\r")
  6987. (setq beg (point))
  6988. (if (and (outline-invisible-p) visp)
  6989. (save-excursion (outline-show-heading)))
  6990. ;; Shift if necessary
  6991. (unless (= shift 0)
  6992. (save-restriction
  6993. (narrow-to-region beg end)
  6994. (while (not (= shift 0))
  6995. (org-map-region func (point-min) (point-max))
  6996. (setq shift (+ delta shift)))
  6997. (goto-char (point-min))
  6998. (setq newend (point-max))))
  6999. (when (or (org-called-interactively-p 'interactive) for-yank)
  7000. (message "Clipboard pasted as level %d subtree" new-level))
  7001. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7002. kill-ring
  7003. (eq org-subtree-clip (current-kill 0))
  7004. org-subtree-clip-folded)
  7005. ;; The tree was folded before it was killed/copied
  7006. (hide-subtree))
  7007. (and for-yank (goto-char newend)))))
  7008. (defun org-kill-is-subtree-p (&optional txt)
  7009. "Check if the current kill is an outline subtree, or a set of trees.
  7010. Returns nil if kill does not start with a headline, or if the first
  7011. headline level is not the largest headline level in the tree.
  7012. So this will actually accept several entries of equal levels as well,
  7013. which is OK for `org-paste-subtree'.
  7014. If optional TXT is given, check this string instead of the current kill."
  7015. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7016. (re (org-get-limited-outline-regexp))
  7017. (^re (concat "^" re))
  7018. (start-level (and kill
  7019. (string-match
  7020. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7021. kill)
  7022. (- (match-end 2) (match-beginning 2) 1)))
  7023. (start (1+ (or (match-beginning 2) -1))))
  7024. (if (not start-level)
  7025. (progn
  7026. nil) ;; does not even start with a heading
  7027. (catch 'exit
  7028. (while (setq start (string-match ^re kill (1+ start)))
  7029. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7030. (throw 'exit nil)))
  7031. t))))
  7032. (defvar org-markers-to-move nil
  7033. "Markers that should be moved with a cut-and-paste operation.
  7034. Those markers are stored together with their positions relative to
  7035. the start of the region.")
  7036. (defun org-save-markers-in-region (beg end)
  7037. "Check markers in region.
  7038. If these markers are between BEG and END, record their position relative
  7039. to BEG, so that after moving the block of text, we can put the markers back
  7040. into place.
  7041. This function gets called just before an entry or tree gets cut from the
  7042. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7043. called immediately, to move the markers with the entries."
  7044. (setq org-markers-to-move nil)
  7045. (when (featurep 'org-clock)
  7046. (org-clock-save-markers-for-cut-and-paste beg end))
  7047. (when (featurep 'org-agenda)
  7048. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7049. (defun org-check-and-save-marker (marker beg end)
  7050. "Check if MARKER is between BEG and END.
  7051. If yes, remember the marker and the distance to BEG."
  7052. (when (and (marker-buffer marker)
  7053. (equal (marker-buffer marker) (current-buffer)))
  7054. (if (and (>= marker beg) (< marker end))
  7055. (push (cons marker (- marker beg)) org-markers-to-move))))
  7056. (defun org-reinstall-markers-in-region (beg)
  7057. "Move all remembered markers to their position relative to BEG."
  7058. (mapc (lambda (x)
  7059. (move-marker (car x) (+ beg (cdr x))))
  7060. org-markers-to-move)
  7061. (setq org-markers-to-move nil))
  7062. (defun org-narrow-to-subtree ()
  7063. "Narrow buffer to the current subtree."
  7064. (interactive)
  7065. (save-excursion
  7066. (save-match-data
  7067. (org-with-limited-levels
  7068. (narrow-to-region
  7069. (progn (org-back-to-heading t) (point))
  7070. (progn (org-end-of-subtree t t)
  7071. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7072. (point)))))))
  7073. (defun org-narrow-to-block ()
  7074. "Narrow buffer to the current block."
  7075. (interactive)
  7076. (let* ((case-fold-search t)
  7077. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7078. "^[ \t]*#\\+end_.*")))
  7079. (if blockp
  7080. (narrow-to-region (car blockp) (cdr blockp))
  7081. (error "Not in a block"))))
  7082. (eval-when-compile
  7083. (defvar org-property-drawer-re))
  7084. (defvar org-property-start-re) ;; defined below
  7085. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7086. "Clone the task (subtree) at point N times.
  7087. The clones will be inserted as siblings.
  7088. In interactive use, the user will be prompted for the number of
  7089. clones to be produced, and for a time SHIFT, which may be a
  7090. repeater as used in time stamps, for example `+3d'.
  7091. When a valid repeater is given and the entry contains any time
  7092. stamps, the clones will become a sequence in time, with time
  7093. stamps in the subtree shifted for each clone produced. If SHIFT
  7094. is nil or the empty string, time stamps will be left alone. The
  7095. ID property of the original subtree is removed.
  7096. If the original subtree did contain time stamps with a repeater,
  7097. the following will happen:
  7098. - the repeater will be removed in each clone
  7099. - an additional clone will be produced, with the current, unshifted
  7100. date(s) in the entry.
  7101. - the original entry will be placed *after* all the clones, with
  7102. repeater intact.
  7103. - the start days in the repeater in the original entry will be shifted
  7104. to past the last clone.
  7105. In this way you can spell out a number of instances of a repeating task,
  7106. and still retain the repeater to cover future instances of the task."
  7107. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7108. (let (beg end template task idprop
  7109. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7110. (drawer-re org-drawer-regexp))
  7111. (if (not (and (integerp n) (> n 0)))
  7112. (error "Invalid number of replications %s" n))
  7113. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7114. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7115. shift)))
  7116. (error "Invalid shift specification %s" shift))
  7117. (when doshift
  7118. (setq shift-n (string-to-number (match-string 1 shift))
  7119. shift-what (cdr (assoc (match-string 2 shift)
  7120. '(("d" . day) ("w" . week)
  7121. ("m" . month) ("y" . year))))))
  7122. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7123. (setq nmin 1 nmax n)
  7124. (org-back-to-heading t)
  7125. (setq beg (point))
  7126. (setq idprop (org-entry-get nil "ID"))
  7127. (org-end-of-subtree t t)
  7128. (or (bolp) (insert "\n"))
  7129. (setq end (point))
  7130. (setq template (buffer-substring beg end))
  7131. (when (and doshift
  7132. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7133. (delete-region beg end)
  7134. (setq end beg)
  7135. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7136. (goto-char end)
  7137. (loop for n from nmin to nmax do
  7138. ;; prepare clone
  7139. (with-temp-buffer
  7140. (insert template)
  7141. (org-mode)
  7142. (goto-char (point-min))
  7143. (org-show-subtree)
  7144. (and idprop (if org-clone-delete-id
  7145. (org-entry-delete nil "ID")
  7146. (org-id-get-create t)))
  7147. (unless (= n 0)
  7148. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7149. (kill-whole-line))
  7150. (goto-char (point-min))
  7151. (while (re-search-forward drawer-re nil t)
  7152. (mapc (lambda (d)
  7153. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7154. (goto-char (point-min))
  7155. (when doshift
  7156. (while (re-search-forward org-ts-regexp-both nil t)
  7157. (org-timestamp-change (* n shift-n) shift-what))
  7158. (unless (= n n-no-remove)
  7159. (goto-char (point-min))
  7160. (while (re-search-forward org-ts-regexp nil t)
  7161. (save-excursion
  7162. (goto-char (match-beginning 0))
  7163. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7164. (delete-region (match-beginning 1) (match-end 1)))))))
  7165. (setq task (buffer-string)))
  7166. (insert task))
  7167. (goto-char beg)))
  7168. ;;; Outline Sorting
  7169. (defun org-sort (with-case)
  7170. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7171. Optional argument WITH-CASE means sort case-sensitively."
  7172. (interactive "P")
  7173. (cond
  7174. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7175. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7176. (t
  7177. (org-call-with-arg 'org-sort-entries with-case))))
  7178. (defun org-sort-remove-invisible (s)
  7179. (remove-text-properties 0 (length s) org-rm-props s)
  7180. (while (string-match org-bracket-link-regexp s)
  7181. (setq s (replace-match (if (match-end 2)
  7182. (match-string 3 s)
  7183. (match-string 1 s)) t t s)))
  7184. s)
  7185. (defvar org-priority-regexp) ; defined later in the file
  7186. (defvar org-after-sorting-entries-or-items-hook nil
  7187. "Hook that is run after a bunch of entries or items have been sorted.
  7188. When children are sorted, the cursor is in the parent line when this
  7189. hook gets called. When a region or a plain list is sorted, the cursor
  7190. will be in the first entry of the sorted region/list.")
  7191. (defun org-sort-entries
  7192. (&optional with-case sorting-type getkey-func compare-func property)
  7193. "Sort entries on a certain level of an outline tree.
  7194. If there is an active region, the entries in the region are sorted.
  7195. Else, if the cursor is before the first entry, sort the top-level items.
  7196. Else, the children of the entry at point are sorted.
  7197. Sorting can be alphabetically, numerically, by date/time as given by
  7198. a time stamp, by a property or by priority.
  7199. The command prompts for the sorting type unless it has been given to the
  7200. function through the SORTING-TYPE argument, which needs to be a character,
  7201. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7202. precise meaning of each character:
  7203. n Numerically, by converting the beginning of the entry/item to a number.
  7204. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7205. o By order of TODO keywords.
  7206. t By date/time, either the first active time stamp in the entry, or, if
  7207. none exist, by the first inactive one.
  7208. s By the scheduled date/time.
  7209. d By deadline date/time.
  7210. c By creation time, which is assumed to be the first inactive time stamp
  7211. at the beginning of a line.
  7212. p By priority according to the cookie.
  7213. r By the value of a property.
  7214. Capital letters will reverse the sort order.
  7215. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7216. called with point at the beginning of the record. It must return either
  7217. a string or a number that should serve as the sorting key for that record.
  7218. Comparing entries ignores case by default. However, with an optional argument
  7219. WITH-CASE, the sorting considers case as well."
  7220. (interactive "P")
  7221. (let ((case-func (if with-case 'identity 'downcase))
  7222. (cmstr
  7223. ;; The clock marker is lost when using `sort-subr', let's
  7224. ;; store the clocking string.
  7225. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7226. (save-excursion
  7227. (goto-char org-clock-marker)
  7228. (looking-back "^.*") (match-string-no-properties 0))))
  7229. start beg end stars re re2
  7230. txt what tmp)
  7231. ;; Find beginning and end of region to sort
  7232. (cond
  7233. ((org-region-active-p)
  7234. ;; we will sort the region
  7235. (setq end (region-end)
  7236. what "region")
  7237. (goto-char (region-beginning))
  7238. (if (not (org-at-heading-p)) (outline-next-heading))
  7239. (setq start (point)))
  7240. ((or (org-at-heading-p)
  7241. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7242. ;; we will sort the children of the current headline
  7243. (org-back-to-heading)
  7244. (setq start (point)
  7245. end (progn (org-end-of-subtree t t)
  7246. (or (bolp) (insert "\n"))
  7247. (org-back-over-empty-lines)
  7248. (point))
  7249. what "children")
  7250. (goto-char start)
  7251. (show-subtree)
  7252. (outline-next-heading))
  7253. (t
  7254. ;; we will sort the top-level entries in this file
  7255. (goto-char (point-min))
  7256. (or (org-at-heading-p) (outline-next-heading))
  7257. (setq start (point))
  7258. (goto-char (point-max))
  7259. (beginning-of-line 1)
  7260. (when (looking-at ".*?\\S-")
  7261. ;; File ends in a non-white line
  7262. (end-of-line 1)
  7263. (insert "\n"))
  7264. (setq end (point-max))
  7265. (setq what "top-level")
  7266. (goto-char start)
  7267. (show-all)))
  7268. (setq beg (point))
  7269. (if (>= beg end) (error "Nothing to sort"))
  7270. (looking-at "\\(\\*+\\)")
  7271. (setq stars (match-string 1)
  7272. re (concat "^" (regexp-quote stars) " +")
  7273. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7274. txt (buffer-substring beg end))
  7275. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7276. (if (and (not (equal stars "*")) (string-match re2 txt))
  7277. (error "Region to sort contains a level above the first entry"))
  7278. (unless sorting-type
  7279. (message
  7280. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7281. [t]ime [s]cheduled [d]eadline [c]reated
  7282. A/N/P/R/O/F/T/S/D/C means reversed:"
  7283. what)
  7284. (setq sorting-type (read-char-exclusive))
  7285. (and (= (downcase sorting-type) ?f)
  7286. (setq getkey-func
  7287. (org-icompleting-read "Sort using function: "
  7288. obarray 'fboundp t nil nil))
  7289. (setq getkey-func (intern getkey-func)))
  7290. (and (= (downcase sorting-type) ?r)
  7291. (setq property
  7292. (org-icompleting-read "Property: "
  7293. (mapcar 'list (org-buffer-property-keys t))
  7294. nil t))))
  7295. (message "Sorting entries...")
  7296. (save-restriction
  7297. (narrow-to-region start end)
  7298. (let ((dcst (downcase sorting-type))
  7299. (case-fold-search nil)
  7300. (now (current-time)))
  7301. (sort-subr
  7302. (/= dcst sorting-type)
  7303. ;; This function moves to the beginning character of the "record" to
  7304. ;; be sorted.
  7305. (lambda nil
  7306. (if (re-search-forward re nil t)
  7307. (goto-char (match-beginning 0))
  7308. (goto-char (point-max))))
  7309. ;; This function moves to the last character of the "record" being
  7310. ;; sorted.
  7311. (lambda nil
  7312. (save-match-data
  7313. (condition-case nil
  7314. (outline-forward-same-level 1)
  7315. (error
  7316. (goto-char (point-max))))))
  7317. ;; This function returns the value that gets sorted against.
  7318. (lambda nil
  7319. (cond
  7320. ((= dcst ?n)
  7321. (if (looking-at org-complex-heading-regexp)
  7322. (string-to-number (match-string 4))
  7323. nil))
  7324. ((= dcst ?a)
  7325. (if (looking-at org-complex-heading-regexp)
  7326. (funcall case-func (match-string 4))
  7327. nil))
  7328. ((= dcst ?t)
  7329. (let ((end (save-excursion (outline-next-heading) (point))))
  7330. (if (or (re-search-forward org-ts-regexp end t)
  7331. (re-search-forward org-ts-regexp-both end t))
  7332. (org-time-string-to-seconds (match-string 0))
  7333. (org-float-time now))))
  7334. ((= dcst ?c)
  7335. (let ((end (save-excursion (outline-next-heading) (point))))
  7336. (if (re-search-forward
  7337. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7338. end t)
  7339. (org-time-string-to-seconds (match-string 0))
  7340. (org-float-time now))))
  7341. ((= dcst ?s)
  7342. (let ((end (save-excursion (outline-next-heading) (point))))
  7343. (if (re-search-forward org-scheduled-time-regexp end t)
  7344. (org-time-string-to-seconds (match-string 1))
  7345. (org-float-time now))))
  7346. ((= dcst ?d)
  7347. (let ((end (save-excursion (outline-next-heading) (point))))
  7348. (if (re-search-forward org-deadline-time-regexp end t)
  7349. (org-time-string-to-seconds (match-string 1))
  7350. (org-float-time now))))
  7351. ((= dcst ?p)
  7352. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7353. (string-to-char (match-string 2))
  7354. org-default-priority))
  7355. ((= dcst ?r)
  7356. (or (org-entry-get nil property) ""))
  7357. ((= dcst ?o)
  7358. (if (looking-at org-complex-heading-regexp)
  7359. (- 9999 (length (member (match-string 2)
  7360. org-todo-keywords-1)))))
  7361. ((= dcst ?f)
  7362. (if getkey-func
  7363. (progn
  7364. (setq tmp (funcall getkey-func))
  7365. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7366. tmp)
  7367. (error "Invalid key function `%s'" getkey-func)))
  7368. (t (error "Invalid sorting type `%c'" sorting-type))))
  7369. nil
  7370. (cond
  7371. ((= dcst ?a) 'string<)
  7372. ((= dcst ?f) compare-func)
  7373. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7374. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7375. ;; Reset the clock marker if needed
  7376. (when cmstr
  7377. (save-excursion
  7378. (goto-char start)
  7379. (search-forward cmstr nil t)
  7380. (move-marker org-clock-marker (point))))
  7381. (message "Sorting entries...done")))
  7382. (defun org-do-sort (table what &optional with-case sorting-type)
  7383. "Sort TABLE of WHAT according to SORTING-TYPE.
  7384. The user will be prompted for the SORTING-TYPE if the call to this
  7385. function does not specify it. WHAT is only for the prompt, to indicate
  7386. what is being sorted. The sorting key will be extracted from
  7387. the car of the elements of the table.
  7388. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7389. (unless sorting-type
  7390. (message
  7391. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7392. what)
  7393. (setq sorting-type (read-char-exclusive)))
  7394. (let ((dcst (downcase sorting-type))
  7395. extractfun comparefun)
  7396. ;; Define the appropriate functions
  7397. (cond
  7398. ((= dcst ?n)
  7399. (setq extractfun 'string-to-number
  7400. comparefun (if (= dcst sorting-type) '< '>)))
  7401. ((= dcst ?a)
  7402. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7403. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7404. comparefun (if (= dcst sorting-type)
  7405. 'string<
  7406. (lambda (a b) (and (not (string< a b))
  7407. (not (string= a b)))))))
  7408. ((= dcst ?t)
  7409. (setq extractfun
  7410. (lambda (x)
  7411. (if (or (string-match org-ts-regexp x)
  7412. (string-match org-ts-regexp-both x))
  7413. (org-float-time
  7414. (org-time-string-to-time (match-string 0 x)))
  7415. 0))
  7416. comparefun (if (= dcst sorting-type) '< '>)))
  7417. (t (error "Invalid sorting type `%c'" sorting-type)))
  7418. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7419. table)
  7420. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7421. ;;; The orgstruct minor mode
  7422. ;; Define a minor mode which can be used in other modes in order to
  7423. ;; integrate the org-mode structure editing commands.
  7424. ;; This is really a hack, because the org-mode structure commands use
  7425. ;; keys which normally belong to the major mode. Here is how it
  7426. ;; works: The minor mode defines all the keys necessary to operate the
  7427. ;; structure commands, but wraps the commands into a function which
  7428. ;; tests if the cursor is currently at a headline or a plain list
  7429. ;; item. If that is the case, the structure command is used,
  7430. ;; temporarily setting many Org-mode variables like regular
  7431. ;; expressions for filling etc. However, when any of those keys is
  7432. ;; used at a different location, function uses `key-binding' to look
  7433. ;; up if the key has an associated command in another currently active
  7434. ;; keymap (minor modes, major mode, global), and executes that
  7435. ;; command. There might be problems if any of the keys is otherwise
  7436. ;; used as a prefix key.
  7437. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7438. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7439. ;; addresses this by checking explicitly for both bindings.
  7440. (defvar orgstruct-mode-map (make-sparse-keymap)
  7441. "Keymap for the minor `orgstruct-mode'.")
  7442. (defvar org-local-vars nil
  7443. "List of local variables, for use by `orgstruct-mode'.")
  7444. ;;;###autoload
  7445. (define-minor-mode orgstruct-mode
  7446. "Toggle the minor mode `orgstruct-mode'.
  7447. This mode is for using Org-mode structure commands in other
  7448. modes. The following keys behave as if Org-mode were active, if
  7449. the cursor is on a headline, or on a plain list item (both as
  7450. defined by Org-mode).
  7451. M-up Move entry/item up
  7452. M-down Move entry/item down
  7453. M-left Promote
  7454. M-right Demote
  7455. M-S-up Move entry/item up
  7456. M-S-down Move entry/item down
  7457. M-S-left Promote subtree
  7458. M-S-right Demote subtree
  7459. M-q Fill paragraph and items like in Org-mode
  7460. C-c ^ Sort entries
  7461. C-c - Cycle list bullet
  7462. TAB Cycle item visibility
  7463. M-RET Insert new heading/item
  7464. S-M-RET Insert new TODO heading / Checkbox item
  7465. C-c C-c Set tags / toggle checkbox"
  7466. nil " OrgStruct" nil
  7467. (org-load-modules-maybe)
  7468. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7469. ;;;###autoload
  7470. (defun turn-on-orgstruct ()
  7471. "Unconditionally turn on `orgstruct-mode'."
  7472. (orgstruct-mode 1))
  7473. (defvar org-fb-vars nil)
  7474. (make-variable-buffer-local 'org-fb-vars)
  7475. (defun orgstruct++-mode (&optional arg)
  7476. "Toggle `orgstruct-mode', the enhanced version of it.
  7477. In addition to setting orgstruct-mode, this also exports all
  7478. indentation and autofilling variables from org-mode into the
  7479. buffer. It will also recognize item context in multiline items."
  7480. (interactive "P")
  7481. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7482. (if (< arg 1)
  7483. (progn (orgstruct-mode -1)
  7484. (mapc (lambda(v)
  7485. (org-set-local (car v)
  7486. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7487. org-fb-vars))
  7488. (orgstruct-mode 1)
  7489. (setq org-fb-vars nil)
  7490. (let (var val)
  7491. (mapc
  7492. (lambda (x)
  7493. (when (string-match
  7494. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7495. (symbol-name (car x)))
  7496. (setq var (car x) val (nth 1 x))
  7497. (push (list var `(quote ,(eval var))) org-fb-vars)
  7498. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7499. org-local-vars)
  7500. (org-set-local 'orgstruct-is-++ t))))
  7501. (defvar orgstruct-is-++ nil
  7502. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7503. (make-variable-buffer-local 'orgstruct-is-++)
  7504. ;;;###autoload
  7505. (defun turn-on-orgstruct++ ()
  7506. "Unconditionally turn on `orgstruct++-mode'."
  7507. (orgstruct++-mode 1))
  7508. (defun orgstruct-error ()
  7509. "Error when there is no default binding for a structure key."
  7510. (interactive)
  7511. (error "This key has no function outside structure elements"))
  7512. (defun orgstruct-setup ()
  7513. "Setup orgstruct keymaps."
  7514. (let ((nfunc 0)
  7515. (bindings
  7516. (list
  7517. '([(meta up)] org-metaup)
  7518. '([(meta down)] org-metadown)
  7519. '([(meta left)] org-metaleft)
  7520. '([(meta right)] org-metaright)
  7521. '([(meta shift up)] org-shiftmetaup)
  7522. '([(meta shift down)] org-shiftmetadown)
  7523. '([(meta shift left)] org-shiftmetaleft)
  7524. '([(meta shift right)] org-shiftmetaright)
  7525. '([?\e (up)] org-metaup)
  7526. '([?\e (down)] org-metadown)
  7527. '([?\e (left)] org-metaleft)
  7528. '([?\e (right)] org-metaright)
  7529. '([?\e (shift up)] org-shiftmetaup)
  7530. '([?\e (shift down)] org-shiftmetadown)
  7531. '([?\e (shift left)] org-shiftmetaleft)
  7532. '([?\e (shift right)] org-shiftmetaright)
  7533. '([(shift up)] org-shiftup)
  7534. '([(shift down)] org-shiftdown)
  7535. '([(shift left)] org-shiftleft)
  7536. '([(shift right)] org-shiftright)
  7537. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7538. '("\M-q" fill-paragraph)
  7539. '("\C-c^" org-sort)
  7540. '("\C-c-" org-cycle-list-bullet)))
  7541. elt key fun cmd)
  7542. (while (setq elt (pop bindings))
  7543. (setq nfunc (1+ nfunc))
  7544. (setq key (org-key (car elt))
  7545. fun (nth 1 elt)
  7546. cmd (orgstruct-make-binding fun nfunc key))
  7547. (org-defkey orgstruct-mode-map key cmd))
  7548. ;; Prevent an error for users who forgot to make autoloads
  7549. (require 'org-element)
  7550. ;; Special treatment needed for TAB and RET
  7551. (org-defkey orgstruct-mode-map [(tab)]
  7552. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7553. (org-defkey orgstruct-mode-map "\C-i"
  7554. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7555. (org-defkey orgstruct-mode-map "\M-\C-m"
  7556. (orgstruct-make-binding 'org-insert-heading 105
  7557. "\M-\C-m" [(meta return)]))
  7558. (org-defkey orgstruct-mode-map [(meta return)]
  7559. (orgstruct-make-binding 'org-insert-heading 106
  7560. [(meta return)] "\M-\C-m"))
  7561. (org-defkey orgstruct-mode-map [(shift meta return)]
  7562. (orgstruct-make-binding 'org-insert-todo-heading 107
  7563. [(meta return)] "\M-\C-m"))
  7564. (org-defkey orgstruct-mode-map "\e\C-m"
  7565. (orgstruct-make-binding 'org-insert-heading 108
  7566. "\e\C-m" [?\e (return)]))
  7567. (org-defkey orgstruct-mode-map [?\e (return)]
  7568. (orgstruct-make-binding 'org-insert-heading 109
  7569. [?\e (return)] "\e\C-m"))
  7570. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7571. (orgstruct-make-binding 'org-insert-todo-heading 110
  7572. [?\e (return)] "\e\C-m"))
  7573. (unless org-local-vars
  7574. (setq org-local-vars (org-get-local-variables)))
  7575. t))
  7576. (defun orgstruct-make-binding (fun n &rest keys)
  7577. "Create a function for binding in the structure minor mode.
  7578. FUN is the command to call inside a table. N is used to create a unique
  7579. command name. KEYS are keys that should be checked in for a command
  7580. to execute outside of tables."
  7581. (eval
  7582. (list 'defun
  7583. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7584. '(arg)
  7585. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7586. "Outside of structure, run the binding of `"
  7587. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7588. "'.")
  7589. '(interactive "p")
  7590. (list 'if
  7591. `(org-context-p 'headline 'item
  7592. (and orgstruct-is-++
  7593. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7594. 'item-body))
  7595. (list 'org-run-like-in-org-mode (list 'quote fun))
  7596. (list 'let '(orgstruct-mode)
  7597. (list 'call-interactively
  7598. (append '(or)
  7599. (mapcar (lambda (k)
  7600. (list 'key-binding k))
  7601. keys)
  7602. '('orgstruct-error))))))))
  7603. (defun org-contextualize-keys (alist contexts)
  7604. "Return valid elements in ALIST depending on CONTEXTS.
  7605. `org-agenda-custom-commands' or `org-capture-templates' are the
  7606. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7607. or `org-capture-templates-contexts' are the associated contexts
  7608. definitions."
  7609. (let ((contexts
  7610. ;; normalize contexts
  7611. (mapcar
  7612. (lambda(c) (cond ((listp (cadr c))
  7613. (list (car c) (car c) (cadr c)))
  7614. ((string= "" (cadr c))
  7615. (list (car c) (car c) (caddr c)))
  7616. (t c))) contexts))
  7617. (a alist) c r s)
  7618. ;; loop over all commands or templates
  7619. (while (setq c (pop a))
  7620. (let (vrules repl)
  7621. (cond
  7622. ((not (assoc (car c) contexts))
  7623. (push c r))
  7624. ((and (assoc (car c) contexts)
  7625. (setq vrules (org-contextualize-validate-key
  7626. (car c) contexts)))
  7627. (mapc (lambda (vr)
  7628. (when (not (equal (car vr) (cadr vr)))
  7629. (setq repl vr))) vrules)
  7630. (if (not repl) (push c r)
  7631. (push (cadr repl) s)
  7632. (push
  7633. (cons (car c)
  7634. (cdr (or (assoc (cadr repl) alist)
  7635. (error "Undefined key `%s' as contextual replacement for `%s'"
  7636. (cadr repl) (car c)))))
  7637. r))))))
  7638. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7639. (delq
  7640. nil
  7641. (delete-dups
  7642. (mapcar (lambda (x)
  7643. (let ((tpl (car x)))
  7644. (when (not (delq
  7645. nil
  7646. (mapcar (lambda(y)
  7647. (equal y tpl)) s))) x)))
  7648. (reverse r))))))
  7649. (defun org-contextualize-validate-key (key contexts)
  7650. "Check CONTEXTS for agenda or capture KEY."
  7651. (let (r rr res)
  7652. (while (setq r (pop contexts))
  7653. (mapc
  7654. (lambda (rr)
  7655. (when
  7656. (and (equal key (car r))
  7657. (if (functionp rr) (funcall rr)
  7658. (or (and (eq (car rr) 'in-file)
  7659. (buffer-file-name)
  7660. (string-match (cdr rr) (buffer-file-name)))
  7661. (and (eq (car rr) 'in-mode)
  7662. (string-match (cdr rr) (symbol-name major-mode)))
  7663. (when (and (eq (car rr) 'not-in-file)
  7664. (buffer-file-name))
  7665. (not (string-match (cdr rr) (buffer-file-name))))
  7666. (when (eq (car rr) 'not-in-mode)
  7667. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7668. (push r res)))
  7669. (car (last r))))
  7670. (delete-dups (delq nil res))))
  7671. (defun org-context-p (&rest contexts)
  7672. "Check if local context is any of CONTEXTS.
  7673. Possible values in the list of contexts are `table', `headline', and `item'."
  7674. (let ((pos (point)))
  7675. (goto-char (point-at-bol))
  7676. (prog1 (or (and (memq 'table contexts)
  7677. (looking-at "[ \t]*|"))
  7678. (and (memq 'headline contexts)
  7679. (looking-at org-outline-regexp))
  7680. (and (memq 'item contexts)
  7681. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7682. (and (memq 'item-body contexts)
  7683. (org-in-item-p)))
  7684. (goto-char pos))))
  7685. (defun org-get-local-variables ()
  7686. "Return a list of all local variables in an Org mode buffer."
  7687. (let (varlist)
  7688. (with-current-buffer (get-buffer-create "*Org tmp*")
  7689. (erase-buffer)
  7690. (org-mode)
  7691. (setq varlist (buffer-local-variables)))
  7692. (kill-buffer "*Org tmp*")
  7693. (delq nil
  7694. (mapcar
  7695. (lambda (x)
  7696. (setq x
  7697. (if (symbolp x)
  7698. (list x)
  7699. (list (car x) (list 'quote (cdr x)))))
  7700. (if (string-match
  7701. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7702. (symbol-name (car x)))
  7703. x nil))
  7704. varlist))))
  7705. (defun org-clone-local-variables (from-buffer &optional regexp)
  7706. "Clone local variables from FROM-BUFFER.
  7707. Optional argument REGEXP selects variables to clone."
  7708. (mapc
  7709. (lambda (pair)
  7710. (and (symbolp (car pair))
  7711. (or (null regexp)
  7712. (string-match regexp (symbol-name (car pair))))
  7713. (set (make-local-variable (car pair))
  7714. (cdr pair))))
  7715. (buffer-local-variables from-buffer)))
  7716. ;;;###autoload
  7717. (defun org-run-like-in-org-mode (cmd)
  7718. "Run a command, pretending that the current buffer is in Org-mode.
  7719. This will temporarily bind local variables that are typically bound in
  7720. Org-mode to the values they have in Org-mode, and then interactively
  7721. call CMD."
  7722. (org-load-modules-maybe)
  7723. (unless org-local-vars
  7724. (setq org-local-vars (org-get-local-variables)))
  7725. (eval (list 'let org-local-vars
  7726. (list 'call-interactively (list 'quote cmd)))))
  7727. ;;;; Archiving
  7728. (defun org-get-category (&optional pos force-refresh)
  7729. "Get the category applying to position POS."
  7730. (save-match-data
  7731. (if force-refresh (org-refresh-category-properties))
  7732. (let ((pos (or pos (point))))
  7733. (or (get-text-property pos 'org-category)
  7734. (progn (org-refresh-category-properties)
  7735. (get-text-property pos 'org-category))))))
  7736. (defun org-refresh-category-properties ()
  7737. "Refresh category text properties in the buffer."
  7738. (let ((case-fold-search t)
  7739. (inhibit-read-only t)
  7740. (def-cat (cond
  7741. ((null org-category)
  7742. (if buffer-file-name
  7743. (file-name-sans-extension
  7744. (file-name-nondirectory buffer-file-name))
  7745. "???"))
  7746. ((symbolp org-category) (symbol-name org-category))
  7747. (t org-category)))
  7748. beg end cat pos optionp)
  7749. (org-unmodified
  7750. (save-excursion
  7751. (save-restriction
  7752. (widen)
  7753. (goto-char (point-min))
  7754. (put-text-property (point) (point-max) 'org-category def-cat)
  7755. (while (re-search-forward
  7756. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7757. (setq pos (match-end 0)
  7758. optionp (equal (char-after (match-beginning 0)) ?#)
  7759. cat (org-trim (match-string 2)))
  7760. (if optionp
  7761. (setq beg (point-at-bol) end (point-max))
  7762. (org-back-to-heading t)
  7763. (setq beg (point) end (org-end-of-subtree t t)))
  7764. (put-text-property beg end 'org-category cat)
  7765. (put-text-property beg end 'org-category-position beg)
  7766. (goto-char pos)))))))
  7767. ;;;; Link Stuff
  7768. ;;; Link abbreviations
  7769. (defun org-link-expand-abbrev (link)
  7770. "Apply replacements as defined in `org-link-abbrev-alist'."
  7771. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7772. (let* ((key (match-string 1 link))
  7773. (as (or (assoc key org-link-abbrev-alist-local)
  7774. (assoc key org-link-abbrev-alist)))
  7775. (tag (and (match-end 2) (match-string 3 link)))
  7776. rpl)
  7777. (if (not as)
  7778. link
  7779. (setq rpl (cdr as))
  7780. (cond
  7781. ((symbolp rpl) (funcall rpl tag))
  7782. ((string-match "%(\\([^)]+\\))" rpl)
  7783. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7784. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7785. ((string-match "%h" rpl)
  7786. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7787. (t (concat rpl tag)))))
  7788. link))
  7789. ;;; Storing and inserting links
  7790. (defvar org-insert-link-history nil
  7791. "Minibuffer history for links inserted with `org-insert-link'.")
  7792. (defvar org-stored-links nil
  7793. "Contains the links stored with `org-store-link'.")
  7794. (defvar org-store-link-plist nil
  7795. "Plist with info about the most recently link created with `org-store-link'.")
  7796. (defvar org-link-protocols nil
  7797. "Link protocols added to Org-mode using `org-add-link-type'.")
  7798. (defvar org-store-link-functions nil
  7799. "List of functions that are called to create and store a link.
  7800. Each function will be called in turn until one returns a non-nil
  7801. value. Each function should check if it is responsible for creating
  7802. this link (for example by looking at the major mode).
  7803. If not, it must exit and return nil.
  7804. If yes, it should return a non-nil value after a calling
  7805. `org-store-link-props' with a list of properties and values.
  7806. Special properties are:
  7807. :type The link prefix, like \"http\". This must be given.
  7808. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7809. This is obligatory as well.
  7810. :description Optional default description for the second pair
  7811. of brackets in an Org-mode link. The user can still change
  7812. this when inserting this link into an Org-mode buffer.
  7813. In addition to these, any additional properties can be specified
  7814. and then used in capture templates.")
  7815. (defun org-add-link-type (type &optional follow export)
  7816. "Add TYPE to the list of `org-link-types'.
  7817. Re-compute all regular expressions depending on `org-link-types'
  7818. FOLLOW and EXPORT are two functions.
  7819. FOLLOW should take the link path as the single argument and do whatever
  7820. is necessary to follow the link, for example find a file or display
  7821. a mail message.
  7822. EXPORT should format the link path for export to one of the export formats.
  7823. It should be a function accepting three arguments:
  7824. path the path of the link, the text after the prefix (like \"http:\")
  7825. desc the description of the link, if any, or a description added by
  7826. org-export-normalize-links if there is none
  7827. format the export format, a symbol like `html' or `latex' or `ascii'..
  7828. The function may use the FORMAT information to return different values
  7829. depending on the format. The return value will be put literally into
  7830. the exported file. If the return value is nil, this means Org should
  7831. do what it normally does with links which do not have EXPORT defined.
  7832. Org-mode has a built-in default for exporting links. If you are happy with
  7833. this default, there is no need to define an export function for the link
  7834. type. For a simple example of an export function, see `org-bbdb.el'."
  7835. (add-to-list 'org-link-types type t)
  7836. (org-make-link-regexps)
  7837. (if (assoc type org-link-protocols)
  7838. (setcdr (assoc type org-link-protocols) (list follow export))
  7839. (push (list type follow export) org-link-protocols)))
  7840. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  7841. (defvar org-link-to-org-use-id) ; Defined in org-id.el
  7842. ;;;###autoload
  7843. (defun org-store-link (arg)
  7844. "\\<org-mode-map>Store an org-link to the current location.
  7845. This link is added to `org-stored-links' and can later be inserted
  7846. into an org-buffer with \\[org-insert-link].
  7847. For some link types, a prefix arg is interpreted:
  7848. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7849. For file links, arg negates `org-context-in-file-links'."
  7850. (interactive "P")
  7851. (org-load-modules-maybe)
  7852. (setq org-store-link-plist nil) ; reset
  7853. (org-with-limited-levels
  7854. (let (link cpltxt desc description search txt custom-id agenda-link)
  7855. (cond
  7856. ((run-hook-with-args-until-success 'org-store-link-functions)
  7857. (setq link (plist-get org-store-link-plist :link)
  7858. desc (or (plist-get org-store-link-plist :description) link)))
  7859. ((org-src-edit-buffer-p)
  7860. (let (label gc)
  7861. (while (or (not label)
  7862. (save-excursion
  7863. (save-restriction
  7864. (widen)
  7865. (goto-char (point-min))
  7866. (re-search-forward
  7867. (regexp-quote (format org-coderef-label-format label))
  7868. nil t))))
  7869. (when label (message "Label exists already") (sit-for 2))
  7870. (setq label (read-string "Code line label: " label)))
  7871. (end-of-line 1)
  7872. (setq link (format org-coderef-label-format label))
  7873. (setq gc (- 79 (length link)))
  7874. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7875. (insert link)
  7876. (setq link (concat "(" label ")") desc nil)))
  7877. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7878. ;; We are in the agenda, link to referenced location
  7879. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7880. (get-text-property (point) 'org-marker))))
  7881. (when m
  7882. (org-with-point-at m
  7883. (setq agenda-link
  7884. (if (org-called-interactively-p 'any)
  7885. (call-interactively 'org-store-link)
  7886. (org-store-link nil)))))))
  7887. ((eq major-mode 'calendar-mode)
  7888. (let ((cd (calendar-cursor-to-date)))
  7889. (setq link
  7890. (format-time-string
  7891. (car org-time-stamp-formats)
  7892. (apply 'encode-time
  7893. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7894. nil nil nil))))
  7895. (org-store-link-props :type "calendar" :date cd)))
  7896. ((eq major-mode 'help-mode)
  7897. (setq link (concat "help:" (save-excursion
  7898. (goto-char (point-min))
  7899. (looking-at "^[^ ]+")
  7900. (match-string 0))))
  7901. (org-store-link-props :type "help"))
  7902. ((eq major-mode 'w3-mode)
  7903. (setq cpltxt (if (and (buffer-name)
  7904. (not (string-match "Untitled" (buffer-name))))
  7905. (buffer-name)
  7906. (url-view-url t))
  7907. link (url-view-url t))
  7908. (org-store-link-props :type "w3" :url (url-view-url t)))
  7909. ((eq major-mode 'w3m-mode)
  7910. (setq cpltxt (or w3m-current-title w3m-current-url)
  7911. link w3m-current-url)
  7912. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7913. ((setq search (run-hook-with-args-until-success
  7914. 'org-create-file-search-functions))
  7915. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7916. "::" search))
  7917. (setq cpltxt (or description link)))
  7918. ((eq major-mode 'image-mode)
  7919. (setq cpltxt (concat "file:"
  7920. (abbreviate-file-name buffer-file-name))
  7921. link cpltxt)
  7922. (org-store-link-props :type "image" :file buffer-file-name))
  7923. ((eq major-mode 'dired-mode)
  7924. ;; link to the file in the current line
  7925. (let ((file (dired-get-filename nil t)))
  7926. (setq file (if file
  7927. (abbreviate-file-name
  7928. (expand-file-name (dired-get-filename nil t)))
  7929. ;; otherwise, no file so use current directory.
  7930. default-directory))
  7931. (setq cpltxt (concat "file:" file)
  7932. link cpltxt)))
  7933. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7934. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7935. (cond
  7936. ((org-in-regexp "<<\\(.*?\\)>>")
  7937. (setq cpltxt
  7938. (concat "file:"
  7939. (abbreviate-file-name
  7940. (buffer-file-name (buffer-base-buffer)))
  7941. "::" (match-string 1))
  7942. link cpltxt))
  7943. ((and (featurep 'org-id)
  7944. (or (eq org-link-to-org-use-id t)
  7945. (and (org-called-interactively-p 'any)
  7946. (or (eq org-link-to-org-use-id 'create-if-interactive)
  7947. (and (eq org-link-to-org-use-id
  7948. 'create-if-interactive-and-no-custom-id)
  7949. (not custom-id))))
  7950. (and org-link-to-org-use-id (org-entry-get nil "ID"))))
  7951. ;; We can make a link using the ID.
  7952. (setq link (condition-case nil
  7953. (prog1 (org-id-store-link)
  7954. (setq desc (plist-get org-store-link-plist :description)))
  7955. (error
  7956. ;; probably before first headline, link to file only
  7957. (concat "file:"
  7958. (abbreviate-file-name
  7959. (buffer-file-name (buffer-base-buffer))))))))
  7960. (t
  7961. ;; Just link to current headline
  7962. (setq cpltxt (concat "file:"
  7963. (abbreviate-file-name
  7964. (buffer-file-name (buffer-base-buffer)))))
  7965. ;; Add a context search string
  7966. (when (org-xor org-context-in-file-links arg)
  7967. (setq txt (cond
  7968. ((org-at-heading-p) nil)
  7969. ((org-region-active-p)
  7970. (buffer-substring (region-beginning) (region-end)))))
  7971. (when (or (null txt) (string-match "\\S-" txt))
  7972. (setq cpltxt
  7973. (concat cpltxt "::"
  7974. (condition-case nil
  7975. (org-make-org-heading-search-string txt)
  7976. (error "")))
  7977. desc (or (nth 4 (ignore-errors
  7978. (org-heading-components))) "NONE"))))
  7979. (if (string-match "::\\'" cpltxt)
  7980. (setq cpltxt (substring cpltxt 0 -2)))
  7981. (setq link cpltxt))))
  7982. ((buffer-file-name (buffer-base-buffer))
  7983. ;; Just link to this file here.
  7984. (setq cpltxt (concat "file:"
  7985. (abbreviate-file-name
  7986. (buffer-file-name (buffer-base-buffer)))))
  7987. ;; Add a context string
  7988. (when (org-xor org-context-in-file-links arg)
  7989. (setq txt (if (org-region-active-p)
  7990. (buffer-substring (region-beginning) (region-end))
  7991. (buffer-substring (point-at-bol) (point-at-eol))))
  7992. ;; Only use search option if there is some text.
  7993. (when (string-match "\\S-" txt)
  7994. (setq cpltxt
  7995. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7996. desc "NONE")))
  7997. (setq link cpltxt))
  7998. ((org-called-interactively-p 'interactive)
  7999. (error "Cannot link to a buffer which is not visiting a file"))
  8000. (t (setq link nil)))
  8001. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8002. (setq link (or link cpltxt)
  8003. desc (or desc cpltxt))
  8004. (if (equal desc "NONE") (setq desc nil))
  8005. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8006. (progn
  8007. (setq org-stored-links
  8008. (cons (list link desc) org-stored-links))
  8009. (message "Stored: %s" (or desc link))
  8010. (when custom-id
  8011. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8012. "::#" custom-id))
  8013. (setq org-stored-links
  8014. (cons (list link desc) org-stored-links))))
  8015. (or agenda-link (and link (org-make-link-string link desc)))))))
  8016. (defun org-store-link-props (&rest plist)
  8017. "Store link properties, extract names and addresses."
  8018. (let (x adr)
  8019. (when (setq x (plist-get plist :from))
  8020. (setq adr (mail-extract-address-components x))
  8021. (setq plist (plist-put plist :fromname (car adr)))
  8022. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8023. (when (setq x (plist-get plist :to))
  8024. (setq adr (mail-extract-address-components x))
  8025. (setq plist (plist-put plist :toname (car adr)))
  8026. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8027. (let ((from (plist-get plist :from))
  8028. (to (plist-get plist :to)))
  8029. (when (and from to org-from-is-user-regexp)
  8030. (setq plist
  8031. (plist-put plist :fromto
  8032. (if (string-match org-from-is-user-regexp from)
  8033. (concat "to %t")
  8034. (concat "from %f"))))))
  8035. (setq org-store-link-plist plist))
  8036. (defun org-add-link-props (&rest plist)
  8037. "Add these properties to the link property list."
  8038. (let (key value)
  8039. (while plist
  8040. (setq key (pop plist) value (pop plist))
  8041. (setq org-store-link-plist
  8042. (plist-put org-store-link-plist key value)))))
  8043. (defun org-email-link-description (&optional fmt)
  8044. "Return the description part of an email link.
  8045. This takes information from `org-store-link-plist' and formats it
  8046. according to FMT (default from `org-email-link-description-format')."
  8047. (setq fmt (or fmt org-email-link-description-format))
  8048. (let* ((p org-store-link-plist)
  8049. (to (plist-get p :toaddress))
  8050. (from (plist-get p :fromaddress))
  8051. (table
  8052. (list
  8053. (cons "%c" (plist-get p :fromto))
  8054. (cons "%F" (plist-get p :from))
  8055. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8056. (cons "%T" (plist-get p :to))
  8057. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8058. (cons "%s" (plist-get p :subject))
  8059. (cons "%d" (plist-get p :date))
  8060. (cons "%m" (plist-get p :message-id)))))
  8061. (when (string-match "%c" fmt)
  8062. ;; Check if the user wrote this message
  8063. (if (and org-from-is-user-regexp from to
  8064. (save-match-data (string-match org-from-is-user-regexp from)))
  8065. (setq fmt (replace-match "to %t" t t fmt))
  8066. (setq fmt (replace-match "from %f" t t fmt))))
  8067. (org-replace-escapes fmt table)))
  8068. (defun org-make-org-heading-search-string (&optional string heading)
  8069. "Make search string for STRING or current headline."
  8070. (interactive)
  8071. (let ((s (or string (org-get-heading)))
  8072. (lines org-context-in-file-links))
  8073. (unless (and string (not heading))
  8074. ;; We are using a headline, clean up garbage in there.
  8075. (if (string-match org-todo-regexp s)
  8076. (setq s (replace-match "" t t s)))
  8077. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8078. (setq s (replace-match "" t t s)))
  8079. (setq s (org-trim s))
  8080. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8081. org-comment-string "\\)") s)
  8082. (setq s (replace-match "" t t s)))
  8083. (while (string-match org-ts-regexp s)
  8084. (setq s (replace-match "" t t s))))
  8085. (or string (setq s (concat "*" s))) ; Add * for headlines
  8086. (when (and string (integerp lines) (> lines 0))
  8087. (let ((slines (org-split-string s "\n")))
  8088. (when (< lines (length slines))
  8089. (setq s (mapconcat
  8090. 'identity
  8091. (reverse (nthcdr (- (length slines) lines)
  8092. (reverse slines))) "\n")))))
  8093. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8094. (defun org-make-link-string (link &optional description)
  8095. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8096. (unless (string-match "\\S-" link)
  8097. (error "Empty link"))
  8098. (when (and description
  8099. (stringp description)
  8100. (not (string-match "\\S-" description)))
  8101. (setq description nil))
  8102. (when (stringp description)
  8103. ;; Remove brackets from the description, they are fatal.
  8104. (while (string-match "\\[" description)
  8105. (setq description (replace-match "{" t t description)))
  8106. (while (string-match "\\]" description)
  8107. (setq description (replace-match "}" t t description))))
  8108. (when (equal link description)
  8109. ;; No description needed, it is identical
  8110. (setq description nil))
  8111. (when (and (not description)
  8112. (not (string-match (org-image-file-name-regexp) link))
  8113. (not (equal link (org-link-escape link))))
  8114. (setq description (org-extract-attributes link)))
  8115. (setq link
  8116. (cond ((string-match (org-image-file-name-regexp) link) link)
  8117. ((string-match org-link-types-re link)
  8118. (concat (match-string 1 link)
  8119. (org-link-escape (substring link (match-end 1)))))
  8120. (t (org-link-escape link))))
  8121. (concat "[[" link "]"
  8122. (if description (concat "[" description "]") "")
  8123. "]"))
  8124. (defconst org-link-escape-chars
  8125. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8126. "List of characters that should be escaped in link.
  8127. This is the list that is used for internal purposes.")
  8128. (defconst org-link-escape-chars-browser
  8129. '(?\ )
  8130. "List of escapes for characters that are problematic in links.
  8131. This is the list that is used before handing over to the browser.")
  8132. (defun org-link-escape (text &optional table merge)
  8133. "Return percent escaped representation of TEXT.
  8134. TEXT is a string with the text to escape.
  8135. Optional argument TABLE is a list with characters that should be
  8136. escaped. When nil, `org-link-escape-chars' is used.
  8137. If optional argument MERGE is set, merge TABLE into
  8138. `org-link-escape-chars'."
  8139. (cond
  8140. ((and table merge)
  8141. (mapc (lambda (defchr)
  8142. (unless (member defchr table)
  8143. (setq table (cons defchr table)))) org-link-escape-chars))
  8144. ((null table)
  8145. (setq table org-link-escape-chars)))
  8146. (mapconcat
  8147. (lambda (char)
  8148. (if (or (member char table)
  8149. (and (or (< char 32) (= char 37) (> char 126))
  8150. org-url-hexify-p))
  8151. (mapconcat (lambda (sequence-element)
  8152. (format "%%%.2X" sequence-element))
  8153. (or (encode-coding-char char 'utf-8)
  8154. (error "Unable to percent escape character: %s"
  8155. (char-to-string char))) "")
  8156. (char-to-string char))) text ""))
  8157. (defun org-link-unescape (str)
  8158. "Unhex hexified Unicode strings as returned from the JavaScript function
  8159. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8160. (unless (and (null str) (string= "" str))
  8161. (let ((pos 0) (case-fold-search t) unhexed)
  8162. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8163. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8164. (setq str (replace-match unhexed t t str))
  8165. (setq pos (+ pos (length unhexed))))))
  8166. str)
  8167. (defun org-link-unescape-compound (hex)
  8168. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8169. Note: this function also decodes single byte encodings like
  8170. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8171. (save-match-data
  8172. (let* ((bytes (cdr (split-string hex "%")))
  8173. (ret "")
  8174. (eat 0)
  8175. (sum 0))
  8176. (while bytes
  8177. (let* ((val (string-to-number (pop bytes) 16))
  8178. (shift-xor
  8179. (if (= 0 eat)
  8180. (cond
  8181. ((>= val 252) (cons 6 252))
  8182. ((>= val 248) (cons 5 248))
  8183. ((>= val 240) (cons 4 240))
  8184. ((>= val 224) (cons 3 224))
  8185. ((>= val 192) (cons 2 192))
  8186. (t (cons 0 0)))
  8187. (cons 6 128))))
  8188. (if (>= val 192) (setq eat (car shift-xor)))
  8189. (setq val (logxor val (cdr shift-xor)))
  8190. (setq sum (+ (lsh sum (car shift-xor)) val))
  8191. (if (> eat 0) (setq eat (- eat 1)))
  8192. (cond
  8193. ((= 0 eat) ;multi byte
  8194. (setq ret (concat ret (org-char-to-string sum)))
  8195. (setq sum 0))
  8196. ((not bytes) ; single byte(s)
  8197. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8198. )) ;; end (while bytes
  8199. ret )))
  8200. (defun org-link-unescape-single-byte-sequence (hex)
  8201. "Unhexify hex-encoded single byte character sequences."
  8202. (mapconcat (lambda (byte)
  8203. (char-to-string (string-to-number byte 16)))
  8204. (cdr (split-string hex "%")) ""))
  8205. (defun org-xor (a b)
  8206. "Exclusive or."
  8207. (if a (not b) b))
  8208. (defun org-fixup-message-id-for-http (s)
  8209. "Replace special characters in a message id, so it can be used in an http query."
  8210. (when (string-match "%" s)
  8211. (setq s (mapconcat (lambda (c)
  8212. (if (eq c ?%)
  8213. "%25"
  8214. (char-to-string c)))
  8215. s "")))
  8216. (while (string-match "<" s)
  8217. (setq s (replace-match "%3C" t t s)))
  8218. (while (string-match ">" s)
  8219. (setq s (replace-match "%3E" t t s)))
  8220. (while (string-match "@" s)
  8221. (setq s (replace-match "%40" t t s)))
  8222. s)
  8223. (defun org-link-prettify (link)
  8224. "Return a human-readable representation of LINK.
  8225. The car of LINK must be a raw link the cdr of LINK must be either
  8226. a link description or nil."
  8227. (let ((desc (or (cadr link) "<no description>")))
  8228. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8229. "<" (car link) ">")))
  8230. ;;;###autoload
  8231. (defun org-insert-link-global ()
  8232. "Insert a link like Org-mode does.
  8233. This command can be called in any mode to insert a link in Org-mode syntax."
  8234. (interactive)
  8235. (org-load-modules-maybe)
  8236. (org-run-like-in-org-mode 'org-insert-link))
  8237. (defun org-insert-all-links (&optional keep)
  8238. "Insert all links in `org-stored-links'."
  8239. (interactive "P")
  8240. (let ((links (copy-sequence org-stored-links)) l)
  8241. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8242. (insert "- ")
  8243. (org-insert-link nil (car l) (cadr l))
  8244. (insert "\n"))))
  8245. (defun org-link-fontify-links-to-this-file ()
  8246. "Fontify links to the current file in `org-stored-links'."
  8247. (let ((f (buffer-file-name)) a b)
  8248. (setq a (mapcar (lambda(l)
  8249. (let ((ll (car l)))
  8250. (when (and (string-match "^file:\\(.+\\)::" ll)
  8251. (equal f (expand-file-name (match-string 1 ll))))
  8252. ll)))
  8253. org-stored-links))
  8254. (when (featurep 'org-id)
  8255. (setq b (mapcar (lambda(l)
  8256. (let ((ll (car l)))
  8257. (when (and (string-match "^id:\\(.+\\)$" ll)
  8258. (equal f (expand-file-name
  8259. (or (org-id-find-id-file
  8260. (match-string 1 ll)) ""))))
  8261. ll)))
  8262. org-stored-links)))
  8263. (mapcar (lambda(l)
  8264. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8265. (delq nil (append a b)))))
  8266. (defvar org-link-links-in-this-file nil)
  8267. (defun org-insert-link (&optional complete-file link-location default-description)
  8268. "Insert a link. At the prompt, enter the link.
  8269. Completion can be used to insert any of the link protocol prefixes like
  8270. http or ftp in use.
  8271. The history can be used to select a link previously stored with
  8272. `org-store-link'. When the empty string is entered (i.e. if you just
  8273. press RET at the prompt), the link defaults to the most recently
  8274. stored link. As SPC triggers completion in the minibuffer, you need to
  8275. use M-SPC or C-q SPC to force the insertion of a space character.
  8276. You will also be prompted for a description, and if one is given, it will
  8277. be displayed in the buffer instead of the link.
  8278. If there is already a link at point, this command will allow you to edit link
  8279. and description parts.
  8280. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8281. be selected using completion. The path to the file will be relative to the
  8282. current directory if the file is in the current directory or a subdirectory.
  8283. Otherwise, the link will be the absolute path as completed in the minibuffer
  8284. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8285. option `org-link-file-path-type'.
  8286. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8287. the current directory or below.
  8288. With three \\[universal-argument] prefixes, negate the meaning of
  8289. `org-keep-stored-link-after-insertion'.
  8290. If `org-make-link-description-function' is non-nil, this function will be
  8291. called with the link target, and the result will be the default
  8292. link description.
  8293. If the LINK-LOCATION parameter is non-nil, this value will be
  8294. used as the link location instead of reading one interactively.
  8295. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8296. be used as the default description."
  8297. (interactive "P")
  8298. (let* ((wcf (current-window-configuration))
  8299. (region (if (org-region-active-p)
  8300. (buffer-substring (region-beginning) (region-end))))
  8301. (remove (and region (list (region-beginning) (region-end))))
  8302. (desc region)
  8303. tmphist ; byte-compile incorrectly complains about this
  8304. (link link-location)
  8305. (abbrevs org-link-abbrev-alist-local)
  8306. entry file all-prefixes auto-desc)
  8307. (cond
  8308. (link-location) ; specified by arg, just use it.
  8309. ((org-in-regexp org-bracket-link-regexp 1)
  8310. ;; We do have a link at point, and we are going to edit it.
  8311. (setq remove (list (match-beginning 0) (match-end 0)))
  8312. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8313. (setq link (read-string "Link: "
  8314. (org-link-unescape
  8315. (org-match-string-no-properties 1)))))
  8316. ((or (org-in-regexp org-angle-link-re)
  8317. (org-in-regexp org-plain-link-re))
  8318. ;; Convert to bracket link
  8319. (setq remove (list (match-beginning 0) (match-end 0))
  8320. link (read-string "Link: "
  8321. (org-remove-angle-brackets (match-string 0)))))
  8322. ((member complete-file '((4) (16)))
  8323. ;; Completing read for file names.
  8324. (setq link (org-file-complete-link complete-file)))
  8325. (t
  8326. ;; Read link, with completion for stored links.
  8327. (org-link-fontify-links-to-this-file)
  8328. (org-switch-to-buffer-other-window "*Org Links*")
  8329. (with-current-buffer "*Org Links*"
  8330. (erase-buffer)
  8331. (insert "Insert a link.
  8332. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8333. (when org-stored-links
  8334. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8335. (insert (mapconcat 'org-link-prettify
  8336. (reverse org-stored-links) "\n")))
  8337. (goto-char (point-min)))
  8338. (let ((cw (selected-window)))
  8339. (select-window (get-buffer-window "*Org Links*" 'visible))
  8340. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8341. (unless (pos-visible-in-window-p (point-max))
  8342. (org-fit-window-to-buffer))
  8343. (and (window-live-p cw) (select-window cw)))
  8344. ;; Fake a link history, containing the stored links.
  8345. (setq tmphist (append (mapcar 'car org-stored-links)
  8346. org-insert-link-history))
  8347. (setq all-prefixes (append (mapcar 'car abbrevs)
  8348. (mapcar 'car org-link-abbrev-alist)
  8349. org-link-types))
  8350. (unwind-protect
  8351. (progn
  8352. (setq link
  8353. (let ((org-completion-use-ido nil)
  8354. (org-completion-use-iswitchb nil))
  8355. (org-completing-read
  8356. "Link: "
  8357. (append
  8358. (mapcar (lambda (x) (list (concat x ":")))
  8359. all-prefixes)
  8360. (mapcar 'car org-stored-links)
  8361. (mapcar 'cadr org-stored-links))
  8362. nil nil nil
  8363. 'tmphist
  8364. (caar org-stored-links))))
  8365. (if (not (string-match "\\S-" link))
  8366. (error "No link selected"))
  8367. (mapc (lambda(l)
  8368. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8369. org-stored-links)
  8370. (if (or (member link all-prefixes)
  8371. (and (equal ":" (substring link -1))
  8372. (member (substring link 0 -1) all-prefixes)
  8373. (setq link (substring link 0 -1))))
  8374. (setq link (org-link-try-special-completion link))))
  8375. (set-window-configuration wcf)
  8376. (kill-buffer "*Org Links*"))
  8377. (setq entry (assoc link org-stored-links))
  8378. (or entry (push link org-insert-link-history))
  8379. (setq desc (or desc (nth 1 entry)))))
  8380. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8381. (not org-keep-stored-link-after-insertion))
  8382. (setq org-stored-links (delq (assoc link org-stored-links)
  8383. org-stored-links)))
  8384. (if (string-match org-plain-link-re link)
  8385. ;; URL-like link, normalize the use of angular brackets.
  8386. (setq link (org-remove-angle-brackets link)))
  8387. ;; Check if we are linking to the current file with a search
  8388. ;; option If yes, simplify the link by using only the search
  8389. ;; option.
  8390. (when (and buffer-file-name
  8391. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8392. (let* ((path (match-string 1 link))
  8393. (case-fold-search nil)
  8394. (search (match-string 2 link)))
  8395. (save-match-data
  8396. (if (equal (file-truename buffer-file-name) (file-truename path))
  8397. ;; We are linking to this same file, with a search option
  8398. (setq link search)))))
  8399. ;; Check if we can/should use a relative path. If yes, simplify the link
  8400. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8401. (let* ((type (match-string 1 link))
  8402. (path (match-string 2 link))
  8403. (origpath path)
  8404. (case-fold-search nil))
  8405. (cond
  8406. ((or (eq org-link-file-path-type 'absolute)
  8407. (equal complete-file '(16)))
  8408. (setq path (abbreviate-file-name (expand-file-name path))))
  8409. ((eq org-link-file-path-type 'noabbrev)
  8410. (setq path (expand-file-name path)))
  8411. ((eq org-link-file-path-type 'relative)
  8412. (setq path (file-relative-name path)))
  8413. (t
  8414. (save-match-data
  8415. (if (string-match (concat "^" (regexp-quote
  8416. (expand-file-name
  8417. (file-name-as-directory
  8418. default-directory))))
  8419. (expand-file-name path))
  8420. ;; We are linking a file with relative path name.
  8421. (setq path (substring (expand-file-name path)
  8422. (match-end 0)))
  8423. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8424. (setq link (concat type path))
  8425. (if (equal desc origpath)
  8426. (setq desc path))))
  8427. (if org-make-link-description-function
  8428. (setq desc
  8429. (or (condition-case nil
  8430. (funcall org-make-link-description-function link desc)
  8431. (error (progn (message "Can't get link description from `%s'"
  8432. (symbol-name org-make-link-description-function))
  8433. (sit-for 2) nil)))
  8434. (read-string "Description: " default-description)))
  8435. (if default-description (setq desc default-description)
  8436. (setq desc (or (and auto-desc desc)
  8437. (read-string "Description: " desc)))))
  8438. (unless (string-match "\\S-" desc) (setq desc nil))
  8439. (if remove (apply 'delete-region remove))
  8440. (insert (org-make-link-string link desc))))
  8441. (defun org-link-try-special-completion (type)
  8442. "If there is completion support for link type TYPE, offer it."
  8443. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8444. (if (functionp fun)
  8445. (funcall fun)
  8446. (read-string "Link (no completion support): " (concat type ":")))))
  8447. (defun org-file-complete-link (&optional arg)
  8448. "Create a file link using completion."
  8449. (let (file link)
  8450. (setq file (read-file-name "File: "))
  8451. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8452. (pwd1 (file-name-as-directory (abbreviate-file-name
  8453. (expand-file-name ".")))))
  8454. (cond
  8455. ((equal arg '(16))
  8456. (setq link (concat
  8457. "file:"
  8458. (abbreviate-file-name (expand-file-name file)))))
  8459. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8460. (setq link (concat "file:" (match-string 1 file))))
  8461. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8462. (expand-file-name file))
  8463. (setq link (concat
  8464. "file:" (match-string 1 (expand-file-name file)))))
  8465. (t (setq link (concat "file:" file)))))
  8466. link))
  8467. (defun org-completing-read (&rest args)
  8468. "Completing-read with SPACE being a normal character."
  8469. (let ((enable-recursive-minibuffers t)
  8470. (minibuffer-local-completion-map
  8471. (copy-keymap minibuffer-local-completion-map)))
  8472. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8473. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8474. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8475. (apply 'org-icompleting-read args)))
  8476. (defun org-completing-read-no-i (&rest args)
  8477. (let (org-completion-use-ido org-completion-use-iswitchb)
  8478. (apply 'org-completing-read args)))
  8479. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8480. "Use iswitch as a completing-read replacement to choose from choices.
  8481. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8482. from."
  8483. (let* ((iswitchb-use-virtual-buffers nil)
  8484. (iswitchb-make-buflist-hook
  8485. (lambda ()
  8486. (setq iswitchb-temp-buflist choices))))
  8487. (iswitchb-read-buffer prompt)))
  8488. (defun org-icompleting-read (&rest args)
  8489. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8490. (org-without-partial-completion
  8491. (if (and org-completion-use-ido
  8492. (fboundp 'ido-completing-read)
  8493. (boundp 'ido-mode) ido-mode
  8494. (listp (second args)))
  8495. (let ((ido-enter-matching-directory nil))
  8496. (apply 'ido-completing-read (concat (car args))
  8497. (if (consp (car (nth 1 args)))
  8498. (mapcar 'car (nth 1 args))
  8499. (nth 1 args))
  8500. (cddr args)))
  8501. (if (and org-completion-use-iswitchb
  8502. (boundp 'iswitchb-mode) iswitchb-mode
  8503. (listp (second args)))
  8504. (apply 'org-iswitchb-completing-read (concat (car args))
  8505. (if (consp (car (nth 1 args)))
  8506. (mapcar 'car (nth 1 args))
  8507. (nth 1 args))
  8508. (cddr args))
  8509. (apply 'completing-read args)))))
  8510. (defun org-extract-attributes (s)
  8511. "Extract the attributes cookie from a string and set as text property."
  8512. (let (a attr (start 0) key value)
  8513. (save-match-data
  8514. (when (string-match "{{\\([^}]+\\)}}$" s)
  8515. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8516. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8517. (setq key (match-string 1 a) value (match-string 2 a)
  8518. start (match-end 0)
  8519. attr (plist-put attr (intern key) value))))
  8520. (org-add-props s nil 'org-attr attr))
  8521. s))
  8522. (defun org-extract-attributes-from-string (tag)
  8523. (let (key value attr)
  8524. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8525. (setq key (match-string 1 tag) value (match-string 2 tag)
  8526. tag (replace-match "" t t tag)
  8527. attr (plist-put attr (intern key) value)))
  8528. (cons tag attr)))
  8529. (defun org-attributes-to-string (plist)
  8530. "Format a property list into an HTML attribute list."
  8531. (let ((s "") key value)
  8532. (while plist
  8533. (setq key (pop plist) value (pop plist))
  8534. (and value
  8535. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8536. s))
  8537. ;;; Opening/following a link
  8538. (defvar org-link-search-failed nil)
  8539. (defvar org-open-link-functions nil
  8540. "Hook for functions finding a plain text link.
  8541. These functions must take a single argument, the link content.
  8542. They will be called for links that look like [[link text][description]]
  8543. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8544. like a filename (e.g. \"./blue.png\").
  8545. These functions will be called *before* Org attempts to resolve the
  8546. link by doing text searches in the current buffer - so if you want a
  8547. link \"[[target]]\" to still find \"<<target>>\", your function should
  8548. handle this as a special case.
  8549. When the function does handle the link, it must return a non-nil value.
  8550. If it decides that it is not responsible for this link, it must return
  8551. nil to indicate that that Org-mode can continue with other options
  8552. like exact and fuzzy text search.")
  8553. (defun org-next-link ()
  8554. "Move forward to the next link.
  8555. If the link is in hidden text, expose it."
  8556. (interactive)
  8557. (when (and org-link-search-failed (eq this-command last-command))
  8558. (goto-char (point-min))
  8559. (message "Link search wrapped back to beginning of buffer"))
  8560. (setq org-link-search-failed nil)
  8561. (let* ((pos (point))
  8562. (ct (org-context))
  8563. (a (assoc :link ct)))
  8564. (if a (goto-char (nth 2 a)))
  8565. (if (re-search-forward org-any-link-re nil t)
  8566. (progn
  8567. (goto-char (match-beginning 0))
  8568. (if (outline-invisible-p) (org-show-context)))
  8569. (goto-char pos)
  8570. (setq org-link-search-failed t)
  8571. (error "No further link found"))))
  8572. (defun org-previous-link ()
  8573. "Move backward to the previous link.
  8574. If the link is in hidden text, expose it."
  8575. (interactive)
  8576. (when (and org-link-search-failed (eq this-command last-command))
  8577. (goto-char (point-max))
  8578. (message "Link search wrapped back to end of buffer"))
  8579. (setq org-link-search-failed nil)
  8580. (let* ((pos (point))
  8581. (ct (org-context))
  8582. (a (assoc :link ct)))
  8583. (if a (goto-char (nth 1 a)))
  8584. (if (re-search-backward org-any-link-re nil t)
  8585. (progn
  8586. (goto-char (match-beginning 0))
  8587. (if (outline-invisible-p) (org-show-context)))
  8588. (goto-char pos)
  8589. (setq org-link-search-failed t)
  8590. (error "No further link found"))))
  8591. (defun org-translate-link (s)
  8592. "Translate a link string if a translation function has been defined."
  8593. (if (and org-link-translation-function
  8594. (fboundp org-link-translation-function)
  8595. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8596. (progn
  8597. (setq s (funcall org-link-translation-function
  8598. (match-string 1 s) (match-string 2 s)))
  8599. (concat (car s) ":" (cdr s)))
  8600. s))
  8601. (defun org-translate-link-from-planner (type path)
  8602. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8603. This is still an experimental function, your mileage may vary."
  8604. (cond
  8605. ((member type '("http" "https" "news" "ftp"))
  8606. ;; standard Internet links are the same.
  8607. nil)
  8608. ((and (equal type "irc") (string-match "^//" path))
  8609. ;; Planner has two / at the beginning of an irc link, we have 1.
  8610. ;; We should have zero, actually....
  8611. (setq path (substring path 1)))
  8612. ((and (equal type "lisp") (string-match "^/" path))
  8613. ;; Planner has a slash, we do not.
  8614. (setq type "elisp" path (substring path 1)))
  8615. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8616. ;; A typical message link. Planner has the id after the final slash,
  8617. ;; we separate it with a hash mark
  8618. (setq path (concat (match-string 1 path) "#"
  8619. (org-remove-angle-brackets (match-string 2 path)))))
  8620. )
  8621. (cons type path))
  8622. (defun org-find-file-at-mouse (ev)
  8623. "Open file link or URL at mouse."
  8624. (interactive "e")
  8625. (mouse-set-point ev)
  8626. (org-open-at-point 'in-emacs))
  8627. (defun org-open-at-mouse (ev)
  8628. "Open file link or URL at mouse.
  8629. See the docstring of `org-open-file' for details."
  8630. (interactive "e")
  8631. (mouse-set-point ev)
  8632. (if (eq major-mode 'org-agenda-mode)
  8633. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8634. (org-open-at-point))
  8635. (defvar org-window-config-before-follow-link nil
  8636. "The window configuration before following a link.
  8637. This is saved in case the need arises to restore it.")
  8638. (defvar org-open-link-marker (make-marker)
  8639. "Marker pointing to the location where `org-open-at-point; was called.")
  8640. ;;;###autoload
  8641. (defun org-open-at-point-global ()
  8642. "Follow a link like Org-mode does.
  8643. This command can be called in any mode to follow a link that has
  8644. Org-mode syntax."
  8645. (interactive)
  8646. (org-run-like-in-org-mode 'org-open-at-point))
  8647. ;;;###autoload
  8648. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8649. "Open a link in the string S, as if it was in Org-mode."
  8650. (interactive "sLink: \nP")
  8651. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8652. (with-temp-buffer
  8653. (let ((org-inhibit-startup (not reference-buffer)))
  8654. (org-mode)
  8655. (insert s)
  8656. (goto-char (point-min))
  8657. (when reference-buffer
  8658. (setq org-link-abbrev-alist-local
  8659. (with-current-buffer reference-buffer
  8660. org-link-abbrev-alist-local)))
  8661. (org-open-at-point arg reference-buffer)))))
  8662. (defvar org-open-at-point-functions nil
  8663. "Hook that is run when following a link at point.
  8664. Functions in this hook must return t if they identify and follow
  8665. a link at point. If they don't find anything interesting at point,
  8666. they must return nil.")
  8667. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8668. (defun org-open-at-point (&optional arg reference-buffer)
  8669. "Open link at or after point.
  8670. If there is no link at point, this function will search forward up to
  8671. the end of the current line.
  8672. Normally, files will be opened by an appropriate application. If the
  8673. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8674. With a double prefix argument, try to open outside of Emacs, in the
  8675. application the system uses for this file type."
  8676. (interactive "P")
  8677. ;; if in a code block, then open the block's results
  8678. (unless (call-interactively #'org-babel-open-src-block-result)
  8679. (org-load-modules-maybe)
  8680. (move-marker org-open-link-marker (point))
  8681. (setq org-window-config-before-follow-link (current-window-configuration))
  8682. (org-remove-occur-highlights nil nil t)
  8683. (cond
  8684. ((and (org-at-heading-p)
  8685. (not (org-at-timestamp-p t))
  8686. (not (org-in-regexp
  8687. (concat org-plain-link-re "\\|"
  8688. org-bracket-link-regexp "\\|"
  8689. org-angle-link-re "\\|"
  8690. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8691. (not (get-text-property (point) 'org-linked-text)))
  8692. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8693. (lk (car lkall))
  8694. (lkend (cdr lkall)))
  8695. (when lk
  8696. (prog1 (search-forward lk nil lkend)
  8697. (goto-char (match-beginning 0))
  8698. (org-open-at-point))))
  8699. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8700. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8701. ((and (org-at-timestamp-p t)
  8702. (not (org-in-regexp org-bracket-link-regexp)))
  8703. (org-follow-timestamp-link))
  8704. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8705. (not (org-in-regexp org-any-link-re)))
  8706. (org-footnote-action))
  8707. (t
  8708. (let (type path link line search (pos (point)))
  8709. (catch 'match
  8710. (save-excursion
  8711. (skip-chars-forward "^]\n\r")
  8712. (when (org-in-regexp org-bracket-link-regexp 1)
  8713. (setq link (org-extract-attributes
  8714. (org-link-unescape (org-match-string-no-properties 1))))
  8715. (while (string-match " *\n *" link)
  8716. (setq link (replace-match " " t t link)))
  8717. (setq link (org-link-expand-abbrev link))
  8718. (cond
  8719. ((or (file-name-absolute-p link)
  8720. (string-match "^\\.\\.?/" link))
  8721. (setq type "file" path link))
  8722. ((string-match org-link-re-with-space3 link)
  8723. (setq type (match-string 1 link) path (match-string 2 link)))
  8724. ((string-match "^help:+\\(.+\\)" link)
  8725. (setq type "help" path (match-string 1 link)))
  8726. (t (setq type "thisfile" path link)))
  8727. (throw 'match t)))
  8728. (when (get-text-property (point) 'org-linked-text)
  8729. (setq type "thisfile"
  8730. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8731. (1+ (point)) (point))
  8732. path (buffer-substring
  8733. (or (previous-single-property-change pos 'org-linked-text)
  8734. (point-min))
  8735. (or (next-single-property-change pos 'org-linked-text)
  8736. (point-max))))
  8737. (throw 'match t))
  8738. (save-excursion
  8739. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8740. (when (or (org-in-regexp org-angle-link-re)
  8741. (and plinkpos (goto-char (car plinkpos))
  8742. (save-match-data (not (looking-back "\\[\\[")))))
  8743. (setq type (match-string 1)
  8744. path (org-link-unescape (match-string 2)))
  8745. (throw 'match t))))
  8746. (save-excursion
  8747. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8748. (setq type "tags"
  8749. path (match-string 1))
  8750. (while (string-match ":" path)
  8751. (setq path (replace-match "+" t t path)))
  8752. (throw 'match t)))
  8753. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8754. (setq type "tree-match"
  8755. path (match-string 1))
  8756. (throw 'match t)))
  8757. (unless path
  8758. (user-error "No link found"))
  8759. ;; switch back to reference buffer
  8760. ;; needed when if called in a temporary buffer through
  8761. ;; org-open-link-from-string
  8762. (with-current-buffer (or reference-buffer (current-buffer))
  8763. ;; Remove any trailing spaces in path
  8764. (if (string-match " +\\'" path)
  8765. (setq path (replace-match "" t t path)))
  8766. (if (and org-link-translation-function
  8767. (fboundp org-link-translation-function))
  8768. ;; Check if we need to translate the link
  8769. (let ((tmp (funcall org-link-translation-function type path)))
  8770. (setq type (car tmp) path (cdr tmp))))
  8771. (cond
  8772. ((assoc type org-link-protocols)
  8773. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8774. ((equal type "help")
  8775. (let ((f-or-v (intern path)))
  8776. (cond ((fboundp f-or-v)
  8777. (describe-function f-or-v))
  8778. ((boundp f-or-v)
  8779. (describe-variable f-or-v))
  8780. (t (error "Not a known function or variable")))))
  8781. ((equal type "mailto")
  8782. (let ((cmd (car org-link-mailto-program))
  8783. (args (cdr org-link-mailto-program)) args1
  8784. (address path) (subject "") a)
  8785. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8786. (setq address (match-string 1 path)
  8787. subject (org-link-escape (match-string 2 path))))
  8788. (while args
  8789. (cond
  8790. ((not (stringp (car args))) (push (pop args) args1))
  8791. (t (setq a (pop args))
  8792. (if (string-match "%a" a)
  8793. (setq a (replace-match address t t a)))
  8794. (if (string-match "%s" a)
  8795. (setq a (replace-match subject t t a)))
  8796. (push a args1))))
  8797. (apply cmd (nreverse args1))))
  8798. ((member type '("http" "https" "ftp" "news"))
  8799. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8800. (org-link-escape
  8801. path org-link-escape-chars-browser)
  8802. path))))
  8803. ((string= type "doi")
  8804. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8805. (org-link-escape
  8806. path org-link-escape-chars-browser)
  8807. path))))
  8808. ((member type '("message"))
  8809. (browse-url (concat type ":" path)))
  8810. ((string= type "tags")
  8811. (org-tags-view arg path))
  8812. ((string= type "tree-match")
  8813. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8814. ((string= type "file")
  8815. (if (string-match "::\\([0-9]+\\)\\'" path)
  8816. (setq line (string-to-number (match-string 1 path))
  8817. path (substring path 0 (match-beginning 0)))
  8818. (if (string-match "::\\(.+\\)\\'" path)
  8819. (setq search (match-string 1 path)
  8820. path (substring path 0 (match-beginning 0)))))
  8821. (if (string-match "[*?{]" (file-name-nondirectory path))
  8822. (dired path)
  8823. (org-open-file path arg line search)))
  8824. ((string= type "shell")
  8825. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8826. (cmd path))
  8827. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8828. (string-match org-confirm-shell-link-not-regexp cmd))
  8829. (not org-confirm-shell-link-function)
  8830. (funcall org-confirm-shell-link-function
  8831. (format "Execute \"%s\" in shell? "
  8832. (org-add-props cmd nil
  8833. 'face 'org-warning))))
  8834. (progn
  8835. (message "Executing %s" cmd)
  8836. (shell-command cmd buf)
  8837. (if (featurep 'midnight)
  8838. (setq clean-buffer-list-kill-buffer-names
  8839. (cons buf clean-buffer-list-kill-buffer-names))))
  8840. (error "Abort"))))
  8841. ((string= type "elisp")
  8842. (let ((cmd path))
  8843. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8844. (string-match org-confirm-elisp-link-not-regexp cmd))
  8845. (not org-confirm-elisp-link-function)
  8846. (funcall org-confirm-elisp-link-function
  8847. (format "Execute \"%s\" as elisp? "
  8848. (org-add-props cmd nil
  8849. 'face 'org-warning))))
  8850. (message "%s => %s" cmd
  8851. (if (equal (string-to-char cmd) ?\()
  8852. (eval (read cmd))
  8853. (call-interactively (read cmd))))
  8854. (error "Abort"))))
  8855. ((and (string= type "thisfile")
  8856. (run-hook-with-args-until-success
  8857. 'org-open-link-functions path)))
  8858. ((string= type "thisfile")
  8859. (if arg
  8860. (switch-to-buffer-other-window
  8861. (org-get-buffer-for-internal-link (current-buffer)))
  8862. (org-mark-ring-push))
  8863. (let ((cmd `(org-link-search
  8864. ,path
  8865. ,(cond ((equal arg '(4)) ''occur)
  8866. ((equal arg '(16)) ''org-occur))
  8867. ,pos)))
  8868. (condition-case nil (let ((org-link-search-inhibit-query t))
  8869. (eval cmd))
  8870. (error (progn (widen) (eval cmd))))))
  8871. (t (browse-url-at-point)))))))
  8872. (move-marker org-open-link-marker nil)
  8873. (run-hook-with-args 'org-follow-link-hook)))
  8874. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  8875. "Offer links in the current entry and return the selected link.
  8876. If there is only one link, return it.
  8877. If NTH is an integer, return the NTH link found.
  8878. If ZERO is a string, check also this string for a link, and if
  8879. there is one, return it."
  8880. (with-current-buffer buffer
  8881. (save-excursion
  8882. (save-restriction
  8883. (widen)
  8884. (goto-char marker)
  8885. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8886. "\\(" org-angle-link-re "\\)\\|"
  8887. "\\(" org-plain-link-re "\\)"))
  8888. (cnt ?0)
  8889. (in-emacs (if (integerp nth) nil nth))
  8890. have-zero end links link c)
  8891. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8892. (push (match-string 0 zero) links)
  8893. (setq cnt (1- cnt) have-zero t))
  8894. (save-excursion
  8895. (org-back-to-heading t)
  8896. (setq end (save-excursion (outline-next-heading) (point)))
  8897. (while (re-search-forward re end t)
  8898. (push (match-string 0) links))
  8899. (setq links (org-uniquify (reverse links))))
  8900. (cond
  8901. ((null links)
  8902. (message "No links"))
  8903. ((equal (length links) 1)
  8904. (setq link (car links)))
  8905. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8906. (setq link (nth (if have-zero nth (1- nth)) links)))
  8907. (t ; we have to select a link
  8908. (save-excursion
  8909. (save-window-excursion
  8910. (delete-other-windows)
  8911. (with-output-to-temp-buffer "*Select Link*"
  8912. (mapc (lambda (l)
  8913. (if (not (string-match org-bracket-link-regexp l))
  8914. (princ (format "[%c] %s\n" (incf cnt)
  8915. (org-remove-angle-brackets l)))
  8916. (if (match-end 3)
  8917. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8918. (match-string 3 l) (match-string 1 l)))
  8919. (princ (format "[%c] %s\n" (incf cnt)
  8920. (match-string 1 l))))))
  8921. links))
  8922. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8923. (message "Select link to open, RET to open all:")
  8924. (setq c (read-char-exclusive))
  8925. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8926. (when (equal c ?q) (error "Abort"))
  8927. (if (equal c ?\C-m)
  8928. (setq link links)
  8929. (setq nth (- c ?0))
  8930. (if have-zero (setq nth (1+ nth)))
  8931. (unless (and (integerp nth) (>= (length links) nth))
  8932. (error "Invalid link selection"))
  8933. (setq link (nth (1- nth) links)))))
  8934. (cons link end))))))
  8935. ;; Add special file links that specify the way of opening
  8936. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8937. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8938. (defun org-open-file-with-system (path)
  8939. "Open file at PATH using the system way of opening it."
  8940. (org-open-file path 'system))
  8941. (defun org-open-file-with-emacs (path)
  8942. "Open file at PATH in Emacs."
  8943. (org-open-file path 'emacs))
  8944. (defun org-remove-file-link-modifiers ()
  8945. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8946. (goto-char (point-min))
  8947. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8948. (org-if-unprotected
  8949. (replace-match "file:" t t))))
  8950. (eval-after-load "org-exp"
  8951. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8952. 'org-remove-file-link-modifiers))
  8953. ;;;; Time estimates
  8954. (defun org-get-effort (&optional pom)
  8955. "Get the effort estimate for the current entry."
  8956. (org-entry-get pom org-effort-property))
  8957. ;;; File search
  8958. (defvar org-create-file-search-functions nil
  8959. "List of functions to construct the right search string for a file link.
  8960. These functions are called in turn with point at the location to
  8961. which the link should point.
  8962. A function in the hook should first test if it would like to
  8963. handle this file type, for example by checking the `major-mode'
  8964. or the file extension. If it decides not to handle this file, it
  8965. should just return nil to give other functions a chance. If it
  8966. does handle the file, it must return the search string to be used
  8967. when following the link. The search string will be part of the
  8968. file link, given after a double colon, and `org-open-at-point'
  8969. will automatically search for it. If special measures must be
  8970. taken to make the search successful, another function should be
  8971. added to the companion hook `org-execute-file-search-functions',
  8972. which see.
  8973. A function in this hook may also use `setq' to set the variable
  8974. `description' to provide a suggestion for the descriptive text to
  8975. be used for this link when it gets inserted into an Org-mode
  8976. buffer with \\[org-insert-link].")
  8977. (defvar org-execute-file-search-functions nil
  8978. "List of functions to execute a file search triggered by a link.
  8979. Functions added to this hook must accept a single argument, the
  8980. search string that was part of the file link, the part after the
  8981. double colon. The function must first check if it would like to
  8982. handle this search, for example by checking the `major-mode' or
  8983. the file extension. If it decides not to handle this search, it
  8984. should just return nil to give other functions a chance. If it
  8985. does handle the search, it must return a non-nil value to keep
  8986. other functions from trying.
  8987. Each function can access the current prefix argument through the
  8988. variable `current-prefix-argument'. Note that a single prefix is
  8989. used to force opening a link in Emacs, so it may be good to only
  8990. use a numeric or double prefix to guide the search function.
  8991. In case this is needed, a function in this hook can also restore
  8992. the window configuration before `org-open-at-point' was called using:
  8993. (set-window-configuration org-window-config-before-follow-link)")
  8994. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8995. (defun org-link-search (s &optional type avoid-pos stealth)
  8996. "Search for a link search option.
  8997. If S is surrounded by forward slashes, it is interpreted as a
  8998. regular expression. In org-mode files, this will create an `org-occur'
  8999. sparse tree. In ordinary files, `occur' will be used to list matches.
  9000. If the current buffer is in `dired-mode', grep will be used to search
  9001. in all files. If AVOID-POS is given, ignore matches near that position.
  9002. When optional argument STEALTH is non-nil, do not modify
  9003. visibility around point, thus ignoring
  9004. `org-show-hierarchy-above', `org-show-following-heading' and
  9005. `org-show-siblings' variables."
  9006. (let ((case-fold-search t)
  9007. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9008. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9009. (append '(("") (" ") ("\t") ("\n"))
  9010. org-emphasis-alist)
  9011. "\\|") "\\)"))
  9012. (pos (point))
  9013. (pre nil) (post nil)
  9014. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9015. (cond
  9016. ;; First check if there are any special search functions
  9017. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9018. ;; Now try the builtin stuff
  9019. ((and (equal (string-to-char s0) ?#)
  9020. (> (length s0) 1)
  9021. (save-excursion
  9022. (goto-char (point-min))
  9023. (and
  9024. (re-search-forward
  9025. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9026. (setq type 'dedicated
  9027. pos (match-beginning 0))))
  9028. ;; There is an exact target for this
  9029. (goto-char pos)
  9030. (org-back-to-heading t)))
  9031. ((save-excursion
  9032. (goto-char (point-min))
  9033. (and
  9034. (re-search-forward
  9035. (concat "<<" (regexp-quote s0) ">>") nil t)
  9036. (setq type 'dedicated
  9037. pos (match-beginning 0))))
  9038. ;; There is an exact target for this
  9039. (goto-char pos))
  9040. ((save-excursion
  9041. (goto-char (point-min))
  9042. (and
  9043. (re-search-forward
  9044. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9045. (setq type 'dedicated pos (match-beginning 0))))
  9046. ;; Found an invisible target.
  9047. (goto-char pos))
  9048. ((save-excursion
  9049. (goto-char (point-min))
  9050. (and
  9051. (re-search-forward
  9052. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9053. (setq type 'dedicated pos (match-beginning 0))))
  9054. ;; Found an element with a matching #+name affiliated keyword.
  9055. (goto-char pos))
  9056. ((and (string-match "^(\\(.*\\))$" s0)
  9057. (save-excursion
  9058. (goto-char (point-min))
  9059. (and
  9060. (re-search-forward
  9061. (concat "[^[]" (regexp-quote
  9062. (format org-coderef-label-format
  9063. (match-string 1 s0))))
  9064. nil t)
  9065. (setq type 'dedicated
  9066. pos (1+ (match-beginning 0))))))
  9067. ;; There is a coderef target for this
  9068. (goto-char pos))
  9069. ((string-match "^/\\(.*\\)/$" s)
  9070. ;; A regular expression
  9071. (cond
  9072. ((derived-mode-p 'org-mode)
  9073. (org-occur (match-string 1 s)))
  9074. ;;((eq major-mode 'dired-mode)
  9075. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9076. (t (org-do-occur (match-string 1 s)))))
  9077. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9078. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9079. (goto-char (point-min))
  9080. (cond
  9081. ((let (case-fold-search)
  9082. (re-search-forward (format org-complex-heading-regexp-format
  9083. (regexp-quote s))
  9084. nil t))
  9085. ;; OK, found a match
  9086. (setq type 'dedicated)
  9087. (goto-char (match-beginning 0)))
  9088. ((and (not org-link-search-inhibit-query)
  9089. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9090. (y-or-n-p "No match - create this as a new heading? "))
  9091. (goto-char (point-max))
  9092. (or (bolp) (newline))
  9093. (insert "* " s "\n")
  9094. (beginning-of-line 0))
  9095. (t
  9096. (goto-char pos)
  9097. (error "No match"))))
  9098. (t
  9099. ;; A normal search string
  9100. (when (equal (string-to-char s) ?*)
  9101. ;; Anchor on headlines, post may include tags.
  9102. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9103. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9104. s (substring s 1)))
  9105. (remove-text-properties
  9106. 0 (length s)
  9107. '(face nil mouse-face nil keymap nil fontified nil) s)
  9108. ;; Make a series of regular expressions to find a match
  9109. (setq words (org-split-string s "[ \n\r\t]+")
  9110. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9111. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9112. "\\)" markers)
  9113. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9114. re2a (concat "[ \t\r\n]" re2a_)
  9115. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9116. re4 (concat "[^a-zA-Z_]" re4_)
  9117. re1 (concat pre re2 post)
  9118. re3 (concat pre (if pre re4_ re4) post)
  9119. re5 (concat pre ".*" re4)
  9120. re2 (concat pre re2)
  9121. re2a (concat pre (if pre re2a_ re2a))
  9122. re4 (concat pre (if pre re4_ re4))
  9123. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9124. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9125. re5 "\\)"
  9126. ))
  9127. (cond
  9128. ((eq type 'org-occur) (org-occur reall))
  9129. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9130. (t (goto-char (point-min))
  9131. (setq type 'fuzzy)
  9132. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9133. (org-search-not-self 1 re1 nil t)
  9134. (org-search-not-self 1 re2 nil t)
  9135. (org-search-not-self 1 re2a nil t)
  9136. (org-search-not-self 1 re3 nil t)
  9137. (org-search-not-self 1 re4 nil t)
  9138. (org-search-not-self 1 re5 nil t)
  9139. )
  9140. (goto-char (match-beginning 1))
  9141. (goto-char pos)
  9142. (error "No match"))))))
  9143. (and (derived-mode-p 'org-mode)
  9144. (not stealth)
  9145. (org-show-context 'link-search))
  9146. type))
  9147. (defun org-search-not-self (group &rest args)
  9148. "Execute `re-search-forward', but only accept matches that do not
  9149. enclose the position of `org-open-link-marker'."
  9150. (let ((m org-open-link-marker))
  9151. (catch 'exit
  9152. (while (apply 're-search-forward args)
  9153. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9154. (goto-char (match-end group))
  9155. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9156. (> (match-beginning 0) (marker-position m))
  9157. (< (match-end 0) (marker-position m)))
  9158. (save-match-data
  9159. (or (not (org-in-regexp
  9160. org-bracket-link-analytic-regexp 1))
  9161. (not (match-end 4)) ; no description
  9162. (and (<= (match-beginning 4) (point))
  9163. (>= (match-end 4) (point))))))
  9164. (throw 'exit (point))))))))
  9165. (defun org-get-buffer-for-internal-link (buffer)
  9166. "Return a buffer to be used for displaying the link target of internal links."
  9167. (cond
  9168. ((not org-display-internal-link-with-indirect-buffer)
  9169. buffer)
  9170. ((string-match "(Clone)$" (buffer-name buffer))
  9171. (message "Buffer is already a clone, not making another one")
  9172. ;; we also do not modify visibility in this case
  9173. buffer)
  9174. (t ; make a new indirect buffer for displaying the link
  9175. (let* ((bn (buffer-name buffer))
  9176. (ibn (concat bn "(Clone)"))
  9177. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9178. (with-current-buffer ib (org-overview))
  9179. ib))))
  9180. (defun org-do-occur (regexp &optional cleanup)
  9181. "Call the Emacs command `occur'.
  9182. If CLEANUP is non-nil, remove the printout of the regular expression
  9183. in the *Occur* buffer. This is useful if the regex is long and not useful
  9184. to read."
  9185. (occur regexp)
  9186. (when cleanup
  9187. (let ((cwin (selected-window)) win beg end)
  9188. (when (setq win (get-buffer-window "*Occur*"))
  9189. (select-window win))
  9190. (goto-char (point-min))
  9191. (when (re-search-forward "match[a-z]+" nil t)
  9192. (setq beg (match-end 0))
  9193. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9194. (setq end (1- (match-beginning 0)))))
  9195. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9196. (goto-char (point-min))
  9197. (select-window cwin))))
  9198. ;;; The mark ring for links jumps
  9199. (defvar org-mark-ring nil
  9200. "Mark ring for positions before jumps in Org-mode.")
  9201. (defvar org-mark-ring-last-goto nil
  9202. "Last position in the mark ring used to go back.")
  9203. ;; Fill and close the ring
  9204. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9205. (loop for i from 1 to org-mark-ring-length do
  9206. (push (make-marker) org-mark-ring))
  9207. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9208. org-mark-ring)
  9209. (defun org-mark-ring-push (&optional pos buffer)
  9210. "Put the current position or POS into the mark ring and rotate it."
  9211. (interactive)
  9212. (setq pos (or pos (point)))
  9213. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9214. (move-marker (car org-mark-ring)
  9215. (or pos (point))
  9216. (or buffer (current-buffer)))
  9217. (message "%s"
  9218. (substitute-command-keys
  9219. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9220. (defun org-mark-ring-goto (&optional n)
  9221. "Jump to the previous position in the mark ring.
  9222. With prefix arg N, jump back that many stored positions. When
  9223. called several times in succession, walk through the entire ring.
  9224. Org-mode commands jumping to a different position in the current file,
  9225. or to another Org-mode file, automatically push the old position
  9226. onto the ring."
  9227. (interactive "p")
  9228. (let (p m)
  9229. (if (eq last-command this-command)
  9230. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9231. (setq p org-mark-ring))
  9232. (setq org-mark-ring-last-goto p)
  9233. (setq m (car p))
  9234. (org-pop-to-buffer-same-window (marker-buffer m))
  9235. (goto-char m)
  9236. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9237. (defun org-remove-angle-brackets (s)
  9238. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9239. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9240. s)
  9241. (defun org-add-angle-brackets (s)
  9242. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9243. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9244. s)
  9245. (defun org-remove-double-quotes (s)
  9246. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9247. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9248. s)
  9249. ;;; Following specific links
  9250. (defun org-follow-timestamp-link ()
  9251. "Open an agenda view for the time-stamp date/range at point."
  9252. (cond
  9253. ((org-at-date-range-p t)
  9254. (let ((org-agenda-start-on-weekday)
  9255. (t1 (match-string 1))
  9256. (t2 (match-string 2)) tt1 tt2)
  9257. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9258. tt2 (time-to-days (org-time-string-to-time t2)))
  9259. (let ((org-agenda-buffer-tmp-name
  9260. (format "*Org Agenda(a:%s)"
  9261. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9262. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9263. ((org-at-timestamp-p t)
  9264. (let ((org-agenda-buffer-tmp-name
  9265. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9266. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9267. (substring (match-string 1) 0 10)))
  9268. 1)))
  9269. (t (error "This should not happen"))))
  9270. ;;; Following file links
  9271. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9272. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9273. (declare-function mailcap-mime-info
  9274. "mailcap" (string &optional request no-decode))
  9275. (defvar org-wait nil)
  9276. (defun org-open-file (path &optional in-emacs line search)
  9277. "Open the file at PATH.
  9278. First, this expands any special file name abbreviations. Then the
  9279. configuration variable `org-file-apps' is checked if it contains an
  9280. entry for this file type, and if yes, the corresponding command is launched.
  9281. If no application is found, Emacs simply visits the file.
  9282. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9283. With a double \\[universal-argument] \\[universal-argument] \
  9284. prefix arg, Org tries to avoid opening in Emacs
  9285. and to use an external application to visit the file.
  9286. Optional LINE specifies a line to go to, optional SEARCH a string
  9287. to search for. If LINE or SEARCH is given, the file will be
  9288. opened in Emacs, unless an entry from org-file-apps that makes
  9289. use of groups in a regexp matches.
  9290. If you want to change the way frames are used when following a
  9291. link, please customize `org-link-frame-setup'.
  9292. If the file does not exist, an error is thrown."
  9293. (let* ((file (if (equal path "")
  9294. buffer-file-name
  9295. (substitute-in-file-name (expand-file-name path))))
  9296. (file-apps (append org-file-apps (org-default-apps)))
  9297. (apps (org-remove-if
  9298. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9299. (apps-dlink (org-remove-if-not
  9300. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9301. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9302. (dirp (if remp nil (file-directory-p file)))
  9303. (file (if (and dirp org-open-directory-means-index-dot-org)
  9304. (concat (file-name-as-directory file) "index.org")
  9305. file))
  9306. (a-m-a-p (assq 'auto-mode apps))
  9307. (dfile (downcase file))
  9308. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9309. (link (cond ((and (eq line nil)
  9310. (eq search nil))
  9311. file)
  9312. (line
  9313. (concat file "::" (number-to-string line)))
  9314. (search
  9315. (concat file "::" search))))
  9316. (dlink (downcase link))
  9317. (old-buffer (current-buffer))
  9318. (old-pos (point))
  9319. (old-mode major-mode)
  9320. ext cmd link-match-data)
  9321. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9322. (setq ext (match-string 1 dfile))
  9323. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9324. (setq ext (match-string 1 dfile))))
  9325. (cond
  9326. ((member in-emacs '((16) system))
  9327. (setq cmd (cdr (assoc 'system apps))))
  9328. (in-emacs (setq cmd 'emacs))
  9329. (t
  9330. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9331. (and dirp (cdr (assoc 'directory apps)))
  9332. ; first, try matching against apps-dlink
  9333. ; if we get a match here, store the match data for later
  9334. (let ((match (assoc-default dlink apps-dlink
  9335. 'string-match)))
  9336. (if match
  9337. (progn (setq link-match-data (match-data))
  9338. match)
  9339. (progn (setq in-emacs (or in-emacs line search))
  9340. nil))) ; if we have no match in apps-dlink,
  9341. ; always open the file in emacs if line or search
  9342. ; is given (for backwards compatibility)
  9343. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9344. 'string-match)
  9345. (cdr (assoc ext apps))
  9346. (cdr (assoc t apps))))))
  9347. (when (eq cmd 'system)
  9348. (setq cmd (cdr (assoc 'system apps))))
  9349. (when (eq cmd 'default)
  9350. (setq cmd (cdr (assoc t apps))))
  9351. (when (eq cmd 'mailcap)
  9352. (require 'mailcap)
  9353. (mailcap-parse-mailcaps)
  9354. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9355. (command (mailcap-mime-info mime-type)))
  9356. (if (stringp command)
  9357. (setq cmd command)
  9358. (setq cmd 'emacs))))
  9359. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9360. (not (file-exists-p file))
  9361. (not org-open-non-existing-files))
  9362. (error "No such file: %s" file))
  9363. (cond
  9364. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9365. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9366. (while (string-match "['\"]%s['\"]" cmd)
  9367. (setq cmd (replace-match "%s" t t cmd)))
  9368. (while (string-match "%s" cmd)
  9369. (setq cmd (replace-match
  9370. (save-match-data
  9371. (shell-quote-argument
  9372. (convert-standard-filename file)))
  9373. t t cmd)))
  9374. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9375. (save-match-data
  9376. (let ((match-index 1)
  9377. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9378. (set-match-data link-match-data)
  9379. (while (<= match-index number-of-groups)
  9380. (let ((regex (concat "%" (number-to-string match-index)))
  9381. (replace-with (match-string match-index dlink)))
  9382. (while (string-match regex cmd)
  9383. (setq cmd (replace-match replace-with t t cmd))))
  9384. (setq match-index (+ match-index 1)))))
  9385. (save-window-excursion
  9386. (start-process-shell-command cmd nil cmd)
  9387. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9388. ))
  9389. ((or (stringp cmd)
  9390. (eq cmd 'emacs))
  9391. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9392. (widen)
  9393. (if line (org-goto-line line)
  9394. (if search (org-link-search search))))
  9395. ((consp cmd)
  9396. (let ((file (convert-standard-filename file)))
  9397. (save-match-data
  9398. (set-match-data link-match-data)
  9399. (eval cmd))))
  9400. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9401. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9402. (or (not (equal old-buffer (current-buffer)))
  9403. (not (equal old-pos (point))))
  9404. (org-mark-ring-push old-pos old-buffer))))
  9405. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9406. "This function returns non-nil if `entry' uses a regular
  9407. expression which should be matched against the whole link by
  9408. org-open-file.
  9409. It assumes that is the case when the entry uses a regular
  9410. expression which has at least one grouping construct and the
  9411. action is either a lisp form or a command string containing
  9412. '%1', i.e. using at least one subexpression match as a
  9413. parameter."
  9414. (let ((selector (car entry))
  9415. (action (cdr entry)))
  9416. (if (stringp selector)
  9417. (and (> (regexp-opt-depth selector) 0)
  9418. (or (and (stringp action)
  9419. (string-match "%[0-9]" action))
  9420. (consp action)))
  9421. nil)))
  9422. (defun org-default-apps ()
  9423. "Return the default applications for this operating system."
  9424. (cond
  9425. ((eq system-type 'darwin)
  9426. org-file-apps-defaults-macosx)
  9427. ((eq system-type 'windows-nt)
  9428. org-file-apps-defaults-windowsnt)
  9429. (t org-file-apps-defaults-gnu)))
  9430. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9431. "Convert extensions to regular expressions in the cars of LIST.
  9432. Also, weed out any non-string entries, because the return value is used
  9433. only for regexp matching.
  9434. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9435. point to the symbol `emacs', indicating that the file should
  9436. be opened in Emacs."
  9437. (append
  9438. (delq nil
  9439. (mapcar (lambda (x)
  9440. (if (not (stringp (car x)))
  9441. nil
  9442. (if (string-match "\\W" (car x))
  9443. x
  9444. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9445. list))
  9446. (if add-auto-mode
  9447. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9448. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9449. (defun org-file-remote-p (file)
  9450. "Test whether FILE specifies a location on a remote system.
  9451. Return non-nil if the location is indeed remote.
  9452. For example, the filename \"/user@host:/foo\" specifies a location
  9453. on the system \"/user@host:\"."
  9454. (cond ((fboundp 'file-remote-p)
  9455. (file-remote-p file))
  9456. ((fboundp 'tramp-handle-file-remote-p)
  9457. (tramp-handle-file-remote-p file))
  9458. ((and (boundp 'ange-ftp-name-format)
  9459. (string-match (car ange-ftp-name-format) file))
  9460. t)))
  9461. ;;;; Refiling
  9462. (defun org-get-org-file ()
  9463. "Read a filename, with default directory `org-directory'."
  9464. (let ((default (or org-default-notes-file remember-data-file)))
  9465. (read-file-name (format "File name [%s]: " default)
  9466. (file-name-as-directory org-directory)
  9467. default)))
  9468. (defun org-notes-order-reversed-p ()
  9469. "Check if the current file should receive notes in reversed order."
  9470. (cond
  9471. ((not org-reverse-note-order) nil)
  9472. ((eq t org-reverse-note-order) t)
  9473. ((not (listp org-reverse-note-order)) nil)
  9474. (t (catch 'exit
  9475. (let ((all org-reverse-note-order)
  9476. entry)
  9477. (while (setq entry (pop all))
  9478. (if (string-match (car entry) buffer-file-name)
  9479. (throw 'exit (cdr entry))))
  9480. nil)))))
  9481. (defvar org-refile-target-table nil
  9482. "The list of refile targets, created by `org-refile'.")
  9483. (defvar org-agenda-new-buffers nil
  9484. "Buffers created to visit agenda files.")
  9485. (defvar org-refile-cache nil
  9486. "Cache for refile targets.")
  9487. (defvar org-refile-markers nil
  9488. "All the markers used for caching refile locations.")
  9489. (defun org-refile-marker (pos)
  9490. "Get a new refile marker, but only if caching is in use."
  9491. (if (not org-refile-use-cache)
  9492. pos
  9493. (let ((m (make-marker)))
  9494. (move-marker m pos)
  9495. (push m org-refile-markers)
  9496. m)))
  9497. (defun org-refile-cache-clear ()
  9498. "Clear the refile cache and disable all the markers."
  9499. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9500. (setq org-refile-markers nil)
  9501. (setq org-refile-cache nil)
  9502. (message "Refile cache has been cleared"))
  9503. (defun org-refile-cache-check-set (set)
  9504. "Check if all the markers in the cache still have live buffers."
  9505. (let (marker)
  9506. (catch 'exit
  9507. (while (and set (setq marker (nth 3 (pop set))))
  9508. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9509. (when (and marker (null (marker-buffer marker)))
  9510. (message "not found") (sit-for 3)
  9511. (throw 'exit nil)))
  9512. t)))
  9513. (defun org-refile-cache-put (set &rest identifiers)
  9514. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9515. (let* ((key (sha1 (prin1-to-string identifiers)))
  9516. (entry (assoc key org-refile-cache)))
  9517. (if entry
  9518. (setcdr entry set)
  9519. (push (cons key set) org-refile-cache))))
  9520. (defun org-refile-cache-get (&rest identifiers)
  9521. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9522. (cond
  9523. ((not org-refile-cache) nil)
  9524. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9525. (t
  9526. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9527. org-refile-cache))))
  9528. (and set (org-refile-cache-check-set set) set)))))
  9529. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9530. "Produce a table with refile targets."
  9531. (let ((case-fold-search nil)
  9532. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9533. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9534. targets tgs txt re files f desc descre fast-path-p level pos0)
  9535. (message "Getting targets...")
  9536. (with-current-buffer (or default-buffer (current-buffer))
  9537. (while (setq entry (pop entries))
  9538. (setq files (car entry) desc (cdr entry))
  9539. (setq fast-path-p nil)
  9540. (cond
  9541. ((null files) (setq files (list (current-buffer))))
  9542. ((eq files 'org-agenda-files)
  9543. (setq files (org-agenda-files 'unrestricted)))
  9544. ((and (symbolp files) (fboundp files))
  9545. (setq files (funcall files)))
  9546. ((and (symbolp files) (boundp files))
  9547. (setq files (symbol-value files))))
  9548. (if (stringp files) (setq files (list files)))
  9549. (cond
  9550. ((eq (car desc) :tag)
  9551. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9552. ((eq (car desc) :todo)
  9553. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9554. ((eq (car desc) :regexp)
  9555. (setq descre (cdr desc)))
  9556. ((eq (car desc) :level)
  9557. (setq descre (concat "^\\*\\{" (number-to-string
  9558. (if org-odd-levels-only
  9559. (1- (* 2 (cdr desc)))
  9560. (cdr desc)))
  9561. "\\}[ \t]")))
  9562. ((eq (car desc) :maxlevel)
  9563. (setq fast-path-p t)
  9564. (setq descre (concat "^\\*\\{1," (number-to-string
  9565. (if org-odd-levels-only
  9566. (1- (* 2 (cdr desc)))
  9567. (cdr desc)))
  9568. "\\}[ \t]")))
  9569. (t (error "Bad refiling target description %s" desc)))
  9570. (while (setq f (pop files))
  9571. (with-current-buffer
  9572. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9573. (or
  9574. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9575. (progn
  9576. (if (bufferp f) (setq f (buffer-file-name
  9577. (buffer-base-buffer f))))
  9578. (setq f (and f (expand-file-name f)))
  9579. (if (eq org-refile-use-outline-path 'file)
  9580. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9581. (save-excursion
  9582. (save-restriction
  9583. (widen)
  9584. (goto-char (point-min))
  9585. (while (re-search-forward descre nil t)
  9586. (goto-char (setq pos0 (point-at-bol)))
  9587. (catch 'next
  9588. (when org-refile-target-verify-function
  9589. (save-match-data
  9590. (or (funcall org-refile-target-verify-function)
  9591. (throw 'next t))))
  9592. (when (and (looking-at org-complex-heading-regexp)
  9593. (not (member (match-string 4) excluded-entries))
  9594. (match-string 4))
  9595. (setq level (org-reduced-level
  9596. (- (match-end 1) (match-beginning 1)))
  9597. txt (org-link-display-format (match-string 4))
  9598. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9599. re (format org-complex-heading-regexp-format
  9600. (regexp-quote (match-string 4))))
  9601. (when org-refile-use-outline-path
  9602. (setq txt (mapconcat
  9603. 'org-protect-slash
  9604. (append
  9605. (if (eq org-refile-use-outline-path
  9606. 'file)
  9607. (list (file-name-nondirectory
  9608. (buffer-file-name
  9609. (buffer-base-buffer))))
  9610. (if (eq org-refile-use-outline-path
  9611. 'full-file-path)
  9612. (list (buffer-file-name
  9613. (buffer-base-buffer)))))
  9614. (org-get-outline-path fast-path-p
  9615. level txt)
  9616. (list txt))
  9617. "/")))
  9618. (push (list txt f re (org-refile-marker (point)))
  9619. tgs)))
  9620. (when (= (point) pos0)
  9621. ;; verification function has not moved point
  9622. (goto-char (point-at-eol))))))))
  9623. (when org-refile-use-cache
  9624. (org-refile-cache-put tgs (buffer-file-name) descre))
  9625. (setq targets (append tgs targets))
  9626. ))))
  9627. (message "Getting targets...done")
  9628. (nreverse targets)))
  9629. (defun org-protect-slash (s)
  9630. (while (string-match "/" s)
  9631. (setq s (replace-match "\\" t t s)))
  9632. s)
  9633. (defvar org-olpa (make-vector 20 nil))
  9634. (defun org-get-outline-path (&optional fastp level heading)
  9635. "Return the outline path to the current entry, as a list.
  9636. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9637. routine which makes outline path derivations for an entire file,
  9638. avoiding backtracing. Refile target collection makes use of that."
  9639. (if fastp
  9640. (progn
  9641. (if (> level 19)
  9642. (error "Outline path failure, more than 19 levels"))
  9643. (loop for i from level upto 19 do
  9644. (aset org-olpa i nil))
  9645. (prog1
  9646. (delq nil (append org-olpa nil))
  9647. (aset org-olpa level heading)))
  9648. (let (rtn case-fold-search)
  9649. (save-excursion
  9650. (save-restriction
  9651. (widen)
  9652. (while (org-up-heading-safe)
  9653. (when (looking-at org-complex-heading-regexp)
  9654. (push (org-match-string-no-properties 4) rtn)))
  9655. rtn)))))
  9656. (defun org-format-outline-path (path &optional width prefix)
  9657. "Format the outline path PATH for display.
  9658. Width is the maximum number of characters that is available.
  9659. Prefix is a prefix to be included in the returned string,
  9660. such as the file name."
  9661. (setq width (or width 79))
  9662. (if prefix (setq width (- width (length prefix))))
  9663. (if (not path)
  9664. (or prefix "")
  9665. (let* ((nsteps (length path))
  9666. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9667. (maxwidth (if (<= total-width width)
  9668. 10000 ;; everything fits
  9669. ;; we need to shorten the level headings
  9670. (/ (- width nsteps) nsteps)))
  9671. (org-odd-levels-only nil)
  9672. (n 0)
  9673. (total (1+ (length prefix))))
  9674. (setq maxwidth (max maxwidth 10))
  9675. (concat prefix
  9676. (mapconcat
  9677. (lambda (h)
  9678. (setq n (1+ n))
  9679. (if (and (= n nsteps) (< maxwidth 10000))
  9680. (setq maxwidth (- total-width total)))
  9681. (if (< (length h) maxwidth)
  9682. (progn (setq total (+ total (length h) 1)) h)
  9683. (setq h (substring h 0 (- maxwidth 2))
  9684. total (+ total maxwidth 1))
  9685. (if (string-match "[ \t]+\\'" h)
  9686. (setq h (substring h 0 (match-beginning 0))))
  9687. (setq h (concat h "..")))
  9688. (org-add-props h nil 'face
  9689. (nth (% (1- n) org-n-level-faces)
  9690. org-level-faces))
  9691. h)
  9692. path "/")))))
  9693. (defun org-display-outline-path (&optional file current)
  9694. "Display the current outline path in the echo area."
  9695. (interactive "P")
  9696. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9697. (case-fold-search nil)
  9698. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9699. (if current (setq path (append path
  9700. (save-excursion
  9701. (org-back-to-heading t)
  9702. (if (looking-at org-complex-heading-regexp)
  9703. (list (match-string 4)))))))
  9704. (message "%s"
  9705. (org-format-outline-path
  9706. path
  9707. (1- (frame-width))
  9708. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9709. (defvar org-refile-history nil
  9710. "History for refiling operations.")
  9711. (defvar org-after-refile-insert-hook nil
  9712. "Hook run after `org-refile' has inserted its stuff at the new location.
  9713. Note that this is still *before* the stuff will be removed from
  9714. the *old* location.")
  9715. (defvar org-capture-last-stored-marker)
  9716. (defun org-refile (&optional goto default-buffer rfloc)
  9717. "Move the entry or entries at point to another heading.
  9718. The list of target headings is compiled using the information in
  9719. `org-refile-targets', which see.
  9720. At the target location, the entry is filed as a subitem of the target
  9721. heading. Depending on `org-reverse-note-order', the new subitem will
  9722. either be the first or the last subitem.
  9723. If there is an active region, all entries in that region will be moved.
  9724. However, the region must fulfill the requirement that the first heading
  9725. is the first one sets the top-level of the moved text - at most siblings
  9726. below it are allowed.
  9727. With prefix arg GOTO, the command will only visit the target location
  9728. and not actually move anything.
  9729. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9730. go to the location where the last refiling operation has put the subtree.
  9731. With a prefix argument of `2', refile to the running clock.
  9732. RFLOC can be a refile location obtained in a different way.
  9733. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9734. If you are using target caching (see `org-refile-use-cache'),
  9735. you have to clear the target cache in order to find new targets.
  9736. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9737. prefix argument (`C-u C-u C-u C-c C-w')."
  9738. (interactive "P")
  9739. (if (member goto '(0 (64)))
  9740. (org-refile-cache-clear)
  9741. (let* ((cbuf (current-buffer))
  9742. (regionp (org-region-active-p))
  9743. (region-start (and regionp (region-beginning)))
  9744. (region-end (and regionp (region-end)))
  9745. (region-length (and regionp (- region-end region-start)))
  9746. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9747. pos it nbuf file re level reversed)
  9748. (setq last-command nil)
  9749. (when regionp
  9750. (goto-char region-start)
  9751. (or (bolp) (goto-char (point-at-bol)))
  9752. (setq region-start (point))
  9753. (unless (or (org-kill-is-subtree-p
  9754. (buffer-substring region-start region-end))
  9755. (prog1 org-refile-active-region-within-subtree
  9756. (org-toggle-heading)))
  9757. (error "The region is not a (sequence of) subtree(s)")))
  9758. (if (equal goto '(16))
  9759. (org-refile-goto-last-stored)
  9760. (when (or
  9761. (and (equal goto 2)
  9762. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9763. (prog1
  9764. (setq it (list (or org-clock-heading "running clock")
  9765. (buffer-file-name
  9766. (marker-buffer org-clock-hd-marker))
  9767. ""
  9768. (marker-position org-clock-hd-marker)))
  9769. (setq goto nil)))
  9770. (setq it (or rfloc
  9771. (let (heading-text)
  9772. (save-excursion
  9773. (unless goto
  9774. (org-back-to-heading t)
  9775. (setq heading-text
  9776. (nth 4 (org-heading-components))))
  9777. (org-refile-get-location
  9778. (cond (goto "Goto")
  9779. (regionp "Refile region to")
  9780. (t (concat "Refile subtree \""
  9781. heading-text "\" to")))
  9782. default-buffer
  9783. (and (not (equal '(4) goto))
  9784. org-refile-allow-creating-parent-nodes)
  9785. goto))))))
  9786. (setq file (nth 1 it)
  9787. re (nth 2 it)
  9788. pos (nth 3 it))
  9789. (if (and (not goto)
  9790. pos
  9791. (equal (buffer-file-name) file)
  9792. (if regionp
  9793. (and (>= pos region-start)
  9794. (<= pos region-end))
  9795. (and (>= pos (point))
  9796. (< pos (save-excursion
  9797. (org-end-of-subtree t t))))))
  9798. (error "Cannot refile to position inside the tree or region"))
  9799. (setq nbuf (or (find-buffer-visiting file)
  9800. (find-file-noselect file)))
  9801. (if goto
  9802. (progn
  9803. (org-pop-to-buffer-same-window nbuf)
  9804. (goto-char pos)
  9805. (org-show-context 'org-goto))
  9806. (if regionp
  9807. (progn
  9808. (org-kill-new (buffer-substring region-start region-end))
  9809. (org-save-markers-in-region region-start region-end))
  9810. (org-copy-subtree 1 nil t))
  9811. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9812. (find-file-noselect file)))
  9813. (setq reversed (org-notes-order-reversed-p))
  9814. (save-excursion
  9815. (save-restriction
  9816. (widen)
  9817. (if pos
  9818. (progn
  9819. (goto-char pos)
  9820. (looking-at org-outline-regexp)
  9821. (setq level (org-get-valid-level (funcall outline-level) 1))
  9822. (goto-char
  9823. (if reversed
  9824. (or (outline-next-heading) (point-max))
  9825. (or (save-excursion (org-get-next-sibling))
  9826. (org-end-of-subtree t t)
  9827. (point-max)))))
  9828. (setq level 1)
  9829. (if (not reversed)
  9830. (goto-char (point-max))
  9831. (goto-char (point-min))
  9832. (or (outline-next-heading) (goto-char (point-max)))))
  9833. (if (not (bolp)) (newline))
  9834. (org-paste-subtree level)
  9835. (when org-log-refile
  9836. (org-add-log-setup 'refile nil nil 'findpos
  9837. org-log-refile)
  9838. (unless (eq org-log-refile 'note)
  9839. (save-excursion (org-add-log-note))))
  9840. (and org-auto-align-tags
  9841. (let ((org-loop-over-headlines-in-active-region nil))
  9842. (org-set-tags nil t)))
  9843. (with-demoted-errors
  9844. (bookmark-set "org-refile-last-stored"))
  9845. ;; If we are refiling for capture, make sure that the
  9846. ;; last-capture pointers point here
  9847. (when (org-bound-and-true-p org-refile-for-capture)
  9848. (with-demoted-errors
  9849. (bookmark-set "org-capture-last-stored-marker"))
  9850. (move-marker org-capture-last-stored-marker (point)))
  9851. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9852. (run-hooks 'org-after-refile-insert-hook))))
  9853. (if regionp
  9854. (delete-region (point) (+ (point) region-length))
  9855. (org-cut-subtree))
  9856. (when (featurep 'org-inlinetask)
  9857. (org-inlinetask-remove-END-maybe))
  9858. (setq org-markers-to-move nil)
  9859. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9860. (defun org-refile-goto-last-stored ()
  9861. "Go to the location where the last refile was stored."
  9862. (interactive)
  9863. (bookmark-jump "org-refile-last-stored")
  9864. (message "This is the location of the last refile"))
  9865. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9866. no-exclude)
  9867. "Prompt the user for a refile location, using PROMPT.
  9868. PROMPT should not be suffixed with a colon and a space, because
  9869. this function appends the default value from
  9870. `org-refile-history' automatically, if that is not empty.
  9871. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9872. this is used for the GOTO interface."
  9873. (let ((org-refile-targets org-refile-targets)
  9874. (org-refile-use-outline-path org-refile-use-outline-path)
  9875. excluded-entries)
  9876. (when (and (derived-mode-p 'org-mode)
  9877. (not org-refile-use-cache)
  9878. (not no-exclude))
  9879. (org-map-tree
  9880. (lambda()
  9881. (setq excluded-entries
  9882. (append excluded-entries (list (org-get-heading t t)))))))
  9883. (setq org-refile-target-table
  9884. (org-refile-get-targets default-buffer excluded-entries)))
  9885. (unless org-refile-target-table
  9886. (error "No refile targets"))
  9887. (let* ((prompt (concat prompt
  9888. (and (car org-refile-history)
  9889. (concat " (default " (car org-refile-history) ")"))
  9890. ": "))
  9891. (cbuf (current-buffer))
  9892. (partial-completion-mode nil)
  9893. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9894. (cfunc (if (and org-refile-use-outline-path
  9895. org-outline-path-complete-in-steps)
  9896. 'org-olpath-completing-read
  9897. 'org-icompleting-read))
  9898. (extra (if org-refile-use-outline-path "/" ""))
  9899. (filename (and cfn (expand-file-name cfn)))
  9900. (tbl (mapcar
  9901. (lambda (x)
  9902. (if (and (not (member org-refile-use-outline-path
  9903. '(file full-file-path)))
  9904. (not (equal filename (nth 1 x))))
  9905. (cons (concat (car x) extra " ("
  9906. (file-name-nondirectory (nth 1 x)) ")")
  9907. (cdr x))
  9908. (cons (concat (car x) extra) (cdr x))))
  9909. org-refile-target-table))
  9910. (completion-ignore-case t)
  9911. pa answ parent-target child parent old-hist)
  9912. (setq old-hist org-refile-history)
  9913. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9914. nil 'org-refile-history (car org-refile-history)))
  9915. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9916. (org-refile-check-position pa)
  9917. (if pa
  9918. (progn
  9919. (when (or (not org-refile-history)
  9920. (not (eq old-hist org-refile-history))
  9921. (not (equal (car pa) (car org-refile-history))))
  9922. (setq org-refile-history
  9923. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9924. org-refile-history
  9925. (cdr org-refile-history))))
  9926. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9927. (pop org-refile-history)))
  9928. pa)
  9929. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9930. (progn
  9931. (setq parent (match-string 1 answ)
  9932. child (match-string 2 answ))
  9933. (setq parent-target (or (assoc parent tbl)
  9934. (assoc (concat parent "/") tbl)))
  9935. (when (and parent-target
  9936. (or (eq new-nodes t)
  9937. (and (eq new-nodes 'confirm)
  9938. (y-or-n-p (format "Create new node \"%s\"? "
  9939. child)))))
  9940. (org-refile-new-child parent-target child)))
  9941. (error "Invalid target location")))))
  9942. (declare-function org-string-nw-p "org-macs" (s))
  9943. (defun org-refile-check-position (refile-pointer)
  9944. "Check if the refile pointer matches the headline to which it points."
  9945. (let* ((file (nth 1 refile-pointer))
  9946. (re (nth 2 refile-pointer))
  9947. (pos (nth 3 refile-pointer))
  9948. buffer)
  9949. (if (and (not (markerp pos)) (not file))
  9950. (error "Please save the buffer to a file before refiling")
  9951. (when (org-string-nw-p re)
  9952. (setq buffer (if (markerp pos)
  9953. (marker-buffer pos)
  9954. (or (find-buffer-visiting file)
  9955. (find-file-noselect file))))
  9956. (with-current-buffer buffer
  9957. (save-excursion
  9958. (save-restriction
  9959. (widen)
  9960. (goto-char pos)
  9961. (beginning-of-line 1)
  9962. (unless (org-looking-at-p re)
  9963. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  9964. (defun org-refile-new-child (parent-target child)
  9965. "Use refile target PARENT-TARGET to add new CHILD below it."
  9966. (unless parent-target
  9967. (error "Cannot find parent for new node"))
  9968. (let ((file (nth 1 parent-target))
  9969. (pos (nth 3 parent-target))
  9970. level)
  9971. (with-current-buffer (or (find-buffer-visiting file)
  9972. (find-file-noselect file))
  9973. (save-excursion
  9974. (save-restriction
  9975. (widen)
  9976. (if pos
  9977. (goto-char pos)
  9978. (goto-char (point-max))
  9979. (if (not (bolp)) (newline)))
  9980. (when (looking-at org-outline-regexp)
  9981. (setq level (funcall outline-level))
  9982. (org-end-of-subtree t t))
  9983. (org-back-over-empty-lines)
  9984. (insert "\n" (make-string
  9985. (if pos (org-get-valid-level level 1) 1) ?*)
  9986. " " child "\n")
  9987. (beginning-of-line 0)
  9988. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9989. (defun org-olpath-completing-read (prompt collection &rest args)
  9990. "Read an outline path like a file name."
  9991. (let ((thetable collection)
  9992. (org-completion-use-ido nil) ; does not work with ido.
  9993. (org-completion-use-iswitchb nil)) ; or iswitchb
  9994. (apply
  9995. 'org-icompleting-read prompt
  9996. (lambda (string predicate &optional flag)
  9997. (let (rtn r f (l (length string)))
  9998. (cond
  9999. ((eq flag nil)
  10000. ;; try completion
  10001. (try-completion string thetable))
  10002. ((eq flag t)
  10003. ;; all-completions
  10004. (setq rtn (all-completions string thetable predicate))
  10005. (mapcar
  10006. (lambda (x)
  10007. (setq r (substring x l))
  10008. (if (string-match " ([^)]*)$" x)
  10009. (setq f (match-string 0 x))
  10010. (setq f ""))
  10011. (if (string-match "/" r)
  10012. (concat string (substring r 0 (match-end 0)) f)
  10013. x))
  10014. rtn))
  10015. ((eq flag 'lambda)
  10016. ;; exact match?
  10017. (assoc string thetable)))))
  10018. args)))
  10019. ;;;; Dynamic blocks
  10020. (defun org-find-dblock (name)
  10021. "Find the first dynamic block with name NAME in the buffer.
  10022. If not found, stay at current position and return nil."
  10023. (let ((case-fold-search t) pos)
  10024. (save-excursion
  10025. (goto-char (point-min))
  10026. (setq pos (and (re-search-forward
  10027. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10028. (match-beginning 0))))
  10029. (if pos (goto-char pos))
  10030. pos))
  10031. (defconst org-dblock-start-re
  10032. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10033. "Matches the start line of a dynamic block, with parameters.")
  10034. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10035. "Matches the end of a dynamic block.")
  10036. (defun org-create-dblock (plist)
  10037. "Create a dynamic block section, with parameters taken from PLIST.
  10038. PLIST must contain a :name entry which is used as name of the block."
  10039. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10040. (end-of-line 1)
  10041. (newline))
  10042. (let ((col (current-column))
  10043. (name (plist-get plist :name)))
  10044. (insert "#+BEGIN: " name)
  10045. (while plist
  10046. (if (eq (car plist) :name)
  10047. (setq plist (cddr plist))
  10048. (insert " " (prin1-to-string (pop plist)))))
  10049. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10050. (beginning-of-line -2)))
  10051. (defun org-prepare-dblock ()
  10052. "Prepare dynamic block for refresh.
  10053. This empties the block, puts the cursor at the insert position and returns
  10054. the property list including an extra property :name with the block name."
  10055. (unless (looking-at org-dblock-start-re)
  10056. (error "Not at a dynamic block"))
  10057. (let* ((begdel (1+ (match-end 0)))
  10058. (name (org-no-properties (match-string 1)))
  10059. (params (append (list :name name)
  10060. (read (concat "(" (match-string 3) ")")))))
  10061. (save-excursion
  10062. (beginning-of-line 1)
  10063. (skip-chars-forward " \t")
  10064. (setq params (plist-put params :indentation-column (current-column))))
  10065. (unless (re-search-forward org-dblock-end-re nil t)
  10066. (error "Dynamic block not terminated"))
  10067. (setq params
  10068. (append params
  10069. (list :content (buffer-substring
  10070. begdel (match-beginning 0)))))
  10071. (delete-region begdel (match-beginning 0))
  10072. (goto-char begdel)
  10073. (open-line 1)
  10074. params))
  10075. (defun org-map-dblocks (&optional command)
  10076. "Apply COMMAND to all dynamic blocks in the current buffer.
  10077. If COMMAND is not given, use `org-update-dblock'."
  10078. (let ((cmd (or command 'org-update-dblock)))
  10079. (save-excursion
  10080. (goto-char (point-min))
  10081. (while (re-search-forward org-dblock-start-re nil t)
  10082. (goto-char (match-beginning 0))
  10083. (save-excursion
  10084. (condition-case nil
  10085. (funcall cmd)
  10086. (error (message "Error during update of dynamic block"))))
  10087. (unless (re-search-forward org-dblock-end-re nil t)
  10088. (error "Dynamic block not terminated"))))))
  10089. (defun org-dblock-update (&optional arg)
  10090. "User command for updating dynamic blocks.
  10091. Update the dynamic block at point. With prefix ARG, update all dynamic
  10092. blocks in the buffer."
  10093. (interactive "P")
  10094. (if arg
  10095. (org-update-all-dblocks)
  10096. (or (looking-at org-dblock-start-re)
  10097. (org-beginning-of-dblock))
  10098. (org-update-dblock)))
  10099. (defun org-update-dblock ()
  10100. "Update the dynamic block at point.
  10101. This means to empty the block, parse for parameters and then call
  10102. the correct writing function."
  10103. (interactive)
  10104. (save-window-excursion
  10105. (let* ((pos (point))
  10106. (line (org-current-line))
  10107. (params (org-prepare-dblock))
  10108. (name (plist-get params :name))
  10109. (indent (plist-get params :indentation-column))
  10110. (cmd (intern (concat "org-dblock-write:" name))))
  10111. (message "Updating dynamic block `%s' at line %d..." name line)
  10112. (funcall cmd params)
  10113. (message "Updating dynamic block `%s' at line %d...done" name line)
  10114. (goto-char pos)
  10115. (when (and indent (> indent 0))
  10116. (setq indent (make-string indent ?\ ))
  10117. (save-excursion
  10118. (org-beginning-of-dblock)
  10119. (forward-line 1)
  10120. (while (not (looking-at org-dblock-end-re))
  10121. (insert indent)
  10122. (beginning-of-line 2))
  10123. (when (looking-at org-dblock-end-re)
  10124. (and (looking-at "[ \t]+")
  10125. (replace-match ""))
  10126. (insert indent)))))))
  10127. (defun org-beginning-of-dblock ()
  10128. "Find the beginning of the dynamic block at point.
  10129. Error if there is no such block at point."
  10130. (let ((pos (point))
  10131. beg)
  10132. (end-of-line 1)
  10133. (if (and (re-search-backward org-dblock-start-re nil t)
  10134. (setq beg (match-beginning 0))
  10135. (re-search-forward org-dblock-end-re nil t)
  10136. (> (match-end 0) pos))
  10137. (goto-char beg)
  10138. (goto-char pos)
  10139. (error "Not in a dynamic block"))))
  10140. (defun org-update-all-dblocks ()
  10141. "Update all dynamic blocks in the buffer.
  10142. This function can be used in a hook."
  10143. (interactive)
  10144. (when (derived-mode-p 'org-mode)
  10145. (org-map-dblocks 'org-update-dblock)))
  10146. ;;;; Completion
  10147. (defconst org-additional-option-like-keywords
  10148. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10149. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10150. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10151. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10152. "BEGIN:" "END:"
  10153. "ORGTBL" "TBLFM:" "TBLNAME:"
  10154. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10155. "BEGIN_VERBATIM" "END_VERBATIM"
  10156. "BEGIN_QUOTE" "END_QUOTE"
  10157. "BEGIN_VERSE" "END_VERSE"
  10158. "BEGIN_CENTER" "END_CENTER"
  10159. "BEGIN_SRC" "END_SRC"
  10160. "BEGIN_RESULT" "END_RESULT"
  10161. "BEGIN_lstlisting" "END_lstlisting"
  10162. "NAME:" "RESULTS:"
  10163. "HEADER:" "HEADERS:"
  10164. "COLUMNS:" "PROPERTY:"
  10165. "CAPTION:" "LABEL:"
  10166. "SETUPFILE:"
  10167. "INCLUDE:" "INDEX:"
  10168. "BIND:"
  10169. "MACRO:"))
  10170. (defconst org-options-keywords
  10171. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10172. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10173. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10174. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10175. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10176. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10177. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10178. (defconst org-additional-option-like-keywords-for-flyspell
  10179. (delete-dups
  10180. (split-string
  10181. (mapconcat (lambda(k)
  10182. (replace-regexp-in-string
  10183. "_\\|:" " "
  10184. (concat k " " (downcase k) " " (upcase k))))
  10185. (append org-options-keywords org-additional-option-like-keywords)
  10186. " ")
  10187. " +" t)))
  10188. (defcustom org-structure-template-alist
  10189. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10190. "<src lang=\"?\">\n\n</src>")
  10191. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10192. "<example>\n?\n</example>")
  10193. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10194. "<quote>\n?\n</quote>")
  10195. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10196. "<verse>\n?\n</verse>")
  10197. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10198. "<verbatim>\n?\n</verbatim>")
  10199. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10200. "<center>\n?\n</center>")
  10201. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10202. "<literal style=\"latex\">\n?\n</literal>")
  10203. ("L" "#+LaTeX: "
  10204. "<literal style=\"latex\">?</literal>")
  10205. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10206. "<literal style=\"html\">\n?\n</literal>")
  10207. ("H" "#+HTML: "
  10208. "<literal style=\"html\">?</literal>")
  10209. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10210. ("A" "#+ASCII: ")
  10211. ("i" "#+INDEX: ?"
  10212. "#+INDEX: ?")
  10213. ("I" "#+INCLUDE: %file ?"
  10214. "<include file=%file markup=\"?\">"))
  10215. "Structure completion elements.
  10216. This is a list of abbreviation keys and values. The value gets inserted
  10217. if you type `<' followed by the key and then press the completion key,
  10218. usually `M-TAB'. %file will be replaced by a file name after prompting
  10219. for the file using completion. The cursor will be placed at the position
  10220. of the `?` in the template.
  10221. There are two templates for each key, the first uses the original Org syntax,
  10222. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10223. the default when the /org-mtags.el/ module has been loaded. See also the
  10224. variable `org-mtags-prefer-muse-templates'."
  10225. :group 'org-completion
  10226. :type '(repeat
  10227. (string :tag "Key")
  10228. (string :tag "Template")
  10229. (string :tag "Muse Template")))
  10230. (defun org-try-structure-completion ()
  10231. "Try to complete a structure template before point.
  10232. This looks for strings like \"<e\" on an otherwise empty line and
  10233. expands them."
  10234. (let ((l (buffer-substring (point-at-bol) (point)))
  10235. a)
  10236. (when (and (looking-at "[ \t]*$")
  10237. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10238. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10239. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10240. (match-beginning 1)) a)
  10241. t)))
  10242. (defun org-complete-expand-structure-template (start cell)
  10243. "Expand a structure template."
  10244. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10245. (rpl (nth (if musep 2 1) cell))
  10246. (ind ""))
  10247. (delete-region start (point))
  10248. (when (string-match "\\`#\\+" rpl)
  10249. (cond
  10250. ((bolp))
  10251. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10252. (setq ind (buffer-substring (point-at-bol) (point))))
  10253. (t (newline))))
  10254. (setq start (point))
  10255. (if (string-match "%file" rpl)
  10256. (setq rpl (replace-match
  10257. (concat
  10258. "\""
  10259. (save-match-data
  10260. (abbreviate-file-name (read-file-name "Include file: ")))
  10261. "\"")
  10262. t t rpl)))
  10263. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10264. (concat "\n" ind)))
  10265. (insert rpl)
  10266. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10267. ;;;; TODO, DEADLINE, Comments
  10268. (defun org-toggle-comment ()
  10269. "Change the COMMENT state of an entry."
  10270. (interactive)
  10271. (save-excursion
  10272. (org-back-to-heading)
  10273. (let (case-fold-search)
  10274. (cond
  10275. ((looking-at (format org-heading-keyword-regexp-format
  10276. org-comment-string))
  10277. (goto-char (match-end 1))
  10278. (looking-at (concat " +" org-comment-string))
  10279. (replace-match "" t t)
  10280. (when (eolp) (insert " ")))
  10281. ((looking-at org-outline-regexp)
  10282. (goto-char (match-end 0))
  10283. (insert org-comment-string " "))))))
  10284. (defvar org-last-todo-state-is-todo nil
  10285. "This is non-nil when the last TODO state change led to a TODO state.
  10286. If the last change removed the TODO tag or switched to DONE, then
  10287. this is nil.")
  10288. (defvar org-setting-tags nil) ; dynamically skipped
  10289. (defvar org-todo-setup-filter-hook nil
  10290. "Hook for functions that pre-filter todo specs.
  10291. Each function takes a todo spec and returns either nil or the spec
  10292. transformed into canonical form." )
  10293. (defvar org-todo-get-default-hook nil
  10294. "Hook for functions that get a default item for todo.
  10295. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10296. nil or a string to be used for the todo mark." )
  10297. (defvar org-agenda-headline-snapshot-before-repeat)
  10298. (defun org-current-effective-time ()
  10299. "Return current time adjusted for `org-extend-today-until' variable."
  10300. (let* ((ct (org-current-time))
  10301. (dct (decode-time ct))
  10302. (ct1
  10303. (if (and org-use-effective-time
  10304. (< (nth 2 dct) org-extend-today-until))
  10305. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10306. ct)))
  10307. ct1))
  10308. (defun org-todo-yesterday (&optional arg)
  10309. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10310. (interactive "P")
  10311. (if (eq major-mode 'org-agenda-mode)
  10312. (apply 'org-agenda-todo-yesterday arg)
  10313. (let* ((hour (third (decode-time
  10314. (org-current-time))))
  10315. (org-extend-today-until (1+ hour)))
  10316. (org-todo arg))))
  10317. (defun org-todo (&optional arg)
  10318. "Change the TODO state of an item.
  10319. The state of an item is given by a keyword at the start of the heading,
  10320. like
  10321. *** TODO Write paper
  10322. *** DONE Call mom
  10323. The different keywords are specified in the variable `org-todo-keywords'.
  10324. By default the available states are \"TODO\" and \"DONE\".
  10325. So for this example: when the item starts with TODO, it is changed to DONE.
  10326. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10327. DONE are present, add TODO at the beginning of the heading.
  10328. With \\[universal-argument] prefix arg, use completion to determine the new \
  10329. state.
  10330. With numeric prefix arg, switch to that state.
  10331. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10332. keywords (nextset).
  10333. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10334. With a numeric prefix arg of 0, inhibit note taking for the change.
  10335. For calling through lisp, arg is also interpreted in the following way:
  10336. 'none -> empty state
  10337. \"\"(empty string) -> switch to empty state
  10338. 'done -> switch to DONE
  10339. 'nextset -> switch to the next set of keywords
  10340. 'previousset -> switch to the previous set of keywords
  10341. \"WAITING\" -> switch to the specified keyword, but only if it
  10342. really is a member of `org-todo-keywords'."
  10343. (interactive "P")
  10344. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10345. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10346. 'region-start-level 'region))
  10347. org-loop-over-headlines-in-active-region)
  10348. (org-map-entries
  10349. `(org-todo ,arg)
  10350. org-loop-over-headlines-in-active-region
  10351. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10352. (if (equal arg '(16)) (setq arg 'nextset))
  10353. (let ((org-blocker-hook org-blocker-hook)
  10354. commentp
  10355. case-fold-search)
  10356. (when (equal arg '(64))
  10357. (setq arg nil org-blocker-hook nil))
  10358. (when (and org-blocker-hook
  10359. (or org-inhibit-blocking
  10360. (org-entry-get nil "NOBLOCKING")))
  10361. (setq org-blocker-hook nil))
  10362. (save-excursion
  10363. (catch 'exit
  10364. (org-back-to-heading t)
  10365. (when (looking-at (concat "^\\*+ " org-comment-string))
  10366. (org-toggle-comment)
  10367. (setq commentp t))
  10368. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10369. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10370. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10371. (let* ((match-data (match-data))
  10372. (startpos (point-at-bol))
  10373. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10374. (org-log-done org-log-done)
  10375. (org-log-repeat org-log-repeat)
  10376. (org-todo-log-states org-todo-log-states)
  10377. (org-inhibit-logging
  10378. (if (equal arg 0)
  10379. (progn (setq arg nil) 'note) org-inhibit-logging))
  10380. (this (match-string 1))
  10381. (hl-pos (match-beginning 0))
  10382. (head (org-get-todo-sequence-head this))
  10383. (ass (assoc head org-todo-kwd-alist))
  10384. (interpret (nth 1 ass))
  10385. (done-word (nth 3 ass))
  10386. (final-done-word (nth 4 ass))
  10387. (org-last-state (or this ""))
  10388. (completion-ignore-case t)
  10389. (member (member this org-todo-keywords-1))
  10390. (tail (cdr member))
  10391. (org-state (cond
  10392. ((and org-todo-key-trigger
  10393. (or (and (equal arg '(4))
  10394. (eq org-use-fast-todo-selection 'prefix))
  10395. (and (not arg) org-use-fast-todo-selection
  10396. (not (eq org-use-fast-todo-selection
  10397. 'prefix)))))
  10398. ;; Use fast selection
  10399. (org-fast-todo-selection))
  10400. ((and (equal arg '(4))
  10401. (or (not org-use-fast-todo-selection)
  10402. (not org-todo-key-trigger)))
  10403. ;; Read a state with completion
  10404. (org-icompleting-read
  10405. "State: " (mapcar (lambda(x) (list x))
  10406. org-todo-keywords-1)
  10407. nil t))
  10408. ((eq arg 'right)
  10409. (if this
  10410. (if tail (car tail) nil)
  10411. (car org-todo-keywords-1)))
  10412. ((eq arg 'left)
  10413. (if (equal member org-todo-keywords-1)
  10414. nil
  10415. (if this
  10416. (nth (- (length org-todo-keywords-1)
  10417. (length tail) 2)
  10418. org-todo-keywords-1)
  10419. (org-last org-todo-keywords-1))))
  10420. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10421. (setq arg nil))) ; hack to fall back to cycling
  10422. (arg
  10423. ;; user or caller requests a specific state
  10424. (cond
  10425. ((equal arg "") nil)
  10426. ((eq arg 'none) nil)
  10427. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10428. ((eq arg 'nextset)
  10429. (or (car (cdr (member head org-todo-heads)))
  10430. (car org-todo-heads)))
  10431. ((eq arg 'previousset)
  10432. (let ((org-todo-heads (reverse org-todo-heads)))
  10433. (or (car (cdr (member head org-todo-heads)))
  10434. (car org-todo-heads))))
  10435. ((car (member arg org-todo-keywords-1)))
  10436. ((stringp arg)
  10437. (error "State `%s' not valid in this file" arg))
  10438. ((nth (1- (prefix-numeric-value arg))
  10439. org-todo-keywords-1))))
  10440. ((null member) (or head (car org-todo-keywords-1)))
  10441. ((equal this final-done-word) nil) ;; -> make empty
  10442. ((null tail) nil) ;; -> first entry
  10443. ((memq interpret '(type priority))
  10444. (if (eq this-command last-command)
  10445. (car tail)
  10446. (if (> (length tail) 0)
  10447. (or done-word (car org-done-keywords))
  10448. nil)))
  10449. (t
  10450. (car tail))))
  10451. (org-state (or
  10452. (run-hook-with-args-until-success
  10453. 'org-todo-get-default-hook org-state org-last-state)
  10454. org-state))
  10455. (next (if org-state (concat " " org-state " ") " "))
  10456. (change-plist (list :type 'todo-state-change :from this :to org-state
  10457. :position startpos))
  10458. dolog now-done-p)
  10459. (when org-blocker-hook
  10460. (setq org-last-todo-state-is-todo
  10461. (not (member this org-done-keywords)))
  10462. (unless (save-excursion
  10463. (save-match-data
  10464. (org-with-wide-buffer
  10465. (run-hook-with-args-until-failure
  10466. 'org-blocker-hook change-plist))))
  10467. (if (org-called-interactively-p 'interactive)
  10468. (error "TODO state change from %s to %s blocked" this org-state)
  10469. ;; fail silently
  10470. (message "TODO state change from %s to %s blocked" this org-state)
  10471. (throw 'exit nil))))
  10472. (store-match-data match-data)
  10473. (replace-match next t t)
  10474. (unless (pos-visible-in-window-p hl-pos)
  10475. (message "TODO state changed to %s" (org-trim next)))
  10476. (unless head
  10477. (setq head (org-get-todo-sequence-head org-state)
  10478. ass (assoc head org-todo-kwd-alist)
  10479. interpret (nth 1 ass)
  10480. done-word (nth 3 ass)
  10481. final-done-word (nth 4 ass)))
  10482. (when (memq arg '(nextset previousset))
  10483. (message "Keyword-Set %d/%d: %s"
  10484. (- (length org-todo-sets) -1
  10485. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10486. (length org-todo-sets)
  10487. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10488. (setq org-last-todo-state-is-todo
  10489. (not (member org-state org-done-keywords)))
  10490. (setq now-done-p (and (member org-state org-done-keywords)
  10491. (not (member this org-done-keywords))))
  10492. (and logging (org-local-logging logging))
  10493. (when (and (or org-todo-log-states org-log-done)
  10494. (not (eq org-inhibit-logging t))
  10495. (not (memq arg '(nextset previousset))))
  10496. ;; we need to look at recording a time and note
  10497. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10498. (nth 2 (assoc this org-todo-log-states))))
  10499. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10500. (setq dolog 'time))
  10501. (when (and org-state
  10502. (member org-state org-not-done-keywords)
  10503. (not (member this org-not-done-keywords)))
  10504. ;; This is now a todo state and was not one before
  10505. ;; If there was a CLOSED time stamp, get rid of it.
  10506. (org-add-planning-info nil nil 'closed))
  10507. (when (and now-done-p org-log-done)
  10508. ;; It is now done, and it was not done before
  10509. (org-add-planning-info 'closed (org-current-effective-time))
  10510. (if (and (not dolog) (eq 'note org-log-done))
  10511. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10512. (when (and org-state dolog)
  10513. ;; This is a non-nil state, and we need to log it
  10514. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10515. ;; Fixup tag positioning
  10516. (org-todo-trigger-tag-changes org-state)
  10517. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10518. (when org-provide-todo-statistics
  10519. (org-update-parent-todo-statistics))
  10520. (run-hooks 'org-after-todo-state-change-hook)
  10521. (if (and arg (not (member org-state org-done-keywords)))
  10522. (setq head (org-get-todo-sequence-head org-state)))
  10523. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10524. ;; Do we need to trigger a repeat?
  10525. (when now-done-p
  10526. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10527. ;; This is for the agenda, take a snapshot of the headline.
  10528. (save-match-data
  10529. (setq org-agenda-headline-snapshot-before-repeat
  10530. (org-get-heading))))
  10531. (org-auto-repeat-maybe org-state))
  10532. ;; Fixup cursor location if close to the keyword
  10533. (if (and (outline-on-heading-p)
  10534. (not (bolp))
  10535. (save-excursion (beginning-of-line 1)
  10536. (looking-at org-todo-line-regexp))
  10537. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10538. (progn
  10539. (goto-char (or (match-end 2) (match-end 1)))
  10540. (and (looking-at " ") (just-one-space))))
  10541. (when org-trigger-hook
  10542. (save-excursion
  10543. (run-hook-with-args 'org-trigger-hook change-plist)))
  10544. (when commentp (org-toggle-comment))))))))
  10545. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10546. "Block turning an entry into a TODO, using the hierarchy.
  10547. This checks whether the current task should be blocked from state
  10548. changes. Such blocking occurs when:
  10549. 1. The task has children which are not all in a completed state.
  10550. 2. A task has a parent with the property :ORDERED:, and there
  10551. are siblings prior to the current task with incomplete
  10552. status.
  10553. 3. The parent of the task is blocked because it has siblings that should
  10554. be done first, or is child of a block grandparent TODO entry."
  10555. (if (not org-enforce-todo-dependencies)
  10556. t ; if locally turned off don't block
  10557. (catch 'dont-block
  10558. ;; If this is not a todo state change, or if this entry is already DONE,
  10559. ;; do not block
  10560. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10561. (member (plist-get change-plist :from)
  10562. (cons 'done org-done-keywords))
  10563. (member (plist-get change-plist :to)
  10564. (cons 'todo org-not-done-keywords))
  10565. (not (plist-get change-plist :to)))
  10566. (throw 'dont-block t))
  10567. ;; If this task has children, and any are undone, it's blocked
  10568. (save-excursion
  10569. (org-back-to-heading t)
  10570. (let ((this-level (funcall outline-level)))
  10571. (outline-next-heading)
  10572. (let ((child-level (funcall outline-level)))
  10573. (while (and (not (eobp))
  10574. (> child-level this-level))
  10575. ;; this todo has children, check whether they are all
  10576. ;; completed
  10577. (if (and (not (org-entry-is-done-p))
  10578. (org-entry-is-todo-p))
  10579. (throw 'dont-block nil))
  10580. (outline-next-heading)
  10581. (setq child-level (funcall outline-level))))))
  10582. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10583. ;; any previous siblings are undone, it's blocked
  10584. (save-excursion
  10585. (org-back-to-heading t)
  10586. (let* ((pos (point))
  10587. (parent-pos (and (org-up-heading-safe) (point))))
  10588. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10589. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10590. (forward-line 1)
  10591. (re-search-forward org-not-done-heading-regexp pos t))
  10592. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10593. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10594. (while t
  10595. (goto-char parent-pos)
  10596. (if (not (looking-at org-not-done-heading-regexp))
  10597. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10598. (setq pos (point))
  10599. (setq parent-pos (and (org-up-heading-safe) (point)))
  10600. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10601. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10602. (forward-line 1)
  10603. (re-search-forward org-not-done-heading-regexp pos t))
  10604. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10605. (defcustom org-track-ordered-property-with-tag nil
  10606. "Should the ORDERED property also be shown as a tag?
  10607. The ORDERED property decides if an entry should require subtasks to be
  10608. completed in sequence. Since a property is not very visible, setting
  10609. this option means that toggling the ORDERED property with the command
  10610. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10611. not relevant for the behavior, but it makes things more visible.
  10612. Note that toggling the tag with tags commands will not change the property
  10613. and therefore not influence behavior!
  10614. This can be t, meaning the tag ORDERED should be used, It can also be a
  10615. string to select a different tag for this task."
  10616. :group 'org-todo
  10617. :type '(choice
  10618. (const :tag "No tracking" nil)
  10619. (const :tag "Track with ORDERED tag" t)
  10620. (string :tag "Use other tag")))
  10621. (defun org-toggle-ordered-property ()
  10622. "Toggle the ORDERED property of the current entry.
  10623. For better visibility, you can track the value of this property with a tag.
  10624. See variable `org-track-ordered-property-with-tag'."
  10625. (interactive)
  10626. (let* ((t1 org-track-ordered-property-with-tag)
  10627. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10628. (save-excursion
  10629. (org-back-to-heading)
  10630. (if (org-entry-get nil "ORDERED")
  10631. (progn
  10632. (org-delete-property "ORDERED")
  10633. (and tag (org-toggle-tag tag 'off))
  10634. (message "Subtasks can be completed in arbitrary order"))
  10635. (org-entry-put nil "ORDERED" "t")
  10636. (and tag (org-toggle-tag tag 'on))
  10637. (message "Subtasks must be completed in sequence")))))
  10638. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10639. (defun org-block-todo-from-checkboxes (change-plist)
  10640. "Block turning an entry into a TODO, using checkboxes.
  10641. This checks whether the current task should be blocked from state
  10642. changes because there are unchecked boxes in this entry."
  10643. (if (not org-enforce-todo-checkbox-dependencies)
  10644. t ; if locally turned off don't block
  10645. (catch 'dont-block
  10646. ;; If this is not a todo state change, or if this entry is already DONE,
  10647. ;; do not block
  10648. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10649. (member (plist-get change-plist :from)
  10650. (cons 'done org-done-keywords))
  10651. (member (plist-get change-plist :to)
  10652. (cons 'todo org-not-done-keywords))
  10653. (not (plist-get change-plist :to)))
  10654. (throw 'dont-block t))
  10655. ;; If this task has checkboxes that are not checked, it's blocked
  10656. (save-excursion
  10657. (org-back-to-heading t)
  10658. (let ((beg (point)) end)
  10659. (outline-next-heading)
  10660. (setq end (point))
  10661. (goto-char beg)
  10662. (if (org-list-search-forward
  10663. (concat (org-item-beginning-re)
  10664. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10665. "\\[[- ]\\]")
  10666. end t)
  10667. (progn
  10668. (if (boundp 'org-blocked-by-checkboxes)
  10669. (setq org-blocked-by-checkboxes t))
  10670. (throw 'dont-block nil)))))
  10671. t))) ; do not block
  10672. (defun org-entry-blocked-p ()
  10673. "Is the current entry blocked?"
  10674. (org-with-buffer-modified-unmodified
  10675. (if (org-entry-get nil "NOBLOCKING")
  10676. nil ;; Never block this entry
  10677. (not
  10678. (run-hook-with-args-until-failure
  10679. 'org-blocker-hook
  10680. (list :type 'todo-state-change
  10681. :position (point)
  10682. :from 'todo
  10683. :to 'done))))))
  10684. (defun org-update-statistics-cookies (all)
  10685. "Update the statistics cookie, either from TODO or from checkboxes.
  10686. This should be called with the cursor in a line with a statistics cookie."
  10687. (interactive "P")
  10688. (if all
  10689. (progn
  10690. (org-update-checkbox-count 'all)
  10691. (org-map-entries 'org-update-parent-todo-statistics))
  10692. (if (not (org-at-heading-p))
  10693. (org-update-checkbox-count)
  10694. (let ((pos (point-marker))
  10695. end l1 l2)
  10696. (ignore-errors (org-back-to-heading t))
  10697. (if (not (org-at-heading-p))
  10698. (org-update-checkbox-count)
  10699. (setq l1 (org-outline-level))
  10700. (setq end (save-excursion
  10701. (outline-next-heading)
  10702. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10703. (point)))
  10704. (if (and (save-excursion
  10705. (re-search-forward
  10706. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10707. (not (save-excursion (re-search-forward
  10708. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10709. (org-update-checkbox-count)
  10710. (if (and l2 (> l2 l1))
  10711. (progn
  10712. (goto-char end)
  10713. (org-update-parent-todo-statistics))
  10714. (goto-char pos)
  10715. (beginning-of-line 1)
  10716. (while (re-search-forward
  10717. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10718. (point-at-eol) t)
  10719. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10720. (goto-char pos)
  10721. (move-marker pos nil)))))
  10722. (defvar org-entry-property-inherited-from) ;; defined below
  10723. (defun org-update-parent-todo-statistics ()
  10724. "Update any statistics cookie in the parent of the current headline.
  10725. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10726. the entire subtree and this will travel up the hierarchy and update
  10727. statistics everywhere."
  10728. (let* ((prop (save-excursion (org-up-heading-safe)
  10729. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10730. (recursive (or (not org-hierarchical-todo-statistics)
  10731. (and prop (string-match "\\<recursive\\>" prop))))
  10732. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10733. 0))
  10734. (first t)
  10735. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10736. level ltoggle l1 new ndel
  10737. (cnt-all 0) (cnt-done 0) is-percent kwd
  10738. checkbox-beg ov ovs ove cookie-present)
  10739. (catch 'exit
  10740. (save-excursion
  10741. (beginning-of-line 1)
  10742. (setq ltoggle (funcall outline-level))
  10743. ;; Three situations are to consider:
  10744. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10745. ;; to the top-level ancestor on the headline;
  10746. ;; 2. If parent has "recursive" property, repeat up to the
  10747. ;; headline setting that property, taking inheritance into
  10748. ;; account;
  10749. ;; 3. Else, move up to direct parent and proceed only once.
  10750. (while (and (setq level (org-up-heading-safe))
  10751. (or recursive first)
  10752. (>= (point) lim))
  10753. (setq first nil cookie-present nil)
  10754. (unless (and level
  10755. (not (string-match
  10756. "\\<checkbox\\>"
  10757. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10758. "")))))
  10759. (throw 'exit nil))
  10760. (while (re-search-forward box-re (point-at-eol) t)
  10761. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10762. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10763. (save-match-data
  10764. (unless (outline-next-heading) (throw 'exit nil))
  10765. (while (and (looking-at org-complex-heading-regexp)
  10766. (> (setq l1 (length (match-string 1))) level))
  10767. (setq kwd (and (or recursive (= l1 ltoggle))
  10768. (match-string 2)))
  10769. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10770. (and (listp org-provide-todo-statistics)
  10771. (or (member kwd org-provide-todo-statistics)
  10772. (member kwd org-done-keywords))))
  10773. (setq cnt-all (1+ cnt-all))
  10774. (if (eq org-provide-todo-statistics t)
  10775. (and kwd (setq cnt-all (1+ cnt-all)))))
  10776. (and (member kwd org-done-keywords)
  10777. (setq cnt-done (1+ cnt-done)))
  10778. (outline-next-heading)))
  10779. (setq new
  10780. (if is-percent
  10781. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10782. (format "[%d/%d]" cnt-done cnt-all))
  10783. ndel (- (match-end 0) checkbox-beg))
  10784. ;; handle overlays when updating cookie from column view
  10785. (when (setq ov (car (overlays-at checkbox-beg)))
  10786. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10787. (delete-overlay ov))
  10788. (goto-char checkbox-beg)
  10789. (insert new)
  10790. (delete-region (point) (+ (point) ndel))
  10791. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10792. (when ov (move-overlay ov ovs ove)))
  10793. (when cookie-present
  10794. (run-hook-with-args 'org-after-todo-statistics-hook
  10795. cnt-done (- cnt-all cnt-done))))))
  10796. (run-hooks 'org-todo-statistics-hook)))
  10797. (defvar org-after-todo-statistics-hook nil
  10798. "Hook that is called after a TODO statistics cookie has been updated.
  10799. Each function is called with two arguments: the number of not-done entries
  10800. and the number of done entries.
  10801. For example, the following function, when added to this hook, will switch
  10802. an entry to DONE when all children are done, and back to TODO when new
  10803. entries are set to a TODO status. Note that this hook is only called
  10804. when there is a statistics cookie in the headline!
  10805. (defun org-summary-todo (n-done n-not-done)
  10806. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10807. (let (org-log-done org-log-states) ; turn off logging
  10808. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10809. ")
  10810. (defvar org-todo-statistics-hook nil
  10811. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10812. This hook runs even if there is no statistics cookie present, in which case
  10813. `org-after-todo-statistics-hook' would not run.")
  10814. (defun org-todo-trigger-tag-changes (state)
  10815. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10816. (let ((l org-todo-state-tags-triggers)
  10817. changes)
  10818. (when (or (not state) (equal state ""))
  10819. (setq changes (append changes (cdr (assoc "" l)))))
  10820. (when (and (stringp state) (> (length state) 0))
  10821. (setq changes (append changes (cdr (assoc state l)))))
  10822. (when (member state org-not-done-keywords)
  10823. (setq changes (append changes (cdr (assoc 'todo l)))))
  10824. (when (member state org-done-keywords)
  10825. (setq changes (append changes (cdr (assoc 'done l)))))
  10826. (dolist (c changes)
  10827. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10828. (defun org-local-logging (value)
  10829. "Get logging settings from a property VALUE."
  10830. (let* (words w a)
  10831. ;; directly set the variables, they are already local.
  10832. (setq org-log-done nil
  10833. org-log-repeat nil
  10834. org-todo-log-states nil)
  10835. (setq words (org-split-string value))
  10836. (while (setq w (pop words))
  10837. (cond
  10838. ((setq a (assoc w org-startup-options))
  10839. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10840. (set (nth 1 a) (nth 2 a))))
  10841. ((setq a (org-extract-log-state-settings w))
  10842. (and (member (car a) org-todo-keywords-1)
  10843. (push a org-todo-log-states)))))))
  10844. (defun org-get-todo-sequence-head (kwd)
  10845. "Return the head of the TODO sequence to which KWD belongs.
  10846. If KWD is not set, check if there is a text property remembering the
  10847. right sequence."
  10848. (let (p)
  10849. (cond
  10850. ((not kwd)
  10851. (or (get-text-property (point-at-bol) 'org-todo-head)
  10852. (progn
  10853. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10854. nil (point-at-eol)))
  10855. (get-text-property p 'org-todo-head))))
  10856. ((not (member kwd org-todo-keywords-1))
  10857. (car org-todo-keywords-1))
  10858. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10859. (defun org-fast-todo-selection ()
  10860. "Fast TODO keyword selection with single keys.
  10861. Returns the new TODO keyword, or nil if no state change should occur."
  10862. (let* ((fulltable org-todo-key-alist)
  10863. (done-keywords org-done-keywords) ;; needed for the faces.
  10864. (maxlen (apply 'max (mapcar
  10865. (lambda (x)
  10866. (if (stringp (car x)) (string-width (car x)) 0))
  10867. fulltable)))
  10868. (expert nil)
  10869. (fwidth (+ maxlen 3 1 3))
  10870. (ncol (/ (- (window-width) 4) fwidth))
  10871. tg cnt e c tbl
  10872. groups ingroup)
  10873. (save-excursion
  10874. (save-window-excursion
  10875. (if expert
  10876. (set-buffer (get-buffer-create " *Org todo*"))
  10877. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10878. (erase-buffer)
  10879. (org-set-local 'org-done-keywords done-keywords)
  10880. (setq tbl fulltable cnt 0)
  10881. (while (setq e (pop tbl))
  10882. (cond
  10883. ((equal e '(:startgroup))
  10884. (push '() groups) (setq ingroup t)
  10885. (when (not (= cnt 0))
  10886. (setq cnt 0)
  10887. (insert "\n"))
  10888. (insert "{ "))
  10889. ((equal e '(:endgroup))
  10890. (setq ingroup nil cnt 0)
  10891. (insert "}\n"))
  10892. ((equal e '(:newline))
  10893. (when (not (= cnt 0))
  10894. (setq cnt 0)
  10895. (insert "\n")
  10896. (setq e (car tbl))
  10897. (while (equal (car tbl) '(:newline))
  10898. (insert "\n")
  10899. (setq tbl (cdr tbl)))))
  10900. (t
  10901. (setq tg (car e) c (cdr e))
  10902. (if ingroup (push tg (car groups)))
  10903. (setq tg (org-add-props tg nil 'face
  10904. (org-get-todo-face tg)))
  10905. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10906. (insert "[" c "] " tg (make-string
  10907. (- fwidth 4 (length tg)) ?\ ))
  10908. (when (= (setq cnt (1+ cnt)) ncol)
  10909. (insert "\n")
  10910. (if ingroup (insert " "))
  10911. (setq cnt 0)))))
  10912. (insert "\n")
  10913. (goto-char (point-min))
  10914. (if (not expert) (org-fit-window-to-buffer))
  10915. (message "[a-z..]:Set [SPC]:clear")
  10916. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10917. (cond
  10918. ((or (= c ?\C-g)
  10919. (and (= c ?q) (not (rassoc c fulltable))))
  10920. (setq quit-flag t))
  10921. ((= c ?\ ) nil)
  10922. ((setq e (rassoc c fulltable) tg (car e))
  10923. tg)
  10924. (t (setq quit-flag t)))))))
  10925. (defun org-entry-is-todo-p ()
  10926. (member (org-get-todo-state) org-not-done-keywords))
  10927. (defun org-entry-is-done-p ()
  10928. (member (org-get-todo-state) org-done-keywords))
  10929. (defun org-get-todo-state ()
  10930. (save-excursion
  10931. (org-back-to-heading t)
  10932. (and (looking-at org-todo-line-regexp)
  10933. (match-end 2)
  10934. (match-string 2))))
  10935. (defun org-at-date-range-p (&optional inactive-ok)
  10936. "Is the cursor inside a date range?"
  10937. (interactive)
  10938. (save-excursion
  10939. (catch 'exit
  10940. (let ((pos (point)))
  10941. (skip-chars-backward "^[<\r\n")
  10942. (skip-chars-backward "<[")
  10943. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10944. (>= (match-end 0) pos)
  10945. (throw 'exit t))
  10946. (skip-chars-backward "^<[\r\n")
  10947. (skip-chars-backward "<[")
  10948. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10949. (>= (match-end 0) pos)
  10950. (throw 'exit t)))
  10951. nil)))
  10952. (defun org-get-repeat (&optional tagline)
  10953. "Check if there is a deadline/schedule with repeater in this entry."
  10954. (save-match-data
  10955. (save-excursion
  10956. (org-back-to-heading t)
  10957. (and (re-search-forward (if tagline
  10958. (concat tagline "\\s-*" org-repeat-re)
  10959. org-repeat-re)
  10960. (org-entry-end-position) t)
  10961. (match-string-no-properties 1)))))
  10962. (defvar org-last-changed-timestamp)
  10963. (defvar org-last-inserted-timestamp)
  10964. (defvar org-log-post-message)
  10965. (defvar org-log-note-purpose)
  10966. (defvar org-log-note-how)
  10967. (defvar org-log-note-extra)
  10968. (defun org-auto-repeat-maybe (done-word)
  10969. "Check if the current headline contains a repeated deadline/schedule.
  10970. If yes, set TODO state back to what it was and change the base date
  10971. of repeating deadline/scheduled time stamps to new date.
  10972. This function is run automatically after each state change to a DONE state."
  10973. ;; last-state is dynamically scoped into this function
  10974. (let* ((repeat (org-get-repeat))
  10975. (aa (assoc org-last-state org-todo-kwd-alist))
  10976. (interpret (nth 1 aa))
  10977. (head (nth 2 aa))
  10978. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  10979. (msg "Entry repeats: ")
  10980. (org-log-done nil)
  10981. (org-todo-log-states nil)
  10982. re type n what ts time to-state)
  10983. (when repeat
  10984. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10985. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10986. org-todo-repeat-to-state))
  10987. (unless (and to-state (member to-state org-todo-keywords-1))
  10988. (setq to-state (if (eq interpret 'type) org-last-state head)))
  10989. (org-todo to-state)
  10990. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10991. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10992. (org-time-stamp-format t t))))
  10993. (when org-log-repeat
  10994. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10995. (memq 'org-add-log-note post-command-hook))
  10996. ;; OK, we are already setup for some record
  10997. (if (eq org-log-repeat 'note)
  10998. ;; make sure we take a note, not only a time stamp
  10999. (setq org-log-note-how 'note))
  11000. ;; Set up for taking a record
  11001. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11002. org-last-state
  11003. 'findpos org-log-repeat)))
  11004. (org-back-to-heading t)
  11005. (org-add-planning-info nil nil 'closed)
  11006. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11007. org-deadline-time-regexp "\\)\\|\\("
  11008. org-ts-regexp "\\)"))
  11009. (while (re-search-forward
  11010. re (save-excursion (outline-next-heading) (point)) t)
  11011. (setq type (if (match-end 1) org-scheduled-string
  11012. (if (match-end 3) org-deadline-string "Plain:"))
  11013. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11014. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11015. (setq n (string-to-number (match-string 2 ts))
  11016. what (match-string 3 ts))
  11017. (if (equal what "w") (setq n (* n 7) what "d"))
  11018. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11019. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11020. ;; Preparation, see if we need to modify the start date for the change
  11021. (when (match-end 1)
  11022. (setq time (save-match-data (org-time-string-to-time ts)))
  11023. (cond
  11024. ((equal (match-string 1 ts) ".")
  11025. ;; Shift starting date to today
  11026. (org-timestamp-change
  11027. (- (org-today) (time-to-days time))
  11028. 'day))
  11029. ((equal (match-string 1 ts) "+")
  11030. (let ((nshiftmax 10) (nshift 0))
  11031. (while (or (= nshift 0)
  11032. (<= (time-to-days time)
  11033. (time-to-days (current-time))))
  11034. (when (= (incf nshift) nshiftmax)
  11035. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11036. (error "Abort")))
  11037. (org-timestamp-change n (cdr (assoc what whata)))
  11038. (org-at-timestamp-p t)
  11039. (setq ts (match-string 1))
  11040. (setq time (save-match-data (org-time-string-to-time ts)))))
  11041. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11042. ;; rematch, so that we have everything in place for the real shift
  11043. (org-at-timestamp-p t)
  11044. (setq ts (match-string 1))
  11045. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11046. (org-timestamp-change n (cdr (assoc what whata)))
  11047. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11048. (setq org-log-post-message msg)
  11049. (message "%s" msg))))
  11050. (defun org-show-todo-tree (arg)
  11051. "Make a compact tree which shows all headlines marked with TODO.
  11052. The tree will show the lines where the regexp matches, and all higher
  11053. headlines above the match.
  11054. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11055. With a numeric prefix N, construct a sparse tree for the Nth element
  11056. of `org-todo-keywords-1'."
  11057. (interactive "P")
  11058. (let ((case-fold-search nil)
  11059. (kwd-re
  11060. (cond ((null arg) org-not-done-regexp)
  11061. ((equal arg '(4))
  11062. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11063. (mapcar 'list org-todo-keywords-1))))
  11064. (concat "\\("
  11065. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11066. "\\)\\>")))
  11067. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11068. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11069. org-todo-keywords-1)))
  11070. (t (error "Invalid prefix argument: %s" arg)))))
  11071. (message "%d TODO entries found"
  11072. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11073. (defun org-deadline (&optional remove time)
  11074. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11075. With argument REMOVE, remove any deadline from the item.
  11076. With argument TIME, set the deadline at the corresponding date. TIME
  11077. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11078. (interactive "P")
  11079. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11080. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11081. 'region-start-level 'region))
  11082. org-loop-over-headlines-in-active-region)
  11083. (org-map-entries
  11084. `(org-deadline ',remove ,time)
  11085. org-loop-over-headlines-in-active-region
  11086. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11087. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11088. (repeater (and old-date
  11089. (string-match
  11090. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11091. old-date)
  11092. (match-string 1 old-date))))
  11093. (if remove
  11094. (progn
  11095. (when (and old-date org-log-redeadline)
  11096. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11097. org-log-redeadline))
  11098. (org-remove-timestamp-with-keyword org-deadline-string)
  11099. (message "Item no longer has a deadline."))
  11100. (org-add-planning-info 'deadline time 'closed)
  11101. (when (and old-date org-log-redeadline
  11102. (not (equal old-date
  11103. (substring org-last-inserted-timestamp 1 -1))))
  11104. (org-add-log-setup 'redeadline nil old-date 'findpos
  11105. org-log-redeadline))
  11106. (when repeater
  11107. (save-excursion
  11108. (org-back-to-heading t)
  11109. (when (re-search-forward (concat org-deadline-string " "
  11110. org-last-inserted-timestamp)
  11111. (save-excursion
  11112. (outline-next-heading) (point)) t)
  11113. (goto-char (1- (match-end 0)))
  11114. (insert " " repeater)
  11115. (setq org-last-inserted-timestamp
  11116. (concat (substring org-last-inserted-timestamp 0 -1)
  11117. " " repeater
  11118. (substring org-last-inserted-timestamp -1))))))
  11119. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11120. (defun org-schedule (&optional remove time)
  11121. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11122. With argument REMOVE, remove any scheduling date from the item.
  11123. With argument TIME, scheduled at the corresponding date. TIME can
  11124. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11125. (interactive "P")
  11126. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11127. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11128. 'region-start-level 'region))
  11129. org-loop-over-headlines-in-active-region)
  11130. (org-map-entries
  11131. `(org-schedule ',remove ,time)
  11132. org-loop-over-headlines-in-active-region
  11133. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11134. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11135. (repeater (and old-date
  11136. (string-match
  11137. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11138. old-date)
  11139. (match-string 1 old-date))))
  11140. (if remove
  11141. (progn
  11142. (when (and old-date org-log-reschedule)
  11143. (org-add-log-setup 'delschedule nil old-date 'findpos
  11144. org-log-reschedule))
  11145. (org-remove-timestamp-with-keyword org-scheduled-string)
  11146. (message "Item is no longer scheduled."))
  11147. (org-add-planning-info 'scheduled time 'closed)
  11148. (when (and old-date org-log-reschedule
  11149. (not (equal old-date
  11150. (substring org-last-inserted-timestamp 1 -1))))
  11151. (org-add-log-setup 'reschedule nil old-date 'findpos
  11152. org-log-reschedule))
  11153. (when repeater
  11154. (save-excursion
  11155. (org-back-to-heading t)
  11156. (when (re-search-forward (concat org-scheduled-string " "
  11157. org-last-inserted-timestamp)
  11158. (save-excursion
  11159. (outline-next-heading) (point)) t)
  11160. (goto-char (1- (match-end 0)))
  11161. (insert " " repeater)
  11162. (setq org-last-inserted-timestamp
  11163. (concat (substring org-last-inserted-timestamp 0 -1)
  11164. " " repeater
  11165. (substring org-last-inserted-timestamp -1))))))
  11166. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11167. (defun org-get-scheduled-time (pom &optional inherit)
  11168. "Get the scheduled time as a time tuple, of a format suitable
  11169. for calling org-schedule with, or if there is no scheduling,
  11170. returns nil."
  11171. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11172. (when time
  11173. (apply 'encode-time (org-parse-time-string time)))))
  11174. (defun org-get-deadline-time (pom &optional inherit)
  11175. "Get the deadline as a time tuple, of a format suitable for
  11176. calling org-deadline with, or if there is no scheduling, returns
  11177. nil."
  11178. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11179. (when time
  11180. (apply 'encode-time (org-parse-time-string time)))))
  11181. (defun org-remove-timestamp-with-keyword (keyword)
  11182. "Remove all time stamps with KEYWORD in the current entry."
  11183. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11184. beg)
  11185. (save-excursion
  11186. (org-back-to-heading t)
  11187. (setq beg (point))
  11188. (outline-next-heading)
  11189. (while (re-search-backward re beg t)
  11190. (replace-match "")
  11191. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11192. (equal (char-before) ?\ ))
  11193. (backward-delete-char 1)
  11194. (if (string-match "^[ \t]*$" (buffer-substring
  11195. (point-at-bol) (point-at-eol)))
  11196. (delete-region (point-at-bol)
  11197. (min (point-max) (1+ (point-at-eol))))))))))
  11198. (defun org-add-planning-info (what &optional time &rest remove)
  11199. "Insert new timestamp with keyword in the line directly after the headline.
  11200. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11201. If non is given, the user is prompted for a date.
  11202. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11203. be removed."
  11204. (interactive)
  11205. (let (org-time-was-given org-end-time-was-given ts
  11206. end default-time default-input)
  11207. (catch 'exit
  11208. (when (and (memq what '(scheduled deadline))
  11209. (or (not time)
  11210. (and (stringp time)
  11211. (string-match "^[-+]+[0-9]" time))))
  11212. ;; Try to get a default date/time from existing timestamp
  11213. (save-excursion
  11214. (org-back-to-heading t)
  11215. (setq end (save-excursion (outline-next-heading) (point)))
  11216. (when (re-search-forward (if (eq what 'scheduled)
  11217. org-scheduled-time-regexp
  11218. org-deadline-time-regexp)
  11219. end t)
  11220. (setq ts (match-string 1)
  11221. default-time
  11222. (apply 'encode-time (org-parse-time-string ts))
  11223. default-input (and ts (org-get-compact-tod ts))))))
  11224. (when what
  11225. (setq time
  11226. (if (stringp time)
  11227. ;; This is a string (relative or absolute), set proper date
  11228. (apply 'encode-time
  11229. (org-read-date-analyze
  11230. time default-time (decode-time default-time)))
  11231. ;; If necessary, get the time from the user
  11232. (or time (org-read-date nil 'to-time nil nil
  11233. default-time default-input)))))
  11234. (when (and org-insert-labeled-timestamps-at-point
  11235. (member what '(scheduled deadline)))
  11236. (insert
  11237. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11238. (org-insert-time-stamp time org-time-was-given
  11239. nil nil nil (list org-end-time-was-given))
  11240. (setq what nil))
  11241. (save-excursion
  11242. (save-restriction
  11243. (let (col list elt ts buffer-invisibility-spec)
  11244. (org-back-to-heading t)
  11245. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11246. (goto-char (match-end 1))
  11247. (setq col (current-column))
  11248. (goto-char (match-end 0))
  11249. (if (eobp) (insert "\n") (forward-char 1))
  11250. (when (and (not what)
  11251. (not (looking-at
  11252. (concat "[ \t]*"
  11253. org-keyword-time-not-clock-regexp))))
  11254. ;; Nothing to add, nothing to remove...... :-)
  11255. (throw 'exit nil))
  11256. (if (and (not (looking-at org-outline-regexp))
  11257. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11258. "[^\r\n]*"))
  11259. (not (equal (match-string 1) org-clock-string)))
  11260. (narrow-to-region (match-beginning 0) (match-end 0))
  11261. (insert-before-markers "\n")
  11262. (backward-char 1)
  11263. (narrow-to-region (point) (point))
  11264. (and org-adapt-indentation (org-indent-to-column col)))
  11265. ;; Check if we have to remove something.
  11266. (setq list (cons what remove))
  11267. (while list
  11268. (setq elt (pop list))
  11269. (when (or (and (eq elt 'scheduled)
  11270. (re-search-forward org-scheduled-time-regexp nil t))
  11271. (and (eq elt 'deadline)
  11272. (re-search-forward org-deadline-time-regexp nil t))
  11273. (and (eq elt 'closed)
  11274. (re-search-forward org-closed-time-regexp nil t)))
  11275. (replace-match "")
  11276. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11277. (and (looking-at "[ \t]+") (replace-match ""))
  11278. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11279. (when what
  11280. (insert
  11281. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11282. (cond ((eq what 'scheduled) org-scheduled-string)
  11283. ((eq what 'deadline) org-deadline-string)
  11284. ((eq what 'closed) org-closed-string))
  11285. " ")
  11286. (setq ts (org-insert-time-stamp
  11287. time
  11288. (or org-time-was-given
  11289. (and (eq what 'closed) org-log-done-with-time))
  11290. (eq what 'closed)
  11291. nil nil (list org-end-time-was-given)))
  11292. (insert
  11293. (if (not (or (bolp) (eq (char-before) ?\ )
  11294. (memq (char-after) '(32 10))
  11295. (eobp))) " " ""))
  11296. (end-of-line 1))
  11297. (goto-char (point-min))
  11298. (widen)
  11299. (if (and (looking-at "[ \t]*\n")
  11300. (equal (char-before) ?\n))
  11301. (delete-region (1- (point)) (point-at-eol)))
  11302. ts))))))
  11303. (defvar org-log-note-marker (make-marker))
  11304. (defvar org-log-note-purpose nil)
  11305. (defvar org-log-note-state nil)
  11306. (defvar org-log-note-previous-state nil)
  11307. (defvar org-log-note-how nil)
  11308. (defvar org-log-note-extra nil)
  11309. (defvar org-log-note-window-configuration nil)
  11310. (defvar org-log-note-return-to (make-marker))
  11311. (defvar org-log-note-effective-time nil
  11312. "Remembered current time so that dynamically scoped
  11313. `org-extend-today-until' affects tha timestamps in state change
  11314. log")
  11315. (defvar org-log-post-message nil
  11316. "Message to be displayed after a log note has been stored.
  11317. The auto-repeater uses this.")
  11318. (defun org-add-note ()
  11319. "Add a note to the current entry.
  11320. This is done in the same way as adding a state change note."
  11321. (interactive)
  11322. (org-add-log-setup 'note nil nil 'findpos nil))
  11323. (defvar org-property-end-re)
  11324. (defun org-add-log-setup (&optional purpose state prev-state
  11325. findpos how extra)
  11326. "Set up the post command hook to take a note.
  11327. If this is about to TODO state change, the new state is expected in STATE.
  11328. When FINDPOS is non-nil, find the correct position for the note in
  11329. the current entry. If not, assume that it can be inserted at point.
  11330. HOW is an indicator what kind of note should be created.
  11331. EXTRA is additional text that will be inserted into the notes buffer."
  11332. (let* ((org-log-into-drawer (org-log-into-drawer))
  11333. (drawer (cond ((stringp org-log-into-drawer)
  11334. org-log-into-drawer)
  11335. (org-log-into-drawer "LOGBOOK"))))
  11336. (save-restriction
  11337. (save-excursion
  11338. (when findpos
  11339. (org-back-to-heading t)
  11340. (narrow-to-region (point) (save-excursion
  11341. (outline-next-heading) (point)))
  11342. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11343. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11344. "[^\r\n]*\\)?"))
  11345. (goto-char (match-end 0))
  11346. (cond
  11347. (drawer
  11348. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11349. nil t)
  11350. (progn
  11351. (goto-char (match-end 0))
  11352. (or org-log-states-order-reversed
  11353. (and (re-search-forward org-property-end-re nil t)
  11354. (goto-char (1- (match-beginning 0))))))
  11355. (insert "\n:" drawer ":\n:END:")
  11356. (beginning-of-line 0)
  11357. (org-indent-line)
  11358. (beginning-of-line 2)
  11359. (org-indent-line)
  11360. (end-of-line 0)))
  11361. ((and org-log-state-notes-insert-after-drawers
  11362. (save-excursion
  11363. (forward-line) (looking-at org-drawer-regexp)))
  11364. (forward-line)
  11365. (while (looking-at org-drawer-regexp)
  11366. (goto-char (match-end 0))
  11367. (re-search-forward org-property-end-re (point-max) t)
  11368. (forward-line))
  11369. (forward-line -1)))
  11370. (unless org-log-states-order-reversed
  11371. (and (= (char-after) ?\n) (forward-char 1))
  11372. (org-skip-over-state-notes)
  11373. (skip-chars-backward " \t\n\r")))
  11374. (move-marker org-log-note-marker (point))
  11375. (setq org-log-note-purpose purpose
  11376. org-log-note-state state
  11377. org-log-note-previous-state prev-state
  11378. org-log-note-how how
  11379. org-log-note-extra extra
  11380. org-log-note-effective-time (org-current-effective-time))
  11381. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11382. (defun org-skip-over-state-notes ()
  11383. "Skip past the list of State notes in an entry."
  11384. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11385. (when (ignore-errors (goto-char (org-in-item-p)))
  11386. (let* ((struct (org-list-struct))
  11387. (prevs (org-list-prevs-alist struct)))
  11388. (while (looking-at "[ \t]*- State")
  11389. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11390. (org-list-get-item-end (point) struct)))))))
  11391. (defun org-add-log-note (&optional purpose)
  11392. "Pop up a window for taking a note, and add this note later at point."
  11393. (remove-hook 'post-command-hook 'org-add-log-note)
  11394. (setq org-log-note-window-configuration (current-window-configuration))
  11395. (delete-other-windows)
  11396. (move-marker org-log-note-return-to (point))
  11397. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11398. (goto-char org-log-note-marker)
  11399. (org-switch-to-buffer-other-window "*Org Note*")
  11400. (erase-buffer)
  11401. (if (memq org-log-note-how '(time state))
  11402. (let (current-prefix-arg) (org-store-log-note))
  11403. (let ((org-inhibit-startup t)) (org-mode))
  11404. (insert (format "# Insert note for %s.
  11405. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11406. (cond
  11407. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11408. ((eq org-log-note-purpose 'done) "closed todo item")
  11409. ((eq org-log-note-purpose 'state)
  11410. (format "state change from \"%s\" to \"%s\""
  11411. (or org-log-note-previous-state "")
  11412. (or org-log-note-state "")))
  11413. ((eq org-log-note-purpose 'reschedule)
  11414. "rescheduling")
  11415. ((eq org-log-note-purpose 'delschedule)
  11416. "no longer scheduled")
  11417. ((eq org-log-note-purpose 'redeadline)
  11418. "changing deadline")
  11419. ((eq org-log-note-purpose 'deldeadline)
  11420. "removing deadline")
  11421. ((eq org-log-note-purpose 'refile)
  11422. "refiling")
  11423. ((eq org-log-note-purpose 'note)
  11424. "this entry")
  11425. (t (error "This should not happen")))))
  11426. (if org-log-note-extra (insert org-log-note-extra))
  11427. (org-set-local 'org-finish-function 'org-store-log-note)
  11428. (run-hooks 'org-log-buffer-setup-hook)))
  11429. (defvar org-note-abort nil) ; dynamically scoped
  11430. (defun org-store-log-note ()
  11431. "Finish taking a log note, and insert it to where it belongs."
  11432. (let ((txt (buffer-string))
  11433. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11434. lines ind bul)
  11435. (kill-buffer (current-buffer))
  11436. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11437. (setq txt (replace-match "" t t txt)))
  11438. (if (string-match "\\s-+\\'" txt)
  11439. (setq txt (replace-match "" t t txt)))
  11440. (setq lines (org-split-string txt "\n"))
  11441. (when (and note (string-match "\\S-" note))
  11442. (setq note
  11443. (org-replace-escapes
  11444. note
  11445. (list (cons "%u" (user-login-name))
  11446. (cons "%U" user-full-name)
  11447. (cons "%t" (format-time-string
  11448. (org-time-stamp-format 'long 'inactive)
  11449. org-log-note-effective-time))
  11450. (cons "%T" (format-time-string
  11451. (org-time-stamp-format 'long nil)
  11452. org-log-note-effective-time))
  11453. (cons "%d" (format-time-string
  11454. (org-time-stamp-format nil 'inactive)
  11455. org-log-note-effective-time))
  11456. (cons "%D" (format-time-string
  11457. (org-time-stamp-format nil nil)
  11458. org-log-note-effective-time))
  11459. (cons "%s" (if org-log-note-state
  11460. (concat "\"" org-log-note-state "\"")
  11461. ""))
  11462. (cons "%S" (if org-log-note-previous-state
  11463. (concat "\"" org-log-note-previous-state "\"")
  11464. "\"\"")))))
  11465. (if lines (setq note (concat note " \\\\")))
  11466. (push note lines))
  11467. (when (or current-prefix-arg org-note-abort)
  11468. (when org-log-into-drawer
  11469. (org-remove-empty-drawer-at
  11470. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11471. org-log-note-marker))
  11472. (setq lines nil))
  11473. (when lines
  11474. (with-current-buffer (marker-buffer org-log-note-marker)
  11475. (save-excursion
  11476. (goto-char org-log-note-marker)
  11477. (move-marker org-log-note-marker nil)
  11478. (end-of-line 1)
  11479. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11480. (setq ind (save-excursion
  11481. (if (ignore-errors (goto-char (org-in-item-p)))
  11482. (let ((struct (org-list-struct)))
  11483. (org-list-get-ind
  11484. (org-list-get-top-point struct) struct))
  11485. (skip-chars-backward " \r\t\n")
  11486. (cond
  11487. ((and (org-at-heading-p)
  11488. org-adapt-indentation)
  11489. (1+ (org-current-level)))
  11490. ((org-at-heading-p) 0)
  11491. (t (org-get-indentation))))))
  11492. (setq bul (org-list-bullet-string "-"))
  11493. (org-indent-line-to ind)
  11494. (insert bul (pop lines))
  11495. (let ((ind-body (+ (length bul) ind)))
  11496. (while lines
  11497. (insert "\n")
  11498. (org-indent-line-to ind-body)
  11499. (insert (pop lines))))
  11500. (message "Note stored")
  11501. (org-back-to-heading t)
  11502. (org-cycle-hide-drawers 'children)))))
  11503. (set-window-configuration org-log-note-window-configuration)
  11504. (with-current-buffer (marker-buffer org-log-note-return-to)
  11505. (goto-char org-log-note-return-to))
  11506. (move-marker org-log-note-return-to nil)
  11507. (and org-log-post-message (message "%s" org-log-post-message)))
  11508. (defun org-remove-empty-drawer-at (drawer pos)
  11509. "Remove an empty drawer DRAWER at position POS.
  11510. POS may also be a marker."
  11511. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11512. (save-excursion
  11513. (save-restriction
  11514. (widen)
  11515. (goto-char pos)
  11516. (if (org-in-regexp
  11517. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11518. (replace-match ""))))))
  11519. (defvar org-ts-type nil)
  11520. (defun org-sparse-tree (&optional arg type)
  11521. "Create a sparse tree, prompt for the details.
  11522. This command can create sparse trees. You first need to select the type
  11523. of match used to create the tree:
  11524. t Show all TODO entries.
  11525. T Show entries with a specific TODO keyword.
  11526. m Show entries selected by a tags/property match.
  11527. p Enter a property name and its value (both with completion on existing
  11528. names/values) and show entries with that property.
  11529. r Show entries matching a regular expression (`/' can be used as well).
  11530. b Show deadlines and scheduled items before a date.
  11531. a Show deadlines and scheduled items after a date.
  11532. d Show deadlines due within `org-deadline-warning-days'.
  11533. D Show deadlines and scheduled items between a date range."
  11534. (interactive "P")
  11535. (let (ans kwd value ts-type)
  11536. (setq type (or type org-sparse-tree-default-date-type))
  11537. (setq org-ts-type type)
  11538. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11539. (cond ((eq type 'all) "all timestamps")
  11540. ((eq type 'scheduled) "only scheduled")
  11541. ((eq type 'deadline) "only deadline")
  11542. ((eq type 'active) "only active timestamps")
  11543. ((eq type 'inactive) "only inactive timestamps")
  11544. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11545. (t "scheduled/deadline")))
  11546. (setq ans (read-char-exclusive))
  11547. (cond
  11548. ((equal ans ?c)
  11549. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11550. ((equal ans ?d)
  11551. (call-interactively 'org-check-deadlines))
  11552. ((equal ans ?b)
  11553. (call-interactively 'org-check-before-date))
  11554. ((equal ans ?a)
  11555. (call-interactively 'org-check-after-date))
  11556. ((equal ans ?D)
  11557. (call-interactively 'org-check-dates-range))
  11558. ((equal ans ?t)
  11559. (call-interactively 'org-show-todo-tree))
  11560. ((equal ans ?T)
  11561. (org-show-todo-tree '(4)))
  11562. ((member ans '(?T ?m))
  11563. (call-interactively 'org-match-sparse-tree))
  11564. ((member ans '(?p ?P))
  11565. (setq kwd (org-icompleting-read "Property: "
  11566. (mapcar 'list (org-buffer-property-keys))))
  11567. (setq value (org-icompleting-read "Value: "
  11568. (mapcar 'list (org-property-values kwd))))
  11569. (unless (string-match "\\`{.*}\\'" value)
  11570. (setq value (concat "\"" value "\"")))
  11571. (org-match-sparse-tree arg (concat kwd "=" value)))
  11572. ((member ans '(?r ?R ?/))
  11573. (call-interactively 'org-occur))
  11574. (t (error "No such sparse tree command \"%c\"" ans)))))
  11575. (defvar org-occur-highlights nil
  11576. "List of overlays used for occur matches.")
  11577. (make-variable-buffer-local 'org-occur-highlights)
  11578. (defvar org-occur-parameters nil
  11579. "Parameters of the active org-occur calls.
  11580. This is a list, each call to org-occur pushes as cons cell,
  11581. containing the regular expression and the callback, onto the list.
  11582. The list can contain several entries if `org-occur' has been called
  11583. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11584. will only contain one set of parameters. When the highlights are
  11585. removed (for example with `C-c C-c', or with the next edit (depending
  11586. on `org-remove-highlights-with-change'), this variable is emptied
  11587. as well.")
  11588. (make-variable-buffer-local 'org-occur-parameters)
  11589. (defun org-occur (regexp &optional keep-previous callback)
  11590. "Make a compact tree which shows all matches of REGEXP.
  11591. The tree will show the lines where the regexp matches, and all higher
  11592. headlines above the match. It will also show the heading after the match,
  11593. to make sure editing the matching entry is easy.
  11594. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11595. call to `org-occur' will be kept, to allow stacking of calls to this
  11596. command.
  11597. If CALLBACK is non-nil, it is a function which is called to confirm
  11598. that the match should indeed be shown."
  11599. (interactive "sRegexp: \nP")
  11600. (when (equal regexp "")
  11601. (error "Regexp cannot be empty"))
  11602. (unless keep-previous
  11603. (org-remove-occur-highlights nil nil t))
  11604. (push (cons regexp callback) org-occur-parameters)
  11605. (let ((cnt 0))
  11606. (save-excursion
  11607. (goto-char (point-min))
  11608. (if (or (not keep-previous) ; do not want to keep
  11609. (not org-occur-highlights)) ; no previous matches
  11610. ;; hide everything
  11611. (org-overview))
  11612. (while (re-search-forward regexp nil t)
  11613. (when (or (not callback)
  11614. (save-match-data (funcall callback)))
  11615. (setq cnt (1+ cnt))
  11616. (when org-highlight-sparse-tree-matches
  11617. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11618. (org-show-context 'occur-tree))))
  11619. (when org-remove-highlights-with-change
  11620. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11621. nil 'local))
  11622. (unless org-sparse-tree-open-archived-trees
  11623. (org-hide-archived-subtrees (point-min) (point-max)))
  11624. (run-hooks 'org-occur-hook)
  11625. (if (org-called-interactively-p 'interactive)
  11626. (message "%d match(es) for regexp %s" cnt regexp))
  11627. cnt))
  11628. (defun org-occur-next-match (&optional n reset)
  11629. "Function for `next-error-function' to find sparse tree matches.
  11630. N is the number of matches to move, when negative move backwards.
  11631. RESET is entirely ignored - this function always goes back to the
  11632. starting point when no match is found."
  11633. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11634. (search-func (if (< n 0)
  11635. 'previous-single-char-property-change
  11636. 'next-single-char-property-change))
  11637. (n (abs n))
  11638. (pos (point))
  11639. p1)
  11640. (catch 'exit
  11641. (while (setq p1 (funcall search-func (point) 'org-type))
  11642. (when (equal p1 limit)
  11643. (goto-char pos)
  11644. (error "No more matches"))
  11645. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11646. (setq n (1- n))
  11647. (when (= n 0)
  11648. (goto-char p1)
  11649. (throw 'exit (point))))
  11650. (goto-char p1))
  11651. (goto-char p1)
  11652. (error "No more matches"))))
  11653. (defun org-show-context (&optional key)
  11654. "Make sure point and context are visible.
  11655. How much context is shown depends upon the variables
  11656. `org-show-hierarchy-above', `org-show-following-heading',
  11657. `org-show-entry-below' and `org-show-siblings'."
  11658. (let ((heading-p (org-at-heading-p t))
  11659. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11660. (following-p (org-get-alist-option org-show-following-heading key))
  11661. (entry-p (org-get-alist-option org-show-entry-below key))
  11662. (siblings-p (org-get-alist-option org-show-siblings key)))
  11663. (catch 'exit
  11664. ;; Show heading or entry text
  11665. (if (and heading-p (not entry-p))
  11666. (org-flag-heading nil) ; only show the heading
  11667. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11668. (org-show-hidden-entry))) ; show entire entry
  11669. (when following-p
  11670. ;; Show next sibling, or heading below text
  11671. (save-excursion
  11672. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11673. (org-flag-heading nil))))
  11674. (when siblings-p (org-show-siblings))
  11675. (when hierarchy-p
  11676. ;; show all higher headings, possibly with siblings
  11677. (save-excursion
  11678. (while (and (condition-case nil
  11679. (progn (org-up-heading-all 1) t)
  11680. (error nil))
  11681. (not (bobp)))
  11682. (org-flag-heading nil)
  11683. (when siblings-p (org-show-siblings))))))))
  11684. (defvar org-reveal-start-hook nil
  11685. "Hook run before revealing a location.")
  11686. (defun org-reveal (&optional siblings)
  11687. "Show current entry, hierarchy above it, and the following headline.
  11688. This can be used to show a consistent set of context around locations
  11689. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11690. not t for the search context.
  11691. With optional argument SIBLINGS, on each level of the hierarchy all
  11692. siblings are shown. This repairs the tree structure to what it would
  11693. look like when opened with hierarchical calls to `org-cycle'.
  11694. With double optional argument \\[universal-argument] \\[universal-argument], \
  11695. go to the parent and show the
  11696. entire tree."
  11697. (interactive "P")
  11698. (run-hooks 'org-reveal-start-hook)
  11699. (let ((org-show-hierarchy-above t)
  11700. (org-show-following-heading t)
  11701. (org-show-siblings (if siblings t org-show-siblings)))
  11702. (org-show-context nil))
  11703. (when (equal siblings '(16))
  11704. (save-excursion
  11705. (when (org-up-heading-safe)
  11706. (org-show-subtree)
  11707. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11708. (defun org-highlight-new-match (beg end)
  11709. "Highlight from BEG to END and mark the highlight is an occur headline."
  11710. (let ((ov (make-overlay beg end)))
  11711. (overlay-put ov 'face 'secondary-selection)
  11712. (overlay-put ov 'org-type 'org-occur)
  11713. (push ov org-occur-highlights)))
  11714. (defun org-remove-occur-highlights (&optional beg end noremove)
  11715. "Remove the occur highlights from the buffer.
  11716. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11717. from the `before-change-functions' in the current buffer."
  11718. (interactive)
  11719. (unless org-inhibit-highlight-removal
  11720. (mapc 'delete-overlay org-occur-highlights)
  11721. (setq org-occur-highlights nil)
  11722. (setq org-occur-parameters nil)
  11723. (unless noremove
  11724. (remove-hook 'before-change-functions
  11725. 'org-remove-occur-highlights 'local))))
  11726. ;;;; Priorities
  11727. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11728. "Regular expression matching the priority indicator.")
  11729. (defvar org-remove-priority-next-time nil)
  11730. (defun org-priority-up ()
  11731. "Increase the priority of the current item."
  11732. (interactive)
  11733. (org-priority 'up))
  11734. (defun org-priority-down ()
  11735. "Decrease the priority of the current item."
  11736. (interactive)
  11737. (org-priority 'down))
  11738. (defun org-priority (&optional action show)
  11739. "Change the priority of an item.
  11740. ACTION can be `set', `up', `down', or a character."
  11741. (interactive "P")
  11742. (if (equal action '(4))
  11743. (org-show-priority)
  11744. (unless org-enable-priority-commands
  11745. (error "Priority commands are disabled"))
  11746. (setq action (or action 'set))
  11747. (let (current new news have remove)
  11748. (save-excursion
  11749. (org-back-to-heading t)
  11750. (if (looking-at org-priority-regexp)
  11751. (setq current (string-to-char (match-string 2))
  11752. have t))
  11753. (cond
  11754. ((eq action 'remove)
  11755. (setq remove t new ?\ ))
  11756. ((or (eq action 'set)
  11757. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11758. (if (not (eq action 'set))
  11759. (setq new action)
  11760. (message "Priority %c-%c, SPC to remove: "
  11761. org-highest-priority org-lowest-priority)
  11762. (save-match-data
  11763. (setq new (read-char-exclusive))))
  11764. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11765. (= (upcase org-lowest-priority) org-lowest-priority))
  11766. (setq new (upcase new)))
  11767. (cond ((equal new ?\ ) (setq remove t))
  11768. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11769. (error "Priority must be between `%c' and `%c'"
  11770. org-highest-priority org-lowest-priority))))
  11771. ((eq action 'up)
  11772. (setq new (if have
  11773. (1- current) ; normal cycling
  11774. ;; last priority was empty
  11775. (if (eq last-command this-command)
  11776. org-lowest-priority ; wrap around empty to lowest
  11777. ;; default
  11778. (if org-priority-start-cycle-with-default
  11779. org-default-priority
  11780. (1- org-default-priority))))))
  11781. ((eq action 'down)
  11782. (setq new (if have
  11783. (1+ current) ; normal cycling
  11784. ;; last priority was empty
  11785. (if (eq last-command this-command)
  11786. org-highest-priority ; wrap around empty to highest
  11787. ;; default
  11788. (if org-priority-start-cycle-with-default
  11789. org-default-priority
  11790. (1+ org-default-priority))))))
  11791. (t (error "Invalid action")))
  11792. (if (or (< (upcase new) org-highest-priority)
  11793. (> (upcase new) org-lowest-priority))
  11794. (if (and (memq action '(up down))
  11795. (not have) (not (eq last-command this-command)))
  11796. ;; `new' is from default priority
  11797. (error
  11798. "The default can not be set, see `org-default-priority' why")
  11799. ;; normal cycling: `new' is beyond highest/lowest priority
  11800. ;; and is wrapped around to the empty priority
  11801. (setq remove t)))
  11802. (setq news (format "%c" new))
  11803. (if have
  11804. (if remove
  11805. (replace-match "" t t nil 1)
  11806. (replace-match news t t nil 2))
  11807. (if remove
  11808. (error "No priority cookie found in line")
  11809. (let ((case-fold-search nil))
  11810. (looking-at org-todo-line-regexp))
  11811. (if (match-end 2)
  11812. (progn
  11813. (goto-char (match-end 2))
  11814. (insert " [#" news "]"))
  11815. (goto-char (match-beginning 3))
  11816. (insert "[#" news "] "))))
  11817. (org-preserve-lc (org-set-tags nil 'align)))
  11818. (if remove
  11819. (message "Priority removed")
  11820. (message "Priority of current item set to %s" news)))))
  11821. (defun org-show-priority ()
  11822. "Show the priority of the current item.
  11823. This priority is composed of the main priority given with the [#A] cookies,
  11824. and by additional input from the age of a schedules or deadline entry."
  11825. (interactive)
  11826. (let ((pri (if (eq major-mode 'org-agenda-mode)
  11827. (org-get-at-bol 'priority)
  11828. (save-excursion
  11829. (save-match-data
  11830. (beginning-of-line)
  11831. (and (looking-at org-heading-regexp)
  11832. (org-get-priority (match-string 0))))))))
  11833. (message "Priority is %d" (if pri pri -1000))))
  11834. (defun org-get-priority (s)
  11835. "Find priority cookie and return priority."
  11836. (if (functionp org-get-priority-function)
  11837. (funcall org-get-priority-function)
  11838. (save-match-data
  11839. (if (not (string-match org-priority-regexp s))
  11840. (* 1000 (- org-lowest-priority org-default-priority))
  11841. (* 1000 (- org-lowest-priority
  11842. (string-to-char (match-string 2 s))))))))
  11843. ;;;; Tags
  11844. (defvar org-agenda-archives-mode)
  11845. (defvar org-map-continue-from nil
  11846. "Position from where mapping should continue.
  11847. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11848. (defvar org-scanner-tags nil
  11849. "The current tag list while the tags scanner is running.")
  11850. (defvar org-trust-scanner-tags nil
  11851. "Should `org-get-tags-at' use the tags for the scanner.
  11852. This is for internal dynamical scoping only.
  11853. When this is non-nil, the function `org-get-tags-at' will return the value
  11854. of `org-scanner-tags' instead of building the list by itself. This
  11855. can lead to large speed-ups when the tags scanner is used in a file with
  11856. many entries, and when the list of tags is retrieved, for example to
  11857. obtain a list of properties. Building the tags list for each entry in such
  11858. a file becomes an N^2 operation - but with this variable set, it scales
  11859. as N.")
  11860. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11861. "Scan headline tags with inheritance and produce output ACTION.
  11862. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11863. or `agenda' to produce an entry list for an agenda view. It can also be
  11864. a Lisp form or a function that should be called at each matched headline, in
  11865. this case the return value is a list of all return values from these calls.
  11866. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11867. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11868. only lines with a not-done TODO keyword are included in the output.
  11869. This should be the same variable that was scoped into
  11870. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11871. START-LEVEL can be a string with asterisks, reducing the scope to
  11872. headlines matching this string."
  11873. (require 'org-agenda)
  11874. (let* ((re (concat "^"
  11875. (if start-level
  11876. ;; Get the correct level to match
  11877. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11878. org-outline-regexp)
  11879. " *\\(\\<\\("
  11880. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11881. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11882. (props (list 'face 'default
  11883. 'done-face 'org-agenda-done
  11884. 'undone-face 'default
  11885. 'mouse-face 'highlight
  11886. 'org-not-done-regexp org-not-done-regexp
  11887. 'org-todo-regexp org-todo-regexp
  11888. 'org-complex-heading-regexp org-complex-heading-regexp
  11889. 'help-echo
  11890. (format "mouse-2 or RET jump to org file %s"
  11891. (abbreviate-file-name
  11892. (or (buffer-file-name (buffer-base-buffer))
  11893. (buffer-name (buffer-base-buffer)))))))
  11894. (case-fold-search nil)
  11895. (org-map-continue-from nil)
  11896. lspos tags tags-list
  11897. (tags-alist (list (cons 0 org-file-tags)))
  11898. (llast 0) rtn rtn1 level category i txt
  11899. todo marker entry priority)
  11900. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11901. (setq action (list 'lambda nil action)))
  11902. (save-excursion
  11903. (goto-char (point-min))
  11904. (when (eq action 'sparse-tree)
  11905. (org-overview)
  11906. (org-remove-occur-highlights))
  11907. (while (re-search-forward re nil t)
  11908. (setq org-map-continue-from nil)
  11909. (catch :skip
  11910. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11911. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11912. (goto-char (setq lspos (match-beginning 0)))
  11913. (setq level (org-reduced-level (funcall outline-level))
  11914. category (org-get-category))
  11915. (setq i llast llast level)
  11916. ;; remove tag lists from same and sublevels
  11917. (while (>= i level)
  11918. (when (setq entry (assoc i tags-alist))
  11919. (setq tags-alist (delete entry tags-alist)))
  11920. (setq i (1- i)))
  11921. ;; add the next tags
  11922. (when tags
  11923. (setq tags (org-split-string tags ":")
  11924. tags-alist
  11925. (cons (cons level tags) tags-alist)))
  11926. ;; compile tags for current headline
  11927. (setq tags-list
  11928. (if org-use-tag-inheritance
  11929. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11930. tags)
  11931. org-scanner-tags tags-list)
  11932. (when org-use-tag-inheritance
  11933. (setcdr (car tags-alist)
  11934. (mapcar (lambda (x)
  11935. (setq x (copy-sequence x))
  11936. (org-add-prop-inherited x))
  11937. (cdar tags-alist))))
  11938. (when (and tags org-use-tag-inheritance
  11939. (or (not (eq t org-use-tag-inheritance))
  11940. org-tags-exclude-from-inheritance))
  11941. ;; selective inheritance, remove uninherited ones
  11942. (setcdr (car tags-alist)
  11943. (org-remove-uninherited-tags (cdar tags-alist))))
  11944. (when (and
  11945. ;; eval matcher only when the todo condition is OK
  11946. (and (or (not todo-only) (member todo org-not-done-keywords))
  11947. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11948. (eval matcher)))
  11949. ;; Call the skipper, but return t if it does not skip,
  11950. ;; so that the `and' form continues evaluating
  11951. (progn
  11952. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11953. t)
  11954. ;; Check if timestamps are deselecting this entry
  11955. (or (not todo-only)
  11956. (and (member todo org-not-done-keywords)
  11957. (or (not org-agenda-tags-todo-honor-ignore-options)
  11958. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11959. ;; Extra check for the archive tag
  11960. ;; FIXME: Does the skipper already do this????
  11961. (or
  11962. (not (member org-archive-tag tags-list))
  11963. ;; we have an archive tag, should we use this anyway?
  11964. (or (not org-agenda-skip-archived-trees)
  11965. (and (eq action 'agenda) org-agenda-archives-mode))))
  11966. ;; select this headline
  11967. (cond
  11968. ((eq action 'sparse-tree)
  11969. (and org-highlight-sparse-tree-matches
  11970. (org-get-heading) (match-end 0)
  11971. (org-highlight-new-match
  11972. (match-beginning 1) (match-end 1)))
  11973. (org-show-context 'tags-tree))
  11974. ((eq action 'agenda)
  11975. (setq txt (org-agenda-format-item
  11976. ""
  11977. (concat
  11978. (if (eq org-tags-match-list-sublevels 'indented)
  11979. (make-string (1- level) ?.) "")
  11980. (org-get-heading))
  11981. category
  11982. tags-list)
  11983. priority (org-get-priority txt))
  11984. (goto-char lspos)
  11985. (setq marker (org-agenda-new-marker))
  11986. (org-add-props txt props
  11987. 'org-marker marker 'org-hd-marker marker 'org-category category
  11988. 'todo-state todo
  11989. 'priority priority 'type "tagsmatch")
  11990. (push txt rtn))
  11991. ((functionp action)
  11992. (setq org-map-continue-from nil)
  11993. (save-excursion
  11994. (setq rtn1 (funcall action))
  11995. (push rtn1 rtn)))
  11996. (t (error "Invalid action")))
  11997. ;; if we are to skip sublevels, jump to end of subtree
  11998. (unless org-tags-match-list-sublevels
  11999. (org-end-of-subtree t)
  12000. (backward-char 1))))
  12001. ;; Get the correct position from where to continue
  12002. (if org-map-continue-from
  12003. (goto-char org-map-continue-from)
  12004. (and (= (point) lspos) (end-of-line 1)))))
  12005. (when (and (eq action 'sparse-tree)
  12006. (not org-sparse-tree-open-archived-trees))
  12007. (org-hide-archived-subtrees (point-min) (point-max)))
  12008. (nreverse rtn)))
  12009. (defun org-remove-uninherited-tags (tags)
  12010. "Remove all tags that are not inherited from the list TAGS."
  12011. (cond
  12012. ((eq org-use-tag-inheritance t)
  12013. (if org-tags-exclude-from-inheritance
  12014. (org-delete-all org-tags-exclude-from-inheritance tags)
  12015. tags))
  12016. ((not org-use-tag-inheritance) nil)
  12017. ((stringp org-use-tag-inheritance)
  12018. (delq nil (mapcar
  12019. (lambda (x)
  12020. (if (and (string-match org-use-tag-inheritance x)
  12021. (not (member x org-tags-exclude-from-inheritance)))
  12022. x nil))
  12023. tags)))
  12024. ((listp org-use-tag-inheritance)
  12025. (delq nil (mapcar
  12026. (lambda (x)
  12027. (if (member x org-use-tag-inheritance) x nil))
  12028. tags)))))
  12029. (defun org-match-sparse-tree (&optional todo-only match)
  12030. "Create a sparse tree according to tags string MATCH.
  12031. MATCH can contain positive and negative selection of tags, like
  12032. \"+WORK+URGENT-WITHBOSS\".
  12033. If optional argument TODO-ONLY is non-nil, only select lines that are
  12034. also TODO lines."
  12035. (interactive "P")
  12036. (org-agenda-prepare-buffers (list (current-buffer)))
  12037. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12038. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12039. (defvar org-cached-props nil)
  12040. (defun org-cached-entry-get (pom property)
  12041. (if (or (eq t org-use-property-inheritance)
  12042. (and (stringp org-use-property-inheritance)
  12043. (string-match org-use-property-inheritance property))
  12044. (and (listp org-use-property-inheritance)
  12045. (member property org-use-property-inheritance)))
  12046. ;; Caching is not possible, check it directly
  12047. (org-entry-get pom property 'inherit)
  12048. ;; Get all properties, so that we can do complicated checks easily
  12049. (cdr (assoc property (or org-cached-props
  12050. (setq org-cached-props
  12051. (org-entry-properties pom)))))))
  12052. (defun org-global-tags-completion-table (&optional files)
  12053. "Return the list of all tags in all agenda buffer/files.
  12054. Optional FILES argument is a list of files which can be used
  12055. instead of the agenda files."
  12056. (save-excursion
  12057. (org-uniquify
  12058. (delq nil
  12059. (apply 'append
  12060. (mapcar
  12061. (lambda (file)
  12062. (set-buffer (find-file-noselect file))
  12063. (append (org-get-buffer-tags)
  12064. (mapcar (lambda (x) (if (stringp (car-safe x))
  12065. (list (car-safe x)) nil))
  12066. org-tag-alist)))
  12067. (if (and files (car files))
  12068. files
  12069. (org-agenda-files))))))))
  12070. (defun org-make-tags-matcher (match)
  12071. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12072. The variable `todo-only' is scoped dynamically into this function.
  12073. It will be set to t if the matcher restricts matching to TODO entries,
  12074. otherwise will not be touched.
  12075. Returns a cons of the selection string MATCH and the constructed
  12076. lisp form implementing the matcher. The matcher is to be evaluated
  12077. at an Org entry, with point on the headline, and returns t if the
  12078. entry matches the selection string MATCH. The returned lisp form
  12079. references two variables with information about the entry, which
  12080. must be bound around the form's evaluation: todo, the TODO keyword
  12081. at the entry (or nil of none); and tags-list, the list of all tags
  12082. at the entry including inherited ones. Additionally, the category
  12083. of the entry (if any) must be specified as the text property
  12084. 'org-category on the headline.
  12085. See also `org-scan-tags'.
  12086. "
  12087. (declare (special todo-only))
  12088. (unless (boundp 'todo-only)
  12089. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12090. (unless match
  12091. ;; Get a new match request, with completion
  12092. (let ((org-last-tags-completion-table
  12093. (org-global-tags-completion-table)))
  12094. (setq match (org-completing-read-no-i
  12095. "Match: " 'org-tags-completion-function nil nil nil
  12096. 'org-tags-history))))
  12097. ;; Parse the string and create a lisp form
  12098. (let ((match0 match)
  12099. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12100. minus tag mm
  12101. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12102. orterms term orlist re-p str-p level-p level-op time-p
  12103. prop-p pn pv po gv rest)
  12104. (if (string-match "/+" match)
  12105. ;; match contains also a todo-matching request
  12106. (progn
  12107. (setq tagsmatch (substring match 0 (match-beginning 0))
  12108. todomatch (substring match (match-end 0)))
  12109. (if (string-match "^!" todomatch)
  12110. (setq todo-only t todomatch (substring todomatch 1)))
  12111. (if (string-match "^\\s-*$" todomatch)
  12112. (setq todomatch nil)))
  12113. ;; only matching tags
  12114. (setq tagsmatch match todomatch nil))
  12115. ;; Make the tags matcher
  12116. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12117. (setq tagsmatcher t)
  12118. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12119. (while (setq term (pop orterms))
  12120. (while (and (equal (substring term -1) "\\") orterms)
  12121. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12122. (while (string-match re term)
  12123. (setq rest (substring term (match-end 0))
  12124. minus (and (match-end 1)
  12125. (equal (match-string 1 term) "-"))
  12126. tag (save-match-data (replace-regexp-in-string
  12127. "\\\\-" "-"
  12128. (match-string 2 term)))
  12129. re-p (equal (string-to-char tag) ?{)
  12130. level-p (match-end 4)
  12131. prop-p (match-end 5)
  12132. mm (cond
  12133. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12134. (level-p
  12135. (setq level-op (org-op-to-function (match-string 3 term)))
  12136. `(,level-op level ,(string-to-number
  12137. (match-string 4 term))))
  12138. (prop-p
  12139. (setq pn (match-string 5 term)
  12140. po (match-string 6 term)
  12141. pv (match-string 7 term)
  12142. re-p (equal (string-to-char pv) ?{)
  12143. str-p (equal (string-to-char pv) ?\")
  12144. time-p (save-match-data
  12145. (string-match "^\"[[<].*[]>]\"$" pv))
  12146. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12147. (if time-p (setq pv (org-matcher-time pv)))
  12148. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12149. (cond
  12150. ((equal pn "CATEGORY")
  12151. (setq gv '(get-text-property (point) 'org-category)))
  12152. ((equal pn "TODO")
  12153. (setq gv 'todo))
  12154. (t
  12155. (setq gv `(org-cached-entry-get nil ,pn))))
  12156. (if re-p
  12157. (if (eq po 'org<>)
  12158. `(not (string-match ,pv (or ,gv "")))
  12159. `(string-match ,pv (or ,gv "")))
  12160. (if str-p
  12161. `(,po (or ,gv "") ,pv)
  12162. `(,po (string-to-number (or ,gv ""))
  12163. ,(string-to-number pv) ))))
  12164. (t `(member ,tag tags-list)))
  12165. mm (if minus (list 'not mm) mm)
  12166. term rest)
  12167. (push mm tagsmatcher))
  12168. (push (if (> (length tagsmatcher) 1)
  12169. (cons 'and tagsmatcher)
  12170. (car tagsmatcher))
  12171. orlist)
  12172. (setq tagsmatcher nil))
  12173. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12174. (setq tagsmatcher
  12175. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12176. ;; Make the todo matcher
  12177. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12178. (setq todomatcher t)
  12179. (setq orterms (org-split-string todomatch "|") orlist nil)
  12180. (while (setq term (pop orterms))
  12181. (while (string-match re term)
  12182. (setq minus (and (match-end 1)
  12183. (equal (match-string 1 term) "-"))
  12184. kwd (match-string 2 term)
  12185. re-p (equal (string-to-char kwd) ?{)
  12186. term (substring term (match-end 0))
  12187. mm (if re-p
  12188. `(string-match ,(substring kwd 1 -1) todo)
  12189. (list 'equal 'todo kwd))
  12190. mm (if minus (list 'not mm) mm))
  12191. (push mm todomatcher))
  12192. (push (if (> (length todomatcher) 1)
  12193. (cons 'and todomatcher)
  12194. (car todomatcher))
  12195. orlist)
  12196. (setq todomatcher nil))
  12197. (setq todomatcher (if (> (length orlist) 1)
  12198. (cons 'or orlist) (car orlist))))
  12199. ;; Return the string and lisp forms of the matcher
  12200. (setq matcher (if todomatcher
  12201. (list 'and tagsmatcher todomatcher)
  12202. tagsmatcher))
  12203. (when todo-only
  12204. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12205. matcher)))
  12206. (cons match0 matcher)))
  12207. (defun org-op-to-function (op &optional stringp)
  12208. "Turn an operator into the appropriate function."
  12209. (setq op
  12210. (cond
  12211. ((equal op "<" ) '(< string< org-time<))
  12212. ((equal op ">" ) '(> org-string> org-time>))
  12213. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12214. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12215. ((member op '("=" "==")) '(= string= org-time=))
  12216. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12217. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12218. (defun org<> (a b) (not (= a b)))
  12219. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12220. (defun org-string>= (a b) (not (string< a b)))
  12221. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12222. (defun org-string<> (a b) (not (string= a b)))
  12223. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12224. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12225. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12226. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12227. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12228. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12229. (defun org-2ft (s)
  12230. "Convert S to a floating point time.
  12231. If S is already a number, just return it. If it is a string, parse
  12232. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12233. (cond
  12234. ((numberp s) s)
  12235. ((stringp s)
  12236. (condition-case nil
  12237. (float-time (apply 'encode-time (org-parse-time-string s)))
  12238. (error 0.)))
  12239. (t 0.)))
  12240. (defun org-time-today ()
  12241. "Time in seconds today at 0:00.
  12242. Returns the float number of seconds since the beginning of the
  12243. epoch to the beginning of today (00:00)."
  12244. (float-time (apply 'encode-time
  12245. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12246. (defun org-matcher-time (s)
  12247. "Interpret a time comparison value."
  12248. (save-match-data
  12249. (cond
  12250. ((string= s "<now>") (float-time))
  12251. ((string= s "<today>") (org-time-today))
  12252. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12253. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12254. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12255. (+ (org-time-today)
  12256. (* (string-to-number (match-string 1 s))
  12257. (cdr (assoc (match-string 2 s)
  12258. '(("d" . 86400.0) ("w" . 604800.0)
  12259. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12260. (t (org-2ft s)))))
  12261. (defun org-match-any-p (re list)
  12262. "Does re match any element of list?"
  12263. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12264. (delq nil list))
  12265. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12266. (defvar org-tags-overlay (make-overlay 1 1))
  12267. (org-detach-overlay org-tags-overlay)
  12268. (defun org-get-local-tags-at (&optional pos)
  12269. "Get a list of tags defined in the current headline."
  12270. (org-get-tags-at pos 'local))
  12271. (defun org-get-local-tags ()
  12272. "Get a list of tags defined in the current headline."
  12273. (org-get-tags-at nil 'local))
  12274. (defun org-get-tags-at (&optional pos local)
  12275. "Get a list of all headline tags applicable at POS.
  12276. POS defaults to point. If tags are inherited, the list contains
  12277. the targets in the same sequence as the headlines appear, i.e.
  12278. the tags of the current headline come last.
  12279. When LOCAL is non-nil, only return tags from the current headline,
  12280. ignore inherited ones."
  12281. (interactive)
  12282. (if (and org-trust-scanner-tags
  12283. (or (not pos) (equal pos (point)))
  12284. (not local))
  12285. org-scanner-tags
  12286. (let (tags ltags lastpos parent)
  12287. (save-excursion
  12288. (save-restriction
  12289. (widen)
  12290. (goto-char (or pos (point)))
  12291. (save-match-data
  12292. (catch 'done
  12293. (condition-case nil
  12294. (progn
  12295. (org-back-to-heading t)
  12296. (while (not (equal lastpos (point)))
  12297. (setq lastpos (point))
  12298. (when (looking-at
  12299. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12300. (setq ltags (org-split-string
  12301. (org-match-string-no-properties 1) ":"))
  12302. (when parent
  12303. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12304. (setq tags (append
  12305. (if parent
  12306. (org-remove-uninherited-tags ltags)
  12307. ltags)
  12308. tags)))
  12309. (or org-use-tag-inheritance (throw 'done t))
  12310. (if local (throw 'done t))
  12311. (or (org-up-heading-safe) (error nil))
  12312. (setq parent t)))
  12313. (error nil)))))
  12314. (if local
  12315. tags
  12316. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12317. (defun org-add-prop-inherited (s)
  12318. (add-text-properties 0 (length s) '(inherited t) s)
  12319. s)
  12320. (defun org-toggle-tag (tag &optional onoff)
  12321. "Toggle the tag TAG for the current line.
  12322. If ONOFF is `on' or `off', don't toggle but set to this state."
  12323. (let (res current)
  12324. (save-excursion
  12325. (org-back-to-heading t)
  12326. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12327. (point-at-eol) t)
  12328. (progn
  12329. (setq current (match-string 1))
  12330. (replace-match ""))
  12331. (setq current ""))
  12332. (setq current (nreverse (org-split-string current ":")))
  12333. (cond
  12334. ((eq onoff 'on)
  12335. (setq res t)
  12336. (or (member tag current) (push tag current)))
  12337. ((eq onoff 'off)
  12338. (or (not (member tag current)) (setq current (delete tag current))))
  12339. (t (if (member tag current)
  12340. (setq current (delete tag current))
  12341. (setq res t)
  12342. (push tag current))))
  12343. (end-of-line 1)
  12344. (if current
  12345. (progn
  12346. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12347. (org-set-tags nil t))
  12348. (delete-horizontal-space))
  12349. (run-hooks 'org-after-tags-change-hook))
  12350. res))
  12351. (defun org-align-tags-here (to-col)
  12352. ;; Assumes that this is a headline
  12353. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12354. (beginning-of-line 1)
  12355. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12356. (< pos (match-beginning 2)))
  12357. (progn
  12358. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12359. (goto-char (match-beginning 1))
  12360. (insert " ")
  12361. (delete-region (point) (1+ (match-beginning 2)))
  12362. (setq ncol (max (current-column)
  12363. (1+ col)
  12364. (if (> to-col 0)
  12365. to-col
  12366. (- (abs to-col) tags-l))))
  12367. (setq p (point))
  12368. (insert (make-string (- ncol (current-column)) ?\ ))
  12369. (setq ncol (current-column))
  12370. (when indent-tabs-mode (tabify p (point-at-eol)))
  12371. (org-move-to-column (min ncol col) t))
  12372. (goto-char pos))))
  12373. (defun org-set-tags-command (&optional arg just-align)
  12374. "Call the set-tags command for the current entry."
  12375. (interactive "P")
  12376. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12377. (org-set-tags arg just-align)
  12378. (save-excursion
  12379. (org-back-to-heading t)
  12380. (org-set-tags arg just-align))))
  12381. (defun org-set-tags-to (data)
  12382. "Set the tags of the current entry to DATA, replacing the current tags.
  12383. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12384. If DATA is nil or the empty string, any tags will be removed."
  12385. (interactive "sTags: ")
  12386. (setq data
  12387. (cond
  12388. ((eq data nil) "")
  12389. ((equal data "") "")
  12390. ((stringp data)
  12391. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12392. ":"))
  12393. ((listp data)
  12394. (concat ":" (mapconcat 'identity data ":") ":"))))
  12395. (when data
  12396. (save-excursion
  12397. (org-back-to-heading t)
  12398. (when (looking-at org-complex-heading-regexp)
  12399. (if (match-end 5)
  12400. (progn
  12401. (goto-char (match-beginning 5))
  12402. (insert data)
  12403. (delete-region (point) (point-at-eol))
  12404. (org-set-tags nil 'align))
  12405. (goto-char (point-at-eol))
  12406. (insert " " data)
  12407. (org-set-tags nil 'align)))
  12408. (beginning-of-line 1)
  12409. (if (looking-at ".*?\\([ \t]+\\)$")
  12410. (delete-region (match-beginning 1) (match-end 1))))))
  12411. (defun org-align-all-tags ()
  12412. "Align the tags i all headings."
  12413. (interactive)
  12414. (save-excursion
  12415. (or (ignore-errors (org-back-to-heading t))
  12416. (outline-next-heading))
  12417. (if (org-at-heading-p)
  12418. (org-set-tags t)
  12419. (message "No headings"))))
  12420. (defvar org-indent-indentation-per-level)
  12421. (defun org-set-tags (&optional arg just-align)
  12422. "Set the tags for the current headline.
  12423. With prefix ARG, realign all tags in headings in the current buffer."
  12424. (interactive "P")
  12425. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12426. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12427. 'region-start-level 'region))
  12428. org-loop-over-headlines-in-active-region)
  12429. (org-map-entries
  12430. ;; We don't use ARG and JUST-ALIGN here these args are not
  12431. ;; useful when looping over headlines
  12432. `(org-set-tags)
  12433. org-loop-over-headlines-in-active-region
  12434. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12435. (let* ((re org-outline-regexp-bol)
  12436. (current (unless arg (org-get-tags-string)))
  12437. (col (current-column))
  12438. (org-setting-tags t)
  12439. table current-tags inherited-tags ; computed below when needed
  12440. tags p0 c0 c1 rpl di tc level)
  12441. (if arg
  12442. (save-excursion
  12443. (goto-char (point-min))
  12444. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12445. (while (re-search-forward re nil t)
  12446. (org-set-tags nil t)
  12447. (end-of-line 1)))
  12448. (message "All tags realigned to column %d" org-tags-column))
  12449. (if just-align
  12450. (setq tags current)
  12451. ;; Get a new set of tags from the user
  12452. (save-excursion
  12453. (setq table (append org-tag-persistent-alist
  12454. (or org-tag-alist (org-get-buffer-tags))
  12455. (and
  12456. org-complete-tags-always-offer-all-agenda-tags
  12457. (org-global-tags-completion-table
  12458. (org-agenda-files))))
  12459. org-last-tags-completion-table table
  12460. current-tags (org-split-string current ":")
  12461. inherited-tags (nreverse
  12462. (nthcdr (length current-tags)
  12463. (nreverse (org-get-tags-at))))
  12464. tags
  12465. (if (or (eq t org-use-fast-tag-selection)
  12466. (and org-use-fast-tag-selection
  12467. (delq nil (mapcar 'cdr table))))
  12468. (org-fast-tag-selection
  12469. current-tags inherited-tags table
  12470. (if org-fast-tag-selection-include-todo
  12471. org-todo-key-alist))
  12472. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12473. (org-trim
  12474. (org-icompleting-read "Tags: "
  12475. 'org-tags-completion-function
  12476. nil nil current 'org-tags-history))))))
  12477. (while (string-match "[-+&]+" tags)
  12478. ;; No boolean logic, just a list
  12479. (setq tags (replace-match ":" t t tags))))
  12480. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12481. (if org-tags-sort-function
  12482. (setq tags (mapconcat 'identity
  12483. (sort (org-split-string
  12484. tags (org-re "[^[:alnum:]_@#%]+"))
  12485. org-tags-sort-function) ":")))
  12486. (if (string-match "\\`[\t ]*\\'" tags)
  12487. (setq tags "")
  12488. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12489. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12490. ;; Insert new tags at the correct column
  12491. (beginning-of-line 1)
  12492. (setq level (or (and (looking-at org-outline-regexp)
  12493. (- (match-end 0) (point) 1))
  12494. 1))
  12495. (cond
  12496. ((and (equal current "") (equal tags "")))
  12497. ((re-search-forward
  12498. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12499. (point-at-eol) t)
  12500. (if (equal tags "")
  12501. (setq rpl "")
  12502. (goto-char (match-beginning 0))
  12503. (setq c0 (current-column)
  12504. ;; compute offset for the case of org-indent-mode active
  12505. di (if org-indent-mode
  12506. (* (1- org-indent-indentation-per-level) (1- level))
  12507. 0)
  12508. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12509. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12510. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12511. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12512. (replace-match rpl t t)
  12513. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12514. tags)
  12515. (t (error "Tags alignment failed")))
  12516. (org-move-to-column col)
  12517. (unless just-align
  12518. (run-hooks 'org-after-tags-change-hook))))))
  12519. (defun org-change-tag-in-region (beg end tag off)
  12520. "Add or remove TAG for each entry in the region.
  12521. This works in the agenda, and also in an org-mode buffer."
  12522. (interactive
  12523. (list (region-beginning) (region-end)
  12524. (let ((org-last-tags-completion-table
  12525. (if (derived-mode-p 'org-mode)
  12526. (org-get-buffer-tags)
  12527. (org-global-tags-completion-table))))
  12528. (org-icompleting-read
  12529. "Tag: " 'org-tags-completion-function nil nil nil
  12530. 'org-tags-history))
  12531. (progn
  12532. (message "[s]et or [r]emove? ")
  12533. (equal (read-char-exclusive) ?r))))
  12534. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12535. (let ((agendap (equal major-mode 'org-agenda-mode))
  12536. l1 l2 m buf pos newhead (cnt 0))
  12537. (goto-char end)
  12538. (setq l2 (1- (org-current-line)))
  12539. (goto-char beg)
  12540. (setq l1 (org-current-line))
  12541. (loop for l from l1 to l2 do
  12542. (org-goto-line l)
  12543. (setq m (get-text-property (point) 'org-hd-marker))
  12544. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12545. (and agendap m))
  12546. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12547. pos (if agendap m (point)))
  12548. (with-current-buffer buf
  12549. (save-excursion
  12550. (save-restriction
  12551. (goto-char pos)
  12552. (setq cnt (1+ cnt))
  12553. (org-toggle-tag tag (if off 'off 'on))
  12554. (setq newhead (org-get-heading)))))
  12555. (and agendap (org-agenda-change-all-lines newhead m))))
  12556. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12557. (defun org-tags-completion-function (string predicate &optional flag)
  12558. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12559. (confirm (lambda (x) (stringp (car x)))))
  12560. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12561. (setq s1 (match-string 1 string)
  12562. s2 (match-string 2 string))
  12563. (setq s1 "" s2 string))
  12564. (cond
  12565. ((eq flag nil)
  12566. ;; try completion
  12567. (setq rtn (try-completion s2 ctable confirm))
  12568. (if (stringp rtn)
  12569. (setq rtn
  12570. (concat s1 s2 (substring rtn (length s2))
  12571. (if (and org-add-colon-after-tag-completion
  12572. (assoc rtn ctable))
  12573. ":" ""))))
  12574. rtn)
  12575. ((eq flag t)
  12576. ;; all-completions
  12577. (all-completions s2 ctable confirm)
  12578. )
  12579. ((eq flag 'lambda)
  12580. ;; exact match?
  12581. (assoc s2 ctable)))
  12582. ))
  12583. (defun org-fast-tag-insert (kwd tags face &optional end)
  12584. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12585. (insert (format "%-12s" (concat kwd ":"))
  12586. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12587. (or end "")))
  12588. (defun org-fast-tag-show-exit (flag)
  12589. (save-excursion
  12590. (org-goto-line 3)
  12591. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12592. (replace-match ""))
  12593. (when flag
  12594. (end-of-line 1)
  12595. (org-move-to-column (- (window-width) 19) t)
  12596. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12597. (defun org-set-current-tags-overlay (current prefix)
  12598. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12599. (if (featurep 'xemacs)
  12600. (org-overlay-display org-tags-overlay (concat prefix s)
  12601. 'secondary-selection)
  12602. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12603. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12604. (defvar org-last-tag-selection-key nil)
  12605. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12606. "Fast tag selection with single keys.
  12607. CURRENT is the current list of tags in the headline, INHERITED is the
  12608. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12609. possibly with grouping information. TODO-TABLE is a similar table with
  12610. TODO keywords, should these have keys assigned to them.
  12611. If the keys are nil, a-z are automatically assigned.
  12612. Returns the new tags string, or nil to not change the current settings."
  12613. (let* ((fulltable (append table todo-table))
  12614. (maxlen (apply 'max (mapcar
  12615. (lambda (x)
  12616. (if (stringp (car x)) (string-width (car x)) 0))
  12617. fulltable)))
  12618. (buf (current-buffer))
  12619. (expert (eq org-fast-tag-selection-single-key 'expert))
  12620. (buffer-tags nil)
  12621. (fwidth (+ maxlen 3 1 3))
  12622. (ncol (/ (- (window-width) 4) fwidth))
  12623. (i-face 'org-done)
  12624. (c-face 'org-todo)
  12625. tg cnt e c char c1 c2 ntable tbl rtn
  12626. ov-start ov-end ov-prefix
  12627. (exit-after-next org-fast-tag-selection-single-key)
  12628. (done-keywords org-done-keywords)
  12629. groups ingroup)
  12630. (save-excursion
  12631. (beginning-of-line 1)
  12632. (if (looking-at
  12633. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12634. (setq ov-start (match-beginning 1)
  12635. ov-end (match-end 1)
  12636. ov-prefix "")
  12637. (setq ov-start (1- (point-at-eol))
  12638. ov-end (1+ ov-start))
  12639. (skip-chars-forward "^\n\r")
  12640. (setq ov-prefix
  12641. (concat
  12642. (buffer-substring (1- (point)) (point))
  12643. (if (> (current-column) org-tags-column)
  12644. " "
  12645. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12646. (move-overlay org-tags-overlay ov-start ov-end)
  12647. (save-window-excursion
  12648. (if expert
  12649. (set-buffer (get-buffer-create " *Org tags*"))
  12650. (delete-other-windows)
  12651. (split-window-vertically)
  12652. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12653. (erase-buffer)
  12654. (org-set-local 'org-done-keywords done-keywords)
  12655. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12656. (org-fast-tag-insert "Current" current c-face "\n\n")
  12657. (org-fast-tag-show-exit exit-after-next)
  12658. (org-set-current-tags-overlay current ov-prefix)
  12659. (setq tbl fulltable char ?a cnt 0)
  12660. (while (setq e (pop tbl))
  12661. (cond
  12662. ((equal (car e) :startgroup)
  12663. (push '() groups) (setq ingroup t)
  12664. (when (not (= cnt 0))
  12665. (setq cnt 0)
  12666. (insert "\n"))
  12667. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12668. ((equal (car e) :endgroup)
  12669. (setq ingroup nil cnt 0)
  12670. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12671. ((equal e '(:newline))
  12672. (when (not (= cnt 0))
  12673. (setq cnt 0)
  12674. (insert "\n")
  12675. (setq e (car tbl))
  12676. (while (equal (car tbl) '(:newline))
  12677. (insert "\n")
  12678. (setq tbl (cdr tbl)))))
  12679. (t
  12680. (setq tg (copy-sequence (car e)) c2 nil)
  12681. (if (cdr e)
  12682. (setq c (cdr e))
  12683. ;; automatically assign a character.
  12684. (setq c1 (string-to-char
  12685. (downcase (substring
  12686. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12687. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12688. (while (or (rassoc char ntable) (rassoc char table))
  12689. (setq char (1+ char)))
  12690. (setq c2 c1))
  12691. (setq c (or c2 char)))
  12692. (if ingroup (push tg (car groups)))
  12693. (setq tg (org-add-props tg nil 'face
  12694. (cond
  12695. ((not (assoc tg table))
  12696. (org-get-todo-face tg))
  12697. ((member tg current) c-face)
  12698. ((member tg inherited) i-face))))
  12699. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12700. (insert "[" c "] " tg (make-string
  12701. (- fwidth 4 (length tg)) ?\ ))
  12702. (push (cons tg c) ntable)
  12703. (when (= (setq cnt (1+ cnt)) ncol)
  12704. (insert "\n")
  12705. (if ingroup (insert " "))
  12706. (setq cnt 0)))))
  12707. (setq ntable (nreverse ntable))
  12708. (insert "\n")
  12709. (goto-char (point-min))
  12710. (if (not expert) (org-fit-window-to-buffer))
  12711. (setq rtn
  12712. (catch 'exit
  12713. (while t
  12714. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12715. (if (not groups) "no " "")
  12716. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12717. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12718. (setq org-last-tag-selection-key c)
  12719. (cond
  12720. ((= c ?\r) (throw 'exit t))
  12721. ((= c ?!)
  12722. (setq groups (not groups))
  12723. (goto-char (point-min))
  12724. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12725. ((= c ?\C-c)
  12726. (if (not expert)
  12727. (org-fast-tag-show-exit
  12728. (setq exit-after-next (not exit-after-next)))
  12729. (setq expert nil)
  12730. (delete-other-windows)
  12731. (set-window-buffer (split-window-vertically) " *Org tags*")
  12732. (org-switch-to-buffer-other-window " *Org tags*")
  12733. (org-fit-window-to-buffer)))
  12734. ((or (= c ?\C-g)
  12735. (and (= c ?q) (not (rassoc c ntable))))
  12736. (org-detach-overlay org-tags-overlay)
  12737. (setq quit-flag t))
  12738. ((= c ?\ )
  12739. (setq current nil)
  12740. (if exit-after-next (setq exit-after-next 'now)))
  12741. ((= c ?\t)
  12742. (condition-case nil
  12743. (setq tg (org-icompleting-read
  12744. "Tag: "
  12745. (or buffer-tags
  12746. (with-current-buffer buf
  12747. (org-get-buffer-tags)))))
  12748. (quit (setq tg "")))
  12749. (when (string-match "\\S-" tg)
  12750. (add-to-list 'buffer-tags (list tg))
  12751. (if (member tg current)
  12752. (setq current (delete tg current))
  12753. (push tg current)))
  12754. (if exit-after-next (setq exit-after-next 'now)))
  12755. ((setq e (rassoc c todo-table) tg (car e))
  12756. (with-current-buffer buf
  12757. (save-excursion (org-todo tg)))
  12758. (if exit-after-next (setq exit-after-next 'now)))
  12759. ((setq e (rassoc c ntable) tg (car e))
  12760. (if (member tg current)
  12761. (setq current (delete tg current))
  12762. (loop for g in groups do
  12763. (if (member tg g)
  12764. (mapc (lambda (x)
  12765. (setq current (delete x current)))
  12766. g)))
  12767. (push tg current))
  12768. (if exit-after-next (setq exit-after-next 'now))))
  12769. ;; Create a sorted list
  12770. (setq current
  12771. (sort current
  12772. (lambda (a b)
  12773. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12774. (if (eq exit-after-next 'now) (throw 'exit t))
  12775. (goto-char (point-min))
  12776. (beginning-of-line 2)
  12777. (delete-region (point) (point-at-eol))
  12778. (org-fast-tag-insert "Current" current c-face)
  12779. (org-set-current-tags-overlay current ov-prefix)
  12780. (while (re-search-forward
  12781. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12782. (setq tg (match-string 1))
  12783. (add-text-properties
  12784. (match-beginning 1) (match-end 1)
  12785. (list 'face
  12786. (cond
  12787. ((member tg current) c-face)
  12788. ((member tg inherited) i-face)
  12789. (t (get-text-property (match-beginning 1) 'face))))))
  12790. (goto-char (point-min)))))
  12791. (org-detach-overlay org-tags-overlay)
  12792. (if rtn
  12793. (mapconcat 'identity current ":")
  12794. nil))))
  12795. (defun org-get-tags-string ()
  12796. "Get the TAGS string in the current headline."
  12797. (unless (org-at-heading-p t)
  12798. (error "Not on a heading"))
  12799. (save-excursion
  12800. (beginning-of-line 1)
  12801. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12802. (org-match-string-no-properties 1)
  12803. "")))
  12804. (defun org-get-tags ()
  12805. "Get the list of tags specified in the current headline."
  12806. (org-split-string (org-get-tags-string) ":"))
  12807. (defun org-get-buffer-tags ()
  12808. "Get a table of all tags used in the buffer, for completion."
  12809. (let (tags)
  12810. (save-excursion
  12811. (goto-char (point-min))
  12812. (while (re-search-forward
  12813. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12814. (when (equal (char-after (point-at-bol 0)) ?*)
  12815. (mapc (lambda (x) (add-to-list 'tags x))
  12816. (org-split-string (org-match-string-no-properties 1) ":")))))
  12817. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12818. (mapcar 'list tags)))
  12819. ;;;; The mapping API
  12820. (defun org-map-entries (func &optional match scope &rest skip)
  12821. "Call FUNC at each headline selected by MATCH in SCOPE.
  12822. FUNC is a function or a lisp form. The function will be called without
  12823. arguments, with the cursor positioned at the beginning of the headline.
  12824. The return values of all calls to the function will be collected and
  12825. returned as a list.
  12826. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12827. does not need to preserve point. After evaluation, the cursor will be
  12828. moved to the end of the line (presumably of the headline of the
  12829. processed entry) and search continues from there. Under some
  12830. circumstances, this may not produce the wanted results. For example,
  12831. if you have removed (e.g. archived) the current (sub)tree it could
  12832. mean that the next entry will be skipped entirely. In such cases, you
  12833. can specify the position from where search should continue by making
  12834. FUNC set the variable `org-map-continue-from' to the desired buffer
  12835. position.
  12836. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12837. Only headlines that are matched by this query will be considered during
  12838. the iteration. When MATCH is nil or t, all headlines will be
  12839. visited by the iteration.
  12840. SCOPE determines the scope of this command. It can be any of:
  12841. nil The current buffer, respecting the restriction if any
  12842. tree The subtree started with the entry at point
  12843. region The entries within the active region, if any
  12844. region-start-level
  12845. The entries within the active region, but only those at
  12846. the same level than the first one.
  12847. file The current buffer, without restriction
  12848. file-with-archives
  12849. The current buffer, and any archives associated with it
  12850. agenda All agenda files
  12851. agenda-with-archives
  12852. All agenda files with any archive files associated with them
  12853. \(file1 file2 ...)
  12854. If this is a list, all files in the list will be scanned
  12855. The remaining args are treated as settings for the skipping facilities of
  12856. the scanner. The following items can be given here:
  12857. archive skip trees with the archive tag.
  12858. comment skip trees with the COMMENT keyword
  12859. function or Emacs Lisp form:
  12860. will be used as value for `org-agenda-skip-function', so whenever
  12861. the function returns t, FUNC will not be called for that
  12862. entry and search will continue from the point where the
  12863. function leaves it.
  12864. If your function needs to retrieve the tags including inherited tags
  12865. at the *current* entry, you can use the value of the variable
  12866. `org-scanner-tags' which will be much faster than getting the value
  12867. with `org-get-tags-at'. If your function gets properties with
  12868. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12869. to t around the call to `org-entry-properties' to get the same speedup.
  12870. Note that if your function moves around to retrieve tags and properties at
  12871. a *different* entry, you cannot use these techniques."
  12872. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12873. (not (org-region-active-p)))
  12874. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12875. (org-agenda-skip-archived-trees (memq 'archive skip))
  12876. (org-agenda-skip-comment-trees (memq 'comment skip))
  12877. (org-agenda-skip-function
  12878. (car (org-delete-all '(comment archive) skip)))
  12879. (org-tags-match-list-sublevels t)
  12880. (start-level (eq scope 'region-start-level))
  12881. matcher file res
  12882. org-todo-keywords-for-agenda
  12883. org-done-keywords-for-agenda
  12884. org-todo-keyword-alist-for-agenda
  12885. org-drawers-for-agenda
  12886. org-tag-alist-for-agenda
  12887. todo-only)
  12888. (cond
  12889. ((eq match t) (setq matcher t))
  12890. ((eq match nil) (setq matcher t))
  12891. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12892. (save-excursion
  12893. (save-restriction
  12894. (cond ((eq scope 'tree)
  12895. (org-back-to-heading t)
  12896. (org-narrow-to-subtree)
  12897. (setq scope nil))
  12898. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12899. (org-region-active-p))
  12900. ;; If needed, set start-level to a string like "2"
  12901. (when start-level
  12902. (save-excursion
  12903. (goto-char (region-beginning))
  12904. (unless (org-at-heading-p) (outline-next-heading))
  12905. (setq start-level (org-current-level))))
  12906. (narrow-to-region (region-beginning)
  12907. (save-excursion
  12908. (goto-char (region-end))
  12909. (unless (and (bolp) (org-at-heading-p))
  12910. (outline-next-heading))
  12911. (point)))
  12912. (setq scope nil)))
  12913. (if (not scope)
  12914. (progn
  12915. (org-agenda-prepare-buffers
  12916. (list (buffer-file-name (current-buffer))))
  12917. (setq res (org-scan-tags func matcher todo-only start-level)))
  12918. ;; Get the right scope
  12919. (cond
  12920. ((and scope (listp scope) (symbolp (car scope)))
  12921. (setq scope (eval scope)))
  12922. ((eq scope 'agenda)
  12923. (setq scope (org-agenda-files t)))
  12924. ((eq scope 'agenda-with-archives)
  12925. (setq scope (org-agenda-files t))
  12926. (setq scope (org-add-archive-files scope)))
  12927. ((eq scope 'file)
  12928. (setq scope (list (buffer-file-name))))
  12929. ((eq scope 'file-with-archives)
  12930. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12931. (org-agenda-prepare-buffers scope)
  12932. (while (setq file (pop scope))
  12933. (with-current-buffer (org-find-base-buffer-visiting file)
  12934. (save-excursion
  12935. (save-restriction
  12936. (widen)
  12937. (goto-char (point-min))
  12938. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12939. res)))
  12940. ;;;; Properties
  12941. ;;; Setting and retrieving properties
  12942. (defconst org-special-properties
  12943. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12944. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  12945. "The special properties valid in Org-mode.
  12946. These are properties that are not defined in the property drawer,
  12947. but in some other way.")
  12948. (defconst org-default-properties
  12949. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12950. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12951. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12952. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12953. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12954. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12955. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12956. "Some properties that are used by Org-mode for various purposes.
  12957. Being in this list makes sure that they are offered for completion.")
  12958. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12959. "Regular expression matching the first line of a property drawer.")
  12960. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12961. "Regular expression matching the last line of a property drawer.")
  12962. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12963. "Regular expression matching the first line of a property drawer.")
  12964. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12965. "Regular expression matching the first line of a property drawer.")
  12966. (defconst org-property-drawer-re
  12967. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12968. org-property-end-re "\\)\n?")
  12969. "Matches an entire property drawer.")
  12970. (defconst org-clock-drawer-re
  12971. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12972. org-property-end-re "\\)\n?")
  12973. "Matches an entire clock drawer.")
  12974. (defsubst org-re-property (property)
  12975. "Return a regexp matching a PROPERTY line.
  12976. Match group 1 will be set to the value."
  12977. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12978. (defsubst org-re-property-keyword (property)
  12979. "Return a regexp matching a PROPERTY line, possibly with no
  12980. value for the property."
  12981. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  12982. (defun org-property-action ()
  12983. "Do an action on properties."
  12984. (interactive)
  12985. (let (c)
  12986. (org-at-property-p)
  12987. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12988. (setq c (read-char-exclusive))
  12989. (cond
  12990. ((equal c ?s)
  12991. (call-interactively 'org-set-property))
  12992. ((equal c ?d)
  12993. (call-interactively 'org-delete-property))
  12994. ((equal c ?D)
  12995. (call-interactively 'org-delete-property-globally))
  12996. ((equal c ?c)
  12997. (call-interactively 'org-compute-property-at-point))
  12998. (t (error "No such property action %c" c)))))
  12999. (defun org-inc-effort ()
  13000. "Increment the value of the effort property in the current entry."
  13001. (interactive)
  13002. (org-set-effort nil t))
  13003. (defun org-set-effort (&optional value increment)
  13004. "Set the effort property of the current entry.
  13005. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13006. 10th allowed value.
  13007. When INCREMENT is non-nil, set the property to the next allowed value."
  13008. (interactive "P")
  13009. (if (equal value 0) (setq value 10))
  13010. (let* ((completion-ignore-case t)
  13011. (prop org-effort-property)
  13012. (cur (org-entry-get nil prop))
  13013. (allowed (org-property-get-allowed-values nil prop 'table))
  13014. (existing (mapcar 'list (org-property-values prop)))
  13015. rpl
  13016. (val (cond
  13017. ((stringp value) value)
  13018. ((and allowed (integerp value))
  13019. (or (car (nth (1- value) allowed))
  13020. (car (org-last allowed))))
  13021. ((and allowed increment)
  13022. (or (caadr (member (list cur) allowed))
  13023. (error "Allowed effort values are not set")))
  13024. (allowed
  13025. (message "Select 1-9,0, [RET%s]: %s"
  13026. (if cur (concat "=" cur) "")
  13027. (mapconcat 'car allowed " "))
  13028. (setq rpl (read-char-exclusive))
  13029. (if (equal rpl ?\r)
  13030. cur
  13031. (setq rpl (- rpl ?0))
  13032. (if (equal rpl 0) (setq rpl 10))
  13033. (if (and (> rpl 0) (<= rpl (length allowed)))
  13034. (car (nth (1- rpl) allowed))
  13035. (org-completing-read "Effort: " allowed nil))))
  13036. (t
  13037. (let (org-completion-use-ido org-completion-use-iswitchb)
  13038. (org-completing-read
  13039. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13040. (concat "[" cur "]") "")
  13041. ": ")
  13042. existing nil nil "" nil cur))))))
  13043. (unless (equal (org-entry-get nil prop) val)
  13044. (org-entry-put nil prop val))
  13045. (message "%s is now %s" prop val)))
  13046. (defun org-at-property-p ()
  13047. "Is cursor inside a property drawer?"
  13048. (save-excursion
  13049. (beginning-of-line 1)
  13050. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13051. (save-match-data ;; Used by calling procedures
  13052. (let ((p (point))
  13053. (range (unless (org-before-first-heading-p)
  13054. (org-get-property-block))))
  13055. (and range (<= (car range) p) (< p (cdr range))))))))
  13056. (defun org-get-property-block (&optional beg end force)
  13057. "Return the (beg . end) range of the body of the property drawer.
  13058. BEG and END are the beginning and end of the current subtree, or of
  13059. the part before the first headline. If they are not given, they will
  13060. be found. If the drawer does not exist and FORCE is non-nil, create
  13061. the drawer."
  13062. (catch 'exit
  13063. (save-excursion
  13064. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13065. (progn (org-back-to-heading t) (point))))
  13066. (end (or end (and (not (outline-next-heading)) (point-max))
  13067. (point))))
  13068. (goto-char beg)
  13069. (if (re-search-forward org-property-start-re end t)
  13070. (setq beg (1+ (match-end 0)))
  13071. (if force
  13072. (save-excursion
  13073. (org-insert-property-drawer)
  13074. (setq end (progn (outline-next-heading) (point))))
  13075. (throw 'exit nil))
  13076. (goto-char beg)
  13077. (if (re-search-forward org-property-start-re end t)
  13078. (setq beg (1+ (match-end 0)))))
  13079. (if (re-search-forward org-property-end-re end t)
  13080. (setq end (match-beginning 0))
  13081. (or force (throw 'exit nil))
  13082. (goto-char beg)
  13083. (setq end beg)
  13084. (org-indent-line)
  13085. (insert ":END:\n"))
  13086. (cons beg end)))))
  13087. (defun org-entry-properties (&optional pom which specific)
  13088. "Get all properties of the entry at point-or-marker POM.
  13089. This includes the TODO keyword, the tags, time strings for deadline,
  13090. scheduled, and clocking, and any additional properties defined in the
  13091. entry. The return value is an alist, keys may occur multiple times
  13092. if the property key was used several times.
  13093. POM may also be nil, in which case the current entry is used.
  13094. If WHICH is nil or `all', get all properties. If WHICH is
  13095. `special' or `standard', only get that subclass. If WHICH
  13096. is a string only get exactly this property. SPECIFIC can be a string, the
  13097. specific property we are interested in. Specifying it can speed
  13098. things up because then unnecessary parsing is avoided."
  13099. (setq which (or which 'all))
  13100. (org-with-point-at pom
  13101. (let ((clockstr (substring org-clock-string 0 -1))
  13102. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13103. (case-fold-search nil)
  13104. beg end range props sum-props key key1 value string clocksum clocksumt)
  13105. (save-excursion
  13106. (when (condition-case nil
  13107. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13108. (error nil))
  13109. (setq beg (point))
  13110. (setq sum-props (get-text-property (point) 'org-summaries))
  13111. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13112. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13113. (outline-next-heading)
  13114. (setq end (point))
  13115. (when (memq which '(all special))
  13116. ;; Get the special properties, like TODO and tags
  13117. (goto-char beg)
  13118. (when (and (or (not specific) (string= specific "TODO"))
  13119. (looking-at org-todo-line-regexp) (match-end 2))
  13120. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13121. (when (and (or (not specific) (string= specific "PRIORITY"))
  13122. (looking-at org-priority-regexp))
  13123. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13124. (when (or (not specific) (string= specific "FILE"))
  13125. (push (cons "FILE" buffer-file-name) props))
  13126. (when (and (or (not specific) (string= specific "TAGS"))
  13127. (setq value (org-get-tags-string))
  13128. (string-match "\\S-" value))
  13129. (push (cons "TAGS" value) props))
  13130. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13131. (setq value (org-get-tags-at)))
  13132. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13133. ":"))
  13134. props))
  13135. (when (or (not specific) (string= specific "BLOCKED"))
  13136. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13137. (when (or (not specific)
  13138. (member specific
  13139. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13140. "TIMESTAMP" "TIMESTAMP_IA")))
  13141. (catch 'match
  13142. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13143. (setq key (if (match-end 1)
  13144. (substring (org-match-string-no-properties 1)
  13145. 0 -1))
  13146. string (if (equal key clockstr)
  13147. (org-trim
  13148. (buffer-substring-no-properties
  13149. (match-beginning 3) (goto-char
  13150. (point-at-eol))))
  13151. (substring (org-match-string-no-properties 3)
  13152. 1 -1)))
  13153. ;; Get the correct property name from the key. This is
  13154. ;; necessary if the user has configured time keywords.
  13155. (setq key1 (concat key ":"))
  13156. (cond
  13157. ((not key)
  13158. (setq key
  13159. (if (= (char-after (match-beginning 3)) ?\[)
  13160. "TIMESTAMP_IA" "TIMESTAMP")))
  13161. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13162. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13163. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13164. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13165. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13166. (progn
  13167. (push (cons key string) props)
  13168. ;; no need to search further if match is found
  13169. (throw 'match t))
  13170. (when (or (equal key "CLOCK") (not (assoc key props)))
  13171. (push (cons key string) props)))))))
  13172. (when (memq which '(all standard))
  13173. ;; Get the standard properties, like :PROP: ...
  13174. (setq range (org-get-property-block beg end))
  13175. (when range
  13176. (goto-char (car range))
  13177. (while (re-search-forward
  13178. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13179. (cdr range) t)
  13180. (setq key (org-match-string-no-properties 1)
  13181. value (org-trim (or (org-match-string-no-properties 2) "")))
  13182. (unless (member key excluded)
  13183. (push (cons key (or value "")) props)))))
  13184. (if clocksum
  13185. (push (cons "CLOCKSUM"
  13186. (org-columns-number-to-string (/ (float clocksum) 60.)
  13187. 'add_times))
  13188. props))
  13189. (if clocksumt
  13190. (push (cons "CLOCKSUM_T"
  13191. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13192. 'add_times))
  13193. props))
  13194. (unless (assoc "CATEGORY" props)
  13195. (push (cons "CATEGORY" (org-get-category)) props))
  13196. (append sum-props (nreverse props)))))))
  13197. (defun org-entry-get (pom property &optional inherit literal-nil)
  13198. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13199. If INHERIT is non-nil and the entry does not have the property,
  13200. then also check higher levels of the hierarchy.
  13201. If INHERIT is the symbol `selective', use inheritance only if the setting
  13202. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13203. If the property is present but empty, the return value is the empty string.
  13204. If the property is not present at all, nil is returned.
  13205. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13206. do not interpret it as the list atom nil. This is used for inheritance
  13207. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13208. (org-with-point-at pom
  13209. (if (and inherit (if (eq inherit 'selective)
  13210. (org-property-inherit-p property)
  13211. t))
  13212. (org-entry-get-with-inheritance property literal-nil)
  13213. (if (member property org-special-properties)
  13214. ;; We need a special property. Use `org-entry-properties' to
  13215. ;; retrieve it, but specify the wanted property
  13216. (cdr (assoc property (org-entry-properties nil 'special property)))
  13217. (let* ((range (org-get-property-block))
  13218. (props (list (or (assoc property org-file-properties)
  13219. (assoc property org-global-properties)
  13220. (assoc property org-global-properties-fixed))))
  13221. (ap (lambda (key)
  13222. (when (re-search-forward
  13223. (org-re-property key) (cdr range) t)
  13224. (setq props
  13225. (org-update-property-plist
  13226. key
  13227. (if (match-end 1)
  13228. (org-match-string-no-properties 1) "")
  13229. props)))))
  13230. val)
  13231. (when (and range (goto-char (car range)))
  13232. (funcall ap property)
  13233. (goto-char (car range))
  13234. (while (funcall ap (concat property "+")))
  13235. (setq val (cdr (assoc property props)))
  13236. (when val (if literal-nil val (org-not-nil val)))))))))
  13237. (defun org-property-or-variable-value (var &optional inherit)
  13238. "Check if there is a property fixing the value of VAR.
  13239. If yes, return this value. If not, return the current value of the variable."
  13240. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13241. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13242. (read prop)
  13243. (symbol-value var))))
  13244. (defun org-entry-delete (pom property)
  13245. "Delete the property PROPERTY from entry at point-or-marker POM."
  13246. (org-with-point-at pom
  13247. (if (member property org-special-properties)
  13248. nil ; cannot delete these properties.
  13249. (let ((range (org-get-property-block)))
  13250. (if (and range
  13251. (goto-char (car range))
  13252. (re-search-forward
  13253. (org-re-property property)
  13254. (cdr range) t))
  13255. (progn
  13256. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13257. t)
  13258. nil)))))
  13259. ;; Multi-values properties are properties that contain multiple values
  13260. ;; These values are assumed to be single words, separated by whitespace.
  13261. (defun org-entry-add-to-multivalued-property (pom property value)
  13262. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13263. (let* ((old (org-entry-get pom property))
  13264. (values (and old (org-split-string old "[ \t]"))))
  13265. (setq value (org-entry-protect-space value))
  13266. (unless (member value values)
  13267. (setq values (cons value values))
  13268. (org-entry-put pom property
  13269. (mapconcat 'identity values " ")))))
  13270. (defun org-entry-remove-from-multivalued-property (pom property value)
  13271. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13272. (let* ((old (org-entry-get pom property))
  13273. (values (and old (org-split-string old "[ \t]"))))
  13274. (setq value (org-entry-protect-space value))
  13275. (when (member value values)
  13276. (setq values (delete value values))
  13277. (org-entry-put pom property
  13278. (mapconcat 'identity values " ")))))
  13279. (defun org-entry-member-in-multivalued-property (pom property value)
  13280. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13281. (let* ((old (org-entry-get pom property))
  13282. (values (and old (org-split-string old "[ \t]"))))
  13283. (setq value (org-entry-protect-space value))
  13284. (member value values)))
  13285. (defun org-entry-get-multivalued-property (pom property)
  13286. "Return a list of values in a multivalued property."
  13287. (let* ((value (org-entry-get pom property))
  13288. (values (and value (org-split-string value "[ \t]"))))
  13289. (mapcar 'org-entry-restore-space values)))
  13290. (defun org-entry-put-multivalued-property (pom property &rest values)
  13291. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13292. VALUES should be a list of strings. Spaces will be protected."
  13293. (org-entry-put pom property
  13294. (mapconcat 'org-entry-protect-space values " "))
  13295. (let* ((value (org-entry-get pom property))
  13296. (values (and value (org-split-string value "[ \t]"))))
  13297. (mapcar 'org-entry-restore-space values)))
  13298. (defun org-entry-protect-space (s)
  13299. "Protect spaces and newline in string S."
  13300. (while (string-match " " s)
  13301. (setq s (replace-match "%20" t t s)))
  13302. (while (string-match "\n" s)
  13303. (setq s (replace-match "%0A" t t s)))
  13304. s)
  13305. (defun org-entry-restore-space (s)
  13306. "Restore spaces and newline in string S."
  13307. (while (string-match "%20" s)
  13308. (setq s (replace-match " " t t s)))
  13309. (while (string-match "%0A" s)
  13310. (setq s (replace-match "\n" t t s)))
  13311. s)
  13312. (defvar org-entry-property-inherited-from (make-marker)
  13313. "Marker pointing to the entry from where a property was inherited.
  13314. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13315. location of the entry where the inheritance search matched. If there was
  13316. no match, the marker will point nowhere.
  13317. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13318. is set.")
  13319. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13320. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13321. The search will stop at the first ancestor which has the property defined.
  13322. If the value found is \"nil\", return nil to show that the property
  13323. should be considered as undefined (this is the meaning of nil here).
  13324. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13325. (move-marker org-entry-property-inherited-from nil)
  13326. (let (tmp)
  13327. (save-excursion
  13328. (save-restriction
  13329. (widen)
  13330. (catch 'ex
  13331. (while t
  13332. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13333. (or (ignore-errors (org-back-to-heading t))
  13334. (goto-char (point-min)))
  13335. (move-marker org-entry-property-inherited-from (point))
  13336. (throw 'ex tmp))
  13337. (or (ignore-errors (org-up-heading-safe))
  13338. (throw 'ex nil))))))
  13339. (setq tmp (or tmp
  13340. (cdr (assoc property org-file-properties))
  13341. (cdr (assoc property org-global-properties))
  13342. (cdr (assoc property org-global-properties-fixed))))
  13343. (if literal-nil tmp (org-not-nil tmp))))
  13344. (defvar org-property-changed-functions nil
  13345. "Hook called when the value of a property has changed.
  13346. Each hook function should accept two arguments, the name of the property
  13347. and the new value.")
  13348. (defun org-entry-put (pom property value)
  13349. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13350. (org-with-point-at pom
  13351. (org-back-to-heading t)
  13352. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13353. range)
  13354. (cond
  13355. ((equal property "TODO")
  13356. (when (and (stringp value) (string-match "\\S-" value)
  13357. (not (member value org-todo-keywords-1)))
  13358. (error "\"%s\" is not a valid TODO state" value))
  13359. (if (or (not value)
  13360. (not (string-match "\\S-" value)))
  13361. (setq value 'none))
  13362. (org-todo value)
  13363. (org-set-tags nil 'align))
  13364. ((equal property "PRIORITY")
  13365. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13366. (string-to-char value) ?\ ))
  13367. (org-set-tags nil 'align))
  13368. ((equal property "SCHEDULED")
  13369. (if (re-search-forward org-scheduled-time-regexp end t)
  13370. (cond
  13371. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13372. ((eq value 'later) (org-timestamp-change 1 'day))
  13373. (t (call-interactively 'org-schedule)))
  13374. (call-interactively 'org-schedule)))
  13375. ((equal property "DEADLINE")
  13376. (if (re-search-forward org-deadline-time-regexp end t)
  13377. (cond
  13378. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13379. ((eq value 'later) (org-timestamp-change 1 'day))
  13380. (t (call-interactively 'org-deadline)))
  13381. (call-interactively 'org-deadline)))
  13382. ((member property org-special-properties)
  13383. (error "The %s property can not yet be set with `org-entry-put'"
  13384. property))
  13385. (t ; a non-special property
  13386. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13387. (setq range (org-get-property-block beg end 'force))
  13388. (goto-char (car range))
  13389. (if (re-search-forward
  13390. (org-re-property-keyword property) (cdr range) t)
  13391. (progn
  13392. (delete-region (match-beginning 0) (match-end 0))
  13393. (goto-char (match-beginning 0)))
  13394. (goto-char (cdr range))
  13395. (insert "\n")
  13396. (backward-char 1)
  13397. (org-indent-line))
  13398. (insert ":" property ":")
  13399. (and value (insert " " value))
  13400. (org-indent-line)))))
  13401. (run-hook-with-args 'org-property-changed-functions property value)))
  13402. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13403. "Get all property keys in the current buffer.
  13404. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13405. like tags and TODO state.
  13406. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13407. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13408. and others.
  13409. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13410. formats in the current buffer."
  13411. (let (rtn range cfmt s p)
  13412. (save-excursion
  13413. (save-restriction
  13414. (widen)
  13415. (goto-char (point-min))
  13416. (while (re-search-forward org-property-start-re nil t)
  13417. (setq range (org-get-property-block))
  13418. (goto-char (car range))
  13419. (while (re-search-forward
  13420. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13421. (cdr range) t)
  13422. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13423. (outline-next-heading))))
  13424. (when include-specials
  13425. (setq rtn (append org-special-properties rtn)))
  13426. (when include-defaults
  13427. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13428. (add-to-list 'rtn org-effort-property))
  13429. (when include-columns
  13430. (save-excursion
  13431. (save-restriction
  13432. (widen)
  13433. (goto-char (point-min))
  13434. (while (re-search-forward
  13435. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13436. nil t)
  13437. (setq cfmt (match-string 2) s 0)
  13438. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13439. cfmt s)
  13440. (setq s (match-end 0)
  13441. p (match-string 1 cfmt))
  13442. (unless (or (equal p "ITEM")
  13443. (member p org-special-properties))
  13444. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13445. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13446. (defun org-property-values (key)
  13447. "Return a list of all values of property KEY in the current buffer."
  13448. (save-excursion
  13449. (save-restriction
  13450. (widen)
  13451. (goto-char (point-min))
  13452. (let ((re (org-re-property key))
  13453. values)
  13454. (while (re-search-forward re nil t)
  13455. (add-to-list 'values (org-trim (match-string 1))))
  13456. (delete "" values)))))
  13457. (defun org-insert-property-drawer ()
  13458. "Insert a property drawer into the current entry."
  13459. (org-back-to-heading t)
  13460. (looking-at org-outline-regexp)
  13461. (let ((indent (if org-adapt-indentation
  13462. (- (match-end 0) (match-beginning 0))
  13463. 0))
  13464. (beg (point))
  13465. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13466. end hiddenp)
  13467. (outline-next-heading)
  13468. (setq end (point))
  13469. (goto-char beg)
  13470. (while (re-search-forward re end t))
  13471. (setq hiddenp (outline-invisible-p))
  13472. (end-of-line 1)
  13473. (and (equal (char-after) ?\n) (forward-char 1))
  13474. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13475. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13476. ;; just skip this line
  13477. (beginning-of-line 2)
  13478. ;; Drawer start, find the end
  13479. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13480. (beginning-of-line 1)))
  13481. (org-skip-over-state-notes)
  13482. (skip-chars-backward " \t\n\r")
  13483. (if (eq (char-before) ?*) (forward-char 1))
  13484. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13485. (beginning-of-line 0)
  13486. (org-indent-to-column indent)
  13487. (beginning-of-line 2)
  13488. (org-indent-to-column indent)
  13489. (beginning-of-line 0)
  13490. (if hiddenp
  13491. (save-excursion
  13492. (org-back-to-heading t)
  13493. (hide-entry))
  13494. (org-flag-drawer t))))
  13495. (defun org-insert-drawer (&optional arg drawer)
  13496. "Insert a drawer at point.
  13497. Optional argument DRAWER, when non-nil, is a string representing
  13498. drawer's name. Otherwise, the user is prompted for a name.
  13499. If a region is active, insert the drawer around that region
  13500. instead.
  13501. Point is left between drawer's boundaries."
  13502. (interactive "P")
  13503. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13504. "LOGBOOK"))
  13505. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13506. ;; internally by Org. They are meant to be inserted
  13507. ;; automatically.
  13508. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13509. ;; Remove system drawers from list. Note: For some reason,
  13510. ;; `org-completing-read' ignores the predicate while
  13511. ;; `completing-read' handles it fine.
  13512. (drawer (if arg "PROPERTIES"
  13513. (or drawer
  13514. (completing-read
  13515. "Drawer: " org-drawers
  13516. (lambda (d) (not (member d system-drawers))))))))
  13517. (cond
  13518. ;; With C-u, fall back on `org-insert-property-drawer'
  13519. (arg (org-insert-property-drawer))
  13520. ;; With an active region, insert a drawer at point.
  13521. ((not (org-region-active-p))
  13522. (progn
  13523. (unless (bolp) (insert "\n"))
  13524. (insert (format ":%s:\n\n:END:\n" drawer))
  13525. (forward-line -2)))
  13526. ;; Otherwise, insert the drawer at point
  13527. (t
  13528. (let ((rbeg (region-beginning))
  13529. (rend (copy-marker (region-end))))
  13530. (unwind-protect
  13531. (progn
  13532. (goto-char rbeg)
  13533. (beginning-of-line)
  13534. (when (save-excursion
  13535. (re-search-forward org-outline-regexp-bol rend t))
  13536. (error "Drawers cannot contain headlines"))
  13537. ;; Position point at the beginning of the first
  13538. ;; non-blank line in region. Insert drawer's opening
  13539. ;; there, then indent it.
  13540. (org-skip-whitespace)
  13541. (beginning-of-line)
  13542. (insert ":" drawer ":\n")
  13543. (forward-line -1)
  13544. (indent-for-tab-command)
  13545. ;; Move point to the beginning of the first blank line
  13546. ;; after the last non-blank line in region. Insert
  13547. ;; drawer's closing, then indent it.
  13548. (goto-char rend)
  13549. (skip-chars-backward " \r\t\n")
  13550. (insert "\n:END:")
  13551. (deactivate-mark t)
  13552. (indent-for-tab-command)
  13553. (unless (eolp) (insert "\n")))
  13554. ;; Clear marker, whatever the outcome of insertion is.
  13555. (set-marker rend nil)))))))
  13556. (defvar org-property-set-functions-alist nil
  13557. "Property set function alist.
  13558. Each entry should have the following format:
  13559. (PROPERTY . READ-FUNCTION)
  13560. The read function will be called with the same argument as
  13561. `org-completing-read'.")
  13562. (defun org-set-property-function (property)
  13563. "Get the function that should be used to set PROPERTY.
  13564. This is computed according to `org-property-set-functions-alist'."
  13565. (or (cdr (assoc property org-property-set-functions-alist))
  13566. 'org-completing-read))
  13567. (defun org-read-property-value (property)
  13568. "Read PROPERTY value from user."
  13569. (let* ((completion-ignore-case t)
  13570. (allowed (org-property-get-allowed-values nil property 'table))
  13571. (cur (org-entry-get nil property))
  13572. (prompt (concat property " value"
  13573. (if (and cur (string-match "\\S-" cur))
  13574. (concat " [" cur "]") "") ": "))
  13575. (set-function (org-set-property-function property))
  13576. (val (if allowed
  13577. (funcall set-function prompt allowed nil
  13578. (not (get-text-property 0 'org-unrestricted
  13579. (caar allowed))))
  13580. (let (org-completion-use-ido org-completion-use-iswitchb)
  13581. (funcall set-function prompt
  13582. (mapcar 'list (org-property-values property))
  13583. nil nil "" nil cur)))))
  13584. (if (equal val "")
  13585. cur
  13586. val)))
  13587. (defvar org-last-set-property nil)
  13588. (defun org-read-property-name ()
  13589. "Read a property name."
  13590. (let* ((completion-ignore-case t)
  13591. (keys (org-buffer-property-keys nil t t))
  13592. (default-prop (or (save-excursion
  13593. (save-match-data
  13594. (beginning-of-line)
  13595. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13596. (null (string= (match-string 1) "END"))
  13597. (match-string 1))))
  13598. org-last-set-property))
  13599. (property (org-icompleting-read
  13600. (concat "Property"
  13601. (if default-prop (concat " [" default-prop "]") "")
  13602. ": ")
  13603. (mapcar 'list keys)
  13604. nil nil nil nil
  13605. default-prop
  13606. )))
  13607. (if (member property keys)
  13608. property
  13609. (or (cdr (assoc (downcase property)
  13610. (mapcar (lambda (x) (cons (downcase x) x))
  13611. keys)))
  13612. property))))
  13613. (defun org-set-property (property value)
  13614. "In the current entry, set PROPERTY to VALUE.
  13615. When called interactively, this will prompt for a property name, offering
  13616. completion on existing and default properties. And then it will prompt
  13617. for a value, offering completion either on allowed values (via an inherited
  13618. xxx_ALL property) or on existing values in other instances of this property
  13619. in the current file."
  13620. (interactive (list nil nil))
  13621. (let* ((property (or property (org-read-property-name)))
  13622. (value (or value (org-read-property-value property)))
  13623. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13624. (setq org-last-set-property property)
  13625. ;; Possibly postprocess the inserted value:
  13626. (when fn (setq value (funcall fn value)))
  13627. (unless (equal (org-entry-get nil property) value)
  13628. (org-entry-put nil property value))))
  13629. (defun org-delete-property (property)
  13630. "In the current entry, delete PROPERTY."
  13631. (interactive
  13632. (let* ((completion-ignore-case t)
  13633. (prop (org-icompleting-read "Property: "
  13634. (org-entry-properties nil 'standard))))
  13635. (list prop)))
  13636. (message "Property %s %s" property
  13637. (if (org-entry-delete nil property)
  13638. "deleted"
  13639. "was not present in the entry")))
  13640. (defun org-delete-property-globally (property)
  13641. "Remove PROPERTY globally, from all entries."
  13642. (interactive
  13643. (let* ((completion-ignore-case t)
  13644. (prop (org-icompleting-read
  13645. "Globally remove property: "
  13646. (mapcar 'list (org-buffer-property-keys)))))
  13647. (list prop)))
  13648. (save-excursion
  13649. (save-restriction
  13650. (widen)
  13651. (goto-char (point-min))
  13652. (let ((cnt 0))
  13653. (while (re-search-forward
  13654. (org-re-property property)
  13655. nil t)
  13656. (setq cnt (1+ cnt))
  13657. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13658. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13659. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13660. (defun org-compute-property-at-point ()
  13661. "Compute the property at point.
  13662. This looks for an enclosing column format, extracts the operator and
  13663. then applies it to the property in the column format's scope."
  13664. (interactive)
  13665. (unless (org-at-property-p)
  13666. (error "Not at a property"))
  13667. (let ((prop (org-match-string-no-properties 2)))
  13668. (org-columns-get-format-and-top-level)
  13669. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13670. (error "No operator defined for property %s" prop))
  13671. (org-columns-compute prop)))
  13672. (defvar org-property-allowed-value-functions nil
  13673. "Hook for functions supplying allowed values for a specific property.
  13674. The functions must take a single argument, the name of the property, and
  13675. return a flat list of allowed values. If \":ETC\" is one of
  13676. the values, this means that these values are intended as defaults for
  13677. completion, but that other values should be allowed too.
  13678. The functions must return nil if they are not responsible for this
  13679. property.")
  13680. (defun org-property-get-allowed-values (pom property &optional table)
  13681. "Get allowed values for the property PROPERTY.
  13682. When TABLE is non-nil, return an alist that can directly be used for
  13683. completion."
  13684. (let (vals)
  13685. (cond
  13686. ((equal property "TODO")
  13687. (setq vals (org-with-point-at pom
  13688. (append org-todo-keywords-1 '("")))))
  13689. ((equal property "PRIORITY")
  13690. (let ((n org-lowest-priority))
  13691. (while (>= n org-highest-priority)
  13692. (push (char-to-string n) vals)
  13693. (setq n (1- n)))))
  13694. ((member property org-special-properties))
  13695. ((setq vals (run-hook-with-args-until-success
  13696. 'org-property-allowed-value-functions property)))
  13697. (t
  13698. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13699. (when (and vals (string-match "\\S-" vals))
  13700. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13701. (setq vals (mapcar (lambda (x)
  13702. (cond ((stringp x) x)
  13703. ((numberp x) (number-to-string x))
  13704. ((symbolp x) (symbol-name x))
  13705. (t "???")))
  13706. vals)))))
  13707. (when (member ":ETC" vals)
  13708. (setq vals (remove ":ETC" vals))
  13709. (org-add-props (car vals) '(org-unrestricted t)))
  13710. (if table (mapcar 'list vals) vals)))
  13711. (defun org-property-previous-allowed-value (&optional previous)
  13712. "Switch to the next allowed value for this property."
  13713. (interactive)
  13714. (org-property-next-allowed-value t))
  13715. (defun org-property-next-allowed-value (&optional previous)
  13716. "Switch to the next allowed value for this property."
  13717. (interactive)
  13718. (unless (org-at-property-p)
  13719. (error "Not at a property"))
  13720. (let* ((key (match-string 2))
  13721. (value (match-string 3))
  13722. (allowed (or (org-property-get-allowed-values (point) key)
  13723. (and (member value '("[ ]" "[-]" "[X]"))
  13724. '("[ ]" "[X]"))))
  13725. nval)
  13726. (unless allowed
  13727. (error "Allowed values for this property have not been defined"))
  13728. (if previous (setq allowed (reverse allowed)))
  13729. (if (member value allowed)
  13730. (setq nval (car (cdr (member value allowed)))))
  13731. (setq nval (or nval (car allowed)))
  13732. (if (equal nval value)
  13733. (error "Only one allowed value for this property"))
  13734. (org-at-property-p)
  13735. (replace-match (concat " :" key ": " nval) t t)
  13736. (org-indent-line)
  13737. (beginning-of-line 1)
  13738. (skip-chars-forward " \t")
  13739. (run-hook-with-args 'org-property-changed-functions key nval)))
  13740. (defun org-find-olp (path &optional this-buffer)
  13741. "Return a marker pointing to the entry at outline path OLP.
  13742. If anything goes wrong, throw an error.
  13743. You can wrap this call to catch the error like this:
  13744. (condition-case msg
  13745. (org-mobile-locate-entry (match-string 4))
  13746. (error (nth 1 msg)))
  13747. The return value will then be either a string with the error message,
  13748. or a marker if everything is OK.
  13749. If THIS-BUFFER is set, the outline path does not contain a file,
  13750. only headings."
  13751. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13752. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13753. (level 1)
  13754. (lmin 1)
  13755. (lmax 1)
  13756. limit re end found pos heading cnt flevel)
  13757. (unless buffer (error "File not found :%s" file))
  13758. (with-current-buffer buffer
  13759. (save-excursion
  13760. (save-restriction
  13761. (widen)
  13762. (setq limit (point-max))
  13763. (goto-char (point-min))
  13764. (while (setq heading (pop path))
  13765. (setq re (format org-complex-heading-regexp-format
  13766. (regexp-quote heading)))
  13767. (setq cnt 0 pos (point))
  13768. (while (re-search-forward re end t)
  13769. (setq level (- (match-end 1) (match-beginning 1)))
  13770. (if (and (>= level lmin) (<= level lmax))
  13771. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13772. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13773. lmax heading))
  13774. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13775. lmax heading))
  13776. (goto-char found)
  13777. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13778. (setq end (save-excursion (org-end-of-subtree t t))))
  13779. (when (org-at-heading-p)
  13780. (point-marker)))))))
  13781. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13782. "Find node HEADING in BUFFER.
  13783. Return a marker to the heading if it was found, or nil if not.
  13784. If POS-ONLY is set, return just the position instead of a marker.
  13785. The heading text must match exact, but it may have a TODO keyword,
  13786. a priority cookie and tags in the standard locations."
  13787. (with-current-buffer (or buffer (current-buffer))
  13788. (save-excursion
  13789. (save-restriction
  13790. (widen)
  13791. (goto-char (point-min))
  13792. (let (case-fold-search)
  13793. (if (re-search-forward
  13794. (format org-complex-heading-regexp-format
  13795. (regexp-quote heading)) nil t)
  13796. (if pos-only
  13797. (match-beginning 0)
  13798. (move-marker (make-marker) (match-beginning 0)))))))))
  13799. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13800. "Find Org node headline HEADING in all .org files in directory DIR.
  13801. When the target headline is found, return a marker to this location."
  13802. (let ((files (directory-files (or dir default-directory)
  13803. nil "\\`[^.#].*\\.org\\'"))
  13804. file visiting m buffer)
  13805. (catch 'found
  13806. (while (setq file (pop files))
  13807. (message "trying %s" file)
  13808. (setq visiting (org-find-base-buffer-visiting file))
  13809. (setq buffer (or visiting (find-file-noselect file)))
  13810. (setq m (org-find-exact-headline-in-buffer
  13811. heading buffer))
  13812. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13813. (and m (throw 'found m))))))
  13814. (defun org-find-entry-with-id (ident)
  13815. "Locate the entry that contains the ID property with exact value IDENT.
  13816. IDENT can be a string, a symbol or a number, this function will search for
  13817. the string representation of it.
  13818. Return the position where this entry starts, or nil if there is no such entry."
  13819. (interactive "sID: ")
  13820. (let ((id (cond
  13821. ((stringp ident) ident)
  13822. ((symbol-name ident) (symbol-name ident))
  13823. ((numberp ident) (number-to-string ident))
  13824. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13825. (case-fold-search nil))
  13826. (save-excursion
  13827. (save-restriction
  13828. (widen)
  13829. (goto-char (point-min))
  13830. (when (re-search-forward
  13831. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13832. nil t)
  13833. (org-back-to-heading t)
  13834. (point))))))
  13835. ;;;; Timestamps
  13836. (defvar org-last-changed-timestamp nil)
  13837. (defvar org-last-inserted-timestamp nil
  13838. "The last time stamp inserted with `org-insert-time-stamp'.")
  13839. (defvar org-time-was-given) ; dynamically scoped parameter
  13840. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13841. (defvar org-ts-what) ; dynamically scoped parameter
  13842. (defun org-time-stamp (arg &optional inactive)
  13843. "Prompt for a date/time and insert a time stamp.
  13844. If the user specifies a time like HH:MM or if this command is
  13845. called with at least one prefix argument, the time stamp contains
  13846. the date and the time. Otherwise, only the date is be included.
  13847. All parts of a date not specified by the user is filled in from
  13848. the current date/time. So if you just press return without
  13849. typing anything, the time stamp will represent the current
  13850. date/time.
  13851. If there is already a timestamp at the cursor, it will be
  13852. modified.
  13853. With two universal prefix arguments, insert an active timestamp
  13854. with the current time without prompting the user."
  13855. (interactive "P")
  13856. (let* ((ts nil)
  13857. (default-time
  13858. ;; Default time is either today, or, when entering a range,
  13859. ;; the range start.
  13860. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13861. (save-excursion
  13862. (re-search-backward
  13863. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13864. (- (point) 20) t)))
  13865. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13866. (current-time)))
  13867. (default-input (and ts (org-get-compact-tod ts)))
  13868. (repeater (save-excursion
  13869. (save-match-data
  13870. (beginning-of-line)
  13871. (when (re-search-forward
  13872. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13873. (save-excursion (progn (end-of-line) (point))) t)
  13874. (match-string 0)))))
  13875. org-time-was-given org-end-time-was-given time)
  13876. (cond
  13877. ((and (org-at-timestamp-p t)
  13878. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13879. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13880. (insert "--")
  13881. (setq time (let ((this-command this-command))
  13882. (org-read-date arg 'totime nil nil
  13883. default-time default-input inactive)))
  13884. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13885. ((org-at-timestamp-p t)
  13886. (setq time (let ((this-command this-command))
  13887. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13888. (when (org-at-timestamp-p t) ; just to get the match data
  13889. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13890. (replace-match "")
  13891. (setq org-last-changed-timestamp
  13892. (org-insert-time-stamp
  13893. time (or org-time-was-given arg)
  13894. inactive nil nil (list org-end-time-was-given)))
  13895. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13896. (setq org-last-changed-timestamp
  13897. (concat (substring org-last-inserted-timestamp 0 -1)
  13898. " " repeater ">"))))
  13899. (message "Timestamp updated"))
  13900. ((equal arg '(16))
  13901. (org-insert-time-stamp (current-time) t))
  13902. (t
  13903. (setq time (let ((this-command this-command))
  13904. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13905. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13906. nil nil (list org-end-time-was-given))))))
  13907. ;; FIXME: can we use this for something else, like computing time differences?
  13908. (defun org-get-compact-tod (s)
  13909. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13910. (let* ((t1 (match-string 1 s))
  13911. (h1 (string-to-number (match-string 2 s)))
  13912. (m1 (string-to-number (match-string 3 s)))
  13913. (t2 (and (match-end 4) (match-string 5 s)))
  13914. (h2 (and t2 (string-to-number (match-string 6 s))))
  13915. (m2 (and t2 (string-to-number (match-string 7 s))))
  13916. dh dm)
  13917. (if (not t2)
  13918. t1
  13919. (setq dh (- h2 h1) dm (- m2 m1))
  13920. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13921. (concat t1 "+" (number-to-string dh)
  13922. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13923. (defun org-time-stamp-inactive (&optional arg)
  13924. "Insert an inactive time stamp.
  13925. An inactive time stamp is enclosed in square brackets instead of angle
  13926. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13927. does not link to the calendar and cannot be changed with the S-cursor keys.
  13928. So these are more for recording a certain time/date."
  13929. (interactive "P")
  13930. (org-time-stamp arg 'inactive))
  13931. (defvar org-date-ovl (make-overlay 1 1))
  13932. (overlay-put org-date-ovl 'face 'org-date-selected)
  13933. (org-detach-overlay org-date-ovl)
  13934. (defvar org-ans1) ; dynamically scoped parameter
  13935. (defvar org-ans2) ; dynamically scoped parameter
  13936. (defvar org-plain-time-of-day-regexp) ; defined below
  13937. (defvar org-overriding-default-time nil) ; dynamically scoped
  13938. (defvar org-read-date-overlay nil)
  13939. (defvar org-dcst nil) ; dynamically scoped
  13940. (defvar org-read-date-history nil)
  13941. (defvar org-read-date-final-answer nil)
  13942. (defvar org-read-date-analyze-futurep nil)
  13943. (defvar org-read-date-analyze-forced-year nil)
  13944. (defvar org-read-date-inactive)
  13945. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13946. default-time default-input inactive)
  13947. "Read a date, possibly a time, and make things smooth for the user.
  13948. The prompt will suggest to enter an ISO date, but you can also enter anything
  13949. which will at least partially be understood by `parse-time-string'.
  13950. Unrecognized parts of the date will default to the current day, month, year,
  13951. hour and minute. If this command is called to replace a timestamp at point,
  13952. or to enter the second timestamp of a range, the default time is taken
  13953. from the existing stamp. Furthermore, the command prefers the future,
  13954. so if you are giving a date where the year is not given, and the day-month
  13955. combination is already past in the current year, it will assume you
  13956. mean next year. For details, see the manual. A few examples:
  13957. 3-2-5 --> 2003-02-05
  13958. feb 15 --> currentyear-02-15
  13959. 2/15 --> currentyear-02-15
  13960. sep 12 9 --> 2009-09-12
  13961. 12:45 --> today 12:45
  13962. 22 sept 0:34 --> currentyear-09-22 0:34
  13963. 12 --> currentyear-currentmonth-12
  13964. Fri --> nearest Friday (today or later)
  13965. etc.
  13966. Furthermore you can specify a relative date by giving, as the *first* thing
  13967. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  13968. change in days weeks, months, years.
  13969. With a single plus or minus, the date is relative to today. With a double
  13970. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13971. +4d --> four days from today
  13972. +4 --> same as above
  13973. +2w --> two weeks from today
  13974. ++5 --> five days from default date
  13975. The function understands only English month and weekday abbreviations.
  13976. While prompting, a calendar is popped up - you can also select the
  13977. date with the mouse (button 1). The calendar shows a period of three
  13978. months. To scroll it to other months, use the keys `>' and `<'.
  13979. If you don't like the calendar, turn it off with
  13980. \(setq org-read-date-popup-calendar nil)
  13981. With optional argument TO-TIME, the date will immediately be converted
  13982. to an internal time.
  13983. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  13984. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  13985. still enter a time, and this function will inform the calling routine
  13986. about this change. The calling routine may then choose to change the
  13987. format used to insert the time stamp into the buffer to include the time.
  13988. With optional argument FROM-STRING, read from this string instead from
  13989. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13990. the time/date that is used for everything that is not specified by the
  13991. user."
  13992. (require 'parse-time)
  13993. (let* ((org-time-stamp-rounding-minutes
  13994. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13995. (org-dcst org-display-custom-times)
  13996. (ct (org-current-time))
  13997. (org-def (or org-overriding-default-time default-time ct))
  13998. (org-defdecode (decode-time org-def))
  13999. (dummy (progn
  14000. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14001. (setcar (nthcdr 2 org-defdecode) -1)
  14002. (setcar (nthcdr 1 org-defdecode) 59)
  14003. (setq org-def (apply 'encode-time org-defdecode)
  14004. org-defdecode (decode-time org-def)))))
  14005. (calendar-frame-setup nil)
  14006. (calendar-setup nil)
  14007. (calendar-move-hook nil)
  14008. (calendar-view-diary-initially-flag nil)
  14009. (calendar-view-holidays-initially-flag nil)
  14010. (timestr (format-time-string
  14011. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14012. (prompt (concat (if prompt (concat prompt " ") "")
  14013. (format "Date+time [%s]: " timestr)))
  14014. ans (org-ans0 "") org-ans1 org-ans2 final)
  14015. (cond
  14016. (from-string (setq ans from-string))
  14017. (org-read-date-popup-calendar
  14018. (save-excursion
  14019. (save-window-excursion
  14020. (calendar)
  14021. (org-eval-in-calendar '(setq cursor-type nil) t)
  14022. (unwind-protect
  14023. (progn
  14024. (calendar-forward-day (- (time-to-days org-def)
  14025. (calendar-absolute-from-gregorian
  14026. (calendar-current-date))))
  14027. (org-eval-in-calendar nil t)
  14028. (let* ((old-map (current-local-map))
  14029. (map (copy-keymap calendar-mode-map))
  14030. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14031. (org-defkey map (kbd "RET") 'org-calendar-select)
  14032. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14033. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14034. (org-defkey minibuffer-local-map [(meta shift left)]
  14035. (lambda () (interactive)
  14036. (org-eval-in-calendar '(calendar-backward-month 1))))
  14037. (org-defkey minibuffer-local-map [(meta shift right)]
  14038. (lambda () (interactive)
  14039. (org-eval-in-calendar '(calendar-forward-month 1))))
  14040. (org-defkey minibuffer-local-map [(meta shift up)]
  14041. (lambda () (interactive)
  14042. (org-eval-in-calendar '(calendar-backward-year 1))))
  14043. (org-defkey minibuffer-local-map [(meta shift down)]
  14044. (lambda () (interactive)
  14045. (org-eval-in-calendar '(calendar-forward-year 1))))
  14046. (org-defkey minibuffer-local-map [?\e (shift left)]
  14047. (lambda () (interactive)
  14048. (org-eval-in-calendar '(calendar-backward-month 1))))
  14049. (org-defkey minibuffer-local-map [?\e (shift right)]
  14050. (lambda () (interactive)
  14051. (org-eval-in-calendar '(calendar-forward-month 1))))
  14052. (org-defkey minibuffer-local-map [?\e (shift up)]
  14053. (lambda () (interactive)
  14054. (org-eval-in-calendar '(calendar-backward-year 1))))
  14055. (org-defkey minibuffer-local-map [?\e (shift down)]
  14056. (lambda () (interactive)
  14057. (org-eval-in-calendar '(calendar-forward-year 1))))
  14058. (org-defkey minibuffer-local-map [(shift up)]
  14059. (lambda () (interactive)
  14060. (org-eval-in-calendar '(calendar-backward-week 1))))
  14061. (org-defkey minibuffer-local-map [(shift down)]
  14062. (lambda () (interactive)
  14063. (org-eval-in-calendar '(calendar-forward-week 1))))
  14064. (org-defkey minibuffer-local-map [(shift left)]
  14065. (lambda () (interactive)
  14066. (org-eval-in-calendar '(calendar-backward-day 1))))
  14067. (org-defkey minibuffer-local-map [(shift right)]
  14068. (lambda () (interactive)
  14069. (org-eval-in-calendar '(calendar-forward-day 1))))
  14070. (org-defkey minibuffer-local-map ">"
  14071. (lambda () (interactive)
  14072. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14073. (org-defkey minibuffer-local-map "<"
  14074. (lambda () (interactive)
  14075. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14076. (org-defkey minibuffer-local-map "\C-v"
  14077. (lambda () (interactive)
  14078. (org-eval-in-calendar
  14079. '(calendar-scroll-left-three-months 1))))
  14080. (org-defkey minibuffer-local-map "\M-v"
  14081. (lambda () (interactive)
  14082. (org-eval-in-calendar
  14083. '(calendar-scroll-right-three-months 1))))
  14084. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14085. (unwind-protect
  14086. (progn
  14087. (use-local-map map)
  14088. (setq org-read-date-inactive inactive)
  14089. (add-hook 'post-command-hook 'org-read-date-display)
  14090. (setq org-ans0 (read-string prompt default-input
  14091. 'org-read-date-history nil))
  14092. ;; org-ans0: from prompt
  14093. ;; org-ans1: from mouse click
  14094. ;; org-ans2: from calendar motion
  14095. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14096. (remove-hook 'post-command-hook 'org-read-date-display)
  14097. (use-local-map old-map)
  14098. (when org-read-date-overlay
  14099. (delete-overlay org-read-date-overlay)
  14100. (setq org-read-date-overlay nil)))))
  14101. (bury-buffer "*Calendar*")))))
  14102. (t ; Naked prompt only
  14103. (unwind-protect
  14104. (setq ans (read-string prompt default-input
  14105. 'org-read-date-history timestr))
  14106. (when org-read-date-overlay
  14107. (delete-overlay org-read-date-overlay)
  14108. (setq org-read-date-overlay nil)))))
  14109. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14110. (when org-read-date-analyze-forced-year
  14111. (message "Year was forced into %s"
  14112. (if org-read-date-force-compatible-dates
  14113. "compatible range (1970-2037)"
  14114. "range representable on this machine"))
  14115. (ding))
  14116. ;; One round trip to get rid of 34th of August and stuff like that....
  14117. (setq final (decode-time (apply 'encode-time final)))
  14118. (setq org-read-date-final-answer ans)
  14119. (if to-time
  14120. (apply 'encode-time final)
  14121. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14122. (format "%04d-%02d-%02d %02d:%02d"
  14123. (nth 5 final) (nth 4 final) (nth 3 final)
  14124. (nth 2 final) (nth 1 final))
  14125. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14126. (defvar org-def)
  14127. (defvar org-defdecode)
  14128. (defvar org-with-time)
  14129. (defun org-read-date-display ()
  14130. "Display the current date prompt interpretation in the minibuffer."
  14131. (when org-read-date-display-live
  14132. (when org-read-date-overlay
  14133. (delete-overlay org-read-date-overlay))
  14134. (when (minibufferp (current-buffer))
  14135. (save-excursion
  14136. (end-of-line 1)
  14137. (while (not (equal (buffer-substring
  14138. (max (point-min) (- (point) 4)) (point))
  14139. " "))
  14140. (insert " ")))
  14141. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14142. " " (or org-ans1 org-ans2)))
  14143. (org-end-time-was-given nil)
  14144. (f (org-read-date-analyze ans org-def org-defdecode))
  14145. (fmts (if org-dcst
  14146. org-time-stamp-custom-formats
  14147. org-time-stamp-formats))
  14148. (fmt (if (or org-with-time
  14149. (and (boundp 'org-time-was-given) org-time-was-given))
  14150. (cdr fmts)
  14151. (car fmts)))
  14152. (txt (format-time-string fmt (apply 'encode-time f)))
  14153. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14154. (txt (concat "=> " txt)))
  14155. (when (and org-end-time-was-given
  14156. (string-match org-plain-time-of-day-regexp txt))
  14157. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14158. org-end-time-was-given
  14159. (substring txt (match-end 0)))))
  14160. (when org-read-date-analyze-futurep
  14161. (setq txt (concat txt " (=>F)")))
  14162. (setq org-read-date-overlay
  14163. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14164. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14165. (defun org-read-date-analyze (ans org-def org-defdecode)
  14166. "Analyze the combined answer of the date prompt."
  14167. ;; FIXME: cleanup and comment
  14168. (let ((nowdecode (decode-time (current-time)))
  14169. delta deltan deltaw deltadef year month day
  14170. hour minute second wday pm h2 m2 tl wday1
  14171. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14172. (setq org-read-date-analyze-futurep nil
  14173. org-read-date-analyze-forced-year nil)
  14174. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14175. (setq ans "+0"))
  14176. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14177. (setq ans (replace-match "" t t ans)
  14178. deltan (car delta)
  14179. deltaw (nth 1 delta)
  14180. deltadef (nth 2 delta)))
  14181. ;; Check if there is an iso week date in there. If yes, store the
  14182. ;; info and postpone interpreting it until the rest of the parsing
  14183. ;; is done.
  14184. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14185. (setq iso-year (if (match-end 1)
  14186. (org-small-year-to-year
  14187. (string-to-number (match-string 1 ans))))
  14188. iso-weekday (if (match-end 3)
  14189. (string-to-number (match-string 3 ans)))
  14190. iso-week (string-to-number (match-string 2 ans)))
  14191. (setq ans (replace-match "" t t ans)))
  14192. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14193. (when (string-match
  14194. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14195. (setq year (if (match-end 2)
  14196. (string-to-number (match-string 2 ans))
  14197. (progn (setq kill-year t)
  14198. (string-to-number (format-time-string "%Y"))))
  14199. month (string-to-number (match-string 3 ans))
  14200. day (string-to-number (match-string 4 ans)))
  14201. (if (< year 100) (setq year (+ 2000 year)))
  14202. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14203. t nil ans)))
  14204. ;; Help matching dotted european dates
  14205. (when (string-match
  14206. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14207. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14208. (setq kill-year t)
  14209. (string-to-number (format-time-string "%Y")))
  14210. day (string-to-number (match-string 1 ans))
  14211. month (string-to-number (match-string 2 ans))
  14212. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14213. t nil ans)))
  14214. ;; Help matching american dates, like 5/30 or 5/30/7
  14215. (when (string-match
  14216. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14217. (setq year (if (match-end 4)
  14218. (string-to-number (match-string 4 ans))
  14219. (progn (setq kill-year t)
  14220. (string-to-number (format-time-string "%Y"))))
  14221. month (string-to-number (match-string 1 ans))
  14222. day (string-to-number (match-string 2 ans)))
  14223. (if (< year 100) (setq year (+ 2000 year)))
  14224. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14225. t nil ans)))
  14226. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14227. ;; If there is a time with am/pm, and *no* time without it, we convert
  14228. ;; so that matching will be successful.
  14229. (loop for i from 1 to 2 do ; twice, for end time as well
  14230. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14231. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14232. (setq hour (string-to-number (match-string 1 ans))
  14233. minute (if (match-end 3)
  14234. (string-to-number (match-string 3 ans))
  14235. 0)
  14236. pm (equal ?p
  14237. (string-to-char (downcase (match-string 4 ans)))))
  14238. (if (and (= hour 12) (not pm))
  14239. (setq hour 0)
  14240. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14241. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14242. t t ans))))
  14243. ;; Check if a time range is given as a duration
  14244. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14245. (setq hour (string-to-number (match-string 1 ans))
  14246. h2 (+ hour (string-to-number (match-string 3 ans)))
  14247. minute (string-to-number (match-string 2 ans))
  14248. m2 (+ minute (if (match-end 5) (string-to-number
  14249. (match-string 5 ans))0)))
  14250. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14251. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14252. t t ans)))
  14253. ;; Check if there is a time range
  14254. (when (boundp 'org-end-time-was-given)
  14255. (setq org-time-was-given nil)
  14256. (when (and (string-match org-plain-time-of-day-regexp ans)
  14257. (match-end 8))
  14258. (setq org-end-time-was-given (match-string 8 ans))
  14259. (setq ans (concat (substring ans 0 (match-beginning 7))
  14260. (substring ans (match-end 7))))))
  14261. (setq tl (parse-time-string ans)
  14262. day (or (nth 3 tl) (nth 3 org-defdecode))
  14263. month (or (nth 4 tl)
  14264. (if (and org-read-date-prefer-future
  14265. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14266. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14267. (nth 4 org-defdecode)))
  14268. year (or (and (not kill-year) (nth 5 tl))
  14269. (if (and org-read-date-prefer-future
  14270. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14271. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14272. (nth 5 org-defdecode)))
  14273. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14274. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14275. second (or (nth 0 tl) 0)
  14276. wday (nth 6 tl))
  14277. (when (and (eq org-read-date-prefer-future 'time)
  14278. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14279. (equal day (nth 3 nowdecode))
  14280. (equal month (nth 4 nowdecode))
  14281. (equal year (nth 5 nowdecode))
  14282. (nth 2 tl)
  14283. (or (< (nth 2 tl) (nth 2 nowdecode))
  14284. (and (= (nth 2 tl) (nth 2 nowdecode))
  14285. (nth 1 tl)
  14286. (< (nth 1 tl) (nth 1 nowdecode)))))
  14287. (setq day (1+ day)
  14288. futurep t))
  14289. ;; Special date definitions below
  14290. (cond
  14291. (iso-week
  14292. ;; There was an iso week
  14293. (require 'cal-iso)
  14294. (setq futurep nil)
  14295. (setq year (or iso-year year)
  14296. day (or iso-weekday wday 1)
  14297. wday nil ; to make sure that the trigger below does not match
  14298. iso-date (calendar-gregorian-from-absolute
  14299. (calendar-absolute-from-iso
  14300. (list iso-week day year))))
  14301. ; FIXME: Should we also push ISO weeks into the future?
  14302. ; (when (and org-read-date-prefer-future
  14303. ; (not iso-year)
  14304. ; (< (calendar-absolute-from-gregorian iso-date)
  14305. ; (time-to-days (current-time))))
  14306. ; (setq year (1+ year)
  14307. ; iso-date (calendar-gregorian-from-absolute
  14308. ; (calendar-absolute-from-iso
  14309. ; (list iso-week day year)))))
  14310. (setq month (car iso-date)
  14311. year (nth 2 iso-date)
  14312. day (nth 1 iso-date)))
  14313. (deltan
  14314. (setq futurep nil)
  14315. (unless deltadef
  14316. (let ((now (decode-time (current-time))))
  14317. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14318. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14319. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14320. ((equal deltaw "m") (setq month (+ month deltan)))
  14321. ((equal deltaw "y") (setq year (+ year deltan)))))
  14322. ((and wday (not (nth 3 tl)))
  14323. ;; Weekday was given, but no day, so pick that day in the week
  14324. ;; on or after the derived date.
  14325. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14326. (unless (equal wday wday1)
  14327. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14328. (if (and (boundp 'org-time-was-given)
  14329. (nth 2 tl))
  14330. (setq org-time-was-given t))
  14331. (if (< year 100) (setq year (+ 2000 year)))
  14332. ;; Check of the date is representable
  14333. (if org-read-date-force-compatible-dates
  14334. (progn
  14335. (if (< year 1970)
  14336. (setq year 1970 org-read-date-analyze-forced-year t))
  14337. (if (> year 2037)
  14338. (setq year 2037 org-read-date-analyze-forced-year t)))
  14339. (condition-case nil
  14340. (ignore (encode-time second minute hour day month year))
  14341. (error
  14342. (setq year (nth 5 org-defdecode))
  14343. (setq org-read-date-analyze-forced-year t))))
  14344. (setq org-read-date-analyze-futurep futurep)
  14345. (list second minute hour day month year)))
  14346. (defvar parse-time-weekdays)
  14347. (defun org-read-date-get-relative (s today default)
  14348. "Check string S for special relative date string.
  14349. TODAY and DEFAULT are internal times, for today and for a default.
  14350. Return shift list (N what def-flag)
  14351. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14352. N is the number of WHATs to shift.
  14353. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14354. the DEFAULT date rather than TODAY."
  14355. (require 'parse-time)
  14356. (when (and
  14357. (string-match
  14358. (concat
  14359. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14360. "\\([0-9]+\\)?"
  14361. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14362. "\\([ \t]\\|$\\)") s)
  14363. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14364. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14365. (string-to-char (substring (match-string 1 s) -1))
  14366. ?+))
  14367. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14368. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14369. (what (if (match-end 3) (match-string 3 s) "d"))
  14370. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14371. (date (if rel default today))
  14372. (wday (nth 6 (decode-time date)))
  14373. delta)
  14374. (if wday1
  14375. (progn
  14376. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14377. (if (= dir ?-) (setq delta (- delta 7)))
  14378. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14379. (list delta "d" rel))
  14380. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14381. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14382. "Turn a user-specified date into the internal representation.
  14383. The internal representation needed by the calendar is (month day year).
  14384. This is a wrapper to handle the brain-dead convention in calendar that
  14385. user function argument order change dependent on argument order."
  14386. (if (boundp 'calendar-date-style)
  14387. (cond
  14388. ((eq calendar-date-style 'american)
  14389. (list arg1 arg2 arg3))
  14390. ((eq calendar-date-style 'european)
  14391. (list arg2 arg1 arg3))
  14392. ((eq calendar-date-style 'iso)
  14393. (list arg2 arg3 arg1)))
  14394. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14395. (if (org-bound-and-true-p european-calendar-style)
  14396. (list arg2 arg1 arg3)
  14397. (list arg1 arg2 arg3)))))
  14398. (defun org-eval-in-calendar (form &optional keepdate)
  14399. "Eval FORM in the calendar window and return to current window.
  14400. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14401. otherwise stick to the current value of `org-ans2'."
  14402. (let ((sf (selected-frame))
  14403. (sw (selected-window)))
  14404. (select-window (get-buffer-window "*Calendar*" t))
  14405. (eval form)
  14406. (when (and (not keepdate) (calendar-cursor-to-date))
  14407. (let* ((date (calendar-cursor-to-date))
  14408. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14409. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14410. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14411. (select-window sw)
  14412. (org-select-frame-set-input-focus sf)))
  14413. (defun org-calendar-select ()
  14414. "Return to `org-read-date' with the date currently selected.
  14415. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14416. (interactive)
  14417. (when (calendar-cursor-to-date)
  14418. (let* ((date (calendar-cursor-to-date))
  14419. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14420. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14421. (if (active-minibuffer-window) (exit-minibuffer))))
  14422. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14423. "Insert a date stamp for the date given by the internal TIME.
  14424. WITH-HM means use the stamp format that includes the time of the day.
  14425. INACTIVE means use square brackets instead of angular ones, so that the
  14426. stamp will not contribute to the agenda.
  14427. PRE and POST are optional strings to be inserted before and after the
  14428. stamp.
  14429. The command returns the inserted time stamp."
  14430. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14431. stamp)
  14432. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14433. (insert-before-markers (or pre ""))
  14434. (when (listp extra)
  14435. (setq extra (car extra))
  14436. (if (and (stringp extra)
  14437. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14438. (setq extra (format "-%02d:%02d"
  14439. (string-to-number (match-string 1 extra))
  14440. (string-to-number (match-string 2 extra))))
  14441. (setq extra nil)))
  14442. (when extra
  14443. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14444. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14445. (insert-before-markers (or post ""))
  14446. (setq org-last-inserted-timestamp stamp)))
  14447. (defun org-toggle-time-stamp-overlays ()
  14448. "Toggle the use of custom time stamp formats."
  14449. (interactive)
  14450. (setq org-display-custom-times (not org-display-custom-times))
  14451. (unless org-display-custom-times
  14452. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14453. (while (setq p (next-single-property-change p 'display))
  14454. (if (and (get-text-property p 'display)
  14455. (eq (get-text-property p 'face) 'org-date))
  14456. (remove-text-properties
  14457. p (setq p (next-single-property-change p 'display))
  14458. '(display t))))
  14459. (set-buffer-modified-p bmp)))
  14460. (if (featurep 'xemacs)
  14461. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14462. (org-restart-font-lock)
  14463. (setq org-table-may-need-update t)
  14464. (if org-display-custom-times
  14465. (message "Time stamps are overlaid with custom format")
  14466. (message "Time stamp overlays removed")))
  14467. (defun org-display-custom-time (beg end)
  14468. "Overlay modified time stamp format over timestamp between BEG and END."
  14469. (let* ((ts (buffer-substring beg end))
  14470. t1 w1 with-hm tf time str w2 (off 0))
  14471. (save-match-data
  14472. (setq t1 (org-parse-time-string ts t))
  14473. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14474. (setq off (- (match-end 0) (match-beginning 0)))))
  14475. (setq end (- end off))
  14476. (setq w1 (- end beg)
  14477. with-hm (and (nth 1 t1) (nth 2 t1))
  14478. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14479. time (org-fix-decoded-time t1)
  14480. str (org-add-props
  14481. (format-time-string
  14482. (substring tf 1 -1) (apply 'encode-time time))
  14483. nil 'mouse-face 'highlight)
  14484. w2 (length str))
  14485. (if (not (= w2 w1))
  14486. (add-text-properties (1+ beg) (+ 2 beg)
  14487. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14488. (if (featurep 'xemacs)
  14489. (progn
  14490. (put-text-property beg end 'invisible t)
  14491. (put-text-property beg end 'end-glyph (make-glyph str)))
  14492. (put-text-property beg end 'display str))))
  14493. (defun org-translate-time (string)
  14494. "Translate all timestamps in STRING to custom format.
  14495. But do this only if the variable `org-display-custom-times' is set."
  14496. (when org-display-custom-times
  14497. (save-match-data
  14498. (let* ((start 0)
  14499. (re org-ts-regexp-both)
  14500. t1 with-hm inactive tf time str beg end)
  14501. (while (setq start (string-match re string start))
  14502. (setq beg (match-beginning 0)
  14503. end (match-end 0)
  14504. t1 (save-match-data
  14505. (org-parse-time-string (substring string beg end) t))
  14506. with-hm (and (nth 1 t1) (nth 2 t1))
  14507. inactive (equal (substring string beg (1+ beg)) "[")
  14508. tf (funcall (if with-hm 'cdr 'car)
  14509. org-time-stamp-custom-formats)
  14510. time (org-fix-decoded-time t1)
  14511. str (format-time-string
  14512. (concat
  14513. (if inactive "[" "<") (substring tf 1 -1)
  14514. (if inactive "]" ">"))
  14515. (apply 'encode-time time))
  14516. string (replace-match str t t string)
  14517. start (+ start (length str)))))))
  14518. string)
  14519. (defun org-fix-decoded-time (time)
  14520. "Set 0 instead of nil for the first 6 elements of time.
  14521. Don't touch the rest."
  14522. (let ((n 0))
  14523. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14524. (defun org-days-to-time (timestamp-string)
  14525. "Difference between TIMESTAMP-STRING and now in days."
  14526. (- (time-to-days (org-time-string-to-time timestamp-string))
  14527. (time-to-days (current-time))))
  14528. (defun org-deadline-close (timestamp-string &optional ndays)
  14529. "Is the time in TIMESTAMP-STRING close to the current date?"
  14530. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14531. (and (< (org-days-to-time timestamp-string) ndays)
  14532. (not (org-entry-is-done-p))))
  14533. (defun org-get-wdays (ts)
  14534. "Get the deadline lead time appropriate for timestring TS."
  14535. (cond
  14536. ((<= org-deadline-warning-days 0)
  14537. ;; 0 or negative, enforce this value no matter what
  14538. (- org-deadline-warning-days))
  14539. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14540. ;; lead time is specified.
  14541. (floor (* (string-to-number (match-string 1 ts))
  14542. (cdr (assoc (match-string 2 ts)
  14543. '(("d" . 1) ("w" . 7)
  14544. ("m" . 30.4) ("y" . 365.25)))))))
  14545. ;; go for the default.
  14546. (t org-deadline-warning-days)))
  14547. (defun org-calendar-select-mouse (ev)
  14548. "Return to `org-read-date' with the date currently selected.
  14549. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14550. (interactive "e")
  14551. (mouse-set-point ev)
  14552. (when (calendar-cursor-to-date)
  14553. (let* ((date (calendar-cursor-to-date))
  14554. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14555. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14556. (if (active-minibuffer-window) (exit-minibuffer))))
  14557. (defun org-check-deadlines (ndays)
  14558. "Check if there are any deadlines due or past due.
  14559. A deadline is considered due if it happens within `org-deadline-warning-days'
  14560. days from today's date. If the deadline appears in an entry marked DONE,
  14561. it is not shown. The prefix arg NDAYS can be used to test that many
  14562. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14563. (interactive "P")
  14564. (let* ((org-warn-days
  14565. (cond
  14566. ((equal ndays '(4)) 100000)
  14567. (ndays (prefix-numeric-value ndays))
  14568. (t (abs org-deadline-warning-days))))
  14569. (case-fold-search nil)
  14570. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14571. (callback
  14572. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14573. (message "%d deadlines past-due or due within %d days"
  14574. (org-occur regexp nil callback)
  14575. org-warn-days)))
  14576. (defsubst org-re-timestamp (type)
  14577. "Return a regexp for timestamp TYPE.
  14578. Allowed values for TYPE are:
  14579. all: all timestamps
  14580. active: only active timestamps (<...>)
  14581. inactive: only inactive timestamps ([...])
  14582. scheduled: only scheduled timestamps
  14583. deadline: only deadline timestamps
  14584. When TYPE is nil, fall back on returning a regexp that matches
  14585. both scheduled and deadline timestamps."
  14586. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14587. ((eq type 'active) org-ts-regexp)
  14588. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14589. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14590. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14591. ((eq type 'scheduled-or-deadline)
  14592. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14593. (defun org-check-before-date (date)
  14594. "Check if there are deadlines or scheduled entries before DATE."
  14595. (interactive (list (org-read-date)))
  14596. (let ((case-fold-search nil)
  14597. (regexp (org-re-timestamp org-ts-type))
  14598. (callback
  14599. (lambda () (time-less-p
  14600. (org-time-string-to-time (match-string 1))
  14601. (org-time-string-to-time date)))))
  14602. (message "%d entries before %s"
  14603. (org-occur regexp nil callback) date)))
  14604. (defun org-check-after-date (date)
  14605. "Check if there are deadlines or scheduled entries after DATE."
  14606. (interactive (list (org-read-date)))
  14607. (let ((case-fold-search nil)
  14608. (regexp (org-re-timestamp org-ts-type))
  14609. (callback
  14610. (lambda () (not
  14611. (time-less-p
  14612. (org-time-string-to-time (match-string 1))
  14613. (org-time-string-to-time date))))))
  14614. (message "%d entries after %s"
  14615. (org-occur regexp nil callback) date)))
  14616. (defun org-check-dates-range (start-date end-date)
  14617. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14618. (interactive (list (org-read-date nil nil nil "Range starts")
  14619. (org-read-date nil nil nil "Range end")))
  14620. (let ((case-fold-search nil)
  14621. (regexp (org-re-timestamp org-ts-type))
  14622. (callback
  14623. (lambda ()
  14624. (let ((match (match-string 1)))
  14625. (and
  14626. (not (time-less-p
  14627. (org-time-string-to-time match)
  14628. (org-time-string-to-time start-date)))
  14629. (time-less-p
  14630. (org-time-string-to-time match)
  14631. (org-time-string-to-time end-date)))))))
  14632. (message "%d entries between %s and %s"
  14633. (org-occur regexp nil callback) start-date end-date)))
  14634. (defun org-evaluate-time-range (&optional to-buffer)
  14635. "Evaluate a time range by computing the difference between start and end.
  14636. Normally the result is just printed in the echo area, but with prefix arg
  14637. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14638. If the time range is actually in a table, the result is inserted into the
  14639. next column.
  14640. For time difference computation, a year is assumed to be exactly 365
  14641. days in order to avoid rounding problems."
  14642. (interactive "P")
  14643. (or
  14644. (org-clock-update-time-maybe)
  14645. (save-excursion
  14646. (unless (org-at-date-range-p t)
  14647. (goto-char (point-at-bol))
  14648. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14649. (if (not (org-at-date-range-p t))
  14650. (error "Not at a time-stamp range, and none found in current line")))
  14651. (let* ((ts1 (match-string 1))
  14652. (ts2 (match-string 2))
  14653. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14654. (match-end (match-end 0))
  14655. (time1 (org-time-string-to-time ts1))
  14656. (time2 (org-time-string-to-time ts2))
  14657. (t1 (org-float-time time1))
  14658. (t2 (org-float-time time2))
  14659. (diff (abs (- t2 t1)))
  14660. (negative (< (- t2 t1) 0))
  14661. ;; (ys (floor (* 365 24 60 60)))
  14662. (ds (* 24 60 60))
  14663. (hs (* 60 60))
  14664. (fy "%dy %dd %02d:%02d")
  14665. (fy1 "%dy %dd")
  14666. (fd "%dd %02d:%02d")
  14667. (fd1 "%dd")
  14668. (fh "%02d:%02d")
  14669. y d h m align)
  14670. (if havetime
  14671. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14672. y 0
  14673. d (floor (/ diff ds)) diff (mod diff ds)
  14674. h (floor (/ diff hs)) diff (mod diff hs)
  14675. m (floor (/ diff 60)))
  14676. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14677. y 0
  14678. d (floor (+ (/ diff ds) 0.5))
  14679. h 0 m 0))
  14680. (if (not to-buffer)
  14681. (message "%s" (org-make-tdiff-string y d h m))
  14682. (if (org-at-table-p)
  14683. (progn
  14684. (goto-char match-end)
  14685. (setq align t)
  14686. (and (looking-at " *|") (goto-char (match-end 0))))
  14687. (goto-char match-end))
  14688. (if (looking-at
  14689. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14690. (replace-match ""))
  14691. (if negative (insert " -"))
  14692. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14693. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14694. (insert " " (format fh h m))))
  14695. (if align (org-table-align))
  14696. (message "Time difference inserted")))))
  14697. (defun org-make-tdiff-string (y d h m)
  14698. (let ((fmt "")
  14699. (l nil))
  14700. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14701. l (push y l)))
  14702. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14703. l (push d l)))
  14704. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14705. l (push h l)))
  14706. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14707. l (push m l)))
  14708. (apply 'format fmt (nreverse l))))
  14709. (defun org-time-string-to-time (s &optional buffer pos)
  14710. "Convert a timestamp string into internal time."
  14711. (condition-case errdata
  14712. (apply 'encode-time (org-parse-time-string s))
  14713. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14714. s (if (not (and buffer pos))
  14715. ""
  14716. (format " at %d in buffer `%s'" pos buffer))
  14717. (cdr errdata)))))
  14718. (defun org-time-string-to-seconds (s)
  14719. "Convert a timestamp string to a number of seconds."
  14720. (org-float-time (org-time-string-to-time s)))
  14721. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14722. "Convert a time stamp to an absolute day number.
  14723. If there is a specifier for a cyclic time stamp, get the closest date to
  14724. DAYNR.
  14725. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14726. The variable date is bound by the calendar when this is called."
  14727. (cond
  14728. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14729. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14730. daynr
  14731. (+ daynr 1000)))
  14732. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14733. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14734. (time-to-days (current-time))) (match-string 0 s)
  14735. prefer show-all))
  14736. (t (time-to-days
  14737. (condition-case errdata
  14738. (apply 'encode-time (org-parse-time-string s))
  14739. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14740. s (if (not (and buffer pos))
  14741. ""
  14742. (format " at %d in buffer `%s'" pos buffer))
  14743. (cdr errdata))))))))
  14744. (defun org-days-to-iso-week (days)
  14745. "Return the iso week number."
  14746. (require 'cal-iso)
  14747. (car (calendar-iso-from-absolute days)))
  14748. (defun org-small-year-to-year (year)
  14749. "Convert 2-digit years into 4-digit years.
  14750. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14751. The year 2000 cannot be abbreviated. Any year larger than 99
  14752. is returned unchanged."
  14753. (if (< year 38)
  14754. (setq year (+ 2000 year))
  14755. (if (< year 100)
  14756. (setq year (+ 1900 year))))
  14757. year)
  14758. (defun org-time-from-absolute (d)
  14759. "Return the time corresponding to date D.
  14760. D may be an absolute day number, or a calendar-type list (month day year)."
  14761. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14762. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14763. (defun org-calendar-holiday ()
  14764. "List of holidays, for Diary display in Org-mode."
  14765. (require 'holidays)
  14766. (let ((hl (funcall
  14767. (if (fboundp 'calendar-check-holidays)
  14768. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14769. (if hl (mapconcat 'identity hl "; "))))
  14770. (defun org-diary-sexp-entry (sexp entry date)
  14771. "Process a SEXP diary ENTRY for DATE."
  14772. (require 'diary-lib)
  14773. (let ((result (if calendar-debug-sexp
  14774. (let ((stack-trace-on-error t))
  14775. (eval (car (read-from-string sexp))))
  14776. (condition-case nil
  14777. (eval (car (read-from-string sexp)))
  14778. (error
  14779. (beep)
  14780. (message "Bad sexp at line %d in %s: %s"
  14781. (org-current-line)
  14782. (buffer-file-name) sexp)
  14783. (sleep-for 2))))))
  14784. (cond ((stringp result) (split-string result "; "))
  14785. ((and (consp result)
  14786. (not (consp (cdr result)))
  14787. (stringp (cdr result))) (cdr result))
  14788. ((and (consp result)
  14789. (stringp (car result))) result)
  14790. (result entry))))
  14791. (defun org-diary-to-ical-string (frombuf)
  14792. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14793. This uses the icalendar.el library."
  14794. (let* ((tmpdir (if (featurep 'xemacs)
  14795. (temp-directory)
  14796. temporary-file-directory))
  14797. (tmpfile (make-temp-name
  14798. (expand-file-name "orgics" tmpdir)))
  14799. buf rtn b e)
  14800. (with-current-buffer frombuf
  14801. (icalendar-export-region (point-min) (point-max) tmpfile)
  14802. (setq buf (find-buffer-visiting tmpfile))
  14803. (set-buffer buf)
  14804. (goto-char (point-min))
  14805. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14806. (setq b (match-beginning 0)))
  14807. (goto-char (point-max))
  14808. (if (re-search-backward "^END:VEVENT" nil t)
  14809. (setq e (match-end 0)))
  14810. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14811. (kill-buffer buf)
  14812. (delete-file tmpfile)
  14813. rtn))
  14814. (defun org-closest-date (start current change prefer show-all)
  14815. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14816. When PREFER is `past', return a date that is either CURRENT or past.
  14817. When PREFER is `future', return a date that is either CURRENT or future.
  14818. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14819. ;; Make the proper lists from the dates
  14820. (catch 'exit
  14821. (let ((a1 '(("h" . hour)
  14822. ("d" . day)
  14823. ("w" . week)
  14824. ("m" . month)
  14825. ("y" . year)))
  14826. (shour (nth 2 (org-parse-time-string start)))
  14827. dn dw sday cday n1 n2 n0
  14828. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14829. (setq start (org-date-to-gregorian start)
  14830. current (org-date-to-gregorian
  14831. (if show-all
  14832. current
  14833. (time-to-days (current-time))))
  14834. sday (calendar-absolute-from-gregorian start)
  14835. cday (calendar-absolute-from-gregorian current))
  14836. (if (<= cday sday) (throw 'exit sday))
  14837. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14838. (setq dn (string-to-number (match-string 1 change))
  14839. dw (cdr (assoc (match-string 2 change) a1)))
  14840. (error "Invalid change specifier: %s" change))
  14841. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14842. (cond
  14843. ((eq dw 'hour)
  14844. (let ((missing-hours
  14845. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  14846. dn)))
  14847. (setq n1 (if (zerop missing-hours) cday
  14848. (- cday (1+ (floor (/ missing-hours 24)))))
  14849. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  14850. ((eq dw 'day)
  14851. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14852. n2 (+ n1 dn)))
  14853. ((eq dw 'year)
  14854. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14855. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14856. (setq date1 (list m d y1)
  14857. n1 (calendar-absolute-from-gregorian date1)
  14858. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14859. n2 (calendar-absolute-from-gregorian date2)))
  14860. ((eq dw 'month)
  14861. ;; approx number of month between the two dates
  14862. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14863. ;; How often does dn fit in there?
  14864. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14865. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14866. m (+ m nm)
  14867. ny (floor (/ m 12))
  14868. y (+ y ny)
  14869. m (- m (* ny 12)))
  14870. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14871. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14872. (setq m2 (+ m dn) y2 y)
  14873. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14874. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14875. (while (<= n2 cday)
  14876. (setq n1 n2 m m2 y y2)
  14877. (setq m2 (+ m dn) y2 y)
  14878. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14879. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14880. ;; Make sure n1 is the earlier date
  14881. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14882. (if show-all
  14883. (cond
  14884. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14885. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14886. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14887. (cond
  14888. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14889. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14890. (t (if (= cday n1) n1 n2)))))))
  14891. (defun org-date-to-gregorian (date)
  14892. "Turn any specification of DATE into a Gregorian date for the calendar."
  14893. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14894. ((and (listp date) (= (length date) 3)) date)
  14895. ((stringp date)
  14896. (setq date (org-parse-time-string date))
  14897. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14898. ((listp date)
  14899. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14900. (defun org-parse-time-string (s &optional nodefault)
  14901. "Parse the standard Org-mode time string.
  14902. This should be a lot faster than the normal `parse-time-string'.
  14903. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14904. hour and minute fields will be nil if not given."
  14905. (if (string-match org-ts-regexp0 s)
  14906. (list 0
  14907. (if (or (match-beginning 8) (not nodefault))
  14908. (string-to-number (or (match-string 8 s) "0")))
  14909. (if (or (match-beginning 7) (not nodefault))
  14910. (string-to-number (or (match-string 7 s) "0")))
  14911. (string-to-number (match-string 4 s))
  14912. (string-to-number (match-string 3 s))
  14913. (string-to-number (match-string 2 s))
  14914. nil nil nil)
  14915. (error "Not a standard Org-mode time string: %s" s)))
  14916. (defun org-timestamp-up (&optional arg)
  14917. "Increase the date item at the cursor by one.
  14918. If the cursor is on the year, change the year. If it is on the month,
  14919. the day or the time, change that.
  14920. With prefix ARG, change by that many units."
  14921. (interactive "p")
  14922. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14923. (defun org-timestamp-down (&optional arg)
  14924. "Decrease the date item at the cursor by one.
  14925. If the cursor is on the year, change the year. If it is on the month,
  14926. the day or the time, change that.
  14927. With prefix ARG, change by that many units."
  14928. (interactive "p")
  14929. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14930. (defun org-timestamp-up-day (&optional arg)
  14931. "Increase the date in the time stamp by one day.
  14932. With prefix ARG, change that many days."
  14933. (interactive "p")
  14934. (if (and (not (org-at-timestamp-p t))
  14935. (org-at-heading-p))
  14936. (org-todo 'up)
  14937. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14938. (defun org-timestamp-down-day (&optional arg)
  14939. "Decrease the date in the time stamp by one day.
  14940. With prefix ARG, change that many days."
  14941. (interactive "p")
  14942. (if (and (not (org-at-timestamp-p t))
  14943. (org-at-heading-p))
  14944. (org-todo 'down)
  14945. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14946. (defun org-at-timestamp-p (&optional inactive-ok)
  14947. "Determine if the cursor is in or at a timestamp."
  14948. (interactive)
  14949. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14950. (pos (point))
  14951. (ans (or (looking-at tsr)
  14952. (save-excursion
  14953. (skip-chars-backward "^[<\n\r\t")
  14954. (if (> (point) (point-min)) (backward-char 1))
  14955. (and (looking-at tsr)
  14956. (> (- (match-end 0) pos) -1))))))
  14957. (and ans
  14958. (boundp 'org-ts-what)
  14959. (setq org-ts-what
  14960. (cond
  14961. ((= pos (match-beginning 0)) 'bracket)
  14962. ;; Point is considered to be "on the bracket" whether
  14963. ;; it's really on it or right after it.
  14964. ((= pos (1- (match-end 0))) 'bracket)
  14965. ((= pos (match-end 0)) 'after)
  14966. ((org-pos-in-match-range pos 2) 'year)
  14967. ((org-pos-in-match-range pos 3) 'month)
  14968. ((org-pos-in-match-range pos 7) 'hour)
  14969. ((org-pos-in-match-range pos 8) 'minute)
  14970. ((or (org-pos-in-match-range pos 4)
  14971. (org-pos-in-match-range pos 5)) 'day)
  14972. ((and (> pos (or (match-end 8) (match-end 5)))
  14973. (< pos (match-end 0)))
  14974. (- pos (or (match-end 8) (match-end 5))))
  14975. (t 'day))))
  14976. ans))
  14977. (defun org-toggle-timestamp-type ()
  14978. "Toggle the type (<active> or [inactive]) of a time stamp."
  14979. (interactive)
  14980. (when (org-at-timestamp-p t)
  14981. (let ((beg (match-beginning 0)) (end (match-end 0))
  14982. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14983. (save-excursion
  14984. (goto-char beg)
  14985. (while (re-search-forward "[][<>]" end t)
  14986. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14987. t t)))
  14988. (message "Timestamp is now %sactive"
  14989. (if (equal (char-after beg) ?<) "" "in")))))
  14990. (defvar org-clock-history) ; defined in org-clock.el
  14991. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  14992. (defun org-timestamp-change (n &optional what updown)
  14993. "Change the date in the time stamp at point.
  14994. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14995. `year', `minute', `second'. If WHAT is not given, the cursor position
  14996. in the timestamp determines what will be changed."
  14997. (let ((origin (point)) origin-cat
  14998. with-hm inactive
  14999. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15000. org-ts-what
  15001. extra rem
  15002. ts time time0 fixnext clrgx)
  15003. (if (not (org-at-timestamp-p t))
  15004. (error "Not at a timestamp"))
  15005. (if (and (not what) (eq org-ts-what 'bracket))
  15006. (org-toggle-timestamp-type)
  15007. ;; Point isn't on brackets. Remember the part of the time-stamp
  15008. ;; the point was in. Indeed, size of time-stamps may change,
  15009. ;; but point must be kept in the same category nonetheless.
  15010. (setq origin-cat org-ts-what)
  15011. (if (and (not what) (not (eq org-ts-what 'day))
  15012. org-display-custom-times
  15013. (get-text-property (point) 'display)
  15014. (not (get-text-property (1- (point)) 'display)))
  15015. (setq org-ts-what 'day))
  15016. (setq org-ts-what (or what org-ts-what)
  15017. inactive (= (char-after (match-beginning 0)) ?\[)
  15018. ts (match-string 0))
  15019. (replace-match "")
  15020. (if (string-match
  15021. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15022. ts)
  15023. (setq extra (match-string 1 ts)))
  15024. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15025. (setq with-hm t))
  15026. (setq time0 (org-parse-time-string ts))
  15027. (when (and updown
  15028. (eq org-ts-what 'minute)
  15029. (not current-prefix-arg))
  15030. ;; This looks like s-up and s-down. Change by one rounding step.
  15031. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15032. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15033. (setcar (cdr time0) (+ (nth 1 time0)
  15034. (if (> n 0) (- rem) (- dm rem))))))
  15035. (setq time
  15036. (encode-time (or (car time0) 0)
  15037. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15038. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15039. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15040. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15041. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15042. (nthcdr 6 time0)))
  15043. (when (and (member org-ts-what '(hour minute))
  15044. extra
  15045. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15046. (setq extra (org-modify-ts-extra
  15047. extra
  15048. (if (eq org-ts-what 'hour) 2 5)
  15049. n dm)))
  15050. (when (integerp org-ts-what)
  15051. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15052. (if (eq what 'calendar)
  15053. (let ((cal-date (org-get-date-from-calendar)))
  15054. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15055. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15056. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15057. (setcar time0 (or (car time0) 0))
  15058. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15059. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15060. (setq time (apply 'encode-time time0))))
  15061. ;; Insert the new time-stamp, and ensure point stays in the same
  15062. ;; category as before (i.e. not after the last position in that
  15063. ;; category).
  15064. (let ((pos (point)))
  15065. ;; Stay before inserted string. `save-excursion' is of no use.
  15066. (setq org-last-changed-timestamp
  15067. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15068. (goto-char pos))
  15069. (save-match-data
  15070. (looking-at org-ts-regexp3)
  15071. (goto-char (cond
  15072. ;; `day' category ends before `hour' if any, or at
  15073. ;; the end of the day name.
  15074. ((eq origin-cat 'day)
  15075. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15076. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15077. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15078. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15079. ;; `year' and `month' have both fixed size: point
  15080. ;; couldn't have moved into another part.
  15081. (t origin))))
  15082. ;; Update clock if on a CLOCK line.
  15083. (org-clock-update-time-maybe)
  15084. ;; Maybe adjust the closest clock in `org-clock-history'
  15085. (when org-clock-adjust-closest
  15086. (if (not (and (org-at-clock-log-p)
  15087. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15088. org-clock-history))))))
  15089. (message "No clock to adjust")
  15090. (cond ((save-excursion ; fix previous clock?
  15091. (re-search-backward org-ts-regexp0 nil t)
  15092. (org-looking-back (concat org-clock-string " \\[")))
  15093. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15094. ((save-excursion ; fix next clock?
  15095. (re-search-backward org-ts-regexp0 nil t)
  15096. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15097. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15098. (save-window-excursion
  15099. ;; Find closest clock to point, adjust the previous/next one in history
  15100. (let* ((p (save-excursion (org-back-to-heading t)))
  15101. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15102. (clfixnth
  15103. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15104. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15105. (if (not clfixpos)
  15106. (message "No clock to adjust")
  15107. (save-excursion
  15108. (org-goto-marker-or-bmk clfixpos)
  15109. (org-show-subtree)
  15110. (when (re-search-forward clrgx nil t)
  15111. (goto-char (match-beginning 1))
  15112. (let (org-clock-adjust-closest)
  15113. (org-timestamp-change n org-ts-what updown))
  15114. (message "Clock adjusted in %s for heading: %s"
  15115. (file-name-nondirectory (buffer-file-name))
  15116. (org-get-heading t t)))))))))
  15117. ;; Try to recenter the calendar window, if any.
  15118. (if (and org-calendar-follow-timestamp-change
  15119. (get-buffer-window "*Calendar*" t)
  15120. (memq org-ts-what '(day month year)))
  15121. (org-recenter-calendar (time-to-days time))))))
  15122. (defun org-modify-ts-extra (s pos n dm)
  15123. "Change the different parts of the lead-time and repeat fields in timestamp."
  15124. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15125. ng h m new rem)
  15126. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15127. (cond
  15128. ((or (org-pos-in-match-range pos 2)
  15129. (org-pos-in-match-range pos 3))
  15130. (setq m (string-to-number (match-string 3 s))
  15131. h (string-to-number (match-string 2 s)))
  15132. (if (org-pos-in-match-range pos 2)
  15133. (setq h (+ h n))
  15134. (setq n (* dm (org-no-warnings (signum n))))
  15135. (when (not (= 0 (setq rem (% m dm))))
  15136. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15137. (setq m (+ m n)))
  15138. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15139. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15140. (setq h (min 24 (max 0 h)))
  15141. (setq ng 1 new (format "-%02d:%02d" h m)))
  15142. ((org-pos-in-match-range pos 6)
  15143. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15144. ((org-pos-in-match-range pos 5)
  15145. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15146. ((org-pos-in-match-range pos 9)
  15147. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15148. ((org-pos-in-match-range pos 8)
  15149. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15150. (when ng
  15151. (setq s (concat
  15152. (substring s 0 (match-beginning ng))
  15153. new
  15154. (substring s (match-end ng))))))
  15155. s))
  15156. (defun org-recenter-calendar (date)
  15157. "If the calendar is visible, recenter it to DATE."
  15158. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15159. (when cwin
  15160. (let ((calendar-move-hook nil))
  15161. (with-selected-window cwin
  15162. (calendar-goto-date (if (listp date) date
  15163. (calendar-gregorian-from-absolute date))))))))
  15164. (defun org-goto-calendar (&optional arg)
  15165. "Go to the Emacs calendar at the current date.
  15166. If there is a time stamp in the current line, go to that date.
  15167. A prefix ARG can be used to force the current date."
  15168. (interactive "P")
  15169. (let ((tsr org-ts-regexp) diff
  15170. (calendar-move-hook nil)
  15171. (calendar-view-holidays-initially-flag nil)
  15172. (calendar-view-diary-initially-flag nil))
  15173. (if (or (org-at-timestamp-p)
  15174. (save-excursion
  15175. (beginning-of-line 1)
  15176. (looking-at (concat ".*" tsr))))
  15177. (let ((d1 (time-to-days (current-time)))
  15178. (d2 (time-to-days
  15179. (org-time-string-to-time (match-string 1)))))
  15180. (setq diff (- d2 d1))))
  15181. (calendar)
  15182. (calendar-goto-today)
  15183. (if (and diff (not arg)) (calendar-forward-day diff))))
  15184. (defun org-get-date-from-calendar ()
  15185. "Return a list (month day year) of date at point in calendar."
  15186. (with-current-buffer "*Calendar*"
  15187. (save-match-data
  15188. (calendar-cursor-to-date))))
  15189. (defun org-date-from-calendar ()
  15190. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15191. If there is already a time stamp at the cursor position, update it."
  15192. (interactive)
  15193. (if (org-at-timestamp-p t)
  15194. (org-timestamp-change 0 'calendar)
  15195. (let ((cal-date (org-get-date-from-calendar)))
  15196. (org-insert-time-stamp
  15197. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15198. (defun org-minutes-to-hh:mm-string (m)
  15199. "Compute H:MM from a number of minutes."
  15200. (let ((h (/ m 60)))
  15201. (setq m (- m (* 60 h)))
  15202. (format org-time-clocksum-format h m)))
  15203. (defun org-hh:mm-string-to-minutes (s)
  15204. "Convert a string H:MM to a number of minutes.
  15205. If the string is just a number, interpret it as minutes.
  15206. In fact, the first hh:mm or number in the string will be taken,
  15207. there can be extra stuff in the string.
  15208. If no number is found, the return value is 0."
  15209. (cond
  15210. ((integerp s) s)
  15211. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15212. (+ (* (string-to-number (match-string 1 s)) 60)
  15213. (string-to-number (match-string 2 s))))
  15214. ((string-match "\\([0-9]+\\)" s)
  15215. (string-to-number (match-string 1 s)))
  15216. (t 0)))
  15217. (defcustom org-effort-durations
  15218. `(("h" . 60)
  15219. ("d" . ,(* 60 8))
  15220. ("w" . ,(* 60 8 5))
  15221. ("m" . ,(* 60 8 5 4))
  15222. ("y" . ,(* 60 8 5 40)))
  15223. "Conversion factor to minutes for an effort modifier.
  15224. Each entry has the form (MODIFIER . MINUTES).
  15225. In an effort string, a number followed by MODIFIER is multiplied
  15226. by the specified number of MINUTES to obtain an effort in
  15227. minutes.
  15228. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15229. effort string \"2hours\" is equivalent to 120 minutes."
  15230. :group 'org-agenda
  15231. :version "24.1"
  15232. :type '(alist :key-type (string :tag "Modifier")
  15233. :value-type (number :tag "Minutes")))
  15234. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15235. "Convert a duration string S to minutes.
  15236. A bare number is interpreted as minutes, modifiers can be set by
  15237. customizing `org-effort-durations' (which see).
  15238. Entries containing a colon are interpreted as H:MM by
  15239. `org-hh:mm-string-to-minutes'."
  15240. (let ((result 0)
  15241. (re (concat "\\([0-9.]+\\) *\\("
  15242. (regexp-opt (mapcar 'car org-effort-durations))
  15243. "\\)")))
  15244. (while (string-match re s)
  15245. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15246. (string-to-number (match-string 1 s))))
  15247. (setq s (replace-match "" nil t s)))
  15248. (setq result (floor result))
  15249. (incf result (org-hh:mm-string-to-minutes s))
  15250. (if output-to-string (number-to-string result) result)))
  15251. ;;;; Files
  15252. (defun org-save-all-org-buffers ()
  15253. "Save all Org-mode buffers without user confirmation."
  15254. (interactive)
  15255. (message "Saving all Org-mode buffers...")
  15256. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15257. (when (featurep 'org-id) (org-id-locations-save))
  15258. (message "Saving all Org-mode buffers... done"))
  15259. (defun org-revert-all-org-buffers ()
  15260. "Revert all Org-mode buffers.
  15261. Prompt for confirmation when there are unsaved changes.
  15262. Be sure you know what you are doing before letting this function
  15263. overwrite your changes.
  15264. This function is useful in a setup where one tracks org files
  15265. with a version control system, to revert on one machine after pulling
  15266. changes from another. I believe the procedure must be like this:
  15267. 1. M-x org-save-all-org-buffers
  15268. 2. Pull changes from the other machine, resolve conflicts
  15269. 3. M-x org-revert-all-org-buffers"
  15270. (interactive)
  15271. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15272. (error "Abort"))
  15273. (save-excursion
  15274. (save-window-excursion
  15275. (mapc
  15276. (lambda (b)
  15277. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15278. (with-current-buffer b buffer-file-name))
  15279. (org-pop-to-buffer-same-window b)
  15280. (revert-buffer t 'no-confirm)))
  15281. (buffer-list))
  15282. (when (and (featurep 'org-id) org-id-track-globally)
  15283. (org-id-locations-load)))))
  15284. ;;;; Agenda files
  15285. ;;;###autoload
  15286. (defun org-switchb (&optional arg)
  15287. "Switch between Org buffers.
  15288. With one prefix argument, restrict available buffers to files.
  15289. With two prefix arguments, restrict available buffers to agenda files.
  15290. Defaults to `iswitchb' for buffer name completion.
  15291. Set `org-completion-use-ido' to make it use ido instead."
  15292. (interactive "P")
  15293. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15294. ((equal arg '(16)) (org-buffer-list 'agenda))
  15295. (t (org-buffer-list))))
  15296. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15297. (org-completion-use-ido org-completion-use-ido))
  15298. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15299. (setq org-completion-use-iswitchb t))
  15300. (org-pop-to-buffer-same-window
  15301. (org-icompleting-read "Org buffer: "
  15302. (mapcar 'list (mapcar 'buffer-name blist))
  15303. nil t))))
  15304. ;;; Define some older names previously used for this functionality
  15305. ;;;###autoload
  15306. (defalias 'org-ido-switchb 'org-switchb)
  15307. ;;;###autoload
  15308. (defalias 'org-iswitchb 'org-switchb)
  15309. (defun org-buffer-list (&optional predicate exclude-tmp)
  15310. "Return a list of Org buffers.
  15311. PREDICATE can be `export', `files' or `agenda'.
  15312. export restrict the list to Export buffers.
  15313. files restrict the list to buffers visiting Org files.
  15314. agenda restrict the list to buffers visiting agenda files.
  15315. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15316. (let* ((bfn nil)
  15317. (agenda-files (and (eq predicate 'agenda)
  15318. (mapcar 'file-truename (org-agenda-files t))))
  15319. (filter
  15320. (cond
  15321. ((eq predicate 'files)
  15322. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15323. ((eq predicate 'export)
  15324. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15325. ((eq predicate 'agenda)
  15326. (lambda (b)
  15327. (with-current-buffer b
  15328. (and (derived-mode-p 'org-mode)
  15329. (setq bfn (buffer-file-name b))
  15330. (member (file-truename bfn) agenda-files)))))
  15331. (t (lambda (b) (with-current-buffer b
  15332. (or (derived-mode-p 'org-mode)
  15333. (string-match "\*Org .*Export"
  15334. (buffer-name b)))))))))
  15335. (delq nil
  15336. (mapcar
  15337. (lambda(b)
  15338. (if (and (funcall filter b)
  15339. (or (not exclude-tmp)
  15340. (not (string-match "tmp" (buffer-name b)))))
  15341. b
  15342. nil))
  15343. (buffer-list)))))
  15344. (defun org-agenda-files (&optional unrestricted archives)
  15345. "Get the list of agenda files.
  15346. Optional UNRESTRICTED means return the full list even if a restriction
  15347. is currently in place.
  15348. When ARCHIVES is t, include all archive files that are really being
  15349. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15350. `org-agenda-archives-mode' is t."
  15351. (let ((files
  15352. (cond
  15353. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15354. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15355. ((listp org-agenda-files) org-agenda-files)
  15356. (t (error "Invalid value of `org-agenda-files'")))))
  15357. (setq files (apply 'append
  15358. (mapcar (lambda (f)
  15359. (if (file-directory-p f)
  15360. (directory-files
  15361. f t org-agenda-file-regexp)
  15362. (list f)))
  15363. files)))
  15364. (when org-agenda-skip-unavailable-files
  15365. (setq files (delq nil
  15366. (mapcar (function
  15367. (lambda (file)
  15368. (and (file-readable-p file) file)))
  15369. files))))
  15370. (when (or (eq archives t)
  15371. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15372. (setq files (org-add-archive-files files)))
  15373. files))
  15374. (defun org-agenda-file-p (&optional file)
  15375. "Return non-nil, if FILE is an agenda file.
  15376. If FILE is omitted, use the file associated with the current
  15377. buffer."
  15378. (member (or file (buffer-file-name))
  15379. (org-agenda-files t)))
  15380. (defun org-edit-agenda-file-list ()
  15381. "Edit the list of agenda files.
  15382. Depending on setup, this either uses customize to edit the variable
  15383. `org-agenda-files', or it visits the file that is holding the list. In the
  15384. latter case, the buffer is set up in a way that saving it automatically kills
  15385. the buffer and restores the previous window configuration."
  15386. (interactive)
  15387. (if (stringp org-agenda-files)
  15388. (let ((cw (current-window-configuration)))
  15389. (find-file org-agenda-files)
  15390. (org-set-local 'org-window-configuration cw)
  15391. (org-add-hook 'after-save-hook
  15392. (lambda ()
  15393. (set-window-configuration
  15394. (prog1 org-window-configuration
  15395. (kill-buffer (current-buffer))))
  15396. (org-install-agenda-files-menu)
  15397. (message "New agenda file list installed"))
  15398. nil 'local)
  15399. (message "%s" (substitute-command-keys
  15400. "Edit list and finish with \\[save-buffer]")))
  15401. (customize-variable 'org-agenda-files)))
  15402. (defun org-store-new-agenda-file-list (list)
  15403. "Set new value for the agenda file list and save it correctly."
  15404. (if (stringp org-agenda-files)
  15405. (let ((fe (org-read-agenda-file-list t)) b u)
  15406. (while (setq b (find-buffer-visiting org-agenda-files))
  15407. (kill-buffer b))
  15408. (with-temp-file org-agenda-files
  15409. (insert
  15410. (mapconcat
  15411. (lambda (f) ;; Keep un-expanded entries.
  15412. (if (setq u (assoc f fe))
  15413. (cdr u)
  15414. f))
  15415. list "\n")
  15416. "\n")))
  15417. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15418. (org-insert-mode-line-in-empty-file nil))
  15419. (setq org-agenda-files list)
  15420. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15421. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15422. "Read the list of agenda files from a file.
  15423. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15424. filenames, used by `org-store-new-agenda-file-list' to write back
  15425. un-expanded file names."
  15426. (when (file-directory-p org-agenda-files)
  15427. (error "`org-agenda-files' cannot be a single directory"))
  15428. (when (stringp org-agenda-files)
  15429. (with-temp-buffer
  15430. (insert-file-contents org-agenda-files)
  15431. (mapcar
  15432. (lambda (f)
  15433. (let ((e (expand-file-name (substitute-in-file-name f)
  15434. org-directory)))
  15435. (if pair-with-expansion
  15436. (cons e f)
  15437. e)))
  15438. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15439. ;;;###autoload
  15440. (defun org-cycle-agenda-files ()
  15441. "Cycle through the files in `org-agenda-files'.
  15442. If the current buffer visits an agenda file, find the next one in the list.
  15443. If the current buffer does not, find the first agenda file."
  15444. (interactive)
  15445. (let* ((fs (org-agenda-files t))
  15446. (files (append fs (list (car fs))))
  15447. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15448. file)
  15449. (unless files (error "No agenda files"))
  15450. (catch 'exit
  15451. (while (setq file (pop files))
  15452. (if (equal (file-truename file) tcf)
  15453. (when (car files)
  15454. (find-file (car files))
  15455. (throw 'exit t))))
  15456. (find-file (car fs)))
  15457. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15458. (defun org-agenda-file-to-front (&optional to-end)
  15459. "Move/add the current file to the top of the agenda file list.
  15460. If the file is not present in the list, it is added to the front. If it is
  15461. present, it is moved there. With optional argument TO-END, add/move to the
  15462. end of the list."
  15463. (interactive "P")
  15464. (let ((org-agenda-skip-unavailable-files nil)
  15465. (file-alist (mapcar (lambda (x)
  15466. (cons (file-truename x) x))
  15467. (org-agenda-files t)))
  15468. (ctf (file-truename
  15469. (or buffer-file-name
  15470. (error "Please save the current buffer to a file"))))
  15471. x had)
  15472. (setq x (assoc ctf file-alist) had x)
  15473. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15474. (if to-end
  15475. (setq file-alist (append (delq x file-alist) (list x)))
  15476. (setq file-alist (cons x (delq x file-alist))))
  15477. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15478. (org-install-agenda-files-menu)
  15479. (message "File %s to %s of agenda file list"
  15480. (if had "moved" "added") (if to-end "end" "front"))))
  15481. (defun org-remove-file (&optional file)
  15482. "Remove current file from the list of files in variable `org-agenda-files'.
  15483. These are the files which are being checked for agenda entries.
  15484. Optional argument FILE means use this file instead of the current."
  15485. (interactive)
  15486. (let* ((org-agenda-skip-unavailable-files nil)
  15487. (file (or file buffer-file-name
  15488. (error "Current buffer does not visit a file")))
  15489. (true-file (file-truename file))
  15490. (afile (abbreviate-file-name file))
  15491. (files (delq nil (mapcar
  15492. (lambda (x)
  15493. (if (equal true-file
  15494. (file-truename x))
  15495. nil x))
  15496. (org-agenda-files t)))))
  15497. (if (not (= (length files) (length (org-agenda-files t))))
  15498. (progn
  15499. (org-store-new-agenda-file-list files)
  15500. (org-install-agenda-files-menu)
  15501. (message "Removed file: %s" afile))
  15502. (message "File was not in list: %s (not removed)" afile))))
  15503. (defun org-file-menu-entry (file)
  15504. (vector file (list 'find-file file) t))
  15505. (defun org-check-agenda-file (file)
  15506. "Make sure FILE exists. If not, ask user what to do."
  15507. (when (not (file-exists-p file))
  15508. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15509. (abbreviate-file-name file))
  15510. (let ((r (downcase (read-char-exclusive))))
  15511. (cond
  15512. ((equal r ?r)
  15513. (org-remove-file file)
  15514. (throw 'nextfile t))
  15515. (t (error "Abort"))))))
  15516. (defun org-get-agenda-file-buffer (file)
  15517. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15518. it to the list of buffers which might be released later."
  15519. (let ((buf (org-find-base-buffer-visiting file)))
  15520. (if buf
  15521. buf ; just return it
  15522. ;; Make a new buffer and remember it
  15523. (setq buf (find-file-noselect file))
  15524. (if buf (push buf org-agenda-new-buffers))
  15525. buf)))
  15526. (defun org-release-buffers (blist)
  15527. "Release all buffers in list, asking the user for confirmation when needed.
  15528. When a buffer is unmodified, it is just killed. When modified, it is saved
  15529. \(if the user agrees) and then killed."
  15530. (let (buf file)
  15531. (while (setq buf (pop blist))
  15532. (setq file (buffer-file-name buf))
  15533. (when (and (buffer-modified-p buf)
  15534. file
  15535. (y-or-n-p (format "Save file %s? " file)))
  15536. (with-current-buffer buf (save-buffer)))
  15537. (kill-buffer buf))))
  15538. (defun org-agenda-prepare-buffers (files)
  15539. "Create buffers for all agenda files, protect archived trees and comments."
  15540. (interactive)
  15541. (let ((pa '(:org-archived t))
  15542. (pc '(:org-comment t))
  15543. (pall '(:org-archived t :org-comment t))
  15544. (inhibit-read-only t)
  15545. (rea (concat ":" org-archive-tag ":"))
  15546. bmp file re)
  15547. (save-excursion
  15548. (save-restriction
  15549. (while (setq file (pop files))
  15550. (catch 'nextfile
  15551. (if (bufferp file)
  15552. (set-buffer file)
  15553. (org-check-agenda-file file)
  15554. (set-buffer (org-get-agenda-file-buffer file)))
  15555. (widen)
  15556. (setq bmp (buffer-modified-p))
  15557. (org-refresh-category-properties)
  15558. (setq org-todo-keywords-for-agenda
  15559. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15560. (setq org-done-keywords-for-agenda
  15561. (append org-done-keywords-for-agenda org-done-keywords))
  15562. (setq org-todo-keyword-alist-for-agenda
  15563. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15564. (setq org-drawers-for-agenda
  15565. (append org-drawers-for-agenda org-drawers))
  15566. (setq org-tag-alist-for-agenda
  15567. (append org-tag-alist-for-agenda org-tag-alist))
  15568. (save-excursion
  15569. (remove-text-properties (point-min) (point-max) pall)
  15570. (when org-agenda-skip-archived-trees
  15571. (goto-char (point-min))
  15572. (while (re-search-forward rea nil t)
  15573. (if (org-at-heading-p t)
  15574. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15575. (goto-char (point-min))
  15576. (setq re (format org-heading-keyword-regexp-format
  15577. org-comment-string))
  15578. (while (re-search-forward re nil t)
  15579. (add-text-properties
  15580. (match-beginning 0) (org-end-of-subtree t) pc)))
  15581. (set-buffer-modified-p bmp)))))
  15582. (setq org-todo-keywords-for-agenda
  15583. (org-uniquify org-todo-keywords-for-agenda))
  15584. (setq org-todo-keyword-alist-for-agenda
  15585. (org-uniquify org-todo-keyword-alist-for-agenda)
  15586. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15587. ;;;; Embedded LaTeX
  15588. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15589. "Keymap for the minor `org-cdlatex-mode'.")
  15590. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15591. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15592. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15593. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15594. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15595. (defvar org-cdlatex-texmathp-advice-is-done nil
  15596. "Flag remembering if we have applied the advice to texmathp already.")
  15597. (define-minor-mode org-cdlatex-mode
  15598. "Toggle the minor `org-cdlatex-mode'.
  15599. This mode supports entering LaTeX environment and math in LaTeX fragments
  15600. in Org-mode.
  15601. \\{org-cdlatex-mode-map}"
  15602. nil " OCDL" nil
  15603. (when org-cdlatex-mode
  15604. (require 'cdlatex)
  15605. (run-hooks 'cdlatex-mode-hook)
  15606. (cdlatex-compute-tables))
  15607. (unless org-cdlatex-texmathp-advice-is-done
  15608. (setq org-cdlatex-texmathp-advice-is-done t)
  15609. (defadvice texmathp (around org-math-always-on activate)
  15610. "Always return t in org-mode buffers.
  15611. This is because we want to insert math symbols without dollars even outside
  15612. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15613. an embedded LaTeX fragment, let texmathp do its job.
  15614. \\[org-cdlatex-mode-map]"
  15615. (interactive)
  15616. (let (p)
  15617. (cond
  15618. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15619. ((eq this-command 'cdlatex-math-symbol)
  15620. (setq ad-return-value t
  15621. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15622. (t
  15623. (let ((p (org-inside-LaTeX-fragment-p)))
  15624. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15625. (setq ad-return-value t
  15626. texmathp-why '("Org-mode embedded math" . 0))
  15627. (if p ad-do-it)))))))))
  15628. (defun turn-on-org-cdlatex ()
  15629. "Unconditionally turn on `org-cdlatex-mode'."
  15630. (org-cdlatex-mode 1))
  15631. (defun org-inside-LaTeX-fragment-p ()
  15632. "Test if point is inside a LaTeX fragment.
  15633. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15634. sequence appearing also before point.
  15635. Even though the matchers for math are configurable, this function assumes
  15636. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15637. delimiters are skipped when they have been removed by customization.
  15638. The return value is nil, or a cons cell with the delimiter and the
  15639. position of this delimiter.
  15640. This function does a reasonably good job, but can locally be fooled by
  15641. for example currency specifications. For example it will assume being in
  15642. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15643. fragments that are properly closed, but during editing, we have to live
  15644. with the uncertainty caused by missing closing delimiters. This function
  15645. looks only before point, not after."
  15646. (catch 'exit
  15647. (let ((pos (point))
  15648. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15649. (lim (progn
  15650. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15651. (point)))
  15652. dd-on str (start 0) m re)
  15653. (goto-char pos)
  15654. (when dodollar
  15655. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15656. re (nth 1 (assoc "$" org-latex-regexps)))
  15657. (while (string-match re str start)
  15658. (cond
  15659. ((= (match-end 0) (length str))
  15660. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15661. ((= (match-end 0) (- (length str) 5))
  15662. (throw 'exit nil))
  15663. (t (setq start (match-end 0))))))
  15664. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15665. (goto-char pos)
  15666. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15667. (and (match-beginning 2) (throw 'exit nil))
  15668. ;; count $$
  15669. (while (re-search-backward "\\$\\$" lim t)
  15670. (setq dd-on (not dd-on)))
  15671. (goto-char pos)
  15672. (if dd-on (cons "$$" m))))))
  15673. (defun org-inside-latex-macro-p ()
  15674. "Is point inside a LaTeX macro or its arguments?"
  15675. (save-match-data
  15676. (org-in-regexp
  15677. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15678. (defun org-try-cdlatex-tab ()
  15679. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15680. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15681. - inside a LaTeX fragment, or
  15682. - after the first word in a line, where an abbreviation expansion could
  15683. insert a LaTeX environment."
  15684. (when org-cdlatex-mode
  15685. (cond
  15686. ;; Before any word on the line: No expansion possible.
  15687. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15688. ;; Just after first word on the line: Expand it. Make sure it
  15689. ;; cannot happen on headlines, though.
  15690. ((save-excursion
  15691. (skip-chars-backward "a-zA-Z0-9*")
  15692. (skip-chars-backward " \t")
  15693. (and (bolp) (not (org-at-heading-p))))
  15694. (cdlatex-tab) t)
  15695. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15696. (defun org-cdlatex-underscore-caret (&optional arg)
  15697. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15698. Revert to the normal definition outside of these fragments."
  15699. (interactive "P")
  15700. (if (org-inside-LaTeX-fragment-p)
  15701. (call-interactively 'cdlatex-sub-superscript)
  15702. (let (org-cdlatex-mode)
  15703. (call-interactively (key-binding (vector last-input-event))))))
  15704. (defun org-cdlatex-math-modify (&optional arg)
  15705. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15706. Revert to the normal definition outside of these fragments."
  15707. (interactive "P")
  15708. (if (org-inside-LaTeX-fragment-p)
  15709. (call-interactively 'cdlatex-math-modify)
  15710. (let (org-cdlatex-mode)
  15711. (call-interactively (key-binding (vector last-input-event))))))
  15712. (defvar org-latex-fragment-image-overlays nil
  15713. "List of overlays carrying the images of latex fragments.")
  15714. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15715. (defun org-remove-latex-fragment-image-overlays ()
  15716. "Remove all overlays with LaTeX fragment images in current buffer."
  15717. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15718. (setq org-latex-fragment-image-overlays nil))
  15719. (defun org-preview-latex-fragment (&optional subtree)
  15720. "Preview the LaTeX fragment at point, or all locally or globally.
  15721. If the cursor is in a LaTeX fragment, create the image and overlay
  15722. it over the source code. If there is no fragment at point, display
  15723. all fragments in the current text, from one headline to the next. With
  15724. prefix SUBTREE, display all fragments in the current subtree. With a
  15725. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15726. the cursor is before the first headline,
  15727. display all fragments in the buffer.
  15728. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15729. (interactive "P")
  15730. (unless buffer-file-name
  15731. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15732. (org-remove-latex-fragment-image-overlays)
  15733. (save-excursion
  15734. (save-restriction
  15735. (let (beg end at msg)
  15736. (cond
  15737. ((or (equal subtree '(16))
  15738. (not (save-excursion
  15739. (re-search-backward org-outline-regexp-bol nil t))))
  15740. (setq beg (point-min) end (point-max)
  15741. msg "Creating images for buffer...%s"))
  15742. ((equal subtree '(4))
  15743. (org-back-to-heading)
  15744. (setq beg (point) end (org-end-of-subtree t)
  15745. msg "Creating images for subtree...%s"))
  15746. (t
  15747. (if (setq at (org-inside-LaTeX-fragment-p))
  15748. (goto-char (max (point-min) (- (cdr at) 2)))
  15749. (org-back-to-heading))
  15750. (setq beg (point) end (progn (outline-next-heading) (point))
  15751. msg (if at "Creating image...%s"
  15752. "Creating images for entry...%s"))))
  15753. (message msg "")
  15754. (narrow-to-region beg end)
  15755. (goto-char beg)
  15756. (org-format-latex
  15757. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  15758. (file-name-nondirectory
  15759. buffer-file-name)))
  15760. default-directory 'overlays msg at 'forbuffer
  15761. org-latex-create-formula-image-program)
  15762. (message msg "done. Use `C-c C-c' to remove images.")))))
  15763. (defvar org-latex-regexps
  15764. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15765. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15766. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15767. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15768. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15769. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15770. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15771. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15772. "Regular expressions for matching embedded LaTeX.")
  15773. (defvar org-export-have-math nil) ;; dynamic scoping
  15774. (defun org-format-latex (prefix &optional dir overlays msg at
  15775. forbuffer processing-type)
  15776. "Replace LaTeX fragments with links to an image, and produce images.
  15777. Some of the options can be changed using the variable
  15778. `org-format-latex-options'."
  15779. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15780. (let* ((prefixnodir (file-name-nondirectory prefix))
  15781. (absprefix (expand-file-name prefix dir))
  15782. (todir (file-name-directory absprefix))
  15783. (opt org-format-latex-options)
  15784. (matchers (plist-get opt :matchers))
  15785. (re-list org-latex-regexps)
  15786. (org-format-latex-header-extra
  15787. (plist-get (org-infile-export-plist) :latex-header-extra))
  15788. (cnt 0) txt hash link beg end re e checkdir
  15789. executables-checked string
  15790. m n block-type block linkfile movefile ov)
  15791. ;; Check the different regular expressions
  15792. (while (setq e (pop re-list))
  15793. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15794. block (if block-type "\n\n" ""))
  15795. (when (member m matchers)
  15796. (goto-char (point-min))
  15797. (while (re-search-forward re nil t)
  15798. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15799. (not (get-text-property (match-beginning n)
  15800. 'org-protected))
  15801. (or (not overlays)
  15802. (not (eq (get-char-property (match-beginning n)
  15803. 'org-overlay-type)
  15804. 'org-latex-overlay))))
  15805. (setq org-export-have-math t)
  15806. (cond
  15807. ((eq processing-type 'verbatim)
  15808. ;; Leave the text verbatim, just protect it
  15809. (add-text-properties (match-beginning n) (match-end n)
  15810. '(org-protected t)))
  15811. ((eq processing-type 'mathjax)
  15812. ;; Prepare for MathJax processing
  15813. (setq string (match-string n))
  15814. (if (member m '("$" "$1"))
  15815. (save-excursion
  15816. (delete-region (match-beginning n) (match-end n))
  15817. (goto-char (match-beginning n))
  15818. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15819. "\\)")
  15820. '(org-protected t))))
  15821. (add-text-properties (match-beginning n) (match-end n)
  15822. '(org-protected t))))
  15823. ((or (eq processing-type 'dvipng)
  15824. (eq processing-type 'imagemagick))
  15825. ;; Process to an image
  15826. (setq txt (match-string n)
  15827. beg (match-beginning n) end (match-end n)
  15828. cnt (1+ cnt))
  15829. (let (print-length print-level) ; make sure full list is printed
  15830. (setq hash (sha1 (prin1-to-string
  15831. (list org-format-latex-header
  15832. org-format-latex-header-extra
  15833. org-export-latex-default-packages-alist
  15834. org-export-latex-packages-alist
  15835. org-format-latex-options
  15836. forbuffer txt)))
  15837. linkfile (format "%s_%s.png" prefix hash)
  15838. movefile (format "%s_%s.png" absprefix hash)))
  15839. (setq link (concat block "[[file:" linkfile "]]" block))
  15840. (if msg (message msg cnt))
  15841. (goto-char beg)
  15842. (unless checkdir ; make sure the directory exists
  15843. (setq checkdir t)
  15844. (or (file-directory-p todir) (make-directory todir t)))
  15845. (cond
  15846. ((eq processing-type 'dvipng)
  15847. (unless executables-checked
  15848. (org-check-external-command
  15849. "latex" "needed to convert LaTeX fragments to images")
  15850. (org-check-external-command
  15851. "dvipng" "needed to convert LaTeX fragments to images")
  15852. (setq executables-checked t))
  15853. (unless (file-exists-p movefile)
  15854. (org-create-formula-image-with-dvipng
  15855. txt movefile opt forbuffer)))
  15856. ((eq processing-type 'imagemagick)
  15857. (unless executables-checked
  15858. (org-check-external-command
  15859. "convert" "you need to install imagemagick")
  15860. (setq executables-checked t))
  15861. (unless (file-exists-p movefile)
  15862. (org-create-formula-image-with-imagemagick
  15863. txt movefile opt forbuffer))))
  15864. (if overlays
  15865. (progn
  15866. (mapc (lambda (o)
  15867. (if (eq (overlay-get o 'org-overlay-type)
  15868. 'org-latex-overlay)
  15869. (delete-overlay o)))
  15870. (overlays-in beg end))
  15871. (setq ov (make-overlay beg end))
  15872. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15873. (if (featurep 'xemacs)
  15874. (progn
  15875. (overlay-put ov 'invisible t)
  15876. (overlay-put
  15877. ov 'end-glyph
  15878. (make-glyph (vector 'png :file movefile))))
  15879. (overlay-put
  15880. ov 'display
  15881. (list 'image :type 'png :file movefile :ascent 'center)))
  15882. (push ov org-latex-fragment-image-overlays)
  15883. (goto-char end))
  15884. (delete-region beg end)
  15885. (insert (org-add-props link
  15886. (list 'org-latex-src
  15887. (replace-regexp-in-string
  15888. "\"" "" txt)
  15889. 'org-latex-src-embed-type
  15890. (if block-type 'paragraph 'character))))))
  15891. ((eq processing-type 'mathml)
  15892. ;; Process to MathML
  15893. (unless executables-checked
  15894. (unless (save-match-data (org-format-latex-mathml-available-p))
  15895. (error "LaTeX to MathML converter not configured"))
  15896. (setq executables-checked t))
  15897. (setq txt (match-string n)
  15898. beg (match-beginning n) end (match-end n)
  15899. cnt (1+ cnt))
  15900. (if msg (message msg cnt))
  15901. (goto-char beg)
  15902. (delete-region beg end)
  15903. (insert (org-format-latex-as-mathml
  15904. txt block-type prefix dir)))
  15905. (t
  15906. (error "Unknown conversion type %s for latex fragments"
  15907. processing-type)))))))))
  15908. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15909. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15910. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15911. sucessful, return the portion between \"<math...> </math>\"
  15912. elements otherwise return nil. When MATHML-FILE is specified,
  15913. write the results in to that file. When invoked as an
  15914. interactive command, prompt for LATEX-FRAG, with initial value
  15915. set to the current active region and echo the results for user
  15916. inspection."
  15917. (interactive (list (let ((frag (when (org-region-active-p)
  15918. (buffer-substring-no-properties
  15919. (region-beginning) (region-end)))))
  15920. (read-string "LaTeX Fragment: " frag nil frag))))
  15921. (unless latex-frag (error "Invalid latex-frag"))
  15922. (let* ((tmp-in-file (file-relative-name
  15923. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15924. (ignore (write-region latex-frag nil tmp-in-file))
  15925. (tmp-out-file (file-relative-name
  15926. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15927. (cmd (format-spec
  15928. org-latex-to-mathml-convert-command
  15929. `((?j . ,(shell-quote-argument
  15930. (expand-file-name org-latex-to-mathml-jar-file)))
  15931. (?I . ,(shell-quote-argument tmp-in-file))
  15932. (?o . ,(shell-quote-argument tmp-out-file)))))
  15933. mathml shell-command-output)
  15934. (when (org-called-interactively-p 'any)
  15935. (unless (org-format-latex-mathml-available-p)
  15936. (error "LaTeX to MathML converter not configured")))
  15937. (message "Running %s" cmd)
  15938. (setq shell-command-output (shell-command-to-string cmd))
  15939. (setq mathml
  15940. (when (file-readable-p tmp-out-file)
  15941. (with-current-buffer (find-file-noselect tmp-out-file t)
  15942. (goto-char (point-min))
  15943. (when (re-search-forward
  15944. (concat
  15945. (regexp-quote
  15946. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15947. "\\(.\\|\n\\)*"
  15948. (regexp-quote "</math>")) nil t)
  15949. (prog1 (match-string 0) (kill-buffer))))))
  15950. (cond
  15951. (mathml
  15952. (setq mathml
  15953. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15954. (when mathml-file
  15955. (write-region mathml nil mathml-file))
  15956. (when (org-called-interactively-p 'any)
  15957. (message mathml)))
  15958. ((message "LaTeX to MathML conversion failed")
  15959. (message shell-command-output)))
  15960. (delete-file tmp-in-file)
  15961. (when (file-exists-p tmp-out-file)
  15962. (delete-file tmp-out-file))
  15963. mathml))
  15964. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15965. prefix &optional dir)
  15966. "Use `org-create-math-formula' but check local cache first."
  15967. (let* ((absprefix (expand-file-name prefix dir))
  15968. (print-length nil) (print-level nil)
  15969. (formula-id (concat
  15970. "formula-"
  15971. (sha1
  15972. (prin1-to-string
  15973. (list latex-frag
  15974. org-latex-to-mathml-convert-command)))))
  15975. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15976. (formula-cache-dir (file-name-directory formula-cache)))
  15977. (unless (file-directory-p formula-cache-dir)
  15978. (make-directory formula-cache-dir t))
  15979. (unless (file-exists-p formula-cache)
  15980. (org-create-math-formula latex-frag formula-cache))
  15981. (if (file-exists-p formula-cache)
  15982. ;; Successful conversion. Return the link to MathML file.
  15983. (org-add-props
  15984. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15985. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15986. 'org-latex-src-embed-type (if latex-frag-type
  15987. 'paragraph 'character)))
  15988. ;; Failed conversion. Return the LaTeX fragment verbatim
  15989. (add-text-properties
  15990. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15991. latex-frag)))
  15992. ;; This function borrows from Ganesh Swami's latex2png.el
  15993. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  15994. "This calls dvipng."
  15995. (require 'org-latex)
  15996. (let* ((tmpdir (if (featurep 'xemacs)
  15997. (temp-directory)
  15998. temporary-file-directory))
  15999. (texfilebase (make-temp-name
  16000. (expand-file-name "orgtex" tmpdir)))
  16001. (texfile (concat texfilebase ".tex"))
  16002. (dvifile (concat texfilebase ".dvi"))
  16003. (pngfile (concat texfilebase ".png"))
  16004. (fnh (if (featurep 'xemacs)
  16005. (font-height (face-font 'default))
  16006. (face-attribute 'default :height nil)))
  16007. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16008. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16009. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16010. "Black"))
  16011. (bg (or (plist-get options (if buffer :background :html-background))
  16012. "Transparent")))
  16013. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  16014. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  16015. (with-temp-file texfile
  16016. (insert (org-splice-latex-header
  16017. org-format-latex-header
  16018. org-export-latex-default-packages-alist
  16019. org-export-latex-packages-alist t
  16020. org-format-latex-header-extra))
  16021. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16022. (require 'org-latex)
  16023. (org-export-latex-fix-inputenc))
  16024. (let ((dir default-directory))
  16025. (condition-case nil
  16026. (progn
  16027. (cd tmpdir)
  16028. (call-process "latex" nil nil nil texfile))
  16029. (error nil))
  16030. (cd dir))
  16031. (if (not (file-exists-p dvifile))
  16032. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16033. (condition-case nil
  16034. (if (featurep 'xemacs)
  16035. (call-process "dvipng" nil nil nil
  16036. "-fg" fg "-bg" bg
  16037. "-T" "tight"
  16038. "-o" pngfile
  16039. dvifile)
  16040. (call-process "dvipng" nil nil nil
  16041. "-fg" fg "-bg" bg
  16042. "-D" dpi
  16043. ;;"-x" scale "-y" scale
  16044. "-T" "tight"
  16045. "-o" pngfile
  16046. dvifile))
  16047. (error nil))
  16048. (if (not (file-exists-p pngfile))
  16049. (if org-format-latex-signal-error
  16050. (error "Failed to create png file from %s" texfile)
  16051. (message "Failed to create png file from %s" texfile)
  16052. nil)
  16053. ;; Use the requested file name and clean up
  16054. (copy-file pngfile tofile 'replace)
  16055. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16056. (if (file-exists-p (concat texfilebase e))
  16057. (delete-file (concat texfilebase e))))
  16058. pngfile))))
  16059. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16060. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16061. "This calls convert, which is included into imagemagick."
  16062. (require 'org-latex)
  16063. (let* ((tmpdir (if (featurep 'xemacs)
  16064. (temp-directory)
  16065. temporary-file-directory))
  16066. (texfilebase (make-temp-name
  16067. (expand-file-name "orgtex" tmpdir)))
  16068. (texfile (concat texfilebase ".tex"))
  16069. (pdffile (concat texfilebase ".pdf"))
  16070. (pngfile (concat texfilebase ".png"))
  16071. (fnh (if (featurep 'xemacs)
  16072. (font-height (face-font 'default))
  16073. (face-attribute 'default :height nil)))
  16074. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16075. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16076. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16077. "black"))
  16078. (bg (or (plist-get options (if buffer :background :html-background))
  16079. "white")))
  16080. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16081. (setq fg (org-latex-color-format fg)))
  16082. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16083. (setq bg (org-latex-color-format
  16084. (if (string= bg "Transparent")(setq bg "white")))))
  16085. (with-temp-file texfile
  16086. (insert (org-splice-latex-header
  16087. org-format-latex-header
  16088. org-export-latex-default-packages-alist
  16089. org-export-latex-packages-alist t
  16090. org-format-latex-header-extra))
  16091. (insert "\n\\begin{document}\n"
  16092. "\\definecolor{fg}{rgb}{" fg "}\n"
  16093. "\\definecolor{bg}{rgb}{" bg "}\n"
  16094. "\n\\pagecolor{bg}\n"
  16095. "\n{\\color{fg}\n"
  16096. string
  16097. "\n}\n"
  16098. "\n\\end{document}\n" )
  16099. (require 'org-latex)
  16100. (org-export-latex-fix-inputenc))
  16101. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16102. (condition-case nil
  16103. (progn
  16104. (cd tmpdir)
  16105. (setq cmds org-latex-to-pdf-process)
  16106. (while cmds
  16107. (setq latex-frags-cmds (pop cmds))
  16108. (if (listp latex-frags-cmds)
  16109. (setq cmds nil)
  16110. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16111. (while latex-frags-cmds
  16112. (setq cmd (pop latex-frags-cmds))
  16113. (while (string-match "%b" cmd)
  16114. (setq cmd (replace-match
  16115. (save-match-data
  16116. (shell-quote-argument texfile))
  16117. t t cmd)))
  16118. (while (string-match "%f" cmd)
  16119. (setq cmd (replace-match
  16120. (save-match-data
  16121. (shell-quote-argument (file-name-nondirectory texfile)))
  16122. t t cmd)))
  16123. (while (string-match "%o" cmd)
  16124. (setq cmd (replace-match
  16125. (save-match-data
  16126. (shell-quote-argument (file-name-directory texfile)))
  16127. t t cmd)))
  16128. (setq cmd (split-string cmd))
  16129. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16130. (error nil))
  16131. (cd dir))
  16132. (if (not (file-exists-p pdffile))
  16133. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16134. (condition-case nil
  16135. (if (featurep 'xemacs)
  16136. (call-process "convert" nil nil nil
  16137. "-density" "96"
  16138. "-trim"
  16139. "-antialias"
  16140. pdffile
  16141. "-quality" "100"
  16142. ;; "-sharpen" "0x1.0"
  16143. pngfile)
  16144. (call-process "convert" nil nil nil
  16145. "-density" dpi
  16146. "-trim"
  16147. "-antialias"
  16148. pdffile
  16149. "-quality" "100"
  16150. ; "-sharpen" "0x1.0"
  16151. pngfile))
  16152. (error nil))
  16153. (if (not (file-exists-p pngfile))
  16154. (if org-format-latex-signal-error
  16155. (error "Failed to create png file from %s" texfile)
  16156. (message "Failed to create png file from %s" texfile)
  16157. nil)
  16158. ;; Use the requested file name and clean up
  16159. (copy-file pngfile tofile 'replace)
  16160. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16161. (if (file-exists-p (concat texfilebase e))
  16162. (delete-file (concat texfilebase e))))
  16163. pngfile))))
  16164. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16165. "Fill a LaTeX header template TPL.
  16166. In the template, the following place holders will be recognized:
  16167. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16168. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16169. [PACKAGES] \\usepackage statements for PKG
  16170. [NO-PACKAGES] do not include PKG
  16171. [EXTRA] the string EXTRA
  16172. [NO-EXTRA] do not include EXTRA
  16173. For backward compatibility, if both the positive and the negative place
  16174. holder is missing, the positive one (without the \"NO-\") will be
  16175. assumed to be present at the end of the template.
  16176. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16177. EXTRA is a string.
  16178. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16179. (let (rpl (end ""))
  16180. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16181. (setq rpl (if (or (match-end 1) (not def-pkg))
  16182. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16183. tpl (replace-match rpl t t tpl))
  16184. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16185. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16186. (setq rpl (if (or (match-end 1) (not pkg))
  16187. "" (org-latex-packages-to-string pkg snippets-p t))
  16188. tpl (replace-match rpl t t tpl))
  16189. (if pkg (setq end
  16190. (concat end "\n"
  16191. (org-latex-packages-to-string pkg snippets-p)))))
  16192. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16193. (setq rpl (if (or (match-end 1) (not extra))
  16194. "" (concat extra "\n"))
  16195. tpl (replace-match rpl t t tpl))
  16196. (if (and extra (string-match "\\S-" extra))
  16197. (setq end (concat end "\n" extra))))
  16198. (if (string-match "\\S-" end)
  16199. (concat tpl "\n" end)
  16200. tpl)))
  16201. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16202. "Turn an alist of packages into a string with the \\usepackage macros."
  16203. (setq pkg (mapconcat (lambda(p)
  16204. (cond
  16205. ((stringp p) p)
  16206. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16207. (format "%% Package %s omitted" (cadr p)))
  16208. ((equal "" (car p))
  16209. (format "\\usepackage{%s}" (cadr p)))
  16210. (t
  16211. (format "\\usepackage[%s]{%s}"
  16212. (car p) (cadr p)))))
  16213. pkg
  16214. "\n"))
  16215. (if newline (concat pkg "\n") pkg))
  16216. (defun org-dvipng-color (attr)
  16217. "Return a RGB color specification for dvipng."
  16218. (apply 'format "rgb %s %s %s"
  16219. (mapcar 'org-normalize-color
  16220. (if (featurep 'xemacs)
  16221. (color-rgb-components
  16222. (face-property 'default
  16223. (cond ((eq attr :foreground) 'foreground)
  16224. ((eq attr :background) 'background))))
  16225. (color-values (face-attribute 'default attr nil))))))
  16226. (defun org-latex-color (attr)
  16227. "Return a RGB color for the LaTeX color package."
  16228. (apply 'format "%s,%s,%s"
  16229. (mapcar 'org-normalize-color
  16230. (if (featurep 'xemacs)
  16231. (color-rgb-components
  16232. (face-property 'default
  16233. (cond ((eq attr :foreground) 'foreground)
  16234. ((eq attr :background) 'background))))
  16235. (color-values (face-attribute 'default attr nil))))))
  16236. (defun org-latex-color-format (color-name)
  16237. "Convert COLOR-NAME to a RGB color value."
  16238. (apply 'format "%s,%s,%s"
  16239. (mapcar 'org-normalize-color
  16240. (color-values color-name))))
  16241. (defun org-normalize-color (value)
  16242. "Return string to be used as color value for an RGB component."
  16243. (format "%g" (/ value 65535.0)))
  16244. ;; Image display
  16245. (defvar org-inline-image-overlays nil)
  16246. (make-variable-buffer-local 'org-inline-image-overlays)
  16247. (defun org-toggle-inline-images (&optional include-linked)
  16248. "Toggle the display of inline images.
  16249. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16250. (interactive "P")
  16251. (if org-inline-image-overlays
  16252. (progn
  16253. (org-remove-inline-images)
  16254. (message "Inline image display turned off"))
  16255. (org-display-inline-images include-linked)
  16256. (if org-inline-image-overlays
  16257. (message "%d images displayed inline"
  16258. (length org-inline-image-overlays))
  16259. (message "No images to display inline"))))
  16260. (defun org-redisplay-inline-images ()
  16261. "Refresh the display of inline images."
  16262. (interactive)
  16263. (if (not org-inline-image-overlays)
  16264. (org-toggle-inline-images)
  16265. (org-toggle-inline-images)
  16266. (org-toggle-inline-images)))
  16267. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16268. "Display inline images.
  16269. Normally only links without a description part are inlined, because this
  16270. is how it will work for export. When INCLUDE-LINKED is set, also links
  16271. with a description part will be inlined. This can be nice for a quick
  16272. look at those images, but it does not reflect what exported files will look
  16273. like.
  16274. When REFRESH is set, refresh existing images between BEG and END.
  16275. This will create new image displays only if necessary.
  16276. BEG and END default to the buffer boundaries."
  16277. (interactive "P")
  16278. (unless refresh
  16279. (org-remove-inline-images)
  16280. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16281. (save-excursion
  16282. (save-restriction
  16283. (widen)
  16284. (setq beg (or beg (point-min)) end (or end (point-max)))
  16285. (goto-char beg)
  16286. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16287. (substring (org-image-file-name-regexp) 0 -2)
  16288. "\\)\\]" (if include-linked "" "\\]")))
  16289. old file ov img)
  16290. (while (re-search-forward re end t)
  16291. (setq old (get-char-property-and-overlay (match-beginning 1)
  16292. 'org-image-overlay))
  16293. (setq file (expand-file-name
  16294. (concat (or (match-string 3) "") (match-string 4))))
  16295. (when (file-exists-p file)
  16296. (if (and (car-safe old) refresh)
  16297. (image-refresh (overlay-get (cdr old) 'display))
  16298. (setq img (save-match-data (create-image file)))
  16299. (when img
  16300. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16301. (overlay-put ov 'display img)
  16302. (overlay-put ov 'face 'default)
  16303. (overlay-put ov 'org-image-overlay t)
  16304. (overlay-put ov 'modification-hooks
  16305. (list 'org-display-inline-remove-overlay))
  16306. (push ov org-inline-image-overlays)))))))))
  16307. (define-obsolete-function-alias
  16308. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16309. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16310. "Remove inline-display overlay if a corresponding region is modified."
  16311. (let ((inhibit-modification-hooks t))
  16312. (when (and ov after)
  16313. (delete ov org-inline-image-overlays)
  16314. (delete-overlay ov))))
  16315. (defun org-remove-inline-images ()
  16316. "Remove inline display of images."
  16317. (interactive)
  16318. (mapc 'delete-overlay org-inline-image-overlays)
  16319. (setq org-inline-image-overlays nil))
  16320. ;;;; Key bindings
  16321. ;; Outline functions from `outline-mode-prefix-map'
  16322. ;; that can be remapped in Org:
  16323. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16324. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16325. (define-key org-mode-map [remap outline-forward-same-level]
  16326. 'org-forward-heading-same-level)
  16327. (define-key org-mode-map [remap outline-backward-same-level]
  16328. 'org-backward-heading-same-level)
  16329. (define-key org-mode-map [remap show-branches]
  16330. 'org-kill-note-or-show-branches)
  16331. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16332. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16333. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16334. ;; Outline functions from `outline-mode-prefix-map' that can not
  16335. ;; be remapped in Org:
  16336. ;;
  16337. ;; - the column "key binding" shows whether the Outline function is still
  16338. ;; available in Org mode on the same key that it has been bound to in
  16339. ;; Outline mode:
  16340. ;; - "overridden": key used for a different functionality in Org mode
  16341. ;; - else: key still bound to the same Outline function in Org mode
  16342. ;;
  16343. ;; | Outline function | key binding | Org replacement |
  16344. ;; |------------------------------------+-------------+-----------------------|
  16345. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16346. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16347. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16348. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16349. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16350. ;; | `show-entry' | overridden | no replacement |
  16351. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16352. ;; | `show-branches' | `C-c C-k' | still same function |
  16353. ;; | `show-subtree' | overridden | visibility cycling |
  16354. ;; | `show-all' | overridden | no replacement |
  16355. ;; | `hide-subtree' | overridden | visibility cycling |
  16356. ;; | `hide-body' | overridden | no replacement |
  16357. ;; | `hide-entry' | overridden | visibility cycling |
  16358. ;; | `hide-leaves' | overridden | no replacement |
  16359. ;; | `hide-sublevels' | overridden | no replacement |
  16360. ;; | `hide-other' | overridden | no replacement |
  16361. ;; Make `C-c C-x' a prefix key
  16362. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16363. ;; TAB key with modifiers
  16364. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16365. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16366. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16367. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16368. ;; The following line is necessary under Suse GNU/Linux
  16369. (unless (featurep 'xemacs)
  16370. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16371. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16372. (define-key org-mode-map [backtab] 'org-shifttab)
  16373. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16374. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16375. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16376. ;; Cursor keys with modifiers
  16377. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16378. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16379. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16380. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16381. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16382. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16383. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16384. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16385. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16386. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16387. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16388. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16389. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16390. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16391. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16392. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16393. ;; Babel keys
  16394. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16395. (mapc (lambda (pair)
  16396. (define-key org-babel-map (car pair) (cdr pair)))
  16397. org-babel-key-bindings)
  16398. ;;; Extra keys for tty access.
  16399. ;; We only set them when really needed because otherwise the
  16400. ;; menus don't show the simple keys
  16401. (when (or org-use-extra-keys
  16402. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16403. (not window-system))
  16404. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16405. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16406. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16407. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16408. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16409. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16410. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16411. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16412. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16413. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16414. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16415. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16416. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16417. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16418. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16419. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16420. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16421. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16422. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16423. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16424. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16425. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16426. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16427. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16428. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16429. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16430. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16431. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16432. ;; All the other keys
  16433. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16434. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16435. (if (boundp 'narrow-map)
  16436. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16437. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16438. (if (boundp 'narrow-map)
  16439. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16440. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16441. (if (boundp 'narrow-map)
  16442. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16443. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16444. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16445. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16446. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16447. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16448. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16449. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16450. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16451. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16452. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16453. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16454. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16455. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16456. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16457. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16458. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16459. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16460. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16461. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16462. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16463. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16464. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16465. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16466. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16467. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16468. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16469. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16470. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16471. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16472. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16473. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16474. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16475. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16476. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16477. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16478. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16479. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16480. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16481. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16482. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16483. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16484. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16485. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16486. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16487. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16488. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16489. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16490. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16491. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16492. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16493. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16494. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16495. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16496. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16497. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16498. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16499. (org-defkey org-mode-map "\C-m" 'org-return)
  16500. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16501. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16502. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16503. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16504. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16505. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16506. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16507. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16508. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16509. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16510. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16511. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16512. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16513. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16514. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16515. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16516. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16517. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16518. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16519. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16520. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16521. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16522. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16523. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16524. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16525. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16526. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16527. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16528. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16529. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16530. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16531. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16532. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16533. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16534. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16535. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16536. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16537. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16538. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16539. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16540. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16541. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16542. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16543. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16544. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16545. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16546. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16547. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16548. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16549. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16550. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16551. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16552. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16553. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16554. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16555. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16556. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16557. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16558. (when (featurep 'xemacs)
  16559. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16560. (defconst org-speed-commands-default
  16561. '(
  16562. ("Outline Navigation")
  16563. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16564. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16565. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16566. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16567. ("u" . (org-speed-move-safe 'outline-up-heading))
  16568. ("j" . org-goto)
  16569. ("g" . (org-refile t))
  16570. ("Outline Visibility")
  16571. ("c" . org-cycle)
  16572. ("C" . org-shifttab)
  16573. (" " . org-display-outline-path)
  16574. (":" . org-columns)
  16575. ("Outline Structure Editing")
  16576. ("U" . org-shiftmetaup)
  16577. ("D" . org-shiftmetadown)
  16578. ("r" . org-metaright)
  16579. ("l" . org-metaleft)
  16580. ("R" . org-shiftmetaright)
  16581. ("L" . org-shiftmetaleft)
  16582. ("i" . (progn (forward-char 1) (call-interactively
  16583. 'org-insert-heading-respect-content)))
  16584. ("^" . org-sort)
  16585. ("w" . org-refile)
  16586. ("a" . org-archive-subtree-default-with-confirmation)
  16587. ("." . org-mark-subtree)
  16588. ("#" . org-toggle-comment)
  16589. ("Clock Commands")
  16590. ("I" . org-clock-in)
  16591. ("O" . org-clock-out)
  16592. ("Meta Data Editing")
  16593. ("t" . org-todo)
  16594. ("," . (org-priority))
  16595. ("0" . (org-priority ?\ ))
  16596. ("1" . (org-priority ?A))
  16597. ("2" . (org-priority ?B))
  16598. ("3" . (org-priority ?C))
  16599. (";" . org-set-tags-command)
  16600. ("e" . org-set-effort)
  16601. ("E" . org-inc-effort)
  16602. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16603. (org-entry-put (point) "APPT_WARNTIME" m)))
  16604. ("Agenda Views etc")
  16605. ("v" . org-agenda)
  16606. ("/" . org-sparse-tree)
  16607. ("Misc")
  16608. ("o" . org-open-at-point)
  16609. ("?" . org-speed-command-help)
  16610. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16611. (">" . (org-agenda-remove-restriction-lock))
  16612. )
  16613. "The default speed commands.")
  16614. (defun org-print-speed-command (e)
  16615. (if (> (length (car e)) 1)
  16616. (progn
  16617. (princ "\n")
  16618. (princ (car e))
  16619. (princ "\n")
  16620. (princ (make-string (length (car e)) ?-))
  16621. (princ "\n"))
  16622. (princ (car e))
  16623. (princ " ")
  16624. (if (symbolp (cdr e))
  16625. (princ (symbol-name (cdr e)))
  16626. (prin1 (cdr e)))
  16627. (princ "\n")))
  16628. (defun org-speed-command-help ()
  16629. "Show the available speed commands."
  16630. (interactive)
  16631. (if (not org-use-speed-commands)
  16632. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16633. (with-output-to-temp-buffer "*Help*"
  16634. (princ "User-defined Speed commands\n===========================\n")
  16635. (mapc 'org-print-speed-command org-speed-commands-user)
  16636. (princ "\n")
  16637. (princ "Built-in Speed commands\n=======================\n")
  16638. (mapc 'org-print-speed-command org-speed-commands-default))
  16639. (with-current-buffer "*Help*"
  16640. (setq truncate-lines t))))
  16641. (defun org-speed-move-safe (cmd)
  16642. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16643. If not, return to the original position and throw an error."
  16644. (interactive)
  16645. (let ((pos (point)))
  16646. (call-interactively cmd)
  16647. (unless (and (bolp) (org-at-heading-p))
  16648. (goto-char pos)
  16649. (error "Boundary reached while executing %s" cmd))))
  16650. (defvar org-self-insert-command-undo-counter 0)
  16651. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16652. (defvar org-speed-command nil)
  16653. (define-obsolete-function-alias
  16654. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  16655. (defun org-speed-command-activate (keys)
  16656. "Hook for activating single-letter speed commands.
  16657. `org-speed-commands-default' specifies a minimal command set.
  16658. Use `org-speed-commands-user' for further customization."
  16659. (when (or (and (bolp) (looking-at org-outline-regexp))
  16660. (and (functionp org-use-speed-commands)
  16661. (funcall org-use-speed-commands)))
  16662. (cdr (assoc keys (append org-speed-commands-user
  16663. org-speed-commands-default)))))
  16664. (define-obsolete-function-alias
  16665. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  16666. (defun org-babel-speed-command-activate (keys)
  16667. "Hook for activating single-letter code block commands."
  16668. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16669. (cdr (assoc keys org-babel-key-bindings))))
  16670. (defcustom org-speed-command-hook
  16671. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16672. "Hook for activating speed commands at strategic locations.
  16673. Hook functions are called in sequence until a valid handler is
  16674. found.
  16675. Each hook takes a single argument, a user-pressed command key
  16676. which is also a `self-insert-command' from the global map.
  16677. Within the hook, examine the cursor position and the command key
  16678. and return nil or a valid handler as appropriate. Handler could
  16679. be one of an interactive command, a function, or a form.
  16680. Set `org-use-speed-commands' to non-nil value to enable this
  16681. hook. The default setting is `org-speed-command-activate'."
  16682. :group 'org-structure
  16683. :version "24.1"
  16684. :type 'hook)
  16685. (defun org-self-insert-command (N)
  16686. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16687. If the cursor is in a table looking at whitespace, the whitespace is
  16688. overwritten, and the table is not marked as requiring realignment."
  16689. (interactive "p")
  16690. (org-check-before-invisible-edit 'insert)
  16691. (cond
  16692. ((and org-use-speed-commands
  16693. (setq org-speed-command
  16694. (run-hook-with-args-until-success
  16695. 'org-speed-command-hook (this-command-keys))))
  16696. (cond
  16697. ((commandp org-speed-command)
  16698. (setq this-command org-speed-command)
  16699. (call-interactively org-speed-command))
  16700. ((functionp org-speed-command)
  16701. (funcall org-speed-command))
  16702. ((and org-speed-command (listp org-speed-command))
  16703. (eval org-speed-command))
  16704. (t (let (org-use-speed-commands)
  16705. (call-interactively 'org-self-insert-command)))))
  16706. ((and
  16707. (org-table-p)
  16708. (progn
  16709. ;; check if we blank the field, and if that triggers align
  16710. (and (featurep 'org-table) org-table-auto-blank-field
  16711. (member last-command
  16712. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16713. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16714. ;; got extra space, this field does not determine column width
  16715. (let (org-table-may-need-update) (org-table-blank-field))
  16716. ;; no extra space, this field may determine column width
  16717. (org-table-blank-field)))
  16718. t)
  16719. (eq N 1)
  16720. (looking-at "[^|\n]* |"))
  16721. (let (org-table-may-need-update)
  16722. (goto-char (1- (match-end 0)))
  16723. (backward-delete-char 1)
  16724. (goto-char (match-beginning 0))
  16725. (self-insert-command N)))
  16726. (t
  16727. (setq org-table-may-need-update t)
  16728. (self-insert-command N)
  16729. (org-fix-tags-on-the-fly)
  16730. (if org-self-insert-cluster-for-undo
  16731. (if (not (eq last-command 'org-self-insert-command))
  16732. (setq org-self-insert-command-undo-counter 1)
  16733. (if (>= org-self-insert-command-undo-counter 20)
  16734. (setq org-self-insert-command-undo-counter 1)
  16735. (and (> org-self-insert-command-undo-counter 0)
  16736. buffer-undo-list (listp buffer-undo-list)
  16737. (not (cadr buffer-undo-list)) ; remove nil entry
  16738. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16739. (setq org-self-insert-command-undo-counter
  16740. (1+ org-self-insert-command-undo-counter))))))))
  16741. (defun org-check-before-invisible-edit (kind)
  16742. "Check is editing if kind KIND would be dangerous with invisible text around.
  16743. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16744. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16745. (if (and org-catch-invisible-edits
  16746. (or (not (boundp 'visible-mode)) (not visible-mode))
  16747. (or (get-char-property (point) 'invisible)
  16748. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16749. ;; OK, we need to take a closer look
  16750. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16751. (invisible-before-point (if (bobp) nil (get-char-property
  16752. (1- (point)) 'invisible)))
  16753. (border-and-ok-direction
  16754. (or
  16755. ;; Check if we are acting predictably before invisible text
  16756. (and invisible-at-point (not invisible-before-point)
  16757. (memq kind '(insert delete-backward)))
  16758. ;; Check if we are acting predictably after invisible text
  16759. ;; This works not well, and I have turned it off. It seems
  16760. ;; better to always show and stop after invisible text.
  16761. ;; (and (not invisible-at-point) invisible-before-point
  16762. ;; (memq kind '(insert delete)))
  16763. )))
  16764. (when (or (memq invisible-at-point '(outline org-hide-block t))
  16765. (memq invisible-before-point '(outline org-hide-block t)))
  16766. (if (eq org-catch-invisible-edits 'error)
  16767. (error "Editing in invisible areas is prohibited - make visible first"))
  16768. (if (and org-custom-properties-overlays
  16769. (y-or-n-p "Display invisible properties in this buffer? "))
  16770. (org-toggle-custom-properties-visibility)
  16771. ;; Make the area visible
  16772. (save-excursion
  16773. (if invisible-before-point
  16774. (goto-char (previous-single-char-property-change
  16775. (point) 'invisible)))
  16776. (org-cycle))
  16777. (cond
  16778. ((eq org-catch-invisible-edits 'show)
  16779. ;; That's it, we do the edit after showing
  16780. (message
  16781. "Unfolding invisible region around point before editing")
  16782. (sit-for 1))
  16783. ((and (eq org-catch-invisible-edits 'smart)
  16784. border-and-ok-direction)
  16785. (message "Unfolding invisible region around point before editing"))
  16786. (t
  16787. ;; Don't do the edit, make the user repeat it in full visibility
  16788. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  16789. (defun org-fix-tags-on-the-fly ()
  16790. (when (and (equal (char-after (point-at-bol)) ?*)
  16791. (org-at-heading-p))
  16792. (org-align-tags-here org-tags-column)))
  16793. (defun org-delete-backward-char (N)
  16794. "Like `delete-backward-char', insert whitespace at field end in tables.
  16795. When deleting backwards, in tables this function will insert whitespace in
  16796. front of the next \"|\" separator, to keep the table aligned. The table will
  16797. still be marked for re-alignment if the field did fill the entire column,
  16798. because, in this case the deletion might narrow the column."
  16799. (interactive "p")
  16800. (save-match-data
  16801. (org-check-before-invisible-edit 'delete-backward)
  16802. (if (and (org-table-p)
  16803. (eq N 1)
  16804. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16805. (looking-at ".*?|"))
  16806. (let ((pos (point))
  16807. (noalign (looking-at "[^|\n\r]* |"))
  16808. (c org-table-may-need-update))
  16809. (backward-delete-char N)
  16810. (if (not overwrite-mode)
  16811. (progn
  16812. (skip-chars-forward "^|")
  16813. (insert " ")
  16814. (goto-char (1- pos))))
  16815. ;; noalign: if there were two spaces at the end, this field
  16816. ;; does not determine the width of the column.
  16817. (if noalign (setq org-table-may-need-update c)))
  16818. (backward-delete-char N)
  16819. (org-fix-tags-on-the-fly))))
  16820. (defun org-delete-char (N)
  16821. "Like `delete-char', but insert whitespace at field end in tables.
  16822. When deleting characters, in tables this function will insert whitespace in
  16823. front of the next \"|\" separator, to keep the table aligned. The table will
  16824. still be marked for re-alignment if the field did fill the entire column,
  16825. because, in this case the deletion might narrow the column."
  16826. (interactive "p")
  16827. (save-match-data
  16828. (org-check-before-invisible-edit 'delete)
  16829. (if (and (org-table-p)
  16830. (not (bolp))
  16831. (not (= (char-after) ?|))
  16832. (eq N 1))
  16833. (if (looking-at ".*?|")
  16834. (let ((pos (point))
  16835. (noalign (looking-at "[^|\n\r]* |"))
  16836. (c org-table-may-need-update))
  16837. (replace-match (concat
  16838. (substring (match-string 0) 1 -1)
  16839. " |"))
  16840. (goto-char pos)
  16841. ;; noalign: if there were two spaces at the end, this field
  16842. ;; does not determine the width of the column.
  16843. (if noalign (setq org-table-may-need-update c)))
  16844. (delete-char N))
  16845. (delete-char N)
  16846. (org-fix-tags-on-the-fly))))
  16847. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16848. (put 'org-self-insert-command 'delete-selection t)
  16849. (put 'orgtbl-self-insert-command 'delete-selection t)
  16850. (put 'org-delete-char 'delete-selection 'supersede)
  16851. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16852. (put 'org-yank 'delete-selection 'yank)
  16853. ;; Make `flyspell-mode' delay after some commands
  16854. (put 'org-self-insert-command 'flyspell-delayed t)
  16855. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16856. (put 'org-delete-char 'flyspell-delayed t)
  16857. (put 'org-delete-backward-char 'flyspell-delayed t)
  16858. ;; Make pabbrev-mode expand after org-mode commands
  16859. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16860. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16861. ;; How to do this: Measure non-white length of current string
  16862. ;; If equal to column width, we should realign.
  16863. (defun org-remap (map &rest commands)
  16864. "In MAP, remap the functions given in COMMANDS.
  16865. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16866. (let (new old)
  16867. (while commands
  16868. (setq old (pop commands) new (pop commands))
  16869. (if (fboundp 'command-remapping)
  16870. (org-defkey map (vector 'remap old) new)
  16871. (substitute-key-definition old new map global-map)))))
  16872. (when (eq org-enable-table-editor 'optimized)
  16873. ;; If the user wants maximum table support, we need to hijack
  16874. ;; some standard editing functions
  16875. (org-remap org-mode-map
  16876. 'self-insert-command 'org-self-insert-command
  16877. 'delete-char 'org-delete-char
  16878. 'delete-backward-char 'org-delete-backward-char)
  16879. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16880. (defvar org-ctrl-c-ctrl-c-hook nil
  16881. "Hook for functions attaching themselves to `C-c C-c'.
  16882. This can be used to add additional functionality to the C-c C-c
  16883. key which executes context-dependent commands. This hook is run
  16884. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16885. run after the last test.
  16886. Each function will be called with no arguments. The function
  16887. must check if the context is appropriate for it to act. If yes,
  16888. it should do its thing and then return a non-nil value. If the
  16889. context is wrong, just do nothing and return nil.")
  16890. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16891. "Hook for functions attaching themselves to `C-c C-c'.
  16892. This can be used to add additional functionality to the C-c C-c
  16893. key which executes context-dependent commands. This hook is run
  16894. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16895. before the first test.
  16896. Each function will be called with no arguments. The function
  16897. must check if the context is appropriate for it to act. If yes,
  16898. it should do its thing and then return a non-nil value. If the
  16899. context is wrong, just do nothing and return nil.")
  16900. (defvar org-tab-first-hook nil
  16901. "Hook for functions to attach themselves to TAB.
  16902. See `org-ctrl-c-ctrl-c-hook' for more information.
  16903. This hook runs as the first action when TAB is pressed, even before
  16904. `org-cycle' messes around with the `outline-regexp' to cater for
  16905. inline tasks and plain list item folding.
  16906. If any function in this hook returns t, any other actions that
  16907. would have been caused by TAB (such as table field motion or visibility
  16908. cycling) will not occur.")
  16909. (defvar org-tab-after-check-for-table-hook nil
  16910. "Hook for functions to attach themselves to TAB.
  16911. See `org-ctrl-c-ctrl-c-hook' for more information.
  16912. This hook runs after it has been established that the cursor is not in a
  16913. table, but before checking if the cursor is in a headline or if global cycling
  16914. should be done.
  16915. If any function in this hook returns t, not other actions like visibility
  16916. cycling will be done.")
  16917. (defvar org-tab-after-check-for-cycling-hook nil
  16918. "Hook for functions to attach themselves to TAB.
  16919. See `org-ctrl-c-ctrl-c-hook' for more information.
  16920. This hook runs after it has been established that not table field motion and
  16921. not visibility should be done because of current context. This is probably
  16922. the place where a package like yasnippets can hook in.")
  16923. (defvar org-tab-before-tab-emulation-hook nil
  16924. "Hook for functions to attach themselves to TAB.
  16925. See `org-ctrl-c-ctrl-c-hook' for more information.
  16926. This hook runs after every other options for TAB have been exhausted, but
  16927. before indentation and \t insertion takes place.")
  16928. (defvar org-metaleft-hook nil
  16929. "Hook for functions attaching themselves to `M-left'.
  16930. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16931. (defvar org-metaright-hook nil
  16932. "Hook for functions attaching themselves to `M-right'.
  16933. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16934. (defvar org-metaup-hook nil
  16935. "Hook for functions attaching themselves to `M-up'.
  16936. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16937. (defvar org-metadown-hook nil
  16938. "Hook for functions attaching themselves to `M-down'.
  16939. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16940. (defvar org-shiftmetaleft-hook nil
  16941. "Hook for functions attaching themselves to `M-S-left'.
  16942. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16943. (defvar org-shiftmetaright-hook nil
  16944. "Hook for functions attaching themselves to `M-S-right'.
  16945. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16946. (defvar org-shiftmetaup-hook nil
  16947. "Hook for functions attaching themselves to `M-S-up'.
  16948. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16949. (defvar org-shiftmetadown-hook nil
  16950. "Hook for functions attaching themselves to `M-S-down'.
  16951. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16952. (defvar org-metareturn-hook nil
  16953. "Hook for functions attaching themselves to `M-RET'.
  16954. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16955. (defvar org-shiftup-hook nil
  16956. "Hook for functions attaching themselves to `S-up'.
  16957. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16958. (defvar org-shiftup-final-hook nil
  16959. "Hook for functions attaching themselves to `S-up'.
  16960. This one runs after all other options except shift-select have been excluded.
  16961. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16962. (defvar org-shiftdown-hook nil
  16963. "Hook for functions attaching themselves to `S-down'.
  16964. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16965. (defvar org-shiftdown-final-hook nil
  16966. "Hook for functions attaching themselves to `S-down'.
  16967. This one runs after all other options except shift-select have been excluded.
  16968. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16969. (defvar org-shiftleft-hook nil
  16970. "Hook for functions attaching themselves to `S-left'.
  16971. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16972. (defvar org-shiftleft-final-hook nil
  16973. "Hook for functions attaching themselves to `S-left'.
  16974. This one runs after all other options except shift-select have been excluded.
  16975. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16976. (defvar org-shiftright-hook nil
  16977. "Hook for functions attaching themselves to `S-right'.
  16978. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16979. (defvar org-shiftright-final-hook nil
  16980. "Hook for functions attaching themselves to `S-right'.
  16981. This one runs after all other options except shift-select have been excluded.
  16982. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16983. (defun org-modifier-cursor-error ()
  16984. "Throw an error, a modified cursor command was applied in wrong context."
  16985. (error "This command is active in special context like tables, headlines or items"))
  16986. (defun org-shiftselect-error ()
  16987. "Throw an error because Shift-Cursor command was applied in wrong context."
  16988. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16989. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16990. (error "This command works only in special context like headlines or timestamps")))
  16991. (defun org-call-for-shift-select (cmd)
  16992. (let ((this-command-keys-shift-translated t))
  16993. (call-interactively cmd)))
  16994. (defun org-shifttab (&optional arg)
  16995. "Global visibility cycling or move to previous table field.
  16996. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16997. on context.
  16998. See the individual commands for more information."
  16999. (interactive "P")
  17000. (cond
  17001. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17002. ((integerp arg)
  17003. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17004. (message "Content view to level: %d" arg)
  17005. (org-content (prefix-numeric-value arg2))
  17006. (setq org-cycle-global-status 'overview)))
  17007. (t (call-interactively 'org-global-cycle))))
  17008. (defun org-shiftmetaleft ()
  17009. "Promote subtree or delete table column.
  17010. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17011. `org-table-delete-column', depending on context. See the
  17012. individual commands for more information."
  17013. (interactive)
  17014. (cond
  17015. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17016. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17017. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17018. ((if (not (org-region-active-p)) (org-at-item-p)
  17019. (save-excursion (goto-char (region-beginning))
  17020. (org-at-item-p)))
  17021. (call-interactively 'org-outdent-item-tree))
  17022. (t (org-modifier-cursor-error))))
  17023. (defun org-shiftmetaright ()
  17024. "Demote subtree or insert table column.
  17025. Calls `org-demote-subtree', `org-indent-item-tree', or
  17026. `org-table-insert-column', depending on context. See the
  17027. individual commands for more information."
  17028. (interactive)
  17029. (cond
  17030. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17031. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17032. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17033. ((if (not (org-region-active-p)) (org-at-item-p)
  17034. (save-excursion (goto-char (region-beginning))
  17035. (org-at-item-p)))
  17036. (call-interactively 'org-indent-item-tree))
  17037. (t (org-modifier-cursor-error))))
  17038. (defun org-shiftmetaup (&optional arg)
  17039. "Move subtree up or kill table row.
  17040. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17041. `org-move-item-up' or `org-timestamp-up', depending on context.
  17042. See the individual commands for more information."
  17043. (interactive "P")
  17044. (cond
  17045. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17046. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17047. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17048. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17049. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17050. (call-interactively 'org-timestamp-up)))
  17051. (t (org-modifier-cursor-error))))
  17052. (defun org-shiftmetadown (&optional arg)
  17053. "Move subtree down or insert table row.
  17054. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17055. `org-move-item-down' or `org-timestamp-up', depending on context.
  17056. See the individual commands for more information."
  17057. (interactive "P")
  17058. (cond
  17059. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17060. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17061. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17062. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17063. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17064. (call-interactively 'org-timestamp-down)))
  17065. (t (org-modifier-cursor-error))))
  17066. (defsubst org-hidden-tree-error ()
  17067. (error
  17068. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17069. (defun org-metaleft (&optional arg)
  17070. "Promote heading or move table column to left.
  17071. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17072. With no specific context, calls the Emacs default `backward-word'.
  17073. See the individual commands for more information."
  17074. (interactive "P")
  17075. (cond
  17076. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17077. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17078. ((org-with-limited-levels
  17079. (or (org-at-heading-p)
  17080. (and (org-region-active-p)
  17081. (save-excursion
  17082. (goto-char (region-beginning))
  17083. (org-at-heading-p)))))
  17084. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17085. (call-interactively 'org-do-promote))
  17086. ;; At an inline task.
  17087. ((org-at-heading-p)
  17088. (call-interactively 'org-inlinetask-promote))
  17089. ((or (org-at-item-p)
  17090. (and (org-region-active-p)
  17091. (save-excursion
  17092. (goto-char (region-beginning))
  17093. (org-at-item-p))))
  17094. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17095. (call-interactively 'org-outdent-item))
  17096. (t (call-interactively 'backward-word))))
  17097. (defun org-metaright (&optional arg)
  17098. "Demote a subtree, a list item or move table column to right.
  17099. In front of a drawer or a block keyword, indent it correctly.
  17100. With no specific context, calls the Emacs default `forward-word'.
  17101. See the individual commands for more information."
  17102. (interactive "P")
  17103. (cond
  17104. ((run-hook-with-args-until-success 'org-metaright-hook))
  17105. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17106. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17107. ((org-at-block-p) (call-interactively 'org-indent-block))
  17108. ((org-with-limited-levels
  17109. (or (org-at-heading-p)
  17110. (and (org-region-active-p)
  17111. (save-excursion
  17112. (goto-char (region-beginning))
  17113. (org-at-heading-p)))))
  17114. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17115. (call-interactively 'org-do-demote))
  17116. ;; At an inline task.
  17117. ((org-at-heading-p)
  17118. (call-interactively 'org-inlinetask-demote))
  17119. ((or (org-at-item-p)
  17120. (and (org-region-active-p)
  17121. (save-excursion
  17122. (goto-char (region-beginning))
  17123. (org-at-item-p))))
  17124. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17125. (call-interactively 'org-indent-item))
  17126. (t (call-interactively 'forward-word))))
  17127. (defun org-check-for-hidden (what)
  17128. "Check if there are hidden headlines/items in the current visual line.
  17129. WHAT can be either `headlines' or `items'. If the current line is
  17130. an outline or item heading and it has a folded subtree below it,
  17131. this function returns t, nil otherwise."
  17132. (let ((re (cond
  17133. ((eq what 'headlines) org-outline-regexp-bol)
  17134. ((eq what 'items) (org-item-beginning-re))
  17135. (t (error "This should not happen"))))
  17136. beg end)
  17137. (save-excursion
  17138. (catch 'exit
  17139. (unless (org-region-active-p)
  17140. (setq beg (point-at-bol))
  17141. (beginning-of-line 2)
  17142. (while (and (not (eobp)) ;; this is like `next-line'
  17143. (get-char-property (1- (point)) 'invisible))
  17144. (beginning-of-line 2))
  17145. (setq end (point))
  17146. (goto-char beg)
  17147. (goto-char (point-at-eol))
  17148. (setq end (max end (point)))
  17149. (while (re-search-forward re end t)
  17150. (if (get-char-property (match-beginning 0) 'invisible)
  17151. (throw 'exit t))))
  17152. nil))))
  17153. (autoload 'org-element-at-point "org-element")
  17154. (autoload 'org-element-type "org-element")
  17155. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  17156. (declare-function org-element-type "org-element" (element))
  17157. (declare-function org-element-contents "org-element" (element))
  17158. (declare-function org-element-property "org-element" (property element))
  17159. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  17160. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  17161. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  17162. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  17163. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  17164. (defun org-metaup (&optional arg)
  17165. "Move subtree up or move table row up.
  17166. Calls `org-move-subtree-up' or `org-table-move-row' or
  17167. `org-move-item-up', depending on context. See the individual commands
  17168. for more information."
  17169. (interactive "P")
  17170. (cond
  17171. ((run-hook-with-args-until-success 'org-metaup-hook))
  17172. ((org-region-active-p)
  17173. (let* ((a (min (region-beginning) (region-end)))
  17174. (b (1- (max (region-beginning) (region-end))))
  17175. (c (save-excursion (goto-char a)
  17176. (move-beginning-of-line 0)))
  17177. (d (save-excursion (goto-char a)
  17178. (move-end-of-line 0) (point))))
  17179. (transpose-regions a b c d)
  17180. (goto-char c)))
  17181. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17182. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17183. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17184. (t (org-drag-element-backward))))
  17185. (defun org-metadown (&optional arg)
  17186. "Move subtree down or move table row down.
  17187. Calls `org-move-subtree-down' or `org-table-move-row' or
  17188. `org-move-item-down', depending on context. See the individual
  17189. commands for more information."
  17190. (interactive "P")
  17191. (cond
  17192. ((run-hook-with-args-until-success 'org-metadown-hook))
  17193. ((org-region-active-p)
  17194. (let* ((a (min (region-beginning) (region-end)))
  17195. (b (max (region-beginning) (region-end)))
  17196. (c (save-excursion (goto-char b)
  17197. (move-beginning-of-line 1)))
  17198. (d (save-excursion (goto-char b)
  17199. (move-end-of-line 1) (1+ (point)))))
  17200. (transpose-regions a b c d)
  17201. (goto-char d)))
  17202. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17203. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17204. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17205. (t (org-drag-element-forward))))
  17206. (defun org-shiftup (&optional arg)
  17207. "Increase item in timestamp or increase priority of current headline.
  17208. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17209. depending on context. See the individual commands for more information."
  17210. (interactive "P")
  17211. (cond
  17212. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17213. ((and org-support-shift-select (org-region-active-p))
  17214. (org-call-for-shift-select 'previous-line))
  17215. ((org-at-timestamp-p t)
  17216. (call-interactively (if org-edit-timestamp-down-means-later
  17217. 'org-timestamp-down 'org-timestamp-up)))
  17218. ((and (not (eq org-support-shift-select 'always))
  17219. org-enable-priority-commands
  17220. (org-at-heading-p))
  17221. (call-interactively 'org-priority-up))
  17222. ((and (not org-support-shift-select) (org-at-item-p))
  17223. (call-interactively 'org-previous-item))
  17224. ((org-clocktable-try-shift 'up arg))
  17225. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17226. (org-support-shift-select
  17227. (org-call-for-shift-select 'previous-line))
  17228. (t (org-shiftselect-error))))
  17229. (defun org-shiftdown (&optional arg)
  17230. "Decrease item in timestamp or decrease priority of current headline.
  17231. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17232. depending on context. See the individual commands for more information."
  17233. (interactive "P")
  17234. (cond
  17235. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17236. ((and org-support-shift-select (org-region-active-p))
  17237. (org-call-for-shift-select 'next-line))
  17238. ((org-at-timestamp-p t)
  17239. (call-interactively (if org-edit-timestamp-down-means-later
  17240. 'org-timestamp-up 'org-timestamp-down)))
  17241. ((and (not (eq org-support-shift-select 'always))
  17242. org-enable-priority-commands
  17243. (org-at-heading-p))
  17244. (call-interactively 'org-priority-down))
  17245. ((and (not org-support-shift-select) (org-at-item-p))
  17246. (call-interactively 'org-next-item))
  17247. ((org-clocktable-try-shift 'down arg))
  17248. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17249. (org-support-shift-select
  17250. (org-call-for-shift-select 'next-line))
  17251. (t (org-shiftselect-error))))
  17252. (defun org-shiftright (&optional arg)
  17253. "Cycle the thing at point or in the current line, depending on context.
  17254. Depending on context, this does one of the following:
  17255. - switch a timestamp at point one day into the future
  17256. - on a headline, switch to the next TODO keyword.
  17257. - on an item, switch entire list to the next bullet type
  17258. - on a property line, switch to the next allowed value
  17259. - on a clocktable definition line, move time block into the future"
  17260. (interactive "P")
  17261. (cond
  17262. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17263. ((and org-support-shift-select (org-region-active-p))
  17264. (org-call-for-shift-select 'forward-char))
  17265. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17266. ((and (not (eq org-support-shift-select 'always))
  17267. (org-at-heading-p))
  17268. (let ((org-inhibit-logging
  17269. (not org-treat-S-cursor-todo-selection-as-state-change))
  17270. (org-inhibit-blocking
  17271. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17272. (org-call-with-arg 'org-todo 'right)))
  17273. ((or (and org-support-shift-select
  17274. (not (eq org-support-shift-select 'always))
  17275. (org-at-item-bullet-p))
  17276. (and (not org-support-shift-select) (org-at-item-p)))
  17277. (org-call-with-arg 'org-cycle-list-bullet nil))
  17278. ((and (not (eq org-support-shift-select 'always))
  17279. (org-at-property-p))
  17280. (call-interactively 'org-property-next-allowed-value))
  17281. ((org-clocktable-try-shift 'right arg))
  17282. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17283. (org-support-shift-select
  17284. (org-call-for-shift-select 'forward-char))
  17285. (t (org-shiftselect-error))))
  17286. (defun org-shiftleft (&optional arg)
  17287. "Cycle the thing at point or in the current line, depending on context.
  17288. Depending on context, this does one of the following:
  17289. - switch a timestamp at point one day into the past
  17290. - on a headline, switch to the previous TODO keyword.
  17291. - on an item, switch entire list to the previous bullet type
  17292. - on a property line, switch to the previous allowed value
  17293. - on a clocktable definition line, move time block into the past"
  17294. (interactive "P")
  17295. (cond
  17296. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17297. ((and org-support-shift-select (org-region-active-p))
  17298. (org-call-for-shift-select 'backward-char))
  17299. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17300. ((and (not (eq org-support-shift-select 'always))
  17301. (org-at-heading-p))
  17302. (let ((org-inhibit-logging
  17303. (not org-treat-S-cursor-todo-selection-as-state-change))
  17304. (org-inhibit-blocking
  17305. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17306. (org-call-with-arg 'org-todo 'left)))
  17307. ((or (and org-support-shift-select
  17308. (not (eq org-support-shift-select 'always))
  17309. (org-at-item-bullet-p))
  17310. (and (not org-support-shift-select) (org-at-item-p)))
  17311. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17312. ((and (not (eq org-support-shift-select 'always))
  17313. (org-at-property-p))
  17314. (call-interactively 'org-property-previous-allowed-value))
  17315. ((org-clocktable-try-shift 'left arg))
  17316. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17317. (org-support-shift-select
  17318. (org-call-for-shift-select 'backward-char))
  17319. (t (org-shiftselect-error))))
  17320. (defun org-shiftcontrolright ()
  17321. "Switch to next TODO set."
  17322. (interactive)
  17323. (cond
  17324. ((and org-support-shift-select (org-region-active-p))
  17325. (org-call-for-shift-select 'forward-word))
  17326. ((and (not (eq org-support-shift-select 'always))
  17327. (org-at-heading-p))
  17328. (org-call-with-arg 'org-todo 'nextset))
  17329. (org-support-shift-select
  17330. (org-call-for-shift-select 'forward-word))
  17331. (t (org-shiftselect-error))))
  17332. (defun org-shiftcontrolleft ()
  17333. "Switch to previous TODO set."
  17334. (interactive)
  17335. (cond
  17336. ((and org-support-shift-select (org-region-active-p))
  17337. (org-call-for-shift-select 'backward-word))
  17338. ((and (not (eq org-support-shift-select 'always))
  17339. (org-at-heading-p))
  17340. (org-call-with-arg 'org-todo 'previousset))
  17341. (org-support-shift-select
  17342. (org-call-for-shift-select 'backward-word))
  17343. (t (org-shiftselect-error))))
  17344. (defun org-shiftcontrolup ()
  17345. "Change timestamps synchronously up in CLOCK log lines."
  17346. (interactive)
  17347. (cond ((and (not org-support-shift-select)
  17348. (org-at-clock-log-p)
  17349. (org-at-timestamp-p t))
  17350. (org-clock-timestamps-up))
  17351. (t (org-shiftselect-error))))
  17352. (defun org-shiftcontroldown ()
  17353. "Change timestamps synchronously down in CLOCK log lines."
  17354. (interactive)
  17355. (cond ((and (not org-support-shift-select)
  17356. (org-at-clock-log-p)
  17357. (org-at-timestamp-p t))
  17358. (org-clock-timestamps-down))
  17359. (t (org-shiftselect-error))))
  17360. (defun org-ctrl-c-ret ()
  17361. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17362. (interactive)
  17363. (cond
  17364. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17365. (t (call-interactively 'org-insert-heading))))
  17366. (defun org-find-visible ()
  17367. (let ((s (point)))
  17368. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17369. (get-char-property s 'invisible)))
  17370. s))
  17371. (defun org-find-invisible ()
  17372. (let ((s (point)))
  17373. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17374. (not (get-char-property s 'invisible))))
  17375. s))
  17376. (defun org-copy-visible (beg end)
  17377. "Copy the visible parts of the region."
  17378. (interactive "r")
  17379. (let (snippets s)
  17380. (save-excursion
  17381. (save-restriction
  17382. (narrow-to-region beg end)
  17383. (setq s (goto-char (point-min)))
  17384. (while (not (= (point) (point-max)))
  17385. (goto-char (org-find-invisible))
  17386. (push (buffer-substring s (point)) snippets)
  17387. (setq s (goto-char (org-find-visible))))))
  17388. (kill-new (apply 'concat (nreverse snippets)))))
  17389. (defun org-copy-special ()
  17390. "Copy region in table or copy current subtree.
  17391. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17392. See the individual commands for more information."
  17393. (interactive)
  17394. (call-interactively
  17395. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17396. (defun org-cut-special ()
  17397. "Cut region in table or cut current subtree.
  17398. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17399. See the individual commands for more information."
  17400. (interactive)
  17401. (call-interactively
  17402. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17403. (defun org-paste-special (arg)
  17404. "Paste rectangular region into table, or past subtree relative to level.
  17405. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17406. See the individual commands for more information."
  17407. (interactive "P")
  17408. (if (org-at-table-p)
  17409. (org-table-paste-rectangle)
  17410. (org-paste-subtree arg)))
  17411. (defsubst org-in-fixed-width-region-p ()
  17412. "Is point in a fixed-width region?"
  17413. (save-match-data
  17414. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17415. (defun org-edit-special (&optional arg)
  17416. "Call a special editor for the stuff at point.
  17417. When at a table, call the formula editor with `org-table-edit-formulas'.
  17418. When in a source code block, call `org-edit-src-code'.
  17419. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17420. When in an #+include line, visit the included file.
  17421. On a link, call `ffap' to visit the link at point.
  17422. Otherwise, return a user error."
  17423. (interactive)
  17424. ;; possibly prep session before editing source
  17425. (when (and (org-in-src-block-p) arg)
  17426. (let* ((info (org-babel-get-src-block-info))
  17427. (lang (nth 0 info))
  17428. (params (nth 2 info))
  17429. (session (cdr (assoc :session params))))
  17430. (when (and info session) ;; we are in a source-code block with a session
  17431. (funcall
  17432. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17433. (cond ;; proceed with `org-edit-special'
  17434. ((save-excursion
  17435. (beginning-of-line 1)
  17436. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17437. (find-file (org-trim (match-string 1))))
  17438. ((or (org-at-table-p)
  17439. (save-excursion
  17440. (beginning-of-line 1)
  17441. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17442. (call-interactively 'org-table-edit-formulas))
  17443. ((or (org-in-block-p '("src" "example" "latex" "html"))
  17444. (org-at-table.el-p))
  17445. (org-edit-src-code))
  17446. ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
  17447. ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
  17448. (t (user-error "No special environment to edit here"))))
  17449. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17450. (defun org-ctrl-c-ctrl-c (&optional arg)
  17451. "Set tags in headline, or update according to changed information at point.
  17452. This command does many different things, depending on context:
  17453. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17454. this is what we do.
  17455. - If the cursor is on a statistics cookie, update it.
  17456. - If the cursor is in a headline, prompt for tags and insert them
  17457. into the current line, aligned to `org-tags-column'. When called
  17458. with prefix arg, realign all tags in the current buffer.
  17459. - If the cursor is in one of the special #+KEYWORD lines, this
  17460. triggers scanning the buffer for these lines and updating the
  17461. information.
  17462. - If the cursor is inside a table, realign the table. This command
  17463. works even if the automatic table editor has been turned off.
  17464. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17465. the entire table.
  17466. - If the cursor is at a footnote reference or definition, jump to
  17467. the corresponding definition or references, respectively.
  17468. - If the cursor is a the beginning of a dynamic block, update it.
  17469. - If the current buffer is a capture buffer, close note and file it.
  17470. - If the cursor is on a <<<target>>>, update radio targets and
  17471. corresponding links in this buffer.
  17472. - If the cursor is on a numbered item in a plain list, renumber the
  17473. ordered list.
  17474. - If the cursor is on a checkbox, toggle it.
  17475. - If the cursor is on a code block, evaluate it. The variable
  17476. `org-confirm-babel-evaluate' can be used to control prompting
  17477. before code block evaluation, by default every code block
  17478. evaluation requires confirmation. Code block evaluation can be
  17479. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17480. (interactive "P")
  17481. (let ((org-enable-table-editor t))
  17482. (cond
  17483. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17484. org-occur-highlights
  17485. org-latex-fragment-image-overlays)
  17486. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17487. (org-remove-occur-highlights)
  17488. (org-remove-latex-fragment-image-overlays)
  17489. (message "Temporary highlights/overlays removed from current buffer"))
  17490. ((and (local-variable-p 'org-finish-function (current-buffer))
  17491. (fboundp org-finish-function))
  17492. (funcall org-finish-function))
  17493. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17494. ((org-in-regexp org-ts-regexp-both)
  17495. (org-timestamp-change 0 'day))
  17496. ((or (looking-at org-property-start-re)
  17497. (org-at-property-p))
  17498. (call-interactively 'org-property-action))
  17499. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17500. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17501. (or (org-at-heading-p) (org-at-item-p)))
  17502. (call-interactively 'org-update-statistics-cookies))
  17503. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17504. ((org-at-table.el-p)
  17505. (message "Use C-c ' to edit table.el tables"))
  17506. ((org-at-table-p)
  17507. (org-table-maybe-eval-formula)
  17508. (if arg
  17509. (call-interactively 'org-table-recalculate)
  17510. (org-table-maybe-recalculate-line))
  17511. (call-interactively 'org-table-align)
  17512. (orgtbl-send-table 'maybe))
  17513. ((or (org-footnote-at-reference-p)
  17514. (org-footnote-at-definition-p))
  17515. (call-interactively 'org-footnote-action))
  17516. ((org-at-item-checkbox-p)
  17517. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17518. ;; list only if at top item.
  17519. (let* ((cbox (match-string 1))
  17520. (struct (org-list-struct))
  17521. (old-struct (copy-tree struct))
  17522. (parents (org-list-parents-alist struct))
  17523. (orderedp (org-entry-get nil "ORDERED"))
  17524. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17525. block-item)
  17526. ;; Use a light version of `org-toggle-checkbox' to avoid
  17527. ;; computing list structure twice.
  17528. (let ((new-box (cond
  17529. ((equal arg '(16)) "[-]")
  17530. ((equal arg '(4)) nil)
  17531. ((equal "[X]" cbox) "[ ]")
  17532. (t "[X]"))))
  17533. (if (and firstp arg)
  17534. ;; If at first item of sub-list, remove check-box from
  17535. ;; every item at the same level.
  17536. (mapc
  17537. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17538. (org-list-get-all-items
  17539. (point-at-bol) struct (org-list-prevs-alist struct)))
  17540. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17541. ;; Replicate `org-list-write-struct', while grabbing a return
  17542. ;; value from `org-list-struct-fix-box'.
  17543. (org-list-struct-fix-ind struct parents 2)
  17544. (org-list-struct-fix-item-end struct)
  17545. (let ((prevs (org-list-prevs-alist struct)))
  17546. (org-list-struct-fix-bul struct prevs)
  17547. (org-list-struct-fix-ind struct parents)
  17548. (setq block-item
  17549. (org-list-struct-fix-box struct parents prevs orderedp)))
  17550. (org-list-struct-apply-struct struct old-struct)
  17551. (org-update-checkbox-count-maybe)
  17552. (when block-item
  17553. (message
  17554. "Checkboxes were removed due to unchecked box at line %d"
  17555. (org-current-line block-item)))
  17556. (when firstp (org-list-send-list 'maybe))))
  17557. ((org-at-item-p)
  17558. ;; Cursor at an item: repair list. Do checkbox related actions
  17559. ;; only if function was called with an argument. Send list only
  17560. ;; if at top item.
  17561. (let* ((struct (org-list-struct))
  17562. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17563. old-struct)
  17564. (when arg
  17565. (setq old-struct (copy-tree struct))
  17566. (if firstp
  17567. ;; If at first item of sub-list, add check-box to every
  17568. ;; item at the same level.
  17569. (mapc
  17570. (lambda (pos)
  17571. (unless (org-list-get-checkbox pos struct)
  17572. (org-list-set-checkbox pos struct "[ ]")))
  17573. (org-list-get-all-items
  17574. (point-at-bol) struct (org-list-prevs-alist struct)))
  17575. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17576. (org-list-write-struct
  17577. struct (org-list-parents-alist struct) old-struct)
  17578. (when arg (org-update-checkbox-count-maybe))
  17579. (when firstp (org-list-send-list 'maybe))))
  17580. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17581. ;; Dynamic block
  17582. (beginning-of-line 1)
  17583. (save-excursion (org-update-dblock)))
  17584. ((save-excursion
  17585. (let ((case-fold-search t))
  17586. (beginning-of-line 1)
  17587. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17588. (cond
  17589. ((or (equal (match-string 1) "TBLFM")
  17590. (equal (match-string 1) "tblfm"))
  17591. ;; Recalculate the table before this line
  17592. (save-excursion
  17593. (beginning-of-line 1)
  17594. (skip-chars-backward " \r\n\t")
  17595. (if (org-at-table-p)
  17596. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17597. (t
  17598. (let ((org-inhibit-startup-visibility-stuff t)
  17599. (org-startup-align-all-tables nil))
  17600. (when (boundp 'org-table-coordinate-overlays)
  17601. (mapc 'delete-overlay org-table-coordinate-overlays)
  17602. (setq org-table-coordinate-overlays nil))
  17603. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17604. (message "Local setup has been refreshed"))))
  17605. ((org-clock-update-time-maybe))
  17606. (t
  17607. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17608. (error "C-c C-c can do nothing useful at this location"))))))
  17609. (defun org-mode-restart ()
  17610. "Restart Org-mode, to scan again for special lines.
  17611. Also updates the keyword regular expressions."
  17612. (interactive)
  17613. (org-mode)
  17614. (message "Org-mode restarted"))
  17615. (defun org-kill-note-or-show-branches ()
  17616. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17617. (interactive)
  17618. (if (not org-finish-function)
  17619. (progn
  17620. (hide-subtree)
  17621. (call-interactively 'show-branches))
  17622. (let ((org-note-abort t))
  17623. (funcall org-finish-function))))
  17624. (defun org-return (&optional indent)
  17625. "Goto next table row or insert a newline.
  17626. Calls `org-table-next-row' or `newline', depending on context.
  17627. See the individual commands for more information."
  17628. (interactive)
  17629. (let (org-ts-what)
  17630. (cond
  17631. ((or (bobp) (org-in-src-block-p))
  17632. (if indent (newline-and-indent) (newline)))
  17633. ((org-at-table-p)
  17634. (org-table-justify-field-maybe)
  17635. (call-interactively 'org-table-next-row))
  17636. ;; when `newline-and-indent' is called within a list, make sure
  17637. ;; text moved stays inside the item.
  17638. ((and (org-in-item-p) indent)
  17639. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17640. (progn
  17641. (save-match-data (newline))
  17642. (org-indent-line-to (length (match-string 0))))
  17643. (let ((ind (org-get-indentation)))
  17644. (newline)
  17645. (if (org-looking-back org-list-end-re)
  17646. (org-indent-line)
  17647. (org-indent-line-to ind)))))
  17648. ((and org-return-follows-link
  17649. (org-at-timestamp-p t)
  17650. (not (eq org-ts-what 'after)))
  17651. (org-follow-timestamp-link))
  17652. ((and org-return-follows-link
  17653. (let ((tprop (get-text-property (point) 'face)))
  17654. (or (eq tprop 'org-link)
  17655. (and (listp tprop) (memq 'org-link tprop)))))
  17656. (call-interactively 'org-open-at-point))
  17657. ((and (org-at-heading-p)
  17658. (looking-at
  17659. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17660. (org-show-entry)
  17661. (end-of-line 1)
  17662. (newline))
  17663. (t (if indent (newline-and-indent) (newline))))))
  17664. (defun org-return-indent ()
  17665. "Goto next table row or insert a newline and indent.
  17666. Calls `org-table-next-row' or `newline-and-indent', depending on
  17667. context. See the individual commands for more information."
  17668. (interactive)
  17669. (org-return t))
  17670. (defun org-ctrl-c-star ()
  17671. "Compute table, or change heading status of lines.
  17672. Calls `org-table-recalculate' or `org-toggle-heading',
  17673. depending on context."
  17674. (interactive)
  17675. (cond
  17676. ((org-at-table-p)
  17677. (call-interactively 'org-table-recalculate))
  17678. (t
  17679. ;; Convert all lines in region to list items
  17680. (call-interactively 'org-toggle-heading))))
  17681. (defun org-ctrl-c-minus ()
  17682. "Insert separator line in table or modify bullet status of line.
  17683. Also turns a plain line or a region of lines into list items.
  17684. Calls `org-table-insert-hline', `org-toggle-item', or
  17685. `org-cycle-list-bullet', depending on context."
  17686. (interactive)
  17687. (cond
  17688. ((org-at-table-p)
  17689. (call-interactively 'org-table-insert-hline))
  17690. ((org-region-active-p)
  17691. (call-interactively 'org-toggle-item))
  17692. ((org-in-item-p)
  17693. (call-interactively 'org-cycle-list-bullet))
  17694. (t
  17695. (call-interactively 'org-toggle-item))))
  17696. (defun org-toggle-item (arg)
  17697. "Convert headings or normal lines to items, items to normal lines.
  17698. If there is no active region, only the current line is considered.
  17699. If the first non blank line in the region is an headline, convert
  17700. all headlines to items, shifting text accordingly.
  17701. If it is an item, convert all items to normal lines.
  17702. If it is normal text, change region into an item. With a prefix
  17703. argument ARG, change each line in region into an item."
  17704. (interactive "P")
  17705. (let ((shift-text
  17706. (function
  17707. ;; Shift text in current section to IND, from point to END.
  17708. ;; The function leaves point to END line.
  17709. (lambda (ind end)
  17710. (let ((min-i 1000) (end (copy-marker end)))
  17711. ;; First determine the minimum indentation (MIN-I) of
  17712. ;; the text.
  17713. (save-excursion
  17714. (catch 'exit
  17715. (while (< (point) end)
  17716. (let ((i (org-get-indentation)))
  17717. (cond
  17718. ;; Skip blank lines and inline tasks.
  17719. ((looking-at "^[ \t]*$"))
  17720. ((looking-at org-outline-regexp-bol))
  17721. ;; We can't find less than 0 indentation.
  17722. ((zerop i) (throw 'exit (setq min-i 0)))
  17723. ((< i min-i) (setq min-i i))))
  17724. (forward-line))))
  17725. ;; Then indent each line so that a line indented to
  17726. ;; MIN-I becomes indented to IND. Ignore blank lines
  17727. ;; and inline tasks in the process.
  17728. (let ((delta (- ind min-i)))
  17729. (while (< (point) end)
  17730. (unless (or (looking-at "^[ \t]*$")
  17731. (looking-at org-outline-regexp-bol))
  17732. (org-indent-line-to (+ (org-get-indentation) delta)))
  17733. (forward-line)))))))
  17734. (skip-blanks
  17735. (function
  17736. ;; Return beginning of first non-blank line, starting from
  17737. ;; line at POS.
  17738. (lambda (pos)
  17739. (save-excursion
  17740. (goto-char pos)
  17741. (skip-chars-forward " \r\t\n")
  17742. (point-at-bol)))))
  17743. beg end)
  17744. ;; Determine boundaries of changes.
  17745. (if (org-region-active-p)
  17746. (setq beg (funcall skip-blanks (region-beginning))
  17747. end (copy-marker (region-end)))
  17748. (setq beg (funcall skip-blanks (point-at-bol))
  17749. end (copy-marker (point-at-eol))))
  17750. ;; Depending on the starting line, choose an action on the text
  17751. ;; between BEG and END.
  17752. (org-with-limited-levels
  17753. (save-excursion
  17754. (goto-char beg)
  17755. (cond
  17756. ;; Case 1. Start at an item: de-itemize. Note that it only
  17757. ;; happens when a region is active: `org-ctrl-c-minus'
  17758. ;; would call `org-cycle-list-bullet' otherwise.
  17759. ((org-at-item-p)
  17760. (while (< (point) end)
  17761. (when (org-at-item-p)
  17762. (skip-chars-forward " \t")
  17763. (delete-region (point) (match-end 0)))
  17764. (forward-line)))
  17765. ;; Case 2. Start at an heading: convert to items.
  17766. ((org-at-heading-p)
  17767. (let* ((bul (org-list-bullet-string "-"))
  17768. (bul-len (length bul))
  17769. ;; Indentation of the first heading. It should be
  17770. ;; relative to the indentation of its parent, if any.
  17771. (start-ind (save-excursion
  17772. (cond
  17773. ((not org-adapt-indentation) 0)
  17774. ((not (outline-previous-heading)) 0)
  17775. (t (length (match-string 0))))))
  17776. ;; Level of first heading. Further headings will be
  17777. ;; compared to it to determine hierarchy in the list.
  17778. (ref-level (org-reduced-level (org-outline-level))))
  17779. (while (< (point) end)
  17780. (let* ((level (org-reduced-level (org-outline-level)))
  17781. (delta (max 0 (- level ref-level))))
  17782. ;; If current headline is less indented than the first
  17783. ;; one, set it as reference, in order to preserve
  17784. ;; subtrees.
  17785. (when (< level ref-level) (setq ref-level level))
  17786. (replace-match bul t t)
  17787. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17788. ;; Ensure all text down to END (or SECTION-END) belongs
  17789. ;; to the newly created item.
  17790. (let ((section-end (save-excursion
  17791. (or (outline-next-heading) (point)))))
  17792. (forward-line)
  17793. (funcall shift-text
  17794. (+ start-ind (* (1+ delta) bul-len))
  17795. (min end section-end)))))))
  17796. ;; Case 3. Normal line with ARG: turn each non-item line into
  17797. ;; an item.
  17798. (arg
  17799. (while (< (point) end)
  17800. (unless (or (org-at-heading-p) (org-at-item-p))
  17801. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17802. (replace-match
  17803. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17804. (forward-line)))
  17805. ;; Case 4. Normal line without ARG: make the first line of
  17806. ;; region an item, and shift indentation of others
  17807. ;; lines to set them as item's body.
  17808. (t (let* ((bul (org-list-bullet-string "-"))
  17809. (bul-len (length bul))
  17810. (ref-ind (org-get-indentation)))
  17811. (skip-chars-forward " \t")
  17812. (insert bul)
  17813. (forward-line)
  17814. (while (< (point) end)
  17815. ;; Ensure that lines less indented than first one
  17816. ;; still get included in item body.
  17817. (funcall shift-text
  17818. (+ ref-ind bul-len)
  17819. (min end (save-excursion (or (outline-next-heading)
  17820. (point)))))
  17821. (forward-line)))))))))
  17822. (defun org-toggle-heading (&optional nstars)
  17823. "Convert headings to normal text, or items or text to headings.
  17824. If there is no active region, only the current line is considered.
  17825. With a \\[universal-argument] prefix, convert the whole list at
  17826. point into heading.
  17827. In a region:
  17828. - If the first non blank line is an headline, remove the stars
  17829. from all headlines in the region.
  17830. - If it is a normal line turn each and every normal line (i.e. not an
  17831. heading or an item) in the region into a heading.
  17832. - If it is a plain list item, turn all plain list items into headings.
  17833. When converting a line into a heading, the number of stars is chosen
  17834. such that the lines become children of the current entry. However,
  17835. when a prefix argument is given, its value determines the number of
  17836. stars to add."
  17837. (interactive "P")
  17838. (let ((skip-blanks
  17839. (function
  17840. ;; Return beginning of first non-blank line, starting from
  17841. ;; line at POS.
  17842. (lambda (pos)
  17843. (save-excursion
  17844. (goto-char pos)
  17845. (while (org-at-comment-p) (forward-line))
  17846. (skip-chars-forward " \r\t\n")
  17847. (point-at-bol)))))
  17848. beg end toggled)
  17849. ;; Determine boundaries of changes. If a universal prefix has
  17850. ;; been given, put the list in a region. If region ends at a bol,
  17851. ;; do not consider the last line to be in the region.
  17852. (when (and current-prefix-arg (org-at-item-p))
  17853. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17854. (org-mark-element))
  17855. (if (org-region-active-p)
  17856. (setq beg (funcall skip-blanks (region-beginning))
  17857. end (copy-marker (save-excursion
  17858. (goto-char (region-end))
  17859. (if (bolp) (point) (point-at-eol)))))
  17860. (setq beg (funcall skip-blanks (point-at-bol))
  17861. end (copy-marker (point-at-eol))))
  17862. ;; Ensure inline tasks don't count as headings.
  17863. (org-with-limited-levels
  17864. (save-excursion
  17865. (goto-char beg)
  17866. (cond
  17867. ;; Case 1. Started at an heading: de-star headings.
  17868. ((org-at-heading-p)
  17869. (while (< (point) end)
  17870. (when (org-at-heading-p t)
  17871. (looking-at org-outline-regexp) (replace-match "")
  17872. (setq toggled t))
  17873. (forward-line)))
  17874. ;; Case 2. Started at an item: change items into headlines.
  17875. ;; One star will be added by `org-list-to-subtree'.
  17876. ((org-at-item-p)
  17877. (let* ((stars (make-string
  17878. (if nstars
  17879. ;; subtract the star that will be added again by
  17880. ;; `org-list-to-subtree'
  17881. (1- (prefix-numeric-value current-prefix-arg))
  17882. (or (org-current-level) 0))
  17883. ?*))
  17884. (add-stars
  17885. (cond (nstars "") ; stars from prefix only
  17886. ((equal stars "") "") ; before first heading
  17887. (org-odd-levels-only "*") ; inside heading, odd
  17888. (t "")))) ; inside heading, oddeven
  17889. (while (< (point) end)
  17890. (when (org-at-item-p)
  17891. ;; Pay attention to cases when region ends before list.
  17892. (let* ((struct (org-list-struct))
  17893. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17894. (save-restriction
  17895. (narrow-to-region (point) list-end)
  17896. (insert
  17897. (org-list-to-subtree
  17898. (org-list-parse-list t)
  17899. '(:istart (concat stars add-stars (funcall get-stars depth))
  17900. :icount (concat stars add-stars (funcall get-stars depth)))))))
  17901. (setq toggled t))
  17902. (forward-line))))
  17903. ;; Case 3. Started at normal text: make every line an heading,
  17904. ;; skipping headlines and items.
  17905. (t (let* ((stars (make-string
  17906. (if nstars
  17907. (prefix-numeric-value current-prefix-arg)
  17908. (or (org-current-level) 0))
  17909. ?*))
  17910. (add-stars
  17911. (cond (nstars "") ; stars from prefix only
  17912. ((equal stars "") "*") ; before first heading
  17913. (org-odd-levels-only "**") ; inside heading, odd
  17914. (t "*"))) ; inside heading, oddeven
  17915. (rpl (concat stars add-stars " ")))
  17916. (while (< (point) end)
  17917. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  17918. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17919. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  17920. (forward-line)))))))
  17921. (unless toggled (message "Cannot toggle heading from here"))))
  17922. (defun org-meta-return (&optional arg)
  17923. "Insert a new heading or wrap a region in a table.
  17924. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17925. See the individual commands for more information."
  17926. (interactive "P")
  17927. (cond
  17928. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17929. ((or (org-at-drawer-p) (org-at-property-p))
  17930. (newline-and-indent))
  17931. ((org-at-table-p)
  17932. (call-interactively 'org-table-wrap-region))
  17933. (t (call-interactively 'org-insert-heading))))
  17934. ;;; Menu entries
  17935. (defsubst org-in-subtree-not-table-p ()
  17936. "Are we in a subtree and not in a table?"
  17937. (and (not (org-before-first-heading-p))
  17938. (not (org-at-table-p))))
  17939. ;; Define the Org-mode menus
  17940. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17941. '("Tbl"
  17942. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17943. ["Next Field" org-cycle (org-at-table-p)]
  17944. ["Previous Field" org-shifttab (org-at-table-p)]
  17945. ["Next Row" org-return (org-at-table-p)]
  17946. "--"
  17947. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17948. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17949. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17950. "--"
  17951. ("Column"
  17952. ["Move Column Left" org-metaleft (org-at-table-p)]
  17953. ["Move Column Right" org-metaright (org-at-table-p)]
  17954. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17955. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17956. ("Row"
  17957. ["Move Row Up" org-metaup (org-at-table-p)]
  17958. ["Move Row Down" org-metadown (org-at-table-p)]
  17959. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17960. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17961. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17962. "--"
  17963. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17964. ("Rectangle"
  17965. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17966. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17967. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17968. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17969. "--"
  17970. ("Calculate"
  17971. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17972. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17973. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17974. "--"
  17975. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17976. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17977. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17978. "--"
  17979. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17980. "--"
  17981. ["Sum Column/Rectangle" org-table-sum
  17982. (or (org-at-table-p) (org-region-active-p))]
  17983. ["Which Column?" org-table-current-column (org-at-table-p)])
  17984. ["Debug Formulas"
  17985. org-table-toggle-formula-debugger
  17986. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17987. ["Show Col/Row Numbers"
  17988. org-table-toggle-coordinate-overlays
  17989. :style toggle
  17990. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17991. "--"
  17992. ["Create" org-table-create (and (not (org-at-table-p))
  17993. org-enable-table-editor)]
  17994. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17995. ["Import from File" org-table-import (not (org-at-table-p))]
  17996. ["Export to File" org-table-export (org-at-table-p)]
  17997. "--"
  17998. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17999. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18000. '("Org"
  18001. ("Show/Hide"
  18002. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18003. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18004. ["Sparse Tree..." org-sparse-tree t]
  18005. ["Reveal Context" org-reveal t]
  18006. ["Show All" show-all t]
  18007. "--"
  18008. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18009. "--"
  18010. ["New Heading" org-insert-heading t]
  18011. ("Navigate Headings"
  18012. ["Up" outline-up-heading t]
  18013. ["Next" outline-next-visible-heading t]
  18014. ["Previous" outline-previous-visible-heading t]
  18015. ["Next Same Level" outline-forward-same-level t]
  18016. ["Previous Same Level" outline-backward-same-level t]
  18017. "--"
  18018. ["Jump" org-goto t])
  18019. ("Edit Structure"
  18020. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18021. "--"
  18022. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18023. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18024. "--"
  18025. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18026. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18027. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18028. "--"
  18029. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18030. "--"
  18031. ["Copy visible text" org-copy-visible t]
  18032. "--"
  18033. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18034. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18035. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18036. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18037. "--"
  18038. ["Sort Region/Children" org-sort t]
  18039. "--"
  18040. ["Convert to odd levels" org-convert-to-odd-levels t]
  18041. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18042. ("Editing"
  18043. ["Emphasis..." org-emphasize t]
  18044. ["Edit Source Example" org-edit-special t]
  18045. "--"
  18046. ["Footnote new/jump" org-footnote-action t]
  18047. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18048. ("Archive"
  18049. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18050. "--"
  18051. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18052. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18053. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18054. )
  18055. "--"
  18056. ("Hyperlinks"
  18057. ["Store Link (Global)" org-store-link t]
  18058. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18059. ["Insert Link" org-insert-link t]
  18060. ["Follow Link" org-open-at-point t]
  18061. "--"
  18062. ["Next link" org-next-link t]
  18063. ["Previous link" org-previous-link t]
  18064. "--"
  18065. ["Descriptive Links"
  18066. org-toggle-link-display
  18067. :style radio
  18068. :selected org-descriptive-links
  18069. ]
  18070. ["Literal Links"
  18071. org-toggle-link-display
  18072. :style radio
  18073. :selected (not org-descriptive-links)])
  18074. "--"
  18075. ("TODO Lists"
  18076. ["TODO/DONE/-" org-todo t]
  18077. ("Select keyword"
  18078. ["Next keyword" org-shiftright (org-at-heading-p)]
  18079. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18080. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18081. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18082. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18083. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18084. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18085. "--"
  18086. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18087. :selected org-enforce-todo-dependencies :style toggle :active t]
  18088. "Settings for tree at point"
  18089. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18090. :selected (org-entry-get nil "ORDERED")
  18091. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18092. ["Do Children parallel" org-toggle-ordered-property :style radio
  18093. :selected (not (org-entry-get nil "ORDERED"))
  18094. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18095. "--"
  18096. ["Set Priority" org-priority t]
  18097. ["Priority Up" org-shiftup t]
  18098. ["Priority Down" org-shiftdown t]
  18099. "--"
  18100. ["Get news from all feeds" org-feed-update-all t]
  18101. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18102. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18103. ("TAGS and Properties"
  18104. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18105. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18106. "--"
  18107. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18108. ["Column view of properties" org-columns t]
  18109. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18110. ("Dates and Scheduling"
  18111. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18112. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18113. ("Change Date"
  18114. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18115. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18116. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18117. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18118. ["Compute Time Range" org-evaluate-time-range t]
  18119. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18120. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18121. "--"
  18122. ["Custom time format" org-toggle-time-stamp-overlays
  18123. :style radio :selected org-display-custom-times]
  18124. "--"
  18125. ["Goto Calendar" org-goto-calendar t]
  18126. ["Date from Calendar" org-date-from-calendar t]
  18127. "--"
  18128. ["Start/Restart Timer" org-timer-start t]
  18129. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18130. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18131. ["Insert Timer String" org-timer t]
  18132. ["Insert Timer Item" org-timer-item t])
  18133. ("Logging work"
  18134. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18135. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18136. ["Clock out" org-clock-out t]
  18137. ["Clock cancel" org-clock-cancel t]
  18138. "--"
  18139. ["Mark as default task" org-clock-mark-default-task t]
  18140. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18141. ["Goto running clock" org-clock-goto t]
  18142. "--"
  18143. ["Display times" org-clock-display t]
  18144. ["Create clock table" org-clock-report t]
  18145. "--"
  18146. ["Record DONE time"
  18147. (progn (setq org-log-done (not org-log-done))
  18148. (message "Switching to %s will %s record a timestamp"
  18149. (car org-done-keywords)
  18150. (if org-log-done "automatically" "not")))
  18151. :style toggle :selected org-log-done])
  18152. "--"
  18153. ["Agenda Command..." org-agenda t]
  18154. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18155. ("File List for Agenda")
  18156. ("Special views current file"
  18157. ["TODO Tree" org-show-todo-tree t]
  18158. ["Check Deadlines" org-check-deadlines t]
  18159. ["Timeline" org-timeline t]
  18160. ["Tags/Property tree" org-match-sparse-tree t])
  18161. "--"
  18162. ["Export/Publish..." org-export t]
  18163. ("LaTeX"
  18164. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18165. :selected org-cdlatex-mode]
  18166. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18167. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18168. ["Modify math symbol" org-cdlatex-math-modify
  18169. (org-inside-LaTeX-fragment-p)]
  18170. ["Insert citation" org-reftex-citation t]
  18171. "--"
  18172. ["Template for BEAMER" (progn (require 'org-beamer)
  18173. (org-insert-beamer-options-template)) t])
  18174. "--"
  18175. ("MobileOrg"
  18176. ["Push Files and Views" org-mobile-push t]
  18177. ["Get Captured and Flagged" org-mobile-pull t]
  18178. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18179. "--"
  18180. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18181. "--"
  18182. ("Documentation"
  18183. ["Show Version" org-version t]
  18184. ["Info Documentation" org-info t])
  18185. ("Customize"
  18186. ["Browse Org Group" org-customize t]
  18187. "--"
  18188. ["Expand This Menu" org-create-customize-menu
  18189. (fboundp 'customize-menu-create)])
  18190. ["Send bug report" org-submit-bug-report t]
  18191. "--"
  18192. ("Refresh/Reload"
  18193. ["Refresh setup current buffer" org-mode-restart t]
  18194. ["Reload Org (after update)" org-reload t]
  18195. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18196. ))
  18197. (defun org-info (&optional node)
  18198. "Read documentation for Org-mode in the info system.
  18199. With optional NODE, go directly to that node."
  18200. (interactive)
  18201. (info (format "(org)%s" (or node ""))))
  18202. ;;;###autoload
  18203. (defun org-submit-bug-report ()
  18204. "Submit a bug report on Org-mode via mail.
  18205. Don't hesitate to report any problems or inaccurate documentation.
  18206. If you don't have setup sending mail from (X)Emacs, please copy the
  18207. output buffer into your mail program, as it gives us important
  18208. information about your Org-mode version and configuration."
  18209. (interactive)
  18210. (require 'reporter)
  18211. (org-load-modules-maybe)
  18212. (org-require-autoloaded-modules)
  18213. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18214. (reporter-submit-bug-report
  18215. "emacs-orgmode@gnu.org"
  18216. (org-version nil 'full)
  18217. (let (list)
  18218. (save-window-excursion
  18219. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18220. (delete-other-windows)
  18221. (erase-buffer)
  18222. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18223. We would like to add your full Org-mode and Outline configuration to the
  18224. bug report. This greatly simplifies the work of the maintainer and
  18225. other experts on the mailing list.
  18226. HOWEVER, some variables you have customized may contain private
  18227. information. The names of customers, colleagues, or friends, might
  18228. appear in the form of file names, tags, todo states, or search strings.
  18229. If you answer yes to the prompt, you might want to check and remove
  18230. such private information before sending the email.")
  18231. (add-text-properties (point-min) (point-max) '(face org-warning))
  18232. (when (yes-or-no-p "Include your Org-mode configuration ")
  18233. (mapatoms
  18234. (lambda (v)
  18235. (and (boundp v)
  18236. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18237. (or (and (symbol-value v)
  18238. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18239. (and
  18240. (get v 'custom-type) (get v 'standard-value)
  18241. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18242. (push v list)))))
  18243. (kill-buffer (get-buffer "*Warn about privacy*"))
  18244. list))
  18245. nil nil
  18246. "Remember to cover the basics, that is, what you expected to happen and
  18247. what in fact did happen. You don't know how to make a good report? See
  18248. http://orgmode.org/manual/Feedback.html#Feedback
  18249. Your bug report will be posted to the Org-mode mailing list.
  18250. ------------------------------------------------------------------------")
  18251. (save-excursion
  18252. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18253. (replace-match "\\1Bug: \\3 [\\2]")))))
  18254. (defun org-install-agenda-files-menu ()
  18255. (let ((bl (buffer-list)))
  18256. (save-excursion
  18257. (while bl
  18258. (set-buffer (pop bl))
  18259. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18260. (when (derived-mode-p 'org-mode)
  18261. (easy-menu-change
  18262. '("Org") "File List for Agenda"
  18263. (append
  18264. (list
  18265. ["Edit File List" (org-edit-agenda-file-list) t]
  18266. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18267. ["Remove Current File from List" org-remove-file t]
  18268. ["Cycle through agenda files" org-cycle-agenda-files t]
  18269. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18270. "--")
  18271. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18272. ;;;; Documentation
  18273. (defun org-require-autoloaded-modules ()
  18274. (interactive)
  18275. (mapc 'require
  18276. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18277. org-docbook org-exp org-html org-icalendar
  18278. org-id org-latex
  18279. org-publish org-remember org-table
  18280. org-timer org-xoxo)))
  18281. ;;;###autoload
  18282. (defun org-reload (&optional uncompiled)
  18283. "Reload all org lisp files.
  18284. With prefix arg UNCOMPILED, load the uncompiled versions."
  18285. (interactive "P")
  18286. (require 'find-func)
  18287. (let* ((file-re "^org\\(-.*\\)?\\.el")
  18288. (dir-org (file-name-directory (org-find-library-dir "org")))
  18289. (dir-org-contrib (ignore-errors
  18290. (file-name-directory
  18291. (org-find-library-dir "org-contribdir"))))
  18292. (babel-files
  18293. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  18294. (append (list nil "comint" "eval" "exp" "keys"
  18295. "lob" "ref" "table" "tangle")
  18296. (delq nil
  18297. (mapcar
  18298. (lambda (lang)
  18299. (when (cdr lang) (symbol-name (car lang))))
  18300. org-babel-load-languages)))))
  18301. (files
  18302. (append babel-files
  18303. (and dir-org-contrib
  18304. (directory-files dir-org-contrib t file-re))
  18305. (directory-files dir-org t file-re)))
  18306. (remove-re (concat (if (featurep 'xemacs)
  18307. "org-colview" "org-colview-xemacs")
  18308. "\\'")))
  18309. (setq files (mapcar 'file-name-sans-extension files))
  18310. (setq files (mapcar
  18311. (lambda (x) (if (string-match remove-re x) nil x))
  18312. files))
  18313. (setq files (delq nil files))
  18314. (mapc
  18315. (lambda (f)
  18316. (when (featurep (intern (file-name-nondirectory f)))
  18317. (if (and (not uncompiled)
  18318. (file-exists-p (concat f ".elc")))
  18319. (load (concat f ".elc") nil nil 'nosuffix)
  18320. (load (concat f ".el") nil nil 'nosuffix))))
  18321. files)
  18322. (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
  18323. (org-version nil 'full 'message))
  18324. ;;;###autoload
  18325. (defun org-customize ()
  18326. "Call the customize function with org as argument."
  18327. (interactive)
  18328. (org-load-modules-maybe)
  18329. (org-require-autoloaded-modules)
  18330. (customize-browse 'org))
  18331. (defun org-create-customize-menu ()
  18332. "Create a full customization menu for Org-mode, insert it into the menu."
  18333. (interactive)
  18334. (org-load-modules-maybe)
  18335. (org-require-autoloaded-modules)
  18336. (if (fboundp 'customize-menu-create)
  18337. (progn
  18338. (easy-menu-change
  18339. '("Org") "Customize"
  18340. `(["Browse Org group" org-customize t]
  18341. "--"
  18342. ,(customize-menu-create 'org)
  18343. ["Set" Custom-set t]
  18344. ["Save" Custom-save t]
  18345. ["Reset to Current" Custom-reset-current t]
  18346. ["Reset to Saved" Custom-reset-saved t]
  18347. ["Reset to Standard Settings" Custom-reset-standard t]))
  18348. (message "\"Org\"-menu now contains full customization menu"))
  18349. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18350. ;;;; Miscellaneous stuff
  18351. ;;; Generally useful functions
  18352. (defun org-get-at-bol (property)
  18353. "Get text property PROPERTY at beginning of line."
  18354. (get-text-property (point-at-bol) property))
  18355. (defun org-find-text-property-in-string (prop s)
  18356. "Return the first non-nil value of property PROP in string S."
  18357. (or (get-text-property 0 prop s)
  18358. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18359. prop s)))
  18360. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18361. "Display the given MESSAGE as a warning."
  18362. (if (fboundp 'display-warning)
  18363. (display-warning 'org message
  18364. (if (featurep 'xemacs) 'warning :warning))
  18365. (let ((buf (get-buffer-create "*Org warnings*")))
  18366. (with-current-buffer buf
  18367. (goto-char (point-max))
  18368. (insert "Warning (Org): " message)
  18369. (unless (bolp)
  18370. (newline)))
  18371. (display-buffer buf)
  18372. (sit-for 0))))
  18373. (defun org-eval (form)
  18374. "Eval FORM and return result."
  18375. (condition-case error
  18376. (eval form)
  18377. (error (format "%%![Error: %s]" error))))
  18378. (defun org-in-clocktable-p ()
  18379. "Check if the cursor is in a clocktable."
  18380. (let ((pos (point)) start)
  18381. (save-excursion
  18382. (end-of-line 1)
  18383. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18384. (setq start (match-beginning 0))
  18385. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18386. (>= (match-end 0) pos)
  18387. start))))
  18388. (defun org-in-commented-line ()
  18389. "Is point in a line starting with `#'?"
  18390. (equal (char-after (point-at-bol)) ?#))
  18391. (defun org-in-indented-comment-line ()
  18392. "Is point in a line starting with `#' after some white space?"
  18393. (save-excursion
  18394. (save-match-data
  18395. (goto-char (point-at-bol))
  18396. (looking-at "[ \t]*#"))))
  18397. (defun org-in-verbatim-emphasis ()
  18398. (save-match-data
  18399. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18400. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18401. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18402. (if (and marker (marker-buffer marker)
  18403. (buffer-live-p (marker-buffer marker)))
  18404. (progn
  18405. (org-pop-to-buffer-same-window (marker-buffer marker))
  18406. (if (or (> marker (point-max)) (< marker (point-min)))
  18407. (widen))
  18408. (goto-char marker)
  18409. (org-show-context 'org-goto))
  18410. (if bookmark
  18411. (bookmark-jump bookmark)
  18412. (error "Cannot find location"))))
  18413. (defun org-quote-csv-field (s)
  18414. "Quote field for inclusion in CSV material."
  18415. (if (string-match "[\",]" s)
  18416. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18417. s))
  18418. (defun org-force-self-insert (N)
  18419. "Needed to enforce self-insert under remapping."
  18420. (interactive "p")
  18421. (self-insert-command N))
  18422. (defun org-string-width (s)
  18423. "Compute width of string, ignoring invisible characters.
  18424. This ignores character with invisibility property `org-link', and also
  18425. characters with property `org-cwidth', because these will become invisible
  18426. upon the next fontification round."
  18427. (let (b l)
  18428. (when (or (eq t buffer-invisibility-spec)
  18429. (assq 'org-link buffer-invisibility-spec))
  18430. (while (setq b (text-property-any 0 (length s)
  18431. 'invisible 'org-link s))
  18432. (setq s (concat (substring s 0 b)
  18433. (substring s (or (next-single-property-change
  18434. b 'invisible s) (length s)))))))
  18435. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18436. (setq s (concat (substring s 0 b)
  18437. (substring s (or (next-single-property-change
  18438. b 'org-cwidth s) (length s))))))
  18439. (setq l (string-width s) b -1)
  18440. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18441. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18442. l))
  18443. (defun org-shorten-string (s maxlength)
  18444. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18445. If the string is shorter or has length MAXLENGTH, just return the
  18446. original string. If it is longer, the functions finds a space in the
  18447. string, breaks this string off at that locations and adds three dots
  18448. as ellipsis. Including the ellipsis, the string will not be longer
  18449. than MAXLENGTH. If finding a good breaking point in the string does
  18450. not work, the string is just chopped off in the middle of a word
  18451. if necessary."
  18452. (if (<= (length s) maxlength)
  18453. s
  18454. (let* ((n (max (- maxlength 4) 1))
  18455. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18456. (if (string-match re s)
  18457. (concat (match-string 1 s) "...")
  18458. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18459. (defun org-get-indentation (&optional line)
  18460. "Get the indentation of the current line, interpreting tabs.
  18461. When LINE is given, assume it represents a line and compute its indentation."
  18462. (if line
  18463. (if (string-match "^ *" (org-remove-tabs line))
  18464. (match-end 0))
  18465. (save-excursion
  18466. (beginning-of-line 1)
  18467. (skip-chars-forward " \t")
  18468. (current-column))))
  18469. (defun org-get-string-indentation (s)
  18470. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18471. (let ((n -1) (i 0) (w tab-width) c)
  18472. (catch 'exit
  18473. (while (< (setq n (1+ n)) (length s))
  18474. (setq c (aref s n))
  18475. (cond ((= c ?\ ) (setq i (1+ i)))
  18476. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18477. (t (throw 'exit t)))))
  18478. i))
  18479. (defun org-remove-tabs (s &optional width)
  18480. "Replace tabulators in S with spaces.
  18481. Assumes that s is a single line, starting in column 0."
  18482. (setq width (or width tab-width))
  18483. (while (string-match "\t" s)
  18484. (setq s (replace-match
  18485. (make-string
  18486. (- (* width (/ (+ (match-beginning 0) width) width))
  18487. (match-beginning 0)) ?\ )
  18488. t t s)))
  18489. s)
  18490. (defun org-fix-indentation (line ind)
  18491. "Fix indentation in LINE.
  18492. IND is a cons cell with target and minimum indentation.
  18493. If the current indentation in LINE is smaller than the minimum,
  18494. leave it alone. If it is larger than ind, set it to the target."
  18495. (let* ((l (org-remove-tabs line))
  18496. (i (org-get-indentation l))
  18497. (i1 (car ind)) (i2 (cdr ind)))
  18498. (if (>= i i2) (setq l (substring line i2)))
  18499. (if (> i1 0)
  18500. (concat (make-string i1 ?\ ) l)
  18501. l)))
  18502. (defun org-remove-indentation (code &optional n)
  18503. "Remove the maximum common indentation from the lines in CODE.
  18504. N may optionally be the number of spaces to remove."
  18505. (with-temp-buffer
  18506. (insert code)
  18507. (org-do-remove-indentation n)
  18508. (buffer-string)))
  18509. (defun org-do-remove-indentation (&optional n)
  18510. "Remove the maximum common indentation from the buffer."
  18511. (untabify (point-min) (point-max))
  18512. (let ((min 10000) re)
  18513. (if n
  18514. (setq min n)
  18515. (goto-char (point-min))
  18516. (while (re-search-forward "^ *[^ \n]" nil t)
  18517. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18518. (unless (or (= min 0) (= min 10000))
  18519. (setq re (format "^ \\{%d\\}" min))
  18520. (goto-char (point-min))
  18521. (while (re-search-forward re nil t)
  18522. (replace-match "")
  18523. (end-of-line 1))
  18524. min)))
  18525. (defun org-fill-template (template alist)
  18526. "Find each %key of ALIST in TEMPLATE and replace it."
  18527. (let ((case-fold-search nil)
  18528. entry key value)
  18529. (setq alist (sort (copy-sequence alist)
  18530. (lambda (a b) (< (length (car a)) (length (car b))))))
  18531. (while (setq entry (pop alist))
  18532. (setq template
  18533. (replace-regexp-in-string
  18534. (concat "%" (regexp-quote (car entry)))
  18535. (or (cdr entry) "") template t t)))
  18536. template))
  18537. (defun org-base-buffer (buffer)
  18538. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18539. (if (not buffer)
  18540. buffer
  18541. (or (buffer-base-buffer buffer)
  18542. buffer)))
  18543. (defun org-trim (s)
  18544. "Remove whitespace at beginning and end of string."
  18545. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18546. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18547. s)
  18548. (defun org-wrap (string &optional width lines)
  18549. "Wrap string to either a number of lines, or a width in characters.
  18550. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18551. that costs. If there is a word longer than WIDTH, the text is actually
  18552. wrapped to the length of that word.
  18553. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18554. many lines, whatever width that takes.
  18555. The return value is a list of lines, without newlines at the end."
  18556. (let* ((words (org-split-string string "[ \t\n]+"))
  18557. (maxword (apply 'max (mapcar 'org-string-width words)))
  18558. w ll)
  18559. (cond (width
  18560. (org-do-wrap words (max maxword width)))
  18561. (lines
  18562. (setq w maxword)
  18563. (setq ll (org-do-wrap words maxword))
  18564. (if (<= (length ll) lines)
  18565. ll
  18566. (setq ll words)
  18567. (while (> (length ll) lines)
  18568. (setq w (1+ w))
  18569. (setq ll (org-do-wrap words w)))
  18570. ll))
  18571. (t (error "Cannot wrap this")))))
  18572. (defun org-do-wrap (words width)
  18573. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18574. (let (lines line)
  18575. (while words
  18576. (setq line (pop words))
  18577. (while (and words (< (+ (length line) (length (car words))) width))
  18578. (setq line (concat line " " (pop words))))
  18579. (setq lines (push line lines)))
  18580. (nreverse lines)))
  18581. (defun org-split-string (string &optional separators)
  18582. "Splits STRING into substrings at SEPARATORS.
  18583. No empty strings are returned if there are matches at the beginning
  18584. and end of string."
  18585. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18586. (start 0)
  18587. notfirst
  18588. (list nil))
  18589. (while (and (string-match rexp string
  18590. (if (and notfirst
  18591. (= start (match-beginning 0))
  18592. (< start (length string)))
  18593. (1+ start) start))
  18594. (< (match-beginning 0) (length string)))
  18595. (setq notfirst t)
  18596. (or (eq (match-beginning 0) 0)
  18597. (and (eq (match-beginning 0) (match-end 0))
  18598. (eq (match-beginning 0) start))
  18599. (setq list
  18600. (cons (substring string start (match-beginning 0))
  18601. list)))
  18602. (setq start (match-end 0)))
  18603. (or (eq start (length string))
  18604. (setq list
  18605. (cons (substring string start)
  18606. list)))
  18607. (nreverse list)))
  18608. (defun org-quote-vert (s)
  18609. "Replace \"|\" with \"\\vert\"."
  18610. (while (string-match "|" s)
  18611. (setq s (replace-match "\\vert" t t s)))
  18612. s)
  18613. (defun org-uuidgen-p (s)
  18614. "Is S an ID created by UUIDGEN?"
  18615. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18616. (defun org-in-src-block-p (&optional inside)
  18617. "Whether point is in a code source block.
  18618. When INSIDE is non-nil, don't consider we are within a src block
  18619. when point is at #+BEGIN_SRC or #+END_SRC."
  18620. (let ((case-fold-search t) ov)
  18621. (or (and (setq ov (overlays-at (point)))
  18622. (memq 'org-block-background
  18623. (overlay-properties (car ov))))
  18624. (and (not inside)
  18625. (save-match-data
  18626. (save-excursion
  18627. (beginning-of-line)
  18628. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  18629. (defun org-context ()
  18630. "Return a list of contexts of the current cursor position.
  18631. If several contexts apply, all are returned.
  18632. Each context entry is a list with a symbol naming the context, and
  18633. two positions indicating start and end of the context. Possible
  18634. contexts are:
  18635. :headline anywhere in a headline
  18636. :headline-stars on the leading stars in a headline
  18637. :todo-keyword on a TODO keyword (including DONE) in a headline
  18638. :tags on the TAGS in a headline
  18639. :priority on the priority cookie in a headline
  18640. :item on the first line of a plain list item
  18641. :item-bullet on the bullet/number of a plain list item
  18642. :checkbox on the checkbox in a plain list item
  18643. :table in an org-mode table
  18644. :table-special on a special filed in a table
  18645. :table-table in a table.el table
  18646. :clocktable in a clocktable
  18647. :src-block in a source block
  18648. :link on a hyperlink
  18649. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18650. :target on a <<target>>
  18651. :radio-target on a <<<radio-target>>>
  18652. :latex-fragment on a LaTeX fragment
  18653. :latex-preview on a LaTeX fragment with overlaid preview image
  18654. This function expects the position to be visible because it uses font-lock
  18655. faces as a help to recognize the following contexts: :table-special, :link,
  18656. and :keyword."
  18657. (let* ((f (get-text-property (point) 'face))
  18658. (faces (if (listp f) f (list f)))
  18659. (case-fold-search t)
  18660. (p (point)) clist o)
  18661. ;; First the large context
  18662. (cond
  18663. ((org-at-heading-p t)
  18664. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18665. (when (progn
  18666. (beginning-of-line 1)
  18667. (looking-at org-todo-line-tags-regexp))
  18668. (push (org-point-in-group p 1 :headline-stars) clist)
  18669. (push (org-point-in-group p 2 :todo-keyword) clist)
  18670. (push (org-point-in-group p 4 :tags) clist))
  18671. (goto-char p)
  18672. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18673. (if (looking-at "\\[#[A-Z0-9]\\]")
  18674. (push (org-point-in-group p 0 :priority) clist)))
  18675. ((org-at-item-p)
  18676. (push (org-point-in-group p 2 :item-bullet) clist)
  18677. (push (list :item (point-at-bol)
  18678. (save-excursion (org-end-of-item) (point)))
  18679. clist)
  18680. (and (org-at-item-checkbox-p)
  18681. (push (org-point-in-group p 0 :checkbox) clist)))
  18682. ((org-at-table-p)
  18683. (push (list :table (org-table-begin) (org-table-end)) clist)
  18684. (if (memq 'org-formula faces)
  18685. (push (list :table-special
  18686. (previous-single-property-change p 'face)
  18687. (next-single-property-change p 'face)) clist)))
  18688. ((org-at-table-p 'any)
  18689. (push (list :table-table) clist)))
  18690. (goto-char p)
  18691. (let ((case-fold-search t))
  18692. ;; New the "medium" contexts: clocktables, source blocks
  18693. (cond ((org-in-clocktable-p)
  18694. (push (list :clocktable
  18695. (and (or (looking-at "#\\+BEGIN: clocktable")
  18696. (search-backward "#+BEGIN: clocktable" nil t))
  18697. (match-beginning 0))
  18698. (and (re-search-forward "#\\+END:?" nil t)
  18699. (match-end 0))) clist))
  18700. ((org-in-src-block-p)
  18701. (push (list :src-block
  18702. (and (or (looking-at "#\\+BEGIN_SRC")
  18703. (search-backward "#+BEGIN_SRC" nil t))
  18704. (match-beginning 0))
  18705. (and (search-forward "#+END_SRC" nil t)
  18706. (match-beginning 0))) clist))))
  18707. (goto-char p)
  18708. ;; Now the small context
  18709. (cond
  18710. ((org-at-timestamp-p)
  18711. (push (org-point-in-group p 0 :timestamp) clist))
  18712. ((memq 'org-link faces)
  18713. (push (list :link
  18714. (previous-single-property-change p 'face)
  18715. (next-single-property-change p 'face)) clist))
  18716. ((memq 'org-special-keyword faces)
  18717. (push (list :keyword
  18718. (previous-single-property-change p 'face)
  18719. (next-single-property-change p 'face)) clist))
  18720. ((org-at-target-p)
  18721. (push (org-point-in-group p 0 :target) clist)
  18722. (goto-char (1- (match-beginning 0)))
  18723. (if (looking-at org-radio-target-regexp)
  18724. (push (org-point-in-group p 0 :radio-target) clist))
  18725. (goto-char p))
  18726. ((setq o (car (delq nil
  18727. (mapcar
  18728. (lambda (x)
  18729. (if (memq x org-latex-fragment-image-overlays) x))
  18730. (overlays-at (point))))))
  18731. (push (list :latex-fragment
  18732. (overlay-start o) (overlay-end o)) clist)
  18733. (push (list :latex-preview
  18734. (overlay-start o) (overlay-end o)) clist))
  18735. ((org-inside-LaTeX-fragment-p)
  18736. ;; FIXME: positions wrong.
  18737. (push (list :latex-fragment (point) (point)) clist)))
  18738. (setq clist (nreverse (delq nil clist)))
  18739. clist))
  18740. ;; FIXME: Compare with at-regexp-p Do we need both?
  18741. (defun org-in-regexp (re &optional nlines visually)
  18742. "Check if point is inside a match of regexp.
  18743. Normally only the current line is checked, but you can include NLINES extra
  18744. lines both before and after point into the search.
  18745. If VISUALLY is set, require that the cursor is not after the match but
  18746. really on, so that the block visually is on the match."
  18747. (catch 'exit
  18748. (let ((pos (point))
  18749. (eol (point-at-eol (+ 1 (or nlines 0))))
  18750. (inc (if visually 1 0)))
  18751. (save-excursion
  18752. (beginning-of-line (- 1 (or nlines 0)))
  18753. (while (re-search-forward re eol t)
  18754. (if (and (<= (match-beginning 0) pos)
  18755. (>= (+ inc (match-end 0)) pos))
  18756. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18757. (defun org-at-regexp-p (regexp)
  18758. "Is point inside a match of REGEXP in the current line?"
  18759. (catch 'exit
  18760. (save-excursion
  18761. (let ((pos (point)) (end (point-at-eol)))
  18762. (beginning-of-line 1)
  18763. (while (re-search-forward regexp end t)
  18764. (if (and (<= (match-beginning 0) pos)
  18765. (>= (match-end 0) pos))
  18766. (throw 'exit t)))
  18767. nil))))
  18768. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18769. "Non-nil when point is between matches of START-RE and END-RE.
  18770. Also return a non-nil value when point is on one of the matches.
  18771. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18772. buffer positions. Default values are the positions of headlines
  18773. surrounding the point.
  18774. The functions returns a cons cell whose car (resp. cdr) is the
  18775. position before START-RE (resp. after END-RE)."
  18776. (save-match-data
  18777. (let ((pos (point))
  18778. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18779. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18780. beg end)
  18781. (save-excursion
  18782. ;; Point is on a block when on START-RE or if START-RE can be
  18783. ;; found before it...
  18784. (and (or (org-at-regexp-p start-re)
  18785. (re-search-backward start-re limit-up t))
  18786. (setq beg (match-beginning 0))
  18787. ;; ... and END-RE after it...
  18788. (goto-char (match-end 0))
  18789. (re-search-forward end-re limit-down t)
  18790. (> (setq end (match-end 0)) pos)
  18791. ;; ... without another START-RE in-between.
  18792. (goto-char (match-beginning 0))
  18793. (not (re-search-backward start-re (1+ beg) t))
  18794. ;; Return value.
  18795. (cons beg end))))))
  18796. (defun org-in-block-p (names)
  18797. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18798. NAMES is a list of strings containing names of blocks.
  18799. Return first block name matched, or nil. Beware that in case of
  18800. nested blocks, the returned name may not belong to the closest
  18801. block from point."
  18802. (save-match-data
  18803. (catch 'exit
  18804. (let ((case-fold-search t)
  18805. (lim-up (save-excursion (outline-previous-heading)))
  18806. (lim-down (save-excursion (outline-next-heading))))
  18807. (mapc (lambda (name)
  18808. (let ((n (regexp-quote name)))
  18809. (when (org-between-regexps-p
  18810. (concat "^[ \t]*#\\+begin_" n)
  18811. (concat "^[ \t]*#\\+end_" n)
  18812. lim-up lim-down)
  18813. (throw 'exit n))))
  18814. names))
  18815. nil)))
  18816. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18817. "Call `multi-occur' with buffers for all agenda files."
  18818. (interactive "sOrg-files matching: \np")
  18819. (let* ((files (org-agenda-files))
  18820. (tnames (mapcar 'file-truename files))
  18821. (extra org-agenda-text-search-extra-files)
  18822. f)
  18823. (when (eq (car extra) 'agenda-archives)
  18824. (setq extra (cdr extra))
  18825. (setq files (org-add-archive-files files)))
  18826. (while (setq f (pop extra))
  18827. (unless (member (file-truename f) tnames)
  18828. (add-to-list 'files f 'append)
  18829. (add-to-list 'tnames (file-truename f) 'append)))
  18830. (multi-occur
  18831. (mapcar (lambda (x)
  18832. (with-current-buffer
  18833. (or (get-file-buffer x) (find-file-noselect x))
  18834. (widen)
  18835. (current-buffer)))
  18836. files)
  18837. regexp)))
  18838. (if (boundp 'occur-mode-find-occurrence-hook)
  18839. ;; Emacs 23
  18840. (add-hook 'occur-mode-find-occurrence-hook
  18841. (lambda ()
  18842. (when (derived-mode-p 'org-mode)
  18843. (org-reveal))))
  18844. ;; Emacs 22
  18845. (defadvice occur-mode-goto-occurrence
  18846. (after org-occur-reveal activate)
  18847. (and (derived-mode-p 'org-mode) (org-reveal)))
  18848. (defadvice occur-mode-goto-occurrence-other-window
  18849. (after org-occur-reveal activate)
  18850. (and (derived-mode-p 'org-mode) (org-reveal)))
  18851. (defadvice occur-mode-display-occurrence
  18852. (after org-occur-reveal activate)
  18853. (when (derived-mode-p 'org-mode)
  18854. (let ((pos (occur-mode-find-occurrence)))
  18855. (with-current-buffer (marker-buffer pos)
  18856. (save-excursion
  18857. (goto-char pos)
  18858. (org-reveal)))))))
  18859. (defun org-occur-link-in-agenda-files ()
  18860. "Create a link and search for it in the agendas.
  18861. The link is not stored in `org-stored-links', it is just created
  18862. for the search purpose."
  18863. (interactive)
  18864. (let ((link (condition-case nil
  18865. (org-store-link nil)
  18866. (error "Unable to create a link to here"))))
  18867. (org-occur-in-agenda-files (regexp-quote link))))
  18868. (defun org-uniquify (list)
  18869. "Remove duplicate elements from LIST."
  18870. (let (res)
  18871. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18872. res))
  18873. (defun org-delete-all (elts list)
  18874. "Remove all elements in ELTS from LIST."
  18875. (while elts
  18876. (setq list (delete (pop elts) list)))
  18877. list)
  18878. (defun org-count (cl-item cl-seq)
  18879. "Count the number of occurrences of ITEM in SEQ.
  18880. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18881. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18882. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18883. (while (< cl-start cl-end)
  18884. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18885. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18886. (setq cl-start (1+ cl-start)))
  18887. cl-count))
  18888. (defun org-remove-if (predicate seq)
  18889. "Remove everything from SEQ that fulfills PREDICATE."
  18890. (let (res e)
  18891. (while seq
  18892. (setq e (pop seq))
  18893. (if (not (funcall predicate e)) (push e res)))
  18894. (nreverse res)))
  18895. (defun org-remove-if-not (predicate seq)
  18896. "Remove everything from SEQ that does not fulfill PREDICATE."
  18897. (let (res e)
  18898. (while seq
  18899. (setq e (pop seq))
  18900. (if (funcall predicate e) (push e res)))
  18901. (nreverse res)))
  18902. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18903. "Reduce two-argument FUNCTION across SEQ.
  18904. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18905. \":initial-value\" removed."
  18906. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18907. (cadr (memq :initial-value cl-keys)))
  18908. (cl-seq (pop cl-seq))
  18909. (t (funcall cl-func)))))
  18910. (while cl-seq
  18911. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18912. cl-accum))
  18913. (defun org-back-over-empty-lines ()
  18914. "Move backwards over whitespace, to the beginning of the first empty line.
  18915. Returns the number of empty lines passed."
  18916. (let ((pos (point)))
  18917. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18918. (skip-chars-backward " \t\n\r")
  18919. (unless (eobp)
  18920. (forward-line -1)))
  18921. (beginning-of-line 2)
  18922. (goto-char (min (point) pos))
  18923. (count-lines (point) pos)))
  18924. (defun org-skip-whitespace ()
  18925. (skip-chars-forward " \t\n\r"))
  18926. (defun org-point-in-group (point group &optional context)
  18927. "Check if POINT is in match-group GROUP.
  18928. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18929. match. If the match group does not exist or point is not inside it,
  18930. return nil."
  18931. (and (match-beginning group)
  18932. (>= point (match-beginning group))
  18933. (<= point (match-end group))
  18934. (if context
  18935. (list context (match-beginning group) (match-end group))
  18936. t)))
  18937. (defun org-switch-to-buffer-other-window (&rest args)
  18938. "Switch to buffer in a second window on the current frame.
  18939. In particular, do not allow pop-up frames.
  18940. Returns the newly created buffer."
  18941. (org-no-popups
  18942. (apply 'switch-to-buffer-other-window args)))
  18943. (defun org-combine-plists (&rest plists)
  18944. "Create a single property list from all plists in PLISTS.
  18945. The process starts by copying the first list, and then setting properties
  18946. from the other lists. Settings in the last list are the most significant
  18947. ones and overrule settings in the other lists."
  18948. (let ((rtn (copy-sequence (pop plists)))
  18949. p v ls)
  18950. (while plists
  18951. (setq ls (pop plists))
  18952. (while ls
  18953. (setq p (pop ls) v (pop ls))
  18954. (setq rtn (plist-put rtn p v))))
  18955. rtn))
  18956. (defun org-replace-escapes (string table)
  18957. "Replace %-escapes in STRING with values in TABLE.
  18958. TABLE is an association list with keys like \"%a\" and string values.
  18959. The sequences in STRING may contain normal field width and padding information,
  18960. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18961. so values can contain further %-escapes if they are define later in TABLE."
  18962. (let ((tbl (copy-alist table))
  18963. (case-fold-search nil)
  18964. (pchg 0)
  18965. e re rpl)
  18966. (while (setq e (pop tbl))
  18967. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18968. (when (and (cdr e) (string-match re (cdr e)))
  18969. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18970. (safe "SREF"))
  18971. (add-text-properties 0 3 (list 'sref sref) safe)
  18972. (setcdr e (replace-match safe t t (cdr e)))))
  18973. (while (string-match re string)
  18974. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18975. (cdr e)))
  18976. (setq string (replace-match rpl t t string))))
  18977. (while (setq pchg (next-property-change pchg string))
  18978. (let ((sref (get-text-property pchg 'sref string)))
  18979. (when (and sref (string-match "SREF" string pchg))
  18980. (setq string (replace-match sref t t string)))))
  18981. string))
  18982. (defun org-sublist (list start end)
  18983. "Return a section of LIST, from START to END.
  18984. Counting starts at 1."
  18985. (let (rtn (c start))
  18986. (setq list (nthcdr (1- start) list))
  18987. (while (and list (<= c end))
  18988. (push (pop list) rtn)
  18989. (setq c (1+ c)))
  18990. (nreverse rtn)))
  18991. (defun org-find-base-buffer-visiting (file)
  18992. "Like `find-buffer-visiting' but always return the base buffer and
  18993. not an indirect buffer."
  18994. (let ((buf (or (get-file-buffer file)
  18995. (find-buffer-visiting file))))
  18996. (if buf
  18997. (or (buffer-base-buffer buf) buf)
  18998. nil)))
  18999. (defun org-image-file-name-regexp (&optional extensions)
  19000. "Return regexp matching the file names of images.
  19001. If EXTENSIONS is given, only match these."
  19002. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19003. (image-file-name-regexp)
  19004. (let ((image-file-name-extensions
  19005. (or extensions
  19006. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19007. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19008. (concat "\\."
  19009. (regexp-opt (nconc (mapcar 'upcase
  19010. image-file-name-extensions)
  19011. image-file-name-extensions)
  19012. t)
  19013. "\\'"))))
  19014. (defun org-file-image-p (file &optional extensions)
  19015. "Return non-nil if FILE is an image."
  19016. (save-match-data
  19017. (string-match (org-image-file-name-regexp extensions) file)))
  19018. (defun org-get-cursor-date ()
  19019. "Return the date at cursor in as a time.
  19020. This works in the calendar and in the agenda, anywhere else it just
  19021. returns the current time."
  19022. (let (date day defd)
  19023. (cond
  19024. ((eq major-mode 'calendar-mode)
  19025. (setq date (calendar-cursor-to-date)
  19026. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19027. ((eq major-mode 'org-agenda-mode)
  19028. (setq day (get-text-property (point) 'day))
  19029. (if day
  19030. (setq date (calendar-gregorian-from-absolute day)
  19031. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  19032. (nth 2 date))))))
  19033. (or defd (current-time))))
  19034. (defun org-mark-subtree (&optional up)
  19035. "Mark the current subtree.
  19036. This puts point at the start of the current subtree, and mark at
  19037. the end. If a numeric prefix UP is given, move up into the
  19038. hierarchy of headlines by UP levels before marking the subtree."
  19039. (interactive "P")
  19040. (org-with-limited-levels
  19041. (cond ((org-at-heading-p) (beginning-of-line))
  19042. ((org-before-first-heading-p) (error "Not in a subtree"))
  19043. (t (outline-previous-visible-heading 1))))
  19044. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19045. (if (org-called-interactively-p 'any)
  19046. (call-interactively 'org-mark-element)
  19047. (org-mark-element)))
  19048. ;;; Indentation
  19049. (defun org-indent-line ()
  19050. "Indent line depending on context."
  19051. (interactive)
  19052. (let* ((pos (point))
  19053. (itemp (org-at-item-p))
  19054. (case-fold-search t)
  19055. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19056. (inline-task-p (and (featurep 'org-inlinetask)
  19057. (org-inlinetask-in-task-p)))
  19058. (inline-re (and inline-task-p
  19059. (org-inlinetask-outline-regexp)))
  19060. column)
  19061. (if (and orgstruct-is-++ (eq pos (point)))
  19062. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19063. (indent-according-to-mode))
  19064. (beginning-of-line 1)
  19065. (cond
  19066. ;; Headings
  19067. ((looking-at org-outline-regexp) (setq column 0))
  19068. ;; Included files
  19069. ((looking-at "#\\+include:") (setq column 0))
  19070. ;; Footnote definition
  19071. ((looking-at org-footnote-definition-re) (setq column 0))
  19072. ;; Literal examples
  19073. ((looking-at "[ \t]*:\\( \\|$\\)")
  19074. (setq column (org-get-indentation))) ; do nothing
  19075. ;; Lists
  19076. ((ignore-errors (goto-char (org-in-item-p)))
  19077. (setq column (if itemp
  19078. (org-get-indentation)
  19079. (org-list-item-body-column (point))))
  19080. (goto-char pos))
  19081. ;; Drawers
  19082. ((and (looking-at "[ \t]*:END:")
  19083. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19084. (save-excursion
  19085. (goto-char (1- (match-beginning 1)))
  19086. (setq column (current-column))))
  19087. ;; Special blocks
  19088. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19089. (save-excursion
  19090. (re-search-backward
  19091. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19092. (setq column (org-get-indentation (match-string 0))))
  19093. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19094. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19095. (save-excursion
  19096. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19097. (setq column
  19098. (cond ((equal (downcase (match-string 1)) "src")
  19099. ;; src blocks: let `org-edit-src-exit' handle them
  19100. (org-get-indentation))
  19101. ((equal (downcase (match-string 1)) "example")
  19102. (max (org-get-indentation)
  19103. (org-get-indentation (match-string 0))))
  19104. (t
  19105. (org-get-indentation (match-string 0))))))
  19106. ;; This line has nothing special, look at the previous relevant
  19107. ;; line to compute indentation
  19108. (t
  19109. (beginning-of-line 0)
  19110. (while (and (not (bobp))
  19111. (not (looking-at org-drawer-regexp))
  19112. ;; When point started in an inline task, do not move
  19113. ;; above task starting line.
  19114. (not (and inline-task-p (looking-at inline-re)))
  19115. ;; Skip drawers, blocks, empty lines, verbatim,
  19116. ;; comments, tables, footnotes definitions, lists,
  19117. ;; inline tasks.
  19118. (or (and (looking-at "[ \t]*:END:")
  19119. (re-search-backward org-drawer-regexp nil t))
  19120. (and (looking-at "[ \t]*#\\+end_")
  19121. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19122. (looking-at "[ \t]*[\n:#|]")
  19123. (looking-at org-footnote-definition-re)
  19124. (and (ignore-errors (goto-char (org-in-item-p)))
  19125. (goto-char
  19126. (org-list-get-top-point (org-list-struct))))
  19127. (and (not inline-task-p)
  19128. (featurep 'org-inlinetask)
  19129. (org-inlinetask-in-task-p)
  19130. (or (org-inlinetask-goto-beginning) t))))
  19131. (beginning-of-line 0))
  19132. (cond
  19133. ;; There was an heading above.
  19134. ((looking-at "\\*+[ \t]+")
  19135. (if (not org-adapt-indentation)
  19136. (setq column 0)
  19137. (goto-char (match-end 0))
  19138. (setq column (current-column))))
  19139. ;; A drawer had started and is unfinished
  19140. ((looking-at org-drawer-regexp)
  19141. (goto-char (1- (match-beginning 1)))
  19142. (setq column (current-column)))
  19143. ;; Else, nothing noticeable found: get indentation and go on.
  19144. (t (setq column (org-get-indentation))))))
  19145. ;; Now apply indentation and move cursor accordingly
  19146. (goto-char pos)
  19147. (if (<= (current-column) (current-indentation))
  19148. (org-indent-line-to column)
  19149. (save-excursion (org-indent-line-to column)))
  19150. ;; Special polishing for properties, see `org-property-format'
  19151. (setq column (current-column))
  19152. (beginning-of-line 1)
  19153. (if (looking-at
  19154. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19155. (replace-match (concat (match-string 1)
  19156. (format org-property-format
  19157. (match-string 2) (match-string 3)))
  19158. t t))
  19159. (org-move-to-column column))))
  19160. (defun org-indent-drawer ()
  19161. "Indent the drawer at point."
  19162. (interactive)
  19163. (let ((p (point))
  19164. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19165. (match-end 0)))
  19166. (folded
  19167. (save-excursion
  19168. (end-of-line)
  19169. (when (overlays-at (point))
  19170. (member 'invisible (overlay-properties
  19171. (car (overlays-at (point)))))))))
  19172. (when folded (org-cycle))
  19173. (indent-for-tab-command)
  19174. (while (and (move-beginning-of-line 2) (< (point) e))
  19175. (indent-for-tab-command))
  19176. (goto-char p)
  19177. (when folded (org-cycle)))
  19178. (message "Drawer at point indented"))
  19179. (defun org-indent-block ()
  19180. "Indent the block at point."
  19181. (interactive)
  19182. (let ((p (point))
  19183. (case-fold-search t)
  19184. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19185. (match-end 0)))
  19186. (folded
  19187. (save-excursion
  19188. (end-of-line)
  19189. (when (overlays-at (point))
  19190. (member 'invisible (overlay-properties
  19191. (car (overlays-at (point)))))))))
  19192. (when folded (org-cycle))
  19193. (indent-for-tab-command)
  19194. (while (and (move-beginning-of-line 2) (< (point) e))
  19195. (indent-for-tab-command))
  19196. (goto-char p)
  19197. (when folded (org-cycle)))
  19198. (message "Block at point indented"))
  19199. (defun org-indent-region (start end)
  19200. "Indent region."
  19201. (interactive "r")
  19202. (save-excursion
  19203. (let ((line-end (org-current-line end)))
  19204. (goto-char start)
  19205. (while (< (org-current-line) line-end)
  19206. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19207. (t (call-interactively 'org-indent-line)))
  19208. (move-beginning-of-line 2)))))
  19209. ;;; Filling
  19210. ;; We use our own fill-paragraph and auto-fill functions.
  19211. ;; `org-fill-paragraph' relies on adaptive filling and context
  19212. ;; checking. Appropriate `fill-prefix' is computed with
  19213. ;; `org-adaptive-fill-function'.
  19214. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19215. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19216. ;; `org-adaptive-fill-function' value. Internally,
  19217. ;; `org-comment-line-break-function' breaks the line.
  19218. ;; `org-setup-filling' installs filling and auto-filling related
  19219. ;; variables during `org-mode' initialization.
  19220. (defun org-setup-filling ()
  19221. (interactive)
  19222. ;; Prevent auto-fill from inserting unwanted new items.
  19223. (when (boundp 'fill-nobreak-predicate)
  19224. (org-set-local
  19225. 'fill-nobreak-predicate
  19226. (org-uniquify
  19227. (append fill-nobreak-predicate
  19228. '(org-fill-paragraph-separate-nobreak-p
  19229. org-fill-line-break-nobreak-p)))))
  19230. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19231. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19232. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19233. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19234. (defvar org-element-paragraph-separate) ; org-element.el
  19235. (defun org-fill-paragraph-separate-nobreak-p ()
  19236. "Non-nil when a line break at point would insert a new item."
  19237. (looking-at (substring org-element-paragraph-separate 1)))
  19238. (defun org-fill-line-break-nobreak-p ()
  19239. "Non-nil when a line break at point would create an Org line break."
  19240. (save-excursion
  19241. (skip-chars-backward "[ \t]")
  19242. (skip-chars-backward "\\\\")
  19243. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19244. (declare-function message-in-body-p "message" ())
  19245. (defvar org-element--affiliated-re) ; From org-element.el
  19246. (defun org-adaptive-fill-function ()
  19247. "Compute a fill prefix for the current line.
  19248. Return fill prefix, as a string, or nil if current line isn't
  19249. meant to be filled."
  19250. (org-with-wide-buffer
  19251. (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
  19252. ;; FIXME: This is really the job of orgstruct++-mode
  19253. (let* ((p (line-beginning-position))
  19254. (element (save-excursion (beginning-of-line)
  19255. (org-element-at-point)))
  19256. (type (org-element-type element))
  19257. (post-affiliated
  19258. (save-excursion
  19259. (goto-char (org-element-property :begin element))
  19260. (while (looking-at org-element--affiliated-re) (forward-line))
  19261. (point))))
  19262. (unless (< p post-affiliated)
  19263. (case type
  19264. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19265. (footnote-definition "")
  19266. ((item plain-list)
  19267. (make-string (org-list-item-body-column post-affiliated) ? ))
  19268. (paragraph
  19269. ;; Fill prefix is usually the same as the current line,
  19270. ;; except if the paragraph is at the beginning of an item.
  19271. (let ((parent (org-element-property :parent element)))
  19272. (cond ((eq (org-element-type parent) 'item)
  19273. (make-string (org-list-item-body-column
  19274. (org-element-property :begin parent))
  19275. ? ))
  19276. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19277. (match-string 0))
  19278. (t ""))))
  19279. (comment-block
  19280. ;; Only fill contents if P is within block boundaries.
  19281. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19282. (forward-line)
  19283. (point)))
  19284. (cend (save-excursion
  19285. (goto-char (org-element-property :end element))
  19286. (skip-chars-backward " \r\t\n")
  19287. (line-beginning-position))))
  19288. (when (and (>= p cbeg) (< p cend))
  19289. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19290. (match-string 0)
  19291. ""))))))))))
  19292. (declare-function message-goto-body "message" ())
  19293. (defvar message-cite-prefix-regexp) ; From message.el
  19294. (defvar org-element-all-objects) ; From org-element.el
  19295. (defun org-fill-paragraph (&optional justify)
  19296. "Fill element at point, when applicable.
  19297. This function only applies to comment blocks, comments, example
  19298. blocks and paragraphs. Also, as a special case, re-align table
  19299. when point is at one.
  19300. If JUSTIFY is non-nil (interactively, with prefix argument),
  19301. justify as well. If `sentence-end-double-space' is non-nil, then
  19302. period followed by one space does not end a sentence, so don't
  19303. break a line there. The variable `fill-column' controls the
  19304. width for filling.
  19305. For convenience, when point is at a plain list, an item or
  19306. a footnote definition, try to fill the first paragraph within."
  19307. (interactive)
  19308. (if (and (derived-mode-p 'message-mode)
  19309. (or (not (message-in-body-p))
  19310. (save-excursion (move-beginning-of-line 1)
  19311. (looking-at message-cite-prefix-regexp))))
  19312. ;; First ensure filling is correct in message-mode.
  19313. (let ((fill-paragraph-function
  19314. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19315. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19316. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19317. (paragraph-separate
  19318. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19319. (fill-paragraph nil))
  19320. (save-excursion
  19321. ;; Move to end of line in order to get the first paragraph
  19322. ;; within a plain list or a footnote definition.
  19323. (end-of-line)
  19324. (let ((element (org-element-at-point)))
  19325. ;; First check if point is in a blank line at the beginning of
  19326. ;; the buffer. In that case, ignore filling.
  19327. (if (< (point) (org-element-property :begin element)) t
  19328. (case (org-element-type element)
  19329. ;; Use major mode filling function is src blocks.
  19330. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19331. ;; Align Org tables, leave table.el tables as-is.
  19332. (table-row (org-table-align) t)
  19333. (table
  19334. (when (eq (org-element-property :type element) 'org)
  19335. (org-table-align))
  19336. t)
  19337. (paragraph
  19338. ;; Paragraphs may contain `line-break' type objects.
  19339. (let ((beg (max (point-min)
  19340. (org-element-property :contents-begin element)))
  19341. (end (min (point-max)
  19342. (org-element-property :contents-end element))))
  19343. ;; Do nothing if point is at an affiliated keyword.
  19344. (if (< (point) beg) t
  19345. (when (derived-mode-p 'message-mode)
  19346. ;; In `message-mode', do not fill following
  19347. ;; citation in current paragraph nor text before
  19348. ;; message body.
  19349. (let ((body-start (save-excursion (message-goto-body))))
  19350. (when body-start (setq beg (max body-start beg))))
  19351. (when (save-excursion
  19352. (re-search-forward
  19353. (concat "^" message-cite-prefix-regexp) end t))
  19354. (setq end (match-beginning 0))))
  19355. ;; Fill paragraph, taking line breaks into
  19356. ;; consideration. For that, slice the paragraph
  19357. ;; using line breaks as separators, and fill the
  19358. ;; parts in reverse order to avoid messing with
  19359. ;; markers.
  19360. (save-excursion
  19361. (goto-char end)
  19362. (mapc
  19363. (lambda (pos)
  19364. (fill-region-as-paragraph pos (point) justify)
  19365. (goto-char pos))
  19366. ;; Find the list of ending positions for line
  19367. ;; breaks in the current paragraph. Add paragraph
  19368. ;; beginning to include first slice.
  19369. (nreverse
  19370. (cons
  19371. beg
  19372. (org-element-map
  19373. (org-element--parse-objects
  19374. beg end nil org-element-all-objects)
  19375. 'line-break
  19376. (lambda (lb) (org-element-property :end lb)))))))
  19377. t)))
  19378. ;; Contents of `comment-block' type elements should be
  19379. ;; filled as plain text, but only if point is within block
  19380. ;; markers.
  19381. (comment-block
  19382. (let* ((case-fold-search t)
  19383. (beg (save-excursion
  19384. (goto-char (org-element-property :begin element))
  19385. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19386. (forward-line)
  19387. (point)))
  19388. (end (save-excursion
  19389. (goto-char (org-element-property :end element))
  19390. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19391. (line-beginning-position))))
  19392. (when (and (>= (point) beg) (< (point) end))
  19393. (fill-region-as-paragraph
  19394. (save-excursion
  19395. (end-of-line)
  19396. (re-search-backward "^[ \t]*$" beg 'move)
  19397. (line-beginning-position))
  19398. (save-excursion
  19399. (beginning-of-line)
  19400. (re-search-forward "^[ \t]*$" end 'move)
  19401. (line-beginning-position))
  19402. justify)))
  19403. t)
  19404. ;; Fill comments.
  19405. (comment (fill-comment-paragraph justify))
  19406. ;; Ignore every other element.
  19407. (otherwise t)))))))
  19408. (defun org-auto-fill-function ()
  19409. "Auto-fill function."
  19410. ;; Check if auto-filling is meaningful.
  19411. (let ((fc (current-fill-column)))
  19412. (when (and fc (> (current-column) fc))
  19413. (let* ((fill-prefix (org-adaptive-fill-function))
  19414. ;; Enforce empty fill prefix, if required. Otherwise, it
  19415. ;; will be computed again.
  19416. (adaptive-fill-mode (not (equal fill-prefix ""))))
  19417. (when fill-prefix (do-auto-fill))))))
  19418. (defun org-comment-line-break-function (&optional soft)
  19419. "Break line at point and indent, continuing comment if within one.
  19420. The inserted newline is marked hard if variable
  19421. `use-hard-newlines' is true, unless optional argument SOFT is
  19422. non-nil."
  19423. (if soft (insert-and-inherit ?\n) (newline 1))
  19424. (save-excursion (forward-char -1) (delete-horizontal-space))
  19425. (delete-horizontal-space)
  19426. (indent-to-left-margin)
  19427. (insert-before-markers-and-inherit fill-prefix))
  19428. ;;; Comments
  19429. ;; Org comments syntax is quite complex. It requires the entire line
  19430. ;; to be just a comment. Also, even with the right syntax at the
  19431. ;; beginning of line, some some elements (i.e. verse-block or
  19432. ;; example-block) don't accept comments. Usual Emacs comment commands
  19433. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19434. ;; Org still relies on `comment-dwim', but cannot trust
  19435. ;; `comment-only-p'. So, `comment-region-function' and
  19436. ;; `uncomment-region-function' both point
  19437. ;; to`org-comment-or-uncomment-region'. Eventually,
  19438. ;; `org-insert-comment' takes care of insertion of comments at the
  19439. ;; beginning of line.
  19440. ;; `org-setup-comments-handling' install comments related variables
  19441. ;; during `org-mode' initialization.
  19442. (defun org-setup-comments-handling ()
  19443. (interactive)
  19444. (org-set-local 'comment-use-syntax nil)
  19445. (org-set-local 'comment-start "# ")
  19446. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19447. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19448. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19449. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19450. (defun org-insert-comment ()
  19451. "Insert an empty comment above current line.
  19452. If the line is empty, insert comment at its beginning."
  19453. (beginning-of-line)
  19454. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19455. (org-indent-line)
  19456. (insert "# "))
  19457. (defvar comment-empty-lines) ; From newcomment.el.
  19458. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19459. "Comment or uncomment each non-blank line in the region.
  19460. Uncomment each non-blank line between BEG and END if it only
  19461. contains commented lines. Otherwise, comment them."
  19462. (save-restriction
  19463. ;; Restrict region
  19464. (narrow-to-region (save-excursion (goto-char beg)
  19465. (skip-chars-forward " \r\t\n" end)
  19466. (line-beginning-position))
  19467. (save-excursion (goto-char end)
  19468. (skip-chars-backward " \r\t\n" beg)
  19469. (line-end-position)))
  19470. (let ((uncommentp
  19471. ;; UNCOMMENTP is non-nil when every non blank line between
  19472. ;; BEG and END is a comment.
  19473. (save-excursion
  19474. (goto-char (point-min))
  19475. (while (and (not (eobp))
  19476. (let ((element (org-element-at-point)))
  19477. (and (eq (org-element-type element) 'comment)
  19478. (goto-char (min (point-max)
  19479. (org-element-property
  19480. :end element)))))))
  19481. (eobp))))
  19482. (if uncommentp
  19483. ;; Only blank lines and comments in region: uncomment it.
  19484. (save-excursion
  19485. (goto-char (point-min))
  19486. (while (not (eobp))
  19487. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19488. (replace-match "" nil nil nil 1))
  19489. (forward-line)))
  19490. ;; Comment each line in region.
  19491. (let ((min-indent (point-max)))
  19492. ;; First find the minimum indentation across all lines.
  19493. (save-excursion
  19494. (goto-char (point-min))
  19495. (while (and (not (eobp)) (not (zerop min-indent)))
  19496. (unless (looking-at "[ \t]*$")
  19497. (setq min-indent (min min-indent (current-indentation))))
  19498. (forward-line)))
  19499. ;; Then loop over all lines.
  19500. (save-excursion
  19501. (goto-char (point-min))
  19502. (while (not (eobp))
  19503. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19504. (org-move-to-column min-indent t)
  19505. (insert comment-start))
  19506. (forward-line))))))))
  19507. ;;; Other stuff.
  19508. (defun org-toggle-fixed-width-section (arg)
  19509. "Toggle the fixed-width export.
  19510. If there is no active region, the QUOTE keyword at the current headline is
  19511. inserted or removed. When present, it causes the text between this headline
  19512. and the next to be exported as fixed-width text, and unmodified.
  19513. If there is an active region, this command adds or removes a colon as the
  19514. first character of this line. If the first character of a line is a colon,
  19515. this line is also exported in fixed-width font."
  19516. (interactive "P")
  19517. (let* ((cc 0)
  19518. (regionp (org-region-active-p))
  19519. (beg (if regionp (region-beginning) (point)))
  19520. (end (if regionp (region-end)))
  19521. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19522. (case-fold-search nil)
  19523. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19524. off)
  19525. (if regionp
  19526. (save-excursion
  19527. (goto-char beg)
  19528. (setq cc (current-column))
  19529. (beginning-of-line 1)
  19530. (setq off (looking-at re))
  19531. (while (> nlines 0)
  19532. (setq nlines (1- nlines))
  19533. (beginning-of-line 1)
  19534. (cond
  19535. (arg
  19536. (org-move-to-column cc t)
  19537. (insert ": \n")
  19538. (forward-line -1))
  19539. ((and off (looking-at re))
  19540. (replace-match "" t t nil 1))
  19541. ((not off) (org-move-to-column cc t) (insert ": ")))
  19542. (forward-line 1)))
  19543. (save-excursion
  19544. (org-back-to-heading)
  19545. (cond
  19546. ((looking-at (format org-heading-keyword-regexp-format
  19547. org-quote-string))
  19548. (goto-char (match-end 1))
  19549. (looking-at (concat " +" org-quote-string))
  19550. (replace-match "" t t)
  19551. (when (eolp) (insert " ")))
  19552. ((looking-at org-outline-regexp)
  19553. (goto-char (match-end 0))
  19554. (insert org-quote-string " ")))))))
  19555. (defun org-reftex-citation ()
  19556. "Use reftex-citation to insert a citation into the buffer.
  19557. This looks for a line like
  19558. #+BIBLIOGRAPHY: foo plain option:-d
  19559. and derives from it that foo.bib is the bibliography file relevant
  19560. for this document. It then installs the necessary environment for RefTeX
  19561. to work in this buffer and calls `reftex-citation' to insert a citation
  19562. into the buffer.
  19563. Export of such citations to both LaTeX and HTML is handled by the contributed
  19564. package org-exp-bibtex by Taru Karttunen."
  19565. (interactive)
  19566. (let ((reftex-docstruct-symbol 'rds)
  19567. (reftex-cite-format "\\cite{%l}")
  19568. rds bib)
  19569. (save-excursion
  19570. (save-restriction
  19571. (widen)
  19572. (let ((case-fold-search t)
  19573. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19574. (if (not (save-excursion
  19575. (or (re-search-forward re nil t)
  19576. (re-search-backward re nil t))))
  19577. (error "No bibliography defined in file")
  19578. (setq bib (concat (match-string 1) ".bib")
  19579. rds (list (list 'bib bib)))))))
  19580. (call-interactively 'reftex-citation)))
  19581. ;;;; Functions extending outline functionality
  19582. (defun org-beginning-of-line (&optional arg)
  19583. "Go to the beginning of the current line. If that is invisible, continue
  19584. to a visible line beginning. This makes the function of C-a more intuitive.
  19585. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19586. first attempt, and only move to after the tags when the cursor is already
  19587. beyond the end of the headline."
  19588. (interactive "P")
  19589. (let ((pos (point))
  19590. (special (if (consp org-special-ctrl-a/e)
  19591. (car org-special-ctrl-a/e)
  19592. org-special-ctrl-a/e))
  19593. refpos)
  19594. (if (org-bound-and-true-p visual-line-mode)
  19595. (beginning-of-visual-line 1)
  19596. (beginning-of-line 1))
  19597. (if (and arg (fboundp 'move-beginning-of-line))
  19598. (call-interactively 'move-beginning-of-line)
  19599. (if (bobp)
  19600. nil
  19601. (backward-char 1)
  19602. (if (org-truely-invisible-p)
  19603. (while (and (not (bobp)) (org-truely-invisible-p))
  19604. (backward-char 1)
  19605. (beginning-of-line 1))
  19606. (forward-char 1))))
  19607. (when special
  19608. (cond
  19609. ((and (looking-at org-complex-heading-regexp)
  19610. (= (char-after (match-end 1)) ?\ ))
  19611. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19612. (point-at-eol)))
  19613. (goto-char
  19614. (if (eq special t)
  19615. (cond ((> pos refpos) refpos)
  19616. ((= pos (point)) refpos)
  19617. (t (point)))
  19618. (cond ((> pos (point)) (point))
  19619. ((not (eq last-command this-command)) (point))
  19620. (t refpos)))))
  19621. ((org-at-item-p)
  19622. ;; Being at an item and not looking at an the item means point
  19623. ;; was previously moved to beginning of a visual line, which
  19624. ;; doesn't contain the item. Therefore, do nothing special,
  19625. ;; just stay here.
  19626. (when (looking-at org-list-full-item-re)
  19627. ;; Set special position at first white space character after
  19628. ;; bullet, and check-box, if any.
  19629. (let ((after-bullet
  19630. (let ((box (match-end 3)))
  19631. (if (not box) (match-end 1)
  19632. (let ((after (char-after box)))
  19633. (if (and after (= after ? )) (1+ box) box))))))
  19634. ;; Special case: Move point to special position when
  19635. ;; currently after it or at beginning of line.
  19636. (if (eq special t)
  19637. (when (or (> pos after-bullet) (= (point) pos))
  19638. (goto-char after-bullet))
  19639. ;; Reversed case: Move point to special position when
  19640. ;; point was already at beginning of line and command is
  19641. ;; repeated.
  19642. (when (and (= (point) pos) (eq last-command this-command))
  19643. (goto-char after-bullet))))))))
  19644. (org-no-warnings
  19645. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19646. (defun org-end-of-line (&optional arg)
  19647. "Go to the end of the line.
  19648. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  19649. tags on the first attempt, and only move to after the tags when
  19650. the cursor is already beyond the end of the headline."
  19651. (interactive "P")
  19652. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  19653. org-special-ctrl-a/e))
  19654. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  19655. 'end-of-visual-line)
  19656. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19657. (t 'end-of-line))))
  19658. (if (or (not special) arg) (call-interactively move-fun)
  19659. (let* ((element (save-excursion (beginning-of-line)
  19660. (org-element-at-point)))
  19661. (type (org-element-type element)))
  19662. (cond
  19663. ((memq type '(headline inlinetask))
  19664. (let ((pos (point)))
  19665. (beginning-of-line 1)
  19666. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19667. (if (eq special t)
  19668. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  19669. (goto-char (match-beginning 1))
  19670. (goto-char (match-end 0)))
  19671. (if (or (< pos (match-end 0))
  19672. (not (eq this-command last-command)))
  19673. (goto-char (match-end 0))
  19674. (goto-char (match-beginning 1))))
  19675. (call-interactively move-fun))))
  19676. ((org-element-property :hiddenp element)
  19677. ;; If element is hidden, `move-end-of-line' would put point
  19678. ;; after it. Use `end-of-line' to stay on current line.
  19679. (call-interactively 'end-of-line))
  19680. (t (call-interactively move-fun)))))
  19681. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19682. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19683. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19684. (defun org-backward-sentence (&optional arg)
  19685. "Go to beginning of sentence, or beginning of table field.
  19686. This will call `backward-sentence' or `org-table-beginning-of-field',
  19687. depending on context."
  19688. (interactive "P")
  19689. (cond
  19690. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19691. (t (call-interactively 'backward-sentence))))
  19692. (defun org-forward-sentence (&optional arg)
  19693. "Go to end of sentence, or end of table field.
  19694. This will call `forward-sentence' or `org-table-end-of-field',
  19695. depending on context."
  19696. (interactive "P")
  19697. (cond
  19698. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19699. (t (call-interactively 'forward-sentence))))
  19700. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19701. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19702. (defun org-kill-line (&optional arg)
  19703. "Kill line, to tags or end of line."
  19704. (interactive "P")
  19705. (cond
  19706. ((or (not org-special-ctrl-k)
  19707. (bolp)
  19708. (not (org-at-heading-p)))
  19709. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19710. org-ctrl-k-protect-subtree)
  19711. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19712. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19713. (error "C-k aborted - would kill hidden subtree")))
  19714. (call-interactively
  19715. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  19716. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19717. (kill-region (point) (match-beginning 1))
  19718. (org-set-tags nil t))
  19719. (t (kill-region (point) (point-at-eol)))))
  19720. (define-key org-mode-map "\C-k" 'org-kill-line)
  19721. (defun org-yank (&optional arg)
  19722. "Yank. If the kill is a subtree, treat it specially.
  19723. This command will look at the current kill and check if is a single
  19724. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19725. cursor is at the beginning of a line or after the stars of a currently
  19726. empty headline, then the yank is handled specially. How exactly depends
  19727. on the value of the following variables, both set by default.
  19728. org-yank-folded-subtrees
  19729. When set, the subtree(s) will be folded after insertion, but only
  19730. if doing so would now swallow text after the yanked text.
  19731. org-yank-adjusted-subtrees
  19732. When set, the subtree will be promoted or demoted in order to
  19733. fit into the local outline tree structure, which means that the level
  19734. will be adjusted so that it becomes the smaller one of the two
  19735. *visible* surrounding headings.
  19736. Any prefix to this command will cause `yank' to be called directly with
  19737. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19738. will just
  19739. plainly yank the text as it is.
  19740. \[1] The test checks if the first non-white line is a heading
  19741. and if there are no other headings with fewer stars."
  19742. (interactive "P")
  19743. (org-yank-generic 'yank arg))
  19744. (defun org-yank-generic (command arg)
  19745. "Perform some yank-like command.
  19746. This function implements the behavior described in the `org-yank'
  19747. documentation. However, it has been generalized to work for any
  19748. interactive command with similar behavior."
  19749. ;; pretend to be command COMMAND
  19750. (setq this-command command)
  19751. (if arg
  19752. (call-interactively command)
  19753. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19754. (and (org-kill-is-subtree-p)
  19755. (or (bolp)
  19756. (and (looking-at "[ \t]*$")
  19757. (string-match
  19758. "\\`\\*+\\'"
  19759. (buffer-substring (point-at-bol) (point)))))))
  19760. swallowp)
  19761. (cond
  19762. ((and subtreep org-yank-folded-subtrees)
  19763. (let ((beg (point))
  19764. end)
  19765. (if (and subtreep org-yank-adjusted-subtrees)
  19766. (org-paste-subtree nil nil 'for-yank)
  19767. (call-interactively command))
  19768. (setq end (point))
  19769. (goto-char beg)
  19770. (when (and (bolp) subtreep
  19771. (not (setq swallowp
  19772. (org-yank-folding-would-swallow-text beg end))))
  19773. (org-with-limited-levels
  19774. (or (looking-at org-outline-regexp)
  19775. (re-search-forward org-outline-regexp-bol end t))
  19776. (while (and (< (point) end) (looking-at org-outline-regexp))
  19777. (hide-subtree)
  19778. (org-cycle-show-empty-lines 'folded)
  19779. (condition-case nil
  19780. (outline-forward-same-level 1)
  19781. (error (goto-char end))))))
  19782. (when swallowp
  19783. (message
  19784. "Inserted text not folded because that would swallow text"))
  19785. (goto-char end)
  19786. (skip-chars-forward " \t\n\r")
  19787. (beginning-of-line 1)
  19788. (push-mark beg 'nomsg)))
  19789. ((and subtreep org-yank-adjusted-subtrees)
  19790. (let ((beg (point-at-bol)))
  19791. (org-paste-subtree nil nil 'for-yank)
  19792. (push-mark beg 'nomsg)))
  19793. (t
  19794. (call-interactively command))))))
  19795. (defun org-yank-folding-would-swallow-text (beg end)
  19796. "Would hide-subtree at BEG swallow any text after END?"
  19797. (let (level)
  19798. (org-with-limited-levels
  19799. (save-excursion
  19800. (goto-char beg)
  19801. (when (or (looking-at org-outline-regexp)
  19802. (re-search-forward org-outline-regexp-bol end t))
  19803. (setq level (org-outline-level)))
  19804. (goto-char end)
  19805. (skip-chars-forward " \t\r\n\v\f")
  19806. (if (or (eobp)
  19807. (and (bolp) (looking-at org-outline-regexp)
  19808. (<= (org-outline-level) level)))
  19809. nil ; Nothing would be swallowed
  19810. t))))) ; something would swallow
  19811. (define-key org-mode-map "\C-y" 'org-yank)
  19812. (defun org-truely-invisible-p ()
  19813. "Check if point is at a character currently not visible.
  19814. This version does not only check the character property, but also
  19815. `visible-mode'."
  19816. ;; Early versions of noutline don't have `outline-invisible-p'.
  19817. (if (org-bound-and-true-p visible-mode)
  19818. nil
  19819. (outline-invisible-p)))
  19820. (defun org-invisible-p2 ()
  19821. "Check if point is at a character currently not visible."
  19822. (save-excursion
  19823. (if (and (eolp) (not (bobp))) (backward-char 1))
  19824. ;; Early versions of noutline don't have `outline-invisible-p'.
  19825. (outline-invisible-p)))
  19826. (defun org-back-to-heading (&optional invisible-ok)
  19827. "Call `outline-back-to-heading', but provide a better error message."
  19828. (condition-case nil
  19829. (outline-back-to-heading invisible-ok)
  19830. (error (error "Before first headline at position %d in buffer %s"
  19831. (point) (current-buffer)))))
  19832. (defun org-before-first-heading-p ()
  19833. "Before first heading?"
  19834. (save-excursion
  19835. (end-of-line)
  19836. (null (re-search-backward org-outline-regexp-bol nil t))))
  19837. (defun org-at-heading-p (&optional ignored)
  19838. (outline-on-heading-p t))
  19839. ;; Compatibility alias with Org versions < 7.8.03
  19840. (defalias 'org-on-heading-p 'org-at-heading-p)
  19841. (defun org-at-comment-p nil
  19842. "Is cursor in a line starting with a # character?"
  19843. (save-excursion
  19844. (beginning-of-line)
  19845. (looking-at "^#")))
  19846. (defun org-at-drawer-p nil
  19847. "Is cursor at a drawer keyword?"
  19848. (save-excursion
  19849. (move-beginning-of-line 1)
  19850. (looking-at org-drawer-regexp)))
  19851. (defun org-at-block-p nil
  19852. "Is cursor at a block keyword?"
  19853. (save-excursion
  19854. (move-beginning-of-line 1)
  19855. (looking-at org-block-regexp)))
  19856. (defun org-point-at-end-of-empty-headline ()
  19857. "If point is at the end of an empty headline, return t, else nil.
  19858. If the heading only contains a TODO keyword, it is still still considered
  19859. empty."
  19860. (and (looking-at "[ \t]*$")
  19861. (when org-todo-line-regexp
  19862. (save-excursion
  19863. (beginning-of-line 1)
  19864. (let ((case-fold-search nil))
  19865. (looking-at org-todo-line-regexp)
  19866. (string= (match-string 3) ""))))))
  19867. (defun org-at-heading-or-item-p ()
  19868. (or (org-at-heading-p) (org-at-item-p)))
  19869. (defun org-at-target-p ()
  19870. (or (org-in-regexp org-radio-target-regexp)
  19871. (org-in-regexp org-target-regexp)))
  19872. ;; Compatibility alias with Org versions < 7.8.03
  19873. (defalias 'org-on-target-p 'org-at-target-p)
  19874. (defun org-up-heading-all (arg)
  19875. "Move to the heading line of which the present line is a subheading.
  19876. This function considers both visible and invisible heading lines.
  19877. With argument, move up ARG levels."
  19878. (if (fboundp 'outline-up-heading-all)
  19879. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19880. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19881. (defun org-up-heading-safe ()
  19882. "Move to the heading line of which the present line is a subheading.
  19883. This version will not throw an error. It will return the level of the
  19884. headline found, or nil if no higher level is found.
  19885. Also, this function will be a lot faster than `outline-up-heading',
  19886. because it relies on stars being the outline starters. This can really
  19887. make a significant difference in outlines with very many siblings."
  19888. (let (start-level re)
  19889. (org-back-to-heading t)
  19890. (setq start-level (funcall outline-level))
  19891. (if (equal start-level 1)
  19892. nil
  19893. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19894. (if (re-search-backward re nil t)
  19895. (funcall outline-level)))))
  19896. (defun org-first-sibling-p ()
  19897. "Is this heading the first child of its parents?"
  19898. (interactive)
  19899. (let ((re org-outline-regexp-bol)
  19900. level l)
  19901. (unless (org-at-heading-p t)
  19902. (error "Not at a heading"))
  19903. (setq level (funcall outline-level))
  19904. (save-excursion
  19905. (if (not (re-search-backward re nil t))
  19906. t
  19907. (setq l (funcall outline-level))
  19908. (< l level)))))
  19909. (defun org-goto-sibling (&optional previous)
  19910. "Goto the next sibling, even if it is invisible.
  19911. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19912. when a sibling was found. When none is found, return nil and don't
  19913. move point."
  19914. (let ((fun (if previous 're-search-backward 're-search-forward))
  19915. (pos (point))
  19916. (re org-outline-regexp-bol)
  19917. level l)
  19918. (when (condition-case nil (org-back-to-heading t) (error nil))
  19919. (setq level (funcall outline-level))
  19920. (catch 'exit
  19921. (or previous (forward-char 1))
  19922. (while (funcall fun re nil t)
  19923. (setq l (funcall outline-level))
  19924. (when (< l level) (goto-char pos) (throw 'exit nil))
  19925. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19926. (goto-char pos)
  19927. nil))))
  19928. (defun org-show-siblings ()
  19929. "Show all siblings of the current headline."
  19930. (save-excursion
  19931. (while (org-goto-sibling) (org-flag-heading nil)))
  19932. (save-excursion
  19933. (while (org-goto-sibling 'previous)
  19934. (org-flag-heading nil))))
  19935. (defun org-goto-first-child ()
  19936. "Goto the first child, even if it is invisible.
  19937. Return t when a child was found. Otherwise don't move point and
  19938. return nil."
  19939. (let (level (pos (point)) (re org-outline-regexp-bol))
  19940. (when (condition-case nil (org-back-to-heading t) (error nil))
  19941. (setq level (outline-level))
  19942. (forward-char 1)
  19943. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19944. (progn (goto-char (match-beginning 0)) t)
  19945. (goto-char pos) nil))))
  19946. (defun org-show-hidden-entry ()
  19947. "Show an entry where even the heading is hidden."
  19948. (save-excursion
  19949. (org-show-entry)))
  19950. (defun org-flag-heading (flag &optional entry)
  19951. "Flag the current heading. FLAG non-nil means make invisible.
  19952. When ENTRY is non-nil, show the entire entry."
  19953. (save-excursion
  19954. (org-back-to-heading t)
  19955. ;; Check if we should show the entire entry
  19956. (if entry
  19957. (progn
  19958. (org-show-entry)
  19959. (save-excursion
  19960. (and (outline-next-heading)
  19961. (org-flag-heading nil))))
  19962. (outline-flag-region (max (point-min) (1- (point)))
  19963. (save-excursion (outline-end-of-heading) (point))
  19964. flag))))
  19965. (defun org-get-next-sibling ()
  19966. "Move to next heading of the same level, and return point.
  19967. If there is no such heading, return nil.
  19968. This is like outline-next-sibling, but invisible headings are ok."
  19969. (let ((level (funcall outline-level)))
  19970. (outline-next-heading)
  19971. (while (and (not (eobp)) (> (funcall outline-level) level))
  19972. (outline-next-heading))
  19973. (if (or (eobp) (< (funcall outline-level) level))
  19974. nil
  19975. (point))))
  19976. (defun org-get-last-sibling ()
  19977. "Move to previous heading of the same level, and return point.
  19978. If there is no such heading, return nil."
  19979. (let ((opoint (point))
  19980. (level (funcall outline-level)))
  19981. (outline-previous-heading)
  19982. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19983. (while (and (> (funcall outline-level) level)
  19984. (not (bobp)))
  19985. (outline-previous-heading))
  19986. (if (< (funcall outline-level) level)
  19987. nil
  19988. (point)))))
  19989. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  19990. "Goto to the end of a subtree."
  19991. ;; This contains an exact copy of the original function, but it uses
  19992. ;; `org-back-to-heading', to make it work also in invisible
  19993. ;; trees. And is uses an invisible-ok argument.
  19994. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19995. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19996. ;; with many children and grandchildren etc, this can be much faster
  19997. ;; than the outline version.
  19998. (org-back-to-heading invisible-ok)
  19999. (let ((first t)
  20000. (level (funcall outline-level)))
  20001. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20002. ;; A true heading (not a plain list item), in Org-mode
  20003. ;; This means we can easily find the end by looking
  20004. ;; only for the right number of stars. Using a regexp to do
  20005. ;; this is so much faster than using a Lisp loop.
  20006. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20007. (forward-char 1)
  20008. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20009. ;; something else, do it the slow way
  20010. (while (and (not (eobp))
  20011. (or first (> (funcall outline-level) level)))
  20012. (setq first nil)
  20013. (outline-next-heading)))
  20014. (unless to-heading
  20015. (if (memq (preceding-char) '(?\n ?\^M))
  20016. (progn
  20017. ;; Go to end of line before heading
  20018. (forward-char -1)
  20019. (if (memq (preceding-char) '(?\n ?\^M))
  20020. ;; leave blank line before heading
  20021. (forward-char -1))))))
  20022. (point))
  20023. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20024. "Use Org version in org-mode, for dramatic speed-up."
  20025. (if (derived-mode-p 'org-mode)
  20026. (progn
  20027. (org-end-of-subtree nil t)
  20028. (unless (eobp) (backward-char 1)))
  20029. ad-do-it))
  20030. (defun org-end-of-meta-data-and-drawers ()
  20031. "Jump to the first text after meta data and drawers in the current entry.
  20032. This will move over empty lines, lines with planning time stamps,
  20033. clocking lines, and drawers."
  20034. (org-back-to-heading t)
  20035. (let ((end (save-excursion (outline-next-heading) (point)))
  20036. (re (concat "\\(" org-drawer-regexp "\\)"
  20037. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20038. (forward-line 1)
  20039. (while (re-search-forward re end t)
  20040. (if (not (match-end 1))
  20041. ;; empty or planning line
  20042. (forward-line 1)
  20043. ;; a drawer, find the end
  20044. (re-search-forward "^[ \t]*:END:" end 'move)
  20045. (forward-line 1)))
  20046. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20047. (point)))
  20048. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20049. "Move forward to the arg'th subheading at same level as this one.
  20050. Stop at the first and last subheadings of a superior heading.
  20051. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20052. non-nil it will also look at invisible ones."
  20053. (interactive "p")
  20054. (org-back-to-heading invisible-ok)
  20055. (org-at-heading-p)
  20056. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20057. (re (format "^\\*\\{1,%d\\} " level))
  20058. l)
  20059. (forward-char 1)
  20060. (while (> arg 0)
  20061. (while (and (re-search-forward re nil 'move)
  20062. (setq l (- (match-end 0) (match-beginning 0) 1))
  20063. (= l level)
  20064. (not invisible-ok)
  20065. (progn (backward-char 1) (outline-invisible-p)))
  20066. (if (< l level) (setq arg 1)))
  20067. (setq arg (1- arg)))
  20068. (beginning-of-line 1)))
  20069. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20070. "Move backward to the arg'th subheading at same level as this one.
  20071. Stop at the first and last subheadings of a superior heading."
  20072. (interactive "p")
  20073. (org-back-to-heading)
  20074. (org-at-heading-p)
  20075. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20076. (re (format "^\\*\\{1,%d\\} " level))
  20077. l)
  20078. (while (> arg 0)
  20079. (while (and (re-search-backward re nil 'move)
  20080. (setq l (- (match-end 0) (match-beginning 0) 1))
  20081. (= l level)
  20082. (not invisible-ok)
  20083. (outline-invisible-p))
  20084. (if (< l level) (setq arg 1)))
  20085. (setq arg (1- arg)))))
  20086. (defun org-forward-element ()
  20087. "Move forward by one element.
  20088. Move to the next element at the same level, when possible."
  20089. (interactive)
  20090. (cond ((eobp) (error "Cannot move further down"))
  20091. ((org-with-limited-levels (org-at-heading-p))
  20092. (let ((origin (point)))
  20093. (org-forward-heading-same-level 1)
  20094. (unless (org-with-limited-levels (org-at-heading-p))
  20095. (goto-char origin)
  20096. (error "Cannot move further down"))))
  20097. (t
  20098. (let* ((elem (org-element-at-point))
  20099. (end (org-element-property :end elem))
  20100. (parent (org-element-property :parent elem)))
  20101. (if (and parent (= (org-element-property :contents-end parent) end))
  20102. (goto-char (org-element-property :end parent))
  20103. (goto-char end))))))
  20104. (defun org-backward-element ()
  20105. "Move backward by one element.
  20106. Move to the previous element at the same level, when possible."
  20107. (interactive)
  20108. (cond ((bobp) (error "Cannot move further up"))
  20109. ((org-with-limited-levels (org-at-heading-p))
  20110. ;; At an headline, move to the previous one, if any, or stay
  20111. ;; here.
  20112. (let ((origin (point)))
  20113. (org-backward-heading-same-level 1)
  20114. (unless (org-with-limited-levels (org-at-heading-p))
  20115. (goto-char origin)
  20116. (error "Cannot move further up"))))
  20117. (t
  20118. (let* ((trail (org-element-at-point 'keep-trail))
  20119. (elem (car trail))
  20120. (prev-elem (nth 1 trail))
  20121. (beg (org-element-property :begin elem)))
  20122. (cond
  20123. ;; Move to beginning of current element if point isn't
  20124. ;; there already.
  20125. ((/= (point) beg) (goto-char beg))
  20126. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20127. ((org-before-first-heading-p) (goto-char (point-min)))
  20128. (t (org-back-to-heading)))))))
  20129. (defun org-up-element ()
  20130. "Move to upper element."
  20131. (interactive)
  20132. (if (org-with-limited-levels (org-at-heading-p))
  20133. (unless (org-up-heading-safe) (error "No surrounding element"))
  20134. (let* ((elem (org-element-at-point))
  20135. (parent (org-element-property :parent elem)))
  20136. (if parent (goto-char (org-element-property :begin parent))
  20137. (if (org-with-limited-levels (org-before-first-heading-p))
  20138. (error "No surrounding element")
  20139. (org-with-limited-levels (org-back-to-heading)))))))
  20140. (defvar org-element-greater-elements)
  20141. (defun org-down-element ()
  20142. "Move to inner element."
  20143. (interactive)
  20144. (let ((element (org-element-at-point)))
  20145. (cond
  20146. ((memq (org-element-type element) '(plain-list table))
  20147. (goto-char (org-element-property :contents-begin element))
  20148. (forward-char))
  20149. ((memq (org-element-type element) org-element-greater-elements)
  20150. ;; If contents are hidden, first disclose them.
  20151. (when (org-element-property :hiddenp element) (org-cycle))
  20152. (goto-char (or (org-element-property :contents-begin element)
  20153. (error "No content for this element"))))
  20154. (t (error "No inner element")))))
  20155. (defun org-drag-element-backward ()
  20156. "Move backward element at point."
  20157. (interactive)
  20158. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20159. (let* ((trail (org-element-at-point 'keep-trail))
  20160. (elem (car trail))
  20161. (prev-elem (nth 1 trail)))
  20162. ;; Error out if no previous element or previous element is
  20163. ;; a parent of the current one.
  20164. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20165. (error "Cannot drag element backward")
  20166. (let ((pos (point)))
  20167. (org-element-swap-A-B prev-elem elem)
  20168. (goto-char (+ (org-element-property :begin prev-elem)
  20169. (- pos (org-element-property :begin elem)))))))))
  20170. (defun org-drag-element-forward ()
  20171. "Move forward element at point."
  20172. (interactive)
  20173. (let* ((pos (point))
  20174. (elem (org-element-at-point)))
  20175. (when (= (point-max) (org-element-property :end elem))
  20176. (error "Cannot drag element forward"))
  20177. (goto-char (org-element-property :end elem))
  20178. (let ((next-elem (org-element-at-point)))
  20179. (when (or (org-element-nested-p elem next-elem)
  20180. (and (eq (org-element-type next-elem) 'headline)
  20181. (not (eq (org-element-type elem) 'headline))))
  20182. (goto-char pos)
  20183. (error "Cannot drag element forward"))
  20184. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20185. ;; body's length (without final blanks) and by the length of
  20186. ;; blanks between ELEM and NEXT-ELEM.
  20187. (let ((size-next (- (save-excursion
  20188. (goto-char (org-element-property :end next-elem))
  20189. (skip-chars-backward " \r\t\n")
  20190. (forward-line)
  20191. ;; Small correction if buffer doesn't end
  20192. ;; with a newline character.
  20193. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20194. (org-element-property :begin next-elem)))
  20195. (size-blank (- (org-element-property :end elem)
  20196. (save-excursion
  20197. (goto-char (org-element-property :end elem))
  20198. (skip-chars-backward " \r\t\n")
  20199. (forward-line)
  20200. (point)))))
  20201. (org-element-swap-A-B elem next-elem)
  20202. (goto-char (+ pos size-next size-blank))))))
  20203. (defun org-mark-element ()
  20204. "Put point at beginning of this element, mark at end.
  20205. Interactively, if this command is repeated or (in Transient Mark
  20206. mode) if the mark is active, it marks the next element after the
  20207. ones already marked."
  20208. (interactive)
  20209. (let (deactivate-mark)
  20210. (if (and (org-called-interactively-p 'any)
  20211. (or (and (eq last-command this-command) (mark t))
  20212. (and transient-mark-mode mark-active)))
  20213. (set-mark
  20214. (save-excursion
  20215. (goto-char (mark))
  20216. (goto-char (org-element-property :end (org-element-at-point)))))
  20217. (let ((element (org-element-at-point)))
  20218. (end-of-line)
  20219. (push-mark (org-element-property :end element) t t)
  20220. (goto-char (org-element-property :begin element))))))
  20221. (defun org-narrow-to-element ()
  20222. "Narrow buffer to current element."
  20223. (interactive)
  20224. (let ((elem (org-element-at-point)))
  20225. (cond
  20226. ((eq (car elem) 'headline)
  20227. (narrow-to-region
  20228. (org-element-property :begin elem)
  20229. (org-element-property :end elem)))
  20230. ((memq (car elem) org-element-greater-elements)
  20231. (narrow-to-region
  20232. (org-element-property :contents-begin elem)
  20233. (org-element-property :contents-end elem)))
  20234. (t
  20235. (narrow-to-region
  20236. (org-element-property :begin elem)
  20237. (org-element-property :end elem))))))
  20238. (defun org-transpose-element ()
  20239. "Transpose current and previous elements, keeping blank lines between.
  20240. Point is moved after both elements."
  20241. (interactive)
  20242. (org-skip-whitespace)
  20243. (let ((end (org-element-property :end (org-element-at-point))))
  20244. (org-drag-element-backward)
  20245. (goto-char end)))
  20246. (defun org-unindent-buffer ()
  20247. "Un-indent the visible part of the buffer.
  20248. Relative indentation (between items, inside blocks, etc.) isn't
  20249. modified."
  20250. (interactive)
  20251. (unless (eq major-mode 'org-mode)
  20252. (error "Cannot un-indent a buffer not in Org mode"))
  20253. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20254. unindent-tree ; For byte-compiler.
  20255. (unindent-tree
  20256. (function
  20257. (lambda (contents)
  20258. (mapc
  20259. (lambda (element)
  20260. (if (memq (org-element-type element) '(headline section))
  20261. (funcall unindent-tree (org-element-contents element))
  20262. (save-excursion
  20263. (save-restriction
  20264. (narrow-to-region
  20265. (org-element-property :begin element)
  20266. (org-element-property :end element))
  20267. (org-do-remove-indentation)))))
  20268. (reverse contents))))))
  20269. (funcall unindent-tree (org-element-contents parse-tree))))
  20270. (defun org-show-subtree ()
  20271. "Show everything after this heading at deeper levels."
  20272. (interactive)
  20273. (outline-flag-region
  20274. (point)
  20275. (save-excursion
  20276. (org-end-of-subtree t t))
  20277. nil))
  20278. (defun org-show-entry ()
  20279. "Show the body directly following this heading.
  20280. Show the heading too, if it is currently invisible."
  20281. (interactive)
  20282. (save-excursion
  20283. (condition-case nil
  20284. (progn
  20285. (org-back-to-heading t)
  20286. (outline-flag-region
  20287. (max (point-min) (1- (point)))
  20288. (save-excursion
  20289. (if (re-search-forward
  20290. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20291. (match-beginning 1)
  20292. (point-max)))
  20293. nil)
  20294. (org-cycle-hide-drawers 'children))
  20295. (error nil))))
  20296. (defun org-make-options-regexp (kwds &optional extra)
  20297. "Make a regular expression for keyword lines."
  20298. (concat
  20299. "^#\\+\\("
  20300. (mapconcat 'regexp-quote kwds "\\|")
  20301. (if extra (concat "\\|" extra))
  20302. "\\):[ \t]*\\(.*\\)"))
  20303. ;; Make isearch reveal the necessary context
  20304. (defun org-isearch-end ()
  20305. "Reveal context after isearch exits."
  20306. (when isearch-success ; only if search was successful
  20307. (if (featurep 'xemacs)
  20308. ;; Under XEmacs, the hook is run in the correct place,
  20309. ;; we directly show the context.
  20310. (org-show-context 'isearch)
  20311. ;; In Emacs the hook runs *before* restoring the overlays.
  20312. ;; So we have to use a one-time post-command-hook to do this.
  20313. ;; (Emacs 22 has a special variable, see function `org-mode')
  20314. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20315. isearch-mode-end-hook-quit)
  20316. ;; Only when the isearch was not quitted.
  20317. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20318. 'append 'local)))))
  20319. (defun org-isearch-post-command ()
  20320. "Remove self from hook, and show context."
  20321. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20322. (org-show-context 'isearch))
  20323. ;;;; Integration with and fixes for other packages
  20324. ;;; Imenu support
  20325. (defvar org-imenu-markers nil
  20326. "All markers currently used by Imenu.")
  20327. (make-variable-buffer-local 'org-imenu-markers)
  20328. (defun org-imenu-new-marker (&optional pos)
  20329. "Return a new marker for use by Imenu, and remember the marker."
  20330. (let ((m (make-marker)))
  20331. (move-marker m (or pos (point)))
  20332. (push m org-imenu-markers)
  20333. m))
  20334. (defun org-imenu-get-tree ()
  20335. "Produce the index for Imenu."
  20336. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20337. (setq org-imenu-markers nil)
  20338. (let* ((n org-imenu-depth)
  20339. (re (concat "^" (org-get-limited-outline-regexp)))
  20340. (subs (make-vector (1+ n) nil))
  20341. (last-level 0)
  20342. m level head)
  20343. (save-excursion
  20344. (save-restriction
  20345. (widen)
  20346. (goto-char (point-max))
  20347. (while (re-search-backward re nil t)
  20348. (setq level (org-reduced-level (funcall outline-level)))
  20349. (when (and (<= level n)
  20350. (looking-at org-complex-heading-regexp))
  20351. (setq head (org-link-display-format
  20352. (org-match-string-no-properties 4))
  20353. m (org-imenu-new-marker))
  20354. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20355. (if (>= level last-level)
  20356. (push (cons head m) (aref subs level))
  20357. (push (cons head (aref subs (1+ level))) (aref subs level))
  20358. (loop for i from (1+ level) to n do (aset subs i nil)))
  20359. (setq last-level level)))))
  20360. (aref subs 1)))
  20361. (eval-after-load "imenu"
  20362. '(progn
  20363. (add-hook 'imenu-after-jump-hook
  20364. (lambda ()
  20365. (if (derived-mode-p 'org-mode)
  20366. (org-show-context 'org-goto))))))
  20367. (defun org-link-display-format (link)
  20368. "Replace a link with either the description, or the link target
  20369. if no description is present"
  20370. (save-match-data
  20371. (if (string-match org-bracket-link-analytic-regexp link)
  20372. (replace-match (if (match-end 5)
  20373. (match-string 5 link)
  20374. (concat (match-string 1 link)
  20375. (match-string 3 link)))
  20376. nil t link)
  20377. link)))
  20378. (defun org-toggle-link-display ()
  20379. "Toggle the literal or descriptive display of links."
  20380. (interactive)
  20381. (if org-descriptive-links
  20382. (progn (org-remove-from-invisibility-spec '(org-link))
  20383. (org-restart-font-lock)
  20384. (setq org-descriptive-links nil))
  20385. (progn (add-to-invisibility-spec '(org-link))
  20386. (org-restart-font-lock)
  20387. (setq org-descriptive-links t))))
  20388. ;; Speedbar support
  20389. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20390. "Overlay marking the agenda restriction line in speedbar.")
  20391. (overlay-put org-speedbar-restriction-lock-overlay
  20392. 'face 'org-agenda-restriction-lock)
  20393. (overlay-put org-speedbar-restriction-lock-overlay
  20394. 'help-echo "Agendas are currently limited to this item.")
  20395. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20396. (defun org-speedbar-set-agenda-restriction ()
  20397. "Restrict future agenda commands to the location at point in speedbar.
  20398. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20399. (interactive)
  20400. (require 'org-agenda)
  20401. (let (p m tp np dir txt)
  20402. (cond
  20403. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20404. 'org-imenu t))
  20405. (setq m (get-text-property p 'org-imenu-marker))
  20406. (with-current-buffer (marker-buffer m)
  20407. (goto-char m)
  20408. (org-agenda-set-restriction-lock 'subtree)))
  20409. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20410. 'speedbar-function 'speedbar-find-file))
  20411. (setq tp (previous-single-property-change
  20412. (1+ p) 'speedbar-function)
  20413. np (next-single-property-change
  20414. tp 'speedbar-function)
  20415. dir (speedbar-line-directory)
  20416. txt (buffer-substring-no-properties (or tp (point-min))
  20417. (or np (point-max))))
  20418. (with-current-buffer (find-file-noselect
  20419. (let ((default-directory dir))
  20420. (expand-file-name txt)))
  20421. (unless (derived-mode-p 'org-mode)
  20422. (error "Cannot restrict to non-Org-mode file"))
  20423. (org-agenda-set-restriction-lock 'file)))
  20424. (t (error "Don't know how to restrict Org-mode's agenda")))
  20425. (move-overlay org-speedbar-restriction-lock-overlay
  20426. (point-at-bol) (point-at-eol))
  20427. (setq current-prefix-arg nil)
  20428. (org-agenda-maybe-redo)))
  20429. (eval-after-load "speedbar"
  20430. '(progn
  20431. (speedbar-add-supported-extension ".org")
  20432. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20433. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20434. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20435. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20436. (add-hook 'speedbar-visiting-tag-hook
  20437. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20438. ;;; Fixes and Hacks for problems with other packages
  20439. ;; Make flyspell not check words in links, to not mess up our keymap
  20440. (defun org-mode-flyspell-verify ()
  20441. "Don't let flyspell put overlays at active buttons, or on
  20442. {todo,all-time,additional-option-like}-keywords."
  20443. (let ((pos (max (1- (point)) (point-min)))
  20444. (word (thing-at-point 'word)))
  20445. (and (not (get-text-property pos 'keymap))
  20446. (not (get-text-property pos 'org-no-flyspell))
  20447. (not (member word org-todo-keywords-1))
  20448. (not (member word org-all-time-keywords))
  20449. (not (member word org-options-keywords))
  20450. (not (member word (mapcar 'car org-startup-options)))
  20451. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20452. (defun org-remove-flyspell-overlays-in (beg end)
  20453. "Remove flyspell overlays in region."
  20454. (and (org-bound-and-true-p flyspell-mode)
  20455. (fboundp 'flyspell-delete-region-overlays)
  20456. (flyspell-delete-region-overlays beg end))
  20457. (add-text-properties beg end '(org-no-flyspell t)))
  20458. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20459. (eval-after-load "bookmark"
  20460. '(if (boundp 'bookmark-after-jump-hook)
  20461. ;; We can use the hook
  20462. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20463. ;; Hook not available, use advice
  20464. (defadvice bookmark-jump (after org-make-visible activate)
  20465. "Make the position visible."
  20466. (org-bookmark-jump-unhide))))
  20467. ;; Make sure saveplace shows the location if it was hidden
  20468. (eval-after-load "saveplace"
  20469. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20470. "Make the position visible."
  20471. (org-bookmark-jump-unhide)))
  20472. ;; Make sure ecb shows the location if it was hidden
  20473. (eval-after-load "ecb"
  20474. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20475. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20476. (if (derived-mode-p 'org-mode)
  20477. (org-show-context))))
  20478. (defun org-bookmark-jump-unhide ()
  20479. "Unhide the current position, to show the bookmark location."
  20480. (and (derived-mode-p 'org-mode)
  20481. (or (outline-invisible-p)
  20482. (save-excursion (goto-char (max (point-min) (1- (point))))
  20483. (outline-invisible-p)))
  20484. (org-show-context 'bookmark-jump)))
  20485. ;; Make session.el ignore our circular variable
  20486. (eval-after-load "session"
  20487. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20488. ;;;; Experimental code
  20489. (defun org-closed-in-range ()
  20490. "Sparse tree of items closed in a certain time range.
  20491. Still experimental, may disappear in the future."
  20492. (interactive)
  20493. ;; Get the time interval from the user.
  20494. (let* ((time1 (org-float-time
  20495. (org-read-date nil 'to-time nil "Starting date: ")))
  20496. (time2 (org-float-time
  20497. (org-read-date nil 'to-time nil "End date:")))
  20498. ;; callback function
  20499. (callback (lambda ()
  20500. (let ((time
  20501. (org-float-time
  20502. (apply 'encode-time
  20503. (org-parse-time-string
  20504. (match-string 1))))))
  20505. ;; check if time in interval
  20506. (and (>= time time1) (<= time time2))))))
  20507. ;; make tree, check each match with the callback
  20508. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20509. ;;;; Finish up
  20510. (provide 'org)
  20511. (run-hooks 'org-load-hook)
  20512. ;;; org.el ends here