org.el 800 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255
  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. ;; Version: 7.8.06
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. (require 'format-spec)
  73. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  74. (when (fboundp 'defvaralias)
  75. (unless (boundp 'calendar-view-holidays-initially-flag)
  76. (defvaralias 'calendar-view-holidays-initially-flag
  77. 'view-calendar-holidays-initially))
  78. (unless (boundp 'calendar-view-diary-initially-flag)
  79. (defvaralias 'calendar-view-diary-initially-flag
  80. 'view-diary-entries-initially))
  81. (unless (boundp 'diary-fancy-buffer)
  82. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  83. (require 'outline) (require 'noutline)
  84. ;; Other stuff we need.
  85. (require 'time-date)
  86. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  87. (require 'easymenu)
  88. (require 'overlay)
  89. (require 'org-macs)
  90. (require 'org-entities)
  91. (require 'org-compat)
  92. (require 'org-faces)
  93. (require 'org-list)
  94. (require 'org-pcomplete)
  95. (require 'org-src)
  96. (require 'org-footnote)
  97. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  98. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  99. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  100. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  101. (declare-function org-at-clock-log-p "org-clock" ())
  102. (declare-function org-clock-timestamps-up "org-clock" ())
  103. (declare-function org-clock-timestamps-down "org-clock" ())
  104. ;; babel
  105. (require 'ob)
  106. (require 'ob-table)
  107. (require 'ob-lob)
  108. (require 'ob-ref)
  109. (require 'ob-tangle)
  110. (require 'ob-comint)
  111. (require 'ob-keys)
  112. ;; load languages based on value of `org-babel-load-languages'
  113. (defvar org-babel-load-languages)
  114. ;;;###autoload
  115. (defun org-babel-do-load-languages (sym value)
  116. "Load the languages defined in `org-babel-load-languages'."
  117. (set-default sym value)
  118. (mapc (lambda (pair)
  119. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  120. (if active
  121. (progn
  122. (require (intern (concat "ob-" lang))))
  123. (progn
  124. (funcall 'fmakunbound
  125. (intern (concat "org-babel-execute:" lang)))
  126. (funcall 'fmakunbound
  127. (intern (concat "org-babel-expand-body:" lang)))))))
  128. org-babel-load-languages))
  129. (defcustom org-babel-load-languages '((emacs-lisp . t))
  130. "Languages which can be evaluated in Org-mode buffers.
  131. This list can be used to load support for any of the languages
  132. below, note that each language will depend on a different set of
  133. system executables and/or Emacs modes. When a language is
  134. \"loaded\", then code blocks in that language can be evaluated
  135. with `org-babel-execute-src-block' bound by default to C-c
  136. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  137. be set to remove code block evaluation from the C-c C-c
  138. keybinding. By default only Emacs Lisp (which has no
  139. requirements) is loaded."
  140. :group 'org-babel
  141. :set 'org-babel-do-load-languages
  142. :type '(alist :tag "Babel Languages"
  143. :key-type
  144. (choice
  145. (const :tag "Awk" awk)
  146. (const :tag "C" C)
  147. (const :tag "R" R)
  148. (const :tag "Asymptote" asymptote)
  149. (const :tag "Calc" calc)
  150. (const :tag "Clojure" clojure)
  151. (const :tag "CSS" css)
  152. (const :tag "Ditaa" ditaa)
  153. (const :tag "Dot" dot)
  154. (const :tag "Emacs Lisp" emacs-lisp)
  155. (const :tag "Fortran" fortran)
  156. (const :tag "Gnuplot" gnuplot)
  157. (const :tag "Haskell" haskell)
  158. (const :tag "Java" java)
  159. (const :tag "Javascript" js)
  160. (const :tag "Latex" latex)
  161. (const :tag "Ledger" ledger)
  162. (const :tag "Lilypond" lilypond)
  163. (const :tag "Maxima" maxima)
  164. (const :tag "Matlab" matlab)
  165. (const :tag "Mscgen" mscgen)
  166. (const :tag "Ocaml" ocaml)
  167. (const :tag "Octave" octave)
  168. (const :tag "Org" org)
  169. (const :tag "Perl" perl)
  170. (const :tag "Pico Lisp" picolisp)
  171. (const :tag "PlantUML" plantuml)
  172. (const :tag "Python" python)
  173. (const :tag "Ruby" ruby)
  174. (const :tag "Sass" sass)
  175. (const :tag "Scheme" scheme)
  176. (const :tag "Screen" screen)
  177. (const :tag "Shell Script" sh)
  178. (const :tag "Shen" shen)
  179. (const :tag "Sql" sql)
  180. (const :tag "Sqlite" sqlite))
  181. :value-type (boolean :tag "Activate" :value t)))
  182. ;;;; Customization variables
  183. (defcustom org-clone-delete-id nil
  184. "Remove ID property of clones of a subtree.
  185. When non-nil, clones of a subtree don't inherit the ID property.
  186. Otherwise they inherit the ID property with a new unique
  187. identifier."
  188. :type 'boolean
  189. :group 'org-id)
  190. ;;; Version
  191. (defconst org-version "7.8.06"
  192. "The version number of the file org.el.")
  193. ;;;###autoload
  194. (defun org-version (&optional here)
  195. "Show the org-mode version in the echo area.
  196. With prefix arg HERE, insert it at point."
  197. (interactive "P")
  198. (let* ((origin default-directory)
  199. (version org-version)
  200. (git-version)
  201. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  202. (when (and (file-exists-p (expand-file-name ".git" dir))
  203. (executable-find "git"))
  204. (unwind-protect
  205. (progn
  206. (cd dir)
  207. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  208. (with-current-buffer "*Shell Command Output*"
  209. (goto-char (point-min))
  210. (setq git-version (buffer-substring (point) (point-at-eol))))
  211. (subst-char-in-string ?- ?. git-version t)
  212. (when (string-match "\\S-"
  213. (shell-command-to-string
  214. "git diff-index --name-only HEAD --"))
  215. (setq git-version (concat git-version ".dirty")))
  216. (setq version (concat version " (" git-version ")"))))
  217. (cd origin)))
  218. (setq version (format "Org-mode version %s" version))
  219. (if here (insert version))
  220. (message version)))
  221. ;;; Compatibility constants
  222. ;;; The custom variables
  223. (defgroup org nil
  224. "Outline-based notes management and organizer."
  225. :tag "Org"
  226. :group 'outlines
  227. :group 'calendar)
  228. (defcustom org-mode-hook nil
  229. "Mode hook for Org-mode, run after the mode was turned on."
  230. :group 'org
  231. :type 'hook)
  232. (defcustom org-load-hook nil
  233. "Hook that is run after org.el has been loaded."
  234. :group 'org
  235. :type 'hook)
  236. (defcustom org-log-buffer-setup-hook nil
  237. "Hook that is run after an Org log buffer is created."
  238. :group 'org
  239. :type 'hook)
  240. (defvar org-modules) ; defined below
  241. (defvar org-modules-loaded nil
  242. "Have the modules been loaded already?")
  243. (defun org-load-modules-maybe (&optional force)
  244. "Load all extensions listed in `org-modules'."
  245. (when (or force (not org-modules-loaded))
  246. (mapc (lambda (ext)
  247. (condition-case nil (require ext)
  248. (error (message "Problems while trying to load feature `%s'" ext))))
  249. org-modules)
  250. (setq org-modules-loaded t)))
  251. (defun org-set-modules (var value)
  252. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  253. (set var value)
  254. (when (featurep 'org)
  255. (org-load-modules-maybe 'force)))
  256. (when (org-bound-and-true-p org-modules)
  257. (let ((a (member 'org-infojs org-modules)))
  258. (and a (setcar a 'org-jsinfo))))
  259. (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)
  260. "Modules that should always be loaded together with org.el.
  261. If a description starts with <C>, the file is not part of Emacs
  262. and loading it will require that you have downloaded and properly installed
  263. the org-mode distribution.
  264. You can also use this system to load external packages (i.e. neither Org
  265. core modules, nor modules from the CONTRIB directory). Just add symbols
  266. to the end of the list. If the package is called org-xyz.el, then you need
  267. to add the symbol `xyz', and the package must have a call to
  268. (provide 'org-xyz)"
  269. :group 'org
  270. :set 'org-set-modules
  271. :type
  272. '(set :greedy t
  273. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  274. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  275. (const :tag " crypt: Encryption of subtrees" org-crypt)
  276. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  277. (const :tag " docview: Links to doc-view buffers" org-docview)
  278. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  279. (const :tag " id: Global IDs for identifying entries" org-id)
  280. (const :tag " info: Links to Info nodes" org-info)
  281. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  282. (const :tag " habit: Track your consistency with habits" org-habit)
  283. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  284. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  285. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  286. (const :tag " mew Links to Mew folders/messages" org-mew)
  287. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  288. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  289. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  290. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  291. (const :tag " vm: Links to VM folders/messages" org-vm)
  292. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  293. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  294. (const :tag " mouse: Additional mouse support" org-mouse)
  295. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  296. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  297. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  298. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  299. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  300. (const :tag "C collector: Collect properties into tables" org-collector)
  301. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  302. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  303. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  304. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  305. (const :tag "C eval: Include command output as text" org-eval)
  306. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  307. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  308. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  309. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  310. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  311. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  312. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  313. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  314. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  315. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  316. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  317. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  318. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  319. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  320. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  321. (const :tag "C registry: A registry for Org-mode links" org-registry)
  322. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  323. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  324. (const :tag "C secretary: Team management with org-mode" org-secretary)
  325. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  326. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  327. (const :tag "C track: Keep up with Org-mode development" org-track)
  328. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  329. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  330. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  331. (defcustom org-support-shift-select nil
  332. "Non-nil means make shift-cursor commands select text when possible.
  333. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  334. start selecting a region, or enlarge regions started in this way.
  335. In Org-mode, in special contexts, these same keys are used for
  336. other purposes, important enough to compete with shift selection.
  337. Org tries to balance these needs by supporting `shift-select-mode'
  338. outside these special contexts, under control of this variable.
  339. The default of this variable is nil, to avoid confusing behavior. Shifted
  340. cursor keys will then execute Org commands in the following contexts:
  341. - on a headline, changing TODO state (left/right) and priority (up/down)
  342. - on a time stamp, changing the time
  343. - in a plain list item, changing the bullet type
  344. - in a property definition line, switching between allowed values
  345. - in the BEGIN line of a clock table (changing the time block).
  346. Outside these contexts, the commands will throw an error.
  347. When this variable is t and the cursor is not in a special
  348. context, Org-mode will support shift-selection for making and
  349. enlarging regions. To make this more effective, the bullet
  350. cycling will no longer happen anywhere in an item line, but only
  351. if the cursor is exactly on the bullet.
  352. If you set this variable to the symbol `always', then the keys
  353. will not be special in headlines, property lines, and item lines,
  354. to make shift selection work there as well. If this is what you
  355. want, you can use the following alternative commands: `C-c C-t'
  356. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  357. can be used to switch TODO sets, `C-c -' to cycle item bullet
  358. types, and properties can be edited by hand or in column view.
  359. However, when the cursor is on a timestamp, shift-cursor commands
  360. will still edit the time stamp - this is just too good to give up.
  361. XEmacs user should have this variable set to nil, because
  362. `shift-select-mode' is in Emacs 23 or later only."
  363. :group 'org
  364. :type '(choice
  365. (const :tag "Never" nil)
  366. (const :tag "When outside special context" t)
  367. (const :tag "Everywhere except timestamps" always)))
  368. (defcustom org-loop-over-headlines-in-active-region nil
  369. "Shall some commands act upon headlines in the active region?
  370. When set to `t', some commands will be performed in all headlines
  371. within the active region.
  372. When set to `start-level', some commands will be performed in all
  373. headlines within the active region, provided that these headlines
  374. are of the same level than the first one.
  375. When set to a string, those commands will be performed on the
  376. matching headlines within the active region. Such string must be
  377. a tags/property/todo match as it is used in the agenda tags view.
  378. The list of commands is: `org-schedule', `org-deadline',
  379. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  380. `org-archive-to-archive-sibling'. The archiving commands skip
  381. already archived entries."
  382. :type '(choice (const :tag "Don't loop" nil)
  383. (const :tag "All headlines in active region" t)
  384. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  385. (string :tag "Tags/Property/Todo matcher"))
  386. :group 'org-todo
  387. :group 'org-archive)
  388. (defgroup org-startup nil
  389. "Options concerning startup of Org-mode."
  390. :tag "Org Startup"
  391. :group 'org)
  392. (defcustom org-startup-folded t
  393. "Non-nil means entering Org-mode will switch to OVERVIEW.
  394. This can also be configured on a per-file basis by adding one of
  395. the following lines anywhere in the buffer:
  396. #+STARTUP: fold (or `overview', this is equivalent)
  397. #+STARTUP: nofold (or `showall', this is equivalent)
  398. #+STARTUP: content
  399. #+STARTUP: showeverything"
  400. :group 'org-startup
  401. :type '(choice
  402. (const :tag "nofold: show all" nil)
  403. (const :tag "fold: overview" t)
  404. (const :tag "content: all headlines" content)
  405. (const :tag "show everything, even drawers" showeverything)))
  406. (defcustom org-startup-truncated t
  407. "Non-nil means entering Org-mode will set `truncate-lines'.
  408. This is useful since some lines containing links can be very long and
  409. uninteresting. Also tables look terrible when wrapped."
  410. :group 'org-startup
  411. :type 'boolean)
  412. (defcustom org-startup-indented nil
  413. "Non-nil means turn on `org-indent-mode' on startup.
  414. This can also be configured on a per-file basis by adding one of
  415. the following lines anywhere in the buffer:
  416. #+STARTUP: indent
  417. #+STARTUP: noindent"
  418. :group 'org-structure
  419. :type '(choice
  420. (const :tag "Not" nil)
  421. (const :tag "Globally (slow on startup in large files)" t)))
  422. (defcustom org-use-sub-superscripts t
  423. "Non-nil means interpret \"_\" and \"^\" for export.
  424. When this option is turned on, you can use TeX-like syntax for sub- and
  425. superscripts. Several characters after \"_\" or \"^\" will be
  426. considered as a single item - so grouping with {} is normally not
  427. needed. For example, the following things will be parsed as single
  428. sub- or superscripts.
  429. 10^24 or 10^tau several digits will be considered 1 item.
  430. 10^-12 or 10^-tau a leading sign with digits or a word
  431. x^2-y^3 will be read as x^2 - y^3, because items are
  432. terminated by almost any nonword/nondigit char.
  433. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  434. Still, ambiguity is possible - so when in doubt use {} to enclose the
  435. sub/superscript. If you set this variable to the symbol `{}',
  436. the braces are *required* in order to trigger interpretations as
  437. sub/superscript. This can be helpful in documents that need \"_\"
  438. frequently in plain text.
  439. Not all export backends support this, but HTML does.
  440. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  441. :group 'org-startup
  442. :group 'org-export-translation
  443. :type '(choice
  444. (const :tag "Always interpret" t)
  445. (const :tag "Only with braces" {})
  446. (const :tag "Never interpret" nil)))
  447. (if (fboundp 'defvaralias)
  448. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  449. (defcustom org-startup-with-beamer-mode nil
  450. "Non-nil means turn on `org-beamer-mode' on startup.
  451. This can also be configured on a per-file basis by adding one of
  452. the following lines anywhere in the buffer:
  453. #+STARTUP: beamer"
  454. :group 'org-startup
  455. :type 'boolean)
  456. (defcustom org-startup-align-all-tables nil
  457. "Non-nil means align all tables when visiting a file.
  458. This is useful when the column width in tables is forced with <N> cookies
  459. in table fields. Such tables will look correct only after the first re-align.
  460. This can also be configured on a per-file basis by adding one of
  461. the following lines anywhere in the buffer:
  462. #+STARTUP: align
  463. #+STARTUP: noalign"
  464. :group 'org-startup
  465. :type 'boolean)
  466. (defcustom org-startup-with-inline-images nil
  467. "Non-nil means show inline images when loading a new Org file.
  468. This can also be configured on a per-file basis by adding one of
  469. the following lines anywhere in the buffer:
  470. #+STARTUP: inlineimages
  471. #+STARTUP: noinlineimages"
  472. :group 'org-startup
  473. :type 'boolean)
  474. (defcustom org-insert-mode-line-in-empty-file nil
  475. "Non-nil means insert the first line setting Org-mode in empty files.
  476. When the function `org-mode' is called interactively in an empty file, this
  477. normally means that the file name does not automatically trigger Org-mode.
  478. To ensure that the file will always be in Org-mode in the future, a
  479. line enforcing Org-mode will be inserted into the buffer, if this option
  480. has been set."
  481. :group 'org-startup
  482. :type 'boolean)
  483. (defcustom org-replace-disputed-keys nil
  484. "Non-nil means use alternative key bindings for some keys.
  485. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  486. These keys are also used by other packages like shift-selection-mode'
  487. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  488. If you want to use Org-mode together with one of these other modes,
  489. or more generally if you would like to move some Org-mode commands to
  490. other keys, set this variable and configure the keys with the variable
  491. `org-disputed-keys'.
  492. This option is only relevant at load-time of Org-mode, and must be set
  493. *before* org.el is loaded. Changing it requires a restart of Emacs to
  494. become effective."
  495. :group 'org-startup
  496. :type 'boolean)
  497. (defcustom org-use-extra-keys nil
  498. "Non-nil means use extra key sequence definitions for certain commands.
  499. This happens automatically if you run XEmacs or if `window-system'
  500. is nil. This variable lets you do the same manually. You must
  501. set it before loading org.
  502. Example: on Carbon Emacs 22 running graphically, with an external
  503. keyboard on a Powerbook, the default way of setting M-left might
  504. not work for either Alt or ESC. Setting this variable will make
  505. it work for ESC."
  506. :group 'org-startup
  507. :type 'boolean)
  508. (if (fboundp 'defvaralias)
  509. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  510. (defcustom org-disputed-keys
  511. '(([(shift up)] . [(meta p)])
  512. ([(shift down)] . [(meta n)])
  513. ([(shift left)] . [(meta -)])
  514. ([(shift right)] . [(meta +)])
  515. ([(control shift right)] . [(meta shift +)])
  516. ([(control shift left)] . [(meta shift -)]))
  517. "Keys for which Org-mode and other modes compete.
  518. This is an alist, cars are the default keys, second element specifies
  519. the alternative to use when `org-replace-disputed-keys' is t.
  520. Keys can be specified in any syntax supported by `define-key'.
  521. The value of this option takes effect only at Org-mode's startup,
  522. therefore you'll have to restart Emacs to apply it after changing."
  523. :group 'org-startup
  524. :type 'alist)
  525. (defun org-key (key)
  526. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  527. Or return the original if not disputed.
  528. Also apply the translations defined in `org-xemacs-key-equivalents'."
  529. (when org-replace-disputed-keys
  530. (let* ((nkey (key-description key))
  531. (x (org-find-if (lambda (x)
  532. (equal (key-description (car x)) nkey))
  533. org-disputed-keys)))
  534. (setq key (if x (cdr x) key))))
  535. (when (featurep 'xemacs)
  536. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  537. key)
  538. (defun org-find-if (predicate seq)
  539. (catch 'exit
  540. (while seq
  541. (if (funcall predicate (car seq))
  542. (throw 'exit (car seq))
  543. (pop seq)))))
  544. (defun org-defkey (keymap key def)
  545. "Define a key, possibly translated, as returned by `org-key'."
  546. (define-key keymap (org-key key) def))
  547. (defcustom org-ellipsis nil
  548. "The ellipsis to use in the Org-mode outline.
  549. When nil, just use the standard three dots. When a string, use that instead,
  550. When a face, use the standard 3 dots, but with the specified face.
  551. The change affects only Org-mode (which will then use its own display table).
  552. Changing this requires executing `M-x org-mode' in a buffer to become
  553. effective."
  554. :group 'org-startup
  555. :type '(choice (const :tag "Default" nil)
  556. (face :tag "Face" :value org-warning)
  557. (string :tag "String" :value "...#")))
  558. (defvar org-display-table nil
  559. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  560. (defgroup org-keywords nil
  561. "Keywords in Org-mode."
  562. :tag "Org Keywords"
  563. :group 'org)
  564. (defcustom org-deadline-string "DEADLINE:"
  565. "String to mark deadline entries.
  566. A deadline is this string, followed by a time stamp. Should be a word,
  567. terminated by a colon. You can insert a schedule keyword and
  568. a timestamp with \\[org-deadline].
  569. Changes become only effective after restarting Emacs."
  570. :group 'org-keywords
  571. :type 'string)
  572. (defcustom org-scheduled-string "SCHEDULED:"
  573. "String to mark scheduled TODO entries.
  574. A schedule is this string, followed by a time stamp. Should be a word,
  575. terminated by a colon. You can insert a schedule keyword and
  576. a timestamp with \\[org-schedule].
  577. Changes become only effective after restarting Emacs."
  578. :group 'org-keywords
  579. :type 'string)
  580. (defcustom org-closed-string "CLOSED:"
  581. "String used as the prefix for timestamps logging closing a TODO entry."
  582. :group 'org-keywords
  583. :type 'string)
  584. (defcustom org-clock-string "CLOCK:"
  585. "String used as prefix for timestamps clocking work hours on an item."
  586. :group 'org-keywords
  587. :type 'string)
  588. (defcustom org-comment-string "COMMENT"
  589. "Entries starting with this keyword will never be exported.
  590. An entry can be toggled between COMMENT and normal with
  591. \\[org-toggle-comment].
  592. Changes become only effective after restarting Emacs."
  593. :group 'org-keywords
  594. :type 'string)
  595. (defcustom org-quote-string "QUOTE"
  596. "Entries starting with this keyword will be exported in fixed-width font.
  597. Quoting applies only to the text in the entry following the headline, and does
  598. not extend beyond the next headline, even if that is lower level.
  599. An entry can be toggled between QUOTE and normal with
  600. \\[org-toggle-fixed-width-section]."
  601. :group 'org-keywords
  602. :type 'string)
  603. (defconst org-repeat-re
  604. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  605. "Regular expression for specifying repeated events.
  606. After a match, group 1 contains the repeat expression.")
  607. (defgroup org-structure nil
  608. "Options concerning the general structure of Org-mode files."
  609. :tag "Org Structure"
  610. :group 'org)
  611. (defgroup org-reveal-location nil
  612. "Options about how to make context of a location visible."
  613. :tag "Org Reveal Location"
  614. :group 'org-structure)
  615. (defconst org-context-choice
  616. '(choice
  617. (const :tag "Always" t)
  618. (const :tag "Never" nil)
  619. (repeat :greedy t :tag "Individual contexts"
  620. (cons
  621. (choice :tag "Context"
  622. (const agenda)
  623. (const org-goto)
  624. (const occur-tree)
  625. (const tags-tree)
  626. (const link-search)
  627. (const mark-goto)
  628. (const bookmark-jump)
  629. (const isearch)
  630. (const default))
  631. (boolean))))
  632. "Contexts for the reveal options.")
  633. (defcustom org-show-hierarchy-above '((default . t))
  634. "Non-nil means show full hierarchy when revealing a location.
  635. Org-mode often shows locations in an org-mode file which might have
  636. been invisible before. When this is set, the hierarchy of headings
  637. above the exposed location is shown.
  638. Turning this off for example for sparse trees makes them very compact.
  639. Instead of t, this can also be an alist specifying this option for different
  640. contexts. Valid contexts are
  641. agenda when exposing an entry from the agenda
  642. org-goto when using the command `org-goto' on key C-c C-j
  643. occur-tree when using the command `org-occur' on key C-c /
  644. tags-tree when constructing a sparse tree based on tags matches
  645. link-search when exposing search matches associated with a link
  646. mark-goto when exposing the jump goal of a mark
  647. bookmark-jump when exposing a bookmark location
  648. isearch when exiting from an incremental search
  649. default default for all contexts not set explicitly"
  650. :group 'org-reveal-location
  651. :type org-context-choice)
  652. (defcustom org-show-following-heading '((default . nil))
  653. "Non-nil means show following heading when revealing a location.
  654. Org-mode often shows locations in an org-mode file which might have
  655. been invisible before. When this is set, the heading following the
  656. match is shown.
  657. Turning this off for example for sparse trees makes them very compact,
  658. but makes it harder to edit the location of the match. In such a case,
  659. use the command \\[org-reveal] to show more context.
  660. Instead of t, this can also be an alist specifying this option for different
  661. contexts. See `org-show-hierarchy-above' for valid contexts."
  662. :group 'org-reveal-location
  663. :type org-context-choice)
  664. (defcustom org-show-siblings '((default . nil) (isearch t))
  665. "Non-nil means show all sibling heading when revealing a location.
  666. Org-mode often shows locations in an org-mode file which might have
  667. been invisible before. When this is set, the sibling of the current entry
  668. heading are all made visible. If `org-show-hierarchy-above' is t,
  669. the same happens on each level of the hierarchy above the current entry.
  670. By default this is on for the isearch context, off for all other contexts.
  671. Turning this off for example for sparse trees makes them very compact,
  672. but makes it harder to edit the location of the match. In such a case,
  673. use the command \\[org-reveal] to show more context.
  674. Instead of t, this can also be an alist specifying this option for different
  675. contexts. See `org-show-hierarchy-above' for valid contexts."
  676. :group 'org-reveal-location
  677. :type org-context-choice)
  678. (defcustom org-show-entry-below '((default . nil))
  679. "Non-nil means show the entry below a headline when revealing a location.
  680. Org-mode often shows locations in an org-mode file which might have
  681. been invisible before. When this is set, the text below the headline that is
  682. exposed is also shown.
  683. By default this is off for all contexts.
  684. Instead of t, this can also be an alist specifying this option for different
  685. contexts. See `org-show-hierarchy-above' for valid contexts."
  686. :group 'org-reveal-location
  687. :type org-context-choice)
  688. (defcustom org-indirect-buffer-display 'other-window
  689. "How should indirect tree buffers be displayed?
  690. This applies to indirect buffers created with the commands
  691. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  692. Valid values are:
  693. current-window Display in the current window
  694. other-window Just display in another window.
  695. dedicated-frame Create one new frame, and re-use it each time.
  696. new-frame Make a new frame each time. Note that in this case
  697. previously-made indirect buffers are kept, and you need to
  698. kill these buffers yourself."
  699. :group 'org-structure
  700. :group 'org-agenda-windows
  701. :type '(choice
  702. (const :tag "In current window" current-window)
  703. (const :tag "In current frame, other window" other-window)
  704. (const :tag "Each time a new frame" new-frame)
  705. (const :tag "One dedicated frame" dedicated-frame)))
  706. (defcustom org-use-speed-commands nil
  707. "Non-nil means activate single letter commands at beginning of a headline.
  708. This may also be a function to test for appropriate locations where speed
  709. commands should be active."
  710. :group 'org-structure
  711. :type '(choice
  712. (const :tag "Never" nil)
  713. (const :tag "At beginning of headline stars" t)
  714. (function)))
  715. (defcustom org-speed-commands-user nil
  716. "Alist of additional speed commands.
  717. This list will be checked before `org-speed-commands-default'
  718. when the variable `org-use-speed-commands' is non-nil
  719. and when the cursor is at the beginning of a headline.
  720. The car if each entry is a string with a single letter, which must
  721. be assigned to `self-insert-command' in the global map.
  722. The cdr is either a command to be called interactively, a function
  723. to be called, or a form to be evaluated.
  724. An entry that is just a list with a single string will be interpreted
  725. as a descriptive headline that will be added when listing the speed
  726. commands in the Help buffer using the `?' speed command."
  727. :group 'org-structure
  728. :type '(repeat :value ("k" . ignore)
  729. (choice :value ("k" . ignore)
  730. (list :tag "Descriptive Headline" (string :tag "Headline"))
  731. (cons :tag "Letter and Command"
  732. (string :tag "Command letter")
  733. (choice
  734. (function)
  735. (sexp))))))
  736. (defgroup org-cycle nil
  737. "Options concerning visibility cycling in Org-mode."
  738. :tag "Org Cycle"
  739. :group 'org-structure)
  740. (defcustom org-cycle-skip-children-state-if-no-children t
  741. "Non-nil means skip CHILDREN state in entries that don't have any."
  742. :group 'org-cycle
  743. :type 'boolean)
  744. (defcustom org-cycle-max-level nil
  745. "Maximum level which should still be subject to visibility cycling.
  746. Levels higher than this will, for cycling, be treated as text, not a headline.
  747. When `org-odd-levels-only' is set, a value of N in this variable actually
  748. means 2N-1 stars as the limiting headline.
  749. When nil, cycle all levels.
  750. Note that the limiting level of cycling is also influenced by
  751. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  752. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  753. than its value."
  754. :group 'org-cycle
  755. :type '(choice
  756. (const :tag "No limit" nil)
  757. (integer :tag "Maximum level")))
  758. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  759. "Names of drawers. Drawers are not opened by cycling on the headline above.
  760. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  761. this:
  762. :DRAWERNAME:
  763. .....
  764. :END:
  765. The drawer \"PROPERTIES\" is special for capturing properties through
  766. the property API.
  767. Drawers can be defined on the per-file basis with a line like:
  768. #+DRAWERS: HIDDEN STATE PROPERTIES"
  769. :group 'org-structure
  770. :group 'org-cycle
  771. :type '(repeat (string :tag "Drawer Name")))
  772. (defcustom org-hide-block-startup nil
  773. "Non-nil means entering Org-mode will fold all blocks.
  774. This can also be set in on a per-file basis with
  775. #+STARTUP: hideblocks
  776. #+STARTUP: showblocks"
  777. :group 'org-startup
  778. :group 'org-cycle
  779. :type 'boolean)
  780. (defcustom org-cycle-global-at-bob nil
  781. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  782. This makes it possible to do global cycling without having to use S-TAB or
  783. \\[universal-argument] TAB. For this special case to work, the first line \
  784. of the buffer
  785. must not be a headline - it may be empty or some other text. When used in
  786. this way, `org-cycle-hook' is disables temporarily, to make sure the
  787. cursor stays at the beginning of the buffer.
  788. When this option is nil, don't do anything special at the beginning
  789. of the buffer."
  790. :group 'org-cycle
  791. :type 'boolean)
  792. (defcustom org-cycle-level-after-item/entry-creation t
  793. "Non-nil means cycle entry level or item indentation in new empty entries.
  794. When the cursor is at the end of an empty headline, i.e with only stars
  795. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  796. and then all possible ancestor states, before returning to the original state.
  797. This makes data entry extremely fast: M-RET to create a new headline,
  798. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  799. When the cursor is at the end of an empty plain list item, one TAB will
  800. make it a subitem, two or more tabs will back up to make this an item
  801. higher up in the item hierarchy."
  802. :group 'org-cycle
  803. :type 'boolean)
  804. (defcustom org-cycle-emulate-tab t
  805. "Where should `org-cycle' emulate TAB.
  806. nil Never
  807. white Only in completely white lines
  808. whitestart Only at the beginning of lines, before the first non-white char
  809. t Everywhere except in headlines
  810. exc-hl-bol Everywhere except at the start of a headline
  811. If TAB is used in a place where it does not emulate TAB, the current subtree
  812. visibility is cycled."
  813. :group 'org-cycle
  814. :type '(choice (const :tag "Never" nil)
  815. (const :tag "Only in completely white lines" white)
  816. (const :tag "Before first char in a line" whitestart)
  817. (const :tag "Everywhere except in headlines" t)
  818. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  819. ))
  820. (defcustom org-cycle-separator-lines 2
  821. "Number of empty lines needed to keep an empty line between collapsed trees.
  822. If you leave an empty line between the end of a subtree and the following
  823. headline, this empty line is hidden when the subtree is folded.
  824. Org-mode will leave (exactly) one empty line visible if the number of
  825. empty lines is equal or larger to the number given in this variable.
  826. So the default 2 means at least 2 empty lines after the end of a subtree
  827. are needed to produce free space between a collapsed subtree and the
  828. following headline.
  829. If the number is negative, and the number of empty lines is at least -N,
  830. all empty lines are shown.
  831. Special case: when 0, never leave empty lines in collapsed view."
  832. :group 'org-cycle
  833. :type 'integer)
  834. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  835. (defcustom org-pre-cycle-hook nil
  836. "Hook that is run before visibility cycling is happening.
  837. The function(s) in this hook must accept a single argument which indicates
  838. the new state that will be set right after running this hook. The
  839. argument is a symbol. Before a global state change, it can have the values
  840. `overview', `content', or `all'. Before a local state change, it can have
  841. the values `folded', `children', or `subtree'."
  842. :group 'org-cycle
  843. :type 'hook)
  844. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  845. org-cycle-hide-drawers
  846. org-cycle-show-empty-lines
  847. org-optimize-window-after-visibility-change)
  848. "Hook that is run after `org-cycle' has changed the buffer visibility.
  849. The function(s) in this hook must accept a single argument which indicates
  850. the new state that was set by the most recent `org-cycle' command. The
  851. argument is a symbol. After a global state change, it can have the values
  852. `overview', `content', or `all'. After a local state change, it can have
  853. the values `folded', `children', or `subtree'."
  854. :group 'org-cycle
  855. :type 'hook)
  856. (defgroup org-edit-structure nil
  857. "Options concerning structure editing in Org-mode."
  858. :tag "Org Edit Structure"
  859. :group 'org-structure)
  860. (defcustom org-odd-levels-only nil
  861. "Non-nil means skip even levels and only use odd levels for the outline.
  862. This has the effect that two stars are being added/taken away in
  863. promotion/demotion commands. It also influences how levels are
  864. handled by the exporters.
  865. Changing it requires restart of `font-lock-mode' to become effective
  866. for fontification also in regions already fontified.
  867. You may also set this on a per-file basis by adding one of the following
  868. lines to the buffer:
  869. #+STARTUP: odd
  870. #+STARTUP: oddeven"
  871. :group 'org-edit-structure
  872. :group 'org-appearance
  873. :type 'boolean)
  874. (defcustom org-adapt-indentation t
  875. "Non-nil means adapt indentation to outline node level.
  876. When this variable is set, Org assumes that you write outlines by
  877. indenting text in each node to align with the headline (after the stars).
  878. The following issues are influenced by this variable:
  879. - When this is set and the *entire* text in an entry is indented, the
  880. indentation is increased by one space in a demotion command, and
  881. decreased by one in a promotion command. If any line in the entry
  882. body starts with text at column 0, indentation is not changed at all.
  883. - Property drawers and planning information is inserted indented when
  884. this variable s set. When nil, they will not be indented.
  885. - TAB indents a line relative to context. The lines below a headline
  886. will be indented when this variable is set.
  887. Note that this is all about true indentation, by adding and removing
  888. space characters. See also `org-indent.el' which does level-dependent
  889. indentation in a virtual way, i.e. at display time in Emacs."
  890. :group 'org-edit-structure
  891. :type 'boolean)
  892. (defcustom org-special-ctrl-a/e nil
  893. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  894. When t, `C-a' will bring back the cursor to the beginning of the
  895. headline text, i.e. after the stars and after a possible TODO keyword.
  896. In an item, this will be the position after the bullet.
  897. When the cursor is already at that position, another `C-a' will bring
  898. it to the beginning of the line.
  899. `C-e' will jump to the end of the headline, ignoring the presence of tags
  900. in the headline. A second `C-e' will then jump to the true end of the
  901. line, after any tags. This also means that, when this variable is
  902. non-nil, `C-e' also will never jump beyond the end of the heading of a
  903. folded section, i.e. not after the ellipses.
  904. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  905. going to the true line boundary first. Only a directly following, identical
  906. keypress will bring the cursor to the special positions.
  907. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  908. set separately."
  909. :group 'org-edit-structure
  910. :type '(choice
  911. (const :tag "off" nil)
  912. (const :tag "on: after stars/bullet and before tags first" t)
  913. (const :tag "reversed: true line boundary first" reversed)
  914. (cons :tag "Set C-a and C-e separately"
  915. (choice :tag "Special C-a"
  916. (const :tag "off" nil)
  917. (const :tag "on: after stars/bullet first" t)
  918. (const :tag "reversed: before stars/bullet first" reversed))
  919. (choice :tag "Special C-e"
  920. (const :tag "off" nil)
  921. (const :tag "on: before tags first" t)
  922. (const :tag "reversed: after tags first" reversed)))))
  923. (if (fboundp 'defvaralias)
  924. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  925. (defcustom org-special-ctrl-k nil
  926. "Non-nil means `C-k' will behave specially in headlines.
  927. When nil, `C-k' will call the default `kill-line' command.
  928. When t, the following will happen while the cursor is in the headline:
  929. - When the cursor is at the beginning of a headline, kill the entire
  930. line and possible the folded subtree below the line.
  931. - When in the middle of the headline text, kill the headline up to the tags.
  932. - When after the headline text, kill the tags."
  933. :group 'org-edit-structure
  934. :type 'boolean)
  935. (defcustom org-ctrl-k-protect-subtree nil
  936. "Non-nil means, do not delete a hidden subtree with C-k.
  937. When set to the symbol `error', simply throw an error when C-k is
  938. used to kill (part-of) a headline that has hidden text behind it.
  939. Any other non-nil value will result in a query to the user, if it is
  940. OK to kill that hidden subtree. When nil, kill without remorse."
  941. :group 'org-edit-structure
  942. :type '(choice
  943. (const :tag "Do not protect hidden subtrees" nil)
  944. (const :tag "Protect hidden subtrees with a security query" t)
  945. (const :tag "Never kill a hidden subtree with C-k" error)))
  946. (defcustom org-catch-invisible-edits nil
  947. "Check if in invisible region before inserting or deleting a character.
  948. Valid values are:
  949. nil Do not check, so just do invisible edits.
  950. error Throw an error and do nothing.
  951. show Make point visible, and do the requested edit.
  952. show-and-error Make point visible, then throw an error and abort the edit.
  953. smart Make point visible, and do insertion/deletion if it is
  954. adjacent to visible text and the change feels predictable.
  955. Never delete a previously invisible character or add in the
  956. middle or right after an invisible region. Basically, this
  957. allows insertion and backward-delete right before ellipses.
  958. FIXME: maybe in this case we should not even show?"
  959. :group 'org-edit-structure
  960. :type '(choice
  961. (const :tag "Do not check" nil)
  962. (const :tag "Throw error when trying to edit" error)
  963. (const :tag "Unhide, but do not do the edit" show-and-error)
  964. (const :tag "Show invisible part and do the edit" show)
  965. (const :tag "Be smart and do the right thing" smart)))
  966. (defcustom org-yank-folded-subtrees t
  967. "Non-nil means when yanking subtrees, fold them.
  968. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  969. it starts with a heading and all other headings in it are either children
  970. or siblings, then fold all the subtrees. However, do this only if no
  971. text after the yank would be swallowed into a folded tree by this action."
  972. :group 'org-edit-structure
  973. :type 'boolean)
  974. (defcustom org-yank-adjusted-subtrees nil
  975. "Non-nil means when yanking subtrees, adjust the level.
  976. With this setting, `org-paste-subtree' is used to insert the subtree, see
  977. this function for details."
  978. :group 'org-edit-structure
  979. :type 'boolean)
  980. (defcustom org-M-RET-may-split-line '((default . t))
  981. "Non-nil means M-RET will split the line at the cursor position.
  982. When nil, it will go to the end of the line before making a
  983. new line.
  984. You may also set this option in a different way for different
  985. contexts. Valid contexts are:
  986. headline when creating a new headline
  987. item when creating a new item
  988. table in a table field
  989. default the value to be used for all contexts not explicitly
  990. customized"
  991. :group 'org-structure
  992. :group 'org-table
  993. :type '(choice
  994. (const :tag "Always" t)
  995. (const :tag "Never" nil)
  996. (repeat :greedy t :tag "Individual contexts"
  997. (cons
  998. (choice :tag "Context"
  999. (const headline)
  1000. (const item)
  1001. (const table)
  1002. (const default))
  1003. (boolean)))))
  1004. (defcustom org-insert-heading-respect-content nil
  1005. "Non-nil means insert new headings after the current subtree.
  1006. When nil, the new heading is created directly after the current line.
  1007. The commands \\[org-insert-heading-respect-content] and
  1008. \\[org-insert-todo-heading-respect-content] turn this variable on
  1009. for the duration of the command."
  1010. :group 'org-structure
  1011. :type 'boolean)
  1012. (defcustom org-blank-before-new-entry '((heading . auto)
  1013. (plain-list-item . auto))
  1014. "Should `org-insert-heading' leave a blank line before new heading/item?
  1015. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1016. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1017. which case Org will look at the surrounding headings/items and try to
  1018. make an intelligent decision whether to insert a blank line or not.
  1019. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1020. set, the setting here is ignored and no empty line is inserted, to avoid
  1021. breaking the list structure."
  1022. :group 'org-edit-structure
  1023. :type '(list
  1024. (cons (const heading)
  1025. (choice (const :tag "Never" nil)
  1026. (const :tag "Always" t)
  1027. (const :tag "Auto" auto)))
  1028. (cons (const plain-list-item)
  1029. (choice (const :tag "Never" nil)
  1030. (const :tag "Always" t)
  1031. (const :tag "Auto" auto)))))
  1032. (defcustom org-insert-heading-hook nil
  1033. "Hook being run after inserting a new heading."
  1034. :group 'org-edit-structure
  1035. :type 'hook)
  1036. (defcustom org-enable-fixed-width-editor t
  1037. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1038. This currently only means they are never auto-wrapped.
  1039. When nil, such lines will be treated like ordinary lines.
  1040. See also the QUOTE keyword."
  1041. :group 'org-edit-structure
  1042. :type 'boolean)
  1043. (defcustom org-goto-auto-isearch t
  1044. "Non-nil means typing characters in `org-goto' starts incremental search."
  1045. :group 'org-edit-structure
  1046. :type 'boolean)
  1047. (defgroup org-sparse-trees nil
  1048. "Options concerning sparse trees in Org-mode."
  1049. :tag "Org Sparse Trees"
  1050. :group 'org-structure)
  1051. (defcustom org-highlight-sparse-tree-matches t
  1052. "Non-nil means highlight all matches that define a sparse tree.
  1053. The highlights will automatically disappear the next time the buffer is
  1054. changed by an edit command."
  1055. :group 'org-sparse-trees
  1056. :type 'boolean)
  1057. (defcustom org-remove-highlights-with-change t
  1058. "Non-nil means any change to the buffer will remove temporary highlights.
  1059. Such highlights are created by `org-occur' and `org-clock-display'.
  1060. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1061. The highlights created by `org-preview-latex-fragment' always need
  1062. `C-c C-c' to be removed."
  1063. :group 'org-sparse-trees
  1064. :group 'org-time
  1065. :type 'boolean)
  1066. (defcustom org-occur-hook '(org-first-headline-recenter)
  1067. "Hook that is run after `org-occur' has constructed a sparse tree.
  1068. This can be used to recenter the window to show as much of the structure
  1069. as possible."
  1070. :group 'org-sparse-trees
  1071. :type 'hook)
  1072. (defgroup org-imenu-and-speedbar nil
  1073. "Options concerning imenu and speedbar in Org-mode."
  1074. :tag "Org Imenu and Speedbar"
  1075. :group 'org-structure)
  1076. (defcustom org-imenu-depth 2
  1077. "The maximum level for Imenu access to Org-mode headlines.
  1078. This also applied for speedbar access."
  1079. :group 'org-imenu-and-speedbar
  1080. :type 'integer)
  1081. (defgroup org-table nil
  1082. "Options concerning tables in Org-mode."
  1083. :tag "Org Table"
  1084. :group 'org)
  1085. (defcustom org-enable-table-editor 'optimized
  1086. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1087. When nil, such lines will be treated like ordinary lines.
  1088. When equal to the symbol `optimized', the table editor will be optimized to
  1089. do the following:
  1090. - Automatic overwrite mode in front of whitespace in table fields.
  1091. This makes the structure of the table stay in tact as long as the edited
  1092. field does not exceed the column width.
  1093. - Minimize the number of realigns. Normally, the table is aligned each time
  1094. TAB or RET are pressed to move to another field. With optimization this
  1095. happens only if changes to a field might have changed the column width.
  1096. Optimization requires replacing the functions `self-insert-command',
  1097. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1098. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1099. very good at guessing when a re-align will be necessary, but you can always
  1100. force one with \\[org-ctrl-c-ctrl-c].
  1101. If you would like to use the optimized version in Org-mode, but the
  1102. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1103. This variable can be used to turn on and off the table editor during a session,
  1104. but in order to toggle optimization, a restart is required.
  1105. See also the variable `org-table-auto-blank-field'."
  1106. :group 'org-table
  1107. :type '(choice
  1108. (const :tag "off" nil)
  1109. (const :tag "on" t)
  1110. (const :tag "on, optimized" optimized)))
  1111. (defcustom org-self-insert-cluster-for-undo t
  1112. "Non-nil means cluster self-insert commands for undo when possible.
  1113. If this is set, then, like in the Emacs command loop, 20 consecutive
  1114. characters will be undone together.
  1115. This is configurable, because there is some impact on typing performance."
  1116. :group 'org-table
  1117. :type 'boolean)
  1118. (defcustom org-table-tab-recognizes-table.el t
  1119. "Non-nil means TAB will automatically notice a table.el table.
  1120. When it sees such a table, it moves point into it and - if necessary -
  1121. calls `table-recognize-table'."
  1122. :group 'org-table-editing
  1123. :type 'boolean)
  1124. (defgroup org-link nil
  1125. "Options concerning links in Org-mode."
  1126. :tag "Org Link"
  1127. :group 'org)
  1128. (defvar org-link-abbrev-alist-local nil
  1129. "Buffer-local version of `org-link-abbrev-alist', which see.
  1130. The value of this is taken from the #+LINK lines.")
  1131. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1132. (defcustom org-link-abbrev-alist nil
  1133. "Alist of link abbreviations.
  1134. The car of each element is a string, to be replaced at the start of a link.
  1135. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1136. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1137. [[linkkey:tag][description]]
  1138. The 'linkkey' must be a word word, starting with a letter, followed
  1139. by letters, numbers, '-' or '_'.
  1140. If REPLACE is a string, the tag will simply be appended to create the link.
  1141. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1142. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1143. be inserted at that point (see the function `url-hexify-string').
  1144. REPLACE may also be a function that will be called with the tag as the
  1145. only argument to create the link, which should be returned as a string.
  1146. See the manual for examples."
  1147. :group 'org-link
  1148. :type '(repeat
  1149. (cons
  1150. (string :tag "Protocol")
  1151. (choice
  1152. (string :tag "Format")
  1153. (function)))))
  1154. (defcustom org-descriptive-links t
  1155. "Non-nil means Org will display descriptive links.
  1156. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1157. \"Org Website\", hiding the link itself and just displaying its
  1158. description. When set to `nil', Org will display the full links
  1159. literally.
  1160. You can interactively set the value of this variable by calling
  1161. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1162. :group 'org-link
  1163. :type 'boolean)
  1164. (defcustom org-link-file-path-type 'adaptive
  1165. "How the path name in file links should be stored.
  1166. Valid values are:
  1167. relative Relative to the current directory, i.e. the directory of the file
  1168. into which the link is being inserted.
  1169. absolute Absolute path, if possible with ~ for home directory.
  1170. noabbrev Absolute path, no abbreviation of home directory.
  1171. adaptive Use relative path for files in the current directory and sub-
  1172. directories of it. For other files, use an absolute path."
  1173. :group 'org-link
  1174. :type '(choice
  1175. (const relative)
  1176. (const absolute)
  1177. (const noabbrev)
  1178. (const adaptive)))
  1179. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1180. "Types of links that should be activated in Org-mode files.
  1181. This is a list of symbols, each leading to the activation of a certain link
  1182. type. In principle, it does not hurt to turn on most link types - there may
  1183. be a small gain when turning off unused link types. The types are:
  1184. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1185. angle Links in angular brackets that may contain whitespace like
  1186. <bbdb:Carsten Dominik>.
  1187. plain Plain links in normal text, no whitespace, like http://google.com.
  1188. radio Text that is matched by a radio target, see manual for details.
  1189. tag Tag settings in a headline (link to tag search).
  1190. date Time stamps (link to calendar).
  1191. footnote Footnote labels.
  1192. Changing this variable requires a restart of Emacs to become effective."
  1193. :group 'org-link
  1194. :type '(set :greedy t
  1195. (const :tag "Double bracket links" bracket)
  1196. (const :tag "Angular bracket links" angle)
  1197. (const :tag "Plain text links" plain)
  1198. (const :tag "Radio target matches" radio)
  1199. (const :tag "Tags" tag)
  1200. (const :tag "Timestamps" date)
  1201. (const :tag "Footnotes" footnote)))
  1202. (defcustom org-make-link-description-function nil
  1203. "Function to use to generate link descriptions from links.
  1204. If nil the link location will be used. This function must take
  1205. two parameters; the first is the link and the second the
  1206. description `org-insert-link' has generated, and should return the
  1207. description to use."
  1208. :group 'org-link
  1209. :type 'function)
  1210. (defgroup org-link-store nil
  1211. "Options concerning storing links in Org-mode."
  1212. :tag "Org Store Link"
  1213. :group 'org-link)
  1214. (defcustom org-email-link-description-format "Email %c: %.30s"
  1215. "Format of the description part of a link to an email or usenet message.
  1216. The following %-escapes will be replaced by corresponding information:
  1217. %F full \"From\" field
  1218. %f name, taken from \"From\" field, address if no name
  1219. %T full \"To\" field
  1220. %t first name in \"To\" field, address if no name
  1221. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1222. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1223. %s subject
  1224. %d date
  1225. %m message-id.
  1226. You may use normal field width specification between the % and the letter.
  1227. This is for example useful to limit the length of the subject.
  1228. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1229. :group 'org-link-store
  1230. :type 'string)
  1231. (defcustom org-from-is-user-regexp
  1232. (let (r1 r2)
  1233. (when (and user-mail-address (not (string= user-mail-address "")))
  1234. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1235. (when (and user-full-name (not (string= user-full-name "")))
  1236. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1237. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1238. "Regexp matched against the \"From:\" header of an email or usenet message.
  1239. It should match if the message is from the user him/herself."
  1240. :group 'org-link-store
  1241. :type 'regexp)
  1242. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1243. "Non-nil means storing a link to an Org file will use entry IDs.
  1244. Note that before this variable is even considered, org-id must be loaded,
  1245. so please customize `org-modules' and turn it on.
  1246. The variable can have the following values:
  1247. t Create an ID if needed to make a link to the current entry.
  1248. create-if-interactive
  1249. If `org-store-link' is called directly (interactively, as a user
  1250. command), do create an ID to support the link. But when doing the
  1251. job for remember, only use the ID if it already exists. The
  1252. purpose of this setting is to avoid proliferation of unwanted
  1253. IDs, just because you happen to be in an Org file when you
  1254. call `org-remember' that automatically and preemptively
  1255. creates a link. If you do want to get an ID link in a remember
  1256. template to an entry not having an ID, create it first by
  1257. explicitly creating a link to it, using `C-c C-l' first.
  1258. create-if-interactive-and-no-custom-id
  1259. Like create-if-interactive, but do not create an ID if there is
  1260. a CUSTOM_ID property defined in the entry. This is the default.
  1261. use-existing
  1262. Use existing ID, do not create one.
  1263. nil Never use an ID to make a link, instead link using a text search for
  1264. the headline text."
  1265. :group 'org-link-store
  1266. :type '(choice
  1267. (const :tag "Create ID to make link" t)
  1268. (const :tag "Create if storing link interactively"
  1269. create-if-interactive)
  1270. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1271. create-if-interactive-and-no-custom-id)
  1272. (const :tag "Only use existing" use-existing)
  1273. (const :tag "Do not use ID to create link" nil)))
  1274. (defcustom org-context-in-file-links t
  1275. "Non-nil means file links from `org-store-link' contain context.
  1276. A search string will be added to the file name with :: as separator and
  1277. used to find the context when the link is activated by the command
  1278. `org-open-at-point'. When this option is t, the entire active region
  1279. will be placed in the search string of the file link. If set to a
  1280. positive integer, only the first n lines of context will be stored.
  1281. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1282. negates this setting for the duration of the command."
  1283. :group 'org-link-store
  1284. :type '(choice boolean integer))
  1285. (defcustom org-keep-stored-link-after-insertion nil
  1286. "Non-nil means keep link in list for entire session.
  1287. The command `org-store-link' adds a link pointing to the current
  1288. location to an internal list. These links accumulate during a session.
  1289. The command `org-insert-link' can be used to insert links into any
  1290. Org-mode file (offering completion for all stored links). When this
  1291. option is nil, every link which has been inserted once using \\[org-insert-link]
  1292. will be removed from the list, to make completing the unused links
  1293. more efficient."
  1294. :group 'org-link-store
  1295. :type 'boolean)
  1296. (defgroup org-link-follow nil
  1297. "Options concerning following links in Org-mode."
  1298. :tag "Org Follow Link"
  1299. :group 'org-link)
  1300. (defcustom org-link-translation-function nil
  1301. "Function to translate links with different syntax to Org syntax.
  1302. This can be used to translate links created for example by the Planner
  1303. or emacs-wiki packages to Org syntax.
  1304. The function must accept two parameters, a TYPE containing the link
  1305. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1306. which is everything after the link protocol. It should return a cons
  1307. with possibly modified values of type and path.
  1308. Org contains a function for this, so if you set this variable to
  1309. `org-translate-link-from-planner', you should be able follow many
  1310. links created by planner."
  1311. :group 'org-link-follow
  1312. :type 'function)
  1313. (defcustom org-follow-link-hook nil
  1314. "Hook that is run after a link has been followed."
  1315. :group 'org-link-follow
  1316. :type 'hook)
  1317. (defcustom org-tab-follows-link nil
  1318. "Non-nil means on links TAB will follow the link.
  1319. Needs to be set before org.el is loaded.
  1320. This really should not be used, it does not make sense, and the
  1321. implementation is bad."
  1322. :group 'org-link-follow
  1323. :type 'boolean)
  1324. (defcustom org-return-follows-link nil
  1325. "Non-nil means on links RET will follow the link."
  1326. :group 'org-link-follow
  1327. :type 'boolean)
  1328. (defcustom org-mouse-1-follows-link
  1329. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1330. "Non-nil means mouse-1 on a link will follow the link.
  1331. A longer mouse click will still set point. Does not work on XEmacs.
  1332. Needs to be set before org.el is loaded."
  1333. :group 'org-link-follow
  1334. :type 'boolean)
  1335. (defcustom org-mark-ring-length 4
  1336. "Number of different positions to be recorded in the ring.
  1337. Changing this requires a restart of Emacs to work correctly."
  1338. :group 'org-link-follow
  1339. :type 'integer)
  1340. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1341. "Non-nil means internal links in Org files must exactly match a headline.
  1342. When nil, the link search tries to match a phrase with all words
  1343. in the search text."
  1344. :group 'org-link-follow
  1345. :type '(choice
  1346. (const :tag "Use fuzzy text search" nil)
  1347. (const :tag "Match only exact headline" t)
  1348. (const :tag "Match exact headline or query to create it"
  1349. query-to-create)))
  1350. (defcustom org-link-frame-setup
  1351. '((vm . vm-visit-folder-other-frame)
  1352. (gnus . org-gnus-no-new-news)
  1353. (file . find-file-other-window)
  1354. (wl . wl-other-frame))
  1355. "Setup the frame configuration for following links.
  1356. When following a link with Emacs, it may often be useful to display
  1357. this link in another window or frame. This variable can be used to
  1358. set this up for the different types of links.
  1359. For VM, use any of
  1360. `vm-visit-folder'
  1361. `vm-visit-folder-other-window'
  1362. `vm-visit-folder-other-frame'
  1363. For Gnus, use any of
  1364. `gnus'
  1365. `gnus-other-frame'
  1366. `org-gnus-no-new-news'
  1367. For FILE, use any of
  1368. `find-file'
  1369. `find-file-other-window'
  1370. `find-file-other-frame'
  1371. For Wanderlust use any of
  1372. `wl'
  1373. `wl-other-frame'
  1374. For the calendar, use the variable `calendar-setup'.
  1375. For BBDB, it is currently only possible to display the matches in
  1376. another window."
  1377. :group 'org-link-follow
  1378. :type '(list
  1379. (cons (const vm)
  1380. (choice
  1381. (const vm-visit-folder)
  1382. (const vm-visit-folder-other-window)
  1383. (const vm-visit-folder-other-frame)))
  1384. (cons (const gnus)
  1385. (choice
  1386. (const gnus)
  1387. (const gnus-other-frame)
  1388. (const org-gnus-no-new-news)))
  1389. (cons (const file)
  1390. (choice
  1391. (const find-file)
  1392. (const find-file-other-window)
  1393. (const find-file-other-frame)))
  1394. (cons (const wl)
  1395. (choice
  1396. (const wl)
  1397. (const wl-other-frame)))))
  1398. (defcustom org-display-internal-link-with-indirect-buffer nil
  1399. "Non-nil means use indirect buffer to display infile links.
  1400. Activating internal links (from one location in a file to another location
  1401. in the same file) normally just jumps to the location. When the link is
  1402. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1403. is displayed in
  1404. another window. When this option is set, the other window actually displays
  1405. an indirect buffer clone of the current buffer, to avoid any visibility
  1406. changes to the current buffer."
  1407. :group 'org-link-follow
  1408. :type 'boolean)
  1409. (defcustom org-open-non-existing-files nil
  1410. "Non-nil means `org-open-file' will open non-existing files.
  1411. When nil, an error will be generated.
  1412. This variable applies only to external applications because they
  1413. might choke on non-existing files. If the link is to a file that
  1414. will be opened in Emacs, the variable is ignored."
  1415. :group 'org-link-follow
  1416. :type 'boolean)
  1417. (defcustom org-open-directory-means-index-dot-org nil
  1418. "Non-nil means a link to a directory really means to index.org.
  1419. When nil, following a directory link will run dired or open a finder/explorer
  1420. window on that directory."
  1421. :group 'org-link-follow
  1422. :type 'boolean)
  1423. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1424. "Function and arguments to call for following mailto links.
  1425. This is a list with the first element being a Lisp function, and the
  1426. remaining elements being arguments to the function. In string arguments,
  1427. %a will be replaced by the address, and %s will be replaced by the subject
  1428. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1429. :group 'org-link-follow
  1430. :type '(choice
  1431. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1432. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1433. (const :tag "message-mail" (message-mail "%a" "%s"))
  1434. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1435. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1436. "Non-nil means ask for confirmation before executing shell links.
  1437. Shell links can be dangerous: just think about a link
  1438. [[shell:rm -rf ~/*][Google Search]]
  1439. This link would show up in your Org-mode document as \"Google Search\",
  1440. but really it would remove your entire home directory.
  1441. Therefore we advise against setting this variable to nil.
  1442. Just change it to `y-or-n-p' if you want to confirm with a
  1443. single keystroke rather than having to type \"yes\"."
  1444. :group 'org-link-follow
  1445. :type '(choice
  1446. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1447. (const :tag "with y-or-n (faster)" y-or-n-p)
  1448. (const :tag "no confirmation (dangerous)" nil)))
  1449. (put 'org-confirm-shell-link-function
  1450. 'safe-local-variable
  1451. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1452. (defcustom org-confirm-shell-link-not-regexp ""
  1453. "A regexp to skip confirmation for shell links."
  1454. :group 'org-link-follow
  1455. :type 'regexp)
  1456. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1457. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1458. Elisp links can be dangerous: just think about a link
  1459. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1460. This link would show up in your Org-mode document as \"Google Search\",
  1461. but really it would remove your entire home directory.
  1462. Therefore we advise against setting this variable to nil.
  1463. Just change it to `y-or-n-p' if you want to confirm with a
  1464. single keystroke rather than having to type \"yes\"."
  1465. :group 'org-link-follow
  1466. :type '(choice
  1467. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1468. (const :tag "with y-or-n (faster)" y-or-n-p)
  1469. (const :tag "no confirmation (dangerous)" nil)))
  1470. (put 'org-confirm-shell-link-function
  1471. 'safe-local-variable
  1472. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1473. (defcustom org-confirm-elisp-link-not-regexp ""
  1474. "A regexp to skip confirmation for Elisp links."
  1475. :group 'org-link-follow
  1476. :type 'regexp)
  1477. (defconst org-file-apps-defaults-gnu
  1478. '((remote . emacs)
  1479. (system . mailcap)
  1480. (t . mailcap))
  1481. "Default file applications on a UNIX or GNU/Linux system.
  1482. See `org-file-apps'.")
  1483. (defconst org-file-apps-defaults-macosx
  1484. '((remote . emacs)
  1485. (t . "open %s")
  1486. (system . "open %s")
  1487. ("ps.gz" . "gv %s")
  1488. ("eps.gz" . "gv %s")
  1489. ("dvi" . "xdvi %s")
  1490. ("fig" . "xfig %s"))
  1491. "Default file applications on a MacOS X system.
  1492. The system \"open\" is known as a default, but we use X11 applications
  1493. for some files for which the OS does not have a good default.
  1494. See `org-file-apps'.")
  1495. (defconst org-file-apps-defaults-windowsnt
  1496. (list
  1497. '(remote . emacs)
  1498. (cons t
  1499. (list (if (featurep 'xemacs)
  1500. 'mswindows-shell-execute
  1501. 'w32-shell-execute)
  1502. "open" 'file))
  1503. (cons 'system
  1504. (list (if (featurep 'xemacs)
  1505. 'mswindows-shell-execute
  1506. 'w32-shell-execute)
  1507. "open" 'file)))
  1508. "Default file applications on a Windows NT system.
  1509. The system \"open\" is used for most files.
  1510. See `org-file-apps'.")
  1511. (defcustom org-file-apps
  1512. '(
  1513. (auto-mode . emacs)
  1514. ("\\.mm\\'" . default)
  1515. ("\\.x?html?\\'" . default)
  1516. ("\\.pdf\\'" . default)
  1517. )
  1518. "External applications for opening `file:path' items in a document.
  1519. Org-mode uses system defaults for different file types, but
  1520. you can use this variable to set the application for a given file
  1521. extension. The entries in this list are cons cells where the car identifies
  1522. files and the cdr the corresponding command. Possible values for the
  1523. file identifier are
  1524. \"string\" A string as a file identifier can be interpreted in different
  1525. ways, depending on its contents:
  1526. - Alphanumeric characters only:
  1527. Match links with this file extension.
  1528. Example: (\"pdf\" . \"evince %s\")
  1529. to open PDFs with evince.
  1530. - Regular expression: Match links where the
  1531. filename matches the regexp. If you want to
  1532. use groups here, use shy groups.
  1533. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1534. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1535. to open *.html and *.xhtml with firefox.
  1536. - Regular expression which contains (non-shy) groups:
  1537. Match links where the whole link, including \"::\", and
  1538. anything after that, matches the regexp.
  1539. In a custom command string, %1, %2, etc. are replaced with
  1540. the parts of the link that were matched by the groups.
  1541. For backwards compatibility, if a command string is given
  1542. that does not use any of the group matches, this case is
  1543. handled identically to the second one (i.e. match against
  1544. file name only).
  1545. In a custom lisp form, you can access the group matches with
  1546. (match-string n link).
  1547. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1548. to open [[file:document.pdf::5]] with evince at page 5.
  1549. `directory' Matches a directory
  1550. `remote' Matches a remote file, accessible through tramp or efs.
  1551. Remote files most likely should be visited through Emacs
  1552. because external applications cannot handle such paths.
  1553. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1554. so all files Emacs knows how to handle. Using this with
  1555. command `emacs' will open most files in Emacs. Beware that this
  1556. will also open html files inside Emacs, unless you add
  1557. (\"html\" . default) to the list as well.
  1558. t Default for files not matched by any of the other options.
  1559. `system' The system command to open files, like `open' on Windows
  1560. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1561. that will be selected if you call `C-c C-o' with a double
  1562. \\[universal-argument] \\[universal-argument] prefix.
  1563. Possible values for the command are:
  1564. `emacs' The file will be visited by the current Emacs process.
  1565. `default' Use the default application for this file type, which is the
  1566. association for t in the list, most likely in the system-specific
  1567. part.
  1568. This can be used to overrule an unwanted setting in the
  1569. system-specific variable.
  1570. `system' Use the system command for opening files, like \"open\".
  1571. This command is specified by the entry whose car is `system'.
  1572. Most likely, the system-specific version of this variable
  1573. does define this command, but you can overrule/replace it
  1574. here.
  1575. string A command to be executed by a shell; %s will be replaced
  1576. by the path to the file.
  1577. sexp A Lisp form which will be evaluated. The file path will
  1578. be available in the Lisp variable `file'.
  1579. For more examples, see the system specific constants
  1580. `org-file-apps-defaults-macosx'
  1581. `org-file-apps-defaults-windowsnt'
  1582. `org-file-apps-defaults-gnu'."
  1583. :group 'org-link-follow
  1584. :type '(repeat
  1585. (cons (choice :value ""
  1586. (string :tag "Extension")
  1587. (const :tag "System command to open files" system)
  1588. (const :tag "Default for unrecognized files" t)
  1589. (const :tag "Remote file" remote)
  1590. (const :tag "Links to a directory" directory)
  1591. (const :tag "Any files that have Emacs modes"
  1592. auto-mode))
  1593. (choice :value ""
  1594. (const :tag "Visit with Emacs" emacs)
  1595. (const :tag "Use default" default)
  1596. (const :tag "Use the system command" system)
  1597. (string :tag "Command")
  1598. (sexp :tag "Lisp form")))))
  1599. (defgroup org-refile nil
  1600. "Options concerning refiling entries in Org-mode."
  1601. :tag "Org Refile"
  1602. :group 'org)
  1603. (defcustom org-directory "~/org"
  1604. "Directory with org files.
  1605. This is just a default location to look for Org files. There is no need
  1606. at all to put your files into this directory. It is only used in the
  1607. following situations:
  1608. 1. When a remember template specifies a target file that is not an
  1609. absolute path. The path will then be interpreted relative to
  1610. `org-directory'
  1611. 2. When a remember note is filed away in an interactive way (when exiting the
  1612. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1613. with `org-directory' as the default path."
  1614. :group 'org-refile
  1615. :group 'org-remember
  1616. :type 'directory)
  1617. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1618. "Default target for storing notes.
  1619. Used as a fall back file for org-remember.el and org-capture.el, for
  1620. templates that do not specify a target file."
  1621. :group 'org-refile
  1622. :group 'org-remember
  1623. :type '(choice
  1624. (const :tag "Default from remember-data-file" nil)
  1625. file))
  1626. (defcustom org-goto-interface 'outline
  1627. "The default interface to be used for `org-goto'.
  1628. Allowed values are:
  1629. outline The interface shows an outline of the relevant file
  1630. and the correct heading is found by moving through
  1631. the outline or by searching with incremental search.
  1632. outline-path-completion Headlines in the current buffer are offered via
  1633. completion. This is the interface also used by
  1634. the refile command."
  1635. :group 'org-refile
  1636. :type '(choice
  1637. (const :tag "Outline" outline)
  1638. (const :tag "Outline-path-completion" outline-path-completion)))
  1639. (defcustom org-goto-max-level 5
  1640. "Maximum target level when running `org-goto' with refile interface."
  1641. :group 'org-refile
  1642. :type 'integer)
  1643. (defcustom org-reverse-note-order nil
  1644. "Non-nil means store new notes at the beginning of a file or entry.
  1645. When nil, new notes will be filed to the end of a file or entry.
  1646. This can also be a list with cons cells of regular expressions that
  1647. are matched against file names, and values."
  1648. :group 'org-remember
  1649. :group 'org-refile
  1650. :type '(choice
  1651. (const :tag "Reverse always" t)
  1652. (const :tag "Reverse never" nil)
  1653. (repeat :tag "By file name regexp"
  1654. (cons regexp boolean))))
  1655. (defcustom org-log-refile nil
  1656. "Information to record when a task is refiled.
  1657. Possible values are:
  1658. nil Don't add anything
  1659. time Add a time stamp to the task
  1660. note Prompt for a note and add it with template `org-log-note-headings'
  1661. This option can also be set with on a per-file-basis with
  1662. #+STARTUP: nologrefile
  1663. #+STARTUP: logrefile
  1664. #+STARTUP: lognoterefile
  1665. You can have local logging settings for a subtree by setting the LOGGING
  1666. property to one or more of these keywords.
  1667. When bulk-refiling from the agenda, the value `note' is forbidden and
  1668. will temporarily be changed to `time'."
  1669. :group 'org-refile
  1670. :group 'org-progress
  1671. :type '(choice
  1672. (const :tag "No logging" nil)
  1673. (const :tag "Record timestamp" time)
  1674. (const :tag "Record timestamp with note." note)))
  1675. (defcustom org-refile-targets nil
  1676. "Targets for refiling entries with \\[org-refile].
  1677. This is a list of cons cells. Each cell contains:
  1678. - a specification of the files to be considered, either a list of files,
  1679. or a symbol whose function or variable value will be used to retrieve
  1680. a file name or a list of file names. If you use `org-agenda-files' for
  1681. that, all agenda files will be scanned for targets. Nil means consider
  1682. headings in the current buffer.
  1683. - A specification of how to find candidate refile targets. This may be
  1684. any of:
  1685. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1686. This tag has to be present in all target headlines, inheritance will
  1687. not be considered.
  1688. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1689. todo keyword.
  1690. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1691. headlines that are refiling targets.
  1692. - a cons cell (:level . N). Any headline of level N is considered a target.
  1693. Note that, when `org-odd-levels-only' is set, level corresponds to
  1694. order in hierarchy, not to the number of stars.
  1695. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1696. Note that, when `org-odd-levels-only' is set, level corresponds to
  1697. order in hierarchy, not to the number of stars.
  1698. Each element of this list generates a set of possible targets.
  1699. The union of these sets is presented (with completion) to
  1700. the user by `org-refile'.
  1701. You can set the variable `org-refile-target-verify-function' to a function
  1702. to verify each headline found by the simple criteria above.
  1703. When this variable is nil, all top-level headlines in the current buffer
  1704. are used, equivalent to the value `((nil . (:level . 1))'."
  1705. :group 'org-refile
  1706. :type '(repeat
  1707. (cons
  1708. (choice :value org-agenda-files
  1709. (const :tag "All agenda files" org-agenda-files)
  1710. (const :tag "Current buffer" nil)
  1711. (function) (variable) (file))
  1712. (choice :tag "Identify target headline by"
  1713. (cons :tag "Specific tag" (const :value :tag) (string))
  1714. (cons :tag "TODO keyword" (const :value :todo) (string))
  1715. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1716. (cons :tag "Level number" (const :value :level) (integer))
  1717. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1718. (defcustom org-refile-target-verify-function nil
  1719. "Function to verify if the headline at point should be a refile target.
  1720. The function will be called without arguments, with point at the
  1721. beginning of the headline. It should return t and leave point
  1722. where it is if the headline is a valid target for refiling.
  1723. If the target should not be selected, the function must return nil.
  1724. In addition to this, it may move point to a place from where the search
  1725. should be continued. For example, the function may decide that the entire
  1726. subtree of the current entry should be excluded and move point to the end
  1727. of the subtree."
  1728. :group 'org-refile
  1729. :type 'function)
  1730. (defcustom org-refile-use-cache nil
  1731. "Non-nil means cache refile targets to speed up the process.
  1732. The cache for a particular file will be updated automatically when
  1733. the buffer has been killed, or when any of the marker used for flagging
  1734. refile targets no longer points at a live buffer.
  1735. If you have added new entries to a buffer that might themselves be targets,
  1736. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1737. find that easier, `C-u C-u C-u C-c C-w'."
  1738. :group 'org-refile
  1739. :type 'boolean)
  1740. (defcustom org-refile-use-outline-path nil
  1741. "Non-nil means provide refile targets as paths.
  1742. So a level 3 headline will be available as level1/level2/level3.
  1743. When the value is `file', also include the file name (without directory)
  1744. into the path. In this case, you can also stop the completion after
  1745. the file name, to get entries inserted as top level in the file.
  1746. When `full-file-path', include the full file path."
  1747. :group 'org-refile
  1748. :type '(choice
  1749. (const :tag "Not" nil)
  1750. (const :tag "Yes" t)
  1751. (const :tag "Start with file name" file)
  1752. (const :tag "Start with full file path" full-file-path)))
  1753. (defcustom org-outline-path-complete-in-steps t
  1754. "Non-nil means complete the outline path in hierarchical steps.
  1755. When Org-mode uses the refile interface to select an outline path
  1756. \(see variable `org-refile-use-outline-path'), the completion of
  1757. the path can be done is a single go, or if can be done in steps down
  1758. the headline hierarchy. Going in steps is probably the best if you
  1759. do not use a special completion package like `ido' or `icicles'.
  1760. However, when using these packages, going in one step can be very
  1761. fast, while still showing the whole path to the entry."
  1762. :group 'org-refile
  1763. :type 'boolean)
  1764. (defcustom org-refile-allow-creating-parent-nodes nil
  1765. "Non-nil means allow to create new nodes as refile targets.
  1766. New nodes are then created by adding \"/new node name\" to the completion
  1767. of an existing node. When the value of this variable is `confirm',
  1768. new node creation must be confirmed by the user (recommended)
  1769. When nil, the completion must match an existing entry.
  1770. Note that, if the new heading is not seen by the criteria
  1771. listed in `org-refile-targets', multiple instances of the same
  1772. heading would be created by trying again to file under the new
  1773. heading."
  1774. :group 'org-refile
  1775. :type '(choice
  1776. (const :tag "Never" nil)
  1777. (const :tag "Always" t)
  1778. (const :tag "Prompt for confirmation" confirm)))
  1779. (defcustom org-refile-active-region-within-subtree nil
  1780. "Non-nil means also refile active region within a subtree.
  1781. By default `org-refile' doesn't allow refiling regions if they
  1782. don't contain a set of subtrees, but it might be convenient to
  1783. do so sometimes: in that case, the first line of the region is
  1784. converted to a headline before refiling."
  1785. :group 'org-refile
  1786. :type 'boolean)
  1787. (defgroup org-todo nil
  1788. "Options concerning TODO items in Org-mode."
  1789. :tag "Org TODO"
  1790. :group 'org)
  1791. (defgroup org-progress nil
  1792. "Options concerning Progress logging in Org-mode."
  1793. :tag "Org Progress"
  1794. :group 'org-time)
  1795. (defvar org-todo-interpretation-widgets
  1796. '((:tag "Sequence (cycling hits every state)" sequence)
  1797. (:tag "Type (cycling directly to DONE)" type))
  1798. "The available interpretation symbols for customizing `org-todo-keywords'.
  1799. Interested libraries should add to this list.")
  1800. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1801. "List of TODO entry keyword sequences and their interpretation.
  1802. \\<org-mode-map>This is a list of sequences.
  1803. Each sequence starts with a symbol, either `sequence' or `type',
  1804. indicating if the keywords should be interpreted as a sequence of
  1805. action steps, or as different types of TODO items. The first
  1806. keywords are states requiring action - these states will select a headline
  1807. for inclusion into the global TODO list Org-mode produces. If one of
  1808. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1809. signify that no further action is necessary. If \"|\" is not found,
  1810. the last keyword is treated as the only DONE state of the sequence.
  1811. The command \\[org-todo] cycles an entry through these states, and one
  1812. additional state where no keyword is present. For details about this
  1813. cycling, see the manual.
  1814. TODO keywords and interpretation can also be set on a per-file basis with
  1815. the special #+SEQ_TODO and #+TYP_TODO lines.
  1816. Each keyword can optionally specify a character for fast state selection
  1817. \(in combination with the variable `org-use-fast-todo-selection')
  1818. and specifiers for state change logging, using the same syntax
  1819. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1820. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1821. indicates to record a time stamp each time this state is selected.
  1822. Each keyword may also specify if a timestamp or a note should be
  1823. recorded when entering or leaving the state, by adding additional
  1824. characters in the parenthesis after the keyword. This looks like this:
  1825. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1826. record only the time of the state change. With X and Y being either
  1827. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1828. Y when leaving the state if and only if the *target* state does not
  1829. define X. You may omit any of the fast-selection key or X or /Y,
  1830. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1831. For backward compatibility, this variable may also be just a list
  1832. of keywords - in this case the interpretation (sequence or type) will be
  1833. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1834. :group 'org-todo
  1835. :group 'org-keywords
  1836. :type '(choice
  1837. (repeat :tag "Old syntax, just keywords"
  1838. (string :tag "Keyword"))
  1839. (repeat :tag "New syntax"
  1840. (cons
  1841. (choice
  1842. :tag "Interpretation"
  1843. ;;Quick and dirty way to see
  1844. ;;`org-todo-interpretations'. This takes the
  1845. ;;place of item arguments
  1846. :convert-widget
  1847. (lambda (widget)
  1848. (widget-put widget
  1849. :args (mapcar
  1850. #'(lambda (x)
  1851. (widget-convert
  1852. (cons 'const x)))
  1853. org-todo-interpretation-widgets))
  1854. widget))
  1855. (repeat
  1856. (string :tag "Keyword"))))))
  1857. (defvar org-todo-keywords-1 nil
  1858. "All TODO and DONE keywords active in a buffer.")
  1859. (make-variable-buffer-local 'org-todo-keywords-1)
  1860. (defvar org-todo-keywords-for-agenda nil)
  1861. (defvar org-done-keywords-for-agenda nil)
  1862. (defvar org-drawers-for-agenda nil)
  1863. (defvar org-todo-keyword-alist-for-agenda nil)
  1864. (defvar org-tag-alist-for-agenda nil)
  1865. (defvar org-agenda-contributing-files nil)
  1866. (defvar org-not-done-keywords nil)
  1867. (make-variable-buffer-local 'org-not-done-keywords)
  1868. (defvar org-done-keywords nil)
  1869. (make-variable-buffer-local 'org-done-keywords)
  1870. (defvar org-todo-heads nil)
  1871. (make-variable-buffer-local 'org-todo-heads)
  1872. (defvar org-todo-sets nil)
  1873. (make-variable-buffer-local 'org-todo-sets)
  1874. (defvar org-todo-log-states nil)
  1875. (make-variable-buffer-local 'org-todo-log-states)
  1876. (defvar org-todo-kwd-alist nil)
  1877. (make-variable-buffer-local 'org-todo-kwd-alist)
  1878. (defvar org-todo-key-alist nil)
  1879. (make-variable-buffer-local 'org-todo-key-alist)
  1880. (defvar org-todo-key-trigger nil)
  1881. (make-variable-buffer-local 'org-todo-key-trigger)
  1882. (defcustom org-todo-interpretation 'sequence
  1883. "Controls how TODO keywords are interpreted.
  1884. This variable is in principle obsolete and is only used for
  1885. backward compatibility, if the interpretation of todo keywords is
  1886. not given already in `org-todo-keywords'. See that variable for
  1887. more information."
  1888. :group 'org-todo
  1889. :group 'org-keywords
  1890. :type '(choice (const sequence)
  1891. (const type)))
  1892. (defcustom org-use-fast-todo-selection t
  1893. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1894. This variable describes if and under what circumstances the cycling
  1895. mechanism for TODO keywords will be replaced by a single-key, direct
  1896. selection scheme.
  1897. When nil, fast selection is never used.
  1898. When the symbol `prefix', it will be used when `org-todo' is called with
  1899. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1900. in an agenda buffer.
  1901. When t, fast selection is used by default. In this case, the prefix
  1902. argument forces cycling instead.
  1903. In all cases, the special interface is only used if access keys have actually
  1904. been assigned by the user, i.e. if keywords in the configuration are followed
  1905. by a letter in parenthesis, like TODO(t)."
  1906. :group 'org-todo
  1907. :type '(choice
  1908. (const :tag "Never" nil)
  1909. (const :tag "By default" t)
  1910. (const :tag "Only with C-u C-c C-t" prefix)))
  1911. (defcustom org-provide-todo-statistics t
  1912. "Non-nil means update todo statistics after insert and toggle.
  1913. ALL-HEADLINES means update todo statistics by including headlines
  1914. with no TODO keyword as well, counting them as not done.
  1915. A list of TODO keywords means the same, but skip keywords that are
  1916. not in this list.
  1917. When this is set, todo statistics is updated in the parent of the
  1918. current entry each time a todo state is changed."
  1919. :group 'org-todo
  1920. :type '(choice
  1921. (const :tag "Yes, only for TODO entries" t)
  1922. (const :tag "Yes, including all entries" 'all-headlines)
  1923. (repeat :tag "Yes, for TODOs in this list"
  1924. (string :tag "TODO keyword"))
  1925. (other :tag "No TODO statistics" nil)))
  1926. (defcustom org-hierarchical-todo-statistics t
  1927. "Non-nil means TODO statistics covers just direct children.
  1928. When nil, all entries in the subtree are considered.
  1929. This has only an effect if `org-provide-todo-statistics' is set.
  1930. To set this to nil for only a single subtree, use a COOKIE_DATA
  1931. property and include the word \"recursive\" into the value."
  1932. :group 'org-todo
  1933. :type 'boolean)
  1934. (defcustom org-after-todo-state-change-hook nil
  1935. "Hook which is run after the state of a TODO item was changed.
  1936. The new state (a string with a TODO keyword, or nil) is available in the
  1937. Lisp variable `state'."
  1938. :group 'org-todo
  1939. :type 'hook)
  1940. (defvar org-blocker-hook nil
  1941. "Hook for functions that are allowed to block a state change.
  1942. Each function gets as its single argument a property list, see
  1943. `org-trigger-hook' for more information about this list.
  1944. If any of the functions in this hook returns nil, the state change
  1945. is blocked.")
  1946. (defvar org-trigger-hook nil
  1947. "Hook for functions that are triggered by a state change.
  1948. Each function gets as its single argument a property list with at least
  1949. the following elements:
  1950. (:type type-of-change :position pos-at-entry-start
  1951. :from old-state :to new-state)
  1952. Depending on the type, more properties may be present.
  1953. This mechanism is currently implemented for:
  1954. TODO state changes
  1955. ------------------
  1956. :type todo-state-change
  1957. :from previous state (keyword as a string), or nil, or a symbol
  1958. 'todo' or 'done', to indicate the general type of state.
  1959. :to new state, like in :from")
  1960. (defcustom org-enforce-todo-dependencies nil
  1961. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1962. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1963. be blocked if any prior sibling is not yet done.
  1964. Finally, if the parent is blocked because of ordered siblings of its own,
  1965. the child will also be blocked."
  1966. :set (lambda (var val)
  1967. (set var val)
  1968. (if val
  1969. (add-hook 'org-blocker-hook
  1970. 'org-block-todo-from-children-or-siblings-or-parent)
  1971. (remove-hook 'org-blocker-hook
  1972. 'org-block-todo-from-children-or-siblings-or-parent)))
  1973. :group 'org-todo
  1974. :type 'boolean)
  1975. (defcustom org-enforce-todo-checkbox-dependencies nil
  1976. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1977. When this is nil, checkboxes have no influence on switching TODO states.
  1978. When non-nil, you first need to check off all check boxes before the TODO
  1979. entry can be switched to DONE.
  1980. This variable needs to be set before org.el is loaded, and you need to
  1981. restart Emacs after a change to make the change effective. The only way
  1982. to change is while Emacs is running is through the customize interface."
  1983. :set (lambda (var val)
  1984. (set var val)
  1985. (if val
  1986. (add-hook 'org-blocker-hook
  1987. 'org-block-todo-from-checkboxes)
  1988. (remove-hook 'org-blocker-hook
  1989. 'org-block-todo-from-checkboxes)))
  1990. :group 'org-todo
  1991. :type 'boolean)
  1992. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1993. "Non-nil means inserting a TODO heading is treated as state change.
  1994. So when the command \\[org-insert-todo-heading] is used, state change
  1995. logging will apply if appropriate. When nil, the new TODO item will
  1996. be inserted directly, and no logging will take place."
  1997. :group 'org-todo
  1998. :type 'boolean)
  1999. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2000. "Non-nil means switching TODO states with S-cursor counts as state change.
  2001. This is the default behavior. However, setting this to nil allows a
  2002. convenient way to select a TODO state and bypass any logging associated
  2003. with that."
  2004. :group 'org-todo
  2005. :type 'boolean)
  2006. (defcustom org-todo-state-tags-triggers nil
  2007. "Tag changes that should be triggered by TODO state changes.
  2008. This is a list. Each entry is
  2009. (state-change (tag . flag) .......)
  2010. State-change can be a string with a state, and empty string to indicate the
  2011. state that has no TODO keyword, or it can be one of the symbols `todo'
  2012. or `done', meaning any not-done or done state, respectively."
  2013. :group 'org-todo
  2014. :group 'org-tags
  2015. :type '(repeat
  2016. (cons (choice :tag "When changing to"
  2017. (const :tag "Not-done state" todo)
  2018. (const :tag "Done state" done)
  2019. (string :tag "State"))
  2020. (repeat
  2021. (cons :tag "Tag action"
  2022. (string :tag "Tag")
  2023. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2024. (defcustom org-log-done nil
  2025. "Information to record when a task moves to the DONE state.
  2026. Possible values are:
  2027. nil Don't add anything, just change the keyword
  2028. time Add a time stamp to the task
  2029. note Prompt for a note and add it with template `org-log-note-headings'
  2030. This option can also be set with on a per-file-basis with
  2031. #+STARTUP: nologdone
  2032. #+STARTUP: logdone
  2033. #+STARTUP: lognotedone
  2034. You can have local logging settings for a subtree by setting the LOGGING
  2035. property to one or more of these keywords."
  2036. :group 'org-todo
  2037. :group 'org-progress
  2038. :type '(choice
  2039. (const :tag "No logging" nil)
  2040. (const :tag "Record CLOSED timestamp" time)
  2041. (const :tag "Record CLOSED timestamp with note." note)))
  2042. ;; Normalize old uses of org-log-done.
  2043. (cond
  2044. ((eq org-log-done t) (setq org-log-done 'time))
  2045. ((and (listp org-log-done) (memq 'done org-log-done))
  2046. (setq org-log-done 'note)))
  2047. (defcustom org-log-reschedule nil
  2048. "Information to record when the scheduling date of a tasks is modified.
  2049. Possible values are:
  2050. nil Don't add anything, just change the date
  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: nologreschedule
  2055. #+STARTUP: logreschedule
  2056. #+STARTUP: lognotereschedule"
  2057. :group 'org-todo
  2058. :group 'org-progress
  2059. :type '(choice
  2060. (const :tag "No logging" nil)
  2061. (const :tag "Record timestamp" time)
  2062. (const :tag "Record timestamp with note." note)))
  2063. (defcustom org-log-redeadline nil
  2064. "Information to record when the deadline date of a tasks is modified.
  2065. Possible values are:
  2066. nil Don't add anything, just change the date
  2067. time Add a time stamp to the task
  2068. note Prompt for a note and add it with template `org-log-note-headings'
  2069. This option can also be set with on a per-file-basis with
  2070. #+STARTUP: nologredeadline
  2071. #+STARTUP: logredeadline
  2072. #+STARTUP: lognoteredeadline
  2073. You can have local logging settings for a subtree by setting the LOGGING
  2074. property to one or more of these keywords."
  2075. :group 'org-todo
  2076. :group 'org-progress
  2077. :type '(choice
  2078. (const :tag "No logging" nil)
  2079. (const :tag "Record timestamp" time)
  2080. (const :tag "Record timestamp with note." note)))
  2081. (defcustom org-log-note-clock-out nil
  2082. "Non-nil means record a note when clocking out of an item.
  2083. This can also be configured on a per-file basis by adding one of
  2084. the following lines anywhere in the buffer:
  2085. #+STARTUP: lognoteclock-out
  2086. #+STARTUP: nolognoteclock-out"
  2087. :group 'org-todo
  2088. :group 'org-progress
  2089. :type 'boolean)
  2090. (defcustom org-log-done-with-time t
  2091. "Non-nil means the CLOSED time stamp will contain date and time.
  2092. When nil, only the date will be recorded."
  2093. :group 'org-progress
  2094. :type 'boolean)
  2095. (defcustom org-log-note-headings
  2096. '((done . "CLOSING NOTE %t")
  2097. (state . "State %-12s from %-12S %t")
  2098. (note . "Note taken on %t")
  2099. (reschedule . "Rescheduled from %S on %t")
  2100. (delschedule . "Not scheduled, was %S on %t")
  2101. (redeadline . "New deadline from %S on %t")
  2102. (deldeadline . "Removed deadline, was %S on %t")
  2103. (refile . "Refiled on %t")
  2104. (clock-out . ""))
  2105. "Headings for notes added to entries.
  2106. The value is an alist, with the car being a symbol indicating the note
  2107. context, and the cdr is the heading to be used. The heading may also be the
  2108. empty string.
  2109. %t in the heading will be replaced by a time stamp.
  2110. %T will be an active time stamp instead the default inactive one
  2111. %d will be replaced by a short-format time stamp.
  2112. %D will be replaced by an active short-format time stamp.
  2113. %s will be replaced by the new TODO state, in double quotes.
  2114. %S will be replaced by the old TODO state, in double quotes.
  2115. %u will be replaced by the user name.
  2116. %U will be replaced by the full user name.
  2117. In fact, it is not a good idea to change the `state' entry, because
  2118. agenda log mode depends on the format of these entries."
  2119. :group 'org-todo
  2120. :group 'org-progress
  2121. :type '(list :greedy t
  2122. (cons (const :tag "Heading when closing an item" done) string)
  2123. (cons (const :tag
  2124. "Heading when changing todo state (todo sequence only)"
  2125. state) string)
  2126. (cons (const :tag "Heading when just taking a note" note) string)
  2127. (cons (const :tag "Heading when clocking out" clock-out) string)
  2128. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2129. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2130. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2131. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2132. (cons (const :tag "Heading when refiling" refile) string)))
  2133. (unless (assq 'note org-log-note-headings)
  2134. (push '(note . "%t") org-log-note-headings))
  2135. (defcustom org-log-into-drawer nil
  2136. "Non-nil means insert state change notes and time stamps into a drawer.
  2137. When nil, state changes notes will be inserted after the headline and
  2138. any scheduling and clock lines, but not inside a drawer.
  2139. The value of this variable should be the name of the drawer to use.
  2140. LOGBOOK is proposed as the default drawer for this purpose, you can
  2141. also set this to a string to define the drawer of your choice.
  2142. A value of t is also allowed, representing \"LOGBOOK\".
  2143. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2144. will be ignored.
  2145. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2146. a subtree."
  2147. :group 'org-todo
  2148. :group 'org-progress
  2149. :type '(choice
  2150. (const :tag "Not into a drawer" nil)
  2151. (const :tag "LOGBOOK" t)
  2152. (string :tag "Other")))
  2153. (if (fboundp 'defvaralias)
  2154. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2155. (defun org-log-into-drawer ()
  2156. "Return the value of `org-log-into-drawer', but let properties overrule.
  2157. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2158. used instead of the default value."
  2159. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2160. (cond
  2161. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2162. ((equal p "t") "LOGBOOK")
  2163. (t p))))
  2164. (defcustom org-log-state-notes-insert-after-drawers nil
  2165. "Non-nil means insert state change notes after any drawers in entry.
  2166. Only the drawers that *immediately* follow the headline and the
  2167. deadline/scheduled line are skipped.
  2168. When nil, insert notes right after the heading and perhaps the line
  2169. with deadline/scheduling if present.
  2170. This variable will have no effect if `org-log-into-drawer' is
  2171. set."
  2172. :group 'org-todo
  2173. :group 'org-progress
  2174. :type 'boolean)
  2175. (defcustom org-log-states-order-reversed t
  2176. "Non-nil means the latest state note will be directly after heading.
  2177. When nil, the state change notes will be ordered according to time."
  2178. :group 'org-todo
  2179. :group 'org-progress
  2180. :type 'boolean)
  2181. (defcustom org-todo-repeat-to-state nil
  2182. "The TODO state to which a repeater should return the repeating task.
  2183. By default this is the first task in a TODO sequence, or the previous state
  2184. in a TODO_TYP set. But you can specify another task here.
  2185. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2186. :group 'org-todo
  2187. :type '(choice (const :tag "Head of sequence" nil)
  2188. (string :tag "Specific state")))
  2189. (defcustom org-log-repeat 'time
  2190. "Non-nil means record moving through the DONE state when triggering repeat.
  2191. An auto-repeating task is immediately switched back to TODO when
  2192. marked DONE. If you are not logging state changes (by adding \"@\"
  2193. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2194. record a closing note, there will be no record of the task moving
  2195. through DONE. This variable forces taking a note anyway.
  2196. nil Don't force a record
  2197. time Record a time stamp
  2198. note Record a note
  2199. This option can also be set with on a per-file-basis with
  2200. #+STARTUP: logrepeat
  2201. #+STARTUP: lognoterepeat
  2202. #+STARTUP: nologrepeat
  2203. You can have local logging settings for a subtree by setting the LOGGING
  2204. property to one or more of these keywords."
  2205. :group 'org-todo
  2206. :group 'org-progress
  2207. :type '(choice
  2208. (const :tag "Don't force a record" nil)
  2209. (const :tag "Force recording the DONE state" time)
  2210. (const :tag "Force recording a note with the DONE state" note)))
  2211. (defgroup org-priorities nil
  2212. "Priorities in Org-mode."
  2213. :tag "Org Priorities"
  2214. :group 'org-todo)
  2215. (defcustom org-enable-priority-commands t
  2216. "Non-nil means priority commands are active.
  2217. When nil, these commands will be disabled, so that you never accidentally
  2218. set a priority."
  2219. :group 'org-priorities
  2220. :type 'boolean)
  2221. (defcustom org-highest-priority ?A
  2222. "The highest priority of TODO items. A character like ?A, ?B etc.
  2223. Must have a smaller ASCII number than `org-lowest-priority'."
  2224. :group 'org-priorities
  2225. :type 'character)
  2226. (defcustom org-lowest-priority ?C
  2227. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2228. Must have a larger ASCII number than `org-highest-priority'."
  2229. :group 'org-priorities
  2230. :type 'character)
  2231. (defcustom org-default-priority ?B
  2232. "The default priority of TODO items.
  2233. This is the priority an item gets if no explicit priority is given.
  2234. When starting to cycle on an empty priority the first step in the cycle
  2235. depends on `org-priority-start-cycle-with-default'. The resulting first
  2236. step priority must not exceed the range from `org-highest-priority' to
  2237. `org-lowest-priority' which means that `org-default-priority' has to be
  2238. in this range exclusive or inclusive the range boundaries. Else the
  2239. first step refuses to set the default and the second will fall back
  2240. to (depending on the command used) the highest or lowest priority."
  2241. :group 'org-priorities
  2242. :type 'character)
  2243. (defcustom org-priority-start-cycle-with-default t
  2244. "Non-nil means start with default priority when starting to cycle.
  2245. When this is nil, the first step in the cycle will be (depending on the
  2246. command used) one higher or lower than the default priority.
  2247. See also `org-default-priority'."
  2248. :group 'org-priorities
  2249. :type 'boolean)
  2250. (defcustom org-get-priority-function nil
  2251. "Function to extract the priority from a string.
  2252. The string is normally the headline. If this is nil Org computes the
  2253. priority from the priority cookie like [#A] in the headline. It returns
  2254. an integer, increasing by 1000 for each priority level.
  2255. The user can set a different function here, which should take a string
  2256. as an argument and return the numeric priority."
  2257. :group 'org-priorities
  2258. :type 'function)
  2259. (defgroup org-time nil
  2260. "Options concerning time stamps and deadlines in Org-mode."
  2261. :tag "Org Time"
  2262. :group 'org)
  2263. (defcustom org-insert-labeled-timestamps-at-point nil
  2264. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2265. When nil, these labeled time stamps are forces into the second line of an
  2266. entry, just after the headline. When scheduling from the global TODO list,
  2267. the time stamp will always be forced into the second line."
  2268. :group 'org-time
  2269. :type 'boolean)
  2270. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2271. "Formats for `format-time-string' which are used for time stamps.
  2272. It is not recommended to change this constant.")
  2273. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2274. "Number of minutes to round time stamps to.
  2275. These are two values, the first applies when first creating a time stamp.
  2276. The second applies when changing it with the commands `S-up' and `S-down'.
  2277. When changing the time stamp, this means that it will change in steps
  2278. of N minutes, as given by the second value.
  2279. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2280. numbers should be factors of 60, so for example 5, 10, 15.
  2281. When this is larger than 1, you can still force an exact time stamp by using
  2282. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2283. and by using a prefix arg to `S-up/down' to specify the exact number
  2284. of minutes to shift."
  2285. :group 'org-time
  2286. :get #'(lambda (var) ; Make sure both elements are there
  2287. (if (integerp (default-value var))
  2288. (list (default-value var) 5)
  2289. (default-value var)))
  2290. :type '(list
  2291. (integer :tag "when inserting times")
  2292. (integer :tag "when modifying times")))
  2293. ;; Normalize old customizations of this variable.
  2294. (when (integerp org-time-stamp-rounding-minutes)
  2295. (setq org-time-stamp-rounding-minutes
  2296. (list org-time-stamp-rounding-minutes
  2297. org-time-stamp-rounding-minutes)))
  2298. (defcustom org-display-custom-times nil
  2299. "Non-nil means overlay custom formats over all time stamps.
  2300. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2301. To turn this on on a per-file basis, insert anywhere in the file:
  2302. #+STARTUP: customtime"
  2303. :group 'org-time
  2304. :set 'set-default
  2305. :type 'sexp)
  2306. (make-variable-buffer-local 'org-display-custom-times)
  2307. (defcustom org-time-stamp-custom-formats
  2308. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2309. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2310. These are overlaid over the default ISO format if the variable
  2311. `org-display-custom-times' is set. Time like %H:%M should be at the
  2312. end of the second format. The custom formats are also honored by export
  2313. commands, if custom time display is turned on at the time of export."
  2314. :group 'org-time
  2315. :type 'sexp)
  2316. (defun org-time-stamp-format (&optional long inactive)
  2317. "Get the right format for a time string."
  2318. (let ((f (if long (cdr org-time-stamp-formats)
  2319. (car org-time-stamp-formats))))
  2320. (if inactive
  2321. (concat "[" (substring f 1 -1) "]")
  2322. f)))
  2323. (defcustom org-time-clocksum-format "%d:%02d"
  2324. "The format string used when creating CLOCKSUM lines.
  2325. This is also used when org-mode generates a time duration."
  2326. :group 'org-time
  2327. :type 'string)
  2328. (defcustom org-time-clocksum-use-fractional nil
  2329. "If non-nil, \\[org-clock-display] uses fractional times.
  2330. org-mode generates a time duration."
  2331. :group 'org-time
  2332. :type 'boolean)
  2333. (defcustom org-time-clocksum-fractional-format "%.2f"
  2334. "The format string used when creating CLOCKSUM lines, or when
  2335. org-mode generates a time duration."
  2336. :group 'org-time
  2337. :type 'string)
  2338. (defcustom org-deadline-warning-days 14
  2339. "No. of days before expiration during which a deadline becomes active.
  2340. This variable governs the display in sparse trees and in the agenda.
  2341. When 0 or negative, it means use this number (the absolute value of it)
  2342. even if a deadline has a different individual lead time specified.
  2343. Custom commands can set this variable in the options section."
  2344. :group 'org-time
  2345. :group 'org-agenda-daily/weekly
  2346. :type 'integer)
  2347. (defcustom org-read-date-prefer-future t
  2348. "Non-nil means assume future for incomplete date input from user.
  2349. This affects the following situations:
  2350. 1. The user gives a month but not a year.
  2351. For example, if it is April and you enter \"feb 2\", this will be read
  2352. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2353. 2. The user gives a day, but no month.
  2354. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2355. read this as the third of *next* month. However, if you enter \"17\",
  2356. it will be considered as *this* month.
  2357. If you set this variable to the symbol `time', then also the following
  2358. will work:
  2359. 3. If the user gives a time, but no day. If the time is before now,
  2360. to will be interpreted as tomorrow.
  2361. Currently none of this works for ISO week specifications.
  2362. When this option is nil, the current day, month and year will always be
  2363. used as defaults.
  2364. See also `org-agenda-jump-prefer-future'."
  2365. :group 'org-time
  2366. :type '(choice
  2367. (const :tag "Never" nil)
  2368. (const :tag "Check month and day" t)
  2369. (const :tag "Check month, day, and time" time)))
  2370. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2371. "Should the agenda jump command prefer the future for incomplete dates?
  2372. The default is to do the same as configured in `org-read-date-prefer-future'.
  2373. But you can also set a deviating value here.
  2374. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2375. :group 'org-agenda
  2376. :group 'org-time
  2377. :type '(choice
  2378. (const :tag "Use org-read-date-prefer-future"
  2379. org-read-date-prefer-future)
  2380. (const :tag "Never" nil)
  2381. (const :tag "Always" t)))
  2382. (defcustom org-read-date-force-compatible-dates t
  2383. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2384. Depending on the system Emacs is running on, certain dates cannot
  2385. be represented with the type used internally to represent time.
  2386. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2387. correctly. Some systems allow for earlier dates, some for later,
  2388. some for both. One way to find out it to insert any date into an
  2389. Org buffer, putting the cursor on the year and hitting S-up and
  2390. S-down to test the range.
  2391. When this variable is set to t, the date/time prompt will not let
  2392. you specify dates outside the 1970-2037 range, so it is certain that
  2393. these dates will work in whatever version of Emacs you are
  2394. running, and also that you can move a file from one Emacs implementation
  2395. to another. WHenever Org is forcing the year for you, it will display
  2396. a message and beep.
  2397. When this variable is nil, Org will check if the date is
  2398. representable in the specific Emacs implementation you are using.
  2399. If not, it will force a year, usually the current year, and beep
  2400. to remind you. Currently this setting is not recommended because
  2401. the likelihood that you will open your Org files in an Emacs that
  2402. has limited date range is not negligible.
  2403. A workaround for this problem is to use diary sexp dates for time
  2404. stamps outside of this range."
  2405. :group 'org-time
  2406. :type 'boolean)
  2407. (defcustom org-read-date-display-live t
  2408. "Non-nil means display current interpretation of date prompt live.
  2409. This display will be in an overlay, in the minibuffer."
  2410. :group 'org-time
  2411. :type 'boolean)
  2412. (defcustom org-read-date-popup-calendar t
  2413. "Non-nil means pop up a calendar when prompting for a date.
  2414. In the calendar, the date can be selected with mouse-1. However, the
  2415. minibuffer will also be active, and you can simply enter the date as well.
  2416. When nil, only the minibuffer will be available."
  2417. :group 'org-time
  2418. :type 'boolean)
  2419. (if (fboundp 'defvaralias)
  2420. (defvaralias 'org-popup-calendar-for-date-prompt
  2421. 'org-read-date-popup-calendar))
  2422. (defcustom org-read-date-minibuffer-setup-hook nil
  2423. "Hook to be used to set up keys for the date/time interface.
  2424. Add key definitions to `minibuffer-local-map', which will be a temporary
  2425. copy."
  2426. :group 'org-time
  2427. :type 'hook)
  2428. (defcustom org-extend-today-until 0
  2429. "The hour when your day really ends. Must be an integer.
  2430. This has influence for the following applications:
  2431. - When switching the agenda to \"today\". It it is still earlier than
  2432. the time given here, the day recognized as TODAY is actually yesterday.
  2433. - When a date is read from the user and it is still before the time given
  2434. here, the current date and time will be assumed to be yesterday, 23:59.
  2435. Also, timestamps inserted in remember templates follow this rule.
  2436. IMPORTANT: This is a feature whose implementation is and likely will
  2437. remain incomplete. Really, it is only here because past midnight seems to
  2438. be the favorite working time of John Wiegley :-)"
  2439. :group 'org-time
  2440. :type 'integer)
  2441. (defcustom org-use-effective-time nil
  2442. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2443. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2444. \"effective time\" of any timestamps between midnight and 8am will be
  2445. 23:59 of the previous day."
  2446. :group 'boolean
  2447. :type 'integer)
  2448. (defcustom org-edit-timestamp-down-means-later nil
  2449. "Non-nil means S-down will increase the time in a time stamp.
  2450. When nil, S-up will increase."
  2451. :group 'org-time
  2452. :type 'boolean)
  2453. (defcustom org-calendar-follow-timestamp-change t
  2454. "Non-nil means make the calendar window follow timestamp changes.
  2455. When a timestamp is modified and the calendar window is visible, it will be
  2456. moved to the new date."
  2457. :group 'org-time
  2458. :type 'boolean)
  2459. (defgroup org-tags nil
  2460. "Options concerning tags in Org-mode."
  2461. :tag "Org Tags"
  2462. :group 'org)
  2463. (defcustom org-tag-alist nil
  2464. "List of tags allowed in Org-mode files.
  2465. When this list is nil, Org-mode will base TAG input on what is already in the
  2466. buffer.
  2467. The value of this variable is an alist, the car of each entry must be a
  2468. keyword as a string, the cdr may be a character that is used to select
  2469. that tag through the fast-tag-selection interface.
  2470. See the manual for details."
  2471. :group 'org-tags
  2472. :type '(repeat
  2473. (choice
  2474. (cons (string :tag "Tag name")
  2475. (character :tag "Access char"))
  2476. (list :tag "Start radio group"
  2477. (const :startgroup)
  2478. (option (string :tag "Group description")))
  2479. (list :tag "End radio group"
  2480. (const :endgroup)
  2481. (option (string :tag "Group description")))
  2482. (const :tag "New line" (:newline)))))
  2483. (defcustom org-tag-persistent-alist nil
  2484. "List of tags that will always appear in all Org-mode files.
  2485. This is in addition to any in buffer settings or customizations
  2486. of `org-tag-alist'.
  2487. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2488. The value of this variable is an alist, the car of each entry must be a
  2489. keyword as a string, the cdr may be a character that is used to select
  2490. that tag through the fast-tag-selection interface.
  2491. See the manual for details.
  2492. To disable these tags on a per-file basis, insert anywhere in the file:
  2493. #+STARTUP: noptag"
  2494. :group 'org-tags
  2495. :type '(repeat
  2496. (choice
  2497. (cons (string :tag "Tag name")
  2498. (character :tag "Access char"))
  2499. (const :tag "Start radio group" (:startgroup))
  2500. (const :tag "End radio group" (:endgroup))
  2501. (const :tag "New line" (:newline)))))
  2502. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2503. "If non-nil, always offer completion for all tags of all agenda files.
  2504. Instead of customizing this variable directly, you might want to
  2505. set it locally for capture buffers, because there no list of
  2506. tags in that file can be created dynamically (there are none).
  2507. (add-hook 'org-capture-mode-hook
  2508. (lambda ()
  2509. (set (make-local-variable
  2510. 'org-complete-tags-always-offer-all-agenda-tags)
  2511. t)))"
  2512. :group 'org-tags
  2513. :type 'boolean)
  2514. (defvar org-file-tags nil
  2515. "List of tags that can be inherited by all entries in the file.
  2516. The tags will be inherited if the variable `org-use-tag-inheritance'
  2517. says they should be.
  2518. This variable is populated from #+FILETAGS lines.")
  2519. (defcustom org-use-fast-tag-selection 'auto
  2520. "Non-nil means use fast tag selection scheme.
  2521. This is a special interface to select and deselect tags with single keys.
  2522. When nil, fast selection is never used.
  2523. When the symbol `auto', fast selection is used if and only if selection
  2524. characters for tags have been configured, either through the variable
  2525. `org-tag-alist' or through a #+TAGS line in the buffer.
  2526. When t, fast selection is always used and selection keys are assigned
  2527. automatically if necessary."
  2528. :group 'org-tags
  2529. :type '(choice
  2530. (const :tag "Always" t)
  2531. (const :tag "Never" nil)
  2532. (const :tag "When selection characters are configured" 'auto)))
  2533. (defcustom org-fast-tag-selection-single-key nil
  2534. "Non-nil means fast tag selection exits after first change.
  2535. When nil, you have to press RET to exit it.
  2536. During fast tag selection, you can toggle this flag with `C-c'.
  2537. This variable can also have the value `expert'. In this case, the window
  2538. displaying the tags menu is not even shown, until you press C-c again."
  2539. :group 'org-tags
  2540. :type '(choice
  2541. (const :tag "No" nil)
  2542. (const :tag "Yes" t)
  2543. (const :tag "Expert" expert)))
  2544. (defvar org-fast-tag-selection-include-todo nil
  2545. "Non-nil means fast tags selection interface will also offer TODO states.
  2546. This is an undocumented feature, you should not rely on it.")
  2547. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2548. "The column to which tags should be indented in a headline.
  2549. If this number is positive, it specifies the column. If it is negative,
  2550. it means that the tags should be flushright to that column. For example,
  2551. -80 works well for a normal 80 character screen.
  2552. When 0, place tags directly after headline text, with only one space in
  2553. between."
  2554. :group 'org-tags
  2555. :type 'integer)
  2556. (defcustom org-auto-align-tags t
  2557. "Non-nil keeps tags aligned when modifying headlines.
  2558. Some operations (i.e. demoting) change the length of a headline and
  2559. therefore shift the tags around. With this option turned on, after
  2560. each such operation the tags are again aligned to `org-tags-column'."
  2561. :group 'org-tags
  2562. :type 'boolean)
  2563. (defcustom org-use-tag-inheritance t
  2564. "Non-nil means tags in levels apply also for sublevels.
  2565. When nil, only the tags directly given in a specific line apply there.
  2566. This may also be a list of tags that should be inherited, or a regexp that
  2567. matches tags that should be inherited. Additional control is possible
  2568. with the variable `org-tags-exclude-from-inheritance' which gives an
  2569. explicit list of tags to be excluded from inheritance., even if the value of
  2570. `org-use-tag-inheritance' would select it for inheritance.
  2571. If this option is t, a match early-on in a tree can lead to a large
  2572. number of matches in the subtree when constructing the agenda or creating
  2573. a sparse tree. If you only want to see the first match in a tree during
  2574. a search, check out the variable `org-tags-match-list-sublevels'."
  2575. :group 'org-tags
  2576. :type '(choice
  2577. (const :tag "Not" nil)
  2578. (const :tag "Always" t)
  2579. (repeat :tag "Specific tags" (string :tag "Tag"))
  2580. (regexp :tag "Tags matched by regexp")))
  2581. (defcustom org-tags-exclude-from-inheritance nil
  2582. "List of tags that should never be inherited.
  2583. This is a way to exclude a few tags from inheritance. For way to do
  2584. the opposite, to actively allow inheritance for selected tags,
  2585. see the variable `org-use-tag-inheritance'."
  2586. :group 'org-tags
  2587. :type '(repeat (string :tag "Tag")))
  2588. (defun org-tag-inherit-p (tag)
  2589. "Check if TAG is one that should be inherited."
  2590. (cond
  2591. ((member tag org-tags-exclude-from-inheritance) nil)
  2592. ((eq org-use-tag-inheritance t) t)
  2593. ((not org-use-tag-inheritance) nil)
  2594. ((stringp org-use-tag-inheritance)
  2595. (string-match org-use-tag-inheritance tag))
  2596. ((listp org-use-tag-inheritance)
  2597. (member tag org-use-tag-inheritance))
  2598. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2599. (defcustom org-tags-match-list-sublevels t
  2600. "Non-nil means list also sublevels of headlines matching a search.
  2601. This variable applies to tags/property searches, and also to stuck
  2602. projects because this search is based on a tags match as well.
  2603. When set to the symbol `indented', sublevels are indented with
  2604. leading dots.
  2605. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2606. the sublevels of a headline matching a tag search often also match
  2607. the same search. Listing all of them can create very long lists.
  2608. Setting this variable to nil causes subtrees of a match to be skipped.
  2609. This variable is semi-obsolete and probably should always be true. It
  2610. is better to limit inheritance to certain tags using the variables
  2611. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2612. :group 'org-tags
  2613. :type '(choice
  2614. (const :tag "No, don't list them" nil)
  2615. (const :tag "Yes, do list them" t)
  2616. (const :tag "List them, indented with leading dots" indented)))
  2617. (defcustom org-tags-sort-function nil
  2618. "When set, tags are sorted using this function as a comparator."
  2619. :group 'org-tags
  2620. :type '(choice
  2621. (const :tag "No sorting" nil)
  2622. (const :tag "Alphabetical" string<)
  2623. (const :tag "Reverse alphabetical" string>)
  2624. (function :tag "Custom function" nil)))
  2625. (defvar org-tags-history nil
  2626. "History of minibuffer reads for tags.")
  2627. (defvar org-last-tags-completion-table nil
  2628. "The last used completion table for tags.")
  2629. (defvar org-after-tags-change-hook nil
  2630. "Hook that is run after the tags in a line have changed.")
  2631. (defgroup org-properties nil
  2632. "Options concerning properties in Org-mode."
  2633. :tag "Org Properties"
  2634. :group 'org)
  2635. (defcustom org-property-format "%-10s %s"
  2636. "How property key/value pairs should be formatted by `indent-line'.
  2637. When `indent-line' hits a property definition, it will format the line
  2638. according to this format, mainly to make sure that the values are
  2639. lined-up with respect to each other."
  2640. :group 'org-properties
  2641. :type 'string)
  2642. (defcustom org-properties-postprocess-alist nil
  2643. "Alist of properties and functions to adjust inserted values.
  2644. Elements of this alist must be of the form
  2645. ([string] [function])
  2646. where [string] must be a property name and [function] must be a
  2647. lambda expression: this lambda expression must take one argument,
  2648. the value to adjust, and return the new value as a string.
  2649. For example, this element will allow the property \"Remaining\"
  2650. to be updated wrt the relation between the \"Effort\" property
  2651. and the clock summary:
  2652. ((\"Remaining\" (lambda(value)
  2653. (let ((clocksum (org-clock-sum-current-item))
  2654. (effort (org-duration-string-to-minutes
  2655. (org-entry-get (point) \"Effort\"))))
  2656. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2657. :group 'org-properties
  2658. :type 'alist)
  2659. (defcustom org-use-property-inheritance nil
  2660. "Non-nil means properties apply also for sublevels.
  2661. This setting is chiefly used during property searches. Turning it on can
  2662. cause significant overhead when doing a search, which is why it is not
  2663. on by default.
  2664. When nil, only the properties directly given in the current entry count.
  2665. When t, every property is inherited. The value may also be a list of
  2666. properties that should have inheritance, or a regular expression matching
  2667. properties that should be inherited.
  2668. However, note that some special properties use inheritance under special
  2669. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2670. and the properties ending in \"_ALL\" when they are used as descriptor
  2671. for valid values of a property.
  2672. Note for programmers:
  2673. When querying an entry with `org-entry-get', you can control if inheritance
  2674. should be used. By default, `org-entry-get' looks only at the local
  2675. properties. You can request inheritance by setting the inherit argument
  2676. to t (to force inheritance) or to `selective' (to respect the setting
  2677. in this variable)."
  2678. :group 'org-properties
  2679. :type '(choice
  2680. (const :tag "Not" nil)
  2681. (const :tag "Always" t)
  2682. (repeat :tag "Specific properties" (string :tag "Property"))
  2683. (regexp :tag "Properties matched by regexp")))
  2684. (defun org-property-inherit-p (property)
  2685. "Check if PROPERTY is one that should be inherited."
  2686. (cond
  2687. ((eq org-use-property-inheritance t) t)
  2688. ((not org-use-property-inheritance) nil)
  2689. ((stringp org-use-property-inheritance)
  2690. (string-match org-use-property-inheritance property))
  2691. ((listp org-use-property-inheritance)
  2692. (member property org-use-property-inheritance))
  2693. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2694. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2695. "The default column format, if no other format has been defined.
  2696. This variable can be set on the per-file basis by inserting a line
  2697. #+COLUMNS: %25ITEM ....."
  2698. :group 'org-properties
  2699. :type 'string)
  2700. (defcustom org-columns-ellipses ".."
  2701. "The ellipses to be used when a field in column view is truncated.
  2702. When this is the empty string, as many characters as possible are shown,
  2703. but then there will be no visual indication that the field has been truncated.
  2704. When this is a string of length N, the last N characters of a truncated
  2705. field are replaced by this string. If the column is narrower than the
  2706. ellipses string, only part of the ellipses string will be shown."
  2707. :group 'org-properties
  2708. :type 'string)
  2709. (defcustom org-columns-modify-value-for-display-function nil
  2710. "Function that modifies values for display in column view.
  2711. For example, it can be used to cut out a certain part from a time stamp.
  2712. The function must take 2 arguments:
  2713. column-title The title of the column (*not* the property name)
  2714. value The value that should be modified.
  2715. The function should return the value that should be displayed,
  2716. or nil if the normal value should be used."
  2717. :group 'org-properties
  2718. :type 'function)
  2719. (defcustom org-effort-property "Effort"
  2720. "The property that is being used to keep track of effort estimates.
  2721. Effort estimates given in this property need to have the format H:MM."
  2722. :group 'org-properties
  2723. :group 'org-progress
  2724. :type '(string :tag "Property"))
  2725. (defconst org-global-properties-fixed
  2726. '(("VISIBILITY_ALL" . "folded children content all")
  2727. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2728. "List of property/value pairs that can be inherited by any entry.
  2729. These are fixed values, for the preset properties. The user variable
  2730. that can be used to add to this list is `org-global-properties'.
  2731. The entries in this list are cons cells where the car is a property
  2732. name and cdr is a string with the value. If the value represents
  2733. multiple items like an \"_ALL\" property, separate the items by
  2734. spaces.")
  2735. (defcustom org-global-properties nil
  2736. "List of property/value pairs that can be inherited by any entry.
  2737. This list will be combined with the constant `org-global-properties-fixed'.
  2738. The entries in this list are cons cells where the car is a property
  2739. name and cdr is a string with the value.
  2740. You can set buffer-local values for the same purpose in the variable
  2741. `org-file-properties' this by adding lines like
  2742. #+PROPERTY: NAME VALUE"
  2743. :group 'org-properties
  2744. :type '(repeat
  2745. (cons (string :tag "Property")
  2746. (string :tag "Value"))))
  2747. (defvar org-file-properties nil
  2748. "List of property/value pairs that can be inherited by any entry.
  2749. Valid for the current buffer.
  2750. This variable is populated from #+PROPERTY lines.")
  2751. (make-variable-buffer-local 'org-file-properties)
  2752. (defgroup org-agenda nil
  2753. "Options concerning agenda views in Org-mode."
  2754. :tag "Org Agenda"
  2755. :group 'org)
  2756. (defvar org-category nil
  2757. "Variable used by org files to set a category for agenda display.
  2758. Such files should use a file variable to set it, for example
  2759. # -*- mode: org; org-category: \"ELisp\"
  2760. or contain a special line
  2761. #+CATEGORY: ELisp
  2762. If the file does not specify a category, then file's base name
  2763. is used instead.")
  2764. (make-variable-buffer-local 'org-category)
  2765. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2766. (defcustom org-agenda-files nil
  2767. "The files to be used for agenda display.
  2768. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2769. \\[org-remove-file]. You can also use customize to edit the list.
  2770. If an entry is a directory, all files in that directory that are matched by
  2771. `org-agenda-file-regexp' will be part of the file list.
  2772. If the value of the variable is not a list but a single file name, then
  2773. the list of agenda files is actually stored and maintained in that file, one
  2774. agenda file per line. In this file paths can be given relative to
  2775. `org-directory'. Tilde expansion and environment variable substitution
  2776. are also made."
  2777. :group 'org-agenda
  2778. :type '(choice
  2779. (repeat :tag "List of files and directories" file)
  2780. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2781. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2782. "Regular expression to match files for `org-agenda-files'.
  2783. If any element in the list in that variable contains a directory instead
  2784. of a normal file, all files in that directory that are matched by this
  2785. regular expression will be included."
  2786. :group 'org-agenda
  2787. :type 'regexp)
  2788. (defcustom org-agenda-text-search-extra-files nil
  2789. "List of extra files to be searched by text search commands.
  2790. These files will be search in addition to the agenda files by the
  2791. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2792. Note that these files will only be searched for text search commands,
  2793. not for the other agenda views like todo lists, tag searches or the weekly
  2794. agenda. This variable is intended to list notes and possibly archive files
  2795. that should also be searched by these two commands.
  2796. In fact, if the first element in the list is the symbol `agenda-archives',
  2797. than all archive files of all agenda files will be added to the search
  2798. scope."
  2799. :group 'org-agenda
  2800. :type '(set :greedy t
  2801. (const :tag "Agenda Archives" agenda-archives)
  2802. (repeat :inline t (file))))
  2803. (if (fboundp 'defvaralias)
  2804. (defvaralias 'org-agenda-multi-occur-extra-files
  2805. 'org-agenda-text-search-extra-files))
  2806. (defcustom org-agenda-skip-unavailable-files nil
  2807. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2808. A nil value means to remove them, after a query, from the list."
  2809. :group 'org-agenda
  2810. :type 'boolean)
  2811. (defcustom org-calendar-to-agenda-key [?c]
  2812. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2813. The command `org-calendar-goto-agenda' will be bound to this key. The
  2814. default is the character `c' because then `c' can be used to switch back and
  2815. forth between agenda and calendar."
  2816. :group 'org-agenda
  2817. :type 'sexp)
  2818. (defcustom org-calendar-agenda-action-key [?k]
  2819. "The key to be installed in `calendar-mode-map' for agenda-action.
  2820. The command `org-agenda-action' will be bound to this key. The
  2821. default is the character `k' because we use the same key in the agenda."
  2822. :group 'org-agenda
  2823. :type 'sexp)
  2824. (defcustom org-calendar-insert-diary-entry-key [?i]
  2825. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2826. This option is irrelevant until `org-agenda-diary-file' has been configured
  2827. to point to an Org-mode file. When that is the case, the command
  2828. `org-agenda-diary-entry' will be bound to the key given here, by default
  2829. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2830. if you want to continue doing this, you need to change this to a different
  2831. key."
  2832. :group 'org-agenda
  2833. :type 'sexp)
  2834. (defcustom org-agenda-diary-file 'diary-file
  2835. "File to which to add new entries with the `i' key in agenda and calendar.
  2836. When this is the symbol `diary-file', the functionality in the Emacs
  2837. calendar will be used to add entries to the `diary-file'. But when this
  2838. points to a file, `org-agenda-diary-entry' will be used instead."
  2839. :group 'org-agenda
  2840. :type '(choice
  2841. (const :tag "The standard Emacs diary file" diary-file)
  2842. (file :tag "Special Org file diary entries")))
  2843. (eval-after-load "calendar"
  2844. '(progn
  2845. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2846. 'org-calendar-goto-agenda)
  2847. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2848. 'org-agenda-action)
  2849. (add-hook 'calendar-mode-hook
  2850. (lambda ()
  2851. (unless (eq org-agenda-diary-file 'diary-file)
  2852. (define-key calendar-mode-map
  2853. org-calendar-insert-diary-entry-key
  2854. 'org-agenda-diary-entry))))))
  2855. (defgroup org-latex nil
  2856. "Options for embedding LaTeX code into Org-mode."
  2857. :tag "Org LaTeX"
  2858. :group 'org)
  2859. (defcustom org-format-latex-options
  2860. '(:foreground default :background default :scale 1.0
  2861. :html-foreground "Black" :html-background "Transparent"
  2862. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2863. "Options for creating images from LaTeX fragments.
  2864. This is a property list with the following properties:
  2865. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2866. `default' means use the foreground of the default face.
  2867. :background the background color, or \"Transparent\".
  2868. `default' means use the background of the default face.
  2869. :scale a scaling factor for the size of the images, to get more pixels
  2870. :html-foreground, :html-background, :html-scale
  2871. the same numbers for HTML export.
  2872. :matchers a list indicating which matchers should be used to
  2873. find LaTeX fragments. Valid members of this list are:
  2874. \"begin\" find environments
  2875. \"$1\" find single characters surrounded by $.$
  2876. \"$\" find math expressions surrounded by $...$
  2877. \"$$\" find math expressions surrounded by $$....$$
  2878. \"\\(\" find math expressions surrounded by \\(...\\)
  2879. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2880. :group 'org-latex
  2881. :type 'plist)
  2882. (defcustom org-format-latex-signal-error t
  2883. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2884. When nil, just push out a message."
  2885. :group 'org-latex
  2886. :type 'boolean)
  2887. (defcustom org-latex-to-mathml-jar-file nil
  2888. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2889. Use this to specify additional executable file say a jar file.
  2890. When using MathToWeb as the converter, specify the full-path to
  2891. your mathtoweb.jar file."
  2892. :group 'org-latex
  2893. :type '(choice
  2894. (const :tag "None" nil)
  2895. (file :tag "JAR file" :must-match t)))
  2896. (defcustom org-latex-to-mathml-convert-command nil
  2897. "Command to convert LaTeX fragments to MathML.
  2898. Replace format-specifiers in the command as noted below and use
  2899. `shell-command' to convert LaTeX to MathML.
  2900. %j: Executable file in fully expanded form as specified by
  2901. `org-latex-to-mathml-jar-file'.
  2902. %I: Input LaTeX file in fully expanded form
  2903. %o: Output MathML file
  2904. This command is used by `org-create-math-formula'.
  2905. When using MathToWeb as the converter, set this to
  2906. \"java -jar %j -unicode -force -df %o %I\"."
  2907. :group 'org-latex
  2908. :type '(choice
  2909. (const :tag "None" nil)
  2910. (string :tag "\nShell command")))
  2911. (defun org-format-latex-mathml-available-p ()
  2912. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2913. (save-match-data
  2914. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2915. org-latex-to-mathml-convert-command)
  2916. (let ((executable (car (split-string
  2917. org-latex-to-mathml-convert-command))))
  2918. (when (executable-find executable)
  2919. (if (string-match
  2920. "%j" org-latex-to-mathml-convert-command)
  2921. (file-readable-p org-latex-to-mathml-jar-file)
  2922. t))))))
  2923. (defcustom org-format-latex-header "\\documentclass{article}
  2924. \\usepackage[usenames]{color}
  2925. \\usepackage{amsmath}
  2926. \\usepackage[mathscr]{eucal}
  2927. \\pagestyle{empty} % do not remove
  2928. \[PACKAGES]
  2929. \[DEFAULT-PACKAGES]
  2930. % The settings below are copied from fullpage.sty
  2931. \\setlength{\\textwidth}{\\paperwidth}
  2932. \\addtolength{\\textwidth}{-3cm}
  2933. \\setlength{\\oddsidemargin}{1.5cm}
  2934. \\addtolength{\\oddsidemargin}{-2.54cm}
  2935. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2936. \\setlength{\\textheight}{\\paperheight}
  2937. \\addtolength{\\textheight}{-\\headheight}
  2938. \\addtolength{\\textheight}{-\\headsep}
  2939. \\addtolength{\\textheight}{-\\footskip}
  2940. \\addtolength{\\textheight}{-3cm}
  2941. \\setlength{\\topmargin}{1.5cm}
  2942. \\addtolength{\\topmargin}{-2.54cm}"
  2943. "The document header used for processing LaTeX fragments.
  2944. It is imperative that this header make sure that no page number
  2945. appears on the page. The package defined in the variables
  2946. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2947. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2948. will be appended."
  2949. :group 'org-latex
  2950. :type 'string)
  2951. (defvar org-format-latex-header-extra nil)
  2952. (defun org-set-packages-alist (var val)
  2953. "Set the packages alist and make sure it has 3 elements per entry."
  2954. (set var (mapcar (lambda (x)
  2955. (if (and (consp x) (= (length x) 2))
  2956. (list (car x) (nth 1 x) t)
  2957. x))
  2958. val)))
  2959. (defun org-get-packages-alist (var)
  2960. "Get the packages alist and make sure it has 3 elements per entry."
  2961. (mapcar (lambda (x)
  2962. (if (and (consp x) (= (length x) 2))
  2963. (list (car x) (nth 1 x) t)
  2964. x))
  2965. (default-value var)))
  2966. ;; The following variables are defined here because is it also used
  2967. ;; when formatting latex fragments. Originally it was part of the
  2968. ;; LaTeX exporter, which is why the name includes "export".
  2969. (defcustom org-export-latex-default-packages-alist
  2970. '(("AUTO" "inputenc" t)
  2971. ("T1" "fontenc" t)
  2972. ("" "fixltx2e" nil)
  2973. ("" "graphicx" t)
  2974. ("" "longtable" nil)
  2975. ("" "float" nil)
  2976. ("" "wrapfig" nil)
  2977. ("" "soul" t)
  2978. ("" "textcomp" t)
  2979. ("" "marvosym" t)
  2980. ("" "wasysym" t)
  2981. ("" "latexsym" t)
  2982. ("" "amssymb" t)
  2983. ("" "hyperref" nil)
  2984. "\\tolerance=1000"
  2985. )
  2986. "Alist of default packages to be inserted in the header.
  2987. Change this only if one of the packages here causes an incompatibility
  2988. with another package you are using.
  2989. The packages in this list are needed by one part or another of Org-mode
  2990. to function properly.
  2991. - inputenc, fontenc: for basic font and character selection
  2992. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2993. for interpreting the entities in `org-entities'. You can skip some of these
  2994. packages if you don't use any of the symbols in it.
  2995. - graphicx: for including images
  2996. - float, wrapfig: for figure placement
  2997. - longtable: for long tables
  2998. - hyperref: for cross references
  2999. Therefore you should not modify this variable unless you know what you
  3000. are doing. The one reason to change it anyway is that you might be loading
  3001. some other package that conflicts with one of the default packages.
  3002. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3003. If SNIPPET-FLAG is t, the package also needs to be included when
  3004. compiling LaTeX snippets into images for inclusion into HTML."
  3005. :group 'org-export-latex
  3006. :set 'org-set-packages-alist
  3007. :get 'org-get-packages-alist
  3008. :type '(repeat
  3009. (choice
  3010. (list :tag "options/package pair"
  3011. (string :tag "options")
  3012. (string :tag "package")
  3013. (boolean :tag "Snippet"))
  3014. (string :tag "A line of LaTeX"))))
  3015. (defcustom org-export-latex-packages-alist nil
  3016. "Alist of packages to be inserted in every LaTeX header.
  3017. These will be inserted after `org-export-latex-default-packages-alist'.
  3018. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3019. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3020. turning LaTeX snippets into images for inclusion into HTML.
  3021. Make sure that you only list packages here which:
  3022. - you want in every file
  3023. - do not conflict with the default packages in
  3024. `org-export-latex-default-packages-alist'
  3025. - do not conflict with the setup in `org-format-latex-header'."
  3026. :group 'org-export-latex
  3027. :set 'org-set-packages-alist
  3028. :get 'org-get-packages-alist
  3029. :type '(repeat
  3030. (choice
  3031. (list :tag "options/package pair"
  3032. (string :tag "options")
  3033. (string :tag "package")
  3034. (boolean :tag "Snippet"))
  3035. (string :tag "A line of LaTeX"))))
  3036. (defgroup org-appearance nil
  3037. "Settings for Org-mode appearance."
  3038. :tag "Org Appearance"
  3039. :group 'org)
  3040. (defcustom org-level-color-stars-only nil
  3041. "Non-nil means fontify only the stars in each headline.
  3042. When nil, the entire headline is fontified.
  3043. Changing it requires restart of `font-lock-mode' to become effective
  3044. also in regions already fontified."
  3045. :group 'org-appearance
  3046. :type 'boolean)
  3047. (defcustom org-hide-leading-stars nil
  3048. "Non-nil means hide the first N-1 stars in a headline.
  3049. This works by using the face `org-hide' for these stars. This
  3050. face is white for a light background, and black for a dark
  3051. background. You may have to customize the face `org-hide' to
  3052. make this work.
  3053. Changing it requires restart of `font-lock-mode' to become effective
  3054. also in regions already fontified.
  3055. You may also set this on a per-file basis by adding one of the following
  3056. lines to the buffer:
  3057. #+STARTUP: hidestars
  3058. #+STARTUP: showstars"
  3059. :group 'org-appearance
  3060. :type 'boolean)
  3061. (defcustom org-hidden-keywords nil
  3062. "List of symbols corresponding to keywords to be hidden the org buffer.
  3063. For example, a value '(title) for this list will make the document's title
  3064. appear in the buffer without the initial #+TITLE: keyword."
  3065. :group 'org-appearance
  3066. :type '(set (const :tag "#+AUTHOR" author)
  3067. (const :tag "#+DATE" date)
  3068. (const :tag "#+EMAIL" email)
  3069. (const :tag "#+TITLE" title)))
  3070. (defcustom org-fontify-done-headline nil
  3071. "Non-nil means change the face of a headline if it is marked DONE.
  3072. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3073. When this is non-nil, the headline after the keyword is set to the
  3074. `org-headline-done' as an additional indication."
  3075. :group 'org-appearance
  3076. :type 'boolean)
  3077. (defcustom org-fontify-emphasized-text t
  3078. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3079. Changing this variable requires a restart of Emacs to take effect."
  3080. :group 'org-appearance
  3081. :type 'boolean)
  3082. (defcustom org-fontify-whole-heading-line nil
  3083. "Non-nil means fontify the whole line for headings.
  3084. This is useful when setting a background color for the
  3085. org-level-* faces."
  3086. :group 'org-appearance
  3087. :type 'boolean)
  3088. (defcustom org-highlight-latex-fragments-and-specials nil
  3089. "Non-nil means fontify what is treated specially by the exporters."
  3090. :group 'org-appearance
  3091. :type 'boolean)
  3092. (defcustom org-hide-emphasis-markers nil
  3093. "Non-nil mean font-lock should hide the emphasis marker characters."
  3094. :group 'org-appearance
  3095. :type 'boolean)
  3096. (defcustom org-pretty-entities nil
  3097. "Non-nil means show entities as UTF8 characters.
  3098. When nil, the \\name form remains in the buffer."
  3099. :group 'org-appearance
  3100. :type 'boolean)
  3101. (defcustom org-pretty-entities-include-sub-superscripts t
  3102. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3103. :group 'org-appearance
  3104. :type 'boolean)
  3105. (defvar org-emph-re nil
  3106. "Regular expression for matching emphasis.
  3107. After a match, the match groups contain these elements:
  3108. 0 The match of the full regular expression, including the characters
  3109. before and after the proper match
  3110. 1 The character before the proper match, or empty at beginning of line
  3111. 2 The proper match, including the leading and trailing markers
  3112. 3 The leading marker like * or /, indicating the type of highlighting
  3113. 4 The text between the emphasis markers, not including the markers
  3114. 5 The character after the match, empty at the end of a line")
  3115. (defvar org-verbatim-re nil
  3116. "Regular expression for matching verbatim text.")
  3117. (defvar org-emphasis-regexp-components) ; defined just below
  3118. (defvar org-emphasis-alist) ; defined just below
  3119. (defun org-set-emph-re (var val)
  3120. "Set variable and compute the emphasis regular expression."
  3121. (set var val)
  3122. (when (and (boundp 'org-emphasis-alist)
  3123. (boundp 'org-emphasis-regexp-components)
  3124. org-emphasis-alist org-emphasis-regexp-components)
  3125. (let* ((e org-emphasis-regexp-components)
  3126. (pre (car e))
  3127. (post (nth 1 e))
  3128. (border (nth 2 e))
  3129. (body (nth 3 e))
  3130. (nl (nth 4 e))
  3131. (body1 (concat body "*?"))
  3132. (markers (mapconcat 'car org-emphasis-alist ""))
  3133. (vmarkers (mapconcat
  3134. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3135. org-emphasis-alist "")))
  3136. ;; make sure special characters appear at the right position in the class
  3137. (if (string-match "\\^" markers)
  3138. (setq markers (concat (replace-match "" t t markers) "^")))
  3139. (if (string-match "-" markers)
  3140. (setq markers (concat (replace-match "" t t markers) "-")))
  3141. (if (string-match "\\^" vmarkers)
  3142. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3143. (if (string-match "-" vmarkers)
  3144. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3145. (if (> nl 0)
  3146. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3147. (int-to-string nl) "\\}")))
  3148. ;; Make the regexp
  3149. (setq org-emph-re
  3150. (concat "\\([" pre "]\\|^\\)"
  3151. "\\("
  3152. "\\([" markers "]\\)"
  3153. "\\("
  3154. "[^" border "]\\|"
  3155. "[^" border "]"
  3156. body1
  3157. "[^" border "]"
  3158. "\\)"
  3159. "\\3\\)"
  3160. "\\([" post "]\\|$\\)"))
  3161. (setq org-verbatim-re
  3162. (concat "\\([" pre "]\\|^\\)"
  3163. "\\("
  3164. "\\([" vmarkers "]\\)"
  3165. "\\("
  3166. "[^" border "]\\|"
  3167. "[^" border "]"
  3168. body1
  3169. "[^" border "]"
  3170. "\\)"
  3171. "\\3\\)"
  3172. "\\([" post "]\\|$\\)")))))
  3173. (defcustom org-emphasis-regexp-components
  3174. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3175. "Components used to build the regular expression for emphasis.
  3176. This is a list with five entries. Terminology: In an emphasis string
  3177. like \" *strong word* \", we call the initial space PREMATCH, the final
  3178. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3179. and \"trong wor\" is the body. The different components in this variable
  3180. specify what is allowed/forbidden in each part:
  3181. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3182. post Chars allowed as postmatch. End of line will be allowed too.
  3183. border The chars *forbidden* as border characters.
  3184. body-regexp A regexp like \".\" to match a body character. Don't use
  3185. non-shy groups here, and don't allow newline here.
  3186. newline The maximum number of newlines allowed in an emphasis exp.
  3187. Use customize to modify this, or restart Emacs after changing it."
  3188. :group 'org-appearance
  3189. :set 'org-set-emph-re
  3190. :type '(list
  3191. (sexp :tag "Allowed chars in pre ")
  3192. (sexp :tag "Allowed chars in post ")
  3193. (sexp :tag "Forbidden chars in border ")
  3194. (sexp :tag "Regexp for body ")
  3195. (integer :tag "number of newlines allowed")
  3196. (option (boolean :tag "Please ignore this button"))))
  3197. (defcustom org-emphasis-alist
  3198. `(("*" bold "<b>" "</b>")
  3199. ("/" italic "<i>" "</i>")
  3200. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3201. ("=" org-code "<code>" "</code>" verbatim)
  3202. ("~" org-verbatim "<code>" "</code>" verbatim)
  3203. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3204. "<del>" "</del>")
  3205. )
  3206. "Special syntax for emphasized text.
  3207. Text starting and ending with a special character will be emphasized, for
  3208. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3209. characters, the face to be used by font-lock for highlighting in Org-mode
  3210. Emacs buffers, and the HTML tags to be used for this.
  3211. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3212. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3213. Use customize to modify this, or restart Emacs after changing it."
  3214. :group 'org-appearance
  3215. :set 'org-set-emph-re
  3216. :type '(repeat
  3217. (list
  3218. (string :tag "Marker character")
  3219. (choice
  3220. (face :tag "Font-lock-face")
  3221. (plist :tag "Face property list"))
  3222. (string :tag "HTML start tag")
  3223. (string :tag "HTML end tag")
  3224. (option (const verbatim)))))
  3225. (defvar org-protecting-blocks
  3226. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3227. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3228. This is needed for font-lock setup.")
  3229. ;;; Miscellaneous options
  3230. (defgroup org-completion nil
  3231. "Completion in Org-mode."
  3232. :tag "Org Completion"
  3233. :group 'org)
  3234. (defcustom org-completion-use-ido nil
  3235. "Non-nil means use ido completion wherever possible.
  3236. Note that `ido-mode' must be active for this variable to be relevant.
  3237. If you decide to turn this variable on, you might well want to turn off
  3238. `org-outline-path-complete-in-steps'.
  3239. See also `org-completion-use-iswitchb'."
  3240. :group 'org-completion
  3241. :type 'boolean)
  3242. (defcustom org-completion-use-iswitchb nil
  3243. "Non-nil means use iswitchb completion wherever possible.
  3244. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3245. If you decide to turn this variable on, you might well want to turn off
  3246. `org-outline-path-complete-in-steps'.
  3247. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3248. :group 'org-completion
  3249. :type 'boolean)
  3250. (defcustom org-completion-fallback-command 'hippie-expand
  3251. "The expansion command called by \\[pcomplete] in normal context.
  3252. Normal means, no org-mode-specific context."
  3253. :group 'org-completion
  3254. :type 'function)
  3255. ;;; Functions and variables from their packages
  3256. ;; Declared here to avoid compiler warnings
  3257. ;; XEmacs only
  3258. (defvar outline-mode-menu-heading)
  3259. (defvar outline-mode-menu-show)
  3260. (defvar outline-mode-menu-hide)
  3261. (defvar zmacs-regions) ; XEmacs regions
  3262. ;; Emacs only
  3263. (defvar mark-active)
  3264. ;; Various packages
  3265. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3266. (declare-function calendar-forward-day "cal-move" (arg))
  3267. (declare-function calendar-goto-date "cal-move" (date))
  3268. (declare-function calendar-goto-today "cal-move" ())
  3269. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3270. (defvar calc-embedded-close-formula)
  3271. (defvar calc-embedded-open-formula)
  3272. (declare-function cdlatex-tab "ext:cdlatex" ())
  3273. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3274. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3275. (defvar font-lock-unfontify-region-function)
  3276. (declare-function iswitchb-read-buffer "iswitchb"
  3277. (prompt &optional default require-match start matches-set))
  3278. (defvar iswitchb-temp-buflist)
  3279. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3280. (defvar org-agenda-tags-todo-honor-ignore-options)
  3281. (declare-function org-agenda-skip "org-agenda" ())
  3282. (declare-function
  3283. org-agenda-format-item "org-agenda"
  3284. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3285. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3286. (declare-function org-agenda-change-all-lines "org-agenda"
  3287. (newhead hdmarker &optional fixface just-this))
  3288. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3289. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3290. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3291. (beg end))
  3292. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3293. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3294. "org-agenda" (&optional end))
  3295. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3296. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3297. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3298. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3299. (declare-function org-indent-mode "org-indent" (&optional arg))
  3300. (declare-function parse-time-string "parse-time" (string))
  3301. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3302. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3303. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3304. (defvar remember-data-file)
  3305. (defvar texmathp-why)
  3306. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3307. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3308. (defvar w3m-current-url)
  3309. (defvar w3m-current-title)
  3310. (defvar org-latex-regexps)
  3311. ;;; Autoload and prepare some org modules
  3312. ;; Some table stuff that needs to be defined here, because it is used
  3313. ;; by the functions setting up org-mode or checking for table context.
  3314. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3315. "Detect an org-type or table-type table.")
  3316. (defconst org-table-line-regexp "^[ \t]*|"
  3317. "Detect an org-type table line.")
  3318. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3319. "Detect an org-type table line.")
  3320. (defconst org-table-hline-regexp "^[ \t]*|-"
  3321. "Detect an org-type table hline.")
  3322. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3323. "Detect a table-type table hline.")
  3324. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3325. "Detect the first line outside a table when searching from within it.
  3326. This works for both table types.")
  3327. ;; Autoload the functions in org-table.el that are needed by functions here.
  3328. (eval-and-compile
  3329. (org-autoload "org-table"
  3330. '(org-table-align org-table-begin org-table-blank-field
  3331. org-table-convert org-table-convert-region org-table-copy-down
  3332. org-table-copy-region org-table-create
  3333. org-table-create-or-convert-from-region
  3334. org-table-create-with-table.el org-table-current-dline
  3335. org-table-cut-region org-table-delete-column org-table-edit-field
  3336. org-table-edit-formulas org-table-end org-table-eval-formula
  3337. org-table-export org-table-field-info
  3338. org-table-get-stored-formulas org-table-goto-column
  3339. org-table-hline-and-move org-table-import org-table-insert-column
  3340. org-table-insert-hline org-table-insert-row org-table-iterate
  3341. org-table-justify-field-maybe org-table-kill-row
  3342. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3343. org-table-move-column org-table-move-column-left
  3344. org-table-move-column-right org-table-move-row
  3345. org-table-move-row-down org-table-move-row-up
  3346. org-table-next-field org-table-next-row org-table-paste-rectangle
  3347. org-table-previous-field org-table-recalculate
  3348. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3349. org-table-toggle-coordinate-overlays
  3350. org-table-toggle-formula-debugger org-table-wrap-region
  3351. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3352. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3353. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3354. (defun org-at-table-p (&optional table-type)
  3355. "Return t if the cursor is inside an org-type table.
  3356. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3357. (if org-enable-table-editor
  3358. (save-excursion
  3359. (beginning-of-line 1)
  3360. (looking-at (if table-type org-table-any-line-regexp
  3361. org-table-line-regexp)))
  3362. nil))
  3363. (defsubst org-table-p () (org-at-table-p))
  3364. (defun org-at-table.el-p ()
  3365. "Return t if and only if we are at a table.el table."
  3366. (and (org-at-table-p 'any)
  3367. (save-excursion
  3368. (goto-char (org-table-begin 'any))
  3369. (looking-at org-table1-hline-regexp))))
  3370. (defun org-table-recognize-table.el ()
  3371. "If there is a table.el table nearby, recognize it and move into it."
  3372. (if org-table-tab-recognizes-table.el
  3373. (if (org-at-table.el-p)
  3374. (progn
  3375. (beginning-of-line 1)
  3376. (if (looking-at org-table-dataline-regexp)
  3377. nil
  3378. (if (looking-at org-table1-hline-regexp)
  3379. (progn
  3380. (beginning-of-line 2)
  3381. (if (looking-at org-table-any-border-regexp)
  3382. (beginning-of-line -1)))))
  3383. (if (re-search-forward "|" (org-table-end t) t)
  3384. (progn
  3385. (require 'table)
  3386. (if (table--at-cell-p (point))
  3387. t
  3388. (message "recognizing table.el table...")
  3389. (table-recognize-table)
  3390. (message "recognizing table.el table...done")))
  3391. (error "This should not happen"))
  3392. t)
  3393. nil)
  3394. nil))
  3395. (defun org-at-table-hline-p ()
  3396. "Return t if the cursor is inside a hline in a table."
  3397. (if org-enable-table-editor
  3398. (save-excursion
  3399. (beginning-of-line 1)
  3400. (looking-at org-table-hline-regexp))
  3401. nil))
  3402. (defvar org-table-clean-did-remove-column nil)
  3403. (defun org-table-map-tables (function &optional quietly)
  3404. "Apply FUNCTION to the start of all tables in the buffer."
  3405. (save-excursion
  3406. (save-restriction
  3407. (widen)
  3408. (goto-char (point-min))
  3409. (while (re-search-forward org-table-any-line-regexp nil t)
  3410. (unless quietly
  3411. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3412. (beginning-of-line 1)
  3413. (when (looking-at org-table-line-regexp)
  3414. (save-excursion (funcall function))
  3415. (or (looking-at org-table-line-regexp)
  3416. (forward-char 1)))
  3417. (re-search-forward org-table-any-border-regexp nil 1))))
  3418. (unless quietly (message "Mapping tables: done")))
  3419. ;; Declare and autoload functions from org-exp.el & Co
  3420. (declare-function org-default-export-plist "org-exp")
  3421. (declare-function org-infile-export-plist "org-exp")
  3422. (declare-function org-get-current-options "org-exp")
  3423. (eval-and-compile
  3424. (org-autoload "org-exp"
  3425. '(org-export org-export-visible
  3426. org-insert-export-options-template
  3427. org-table-clean-before-export))
  3428. (org-autoload "org-ascii"
  3429. '(org-export-as-ascii org-export-ascii-preprocess
  3430. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3431. org-export-region-as-ascii))
  3432. (org-autoload "org-latex"
  3433. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3434. org-replace-region-by-latex org-export-region-as-latex
  3435. org-export-as-latex org-export-as-pdf
  3436. org-export-as-pdf-and-open))
  3437. (org-autoload "org-html"
  3438. '(org-export-as-html-and-open
  3439. org-export-as-html-batch org-export-as-html-to-buffer
  3440. org-replace-region-by-html org-export-region-as-html
  3441. org-export-as-html))
  3442. (org-autoload "org-docbook"
  3443. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3444. org-replace-region-by-docbook org-export-region-as-docbook
  3445. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3446. org-export-as-docbook))
  3447. (org-autoload "org-icalendar"
  3448. '(org-export-icalendar-this-file
  3449. org-export-icalendar-all-agenda-files
  3450. org-export-icalendar-combine-agenda-files))
  3451. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3452. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3453. ;; Declare and autoload functions from org-agenda.el
  3454. (eval-and-compile
  3455. (org-autoload "org-agenda"
  3456. '(org-agenda org-agenda-list org-search-view
  3457. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3458. org-diary org-agenda-to-appt
  3459. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3460. ;; Autoload org-remember
  3461. (eval-and-compile
  3462. (org-autoload "org-remember"
  3463. '(org-remember-insinuate org-remember-annotation
  3464. org-remember-apply-template org-remember org-remember-handler)))
  3465. (eval-and-compile
  3466. (org-autoload "org-capture"
  3467. '(org-capture org-capture-insert-template-here
  3468. org-capture-import-remember-templates)))
  3469. ;; Autoload org-clock.el
  3470. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3471. (beg end))
  3472. (declare-function org-clock-update-mode-line "org-clock" ())
  3473. (declare-function org-resolve-clocks "org-clock"
  3474. (&optional also-non-dangling-p prompt last-valid))
  3475. (defvar org-clock-start-time)
  3476. (defvar org-clock-marker (make-marker)
  3477. "Marker recording the last clock-in.")
  3478. (defvar org-clock-hd-marker (make-marker)
  3479. "Marker recording the last clock-in, but the headline position.")
  3480. (defvar org-clock-heading ""
  3481. "The heading of the current clock entry.")
  3482. (defun org-clock-is-active ()
  3483. "Return non-nil if clock is currently running.
  3484. The return value is actually the clock marker."
  3485. (marker-buffer org-clock-marker))
  3486. (eval-and-compile
  3487. (org-autoload
  3488. "org-clock"
  3489. '(org-clock-in org-clock-out org-clock-cancel
  3490. org-clock-goto org-clock-sum org-clock-display
  3491. org-clock-remove-overlays org-clock-report
  3492. org-clocktable-shift org-dblock-write:clocktable
  3493. org-get-clocktable org-resolve-clocks)))
  3494. (defun org-clock-update-time-maybe ()
  3495. "If this is a CLOCK line, update it and return t.
  3496. Otherwise, return nil."
  3497. (interactive)
  3498. (save-excursion
  3499. (beginning-of-line 1)
  3500. (skip-chars-forward " \t")
  3501. (when (looking-at org-clock-string)
  3502. (let ((re (concat "[ \t]*" org-clock-string
  3503. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3504. "\\([ \t]*=>.*\\)?\\)?"))
  3505. ts te h m s neg)
  3506. (cond
  3507. ((not (looking-at re))
  3508. nil)
  3509. ((not (match-end 2))
  3510. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3511. (> org-clock-marker (point))
  3512. (<= org-clock-marker (point-at-eol)))
  3513. ;; The clock is running here
  3514. (setq org-clock-start-time
  3515. (apply 'encode-time
  3516. (org-parse-time-string (match-string 1))))
  3517. (org-clock-update-mode-line)))
  3518. (t
  3519. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3520. (end-of-line 1)
  3521. (setq ts (match-string 1)
  3522. te (match-string 3))
  3523. (setq s (- (org-float-time
  3524. (apply 'encode-time (org-parse-time-string te)))
  3525. (org-float-time
  3526. (apply 'encode-time (org-parse-time-string ts))))
  3527. neg (< s 0)
  3528. s (abs s)
  3529. h (floor (/ s 3600))
  3530. s (- s (* 3600 h))
  3531. m (floor (/ s 60))
  3532. s (- s (* 60 s)))
  3533. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3534. t))))))
  3535. (defun org-check-running-clock ()
  3536. "Check if the current buffer contains the running clock.
  3537. If yes, offer to stop it and to save the buffer with the changes."
  3538. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3539. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3540. (buffer-name))))
  3541. (org-clock-out)
  3542. (when (y-or-n-p "Save changed buffer?")
  3543. (save-buffer))))
  3544. (defun org-clocktable-try-shift (dir n)
  3545. "Check if this line starts a clock table, if yes, shift the time block."
  3546. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3547. (org-clocktable-shift dir n)))
  3548. ;; Autoload org-timer.el
  3549. (eval-and-compile
  3550. (org-autoload
  3551. "org-timer"
  3552. '(org-timer-start org-timer org-timer-item
  3553. org-timer-change-times-in-region
  3554. org-timer-set-timer
  3555. org-timer-reset-timers
  3556. org-timer-show-remaining-time)))
  3557. ;; Autoload org-feed.el
  3558. (eval-and-compile
  3559. (org-autoload
  3560. "org-feed"
  3561. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3562. ;; Autoload org-indent.el
  3563. ;; Define the variable already here, to make sure we have it.
  3564. (defvar org-indent-mode nil
  3565. "Non-nil if Org-Indent mode is enabled.
  3566. Use the command `org-indent-mode' to change this variable.")
  3567. (eval-and-compile
  3568. (org-autoload
  3569. "org-indent"
  3570. '(org-indent-mode)))
  3571. ;; Autoload org-mobile.el
  3572. (eval-and-compile
  3573. (org-autoload
  3574. "org-mobile"
  3575. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3576. ;; Autoload archiving code
  3577. ;; The stuff that is needed for cycling and tags has to be defined here.
  3578. (defgroup org-archive nil
  3579. "Options concerning archiving in Org-mode."
  3580. :tag "Org Archive"
  3581. :group 'org-structure)
  3582. (defcustom org-archive-location "%s_archive::"
  3583. "The location where subtrees should be archived.
  3584. The value of this variable is a string, consisting of two parts,
  3585. separated by a double-colon. The first part is a filename and
  3586. the second part is a headline.
  3587. When the filename is omitted, archiving happens in the same file.
  3588. %s in the filename will be replaced by the current file
  3589. name (without the directory part). Archiving to a different file
  3590. is useful to keep archived entries from contributing to the
  3591. Org-mode Agenda.
  3592. The archived entries will be filed as subtrees of the specified
  3593. headline. When the headline is omitted, the subtrees are simply
  3594. filed away at the end of the file, as top-level entries. Also in
  3595. the heading you can use %s to represent the file name, this can be
  3596. useful when using the same archive for a number of different files.
  3597. Here are a few examples:
  3598. \"%s_archive::\"
  3599. If the current file is Projects.org, archive in file
  3600. Projects.org_archive, as top-level trees. This is the default.
  3601. \"::* Archived Tasks\"
  3602. Archive in the current file, under the top-level headline
  3603. \"* Archived Tasks\".
  3604. \"~/org/archive.org::\"
  3605. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3606. \"~/org/archive.org::* From %s\"
  3607. Archive in file ~/org/archive.org (absolute path), under headlines
  3608. \"From FILENAME\" where file name is the current file name.
  3609. \"basement::** Finished Tasks\"
  3610. Archive in file ./basement (relative path), as level 3 trees
  3611. below the level 2 heading \"** Finished Tasks\".
  3612. You may set this option on a per-file basis by adding to the buffer a
  3613. line like
  3614. #+ARCHIVE: basement::** Finished Tasks
  3615. You may also define it locally for a subtree by setting an ARCHIVE property
  3616. in the entry. If such a property is found in an entry, or anywhere up
  3617. the hierarchy, it will be used."
  3618. :group 'org-archive
  3619. :type 'string)
  3620. (defcustom org-archive-tag "ARCHIVE"
  3621. "The tag that marks a subtree as archived.
  3622. An archived subtree does not open during visibility cycling, and does
  3623. not contribute to the agenda listings.
  3624. After changing this, font-lock must be restarted in the relevant buffers to
  3625. get the proper fontification."
  3626. :group 'org-archive
  3627. :group 'org-keywords
  3628. :type 'string)
  3629. (defcustom org-agenda-skip-archived-trees t
  3630. "Non-nil means the agenda will skip any items located in archived trees.
  3631. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3632. variable is no longer recommended, you should leave it at the value t.
  3633. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3634. :group 'org-archive
  3635. :group 'org-agenda-skip
  3636. :type 'boolean)
  3637. (defcustom org-columns-skip-archived-trees t
  3638. "Non-nil means ignore archived trees when creating column view."
  3639. :group 'org-archive
  3640. :group 'org-properties
  3641. :type 'boolean)
  3642. (defcustom org-cycle-open-archived-trees nil
  3643. "Non-nil means `org-cycle' will open archived trees.
  3644. An archived tree is a tree marked with the tag ARCHIVE.
  3645. When nil, archived trees will stay folded. You can still open them with
  3646. normal outline commands like `show-all', but not with the cycling commands."
  3647. :group 'org-archive
  3648. :group 'org-cycle
  3649. :type 'boolean)
  3650. (defcustom org-sparse-tree-open-archived-trees nil
  3651. "Non-nil means sparse tree construction shows matches in archived trees.
  3652. When nil, matches in these trees are highlighted, but the trees are kept in
  3653. collapsed state."
  3654. :group 'org-archive
  3655. :group 'org-sparse-trees
  3656. :type 'boolean)
  3657. (defun org-cycle-hide-archived-subtrees (state)
  3658. "Re-hide all archived subtrees after a visibility state change."
  3659. (when (and (not org-cycle-open-archived-trees)
  3660. (not (memq state '(overview folded))))
  3661. (save-excursion
  3662. (let* ((globalp (memq state '(contents all)))
  3663. (beg (if globalp (point-min) (point)))
  3664. (end (if globalp (point-max) (org-end-of-subtree t))))
  3665. (org-hide-archived-subtrees beg end)
  3666. (goto-char beg)
  3667. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3668. (message "%s" (substitute-command-keys
  3669. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3670. (defun org-force-cycle-archived ()
  3671. "Cycle subtree even if it is archived."
  3672. (interactive)
  3673. (setq this-command 'org-cycle)
  3674. (let ((org-cycle-open-archived-trees t))
  3675. (call-interactively 'org-cycle)))
  3676. (defun org-hide-archived-subtrees (beg end)
  3677. "Re-hide all archived subtrees after a visibility state change."
  3678. (save-excursion
  3679. (let* ((re (concat ":" org-archive-tag ":")))
  3680. (goto-char beg)
  3681. (while (re-search-forward re end t)
  3682. (when (org-at-heading-p)
  3683. (org-flag-subtree t)
  3684. (org-end-of-subtree t))))))
  3685. (defun org-flag-subtree (flag)
  3686. (save-excursion
  3687. (org-back-to-heading t)
  3688. (outline-end-of-heading)
  3689. (outline-flag-region (point)
  3690. (progn (org-end-of-subtree t) (point))
  3691. flag)))
  3692. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3693. (eval-and-compile
  3694. (org-autoload "org-archive"
  3695. '(org-add-archive-files org-archive-subtree
  3696. org-archive-to-archive-sibling org-toggle-archive-tag
  3697. org-archive-subtree-default
  3698. org-archive-subtree-default-with-confirmation)))
  3699. ;; Autoload Column View Code
  3700. (declare-function org-columns-number-to-string "org-colview")
  3701. (declare-function org-columns-get-format-and-top-level "org-colview")
  3702. (declare-function org-columns-compute "org-colview")
  3703. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3704. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3705. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3706. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3707. ;; Autoload ID code
  3708. (declare-function org-id-store-link "org-id")
  3709. (declare-function org-id-locations-load "org-id")
  3710. (declare-function org-id-locations-save "org-id")
  3711. (defvar org-id-track-globally)
  3712. (org-autoload "org-id"
  3713. '(org-id-get-create org-id-new org-id-copy org-id-get
  3714. org-id-get-with-outline-path-completion
  3715. org-id-get-with-outline-drilling org-id-store-link
  3716. org-id-goto org-id-find org-id-store-link))
  3717. ;; Autoload Plotting Code
  3718. (org-autoload "org-plot"
  3719. '(org-plot/gnuplot))
  3720. ;;; Variables for pre-computed regular expressions, all buffer local
  3721. (defvar org-drawer-regexp nil
  3722. "Matches first line of a hidden block.")
  3723. (make-variable-buffer-local 'org-drawer-regexp)
  3724. (defvar org-todo-regexp nil
  3725. "Matches any of the TODO state keywords.")
  3726. (make-variable-buffer-local 'org-todo-regexp)
  3727. (defvar org-not-done-regexp nil
  3728. "Matches any of the TODO state keywords except the last one.")
  3729. (make-variable-buffer-local 'org-not-done-regexp)
  3730. (defvar org-not-done-heading-regexp nil
  3731. "Matches a TODO headline that is not done.")
  3732. (make-variable-buffer-local 'org-not-done-regexp)
  3733. (defvar org-todo-line-regexp nil
  3734. "Matches a headline and puts TODO state into group 2 if present.")
  3735. (make-variable-buffer-local 'org-todo-line-regexp)
  3736. (defvar org-complex-heading-regexp nil
  3737. "Matches a headline and puts everything into groups:
  3738. group 1: the stars
  3739. group 2: The todo keyword, maybe
  3740. group 3: Priority cookie
  3741. group 4: True headline
  3742. group 5: Tags")
  3743. (make-variable-buffer-local 'org-complex-heading-regexp)
  3744. (defvar org-complex-heading-regexp-format nil
  3745. "Printf format to make regexp to match an exact headline.
  3746. This regexp will match the headline of any node which has the
  3747. exact headline text that is put into the format, but may have any
  3748. TODO state, priority and tags.")
  3749. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3750. (defvar org-todo-line-tags-regexp nil
  3751. "Matches a headline and puts TODO state into group 2 if present.
  3752. Also put tags into group 4 if tags are present.")
  3753. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3754. (defvar org-ds-keyword-length 12
  3755. "Maximum length of the Deadline and SCHEDULED keywords.")
  3756. (make-variable-buffer-local 'org-ds-keyword-length)
  3757. (defvar org-deadline-regexp nil
  3758. "Matches the DEADLINE keyword.")
  3759. (make-variable-buffer-local 'org-deadline-regexp)
  3760. (defvar org-deadline-time-regexp nil
  3761. "Matches the DEADLINE keyword together with a time stamp.")
  3762. (make-variable-buffer-local 'org-deadline-time-regexp)
  3763. (defvar org-deadline-line-regexp nil
  3764. "Matches the DEADLINE keyword and the rest of the line.")
  3765. (make-variable-buffer-local 'org-deadline-line-regexp)
  3766. (defvar org-scheduled-regexp nil
  3767. "Matches the SCHEDULED keyword.")
  3768. (make-variable-buffer-local 'org-scheduled-regexp)
  3769. (defvar org-scheduled-time-regexp nil
  3770. "Matches the SCHEDULED keyword together with a time stamp.")
  3771. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3772. (defvar org-closed-time-regexp nil
  3773. "Matches the CLOSED keyword together with a time stamp.")
  3774. (make-variable-buffer-local 'org-closed-time-regexp)
  3775. (defvar org-keyword-time-regexp nil
  3776. "Matches any of the 4 keywords, together with the time stamp.")
  3777. (make-variable-buffer-local 'org-keyword-time-regexp)
  3778. (defvar org-keyword-time-not-clock-regexp nil
  3779. "Matches any of the 3 keywords, together with the time stamp.")
  3780. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3781. (defvar org-maybe-keyword-time-regexp nil
  3782. "Matches a timestamp, possibly preceded by a keyword.")
  3783. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3784. (defvar org-planning-or-clock-line-re nil
  3785. "Matches a line with planning or clock info.")
  3786. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3787. (defvar org-all-time-keywords nil
  3788. "List of time keywords.")
  3789. (make-variable-buffer-local 'org-all-time-keywords)
  3790. (defconst org-plain-time-of-day-regexp
  3791. (concat
  3792. "\\(\\<[012]?[0-9]"
  3793. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3794. "\\(--?"
  3795. "\\(\\<[012]?[0-9]"
  3796. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3797. "\\)?")
  3798. "Regular expression to match a plain time or time range.
  3799. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3800. groups carry important information:
  3801. 0 the full match
  3802. 1 the first time, range or not
  3803. 8 the second time, if it is a range.")
  3804. (defconst org-plain-time-extension-regexp
  3805. (concat
  3806. "\\(\\<[012]?[0-9]"
  3807. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3808. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3809. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3810. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3811. groups carry important information:
  3812. 0 the full match
  3813. 7 hours of duration
  3814. 9 minutes of duration")
  3815. (defconst org-stamp-time-of-day-regexp
  3816. (concat
  3817. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3818. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3819. "\\(--?"
  3820. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3821. "Regular expression to match a timestamp time or time range.
  3822. After a match, the following groups carry important information:
  3823. 0 the full match
  3824. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3825. 2 the first time, range or not
  3826. 4 the second time, if it is a range.")
  3827. (defconst org-startup-options
  3828. '(("fold" org-startup-folded t)
  3829. ("overview" org-startup-folded t)
  3830. ("nofold" org-startup-folded nil)
  3831. ("showall" org-startup-folded nil)
  3832. ("showeverything" org-startup-folded showeverything)
  3833. ("content" org-startup-folded content)
  3834. ("indent" org-startup-indented t)
  3835. ("noindent" org-startup-indented nil)
  3836. ("hidestars" org-hide-leading-stars t)
  3837. ("showstars" org-hide-leading-stars nil)
  3838. ("odd" org-odd-levels-only t)
  3839. ("oddeven" org-odd-levels-only nil)
  3840. ("align" org-startup-align-all-tables t)
  3841. ("noalign" org-startup-align-all-tables nil)
  3842. ("inlineimages" org-startup-with-inline-images t)
  3843. ("noinlineimages" org-startup-with-inline-images nil)
  3844. ("customtime" org-display-custom-times t)
  3845. ("logdone" org-log-done time)
  3846. ("lognotedone" org-log-done note)
  3847. ("nologdone" org-log-done nil)
  3848. ("lognoteclock-out" org-log-note-clock-out t)
  3849. ("nolognoteclock-out" org-log-note-clock-out nil)
  3850. ("logrepeat" org-log-repeat state)
  3851. ("lognoterepeat" org-log-repeat note)
  3852. ("nologrepeat" org-log-repeat nil)
  3853. ("logreschedule" org-log-reschedule time)
  3854. ("lognotereschedule" org-log-reschedule note)
  3855. ("nologreschedule" org-log-reschedule nil)
  3856. ("logredeadline" org-log-redeadline time)
  3857. ("lognoteredeadline" org-log-redeadline note)
  3858. ("nologredeadline" org-log-redeadline nil)
  3859. ("logrefile" org-log-refile time)
  3860. ("lognoterefile" org-log-refile note)
  3861. ("nologrefile" org-log-refile nil)
  3862. ("fninline" org-footnote-define-inline t)
  3863. ("nofninline" org-footnote-define-inline nil)
  3864. ("fnlocal" org-footnote-section nil)
  3865. ("fnauto" org-footnote-auto-label t)
  3866. ("fnprompt" org-footnote-auto-label nil)
  3867. ("fnconfirm" org-footnote-auto-label confirm)
  3868. ("fnplain" org-footnote-auto-label plain)
  3869. ("fnadjust" org-footnote-auto-adjust t)
  3870. ("nofnadjust" org-footnote-auto-adjust nil)
  3871. ("constcgs" constants-unit-system cgs)
  3872. ("constSI" constants-unit-system SI)
  3873. ("noptag" org-tag-persistent-alist nil)
  3874. ("hideblocks" org-hide-block-startup t)
  3875. ("nohideblocks" org-hide-block-startup nil)
  3876. ("beamer" org-startup-with-beamer-mode t)
  3877. ("entitiespretty" org-pretty-entities t)
  3878. ("entitiesplain" org-pretty-entities nil))
  3879. "Variable associated with STARTUP options for org-mode.
  3880. Each element is a list of three items: The startup options as written
  3881. in the #+STARTUP line, the corresponding variable, and the value to
  3882. set this variable to if the option is found. An optional forth element PUSH
  3883. means to push this value onto the list in the variable.")
  3884. (defun org-update-property-plist (key val props)
  3885. "Update PROPS with KEY and VAL."
  3886. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3887. (key (if appending (substring key 0 (- (length key) 1)) key))
  3888. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3889. (previous (cdr (assoc key props))))
  3890. (if appending
  3891. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3892. (cons (cons key val) remainder))))
  3893. (defconst org-block-regexp
  3894. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3895. "Regular expression for hiding blocks.")
  3896. (defconst org-heading-keyword-regexp-format
  3897. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3898. "Printf format for a regexp matching an headline with some keyword.
  3899. This regexp will match the headline of any node which has the
  3900. exact keyword that is put into the format. The keyword isn't in
  3901. any group by default, but the stars and the body are.")
  3902. (defconst org-heading-keyword-maybe-regexp-format
  3903. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3904. "Printf format for a regexp matching an headline, possibly with some keyword.
  3905. This regexp can match any headline with the specified keyword, or
  3906. without a keyword. The keyword isn't in any group by default,
  3907. but the stars and the body are.")
  3908. (defun org-set-regexps-and-options ()
  3909. "Precompute regular expressions for current buffer."
  3910. (when (eq major-mode 'org-mode)
  3911. (org-set-local 'org-todo-kwd-alist nil)
  3912. (org-set-local 'org-todo-key-alist nil)
  3913. (org-set-local 'org-todo-key-trigger nil)
  3914. (org-set-local 'org-todo-keywords-1 nil)
  3915. (org-set-local 'org-done-keywords nil)
  3916. (org-set-local 'org-todo-heads nil)
  3917. (org-set-local 'org-todo-sets nil)
  3918. (org-set-local 'org-todo-log-states nil)
  3919. (org-set-local 'org-file-properties nil)
  3920. (org-set-local 'org-file-tags nil)
  3921. (let ((re (org-make-options-regexp
  3922. '("CATEGORY" "TODO" "COLUMNS"
  3923. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3924. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3925. "OPTIONS")
  3926. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3927. (splitre "[ \t]+")
  3928. (scripts org-use-sub-superscripts)
  3929. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3930. tail sep kws1 prio props ftags drawers beamer-p
  3931. ext-setup-or-nil setup-contents (start 0))
  3932. (save-excursion
  3933. (save-restriction
  3934. (widen)
  3935. (goto-char (point-min))
  3936. (while (or (and ext-setup-or-nil
  3937. (string-match re ext-setup-or-nil start)
  3938. (setq start (match-end 0)))
  3939. (and (setq ext-setup-or-nil nil start 0)
  3940. (re-search-forward re nil t)))
  3941. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3942. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3943. (if (stringp value) (setq value (org-trim value)))
  3944. (cond
  3945. ((equal key "CATEGORY")
  3946. (setq cat value))
  3947. ((member key '("SEQ_TODO" "TODO"))
  3948. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3949. ((equal key "TYP_TODO")
  3950. (push (cons 'type (org-split-string value splitre)) kwds))
  3951. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3952. ;; general TODO-like setup
  3953. (push (cons (intern (downcase (match-string 1 key)))
  3954. (org-split-string value splitre)) kwds))
  3955. ((equal key "TAGS")
  3956. (setq tags (append tags (if tags '("\\n") nil)
  3957. (org-split-string value splitre))))
  3958. ((equal key "COLUMNS")
  3959. (org-set-local 'org-columns-default-format value))
  3960. ((equal key "LINK")
  3961. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3962. (push (cons (match-string 1 value)
  3963. (org-trim (match-string 2 value)))
  3964. links)))
  3965. ((equal key "PRIORITIES")
  3966. (setq prio (org-split-string value " +")))
  3967. ((equal key "PROPERTY")
  3968. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3969. (setq props (org-update-property-plist (match-string 1 value)
  3970. (match-string 2 value)
  3971. props))))
  3972. ((equal key "FILETAGS")
  3973. (when (string-match "\\S-" value)
  3974. (setq ftags
  3975. (append
  3976. ftags
  3977. (apply 'append
  3978. (mapcar (lambda (x) (org-split-string x ":"))
  3979. (org-split-string value)))))))
  3980. ((equal key "DRAWERS")
  3981. (setq drawers (org-split-string value splitre)))
  3982. ((equal key "CONSTANTS")
  3983. (setq const (append const (org-split-string value splitre))))
  3984. ((equal key "STARTUP")
  3985. (let ((opts (org-split-string value splitre))
  3986. l var val)
  3987. (while (setq l (pop opts))
  3988. (when (setq l (assoc l org-startup-options))
  3989. (setq var (nth 1 l) val (nth 2 l))
  3990. (if (not (nth 3 l))
  3991. (set (make-local-variable var) val)
  3992. (if (not (listp (symbol-value var)))
  3993. (set (make-local-variable var) nil))
  3994. (set (make-local-variable var) (symbol-value var))
  3995. (add-to-list var val))))))
  3996. ((equal key "ARCHIVE")
  3997. (setq arch value)
  3998. (remove-text-properties 0 (length arch)
  3999. '(face t fontified t) arch))
  4000. ((equal key "LATEX_CLASS")
  4001. (setq beamer-p (equal value "beamer")))
  4002. ((equal key "OPTIONS")
  4003. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4004. (setq scripts (read (match-string 2 value)))))
  4005. ((equal key "SETUPFILE")
  4006. (setq setup-contents (org-file-contents
  4007. (expand-file-name
  4008. (org-remove-double-quotes value))
  4009. 'noerror))
  4010. (if (not ext-setup-or-nil)
  4011. (setq ext-setup-or-nil setup-contents start 0)
  4012. (setq ext-setup-or-nil
  4013. (concat (substring ext-setup-or-nil 0 start)
  4014. "\n" setup-contents "\n"
  4015. (substring ext-setup-or-nil start)))))))
  4016. ;; search for property blocks
  4017. (goto-char (point-min))
  4018. (while (re-search-forward org-block-regexp nil t)
  4019. (when (equal "PROPERTY" (upcase (match-string 1)))
  4020. (setq value (replace-regexp-in-string
  4021. "[\n\r]" " " (match-string 4)))
  4022. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4023. (setq props (org-update-property-plist (match-string 1 value)
  4024. (match-string 2 value)
  4025. props)))))))
  4026. (org-set-local 'org-use-sub-superscripts scripts)
  4027. (when cat
  4028. (org-set-local 'org-category (intern cat))
  4029. (push (cons "CATEGORY" cat) props))
  4030. (when prio
  4031. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4032. (setq prio (mapcar 'string-to-char prio))
  4033. (org-set-local 'org-highest-priority (nth 0 prio))
  4034. (org-set-local 'org-lowest-priority (nth 1 prio))
  4035. (org-set-local 'org-default-priority (nth 2 prio)))
  4036. (and props (org-set-local 'org-file-properties (nreverse props)))
  4037. (and ftags (org-set-local 'org-file-tags
  4038. (mapcar 'org-add-prop-inherited ftags)))
  4039. (and drawers (org-set-local 'org-drawers drawers))
  4040. (and arch (org-set-local 'org-archive-location arch))
  4041. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4042. ;; Process the TODO keywords
  4043. (unless kwds
  4044. ;; Use the global values as if they had been given locally.
  4045. (setq kwds (default-value 'org-todo-keywords))
  4046. (if (stringp (car kwds))
  4047. (setq kwds (list (cons org-todo-interpretation
  4048. (default-value 'org-todo-keywords)))))
  4049. (setq kwds (reverse kwds)))
  4050. (setq kwds (nreverse kwds))
  4051. (let (inter kws kw)
  4052. (while (setq kws (pop kwds))
  4053. (let ((kws (or
  4054. (run-hook-with-args-until-success
  4055. 'org-todo-setup-filter-hook kws)
  4056. kws)))
  4057. (setq inter (pop kws) sep (member "|" kws)
  4058. kws0 (delete "|" (copy-sequence kws))
  4059. kwsa nil
  4060. kws1 (mapcar
  4061. (lambda (x)
  4062. ;; 1 2
  4063. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4064. (progn
  4065. (setq kw (match-string 1 x)
  4066. key (and (match-end 2) (match-string 2 x))
  4067. log (org-extract-log-state-settings x))
  4068. (push (cons kw (and key (string-to-char key))) kwsa)
  4069. (and log (push log org-todo-log-states))
  4070. kw)
  4071. (error "Invalid TODO keyword %s" x)))
  4072. kws0)
  4073. kwsa (if kwsa (append '((:startgroup))
  4074. (nreverse kwsa)
  4075. '((:endgroup))))
  4076. hw (car kws1)
  4077. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4078. tail (list inter hw (car dws) (org-last dws))))
  4079. (add-to-list 'org-todo-heads hw 'append)
  4080. (push kws1 org-todo-sets)
  4081. (setq org-done-keywords (append org-done-keywords dws nil))
  4082. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4083. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4084. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4085. (setq org-todo-sets (nreverse org-todo-sets)
  4086. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4087. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4088. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4089. ;; Process the constants
  4090. (when const
  4091. (let (e cst)
  4092. (while (setq e (pop const))
  4093. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4094. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4095. (setq org-table-formula-constants-local cst)))
  4096. ;; Process the tags.
  4097. (when tags
  4098. (let (e tgs)
  4099. (while (setq e (pop tags))
  4100. (cond
  4101. ((equal e "{") (push '(:startgroup) tgs))
  4102. ((equal e "}") (push '(:endgroup) tgs))
  4103. ((equal e "\\n") (push '(:newline) tgs))
  4104. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4105. (push (cons (match-string 1 e)
  4106. (string-to-char (match-string 2 e)))
  4107. tgs))
  4108. (t (push (list e) tgs))))
  4109. (org-set-local 'org-tag-alist nil)
  4110. (while (setq e (pop tgs))
  4111. (or (and (stringp (car e))
  4112. (assoc (car e) org-tag-alist))
  4113. (push e org-tag-alist)))))
  4114. ;; Compute the regular expressions and other local variables.
  4115. ;; Using `org-outline-regexp-bol' would complicate them much,
  4116. ;; because of the fixed white space at the end of that string.
  4117. (if (not org-done-keywords)
  4118. (setq org-done-keywords (and org-todo-keywords-1
  4119. (list (org-last org-todo-keywords-1)))))
  4120. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4121. (length org-scheduled-string)
  4122. (length org-clock-string)
  4123. (length org-closed-string)))
  4124. org-drawer-regexp
  4125. (concat "^[ \t]*:\\("
  4126. (mapconcat 'regexp-quote org-drawers "\\|")
  4127. "\\):[ \t]*$")
  4128. org-not-done-keywords
  4129. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4130. org-todo-regexp
  4131. (concat "\\("
  4132. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4133. "\\)")
  4134. org-not-done-regexp
  4135. (concat "\\("
  4136. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4137. "\\)")
  4138. org-not-done-heading-regexp
  4139. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4140. org-todo-line-regexp
  4141. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4142. org-complex-heading-regexp
  4143. (concat "^\\(\\*+\\)"
  4144. "\\(?: +" org-todo-regexp "\\)?"
  4145. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4146. "\\(?: +\\(.*?\\)\\)?"
  4147. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4148. "[ \t]*$")
  4149. org-complex-heading-regexp-format
  4150. (concat "^\\(\\*+\\)"
  4151. "\\(?: +" org-todo-regexp "\\)?"
  4152. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4153. "\\(?: +"
  4154. ;; Stats cookies can be stuck to body.
  4155. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4156. "\\(%s\\)"
  4157. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4158. "\\)"
  4159. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4160. "[ \t]*$")
  4161. org-todo-line-tags-regexp
  4162. (concat "^\\(\\*+\\)"
  4163. "\\(?: +" org-todo-regexp "\\)?"
  4164. "\\(?: +\\(.*?\\)\\)?"
  4165. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4166. "[ \t]*$")
  4167. org-deadline-regexp (concat "\\<" org-deadline-string)
  4168. org-deadline-time-regexp
  4169. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4170. org-deadline-line-regexp
  4171. (concat "\\<\\(" org-deadline-string "\\).*")
  4172. org-scheduled-regexp
  4173. (concat "\\<" org-scheduled-string)
  4174. org-scheduled-time-regexp
  4175. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4176. org-closed-time-regexp
  4177. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4178. org-keyword-time-regexp
  4179. (concat "\\<\\(" org-scheduled-string
  4180. "\\|" org-deadline-string
  4181. "\\|" org-closed-string
  4182. "\\|" org-clock-string "\\)"
  4183. " *[[<]\\([^]>]+\\)[]>]")
  4184. org-keyword-time-not-clock-regexp
  4185. (concat "\\<\\(" org-scheduled-string
  4186. "\\|" org-deadline-string
  4187. "\\|" org-closed-string
  4188. "\\)"
  4189. " *[[<]\\([^]>]+\\)[]>]")
  4190. org-maybe-keyword-time-regexp
  4191. (concat "\\(\\<\\(" org-scheduled-string
  4192. "\\|" org-deadline-string
  4193. "\\|" org-closed-string
  4194. "\\|" org-clock-string "\\)\\)?"
  4195. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4196. org-planning-or-clock-line-re
  4197. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4198. "\\|" org-deadline-string
  4199. "\\|" org-closed-string "\\|" org-clock-string
  4200. "\\)\\>\\)")
  4201. org-all-time-keywords
  4202. (mapcar (lambda (w) (substring w 0 -1))
  4203. (list org-scheduled-string org-deadline-string
  4204. org-clock-string org-closed-string))
  4205. )
  4206. (org-compute-latex-and-specials-regexp)
  4207. (org-set-font-lock-defaults))))
  4208. (defun org-file-contents (file &optional noerror)
  4209. "Return the contents of FILE, as a string."
  4210. (if (or (not file)
  4211. (not (file-readable-p file)))
  4212. (if noerror
  4213. (progn
  4214. (message "Cannot read file \"%s\"" file)
  4215. (ding) (sit-for 2)
  4216. "")
  4217. (error "Cannot read file \"%s\"" file))
  4218. (with-temp-buffer
  4219. (insert-file-contents file)
  4220. (buffer-string))))
  4221. (defun org-extract-log-state-settings (x)
  4222. "Extract the log state setting from a TODO keyword string.
  4223. This will extract info from a string like \"WAIT(w@/!)\"."
  4224. (let (kw key log1 log2)
  4225. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4226. (setq kw (match-string 1 x)
  4227. key (and (match-end 2) (match-string 2 x))
  4228. log1 (and (match-end 3) (match-string 3 x))
  4229. log2 (and (match-end 4) (match-string 4 x)))
  4230. (and (or log1 log2)
  4231. (list kw
  4232. (and log1 (if (equal log1 "!") 'time 'note))
  4233. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4234. (defun org-remove-keyword-keys (list)
  4235. "Remove a pair of parenthesis at the end of each string in LIST."
  4236. (mapcar (lambda (x)
  4237. (if (string-match "(.*)$" x)
  4238. (substring x 0 (match-beginning 0))
  4239. x))
  4240. list))
  4241. (defun org-assign-fast-keys (alist)
  4242. "Assign fast keys to a keyword-key alist.
  4243. Respect keys that are already there."
  4244. (let (new e (alt ?0))
  4245. (while (setq e (pop alist))
  4246. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4247. (cdr e)) ;; Key already assigned.
  4248. (push e new)
  4249. (let ((clist (string-to-list (downcase (car e))))
  4250. (used (append new alist)))
  4251. (when (= (car clist) ?@)
  4252. (pop clist))
  4253. (while (and clist (rassoc (car clist) used))
  4254. (pop clist))
  4255. (unless clist
  4256. (while (rassoc alt used)
  4257. (incf alt)))
  4258. (push (cons (car e) (or (car clist) alt)) new))))
  4259. (nreverse new)))
  4260. ;;; Some variables used in various places
  4261. (defvar org-window-configuration nil
  4262. "Used in various places to store a window configuration.")
  4263. (defvar org-selected-window nil
  4264. "Used in various places to store a window configuration.")
  4265. (defvar org-finish-function nil
  4266. "Function to be called when `C-c C-c' is used.
  4267. This is for getting out of special buffers like remember.")
  4268. ;; FIXME: Occasionally check by commenting these, to make sure
  4269. ;; no other functions uses these, forgetting to let-bind them.
  4270. (defvar entry)
  4271. (defvar last-state)
  4272. (defvar date)
  4273. ;; Defined somewhere in this file, but used before definition.
  4274. (defvar org-entities) ;; defined in org-entities.el
  4275. (defvar org-struct-menu)
  4276. (defvar org-org-menu)
  4277. (defvar org-tbl-menu)
  4278. ;;;; Define the Org-mode
  4279. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4280. (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"))
  4281. ;; We use a before-change function to check if a table might need
  4282. ;; an update.
  4283. (defvar org-table-may-need-update t
  4284. "Indicates that a table might need an update.
  4285. This variable is set by `org-before-change-function'.
  4286. `org-table-align' sets it back to nil.")
  4287. (defun org-before-change-function (beg end)
  4288. "Every change indicates that a table might need an update."
  4289. (setq org-table-may-need-update t))
  4290. (defvar org-mode-map)
  4291. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4292. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4293. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4294. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4295. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4296. (defvar org-table-buffer-is-an nil)
  4297. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4298. ;; some places -- e.g. see the macro org-with-limited-levels.
  4299. ;;
  4300. ;; In Org buffers, the value of `outline-regexp' is that of
  4301. ;; `org-outline-regexp'. The only function still directly relying on
  4302. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4303. ;; job when `orgstruct-mode' is active.
  4304. (defvar org-outline-regexp "\\*+ "
  4305. "Regexp to match Org headlines.")
  4306. (defconst org-outline-regexp-bol "^\\*+ "
  4307. "Regexp to match Org headlines.
  4308. This is similar to `org-outline-regexp' but additionally makes
  4309. sure that we are at the beginning of the line.")
  4310. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4311. "Matches an headline, putting stars and text into groups.
  4312. Stars are put in group 1 and the trimmed body in group 2.")
  4313. ;;;###autoload
  4314. (define-derived-mode org-mode outline-mode "Org"
  4315. "Outline-based notes management and organizer, alias
  4316. \"Carsten's outline-mode for keeping track of everything.\"
  4317. Org-mode develops organizational tasks around a NOTES file which
  4318. contains information about projects as plain text. Org-mode is
  4319. implemented on top of outline-mode, which is ideal to keep the content
  4320. of large files well structured. It supports ToDo items, deadlines and
  4321. time stamps, which magically appear in the diary listing of the Emacs
  4322. calendar. Tables are easily created with a built-in table editor.
  4323. Plain text URL-like links connect to websites, emails (VM), Usenet
  4324. messages (Gnus), BBDB entries, and any files related to the project.
  4325. For printing and sharing of notes, an Org-mode file (or a part of it)
  4326. can be exported as a structured ASCII or HTML file.
  4327. The following commands are available:
  4328. \\{org-mode-map}"
  4329. ;; Get rid of Outline menus, they are not needed
  4330. ;; Need to do this here because define-derived-mode sets up
  4331. ;; the keymap so late. Still, it is a waste to call this each time
  4332. ;; we switch another buffer into org-mode.
  4333. (if (featurep 'xemacs)
  4334. (when (boundp 'outline-mode-menu-heading)
  4335. ;; Assume this is Greg's port, it uses easymenu
  4336. (easy-menu-remove outline-mode-menu-heading)
  4337. (easy-menu-remove outline-mode-menu-show)
  4338. (easy-menu-remove outline-mode-menu-hide))
  4339. (define-key org-mode-map [menu-bar headings] 'undefined)
  4340. (define-key org-mode-map [menu-bar hide] 'undefined)
  4341. (define-key org-mode-map [menu-bar show] 'undefined))
  4342. (org-load-modules-maybe)
  4343. (easy-menu-add org-org-menu)
  4344. (easy-menu-add org-tbl-menu)
  4345. (org-install-agenda-files-menu)
  4346. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4347. (add-to-invisibility-spec '(org-cwidth))
  4348. (add-to-invisibility-spec '(org-hide-block . t))
  4349. (when (featurep 'xemacs)
  4350. (org-set-local 'line-move-ignore-invisible t))
  4351. (org-set-local 'outline-regexp org-outline-regexp)
  4352. (org-set-local 'outline-level 'org-outline-level)
  4353. (setq bidi-paragraph-direction 'left-to-right)
  4354. (when (and org-ellipsis
  4355. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4356. (fboundp 'make-glyph-code))
  4357. (unless org-display-table
  4358. (setq org-display-table (make-display-table)))
  4359. (set-display-table-slot
  4360. org-display-table 4
  4361. (vconcat (mapcar
  4362. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4363. org-ellipsis)))
  4364. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4365. (setq buffer-display-table org-display-table))
  4366. (org-set-regexps-and-options)
  4367. (when (and org-tag-faces (not org-tags-special-faces-re))
  4368. ;; tag faces set outside customize.... force initialization.
  4369. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4370. ;; Calc embedded
  4371. (org-set-local 'calc-embedded-open-mode "# ")
  4372. (modify-syntax-entry ?@ "w")
  4373. (if org-startup-truncated (setq truncate-lines t))
  4374. (org-set-local 'font-lock-unfontify-region-function
  4375. 'org-unfontify-region)
  4376. ;; Activate before-change-function
  4377. (org-set-local 'org-table-may-need-update t)
  4378. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4379. 'local)
  4380. ;; Check for running clock before killing a buffer
  4381. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4382. ;; Paragraphs and auto-filling
  4383. (org-set-autofill-regexps)
  4384. (setq indent-line-function 'org-indent-line-function)
  4385. (org-update-radio-target-regexp)
  4386. ;; Beginning/end of defun
  4387. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4388. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4389. ;; Next error for sparse trees
  4390. (org-set-local 'next-error-function 'org-occur-next-match)
  4391. ;; Make sure dependence stuff works reliably, even for users who set it
  4392. ;; too late :-(
  4393. (if org-enforce-todo-dependencies
  4394. (add-hook 'org-blocker-hook
  4395. 'org-block-todo-from-children-or-siblings-or-parent)
  4396. (remove-hook 'org-blocker-hook
  4397. 'org-block-todo-from-children-or-siblings-or-parent))
  4398. (if org-enforce-todo-checkbox-dependencies
  4399. (add-hook 'org-blocker-hook
  4400. 'org-block-todo-from-checkboxes)
  4401. (remove-hook 'org-blocker-hook
  4402. 'org-block-todo-from-checkboxes))
  4403. ;; Comment characters
  4404. (org-set-local 'comment-start "#")
  4405. (org-set-local 'comment-padding " ")
  4406. ;; Align options lines
  4407. (org-set-local
  4408. 'align-mode-rules-list
  4409. '((org-in-buffer-settings
  4410. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4411. (modes . '(org-mode)))))
  4412. ;; Imenu
  4413. (org-set-local 'imenu-create-index-function
  4414. 'org-imenu-get-tree)
  4415. ;; Make isearch reveal context
  4416. (if (or (featurep 'xemacs)
  4417. (not (boundp 'outline-isearch-open-invisible-function)))
  4418. ;; Emacs 21 and XEmacs make use of the hook
  4419. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4420. ;; Emacs 22 deals with this through a special variable
  4421. (org-set-local 'outline-isearch-open-invisible-function
  4422. (lambda (&rest ignore) (org-show-context 'isearch))))
  4423. ;; Turn on org-beamer-mode?
  4424. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4425. ;; Setup the pcomplete hooks
  4426. (set (make-local-variable 'pcomplete-command-completion-function)
  4427. 'org-pcomplete-initial)
  4428. (set (make-local-variable 'pcomplete-command-name-function)
  4429. 'org-command-at-point)
  4430. (set (make-local-variable 'pcomplete-default-completion-function)
  4431. 'ignore)
  4432. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4433. 'org-parse-arguments)
  4434. (set (make-local-variable 'pcomplete-termination-string) "")
  4435. (set (make-local-variable 'face-remapping-alist)
  4436. '((default org-default)))
  4437. ;; If empty file that did not turn on org-mode automatically, make it to.
  4438. (if (and org-insert-mode-line-in-empty-file
  4439. (org-called-interactively-p 'any)
  4440. (= (point-min) (point-max)))
  4441. (insert "# -*- mode: org -*-\n\n"))
  4442. (unless org-inhibit-startup
  4443. (when org-startup-align-all-tables
  4444. (let ((bmp (buffer-modified-p)))
  4445. (org-table-map-tables 'org-table-align 'quietly)
  4446. (set-buffer-modified-p bmp)))
  4447. (when org-startup-with-inline-images
  4448. (org-display-inline-images))
  4449. (when org-startup-indented
  4450. (require 'org-indent)
  4451. (org-indent-mode 1))
  4452. (unless org-inhibit-startup-visibility-stuff
  4453. (org-set-startup-visibility))))
  4454. (when (fboundp 'abbrev-table-put)
  4455. (abbrev-table-put org-mode-abbrev-table
  4456. :parents (list text-mode-abbrev-table)))
  4457. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4458. (defun org-current-time ()
  4459. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4460. (if (> (car org-time-stamp-rounding-minutes) 1)
  4461. (let ((r (car org-time-stamp-rounding-minutes))
  4462. (time (decode-time)))
  4463. (apply 'encode-time
  4464. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4465. (nthcdr 2 time))))
  4466. (current-time)))
  4467. (defun org-today ()
  4468. "Return today date, considering `org-extend-today-until'."
  4469. (time-to-days
  4470. (time-subtract (current-time)
  4471. (list 0 (* 3600 org-extend-today-until) 0))))
  4472. ;;;; Font-Lock stuff, including the activators
  4473. (defvar org-mouse-map (make-sparse-keymap))
  4474. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4475. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4476. (when org-mouse-1-follows-link
  4477. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4478. (when org-tab-follows-link
  4479. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4480. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4481. (require 'font-lock)
  4482. (defconst org-non-link-chars "]\t\n\r<>")
  4483. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4484. "shell" "elisp" "doi" "message"))
  4485. (defvar org-link-types-re nil
  4486. "Matches a link that has a url-like prefix like \"http:\"")
  4487. (defvar org-link-re-with-space nil
  4488. "Matches a link with spaces, optional angular brackets around it.")
  4489. (defvar org-link-re-with-space2 nil
  4490. "Matches a link with spaces, optional angular brackets around it.")
  4491. (defvar org-link-re-with-space3 nil
  4492. "Matches a link with spaces, only for internal part in bracket links.")
  4493. (defvar org-angle-link-re nil
  4494. "Matches link with angular brackets, spaces are allowed.")
  4495. (defvar org-plain-link-re nil
  4496. "Matches plain link, without spaces.")
  4497. (defvar org-bracket-link-regexp nil
  4498. "Matches a link in double brackets.")
  4499. (defvar org-bracket-link-analytic-regexp nil
  4500. "Regular expression used to analyze links.
  4501. Here is what the match groups contain after a match:
  4502. 1: http:
  4503. 2: http
  4504. 3: path
  4505. 4: [desc]
  4506. 5: desc")
  4507. (defvar org-bracket-link-analytic-regexp++ nil
  4508. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4509. (defvar org-any-link-re nil
  4510. "Regular expression matching any link.")
  4511. (defcustom org-match-sexp-depth 3
  4512. "Number of stacked braces for sub/superscript matching.
  4513. This has to be set before loading org.el to be effective."
  4514. :group 'org-export-translation ; ??????????????????????????/
  4515. :type 'integer)
  4516. (defun org-create-multibrace-regexp (left right n)
  4517. "Create a regular expression which will match a balanced sexp.
  4518. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4519. as single character strings.
  4520. The regexp returned will match the entire expression including the
  4521. delimiters. It will also define a single group which contains the
  4522. match except for the outermost delimiters. The maximum depth of
  4523. stacked delimiters is N. Escaping delimiters is not possible."
  4524. (let* ((nothing (concat "[^" left right "]*?"))
  4525. (or "\\|")
  4526. (re nothing)
  4527. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4528. (while (> n 1)
  4529. (setq n (1- n)
  4530. re (concat re or next)
  4531. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4532. (concat left "\\(" re "\\)" right)))
  4533. (defvar org-match-substring-regexp
  4534. (concat
  4535. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4536. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4537. "\\|"
  4538. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4539. "\\|"
  4540. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4541. "The regular expression matching a sub- or superscript.")
  4542. (defvar org-match-substring-with-braces-regexp
  4543. (concat
  4544. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4545. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4546. "\\)")
  4547. "The regular expression matching a sub- or superscript, forcing braces.")
  4548. (defun org-make-link-regexps ()
  4549. "Update the link regular expressions.
  4550. This should be called after the variable `org-link-types' has changed."
  4551. (setq org-link-types-re
  4552. (concat
  4553. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4554. org-link-re-with-space
  4555. (concat
  4556. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4557. "\\([^" org-non-link-chars " ]"
  4558. "[^" org-non-link-chars "]*"
  4559. "[^" org-non-link-chars " ]\\)>?")
  4560. org-link-re-with-space2
  4561. (concat
  4562. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4563. "\\([^" org-non-link-chars " ]"
  4564. "[^\t\n\r]*"
  4565. "[^" org-non-link-chars " ]\\)>?")
  4566. org-link-re-with-space3
  4567. (concat
  4568. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4569. "\\([^" org-non-link-chars " ]"
  4570. "[^\t\n\r]*\\)")
  4571. org-angle-link-re
  4572. (concat
  4573. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4574. "\\([^" org-non-link-chars " ]"
  4575. "[^" org-non-link-chars "]*"
  4576. "\\)>")
  4577. org-plain-link-re
  4578. (concat
  4579. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4580. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4581. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4582. org-bracket-link-regexp
  4583. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4584. org-bracket-link-analytic-regexp
  4585. (concat
  4586. "\\[\\["
  4587. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4588. "\\([^]]+\\)"
  4589. "\\]"
  4590. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4591. "\\]")
  4592. org-bracket-link-analytic-regexp++
  4593. (concat
  4594. "\\[\\["
  4595. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4596. "\\([^]]+\\)"
  4597. "\\]"
  4598. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4599. "\\]")
  4600. org-any-link-re
  4601. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4602. org-angle-link-re "\\)\\|\\("
  4603. org-plain-link-re "\\)")))
  4604. (org-make-link-regexps)
  4605. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4606. "Regular expression for fast time stamp matching.")
  4607. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4608. "Regular expression for fast time stamp matching.")
  4609. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4610. "Regular expression matching time strings for analysis.
  4611. This one does not require the space after the date, so it can be used
  4612. on a string that terminates immediately after the date.")
  4613. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4614. "Regular expression matching time strings for analysis.")
  4615. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4616. "Regular expression matching time stamps, with groups.")
  4617. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4618. "Regular expression matching time stamps (also [..]), with groups.")
  4619. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4620. "Regular expression matching a time stamp range.")
  4621. (defconst org-tr-regexp-both
  4622. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4623. "Regular expression matching a time stamp range.")
  4624. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4625. org-ts-regexp "\\)?")
  4626. "Regular expression matching a time stamp or time stamp range.")
  4627. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4628. org-ts-regexp-both "\\)?")
  4629. "Regular expression matching a time stamp or time stamp range.
  4630. The time stamps may be either active or inactive.")
  4631. (defvar org-emph-face nil)
  4632. (defun org-do-emphasis-faces (limit)
  4633. "Run through the buffer and add overlays to emphasized strings."
  4634. (let (rtn a)
  4635. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4636. (if (not (= (char-after (match-beginning 3))
  4637. (char-after (match-beginning 4))))
  4638. (progn
  4639. (setq rtn t)
  4640. (setq a (assoc (match-string 3) org-emphasis-alist))
  4641. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4642. 'face
  4643. (nth 1 a))
  4644. (and (nth 4 a)
  4645. (org-remove-flyspell-overlays-in
  4646. (match-beginning 0) (match-end 0)))
  4647. (add-text-properties (match-beginning 2) (match-end 2)
  4648. '(font-lock-multiline t org-emphasis t))
  4649. (when org-hide-emphasis-markers
  4650. (add-text-properties (match-end 4) (match-beginning 5)
  4651. '(invisible org-link))
  4652. (add-text-properties (match-beginning 3) (match-end 3)
  4653. '(invisible org-link)))))
  4654. (backward-char 1))
  4655. rtn))
  4656. (defun org-emphasize (&optional char)
  4657. "Insert or change an emphasis, i.e. a font like bold or italic.
  4658. If there is an active region, change that region to a new emphasis.
  4659. If there is no region, just insert the marker characters and position
  4660. the cursor between them.
  4661. CHAR should be either the marker character, or the first character of the
  4662. HTML tag associated with that emphasis. If CHAR is a space, the means
  4663. to remove the emphasis of the selected region.
  4664. If char is not given (for example in an interactive call) it
  4665. will be prompted for."
  4666. (interactive)
  4667. (let ((eal org-emphasis-alist) e det
  4668. (erc org-emphasis-regexp-components)
  4669. (prompt "")
  4670. (string "") beg end move tag c s)
  4671. (if (org-region-active-p)
  4672. (setq beg (region-beginning) end (region-end)
  4673. string (buffer-substring beg end))
  4674. (setq move t))
  4675. (while (setq e (pop eal))
  4676. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4677. c (aref tag 0))
  4678. (push (cons c (string-to-char (car e))) det)
  4679. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4680. (substring tag 1)))))
  4681. (setq det (nreverse det))
  4682. (unless char
  4683. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4684. (setq char (read-char-exclusive)))
  4685. (setq char (or (cdr (assoc char det)) char))
  4686. (if (equal char ?\ )
  4687. (setq s "" move nil)
  4688. (unless (assoc (char-to-string char) org-emphasis-alist)
  4689. (error "No such emphasis marker: \"%c\"" char))
  4690. (setq s (char-to-string char)))
  4691. (while (and (> (length string) 1)
  4692. (equal (substring string 0 1) (substring string -1))
  4693. (assoc (substring string 0 1) org-emphasis-alist))
  4694. (setq string (substring string 1 -1)))
  4695. (setq string (concat s string s))
  4696. (if beg (delete-region beg end))
  4697. (unless (or (bolp)
  4698. (string-match (concat "[" (nth 0 erc) "\n]")
  4699. (char-to-string (char-before (point)))))
  4700. (insert " "))
  4701. (unless (or (eobp)
  4702. (string-match (concat "[" (nth 1 erc) "\n]")
  4703. (char-to-string (char-after (point)))))
  4704. (insert " ") (backward-char 1))
  4705. (insert string)
  4706. (and move (backward-char 1))))
  4707. (defconst org-nonsticky-props
  4708. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4709. (defsubst org-rear-nonsticky-at (pos)
  4710. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4711. (defun org-activate-plain-links (limit)
  4712. "Run through the buffer and add overlays to links."
  4713. (catch 'exit
  4714. (let (f)
  4715. (if (re-search-forward org-plain-link-re limit t)
  4716. (progn
  4717. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4718. (setq f (get-text-property (match-beginning 0) 'face))
  4719. (if (or (eq f 'org-tag)
  4720. (and (listp f) (memq 'org-tag f)))
  4721. nil
  4722. (add-text-properties (match-beginning 0) (match-end 0)
  4723. (list 'mouse-face 'highlight
  4724. 'face 'org-link
  4725. 'keymap org-mouse-map))
  4726. (org-rear-nonsticky-at (match-end 0)))
  4727. t)))))
  4728. (defun org-activate-code (limit)
  4729. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4730. (progn
  4731. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4732. (remove-text-properties (match-beginning 0) (match-end 0)
  4733. '(display t invisible t intangible t))
  4734. t)))
  4735. (defcustom org-src-fontify-natively nil
  4736. "When non-nil, fontify code in code blocks."
  4737. :type 'boolean
  4738. :group 'org-appearance
  4739. :group 'org-babel)
  4740. (defun org-fontify-meta-lines-and-blocks (limit)
  4741. (condition-case nil
  4742. (org-fontify-meta-lines-and-blocks-1 limit)
  4743. (error (message "org-mode fontification error"))))
  4744. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4745. "Fontify #+ lines and blocks, in the correct ways."
  4746. (let ((case-fold-search t))
  4747. (if (re-search-forward
  4748. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4749. limit t)
  4750. (let ((beg (match-beginning 0))
  4751. (block-start (match-end 0))
  4752. (block-end nil)
  4753. (lang (match-string 7))
  4754. (beg1 (line-beginning-position 2))
  4755. (dc1 (downcase (match-string 2)))
  4756. (dc3 (downcase (match-string 3)))
  4757. end end1 quoting block-type ovl)
  4758. (cond
  4759. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4760. ;; a single line of backend-specific content
  4761. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4762. (remove-text-properties (match-beginning 0) (match-end 0)
  4763. '(display t invisible t intangible t))
  4764. (add-text-properties (match-beginning 1) (match-end 3)
  4765. '(font-lock-fontified t face org-meta-line))
  4766. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4767. '(font-lock-fontified t face org-block))
  4768. ; for backend-specific code
  4769. t)
  4770. ((and (match-end 4) (equal dc3 "begin"))
  4771. ;; Truly a block
  4772. (setq block-type (downcase (match-string 5))
  4773. quoting (member block-type org-protecting-blocks))
  4774. (when (re-search-forward
  4775. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4776. nil t) ;; on purpose, we look further than LIMIT
  4777. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4778. (setq block-end (match-beginning 0))
  4779. (when quoting
  4780. (remove-text-properties beg end
  4781. '(display t invisible t intangible t)))
  4782. (add-text-properties
  4783. beg end
  4784. '(font-lock-fontified t font-lock-multiline t))
  4785. (add-text-properties beg beg1 '(face org-meta-line))
  4786. (add-text-properties end1 (min (point-max) (1+ end))
  4787. '(face org-meta-line)) ; for end_src
  4788. (cond
  4789. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4790. (org-src-font-lock-fontify-block lang block-start block-end)
  4791. ;; remove old background overlays
  4792. (mapc (lambda (ov)
  4793. (if (eq (overlay-get ov 'face) 'org-block-background)
  4794. (delete-overlay ov)))
  4795. (overlays-at (/ (+ beg1 block-end) 2)))
  4796. ;; add a background overlay
  4797. (setq ovl (make-overlay beg1 block-end))
  4798. (overlay-put ovl 'face 'org-block-background)
  4799. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4800. (quoting
  4801. (add-text-properties beg1 (min (point-max) (1+ end1))
  4802. '(face org-block))) ; end of source block
  4803. ((not org-fontify-quote-and-verse-blocks))
  4804. ((string= block-type "quote")
  4805. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4806. ((string= block-type "verse")
  4807. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4808. (add-text-properties beg beg1 '(face org-block-begin-line))
  4809. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4810. t))
  4811. ((member dc1 '("title:" "author:" "email:" "date:"))
  4812. (add-text-properties
  4813. beg (match-end 3)
  4814. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4815. '(font-lock-fontified t invisible t)
  4816. '(font-lock-fontified t face org-document-info-keyword)))
  4817. (add-text-properties
  4818. (match-beginning 6) (match-end 6)
  4819. (if (string-equal dc1 "title:")
  4820. '(font-lock-fontified t face org-document-title)
  4821. '(font-lock-fontified t face org-document-info))))
  4822. ((not (member (char-after beg) '(?\ ?\t)))
  4823. ;; just any other in-buffer setting, but not indented
  4824. (add-text-properties
  4825. beg (1+ (match-end 0))
  4826. '(font-lock-fontified t face org-meta-line))
  4827. t)
  4828. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4829. "orgtbl:" "tblfm:" "tblname:" "results:"
  4830. "call:" "header:" "headers:" "name:"))
  4831. (and (match-end 4) (equal dc3 "attr")))
  4832. (add-text-properties
  4833. beg (match-end 0)
  4834. '(font-lock-fontified t face org-meta-line))
  4835. t)
  4836. ((member dc3 '(" " ""))
  4837. (add-text-properties
  4838. beg (match-end 0)
  4839. '(font-lock-fontified t face font-lock-comment-face)))
  4840. (t nil))))))
  4841. (defun org-strip-protective-commas (beg end)
  4842. "Strip protective commas between BEG and END in the current buffer."
  4843. (interactive "r")
  4844. (save-excursion
  4845. (save-match-data
  4846. (goto-char beg)
  4847. (let ((front-line (save-excursion
  4848. (re-search-forward
  4849. "[^[:space:]]" end t)
  4850. (goto-char (match-beginning 0))
  4851. (current-column))))
  4852. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4853. (goto-char (match-beginning 1))
  4854. (when (= (current-column) front-line)
  4855. (replace-match "" nil nil nil 1)))))))
  4856. (defun org-activate-angle-links (limit)
  4857. "Run through the buffer and add overlays to links."
  4858. (if (re-search-forward org-angle-link-re limit t)
  4859. (progn
  4860. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4861. (add-text-properties (match-beginning 0) (match-end 0)
  4862. (list 'mouse-face 'highlight
  4863. 'keymap org-mouse-map))
  4864. (org-rear-nonsticky-at (match-end 0))
  4865. t)))
  4866. (defun org-activate-footnote-links (limit)
  4867. "Run through the buffer and add overlays to footnotes."
  4868. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4869. (when fn
  4870. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4871. (org-remove-flyspell-overlays-in beg end)
  4872. (add-text-properties beg end
  4873. (list 'mouse-face 'highlight
  4874. 'keymap org-mouse-map
  4875. 'help-echo
  4876. (if (= (point-at-bol) beg)
  4877. "Footnote definition"
  4878. "Footnote reference")
  4879. 'font-lock-fontified t
  4880. 'font-lock-multiline t
  4881. 'face 'org-footnote))))))
  4882. (defun org-activate-bracket-links (limit)
  4883. "Run through the buffer and add overlays to bracketed links."
  4884. (if (re-search-forward org-bracket-link-regexp limit t)
  4885. (let* ((help (concat "LINK: "
  4886. (org-match-string-no-properties 1)))
  4887. ;; FIXME: above we should remove the escapes.
  4888. ;; but that requires another match, protecting match data,
  4889. ;; a lot of overhead for font-lock.
  4890. (ip (org-maybe-intangible
  4891. (list 'invisible 'org-link
  4892. 'keymap org-mouse-map 'mouse-face 'highlight
  4893. 'font-lock-multiline t 'help-echo help)))
  4894. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4895. 'font-lock-multiline t 'help-echo help)))
  4896. ;; We need to remove the invisible property here. Table narrowing
  4897. ;; may have made some of this invisible.
  4898. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4899. (remove-text-properties (match-beginning 0) (match-end 0)
  4900. '(invisible nil))
  4901. (if (match-end 3)
  4902. (progn
  4903. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4904. (org-rear-nonsticky-at (match-beginning 3))
  4905. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4906. (org-rear-nonsticky-at (match-end 3))
  4907. (add-text-properties (match-end 3) (match-end 0) ip)
  4908. (org-rear-nonsticky-at (match-end 0)))
  4909. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4910. (org-rear-nonsticky-at (match-beginning 1))
  4911. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4912. (org-rear-nonsticky-at (match-end 1))
  4913. (add-text-properties (match-end 1) (match-end 0) ip)
  4914. (org-rear-nonsticky-at (match-end 0)))
  4915. t)))
  4916. (defun org-activate-dates (limit)
  4917. "Run through the buffer and add overlays to dates."
  4918. (if (re-search-forward org-tsr-regexp-both limit t)
  4919. (progn
  4920. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4921. (add-text-properties (match-beginning 0) (match-end 0)
  4922. (list 'mouse-face 'highlight
  4923. 'keymap org-mouse-map))
  4924. (org-rear-nonsticky-at (match-end 0))
  4925. (when org-display-custom-times
  4926. (if (match-end 3)
  4927. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4928. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4929. t)))
  4930. (defvar org-target-link-regexp nil
  4931. "Regular expression matching radio targets in plain text.")
  4932. (make-variable-buffer-local 'org-target-link-regexp)
  4933. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4934. "Regular expression matching a link target.")
  4935. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4936. "Regular expression matching a radio target.")
  4937. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4938. "Regular expression matching any target.")
  4939. (defun org-activate-target-links (limit)
  4940. "Run through the buffer and add overlays to target matches."
  4941. (when org-target-link-regexp
  4942. (let ((case-fold-search t))
  4943. (if (re-search-forward org-target-link-regexp limit t)
  4944. (progn
  4945. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4946. (add-text-properties (match-beginning 0) (match-end 0)
  4947. (list 'mouse-face 'highlight
  4948. 'keymap org-mouse-map
  4949. 'help-echo "Radio target link"
  4950. 'org-linked-text t))
  4951. (org-rear-nonsticky-at (match-end 0))
  4952. t)))))
  4953. (defun org-update-radio-target-regexp ()
  4954. "Find all radio targets in this file and update the regular expression."
  4955. (interactive)
  4956. (when (memq 'radio org-activate-links)
  4957. (setq org-target-link-regexp
  4958. (org-make-target-link-regexp (org-all-targets 'radio)))
  4959. (org-restart-font-lock)))
  4960. (defun org-hide-wide-columns (limit)
  4961. (let (s e)
  4962. (setq s (text-property-any (point) (or limit (point-max))
  4963. 'org-cwidth t))
  4964. (when s
  4965. (setq e (next-single-property-change s 'org-cwidth))
  4966. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4967. (goto-char e)
  4968. t)))
  4969. (defvar org-latex-and-specials-regexp nil
  4970. "Regular expression for highlighting export special stuff.")
  4971. (defvar org-match-substring-regexp)
  4972. (defvar org-match-substring-with-braces-regexp)
  4973. ;; This should be with the exporter code, but we also use if for font-locking
  4974. (defconst org-export-html-special-string-regexps
  4975. '(("\\\\-" . "&shy;")
  4976. ("---\\([^-]\\)" . "&mdash;\\1")
  4977. ("--\\([^-]\\)" . "&ndash;\\1")
  4978. ("\\.\\.\\." . "&hellip;"))
  4979. "Regular expressions for special string conversion.")
  4980. (defun org-compute-latex-and-specials-regexp ()
  4981. "Compute regular expression for stuff treated specially by exporters."
  4982. (if (not org-highlight-latex-fragments-and-specials)
  4983. (org-set-local 'org-latex-and-specials-regexp nil)
  4984. (require 'org-exp)
  4985. (let*
  4986. ((matchers (plist-get org-format-latex-options :matchers))
  4987. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4988. org-latex-regexps)))
  4989. (org-export-allow-BIND nil)
  4990. (options (org-combine-plists (org-default-export-plist)
  4991. (org-infile-export-plist)))
  4992. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4993. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4994. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4995. (org-export-html-expand (plist-get options :expand-quoted-html))
  4996. (org-export-with-special-strings (plist-get options :special-strings))
  4997. (re-sub
  4998. (cond
  4999. ((equal org-export-with-sub-superscripts '{})
  5000. (list org-match-substring-with-braces-regexp))
  5001. (org-export-with-sub-superscripts
  5002. (list org-match-substring-regexp))
  5003. (t nil)))
  5004. (re-latex
  5005. (if org-export-with-LaTeX-fragments
  5006. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5007. (re-macros
  5008. (if org-export-with-TeX-macros
  5009. (list (concat "\\\\"
  5010. (regexp-opt
  5011. (append
  5012. (delq nil
  5013. (mapcar 'car-safe
  5014. (append org-entities-user
  5015. org-entities)))
  5016. (if (boundp 'org-latex-entities)
  5017. (mapcar (lambda (x)
  5018. (or (car-safe x) x))
  5019. org-latex-entities)
  5020. nil))
  5021. 'words))) ; FIXME
  5022. ))
  5023. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5024. (re-special (if org-export-with-special-strings
  5025. (mapcar (lambda (x) (car x))
  5026. org-export-html-special-string-regexps)))
  5027. (re-rest
  5028. (delq nil
  5029. (list
  5030. (if org-export-html-expand "@<[^>\n]+>")
  5031. ))))
  5032. (org-set-local
  5033. 'org-latex-and-specials-regexp
  5034. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5035. re-rest) "\\|")))))
  5036. (defun org-do-latex-and-special-faces (limit)
  5037. "Run through the buffer and add overlays to links."
  5038. (when org-latex-and-specials-regexp
  5039. (let (rtn d)
  5040. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5041. limit t))
  5042. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5043. 'face))
  5044. '(org-code org-verbatim underline)))
  5045. (progn
  5046. (setq rtn t
  5047. d (cond ((member (char-after (1+ (match-beginning 0)))
  5048. '(?_ ?^)) 1)
  5049. (t 0)))
  5050. (font-lock-prepend-text-property
  5051. (+ d (match-beginning 0)) (match-end 0)
  5052. 'face 'org-latex-and-export-specials)
  5053. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5054. '(font-lock-multiline t)))))
  5055. rtn)))
  5056. (defun org-restart-font-lock ()
  5057. "Restart `font-lock-mode', to force refontification."
  5058. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5059. (font-lock-mode -1)
  5060. (font-lock-mode 1)))
  5061. (defun org-all-targets (&optional radio)
  5062. "Return a list of all targets in this file.
  5063. With optional argument RADIO, only find radio targets."
  5064. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5065. rtn)
  5066. (save-excursion
  5067. (goto-char (point-min))
  5068. (while (re-search-forward re nil t)
  5069. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5070. rtn)))
  5071. (defun org-make-target-link-regexp (targets)
  5072. "Make regular expression matching all strings in TARGETS.
  5073. The regular expression finds the targets also if there is a line break
  5074. between words."
  5075. (and targets
  5076. (concat
  5077. "\\<\\("
  5078. (mapconcat
  5079. (lambda (x)
  5080. (setq x (regexp-quote x))
  5081. (while (string-match " +" x)
  5082. (setq x (replace-match "\\s-+" t t x)))
  5083. x)
  5084. targets
  5085. "\\|")
  5086. "\\)\\>")))
  5087. (defun org-activate-tags (limit)
  5088. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5089. (progn
  5090. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5091. (add-text-properties (match-beginning 1) (match-end 1)
  5092. (list 'mouse-face 'highlight
  5093. 'keymap org-mouse-map))
  5094. (org-rear-nonsticky-at (match-end 1))
  5095. t)))
  5096. (defun org-outline-level ()
  5097. "Compute the outline level of the heading at point.
  5098. This function assumes that the cursor is at the beginning of a line matched
  5099. by `outline-regexp'. Otherwise it returns garbage.
  5100. If this is called at a normal headline, the level is the number of stars.
  5101. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5102. (save-excursion
  5103. (looking-at org-outline-regexp)
  5104. (1- (- (match-end 0) (match-beginning 0)))))
  5105. (defvar org-font-lock-keywords nil)
  5106. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5107. "Regular expression matching a property line.")
  5108. (defvar org-font-lock-hook nil
  5109. "Functions to be called for special font lock stuff.")
  5110. (defvar org-font-lock-set-keywords-hook nil
  5111. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5112. This is called after `org-font-lock-extra-keywords' is defined, but before
  5113. it is installed to be used by font lock. This can be useful if something
  5114. needs to be inserted at a specific position in the font-lock sequence.")
  5115. (defun org-font-lock-hook (limit)
  5116. (run-hook-with-args 'org-font-lock-hook limit))
  5117. (defun org-set-font-lock-defaults ()
  5118. (let* ((em org-fontify-emphasized-text)
  5119. (lk org-activate-links)
  5120. (org-font-lock-extra-keywords
  5121. (list
  5122. ;; Call the hook
  5123. '(org-font-lock-hook)
  5124. ;; Headlines
  5125. `(,(if org-fontify-whole-heading-line
  5126. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5127. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5128. (1 (org-get-level-face 1))
  5129. (2 (org-get-level-face 2))
  5130. (3 (org-get-level-face 3)))
  5131. ;; Table lines
  5132. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5133. (1 'org-table t))
  5134. ;; Table internals
  5135. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5136. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5137. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5138. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5139. ;; Drawers
  5140. (list org-drawer-regexp '(0 'org-special-keyword t))
  5141. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5142. ;; Properties
  5143. (list org-property-re
  5144. '(1 'org-special-keyword t)
  5145. '(3 'org-property-value t))
  5146. ;; Links
  5147. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5148. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5149. (if (memq 'plain lk) '(org-activate-plain-links))
  5150. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5151. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5152. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5153. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5154. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5155. '(org-hide-wide-columns (0 nil append))
  5156. ;; TODO keyword
  5157. (list (format org-heading-keyword-regexp-format
  5158. org-todo-regexp)
  5159. '(2 (org-get-todo-face 2) t))
  5160. ;; DONE
  5161. (if org-fontify-done-headline
  5162. (list (format org-heading-keyword-regexp-format
  5163. (concat
  5164. "\\(?:"
  5165. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5166. "\\)"))
  5167. '(2 'org-headline-done t))
  5168. nil)
  5169. ;; Priorities
  5170. '(org-font-lock-add-priority-faces)
  5171. ;; Tags
  5172. '(org-font-lock-add-tag-faces)
  5173. ;; Special keywords
  5174. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5175. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5176. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5177. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5178. ;; Emphasis
  5179. (if em
  5180. (if (featurep 'xemacs)
  5181. '(org-do-emphasis-faces (0 nil append))
  5182. '(org-do-emphasis-faces)))
  5183. ;; Checkboxes
  5184. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5185. 1 'org-checkbox prepend)
  5186. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5187. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5188. (0 (org-get-checkbox-statistics-face) t)))
  5189. ;; Description list items
  5190. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5191. 1 'bold prepend)
  5192. ;; ARCHIVEd headings
  5193. (list (concat
  5194. org-outline-regexp-bol
  5195. "\\(.*:" org-archive-tag ":.*\\)")
  5196. '(1 'org-archived prepend))
  5197. ;; Specials
  5198. '(org-do-latex-and-special-faces)
  5199. '(org-fontify-entities)
  5200. '(org-raise-scripts)
  5201. ;; Code
  5202. '(org-activate-code (1 'org-code t))
  5203. ;; COMMENT
  5204. (list (format org-heading-keyword-regexp-format
  5205. (concat "\\("
  5206. org-comment-string "\\|" org-quote-string
  5207. "\\)"))
  5208. '(2 'org-special-keyword t))
  5209. '("^#.*" (0 'font-lock-comment-face t))
  5210. ;; Blocks and meta lines
  5211. '(org-fontify-meta-lines-and-blocks)
  5212. )))
  5213. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5214. (run-hooks 'org-font-lock-set-keywords-hook)
  5215. ;; Now set the full font-lock-keywords
  5216. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5217. (org-set-local 'font-lock-defaults
  5218. '(org-font-lock-keywords t nil nil backward-paragraph))
  5219. (kill-local-variable 'font-lock-keywords) nil))
  5220. (defun org-toggle-pretty-entities ()
  5221. "Toggle the composition display of entities as UTF8 characters."
  5222. (interactive)
  5223. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5224. (org-restart-font-lock)
  5225. (if org-pretty-entities
  5226. (message "Entities are displayed as UTF8 characters")
  5227. (save-restriction
  5228. (widen)
  5229. (org-decompose-region (point-min) (point-max))
  5230. (message "Entities are displayed plain"))))
  5231. (defun org-fontify-entities (limit)
  5232. "Find an entity to fontify."
  5233. (let (ee)
  5234. (when org-pretty-entities
  5235. (catch 'match
  5236. (while (re-search-forward
  5237. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5238. limit t)
  5239. (if (and (not (org-in-indented-comment-line))
  5240. (setq ee (org-entity-get (match-string 1)))
  5241. (= (length (nth 6 ee)) 1))
  5242. (progn
  5243. (add-text-properties
  5244. (match-beginning 0) (match-end 1)
  5245. (list 'font-lock-fontified t))
  5246. (compose-region (match-beginning 0) (match-end 1)
  5247. (nth 6 ee) nil)
  5248. (backward-char 1)
  5249. (throw 'match t))))
  5250. nil))))
  5251. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5252. "Fontify string S like in Org-mode."
  5253. (with-temp-buffer
  5254. (insert s)
  5255. (let ((org-odd-levels-only odd-levels))
  5256. (org-mode)
  5257. (font-lock-fontify-buffer)
  5258. (buffer-string))))
  5259. (defvar org-m nil)
  5260. (defvar org-l nil)
  5261. (defvar org-f nil)
  5262. (defun org-get-level-face (n)
  5263. "Get the right face for match N in font-lock matching of headlines."
  5264. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5265. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5266. (if org-cycle-level-faces
  5267. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5268. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5269. (cond
  5270. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5271. ((eq n 2) org-f)
  5272. (t (if org-level-color-stars-only nil org-f))))
  5273. (defun org-get-todo-face (kwd)
  5274. "Get the right face for a TODO keyword KWD.
  5275. If KWD is a number, get the corresponding match group."
  5276. (if (numberp kwd) (setq kwd (match-string kwd)))
  5277. (or (org-face-from-face-or-color
  5278. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5279. (and (member kwd org-done-keywords) 'org-done)
  5280. 'org-todo))
  5281. (defun org-face-from-face-or-color (context inherit face-or-color)
  5282. "Create a face list that inherits INHERIT, but sets the foreground color.
  5283. When FACE-OR-COLOR is not a string, just return it."
  5284. (if (stringp face-or-color)
  5285. (list :inherit inherit
  5286. (cdr (assoc context org-faces-easy-properties))
  5287. face-or-color)
  5288. face-or-color))
  5289. (defun org-font-lock-add-tag-faces (limit)
  5290. "Add the special tag faces."
  5291. (when (and org-tag-faces org-tags-special-faces-re)
  5292. (while (re-search-forward org-tags-special-faces-re limit t)
  5293. (add-text-properties (match-beginning 1) (match-end 1)
  5294. (list 'face (org-get-tag-face 1)
  5295. 'font-lock-fontified t))
  5296. (backward-char 1))))
  5297. (defun org-font-lock-add-priority-faces (limit)
  5298. "Add the special priority faces."
  5299. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  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. ;;;###autoload
  5390. (defvar org-inlinetask-min-level)
  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 (eq major-mode '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. (cond
  5531. ((and (eq last-command this-command)
  5532. (eq org-cycle-global-status 'overview))
  5533. ;; We just created the overview - now do table of contents
  5534. ;; This can be slow in very large buffers, so indicate action
  5535. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5536. (message "CONTENTS...")
  5537. (org-content)
  5538. (message "CONTENTS...done")
  5539. (setq org-cycle-global-status 'contents)
  5540. (run-hook-with-args 'org-cycle-hook 'contents))
  5541. ((and (eq last-command this-command)
  5542. (eq org-cycle-global-status 'contents))
  5543. ;; We just showed the table of contents - now show everything
  5544. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5545. (show-all)
  5546. (message "SHOW ALL")
  5547. (setq org-cycle-global-status 'all)
  5548. (run-hook-with-args 'org-cycle-hook 'all))
  5549. (t
  5550. ;; Default action: go to overview
  5551. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5552. (org-overview)
  5553. (message "OVERVIEW")
  5554. (setq org-cycle-global-status 'overview)
  5555. (run-hook-with-args 'org-cycle-hook 'overview))))
  5556. (defun org-cycle-internal-local ()
  5557. "Do the local cycling action."
  5558. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5559. ;; First, determine end of headline (EOH), end of subtree or item
  5560. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5561. (save-excursion
  5562. (if (org-at-item-p)
  5563. (progn
  5564. (beginning-of-line)
  5565. (setq struct (org-list-struct))
  5566. (setq eoh (point-at-eol))
  5567. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5568. (setq has-children (org-list-has-child-p (point) struct)))
  5569. (org-back-to-heading)
  5570. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5571. (setq eos (save-excursion
  5572. (org-end-of-subtree t)
  5573. (unless (eobp)
  5574. (skip-chars-forward " \t\n"))
  5575. (if (eobp) (point) (1- (point)))))
  5576. (setq has-children
  5577. (or (save-excursion
  5578. (let ((level (funcall outline-level)))
  5579. (outline-next-heading)
  5580. (and (org-at-heading-p t)
  5581. (> (funcall outline-level) level))))
  5582. (save-excursion
  5583. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5584. ;; Determine end invisible part of buffer (EOL)
  5585. (beginning-of-line 2)
  5586. ;; XEmacs doesn't have `next-single-char-property-change'
  5587. (if (featurep 'xemacs)
  5588. (while (and (not (eobp)) ;; this is like `next-line'
  5589. (get-char-property (1- (point)) 'invisible))
  5590. (beginning-of-line 2))
  5591. (while (and (not (eobp)) ;; this is like `next-line'
  5592. (get-char-property (1- (point)) 'invisible))
  5593. (goto-char (next-single-char-property-change (point) 'invisible))
  5594. (and (eolp) (beginning-of-line 2))))
  5595. (setq eol (point)))
  5596. ;; Find out what to do next and set `this-command'
  5597. (cond
  5598. ((= eos eoh)
  5599. ;; Nothing is hidden behind this heading
  5600. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5601. (message "EMPTY ENTRY")
  5602. (setq org-cycle-subtree-status nil)
  5603. (save-excursion
  5604. (goto-char eos)
  5605. (outline-next-heading)
  5606. (if (outline-invisible-p) (org-flag-heading nil))))
  5607. ((and (or (>= eol eos)
  5608. (not (string-match "\\S-" (buffer-substring eol eos))))
  5609. (or has-children
  5610. (not (setq children-skipped
  5611. org-cycle-skip-children-state-if-no-children))))
  5612. ;; Entire subtree is hidden in one line: children view
  5613. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5614. (if (org-at-item-p)
  5615. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5616. (org-show-entry)
  5617. (org-with-limited-levels (show-children))
  5618. ;; FIXME: This slows down the func way too much.
  5619. ;; How keep drawers hidden in subtree anyway?
  5620. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5621. ;; (org-cycle-hide-drawers 'subtree))
  5622. ;; Fold every list in subtree to top-level items.
  5623. (when (eq org-cycle-include-plain-lists 'integrate)
  5624. (save-excursion
  5625. (org-back-to-heading)
  5626. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5627. (beginning-of-line 1)
  5628. (let* ((struct (org-list-struct))
  5629. (prevs (org-list-prevs-alist struct))
  5630. (end (org-list-get-bottom-point struct)))
  5631. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5632. (org-list-get-all-items (point) struct prevs))
  5633. (goto-char end))))))
  5634. (message "CHILDREN")
  5635. (save-excursion
  5636. (goto-char eos)
  5637. (outline-next-heading)
  5638. (if (outline-invisible-p) (org-flag-heading nil)))
  5639. (setq org-cycle-subtree-status 'children)
  5640. (run-hook-with-args 'org-cycle-hook 'children))
  5641. ((or children-skipped
  5642. (and (eq last-command this-command)
  5643. (eq org-cycle-subtree-status 'children)))
  5644. ;; We just showed the children, or no children are there,
  5645. ;; now show everything.
  5646. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5647. (outline-flag-region eoh eos nil)
  5648. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5649. (setq org-cycle-subtree-status 'subtree)
  5650. (run-hook-with-args 'org-cycle-hook 'subtree))
  5651. (t
  5652. ;; Default action: hide the subtree.
  5653. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5654. (outline-flag-region eoh eos t)
  5655. (message "FOLDED")
  5656. (setq org-cycle-subtree-status 'folded)
  5657. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5658. ;;;###autoload
  5659. (defun org-global-cycle (&optional arg)
  5660. "Cycle the global visibility. For details see `org-cycle'.
  5661. With \\[universal-argument] prefix arg, switch to startup visibility.
  5662. With a numeric prefix, show all headlines up to that level."
  5663. (interactive "P")
  5664. (let ((org-cycle-include-plain-lists
  5665. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5666. (cond
  5667. ((integerp arg)
  5668. (show-all)
  5669. (hide-sublevels arg)
  5670. (setq org-cycle-global-status 'contents))
  5671. ((equal arg '(4))
  5672. (org-set-startup-visibility)
  5673. (message "Startup visibility, plus VISIBILITY properties."))
  5674. (t
  5675. (org-cycle '(4))))))
  5676. (defun org-set-startup-visibility ()
  5677. "Set the visibility required by startup options and properties."
  5678. (cond
  5679. ((eq org-startup-folded t)
  5680. (org-cycle '(4)))
  5681. ((eq org-startup-folded 'content)
  5682. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5683. (org-cycle '(4)) (org-cycle '(4)))))
  5684. (unless (eq org-startup-folded 'showeverything)
  5685. (if org-hide-block-startup (org-hide-block-all))
  5686. (org-set-visibility-according-to-property 'no-cleanup)
  5687. (org-cycle-hide-archived-subtrees 'all)
  5688. (org-cycle-hide-drawers 'all)
  5689. (org-cycle-show-empty-lines t)))
  5690. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5691. "Switch subtree visibilities according to :VISIBILITY: property."
  5692. (interactive)
  5693. (let (org-show-entry-below state)
  5694. (save-excursion
  5695. (goto-char (point-min))
  5696. (while (re-search-forward
  5697. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5698. nil t)
  5699. (setq state (match-string 1))
  5700. (save-excursion
  5701. (org-back-to-heading t)
  5702. (hide-subtree)
  5703. (org-reveal)
  5704. (cond
  5705. ((equal state '("fold" "folded"))
  5706. (hide-subtree))
  5707. ((equal state "children")
  5708. (org-show-hidden-entry)
  5709. (show-children))
  5710. ((equal state "content")
  5711. (save-excursion
  5712. (save-restriction
  5713. (org-narrow-to-subtree)
  5714. (org-content))))
  5715. ((member state '("all" "showall"))
  5716. (show-subtree)))))
  5717. (unless no-cleanup
  5718. (org-cycle-hide-archived-subtrees 'all)
  5719. (org-cycle-hide-drawers 'all)
  5720. (org-cycle-show-empty-lines 'all)))))
  5721. ;; This function uses outline-regexp instead of the more fundamental
  5722. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5723. ;; buffers, where outline-regexp is needed.
  5724. (defun org-overview ()
  5725. "Switch to overview mode, showing only top-level headlines.
  5726. Really, this shows all headlines with level equal or greater than the level
  5727. of the first headline in the buffer. This is important, because if the
  5728. first headline is not level one, then (hide-sublevels 1) gives confusing
  5729. results."
  5730. (interactive)
  5731. (let ((level (save-excursion
  5732. (goto-char (point-min))
  5733. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5734. (progn
  5735. (goto-char (match-beginning 0))
  5736. (funcall outline-level))))))
  5737. (and level (hide-sublevels level))))
  5738. (defun org-content (&optional arg)
  5739. "Show all headlines in the buffer, like a table of contents.
  5740. With numerical argument N, show content up to level N."
  5741. (interactive "P")
  5742. (save-excursion
  5743. ;; Visit all headings and show their offspring
  5744. (and (integerp arg) (org-overview))
  5745. (goto-char (point-max))
  5746. (catch 'exit
  5747. (while (and (progn (condition-case nil
  5748. (outline-previous-visible-heading 1)
  5749. (error (goto-char (point-min))))
  5750. t)
  5751. (looking-at org-outline-regexp))
  5752. (if (integerp arg)
  5753. (show-children (1- arg))
  5754. (show-branches))
  5755. (if (bobp) (throw 'exit nil))))))
  5756. (defun org-optimize-window-after-visibility-change (state)
  5757. "Adjust the window after a change in outline visibility.
  5758. This function is the default value of the hook `org-cycle-hook'."
  5759. (when (get-buffer-window (current-buffer))
  5760. (cond
  5761. ((eq state 'content) nil)
  5762. ((eq state 'all) nil)
  5763. ((eq state 'folded) nil)
  5764. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5765. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5766. (defun org-remove-empty-overlays-at (pos)
  5767. "Remove outline overlays that do not contain non-white stuff."
  5768. (mapc
  5769. (lambda (o)
  5770. (and (eq 'outline (overlay-get o 'invisible))
  5771. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5772. (overlay-end o))))
  5773. (delete-overlay o)))
  5774. (overlays-at pos)))
  5775. (defun org-clean-visibility-after-subtree-move ()
  5776. "Fix visibility issues after moving a subtree."
  5777. ;; First, find a reasonable region to look at:
  5778. ;; Start two siblings above, end three below
  5779. (let* ((beg (save-excursion
  5780. (and (org-get-last-sibling)
  5781. (org-get-last-sibling))
  5782. (point)))
  5783. (end (save-excursion
  5784. (and (org-get-next-sibling)
  5785. (org-get-next-sibling)
  5786. (org-get-next-sibling))
  5787. (if (org-at-heading-p)
  5788. (point-at-eol)
  5789. (point))))
  5790. (level (looking-at "\\*+"))
  5791. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5792. (save-excursion
  5793. (save-restriction
  5794. (narrow-to-region beg end)
  5795. (when re
  5796. ;; Properly fold already folded siblings
  5797. (goto-char (point-min))
  5798. (while (re-search-forward re nil t)
  5799. (if (and (not (outline-invisible-p))
  5800. (save-excursion
  5801. (goto-char (point-at-eol)) (outline-invisible-p)))
  5802. (hide-entry))))
  5803. (org-cycle-show-empty-lines 'overview)
  5804. (org-cycle-hide-drawers 'overview)))))
  5805. (defun org-cycle-show-empty-lines (state)
  5806. "Show empty lines above all visible headlines.
  5807. The region to be covered depends on STATE when called through
  5808. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5809. entire buffer covered. Note that an empty line is only shown if there
  5810. are at least `org-cycle-separator-lines' empty lines before the headline."
  5811. (when (not (= org-cycle-separator-lines 0))
  5812. (save-excursion
  5813. (let* ((n (abs org-cycle-separator-lines))
  5814. (re (cond
  5815. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5816. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5817. (t (let ((ns (number-to-string (- n 2))))
  5818. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5819. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5820. beg end b e)
  5821. (cond
  5822. ((memq state '(overview contents t))
  5823. (setq beg (point-min) end (point-max)))
  5824. ((memq state '(children folded))
  5825. (setq beg (point) end (progn (org-end-of-subtree t t)
  5826. (beginning-of-line 2)
  5827. (point)))))
  5828. (when beg
  5829. (goto-char beg)
  5830. (while (re-search-forward re end t)
  5831. (unless (get-char-property (match-end 1) 'invisible)
  5832. (setq e (match-end 1))
  5833. (if (< org-cycle-separator-lines 0)
  5834. (setq b (save-excursion
  5835. (goto-char (match-beginning 0))
  5836. (org-back-over-empty-lines)
  5837. (if (save-excursion
  5838. (goto-char (max (point-min) (1- (point))))
  5839. (org-at-heading-p))
  5840. (1- (point))
  5841. (point))))
  5842. (setq b (match-beginning 1)))
  5843. (outline-flag-region b e nil)))))))
  5844. ;; Never hide empty lines at the end of the file.
  5845. (save-excursion
  5846. (goto-char (point-max))
  5847. (outline-previous-heading)
  5848. (outline-end-of-heading)
  5849. (if (and (looking-at "[ \t\n]+")
  5850. (= (match-end 0) (point-max)))
  5851. (outline-flag-region (point) (match-end 0) nil))))
  5852. (defun org-show-empty-lines-in-parent ()
  5853. "Move to the parent and re-show empty lines before visible headlines."
  5854. (save-excursion
  5855. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5856. (org-cycle-show-empty-lines context))))
  5857. (defun org-files-list ()
  5858. "Return `org-agenda-files' list, plus all open org-mode files.
  5859. This is useful for operations that need to scan all of a user's
  5860. open and agenda-wise Org files."
  5861. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5862. (dolist (buf (buffer-list))
  5863. (with-current-buffer buf
  5864. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5865. (let ((file (expand-file-name (buffer-file-name))))
  5866. (unless (member file files)
  5867. (push file files))))))
  5868. files))
  5869. (defsubst org-entry-beginning-position ()
  5870. "Return the beginning position of the current entry."
  5871. (save-excursion (outline-back-to-heading t) (point)))
  5872. (defsubst org-entry-end-position ()
  5873. "Return the end position of the current entry."
  5874. (save-excursion (outline-next-heading) (point)))
  5875. (defun org-cycle-hide-drawers (state)
  5876. "Re-hide all drawers after a visibility state change."
  5877. (when (and (eq major-mode 'org-mode)
  5878. (not (memq state '(overview folded contents))))
  5879. (save-excursion
  5880. (let* ((globalp (memq state '(contents all)))
  5881. (beg (if globalp (point-min) (point)))
  5882. (end (if globalp (point-max)
  5883. (if (eq state 'children)
  5884. (save-excursion (outline-next-heading) (point))
  5885. (org-end-of-subtree t)))))
  5886. (goto-char beg)
  5887. (while (re-search-forward org-drawer-regexp end t)
  5888. (org-flag-drawer t))))))
  5889. (defun org-flag-drawer (flag)
  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 document, the
  6068. fastest way is often to fold the entire buffer and then dive into the tree.
  6069. This method has the disadvantage, that the previous location will be folded,
  6070. which may not be what you want.
  6071. This command works around this by showing a copy of the current buffer
  6072. in an indirect buffer, in overview mode. You can dive into the tree in
  6073. that copy, use org-occur and incremental search to find a location.
  6074. When pressing RET or `Q', the command returns to the original buffer in
  6075. which the visibility is still unchanged. After RET it will also jump to
  6076. the location selected in the indirect buffer and expose the headline
  6077. hierarchy above."
  6078. (interactive "P")
  6079. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6080. (org-refile-use-outline-path t)
  6081. (org-refile-target-verify-function nil)
  6082. (interface
  6083. (if (not alternative-interface)
  6084. org-goto-interface
  6085. (if (eq org-goto-interface 'outline)
  6086. 'outline-path-completion
  6087. 'outline)))
  6088. (org-goto-start-pos (point))
  6089. (selected-point
  6090. (if (eq interface 'outline)
  6091. (car (org-get-location (current-buffer) org-goto-help))
  6092. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6093. (org-refile-check-position pa)
  6094. (nth 3 pa)))))
  6095. (if selected-point
  6096. (progn
  6097. (org-mark-ring-push org-goto-start-pos)
  6098. (goto-char selected-point)
  6099. (if (or (outline-invisible-p) (org-invisible-p2))
  6100. (org-show-context 'org-goto)))
  6101. (message "Quit"))))
  6102. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6103. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6104. (defvar org-goto-local-auto-isearch-map) ; defined below
  6105. (defun org-get-location (buf help)
  6106. "Let the user select a location in the Org-mode buffer BUF.
  6107. This function uses a recursive edit. It returns the selected position
  6108. or nil."
  6109. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6110. (isearch-hide-immediately nil)
  6111. (isearch-search-fun-function
  6112. (lambda () 'org-goto-local-search-headings))
  6113. (org-goto-selected-point org-goto-exit-command)
  6114. (pop-up-frames nil)
  6115. (special-display-buffer-names nil)
  6116. (special-display-regexps nil)
  6117. (special-display-function nil))
  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 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. (outline-up-heading 1 t)))
  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. (defun org-promote ()
  6618. "Promote the current heading higher up the tree.
  6619. If the region is active in `transient-mark-mode', promote all headings
  6620. in the region."
  6621. (org-back-to-heading t)
  6622. (let* ((level (save-match-data (funcall outline-level)))
  6623. (after-change-functions (remove 'flyspell-after-change-function
  6624. after-change-functions))
  6625. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6626. (diff (abs (- level (length up-head) -1))))
  6627. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6628. (replace-match up-head nil t)
  6629. ;; Fixup tag positioning
  6630. (and org-auto-align-tags (org-set-tags nil t))
  6631. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6632. (run-hooks 'org-after-promote-entry-hook)))
  6633. (defun org-demote ()
  6634. "Demote the current heading lower down the tree.
  6635. If the region is active in `transient-mark-mode', demote all headings
  6636. in the region."
  6637. (org-back-to-heading t)
  6638. (let* ((level (save-match-data (funcall outline-level)))
  6639. (after-change-functions (remove 'flyspell-after-change-function
  6640. after-change-functions))
  6641. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6642. (diff (abs (- level (length down-head) -1))))
  6643. (replace-match down-head nil t)
  6644. ;; Fixup tag positioning
  6645. (and org-auto-align-tags (org-set-tags nil t))
  6646. (if org-adapt-indentation (org-fixup-indentation diff))
  6647. (run-hooks 'org-after-demote-entry-hook)))
  6648. (defun org-cycle-level ()
  6649. "Cycle the level of an empty headline through possible states.
  6650. This goes first to child, then to parent, level, then up the hierarchy.
  6651. After top level, it switches back to sibling level."
  6652. (interactive)
  6653. (let ((org-adapt-indentation nil))
  6654. (when (org-point-at-end-of-empty-headline)
  6655. (setq this-command 'org-cycle-level) ; Only needed for caching
  6656. (let ((cur-level (org-current-level))
  6657. (prev-level (org-get-previous-line-level)))
  6658. (cond
  6659. ;; If first headline in file, promote to top-level.
  6660. ((= prev-level 0)
  6661. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6662. do (org-do-promote)))
  6663. ;; If same level as prev, demote one.
  6664. ((= prev-level cur-level)
  6665. (org-do-demote))
  6666. ;; If parent is top-level, promote to top level if not already.
  6667. ((= prev-level 1)
  6668. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6669. do (org-do-promote)))
  6670. ;; If top-level, return to prev-level.
  6671. ((= cur-level 1)
  6672. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6673. do (org-do-demote)))
  6674. ;; If less than prev-level, promote one.
  6675. ((< cur-level prev-level)
  6676. (org-do-promote))
  6677. ;; If deeper than prev-level, promote until higher than
  6678. ;; prev-level.
  6679. ((> cur-level prev-level)
  6680. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6681. do (org-do-promote))))
  6682. t))))
  6683. (defun org-map-tree (fun)
  6684. "Call FUN for every heading underneath the current one."
  6685. (org-back-to-heading)
  6686. (let ((level (funcall outline-level)))
  6687. (save-excursion
  6688. (funcall fun)
  6689. (while (and (progn
  6690. (outline-next-heading)
  6691. (> (funcall outline-level) level))
  6692. (not (eobp)))
  6693. (funcall fun)))))
  6694. (defun org-map-region (fun beg end)
  6695. "Call FUN for every heading between BEG and END."
  6696. (let ((org-ignore-region t))
  6697. (save-excursion
  6698. (setq end (copy-marker end))
  6699. (goto-char beg)
  6700. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6701. (< (point) end))
  6702. (funcall fun))
  6703. (while (and (progn
  6704. (outline-next-heading)
  6705. (< (point) end))
  6706. (not (eobp)))
  6707. (funcall fun)))))
  6708. (defvar org-property-end-re) ; silence byte-compiler
  6709. (defun org-fixup-indentation (diff)
  6710. "Change the indentation in the current entry by DIFF.
  6711. However, if any line in the current entry has no indentation, or if it
  6712. would end up with no indentation after the change, nothing at all is done."
  6713. (save-excursion
  6714. (let ((end (save-excursion (outline-next-heading)
  6715. (point-marker)))
  6716. (prohibit (if (> diff 0)
  6717. "^\\S-"
  6718. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6719. col)
  6720. (unless (save-excursion (end-of-line 1)
  6721. (re-search-forward prohibit end t))
  6722. (while (and (< (point) end)
  6723. (re-search-forward "^[ \t]+" end t))
  6724. (goto-char (match-end 0))
  6725. (setq col (current-column))
  6726. (if (< diff 0) (replace-match ""))
  6727. (org-indent-to-column (+ diff col))))
  6728. (move-marker end nil))))
  6729. (defun org-convert-to-odd-levels ()
  6730. "Convert an org-mode file with all levels allowed to one with odd levels.
  6731. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6732. level 5 etc."
  6733. (interactive)
  6734. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6735. (let ((outline-level 'org-outline-level)
  6736. (org-odd-levels-only nil) n)
  6737. (save-excursion
  6738. (goto-char (point-min))
  6739. (while (re-search-forward "^\\*\\*+ " nil t)
  6740. (setq n (- (length (match-string 0)) 2))
  6741. (while (>= (setq n (1- n)) 0)
  6742. (org-demote))
  6743. (end-of-line 1))))))
  6744. (defun org-convert-to-oddeven-levels ()
  6745. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6746. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6747. file contains a section with an even level, conversion would
  6748. destroy the structure of the file. An error is signaled in this
  6749. case."
  6750. (interactive)
  6751. (goto-char (point-min))
  6752. ;; First check if there are no even levels
  6753. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6754. (org-show-context t)
  6755. (error "Not all levels are odd in this file. Conversion not possible"))
  6756. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6757. (let ((outline-regexp org-outline-regexp)
  6758. (outline-level 'org-outline-level)
  6759. (org-odd-levels-only nil) n)
  6760. (save-excursion
  6761. (goto-char (point-min))
  6762. (while (re-search-forward "^\\*\\*+ " nil t)
  6763. (setq n (/ (1- (length (match-string 0))) 2))
  6764. (while (>= (setq n (1- n)) 0)
  6765. (org-promote))
  6766. (end-of-line 1))))))
  6767. (defun org-tr-level (n)
  6768. "Make N odd if required."
  6769. (if org-odd-levels-only (1+ (/ n 2)) n))
  6770. ;;; Vertical tree motion, cutting and pasting of subtrees
  6771. (defun org-move-subtree-up (&optional arg)
  6772. "Move the current subtree up past ARG headlines of the same level."
  6773. (interactive "p")
  6774. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6775. (defun org-move-subtree-down (&optional arg)
  6776. "Move the current subtree down past ARG headlines of the same level."
  6777. (interactive "p")
  6778. (setq arg (prefix-numeric-value arg))
  6779. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6780. 'org-get-last-sibling))
  6781. (ins-point (make-marker))
  6782. (cnt (abs arg))
  6783. (col (current-column))
  6784. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6785. ;; Select the tree
  6786. (org-back-to-heading)
  6787. (setq beg0 (point))
  6788. (save-excursion
  6789. (setq ne-beg (org-back-over-empty-lines))
  6790. (setq beg (point)))
  6791. (save-match-data
  6792. (save-excursion (outline-end-of-heading)
  6793. (setq folded (outline-invisible-p)))
  6794. (outline-end-of-subtree))
  6795. (outline-next-heading)
  6796. (setq ne-end (org-back-over-empty-lines))
  6797. (setq end (point))
  6798. (goto-char beg0)
  6799. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6800. ;; include less whitespace
  6801. (save-excursion
  6802. (goto-char beg)
  6803. (forward-line (- ne-beg ne-end))
  6804. (setq beg (point))))
  6805. ;; Find insertion point, with error handling
  6806. (while (> cnt 0)
  6807. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6808. (progn (goto-char beg0)
  6809. (error "Cannot move past superior level or buffer limit")))
  6810. (setq cnt (1- cnt)))
  6811. (if (> arg 0)
  6812. ;; Moving forward - still need to move over subtree
  6813. (progn (org-end-of-subtree t t)
  6814. (save-excursion
  6815. (org-back-over-empty-lines)
  6816. (or (bolp) (newline)))))
  6817. (setq ne-ins (org-back-over-empty-lines))
  6818. (move-marker ins-point (point))
  6819. (setq txt (buffer-substring beg end))
  6820. (org-save-markers-in-region beg end)
  6821. (delete-region beg end)
  6822. (org-remove-empty-overlays-at beg)
  6823. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6824. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6825. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6826. (let ((bbb (point)))
  6827. (insert-before-markers txt)
  6828. (org-reinstall-markers-in-region bbb)
  6829. (move-marker ins-point bbb))
  6830. (or (bolp) (insert "\n"))
  6831. (setq ins-end (point))
  6832. (goto-char ins-point)
  6833. (org-skip-whitespace)
  6834. (when (and (< arg 0)
  6835. (org-first-sibling-p)
  6836. (> ne-ins ne-beg))
  6837. ;; Move whitespace back to beginning
  6838. (save-excursion
  6839. (goto-char ins-end)
  6840. (let ((kill-whole-line t))
  6841. (kill-line (- ne-ins ne-beg)) (point)))
  6842. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6843. (move-marker ins-point nil)
  6844. (if folded
  6845. (hide-subtree)
  6846. (org-show-entry)
  6847. (show-children)
  6848. (org-cycle-hide-drawers 'children))
  6849. (org-clean-visibility-after-subtree-move)
  6850. ;; move back to the initial column we were at
  6851. (move-to-column col)))
  6852. (defvar org-subtree-clip ""
  6853. "Clipboard for cut and paste of subtrees.
  6854. This is actually only a copy of the kill, because we use the normal kill
  6855. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6856. (defvar org-subtree-clip-folded nil
  6857. "Was the last copied subtree folded?
  6858. This is used to fold the tree back after pasting.")
  6859. (defun org-cut-subtree (&optional n)
  6860. "Cut the current subtree into the clipboard.
  6861. With prefix arg N, cut this many sequential subtrees.
  6862. This is a short-hand for marking the subtree and then cutting it."
  6863. (interactive "p")
  6864. (org-copy-subtree n 'cut))
  6865. (defun org-copy-subtree (&optional n cut force-store-markers)
  6866. "Cut the current subtree into the clipboard.
  6867. With prefix arg N, cut this many sequential subtrees.
  6868. This is a short-hand for marking the subtree and then copying it.
  6869. If CUT is non-nil, actually cut the subtree.
  6870. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6871. of some markers in the region, even if CUT is non-nil. This is
  6872. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6873. (interactive "p")
  6874. (let (beg end folded (beg0 (point)))
  6875. (if (org-called-interactively-p 'any)
  6876. (org-back-to-heading nil) ; take what looks like a subtree
  6877. (org-back-to-heading t)) ; take what is really there
  6878. (org-back-over-empty-lines)
  6879. (setq beg (point))
  6880. (skip-chars-forward " \t\r\n")
  6881. (save-match-data
  6882. (save-excursion (outline-end-of-heading)
  6883. (setq folded (outline-invisible-p)))
  6884. (condition-case nil
  6885. (org-forward-same-level (1- n) t)
  6886. (error nil))
  6887. (org-end-of-subtree t t))
  6888. (org-back-over-empty-lines)
  6889. (setq end (point))
  6890. (goto-char beg0)
  6891. (when (> end beg)
  6892. (setq org-subtree-clip-folded folded)
  6893. (when (or cut force-store-markers)
  6894. (org-save-markers-in-region beg end))
  6895. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6896. (setq org-subtree-clip (current-kill 0))
  6897. (message "%s: Subtree(s) with %d characters"
  6898. (if cut "Cut" "Copied")
  6899. (length org-subtree-clip)))))
  6900. (defun org-paste-subtree (&optional level tree for-yank)
  6901. "Paste the clipboard as a subtree, with modification of headline level.
  6902. The entire subtree is promoted or demoted in order to match a new headline
  6903. level.
  6904. If the cursor is at the beginning of a headline, the same level as
  6905. that headline is used to paste the tree
  6906. If not, the new level is derived from the *visible* headings
  6907. before and after the insertion point, and taken to be the inferior headline
  6908. level of the two. So if the previous visible heading is level 3 and the
  6909. next is level 4 (or vice versa), level 4 will be used for insertion.
  6910. This makes sure that the subtree remains an independent subtree and does
  6911. not swallow low level entries.
  6912. You can also force a different level, either by using a numeric prefix
  6913. argument, or by inserting the heading marker by hand. For example, if the
  6914. cursor is after \"*****\", then the tree will be shifted to level 5.
  6915. If optional TREE is given, use this text instead of the kill ring.
  6916. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6917. move back over whitespace before inserting, and move point to the end of
  6918. the inserted text when done."
  6919. (interactive "P")
  6920. (setq tree (or tree (and kill-ring (current-kill 0))))
  6921. (unless (org-kill-is-subtree-p tree)
  6922. (error "%s"
  6923. (substitute-command-keys
  6924. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6925. (org-with-limited-levels
  6926. (let* ((visp (not (outline-invisible-p)))
  6927. (txt tree)
  6928. (^re_ "\\(\\*+\\)[ \t]*")
  6929. (old-level (if (string-match org-outline-regexp-bol txt)
  6930. (- (match-end 0) (match-beginning 0) 1)
  6931. -1))
  6932. (force-level (cond (level (prefix-numeric-value level))
  6933. ((and (looking-at "[ \t]*$")
  6934. (string-match
  6935. "^\\*+$" (buffer-substring
  6936. (point-at-bol) (point))))
  6937. (- (match-end 1) (match-beginning 1)))
  6938. ((and (bolp)
  6939. (looking-at org-outline-regexp))
  6940. (- (match-end 0) (point) 1))
  6941. (t nil)))
  6942. (previous-level (save-excursion
  6943. (condition-case nil
  6944. (progn
  6945. (outline-previous-visible-heading 1)
  6946. (if (looking-at ^re_)
  6947. (- (match-end 0) (match-beginning 0) 1)
  6948. 1))
  6949. (error 1))))
  6950. (next-level (save-excursion
  6951. (condition-case nil
  6952. (progn
  6953. (or (looking-at org-outline-regexp)
  6954. (outline-next-visible-heading 1))
  6955. (if (looking-at ^re_)
  6956. (- (match-end 0) (match-beginning 0) 1)
  6957. 1))
  6958. (error 1))))
  6959. (new-level (or force-level (max previous-level next-level)))
  6960. (shift (if (or (= old-level -1)
  6961. (= new-level -1)
  6962. (= old-level new-level))
  6963. 0
  6964. (- new-level old-level)))
  6965. (delta (if (> shift 0) -1 1))
  6966. (func (if (> shift 0) 'org-demote 'org-promote))
  6967. (org-odd-levels-only nil)
  6968. beg end newend)
  6969. ;; Remove the forced level indicator
  6970. (if force-level
  6971. (delete-region (point-at-bol) (point)))
  6972. ;; Paste
  6973. (beginning-of-line (if (bolp) 1 2))
  6974. (unless for-yank (org-back-over-empty-lines))
  6975. (setq beg (point))
  6976. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6977. (insert-before-markers txt)
  6978. (unless (string-match "\n\\'" txt) (insert "\n"))
  6979. (setq newend (point))
  6980. (org-reinstall-markers-in-region beg)
  6981. (setq end (point))
  6982. (goto-char beg)
  6983. (skip-chars-forward " \t\n\r")
  6984. (setq beg (point))
  6985. (if (and (outline-invisible-p) visp)
  6986. (save-excursion (outline-show-heading)))
  6987. ;; Shift if necessary
  6988. (unless (= shift 0)
  6989. (save-restriction
  6990. (narrow-to-region beg end)
  6991. (while (not (= shift 0))
  6992. (org-map-region func (point-min) (point-max))
  6993. (setq shift (+ delta shift)))
  6994. (goto-char (point-min))
  6995. (setq newend (point-max))))
  6996. (when (or (org-called-interactively-p 'interactive) for-yank)
  6997. (message "Clipboard pasted as level %d subtree" new-level))
  6998. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6999. kill-ring
  7000. (eq org-subtree-clip (current-kill 0))
  7001. org-subtree-clip-folded)
  7002. ;; The tree was folded before it was killed/copied
  7003. (hide-subtree))
  7004. (and for-yank (goto-char newend)))))
  7005. (defun org-kill-is-subtree-p (&optional txt)
  7006. "Check if the current kill is an outline subtree, or a set of trees.
  7007. Returns nil if kill does not start with a headline, or if the first
  7008. headline level is not the largest headline level in the tree.
  7009. So this will actually accept several entries of equal levels as well,
  7010. which is OK for `org-paste-subtree'.
  7011. If optional TXT is given, check this string instead of the current kill."
  7012. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7013. (re (org-get-limited-outline-regexp))
  7014. (^re (concat "^" re))
  7015. (start-level (and kill
  7016. (string-match
  7017. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7018. kill)
  7019. (- (match-end 2) (match-beginning 2) 1)))
  7020. (start (1+ (or (match-beginning 2) -1))))
  7021. (if (not start-level)
  7022. (progn
  7023. nil) ;; does not even start with a heading
  7024. (catch 'exit
  7025. (while (setq start (string-match ^re kill (1+ start)))
  7026. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7027. (throw 'exit nil)))
  7028. t))))
  7029. (defvar org-markers-to-move nil
  7030. "Markers that should be moved with a cut-and-paste operation.
  7031. Those markers are stored together with their positions relative to
  7032. the start of the region.")
  7033. (defun org-save-markers-in-region (beg end)
  7034. "Check markers in region.
  7035. If these markers are between BEG and END, record their position relative
  7036. to BEG, so that after moving the block of text, we can put the markers back
  7037. into place.
  7038. This function gets called just before an entry or tree gets cut from the
  7039. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7040. called immediately, to move the markers with the entries."
  7041. (setq org-markers-to-move nil)
  7042. (when (featurep 'org-clock)
  7043. (org-clock-save-markers-for-cut-and-paste beg end))
  7044. (when (featurep 'org-agenda)
  7045. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7046. (defun org-check-and-save-marker (marker beg end)
  7047. "Check if MARKER is between BEG and END.
  7048. If yes, remember the marker and the distance to BEG."
  7049. (when (and (marker-buffer marker)
  7050. (equal (marker-buffer marker) (current-buffer)))
  7051. (if (and (>= marker beg) (< marker end))
  7052. (push (cons marker (- marker beg)) org-markers-to-move))))
  7053. (defun org-reinstall-markers-in-region (beg)
  7054. "Move all remembered markers to their position relative to BEG."
  7055. (mapc (lambda (x)
  7056. (move-marker (car x) (+ beg (cdr x))))
  7057. org-markers-to-move)
  7058. (setq org-markers-to-move nil))
  7059. (defun org-narrow-to-subtree ()
  7060. "Narrow buffer to the current subtree."
  7061. (interactive)
  7062. (save-excursion
  7063. (save-match-data
  7064. (org-with-limited-levels
  7065. (narrow-to-region
  7066. (progn (org-back-to-heading t) (point))
  7067. (progn (org-end-of-subtree t t)
  7068. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7069. (point)))))))
  7070. (defun org-narrow-to-block ()
  7071. "Narrow buffer to the current block."
  7072. (interactive)
  7073. (let* ((case-fold-search t)
  7074. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7075. "^[ \t]*#\\+end_.*")))
  7076. (if blockp
  7077. (narrow-to-region (car blockp) (cdr blockp))
  7078. (error "Not in a block"))))
  7079. (eval-when-compile
  7080. (defvar org-property-drawer-re))
  7081. (defvar org-property-start-re) ;; defined below
  7082. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7083. "Clone the task (subtree) at point N times.
  7084. The clones will be inserted as siblings.
  7085. In interactive use, the user will be prompted for the number of
  7086. clones to be produced, and for a time SHIFT, which may be a
  7087. repeater as used in time stamps, for example `+3d'.
  7088. When a valid repeater is given and the entry contains any time
  7089. stamps, the clones will become a sequence in time, with time
  7090. stamps in the subtree shifted for each clone produced. If SHIFT
  7091. is nil or the empty string, time stamps will be left alone. The
  7092. ID property of the original subtree is removed.
  7093. If the original subtree did contain time stamps with a repeater,
  7094. the following will happen:
  7095. - the repeater will be removed in each clone
  7096. - an additional clone will be produced, with the current, unshifted
  7097. date(s) in the entry.
  7098. - the original entry will be placed *after* all the clones, with
  7099. repeater intact.
  7100. - the start days in the repeater in the original entry will be shifted
  7101. to past the last clone.
  7102. In this way you can spell out a number of instances of a repeating task,
  7103. and still retain the repeater to cover future instances of the task."
  7104. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7105. (let (beg end template task idprop
  7106. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7107. (drawer-re org-drawer-regexp))
  7108. (if (not (and (integerp n) (> n 0)))
  7109. (error "Invalid number of replications %s" n))
  7110. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7111. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7112. shift)))
  7113. (error "Invalid shift specification %s" shift))
  7114. (when doshift
  7115. (setq shift-n (string-to-number (match-string 1 shift))
  7116. shift-what (cdr (assoc (match-string 2 shift)
  7117. '(("d" . day) ("w" . week)
  7118. ("m" . month) ("y" . year))))))
  7119. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7120. (setq nmin 1 nmax n)
  7121. (org-back-to-heading t)
  7122. (setq beg (point))
  7123. (setq idprop (org-entry-get nil "ID"))
  7124. (org-end-of-subtree t t)
  7125. (or (bolp) (insert "\n"))
  7126. (setq end (point))
  7127. (setq template (buffer-substring beg end))
  7128. (when (and doshift
  7129. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[dwmy][^<>\n]*>" template))
  7130. (delete-region beg end)
  7131. (setq end beg)
  7132. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7133. (goto-char end)
  7134. (loop for n from nmin to nmax do
  7135. ;; prepare clone
  7136. (with-temp-buffer
  7137. (insert template)
  7138. (org-mode)
  7139. (goto-char (point-min))
  7140. (org-show-subtree)
  7141. (and idprop (if org-clone-delete-id
  7142. (org-entry-delete nil "ID")
  7143. (org-id-get-create t)))
  7144. (unless (= n 0)
  7145. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7146. (kill-whole-line))
  7147. (goto-char (point-min))
  7148. (while (re-search-forward drawer-re nil t)
  7149. (mapc (lambda (d)
  7150. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7151. (goto-char (point-min))
  7152. (when doshift
  7153. (while (re-search-forward org-ts-regexp-both nil t)
  7154. (org-timestamp-change (* n shift-n) shift-what))
  7155. (unless (= n n-no-remove)
  7156. (goto-char (point-min))
  7157. (while (re-search-forward org-ts-regexp nil t)
  7158. (save-excursion
  7159. (goto-char (match-beginning 0))
  7160. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[dwmy]\\)")
  7161. (delete-region (match-beginning 1) (match-end 1)))))))
  7162. (setq task (buffer-string)))
  7163. (insert task))
  7164. (goto-char beg)))
  7165. ;;; Outline Sorting
  7166. (defun org-sort (with-case)
  7167. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7168. Optional argument WITH-CASE means sort case-sensitively."
  7169. (interactive "P")
  7170. (cond
  7171. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7172. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7173. (t
  7174. (org-call-with-arg 'org-sort-entries with-case))))
  7175. (defun org-sort-remove-invisible (s)
  7176. (remove-text-properties 0 (length s) org-rm-props s)
  7177. (while (string-match org-bracket-link-regexp s)
  7178. (setq s (replace-match (if (match-end 2)
  7179. (match-string 3 s)
  7180. (match-string 1 s)) t t s)))
  7181. s)
  7182. (defvar org-priority-regexp) ; defined later in the file
  7183. (defvar org-after-sorting-entries-or-items-hook nil
  7184. "Hook that is run after a bunch of entries or items have been sorted.
  7185. When children are sorted, the cursor is in the parent line when this
  7186. hook gets called. When a region or a plain list is sorted, the cursor
  7187. will be in the first entry of the sorted region/list.")
  7188. (defun org-sort-entries
  7189. (&optional with-case sorting-type getkey-func compare-func property)
  7190. "Sort entries on a certain level of an outline tree.
  7191. If there is an active region, the entries in the region are sorted.
  7192. Else, if the cursor is before the first entry, sort the top-level items.
  7193. Else, the children of the entry at point are sorted.
  7194. Sorting can be alphabetically, numerically, by date/time as given by
  7195. a time stamp, by a property or by priority.
  7196. The command prompts for the sorting type unless it has been given to the
  7197. function through the SORTING-TYPE argument, which needs to be a character,
  7198. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7199. precise meaning of each character:
  7200. n Numerically, by converting the beginning of the entry/item to a number.
  7201. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7202. t By date/time, either the first active time stamp in the entry, or, if
  7203. none exist, by the first inactive one.
  7204. s By the scheduled date/time.
  7205. d By deadline date/time.
  7206. c By creation time, which is assumed to be the first inactive time stamp
  7207. at the beginning of a line.
  7208. p By priority according to the cookie.
  7209. r By the value of a property.
  7210. Capital letters will reverse the sort order.
  7211. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7212. called with point at the beginning of the record. It must return either
  7213. a string or a number that should serve as the sorting key for that record.
  7214. Comparing entries ignores case by default. However, with an optional argument
  7215. WITH-CASE, the sorting considers case as well."
  7216. (interactive "P")
  7217. (let ((case-func (if with-case 'identity 'downcase))
  7218. start beg end stars re re2
  7219. txt what tmp)
  7220. ;; Find beginning and end of region to sort
  7221. (cond
  7222. ((org-region-active-p)
  7223. ;; we will sort the region
  7224. (setq end (region-end)
  7225. what "region")
  7226. (goto-char (region-beginning))
  7227. (if (not (org-at-heading-p)) (outline-next-heading))
  7228. (setq start (point)))
  7229. ((or (org-at-heading-p)
  7230. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7231. ;; we will sort the children of the current headline
  7232. (org-back-to-heading)
  7233. (setq start (point)
  7234. end (progn (org-end-of-subtree t t)
  7235. (or (bolp) (insert "\n"))
  7236. (org-back-over-empty-lines)
  7237. (point))
  7238. what "children")
  7239. (goto-char start)
  7240. (show-subtree)
  7241. (outline-next-heading))
  7242. (t
  7243. ;; we will sort the top-level entries in this file
  7244. (goto-char (point-min))
  7245. (or (org-at-heading-p) (outline-next-heading))
  7246. (setq start (point))
  7247. (goto-char (point-max))
  7248. (beginning-of-line 1)
  7249. (when (looking-at ".*?\\S-")
  7250. ;; File ends in a non-white line
  7251. (end-of-line 1)
  7252. (insert "\n"))
  7253. (setq end (point-max))
  7254. (setq what "top-level")
  7255. (goto-char start)
  7256. (show-all)))
  7257. (setq beg (point))
  7258. (if (>= beg end) (error "Nothing to sort"))
  7259. (looking-at "\\(\\*+\\)")
  7260. (setq stars (match-string 1)
  7261. re (concat "^" (regexp-quote stars) " +")
  7262. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7263. txt (buffer-substring beg end))
  7264. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7265. (if (and (not (equal stars "*")) (string-match re2 txt))
  7266. (error "Region to sort contains a level above the first entry"))
  7267. (unless sorting-type
  7268. (message
  7269. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7270. [t]ime [s]cheduled [d]eadline [c]reated
  7271. A/N/T/S/D/C/P/O/F means reversed:"
  7272. what)
  7273. (setq sorting-type (read-char-exclusive))
  7274. (and (= (downcase sorting-type) ?f)
  7275. (setq getkey-func
  7276. (org-icompleting-read "Sort using function: "
  7277. obarray 'fboundp t nil nil))
  7278. (setq getkey-func (intern getkey-func)))
  7279. (and (= (downcase sorting-type) ?r)
  7280. (setq property
  7281. (org-icompleting-read "Property: "
  7282. (mapcar 'list (org-buffer-property-keys t))
  7283. nil t))))
  7284. (message "Sorting entries...")
  7285. (save-restriction
  7286. (narrow-to-region start end)
  7287. (let ((dcst (downcase sorting-type))
  7288. (case-fold-search nil)
  7289. (now (current-time)))
  7290. (sort-subr
  7291. (/= dcst sorting-type)
  7292. ;; This function moves to the beginning character of the "record" to
  7293. ;; be sorted.
  7294. (lambda nil
  7295. (if (re-search-forward re nil t)
  7296. (goto-char (match-beginning 0))
  7297. (goto-char (point-max))))
  7298. ;; This function moves to the last character of the "record" being
  7299. ;; sorted.
  7300. (lambda nil
  7301. (save-match-data
  7302. (condition-case nil
  7303. (outline-forward-same-level 1)
  7304. (error
  7305. (goto-char (point-max))))))
  7306. ;; This function returns the value that gets sorted against.
  7307. (lambda nil
  7308. (cond
  7309. ((= dcst ?n)
  7310. (if (looking-at org-complex-heading-regexp)
  7311. (string-to-number (match-string 4))
  7312. nil))
  7313. ((= dcst ?a)
  7314. (if (looking-at org-complex-heading-regexp)
  7315. (funcall case-func (match-string 4))
  7316. nil))
  7317. ((= dcst ?t)
  7318. (let ((end (save-excursion (outline-next-heading) (point))))
  7319. (if (or (re-search-forward org-ts-regexp end t)
  7320. (re-search-forward org-ts-regexp-both end t))
  7321. (org-time-string-to-seconds (match-string 0))
  7322. (org-float-time now))))
  7323. ((= dcst ?c)
  7324. (let ((end (save-excursion (outline-next-heading) (point))))
  7325. (if (re-search-forward
  7326. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7327. end t)
  7328. (org-time-string-to-seconds (match-string 0))
  7329. (org-float-time now))))
  7330. ((= dcst ?s)
  7331. (let ((end (save-excursion (outline-next-heading) (point))))
  7332. (if (re-search-forward org-scheduled-time-regexp end t)
  7333. (org-time-string-to-seconds (match-string 1))
  7334. (org-float-time now))))
  7335. ((= dcst ?d)
  7336. (let ((end (save-excursion (outline-next-heading) (point))))
  7337. (if (re-search-forward org-deadline-time-regexp end t)
  7338. (org-time-string-to-seconds (match-string 1))
  7339. (org-float-time now))))
  7340. ((= dcst ?p)
  7341. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7342. (string-to-char (match-string 2))
  7343. org-default-priority))
  7344. ((= dcst ?r)
  7345. (or (org-entry-get nil property) ""))
  7346. ((= dcst ?o)
  7347. (if (looking-at org-complex-heading-regexp)
  7348. (- 9999 (length (member (match-string 2)
  7349. org-todo-keywords-1)))))
  7350. ((= dcst ?f)
  7351. (if getkey-func
  7352. (progn
  7353. (setq tmp (funcall getkey-func))
  7354. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7355. tmp)
  7356. (error "Invalid key function `%s'" getkey-func)))
  7357. (t (error "Invalid sorting type `%c'" sorting-type))))
  7358. nil
  7359. (cond
  7360. ((= dcst ?a) 'string<)
  7361. ((= dcst ?f) compare-func)
  7362. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7363. (t nil)))))
  7364. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7365. (message "Sorting entries...done")))
  7366. (defun org-do-sort (table what &optional with-case sorting-type)
  7367. "Sort TABLE of WHAT according to SORTING-TYPE.
  7368. The user will be prompted for the SORTING-TYPE if the call to this
  7369. function does not specify it. WHAT is only for the prompt, to indicate
  7370. what is being sorted. The sorting key will be extracted from
  7371. the car of the elements of the table.
  7372. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7373. (unless sorting-type
  7374. (message
  7375. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7376. what)
  7377. (setq sorting-type (read-char-exclusive)))
  7378. (let ((dcst (downcase sorting-type))
  7379. extractfun comparefun)
  7380. ;; Define the appropriate functions
  7381. (cond
  7382. ((= dcst ?n)
  7383. (setq extractfun 'string-to-number
  7384. comparefun (if (= dcst sorting-type) '< '>)))
  7385. ((= dcst ?a)
  7386. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7387. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7388. comparefun (if (= dcst sorting-type)
  7389. 'string<
  7390. (lambda (a b) (and (not (string< a b))
  7391. (not (string= a b)))))))
  7392. ((= dcst ?t)
  7393. (setq extractfun
  7394. (lambda (x)
  7395. (if (or (string-match org-ts-regexp x)
  7396. (string-match org-ts-regexp-both x))
  7397. (org-float-time
  7398. (org-time-string-to-time (match-string 0 x)))
  7399. 0))
  7400. comparefun (if (= dcst sorting-type) '< '>)))
  7401. (t (error "Invalid sorting type `%c'" sorting-type)))
  7402. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7403. table)
  7404. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7405. ;;; The orgstruct minor mode
  7406. ;; Define a minor mode which can be used in other modes in order to
  7407. ;; integrate the org-mode structure editing commands.
  7408. ;; This is really a hack, because the org-mode structure commands use
  7409. ;; keys which normally belong to the major mode. Here is how it
  7410. ;; works: The minor mode defines all the keys necessary to operate the
  7411. ;; structure commands, but wraps the commands into a function which
  7412. ;; tests if the cursor is currently at a headline or a plain list
  7413. ;; item. If that is the case, the structure command is used,
  7414. ;; temporarily setting many Org-mode variables like regular
  7415. ;; expressions for filling etc. However, when any of those keys is
  7416. ;; used at a different location, function uses `key-binding' to look
  7417. ;; up if the key has an associated command in another currently active
  7418. ;; keymap (minor modes, major mode, global), and executes that
  7419. ;; command. There might be problems if any of the keys is otherwise
  7420. ;; used as a prefix key.
  7421. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7422. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7423. ;; addresses this by checking explicitly for both bindings.
  7424. (defvar orgstruct-mode-map (make-sparse-keymap)
  7425. "Keymap for the minor `orgstruct-mode'.")
  7426. (defvar org-local-vars nil
  7427. "List of local variables, for use by `orgstruct-mode'.")
  7428. ;;;###autoload
  7429. (define-minor-mode orgstruct-mode
  7430. "Toggle the minor mode `orgstruct-mode'.
  7431. This mode is for using Org-mode structure commands in other
  7432. modes. The following keys behave as if Org-mode were active, if
  7433. the cursor is on a headline, or on a plain list item (both as
  7434. defined by Org-mode).
  7435. M-up Move entry/item up
  7436. M-down Move entry/item down
  7437. M-left Promote
  7438. M-right Demote
  7439. M-S-up Move entry/item up
  7440. M-S-down Move entry/item down
  7441. M-S-left Promote subtree
  7442. M-S-right Demote subtree
  7443. M-q Fill paragraph and items like in Org-mode
  7444. C-c ^ Sort entries
  7445. C-c - Cycle list bullet
  7446. TAB Cycle item visibility
  7447. M-RET Insert new heading/item
  7448. S-M-RET Insert new TODO heading / Checkbox item
  7449. C-c C-c Set tags / toggle checkbox"
  7450. nil " OrgStruct" nil
  7451. (org-load-modules-maybe)
  7452. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7453. ;;;###autoload
  7454. (defun turn-on-orgstruct ()
  7455. "Unconditionally turn on `orgstruct-mode'."
  7456. (orgstruct-mode 1))
  7457. (defun orgstruct++-mode (&optional arg)
  7458. "Toggle `orgstruct-mode', the enhanced version of it.
  7459. In addition to setting orgstruct-mode, this also exports all indentation
  7460. and autofilling variables from org-mode into the buffer. It will also
  7461. recognize item context in multiline items.
  7462. Note that turning off orgstruct-mode will *not* remove the
  7463. indentation/paragraph settings. This can only be done by refreshing the
  7464. major mode, for example with \\[normal-mode]."
  7465. (interactive "P")
  7466. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7467. (if (< arg 1)
  7468. (orgstruct-mode -1)
  7469. (orgstruct-mode 1)
  7470. (let (var val)
  7471. (mapc
  7472. (lambda (x)
  7473. (when (string-match
  7474. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7475. (symbol-name (car x)))
  7476. (setq var (car x) val (nth 1 x))
  7477. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7478. org-local-vars)
  7479. (org-set-local 'orgstruct-is-++ t))))
  7480. (defvar orgstruct-is-++ nil
  7481. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7482. (make-variable-buffer-local 'orgstruct-is-++)
  7483. ;;;###autoload
  7484. (defun turn-on-orgstruct++ ()
  7485. "Unconditionally turn on `orgstruct++-mode'."
  7486. (orgstruct++-mode 1))
  7487. (defun orgstruct-error ()
  7488. "Error when there is no default binding for a structure key."
  7489. (interactive)
  7490. (error "This key has no function outside structure elements"))
  7491. (defun orgstruct-setup ()
  7492. "Setup orgstruct keymaps."
  7493. (let ((nfunc 0)
  7494. (bindings
  7495. (list
  7496. '([(meta up)] org-metaup)
  7497. '([(meta down)] org-metadown)
  7498. '([(meta left)] org-metaleft)
  7499. '([(meta right)] org-metaright)
  7500. '([(meta shift up)] org-shiftmetaup)
  7501. '([(meta shift down)] org-shiftmetadown)
  7502. '([(meta shift left)] org-shiftmetaleft)
  7503. '([(meta shift right)] org-shiftmetaright)
  7504. '([?\e (up)] org-metaup)
  7505. '([?\e (down)] org-metadown)
  7506. '([?\e (left)] org-metaleft)
  7507. '([?\e (right)] org-metaright)
  7508. '([?\e (shift up)] org-shiftmetaup)
  7509. '([?\e (shift down)] org-shiftmetadown)
  7510. '([?\e (shift left)] org-shiftmetaleft)
  7511. '([?\e (shift right)] org-shiftmetaright)
  7512. '([(shift up)] org-shiftup)
  7513. '([(shift down)] org-shiftdown)
  7514. '([(shift left)] org-shiftleft)
  7515. '([(shift right)] org-shiftright)
  7516. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7517. '("\M-q" fill-paragraph)
  7518. '("\C-c^" org-sort)
  7519. '("\C-c-" org-cycle-list-bullet)))
  7520. elt key fun cmd)
  7521. (while (setq elt (pop bindings))
  7522. (setq nfunc (1+ nfunc))
  7523. (setq key (org-key (car elt))
  7524. fun (nth 1 elt)
  7525. cmd (orgstruct-make-binding fun nfunc key))
  7526. (org-defkey orgstruct-mode-map key cmd))
  7527. ;; Special treatment needed for TAB and RET
  7528. (org-defkey orgstruct-mode-map [(tab)]
  7529. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7530. (org-defkey orgstruct-mode-map "\C-i"
  7531. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7532. (org-defkey orgstruct-mode-map "\M-\C-m"
  7533. (orgstruct-make-binding 'org-insert-heading 105
  7534. "\M-\C-m" [(meta return)]))
  7535. (org-defkey orgstruct-mode-map [(meta return)]
  7536. (orgstruct-make-binding 'org-insert-heading 106
  7537. [(meta return)] "\M-\C-m"))
  7538. (org-defkey orgstruct-mode-map [(shift meta return)]
  7539. (orgstruct-make-binding 'org-insert-todo-heading 107
  7540. [(meta return)] "\M-\C-m"))
  7541. (org-defkey orgstruct-mode-map "\e\C-m"
  7542. (orgstruct-make-binding 'org-insert-heading 108
  7543. "\e\C-m" [?\e (return)]))
  7544. (org-defkey orgstruct-mode-map [?\e (return)]
  7545. (orgstruct-make-binding 'org-insert-heading 109
  7546. [?\e (return)] "\e\C-m"))
  7547. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7548. (orgstruct-make-binding 'org-insert-todo-heading 110
  7549. [?\e (return)] "\e\C-m"))
  7550. (unless org-local-vars
  7551. (setq org-local-vars (org-get-local-variables)))
  7552. t))
  7553. (defun orgstruct-make-binding (fun n &rest keys)
  7554. "Create a function for binding in the structure minor mode.
  7555. FUN is the command to call inside a table. N is used to create a unique
  7556. command name. KEYS are keys that should be checked in for a command
  7557. to execute outside of tables."
  7558. (eval
  7559. (list 'defun
  7560. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7561. '(arg)
  7562. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7563. "Outside of structure, run the binding of `"
  7564. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7565. "'.")
  7566. '(interactive "p")
  7567. (list 'if
  7568. `(org-context-p 'headline 'item
  7569. (and orgstruct-is-++
  7570. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7571. 'item-body))
  7572. (list 'org-run-like-in-org-mode (list 'quote fun))
  7573. (list 'let '(orgstruct-mode)
  7574. (list 'call-interactively
  7575. (append '(or)
  7576. (mapcar (lambda (k)
  7577. (list 'key-binding k))
  7578. keys)
  7579. '('orgstruct-error))))))))
  7580. (defun org-context-p (&rest contexts)
  7581. "Check if local context is any of CONTEXTS.
  7582. Possible values in the list of contexts are `table', `headline', and `item'."
  7583. (let ((pos (point)))
  7584. (goto-char (point-at-bol))
  7585. (prog1 (or (and (memq 'table contexts)
  7586. (looking-at "[ \t]*|"))
  7587. (and (memq 'headline contexts)
  7588. (looking-at org-outline-regexp))
  7589. (and (memq 'item contexts)
  7590. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7591. (and (memq 'item-body contexts)
  7592. (org-in-item-p)))
  7593. (goto-char pos))))
  7594. (defun org-get-local-variables ()
  7595. "Return a list of all local variables in an org-mode buffer."
  7596. (let (varlist)
  7597. (with-current-buffer (get-buffer-create "*Org tmp*")
  7598. (erase-buffer)
  7599. (org-mode)
  7600. (setq varlist (buffer-local-variables)))
  7601. (kill-buffer "*Org tmp*")
  7602. (delq nil
  7603. (mapcar
  7604. (lambda (x)
  7605. (setq x
  7606. (if (symbolp x)
  7607. (list x)
  7608. (list (car x) (list 'quote (cdr x)))))
  7609. (if (string-match
  7610. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7611. (symbol-name (car x)))
  7612. x nil))
  7613. varlist))))
  7614. (defun org-clone-local-variables (from-buffer &optional regexp)
  7615. "Clone local variables from FROM-BUFFER.
  7616. Optional argument REGEXP selects variables to clone."
  7617. (mapc
  7618. (lambda (pair)
  7619. (and (symbolp (car pair))
  7620. (or (null regexp)
  7621. (string-match regexp (symbol-name (car pair))))
  7622. (set (make-local-variable (car pair))
  7623. (cdr pair))))
  7624. (buffer-local-variables from-buffer)))
  7625. ;;;###autoload
  7626. (defun org-run-like-in-org-mode (cmd)
  7627. "Run a command, pretending that the current buffer is in Org-mode.
  7628. This will temporarily bind local variables that are typically bound in
  7629. Org-mode to the values they have in Org-mode, and then interactively
  7630. call CMD."
  7631. (org-load-modules-maybe)
  7632. (unless org-local-vars
  7633. (setq org-local-vars (org-get-local-variables)))
  7634. (eval (list 'let org-local-vars
  7635. (list 'call-interactively (list 'quote cmd)))))
  7636. ;;;; Archiving
  7637. (defun org-get-category (&optional pos force-refresh)
  7638. "Get the category applying to position POS."
  7639. (save-match-data
  7640. (if force-refresh (org-refresh-category-properties))
  7641. (let ((pos (or pos (point))))
  7642. (or (get-text-property pos 'org-category)
  7643. (progn (org-refresh-category-properties)
  7644. (get-text-property pos 'org-category))))))
  7645. (defun org-refresh-category-properties ()
  7646. "Refresh category text properties in the buffer."
  7647. (let ((def-cat (cond
  7648. ((null org-category)
  7649. (if buffer-file-name
  7650. (file-name-sans-extension
  7651. (file-name-nondirectory buffer-file-name))
  7652. "???"))
  7653. ((symbolp org-category) (symbol-name org-category))
  7654. (t org-category)))
  7655. beg end cat pos optionp)
  7656. (org-unmodified
  7657. (save-excursion
  7658. (save-restriction
  7659. (widen)
  7660. (goto-char (point-min))
  7661. (put-text-property (point) (point-max) 'org-category def-cat)
  7662. (while (re-search-forward
  7663. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7664. (setq pos (match-end 0)
  7665. optionp (equal (char-after (match-beginning 0)) ?#)
  7666. cat (org-trim (match-string 2)))
  7667. (if optionp
  7668. (setq beg (point-at-bol) end (point-max))
  7669. (org-back-to-heading t)
  7670. (setq beg (point) end (org-end-of-subtree t t)))
  7671. (put-text-property beg end 'org-category cat)
  7672. (put-text-property beg end 'org-category-position beg)
  7673. (goto-char pos)))))))
  7674. ;;;; Link Stuff
  7675. ;;; Link abbreviations
  7676. (defun org-link-expand-abbrev (link)
  7677. "Apply replacements as defined in `org-link-abbrev-alist."
  7678. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7679. (let* ((key (match-string 1 link))
  7680. (as (or (assoc key org-link-abbrev-alist-local)
  7681. (assoc key org-link-abbrev-alist)))
  7682. (tag (and (match-end 2) (match-string 3 link)))
  7683. rpl)
  7684. (if (not as)
  7685. link
  7686. (setq rpl (cdr as))
  7687. (cond
  7688. ((symbolp rpl) (funcall rpl tag))
  7689. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7690. ((string-match "%h" rpl)
  7691. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7692. (t (concat rpl tag)))))
  7693. link))
  7694. ;;; Storing and inserting links
  7695. (defvar org-insert-link-history nil
  7696. "Minibuffer history for links inserted with `org-insert-link'.")
  7697. (defvar org-stored-links nil
  7698. "Contains the links stored with `org-store-link'.")
  7699. (defvar org-store-link-plist nil
  7700. "Plist with info about the most recently link created with `org-store-link'.")
  7701. (defvar org-link-protocols nil
  7702. "Link protocols added to Org-mode using `org-add-link-type'.")
  7703. (defvar org-store-link-functions nil
  7704. "List of functions that are called to create and store a link.
  7705. Each function will be called in turn until one returns a non-nil
  7706. value. Each function should check if it is responsible for creating
  7707. this link (for example by looking at the major mode).
  7708. If not, it must exit and return nil.
  7709. If yes, it should return a non-nil value after a calling
  7710. `org-store-link-props' with a list of properties and values.
  7711. Special properties are:
  7712. :type The link prefix, like \"http\". This must be given.
  7713. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7714. This is obligatory as well.
  7715. :description Optional default description for the second pair
  7716. of brackets in an Org-mode link. The user can still change
  7717. this when inserting this link into an Org-mode buffer.
  7718. In addition to these, any additional properties can be specified
  7719. and then used in remember templates.")
  7720. (defun org-add-link-type (type &optional follow export)
  7721. "Add TYPE to the list of `org-link-types'.
  7722. Re-compute all regular expressions depending on `org-link-types'
  7723. FOLLOW and EXPORT are two functions.
  7724. FOLLOW should take the link path as the single argument and do whatever
  7725. is necessary to follow the link, for example find a file or display
  7726. a mail message.
  7727. EXPORT should format the link path for export to one of the export formats.
  7728. It should be a function accepting three arguments:
  7729. path the path of the link, the text after the prefix (like \"http:\")
  7730. desc the description of the link, if any, or a description added by
  7731. org-export-normalize-links if there is none
  7732. format the export format, a symbol like `html' or `latex' or `ascii'..
  7733. The function may use the FORMAT information to return different values
  7734. depending on the format. The return value will be put literally into
  7735. the exported file. If the return value is nil, this means Org should
  7736. do what it normally does with links which do not have EXPORT defined.
  7737. Org-mode has a built-in default for exporting links. If you are happy with
  7738. this default, there is no need to define an export function for the link
  7739. type. For a simple example of an export function, see `org-bbdb.el'."
  7740. (add-to-list 'org-link-types type t)
  7741. (org-make-link-regexps)
  7742. (if (assoc type org-link-protocols)
  7743. (setcdr (assoc type org-link-protocols) (list follow export))
  7744. (push (list type follow export) org-link-protocols)))
  7745. (defvar org-agenda-buffer-name)
  7746. ;;;###autoload
  7747. (defun org-store-link (arg)
  7748. "\\<org-mode-map>Store an org-link to the current location.
  7749. This link is added to `org-stored-links' and can later be inserted
  7750. into an org-buffer with \\[org-insert-link].
  7751. For some link types, a prefix arg is interpreted:
  7752. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7753. For file links, arg negates `org-context-in-file-links'."
  7754. (interactive "P")
  7755. (org-load-modules-maybe)
  7756. (setq org-store-link-plist nil) ; reset
  7757. (org-with-limited-levels
  7758. (let (link cpltxt desc description search txt custom-id agenda-link)
  7759. (cond
  7760. ((run-hook-with-args-until-success 'org-store-link-functions)
  7761. (setq link (plist-get org-store-link-plist :link)
  7762. desc (or (plist-get org-store-link-plist :description) link)))
  7763. ((org-src-edit-buffer-p)
  7764. (let (label gc)
  7765. (while (or (not label)
  7766. (save-excursion
  7767. (save-restriction
  7768. (widen)
  7769. (goto-char (point-min))
  7770. (re-search-forward
  7771. (regexp-quote (format org-coderef-label-format label))
  7772. nil t))))
  7773. (when label (message "Label exists already") (sit-for 2))
  7774. (setq label (read-string "Code line label: " label)))
  7775. (end-of-line 1)
  7776. (setq link (format org-coderef-label-format label))
  7777. (setq gc (- 79 (length link)))
  7778. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7779. (insert link)
  7780. (setq link (concat "(" label ")") desc nil)))
  7781. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7782. ;; We are in the agenda, link to referenced location
  7783. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7784. (get-text-property (point) 'org-marker))))
  7785. (when m
  7786. (org-with-point-at m
  7787. (setq agenda-link
  7788. (if (org-called-interactively-p 'any)
  7789. (call-interactively 'org-store-link)
  7790. (org-store-link nil)))))))
  7791. ((eq major-mode 'calendar-mode)
  7792. (let ((cd (calendar-cursor-to-date)))
  7793. (setq link
  7794. (format-time-string
  7795. (car org-time-stamp-formats)
  7796. (apply 'encode-time
  7797. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7798. nil nil nil))))
  7799. (org-store-link-props :type "calendar" :date cd)))
  7800. ((eq major-mode 'w3-mode)
  7801. (setq cpltxt (if (and (buffer-name)
  7802. (not (string-match "Untitled" (buffer-name))))
  7803. (buffer-name)
  7804. (url-view-url t))
  7805. link (org-make-link (url-view-url t)))
  7806. (org-store-link-props :type "w3" :url (url-view-url t)))
  7807. ((eq major-mode 'w3m-mode)
  7808. (setq cpltxt (or w3m-current-title w3m-current-url)
  7809. link (org-make-link w3m-current-url))
  7810. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7811. ((setq search (run-hook-with-args-until-success
  7812. 'org-create-file-search-functions))
  7813. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7814. "::" search))
  7815. (setq cpltxt (or description link)))
  7816. ((eq major-mode 'image-mode)
  7817. (setq cpltxt (concat "file:"
  7818. (abbreviate-file-name buffer-file-name))
  7819. link (org-make-link cpltxt))
  7820. (org-store-link-props :type "image" :file buffer-file-name))
  7821. ((eq major-mode 'dired-mode)
  7822. ;; link to the file in the current line
  7823. (let ((file (dired-get-filename nil t)))
  7824. (setq file (if file
  7825. (abbreviate-file-name
  7826. (expand-file-name (dired-get-filename nil t)))
  7827. ;; otherwise, no file so use current directory.
  7828. default-directory))
  7829. (setq cpltxt (concat "file:" file)
  7830. link (org-make-link cpltxt))))
  7831. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7832. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7833. (cond
  7834. ((org-in-regexp "<<\\(.*?\\)>>")
  7835. (setq cpltxt
  7836. (concat "file:"
  7837. (abbreviate-file-name
  7838. (buffer-file-name (buffer-base-buffer)))
  7839. "::" (match-string 1))
  7840. link (org-make-link cpltxt)))
  7841. ((and (featurep 'org-id)
  7842. (or (eq org-link-to-org-use-id t)
  7843. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7844. (org-called-interactively-p 'any))
  7845. (and (eq org-link-to-org-use-id
  7846. 'create-if-interactive-and-no-custom-id)
  7847. (org-called-interactively-p 'any)
  7848. (not custom-id))
  7849. (and org-link-to-org-use-id
  7850. (org-entry-get nil "ID"))))
  7851. ;; We can make a link using the ID.
  7852. (setq link (condition-case nil
  7853. (prog1 (org-id-store-link)
  7854. (setq desc (plist-get org-store-link-plist
  7855. :description)))
  7856. (error
  7857. ;; probably before first headline, link to file only
  7858. (concat "file:"
  7859. (abbreviate-file-name
  7860. (buffer-file-name (buffer-base-buffer))))))))
  7861. (t
  7862. ;; Just link to current headline
  7863. (setq cpltxt (concat "file:"
  7864. (abbreviate-file-name
  7865. (buffer-file-name (buffer-base-buffer)))))
  7866. ;; Add a context search string
  7867. (when (org-xor org-context-in-file-links arg)
  7868. (setq txt (cond
  7869. ((org-at-heading-p) nil)
  7870. ((org-region-active-p)
  7871. (buffer-substring (region-beginning) (region-end)))
  7872. (t nil)))
  7873. (when (or (null txt) (string-match "\\S-" txt))
  7874. (setq cpltxt
  7875. (concat cpltxt "::"
  7876. (condition-case nil
  7877. (org-make-org-heading-search-string txt)
  7878. (error "")))
  7879. desc (or (nth 4 (ignore-errors
  7880. (org-heading-components))) "NONE"))))
  7881. (if (string-match "::\\'" cpltxt)
  7882. (setq cpltxt (substring cpltxt 0 -2)))
  7883. (setq link (org-make-link cpltxt)))))
  7884. ((buffer-file-name (buffer-base-buffer))
  7885. ;; Just link to this file here.
  7886. (setq cpltxt (concat "file:"
  7887. (abbreviate-file-name
  7888. (buffer-file-name (buffer-base-buffer)))))
  7889. ;; Add a context string
  7890. (when (org-xor org-context-in-file-links arg)
  7891. (setq txt (if (org-region-active-p)
  7892. (buffer-substring (region-beginning) (region-end))
  7893. (buffer-substring (point-at-bol) (point-at-eol))))
  7894. ;; Only use search option if there is some text.
  7895. (when (string-match "\\S-" txt)
  7896. (setq cpltxt
  7897. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7898. desc "NONE")))
  7899. (setq link (org-make-link cpltxt)))
  7900. ((org-called-interactively-p 'interactive)
  7901. (error "Cannot link to a buffer which is not visiting a file"))
  7902. (t (setq link nil)))
  7903. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7904. (setq link (or link cpltxt)
  7905. desc (or desc cpltxt))
  7906. (if (equal desc "NONE") (setq desc nil))
  7907. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7908. (progn
  7909. (setq org-stored-links
  7910. (cons (list link desc) org-stored-links))
  7911. (message "Stored: %s" (or desc link))
  7912. (when custom-id
  7913. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7914. "::#" custom-id))
  7915. (setq org-stored-links
  7916. (cons (list link desc) org-stored-links))))
  7917. (or agenda-link (and link (org-make-link-string link desc)))))))
  7918. (defun org-store-link-props (&rest plist)
  7919. "Store link properties, extract names and addresses."
  7920. (let (x adr)
  7921. (when (setq x (plist-get plist :from))
  7922. (setq adr (mail-extract-address-components x))
  7923. (setq plist (plist-put plist :fromname (car adr)))
  7924. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7925. (when (setq x (plist-get plist :to))
  7926. (setq adr (mail-extract-address-components x))
  7927. (setq plist (plist-put plist :toname (car adr)))
  7928. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7929. (let ((from (plist-get plist :from))
  7930. (to (plist-get plist :to)))
  7931. (when (and from to org-from-is-user-regexp)
  7932. (setq plist
  7933. (plist-put plist :fromto
  7934. (if (string-match org-from-is-user-regexp from)
  7935. (concat "to %t")
  7936. (concat "from %f"))))))
  7937. (setq org-store-link-plist plist))
  7938. (defun org-add-link-props (&rest plist)
  7939. "Add these properties to the link property list."
  7940. (let (key value)
  7941. (while plist
  7942. (setq key (pop plist) value (pop plist))
  7943. (setq org-store-link-plist
  7944. (plist-put org-store-link-plist key value)))))
  7945. (defun org-email-link-description (&optional fmt)
  7946. "Return the description part of an email link.
  7947. This takes information from `org-store-link-plist' and formats it
  7948. according to FMT (default from `org-email-link-description-format')."
  7949. (setq fmt (or fmt org-email-link-description-format))
  7950. (let* ((p org-store-link-plist)
  7951. (to (plist-get p :toaddress))
  7952. (from (plist-get p :fromaddress))
  7953. (table
  7954. (list
  7955. (cons "%c" (plist-get p :fromto))
  7956. (cons "%F" (plist-get p :from))
  7957. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7958. (cons "%T" (plist-get p :to))
  7959. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7960. (cons "%s" (plist-get p :subject))
  7961. (cons "%d" (plist-get p :date))
  7962. (cons "%m" (plist-get p :message-id)))))
  7963. (when (string-match "%c" fmt)
  7964. ;; Check if the user wrote this message
  7965. (if (and org-from-is-user-regexp from to
  7966. (save-match-data (string-match org-from-is-user-regexp from)))
  7967. (setq fmt (replace-match "to %t" t t fmt))
  7968. (setq fmt (replace-match "from %f" t t fmt))))
  7969. (org-replace-escapes fmt table)))
  7970. (defun org-make-org-heading-search-string (&optional string heading)
  7971. "Make search string for STRING or current headline."
  7972. (interactive)
  7973. (let ((s (or string (org-get-heading)))
  7974. (lines org-context-in-file-links))
  7975. (unless (and string (not heading))
  7976. ;; We are using a headline, clean up garbage in there.
  7977. (if (string-match org-todo-regexp s)
  7978. (setq s (replace-match "" t t s)))
  7979. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7980. (setq s (replace-match "" t t s)))
  7981. (setq s (org-trim s))
  7982. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7983. org-comment-string "\\)") s)
  7984. (setq s (replace-match "" t t s)))
  7985. (while (string-match org-ts-regexp s)
  7986. (setq s (replace-match "" t t s))))
  7987. (or string (setq s (concat "*" s))) ; Add * for headlines
  7988. (when (and string (integerp lines) (> lines 0))
  7989. (let ((slines (org-split-string s "\n")))
  7990. (when (< lines (length slines))
  7991. (setq s (mapconcat
  7992. 'identity
  7993. (reverse (nthcdr (- (length slines) lines)
  7994. (reverse slines))) "\n")))))
  7995. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7996. (defun org-make-link (&rest strings)
  7997. "Concatenate STRINGS."
  7998. (apply 'concat strings))
  7999. (defun org-make-link-string (link &optional description)
  8000. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8001. (unless (string-match "\\S-" link)
  8002. (error "Empty link"))
  8003. (when (and description
  8004. (stringp description)
  8005. (not (string-match "\\S-" description)))
  8006. (setq description nil))
  8007. (when (stringp description)
  8008. ;; Remove brackets from the description, they are fatal.
  8009. (while (string-match "\\[" description)
  8010. (setq description (replace-match "{" t t description)))
  8011. (while (string-match "\\]" description)
  8012. (setq description (replace-match "}" t t description))))
  8013. (when (equal link description)
  8014. ;; No description needed, it is identical
  8015. (setq description nil))
  8016. (when (and (not description)
  8017. (not (string-match (org-image-file-name-regexp) link))
  8018. (not (equal link (org-link-escape link))))
  8019. (setq description (org-extract-attributes link)))
  8020. (setq link
  8021. (cond ((string-match (org-image-file-name-regexp) link) link)
  8022. ((string-match org-link-types-re link)
  8023. (concat (match-string 1 link)
  8024. (org-link-escape (substring link (match-end 1)))))
  8025. (t (org-link-escape link))))
  8026. (concat "[[" link "]"
  8027. (if description (concat "[" description "]") "")
  8028. "]"))
  8029. (defconst org-link-escape-chars
  8030. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8031. "List of characters that should be escaped in link.
  8032. This is the list that is used for internal purposes.")
  8033. (defvar org-url-encoding-use-url-hexify nil)
  8034. (defconst org-link-escape-chars-browser
  8035. '(?\ )
  8036. "List of escapes for characters that are problematic in links.
  8037. This is the list that is used before handing over to the browser.")
  8038. (defun org-link-escape (text &optional table merge)
  8039. "Return percent escaped representation of TEXT.
  8040. TEXT is a string with the text to escape.
  8041. Optional argument TABLE is a list with characters that should be
  8042. escaped. When nil, `org-link-escape-chars' is used.
  8043. If optional argument MERGE is set, merge TABLE into
  8044. `org-link-escape-chars'."
  8045. (if (and org-url-encoding-use-url-hexify (not table))
  8046. (url-hexify-string text)
  8047. (cond
  8048. ((and table merge)
  8049. (mapc (lambda (defchr)
  8050. (unless (member defchr table)
  8051. (setq table (cons defchr table)))) org-link-escape-chars))
  8052. ((null table)
  8053. (setq table org-link-escape-chars)))
  8054. (mapconcat
  8055. (lambda (char)
  8056. (if (or (member char table)
  8057. (< char 32) (= char 37) (> char 126))
  8058. (mapconcat (lambda (sequence-element)
  8059. (format "%%%.2X" sequence-element))
  8060. (or (encode-coding-char char 'utf-8)
  8061. (error "Unable to percent escape character: %s"
  8062. (char-to-string char))) "")
  8063. (char-to-string char))) text "")))
  8064. (defun org-link-unescape (str)
  8065. "Unhex hexified Unicode strings as returned from the JavaScript function
  8066. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8067. (unless (and (null str) (string= "" str))
  8068. (let ((pos 0) (case-fold-search t) unhexed)
  8069. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8070. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8071. (setq str (replace-match unhexed t t str))
  8072. (setq pos (+ pos (length unhexed))))))
  8073. str)
  8074. (defun org-link-unescape-compound (hex)
  8075. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8076. Note: this function also decodes single byte encodings like
  8077. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8078. (save-match-data
  8079. (let* ((bytes (cdr (split-string hex "%")))
  8080. (ret "")
  8081. (eat 0)
  8082. (sum 0))
  8083. (while bytes
  8084. (let* ((val (string-to-number (pop bytes) 16))
  8085. (shift-xor
  8086. (if (= 0 eat)
  8087. (cond
  8088. ((>= val 252) (cons 6 252))
  8089. ((>= val 248) (cons 5 248))
  8090. ((>= val 240) (cons 4 240))
  8091. ((>= val 224) (cons 3 224))
  8092. ((>= val 192) (cons 2 192))
  8093. (t (cons 0 0)))
  8094. (cons 6 128))))
  8095. (if (>= val 192) (setq eat (car shift-xor)))
  8096. (setq val (logxor val (cdr shift-xor)))
  8097. (setq sum (+ (lsh sum (car shift-xor)) val))
  8098. (if (> eat 0) (setq eat (- eat 1)))
  8099. (cond
  8100. ((= 0 eat) ;multi byte
  8101. (setq ret (concat ret (org-char-to-string sum)))
  8102. (setq sum 0))
  8103. ((not bytes) ; single byte(s)
  8104. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8105. )) ;; end (while bytes
  8106. ret )))
  8107. (defun org-link-unescape-single-byte-sequence (hex)
  8108. "Unhexify hex-encoded single byte character sequences."
  8109. (mapconcat (lambda (byte)
  8110. (char-to-string (string-to-number byte 16)))
  8111. (cdr (split-string hex "%")) ""))
  8112. (defun org-xor (a b)
  8113. "Exclusive or."
  8114. (if a (not b) b))
  8115. (defun org-fixup-message-id-for-http (s)
  8116. "Replace special characters in a message id, so it can be used in an http query."
  8117. (when (string-match "%" s)
  8118. (setq s (mapconcat (lambda (c)
  8119. (if (eq c ?%)
  8120. "%25"
  8121. (char-to-string c)))
  8122. s "")))
  8123. (while (string-match "<" s)
  8124. (setq s (replace-match "%3C" t t s)))
  8125. (while (string-match ">" s)
  8126. (setq s (replace-match "%3E" t t s)))
  8127. (while (string-match "@" s)
  8128. (setq s (replace-match "%40" t t s)))
  8129. s)
  8130. ;;;###autoload
  8131. (defun org-insert-link-global ()
  8132. "Insert a link like Org-mode does.
  8133. This command can be called in any mode to insert a link in Org-mode syntax."
  8134. (interactive)
  8135. (org-load-modules-maybe)
  8136. (org-run-like-in-org-mode 'org-insert-link))
  8137. (defun org-insert-link (&optional complete-file link-location default-description)
  8138. "Insert a link. At the prompt, enter the link.
  8139. Completion can be used to insert any of the link protocol prefixes like
  8140. http or ftp in use.
  8141. The history can be used to select a link previously stored with
  8142. `org-store-link'. When the empty string is entered (i.e. if you just
  8143. press RET at the prompt), the link defaults to the most recently
  8144. stored link. As SPC triggers completion in the minibuffer, you need to
  8145. use M-SPC or C-q SPC to force the insertion of a space character.
  8146. You will also be prompted for a description, and if one is given, it will
  8147. be displayed in the buffer instead of the link.
  8148. If there is already a link at point, this command will allow you to edit link
  8149. and description parts.
  8150. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8151. be selected using completion. The path to the file will be relative to the
  8152. current directory if the file is in the current directory or a subdirectory.
  8153. Otherwise, the link will be the absolute path as completed in the minibuffer
  8154. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8155. option `org-link-file-path-type'.
  8156. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8157. the current directory or below.
  8158. With three \\[universal-argument] prefixes, negate the meaning of
  8159. `org-keep-stored-link-after-insertion'.
  8160. If `org-make-link-description-function' is non-nil, this function will be
  8161. called with the link target, and the result will be the default
  8162. link description.
  8163. If the LINK-LOCATION parameter is non-nil, this value will be
  8164. used as the link location instead of reading one interactively.
  8165. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8166. be used as the default description."
  8167. (interactive "P")
  8168. (let* ((wcf (current-window-configuration))
  8169. (region (if (org-region-active-p)
  8170. (buffer-substring (region-beginning) (region-end))))
  8171. (remove (and region (list (region-beginning) (region-end))))
  8172. (desc region)
  8173. tmphist ; byte-compile incorrectly complains about this
  8174. (link link-location)
  8175. entry file all-prefixes)
  8176. (cond
  8177. (link-location) ; specified by arg, just use it.
  8178. ((org-in-regexp org-bracket-link-regexp 1)
  8179. ;; We do have a link at point, and we are going to edit it.
  8180. (setq remove (list (match-beginning 0) (match-end 0)))
  8181. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8182. (setq link (read-string "Link: "
  8183. (org-link-unescape
  8184. (org-match-string-no-properties 1)))))
  8185. ((or (org-in-regexp org-angle-link-re)
  8186. (org-in-regexp org-plain-link-re))
  8187. ;; Convert to bracket link
  8188. (setq remove (list (match-beginning 0) (match-end 0))
  8189. link (read-string "Link: "
  8190. (org-remove-angle-brackets (match-string 0)))))
  8191. ((member complete-file '((4) (16)))
  8192. ;; Completing read for file names.
  8193. (setq link (org-file-complete-link complete-file)))
  8194. (t
  8195. ;; Read link, with completion for stored links.
  8196. (with-output-to-temp-buffer "*Org Links*"
  8197. (princ "Insert a link.
  8198. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8199. (when org-stored-links
  8200. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8201. (princ (mapconcat
  8202. (lambda (x)
  8203. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8204. (reverse org-stored-links) "\n"))))
  8205. (let ((cw (selected-window)))
  8206. (select-window (get-buffer-window "*Org Links*" 'visible))
  8207. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8208. (unless (pos-visible-in-window-p (point-max))
  8209. (org-fit-window-to-buffer))
  8210. (and (window-live-p cw) (select-window cw)))
  8211. ;; Fake a link history, containing the stored links.
  8212. (setq tmphist (append (mapcar 'car org-stored-links)
  8213. org-insert-link-history))
  8214. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8215. (mapcar 'car org-link-abbrev-alist)
  8216. org-link-types))
  8217. (unwind-protect
  8218. (progn
  8219. (setq link
  8220. (let ((org-completion-use-ido nil)
  8221. (org-completion-use-iswitchb nil))
  8222. (org-completing-read
  8223. "Link: "
  8224. (append
  8225. (mapcar (lambda (x) (list (concat x ":")))
  8226. all-prefixes)
  8227. (mapcar 'car org-stored-links))
  8228. nil nil nil
  8229. 'tmphist
  8230. (car (car org-stored-links)))))
  8231. (if (not (string-match "\\S-" link))
  8232. (error "No link selected"))
  8233. (if (or (member link all-prefixes)
  8234. (and (equal ":" (substring link -1))
  8235. (member (substring link 0 -1) all-prefixes)
  8236. (setq link (substring link 0 -1))))
  8237. (setq link (org-link-try-special-completion link))))
  8238. (set-window-configuration wcf)
  8239. (kill-buffer "*Org Links*"))
  8240. (setq entry (assoc link org-stored-links))
  8241. (or entry (push link org-insert-link-history))
  8242. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8243. (not org-keep-stored-link-after-insertion))
  8244. (setq org-stored-links (delq (assoc link org-stored-links)
  8245. org-stored-links)))
  8246. (setq desc (or desc (nth 1 entry)))))
  8247. (if (string-match org-plain-link-re link)
  8248. ;; URL-like link, normalize the use of angular brackets.
  8249. (setq link (org-make-link (org-remove-angle-brackets link))))
  8250. ;; Check if we are linking to the current file with a search option
  8251. ;; If yes, simplify the link by using only the search option.
  8252. (when (and buffer-file-name
  8253. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8254. (let* ((path (match-string 1 link))
  8255. (case-fold-search nil)
  8256. (search (match-string 2 link)))
  8257. (save-match-data
  8258. (if (equal (file-truename buffer-file-name) (file-truename path))
  8259. ;; We are linking to this same file, with a search option
  8260. (setq link search)))))
  8261. ;; Check if we can/should use a relative path. If yes, simplify the link
  8262. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8263. (let* ((type (match-string 1 link))
  8264. (path (match-string 2 link))
  8265. (origpath path)
  8266. (case-fold-search nil))
  8267. (cond
  8268. ((or (eq org-link-file-path-type 'absolute)
  8269. (equal complete-file '(16)))
  8270. (setq path (abbreviate-file-name (expand-file-name path))))
  8271. ((eq org-link-file-path-type 'noabbrev)
  8272. (setq path (expand-file-name path)))
  8273. ((eq org-link-file-path-type 'relative)
  8274. (setq path (file-relative-name path)))
  8275. (t
  8276. (save-match-data
  8277. (if (string-match (concat "^" (regexp-quote
  8278. (expand-file-name
  8279. (file-name-as-directory
  8280. default-directory))))
  8281. (expand-file-name path))
  8282. ;; We are linking a file with relative path name.
  8283. (setq path (substring (expand-file-name path)
  8284. (match-end 0)))
  8285. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8286. (setq link (concat type path))
  8287. (if (equal desc origpath)
  8288. (setq desc path))))
  8289. (if org-make-link-description-function
  8290. (setq desc (funcall org-make-link-description-function link desc))
  8291. (if default-description (setq desc default-description)))
  8292. (setq desc (read-string "Description: " desc))
  8293. (unless (string-match "\\S-" desc) (setq desc nil))
  8294. (if remove (apply 'delete-region remove))
  8295. (insert (org-make-link-string link desc))))
  8296. (defun org-link-try-special-completion (type)
  8297. "If there is completion support for link type TYPE, offer it."
  8298. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8299. (if (functionp fun)
  8300. (funcall fun)
  8301. (read-string "Link (no completion support): " (concat type ":")))))
  8302. (defun org-file-complete-link (&optional arg)
  8303. "Create a file link using completion."
  8304. (let (file link)
  8305. (setq file (read-file-name "File: "))
  8306. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8307. (pwd1 (file-name-as-directory (abbreviate-file-name
  8308. (expand-file-name ".")))))
  8309. (cond
  8310. ((equal arg '(16))
  8311. (setq link (org-make-link
  8312. "file:"
  8313. (abbreviate-file-name (expand-file-name file)))))
  8314. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8315. (setq link (org-make-link "file:" (match-string 1 file))))
  8316. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8317. (expand-file-name file))
  8318. (setq link (org-make-link
  8319. "file:" (match-string 1 (expand-file-name file)))))
  8320. (t (setq link (org-make-link "file:" file)))))
  8321. link))
  8322. (defun org-completing-read (&rest args)
  8323. "Completing-read with SPACE being a normal character."
  8324. (let ((enable-recursive-minibuffers t)
  8325. (minibuffer-local-completion-map
  8326. (copy-keymap minibuffer-local-completion-map)))
  8327. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8328. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8329. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8330. (apply 'org-icompleting-read args)))
  8331. (defun org-completing-read-no-i (&rest args)
  8332. (let (org-completion-use-ido org-completion-use-iswitchb)
  8333. (apply 'org-completing-read args)))
  8334. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8335. "Use iswitch as a completing-read replacement to choose from choices.
  8336. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8337. from."
  8338. (let* ((iswitchb-use-virtual-buffers nil)
  8339. (iswitchb-make-buflist-hook
  8340. (lambda ()
  8341. (setq iswitchb-temp-buflist choices))))
  8342. (iswitchb-read-buffer prompt)))
  8343. (defun org-icompleting-read (&rest args)
  8344. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8345. (org-without-partial-completion
  8346. (if (and org-completion-use-ido
  8347. (fboundp 'ido-completing-read)
  8348. (boundp 'ido-mode) ido-mode
  8349. (listp (second args)))
  8350. (let ((ido-enter-matching-directory nil))
  8351. (apply 'ido-completing-read (concat (car args))
  8352. (if (consp (car (nth 1 args)))
  8353. (mapcar 'car (nth 1 args))
  8354. (nth 1 args))
  8355. (cddr args)))
  8356. (if (and org-completion-use-iswitchb
  8357. (boundp 'iswitchb-mode) iswitchb-mode
  8358. (listp (second args)))
  8359. (apply 'org-iswitchb-completing-read (concat (car args))
  8360. (if (consp (car (nth 1 args)))
  8361. (mapcar 'car (nth 1 args))
  8362. (nth 1 args))
  8363. (cddr args))
  8364. (apply 'completing-read args)))))
  8365. (defun org-extract-attributes (s)
  8366. "Extract the attributes cookie from a string and set as text property."
  8367. (let (a attr (start 0) key value)
  8368. (save-match-data
  8369. (when (string-match "{{\\([^}]+\\)}}$" s)
  8370. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8371. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8372. (setq key (match-string 1 a) value (match-string 2 a)
  8373. start (match-end 0)
  8374. attr (plist-put attr (intern key) value))))
  8375. (org-add-props s nil 'org-attr attr))
  8376. s))
  8377. (defun org-extract-attributes-from-string (tag)
  8378. (let (key value attr)
  8379. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8380. (setq key (match-string 1 tag) value (match-string 2 tag)
  8381. tag (replace-match "" t t tag)
  8382. attr (plist-put attr (intern key) value)))
  8383. (cons tag attr)))
  8384. (defun org-attributes-to-string (plist)
  8385. "Format a property list into an HTML attribute list."
  8386. (let ((s "") key value)
  8387. (while plist
  8388. (setq key (pop plist) value (pop plist))
  8389. (and value
  8390. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8391. s))
  8392. ;;; Opening/following a link
  8393. (defvar org-link-search-failed nil)
  8394. (defvar org-open-link-functions nil
  8395. "Hook for functions finding a plain text link.
  8396. These functions must take a single argument, the link content.
  8397. They will be called for links that look like [[link text][description]]
  8398. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8399. like a filename (e.g. \"./blue.png\").
  8400. These functions will be called *before* Org attempts to resolve the
  8401. link by doing text searches in the current buffer - so if you want a
  8402. link \"[[target]]\" to still find \"<<target>>\", your function should
  8403. handle this as a special case.
  8404. When the function does handle the link, it must return a non-nil value.
  8405. If it decides that it is not responsible for this link, it must return
  8406. nil to indicate that that Org-mode can continue with other options
  8407. like exact and fuzzy text search.")
  8408. (defun org-next-link ()
  8409. "Move forward to the next link.
  8410. If the link is in hidden text, expose it."
  8411. (interactive)
  8412. (when (and org-link-search-failed (eq this-command last-command))
  8413. (goto-char (point-min))
  8414. (message "Link search wrapped back to beginning of buffer"))
  8415. (setq org-link-search-failed nil)
  8416. (let* ((pos (point))
  8417. (ct (org-context))
  8418. (a (assoc :link ct)))
  8419. (if a (goto-char (nth 2 a)))
  8420. (if (re-search-forward org-any-link-re nil t)
  8421. (progn
  8422. (goto-char (match-beginning 0))
  8423. (if (outline-invisible-p) (org-show-context)))
  8424. (goto-char pos)
  8425. (setq org-link-search-failed t)
  8426. (error "No further link found"))))
  8427. (defun org-previous-link ()
  8428. "Move backward to the previous link.
  8429. If the link is in hidden text, expose it."
  8430. (interactive)
  8431. (when (and org-link-search-failed (eq this-command last-command))
  8432. (goto-char (point-max))
  8433. (message "Link search wrapped back to end of buffer"))
  8434. (setq org-link-search-failed nil)
  8435. (let* ((pos (point))
  8436. (ct (org-context))
  8437. (a (assoc :link ct)))
  8438. (if a (goto-char (nth 1 a)))
  8439. (if (re-search-backward org-any-link-re nil t)
  8440. (progn
  8441. (goto-char (match-beginning 0))
  8442. (if (outline-invisible-p) (org-show-context)))
  8443. (goto-char pos)
  8444. (setq org-link-search-failed t)
  8445. (error "No further link found"))))
  8446. (defun org-translate-link (s)
  8447. "Translate a link string if a translation function has been defined."
  8448. (if (and org-link-translation-function
  8449. (fboundp org-link-translation-function)
  8450. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8451. (progn
  8452. (setq s (funcall org-link-translation-function
  8453. (match-string 1) (match-string 2)))
  8454. (concat (car s) ":" (cdr s)))
  8455. s))
  8456. (defun org-translate-link-from-planner (type path)
  8457. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8458. This is still an experimental function, your mileage may vary."
  8459. (cond
  8460. ((member type '("http" "https" "news" "ftp"))
  8461. ;; standard Internet links are the same.
  8462. nil)
  8463. ((and (equal type "irc") (string-match "^//" path))
  8464. ;; Planner has two / at the beginning of an irc link, we have 1.
  8465. ;; We should have zero, actually....
  8466. (setq path (substring path 1)))
  8467. ((and (equal type "lisp") (string-match "^/" path))
  8468. ;; Planner has a slash, we do not.
  8469. (setq type "elisp" path (substring path 1)))
  8470. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8471. ;; A typical message link. Planner has the id after the final slash,
  8472. ;; we separate it with a hash mark
  8473. (setq path (concat (match-string 1 path) "#"
  8474. (org-remove-angle-brackets (match-string 2 path)))))
  8475. )
  8476. (cons type path))
  8477. (defun org-find-file-at-mouse (ev)
  8478. "Open file link or URL at mouse."
  8479. (interactive "e")
  8480. (mouse-set-point ev)
  8481. (org-open-at-point 'in-emacs))
  8482. (defun org-open-at-mouse (ev)
  8483. "Open file link or URL at mouse.
  8484. See the docstring of `org-open-file' for details."
  8485. (interactive "e")
  8486. (mouse-set-point ev)
  8487. (if (eq major-mode 'org-agenda-mode)
  8488. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8489. (org-open-at-point))
  8490. (defvar org-window-config-before-follow-link nil
  8491. "The window configuration before following a link.
  8492. This is saved in case the need arises to restore it.")
  8493. (defvar org-open-link-marker (make-marker)
  8494. "Marker pointing to the location where `org-open-at-point; was called.")
  8495. ;;;###autoload
  8496. (defun org-open-at-point-global ()
  8497. "Follow a link like Org-mode does.
  8498. This command can be called in any mode to follow a link that has
  8499. Org-mode syntax."
  8500. (interactive)
  8501. (org-run-like-in-org-mode 'org-open-at-point))
  8502. ;;;###autoload
  8503. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8504. "Open a link in the string S, as if it was in Org-mode."
  8505. (interactive "sLink: \nP")
  8506. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8507. (with-temp-buffer
  8508. (let ((org-inhibit-startup (not reference-buffer)))
  8509. (org-mode)
  8510. (insert s)
  8511. (goto-char (point-min))
  8512. (when reference-buffer
  8513. (setq org-link-abbrev-alist-local
  8514. (with-current-buffer reference-buffer
  8515. org-link-abbrev-alist-local)))
  8516. (org-open-at-point arg reference-buffer)))))
  8517. (defvar org-open-at-point-functions nil
  8518. "Hook that is run when following a link at point.
  8519. Functions in this hook must return t if they identify and follow
  8520. a link at point. If they don't find anything interesting at point,
  8521. they must return nil.")
  8522. (defun org-open-at-point (&optional arg reference-buffer)
  8523. "Open link at or after point.
  8524. If there is no link at point, this function will search forward up to
  8525. the end of the current line.
  8526. Normally, files will be opened by an appropriate application. If the
  8527. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8528. With a double prefix argument, try to open outside of Emacs, in the
  8529. application the system uses for this file type."
  8530. (interactive "P")
  8531. ;; if in a code block, then open the block's results
  8532. (unless (call-interactively #'org-babel-open-src-block-result)
  8533. (org-load-modules-maybe)
  8534. (move-marker org-open-link-marker (point))
  8535. (setq org-window-config-before-follow-link (current-window-configuration))
  8536. (org-remove-occur-highlights nil nil t)
  8537. (cond
  8538. ((and (org-at-heading-p)
  8539. (not (org-in-regexp
  8540. (concat org-plain-link-re "\\|"
  8541. org-bracket-link-regexp "\\|"
  8542. org-angle-link-re "\\|"
  8543. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8544. (not (get-text-property (point) 'org-linked-text)))
  8545. (or (org-offer-links-in-entry arg)
  8546. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8547. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8548. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8549. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8550. (not (org-in-regexp org-bracket-link-regexp)))
  8551. (org-footnote-action))
  8552. (t
  8553. (let (type path link line search (pos (point)))
  8554. (catch 'match
  8555. (save-excursion
  8556. (skip-chars-forward "^]\n\r")
  8557. (when (org-in-regexp org-bracket-link-regexp 1)
  8558. (setq link (org-extract-attributes
  8559. (org-link-unescape (org-match-string-no-properties 1))))
  8560. (while (string-match " *\n *" link)
  8561. (setq link (replace-match " " t t link)))
  8562. (setq link (org-link-expand-abbrev link))
  8563. (cond
  8564. ((or (file-name-absolute-p link)
  8565. (string-match "^\\.\\.?/" link))
  8566. (setq type "file" path link))
  8567. ((string-match org-link-re-with-space3 link)
  8568. (setq type (match-string 1 link) path (match-string 2 link)))
  8569. (t (setq type "thisfile" path link)))
  8570. (throw 'match t)))
  8571. (when (get-text-property (point) 'org-linked-text)
  8572. (setq type "thisfile"
  8573. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8574. (1+ (point)) (point))
  8575. path (buffer-substring
  8576. (or (previous-single-property-change pos 'org-linked-text)
  8577. (point-min))
  8578. (or (next-single-property-change pos 'org-linked-text)
  8579. (point-max))))
  8580. (throw 'match t))
  8581. (save-excursion
  8582. (when (or (org-in-regexp org-angle-link-re)
  8583. (org-in-regexp org-plain-link-re))
  8584. (setq type (match-string 1)
  8585. path (org-link-unescape (match-string 2)))
  8586. (throw 'match t)))
  8587. (save-excursion
  8588. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8589. (setq type "tags"
  8590. path (match-string 1))
  8591. (while (string-match ":" path)
  8592. (setq path (replace-match "+" t t path)))
  8593. (throw 'match t)))
  8594. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8595. (setq type "tree-match"
  8596. path (match-string 1))
  8597. (throw 'match t)))
  8598. (unless path
  8599. (error "No link found"))
  8600. ;; switch back to reference buffer
  8601. ;; needed when if called in a temporary buffer through
  8602. ;; org-open-link-from-string
  8603. (with-current-buffer (or reference-buffer (current-buffer))
  8604. ;; Remove any trailing spaces in path
  8605. (if (string-match " +\\'" path)
  8606. (setq path (replace-match "" t t path)))
  8607. (if (and org-link-translation-function
  8608. (fboundp org-link-translation-function))
  8609. ;; Check if we need to translate the link
  8610. (let ((tmp (funcall org-link-translation-function type path)))
  8611. (setq type (car tmp) path (cdr tmp))))
  8612. (cond
  8613. ((assoc type org-link-protocols)
  8614. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8615. ((equal type "mailto")
  8616. (let ((cmd (car org-link-mailto-program))
  8617. (args (cdr org-link-mailto-program)) args1
  8618. (address path) (subject "") a)
  8619. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8620. (setq address (match-string 1 path)
  8621. subject (org-link-escape (match-string 2 path))))
  8622. (while args
  8623. (cond
  8624. ((not (stringp (car args))) (push (pop args) args1))
  8625. (t (setq a (pop args))
  8626. (if (string-match "%a" a)
  8627. (setq a (replace-match address t t a)))
  8628. (if (string-match "%s" a)
  8629. (setq a (replace-match subject t t a)))
  8630. (push a args1))))
  8631. (apply cmd (nreverse args1))))
  8632. ((member type '("http" "https" "ftp" "news"))
  8633. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8634. (org-link-escape
  8635. path org-link-escape-chars-browser)
  8636. path))))
  8637. ((string= type "doi")
  8638. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8639. (org-link-escape
  8640. path org-link-escape-chars-browser)
  8641. path))))
  8642. ((member type '("message"))
  8643. (browse-url (concat type ":" path)))
  8644. ((string= type "tags")
  8645. (org-tags-view arg path))
  8646. ((string= type "tree-match")
  8647. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8648. ((string= type "file")
  8649. (if (string-match "::\\([0-9]+\\)\\'" path)
  8650. (setq line (string-to-number (match-string 1 path))
  8651. path (substring path 0 (match-beginning 0)))
  8652. (if (string-match "::\\(.+\\)\\'" path)
  8653. (setq search (match-string 1 path)
  8654. path (substring path 0 (match-beginning 0)))))
  8655. (if (string-match "[*?{]" (file-name-nondirectory path))
  8656. (dired path)
  8657. (org-open-file path arg line search)))
  8658. ((string= type "shell")
  8659. (let ((cmd path))
  8660. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8661. (string-match org-confirm-shell-link-not-regexp cmd))
  8662. (not org-confirm-shell-link-function)
  8663. (funcall org-confirm-shell-link-function
  8664. (format "Execute \"%s\" in shell? "
  8665. (org-add-props cmd nil
  8666. 'face 'org-warning))))
  8667. (progn
  8668. (message "Executing %s" cmd)
  8669. (shell-command cmd))
  8670. (error "Abort"))))
  8671. ((string= type "elisp")
  8672. (let ((cmd path))
  8673. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8674. (string-match org-confirm-elisp-link-not-regexp cmd))
  8675. (not org-confirm-elisp-link-function)
  8676. (funcall org-confirm-elisp-link-function
  8677. (format "Execute \"%s\" as elisp? "
  8678. (org-add-props cmd nil
  8679. 'face 'org-warning))))
  8680. (message "%s => %s" cmd
  8681. (if (equal (string-to-char cmd) ?\()
  8682. (eval (read cmd))
  8683. (call-interactively (read cmd))))
  8684. (error "Abort"))))
  8685. ((and (string= type "thisfile")
  8686. (run-hook-with-args-until-success
  8687. 'org-open-link-functions path)))
  8688. ((string= type "thisfile")
  8689. (if arg
  8690. (switch-to-buffer-other-window
  8691. (org-get-buffer-for-internal-link (current-buffer)))
  8692. (org-mark-ring-push))
  8693. (let ((cmd `(org-link-search
  8694. ,path
  8695. ,(cond ((equal arg '(4)) ''occur)
  8696. ((equal arg '(16)) ''org-occur)
  8697. (t nil))
  8698. ,pos)))
  8699. (condition-case nil (let ((org-link-search-inhibit-query t))
  8700. (eval cmd))
  8701. (error (progn (widen) (eval cmd))))))
  8702. (t
  8703. (browse-url-at-point)))))))
  8704. (move-marker org-open-link-marker nil)
  8705. (run-hook-with-args 'org-follow-link-hook)))
  8706. (defun org-offer-links-in-entry (&optional nth zero)
  8707. "Offer links in the current entry and follow the selected link.
  8708. If there is only one link, follow it immediately as well.
  8709. If NTH is an integer, immediately pick the NTH link found.
  8710. If ZERO is a string, check also this string for a link, and if
  8711. there is one, offer it as link number zero."
  8712. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8713. "\\(" org-angle-link-re "\\)\\|"
  8714. "\\(" org-plain-link-re "\\)"))
  8715. (cnt ?0)
  8716. (in-emacs (if (integerp nth) nil nth))
  8717. have-zero end links link c)
  8718. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8719. (push (match-string 0 zero) links)
  8720. (setq cnt (1- cnt) have-zero t))
  8721. (save-excursion
  8722. (org-back-to-heading t)
  8723. (setq end (save-excursion (outline-next-heading) (point)))
  8724. (while (re-search-forward re end t)
  8725. (push (match-string 0) links))
  8726. (setq links (org-uniquify (reverse links))))
  8727. (cond
  8728. ((null links)
  8729. (message "No links"))
  8730. ((equal (length links) 1)
  8731. (setq link (list (car links))))
  8732. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8733. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8734. (t ; we have to select a link
  8735. (save-excursion
  8736. (save-window-excursion
  8737. (delete-other-windows)
  8738. (with-output-to-temp-buffer "*Select Link*"
  8739. (mapc (lambda (l)
  8740. (if (not (string-match org-bracket-link-regexp l))
  8741. (princ (format "[%c] %s\n" (incf cnt)
  8742. (org-remove-angle-brackets l)))
  8743. (if (match-end 3)
  8744. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8745. (match-string 3 l) (match-string 1 l)))
  8746. (princ (format "[%c] %s\n" (incf cnt)
  8747. (match-string 1 l))))))
  8748. links))
  8749. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8750. (message "Select link to open, RET to open all:")
  8751. (setq c (read-char-exclusive))
  8752. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8753. (when (equal c ?q) (error "Abort"))
  8754. (if (equal c ?\C-m)
  8755. (setq link links)
  8756. (setq nth (- c ?0))
  8757. (if have-zero (setq nth (1+ nth)))
  8758. (unless (and (integerp nth) (>= (length links) nth))
  8759. (error "Invalid link selection"))
  8760. (setq link (list (nth (1- nth) links))))))
  8761. (if link
  8762. (let ((buf (current-buffer)))
  8763. (dolist (l link)
  8764. (org-open-link-from-string l in-emacs buf))
  8765. t)
  8766. nil)))
  8767. ;; Add special file links that specify the way of opening
  8768. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8769. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8770. (defun org-open-file-with-system (path)
  8771. "Open file at PATH using the system way of opening it."
  8772. (org-open-file path 'system))
  8773. (defun org-open-file-with-emacs (path)
  8774. "Open file at PATH in Emacs."
  8775. (org-open-file path 'emacs))
  8776. (defun org-remove-file-link-modifiers ()
  8777. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8778. (goto-char (point-min))
  8779. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8780. (org-if-unprotected
  8781. (replace-match "file:" t t))))
  8782. (eval-after-load "org-exp"
  8783. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8784. 'org-remove-file-link-modifiers))
  8785. ;;;; Time estimates
  8786. (defun org-get-effort (&optional pom)
  8787. "Get the effort estimate for the current entry."
  8788. (org-entry-get pom org-effort-property))
  8789. ;;; File search
  8790. (defvar org-create-file-search-functions nil
  8791. "List of functions to construct the right search string for a file link.
  8792. These functions are called in turn with point at the location to
  8793. which the link should point.
  8794. A function in the hook should first test if it would like to
  8795. handle this file type, for example by checking the `major-mode'
  8796. or the file extension. If it decides not to handle this file, it
  8797. should just return nil to give other functions a chance. If it
  8798. does handle the file, it must return the search string to be used
  8799. when following the link. The search string will be part of the
  8800. file link, given after a double colon, and `org-open-at-point'
  8801. will automatically search for it. If special measures must be
  8802. taken to make the search successful, another function should be
  8803. added to the companion hook `org-execute-file-search-functions',
  8804. which see.
  8805. A function in this hook may also use `setq' to set the variable
  8806. `description' to provide a suggestion for the descriptive text to
  8807. be used for this link when it gets inserted into an Org-mode
  8808. buffer with \\[org-insert-link].")
  8809. (defvar org-execute-file-search-functions nil
  8810. "List of functions to execute a file search triggered by a link.
  8811. Functions added to this hook must accept a single argument, the
  8812. search string that was part of the file link, the part after the
  8813. double colon. The function must first check if it would like to
  8814. handle this search, for example by checking the `major-mode' or
  8815. the file extension. If it decides not to handle this search, it
  8816. should just return nil to give other functions a chance. If it
  8817. does handle the search, it must return a non-nil value to keep
  8818. other functions from trying.
  8819. Each function can access the current prefix argument through the
  8820. variable `current-prefix-argument'. Note that a single prefix is
  8821. used to force opening a link in Emacs, so it may be good to only
  8822. use a numeric or double prefix to guide the search function.
  8823. In case this is needed, a function in this hook can also restore
  8824. the window configuration before `org-open-at-point' was called using:
  8825. (set-window-configuration org-window-config-before-follow-link)")
  8826. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8827. (defun org-link-search (s &optional type avoid-pos stealth)
  8828. "Search for a link search option.
  8829. If S is surrounded by forward slashes, it is interpreted as a
  8830. regular expression. In org-mode files, this will create an `org-occur'
  8831. sparse tree. In ordinary files, `occur' will be used to list matches.
  8832. If the current buffer is in `dired-mode', grep will be used to search
  8833. in all files. If AVOID-POS is given, ignore matches near that position.
  8834. When optional argument STEALTH is non-nil, do not modify
  8835. visibility around point, thus ignoring
  8836. `org-show-hierarchy-above', `org-show-following-heading' and
  8837. `org-show-siblings' variables."
  8838. (let ((case-fold-search t)
  8839. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8840. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8841. (append '(("") (" ") ("\t") ("\n"))
  8842. org-emphasis-alist)
  8843. "\\|") "\\)"))
  8844. (pos (point))
  8845. (pre nil) (post nil)
  8846. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8847. (cond
  8848. ;; First check if there are any special search functions
  8849. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8850. ;; Now try the builtin stuff
  8851. ((and (equal (string-to-char s0) ?#)
  8852. (> (length s0) 1)
  8853. (save-excursion
  8854. (goto-char (point-min))
  8855. (and
  8856. (re-search-forward
  8857. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8858. (setq type 'dedicated
  8859. pos (match-beginning 0))))
  8860. ;; There is an exact target for this
  8861. (goto-char pos)
  8862. (org-back-to-heading t)))
  8863. ((save-excursion
  8864. (goto-char (point-min))
  8865. (and
  8866. (re-search-forward
  8867. (concat "<<" (regexp-quote s0) ">>") nil t)
  8868. (setq type 'dedicated
  8869. pos (match-beginning 0))))
  8870. ;; There is an exact target for this
  8871. (goto-char pos))
  8872. ((and (string-match "^(\\(.*\\))$" s0)
  8873. (save-excursion
  8874. (goto-char (point-min))
  8875. (and
  8876. (re-search-forward
  8877. (concat "[^[]" (regexp-quote
  8878. (format org-coderef-label-format
  8879. (match-string 1 s0))))
  8880. nil t)
  8881. (setq type 'dedicated
  8882. pos (1+ (match-beginning 0))))))
  8883. ;; There is a coderef target for this
  8884. (goto-char pos))
  8885. ((string-match "^/\\(.*\\)/$" s)
  8886. ;; A regular expression
  8887. (cond
  8888. ((eq major-mode 'org-mode)
  8889. (org-occur (match-string 1 s)))
  8890. ;;((eq major-mode 'dired-mode)
  8891. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8892. (t (org-do-occur (match-string 1 s)))))
  8893. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8894. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8895. (goto-char (point-min))
  8896. (cond
  8897. ((let (case-fold-search)
  8898. (re-search-forward (format org-complex-heading-regexp-format
  8899. (regexp-quote s))
  8900. nil t))
  8901. ;; OK, found a match
  8902. (setq type 'dedicated)
  8903. (goto-char (match-beginning 0)))
  8904. ((and (not org-link-search-inhibit-query)
  8905. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8906. (y-or-n-p "No match - create this as a new heading? "))
  8907. (goto-char (point-max))
  8908. (or (bolp) (newline))
  8909. (insert "* " s "\n")
  8910. (beginning-of-line 0))
  8911. (t
  8912. (goto-char pos)
  8913. (error "No match"))))
  8914. (t
  8915. ;; A normal search string
  8916. (when (equal (string-to-char s) ?*)
  8917. ;; Anchor on headlines, post may include tags.
  8918. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8919. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8920. s (substring s 1)))
  8921. (remove-text-properties
  8922. 0 (length s)
  8923. '(face nil mouse-face nil keymap nil fontified nil) s)
  8924. ;; Make a series of regular expressions to find a match
  8925. (setq words (org-split-string s "[ \n\r\t]+")
  8926. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8927. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8928. "\\)" markers)
  8929. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8930. re2a (concat "[ \t\r\n]" re2a_)
  8931. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8932. re4 (concat "[^a-zA-Z_]" re4_)
  8933. re1 (concat pre re2 post)
  8934. re3 (concat pre (if pre re4_ re4) post)
  8935. re5 (concat pre ".*" re4)
  8936. re2 (concat pre re2)
  8937. re2a (concat pre (if pre re2a_ re2a))
  8938. re4 (concat pre (if pre re4_ re4))
  8939. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8940. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8941. re5 "\\)"
  8942. ))
  8943. (cond
  8944. ((eq type 'org-occur) (org-occur reall))
  8945. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8946. (t (goto-char (point-min))
  8947. (setq type 'fuzzy)
  8948. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8949. (org-search-not-self 1 re1 nil t)
  8950. (org-search-not-self 1 re2 nil t)
  8951. (org-search-not-self 1 re2a nil t)
  8952. (org-search-not-self 1 re3 nil t)
  8953. (org-search-not-self 1 re4 nil t)
  8954. (org-search-not-self 1 re5 nil t)
  8955. )
  8956. (goto-char (match-beginning 1))
  8957. (goto-char pos)
  8958. (error "No match"))))))
  8959. (and (eq major-mode 'org-mode)
  8960. (not stealth)
  8961. (org-show-context 'link-search))
  8962. type))
  8963. (defun org-search-not-self (group &rest args)
  8964. "Execute `re-search-forward', but only accept matches that do not
  8965. enclose the position of `org-open-link-marker'."
  8966. (let ((m org-open-link-marker))
  8967. (catch 'exit
  8968. (while (apply 're-search-forward args)
  8969. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8970. (goto-char (match-end group))
  8971. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8972. (> (match-beginning 0) (marker-position m))
  8973. (< (match-end 0) (marker-position m)))
  8974. (save-match-data
  8975. (or (not (org-in-regexp
  8976. org-bracket-link-analytic-regexp 1))
  8977. (not (match-end 4)) ; no description
  8978. (and (<= (match-beginning 4) (point))
  8979. (>= (match-end 4) (point))))))
  8980. (throw 'exit (point))))))))
  8981. (defun org-get-buffer-for-internal-link (buffer)
  8982. "Return a buffer to be used for displaying the link target of internal links."
  8983. (cond
  8984. ((not org-display-internal-link-with-indirect-buffer)
  8985. buffer)
  8986. ((string-match "(Clone)$" (buffer-name buffer))
  8987. (message "Buffer is already a clone, not making another one")
  8988. ;; we also do not modify visibility in this case
  8989. buffer)
  8990. (t ; make a new indirect buffer for displaying the link
  8991. (let* ((bn (buffer-name buffer))
  8992. (ibn (concat bn "(Clone)"))
  8993. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8994. (with-current-buffer ib (org-overview))
  8995. ib))))
  8996. (defun org-do-occur (regexp &optional cleanup)
  8997. "Call the Emacs command `occur'.
  8998. If CLEANUP is non-nil, remove the printout of the regular expression
  8999. in the *Occur* buffer. This is useful if the regex is long and not useful
  9000. to read."
  9001. (occur regexp)
  9002. (when cleanup
  9003. (let ((cwin (selected-window)) win beg end)
  9004. (when (setq win (get-buffer-window "*Occur*"))
  9005. (select-window win))
  9006. (goto-char (point-min))
  9007. (when (re-search-forward "match[a-z]+" nil t)
  9008. (setq beg (match-end 0))
  9009. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9010. (setq end (1- (match-beginning 0)))))
  9011. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9012. (goto-char (point-min))
  9013. (select-window cwin))))
  9014. ;;; The mark ring for links jumps
  9015. (defvar org-mark-ring nil
  9016. "Mark ring for positions before jumps in Org-mode.")
  9017. (defvar org-mark-ring-last-goto nil
  9018. "Last position in the mark ring used to go back.")
  9019. ;; Fill and close the ring
  9020. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9021. (loop for i from 1 to org-mark-ring-length do
  9022. (push (make-marker) org-mark-ring))
  9023. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9024. org-mark-ring)
  9025. (defun org-mark-ring-push (&optional pos buffer)
  9026. "Put the current position or POS into the mark ring and rotate it."
  9027. (interactive)
  9028. (setq pos (or pos (point)))
  9029. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9030. (move-marker (car org-mark-ring)
  9031. (or pos (point))
  9032. (or buffer (current-buffer)))
  9033. (message "%s"
  9034. (substitute-command-keys
  9035. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9036. (defun org-mark-ring-goto (&optional n)
  9037. "Jump to the previous position in the mark ring.
  9038. With prefix arg N, jump back that many stored positions. When
  9039. called several times in succession, walk through the entire ring.
  9040. Org-mode commands jumping to a different position in the current file,
  9041. or to another Org-mode file, automatically push the old position
  9042. onto the ring."
  9043. (interactive "p")
  9044. (let (p m)
  9045. (if (eq last-command this-command)
  9046. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9047. (setq p org-mark-ring))
  9048. (setq org-mark-ring-last-goto p)
  9049. (setq m (car p))
  9050. (org-pop-to-buffer-same-window (marker-buffer m))
  9051. (goto-char m)
  9052. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9053. (defun org-remove-angle-brackets (s)
  9054. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9055. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9056. s)
  9057. (defun org-add-angle-brackets (s)
  9058. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9059. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9060. s)
  9061. (defun org-remove-double-quotes (s)
  9062. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9063. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9064. s)
  9065. ;;; Following specific links
  9066. (defun org-follow-timestamp-link ()
  9067. (cond
  9068. ((org-at-date-range-p t)
  9069. (let ((org-agenda-start-on-weekday)
  9070. (t1 (match-string 1))
  9071. (t2 (match-string 2)))
  9072. (setq t1 (time-to-days (org-time-string-to-time t1))
  9073. t2 (time-to-days (org-time-string-to-time t2)))
  9074. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9075. ((org-at-timestamp-p t)
  9076. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9077. (substring (match-string 1) 0 10)))
  9078. 1))
  9079. (t (error "This should not happen"))))
  9080. ;;; Following file links
  9081. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9082. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9083. (declare-function mailcap-mime-info
  9084. "mailcap" (string &optional request no-decode))
  9085. (defvar org-wait nil)
  9086. (defun org-open-file (path &optional in-emacs line search)
  9087. "Open the file at PATH.
  9088. First, this expands any special file name abbreviations. Then the
  9089. configuration variable `org-file-apps' is checked if it contains an
  9090. entry for this file type, and if yes, the corresponding command is launched.
  9091. If no application is found, Emacs simply visits the file.
  9092. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9093. With a double \\[universal-argument] \\[universal-argument] \
  9094. prefix arg, Org tries to avoid opening in Emacs
  9095. and to use an external application to visit the file.
  9096. Optional LINE specifies a line to go to, optional SEARCH a string
  9097. to search for. If LINE or SEARCH is given, the file will be
  9098. opened in Emacs, unless an entry from org-file-apps that makes
  9099. use of groups in a regexp matches.
  9100. If you want to change the way frames are used when following a
  9101. link, please customize `org-link-frame-setup'.
  9102. If the file does not exist, an error is thrown."
  9103. (let* ((file (if (equal path "")
  9104. buffer-file-name
  9105. (substitute-in-file-name (expand-file-name path))))
  9106. (file-apps (append org-file-apps (org-default-apps)))
  9107. (apps (org-remove-if
  9108. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9109. (apps-dlink (org-remove-if-not
  9110. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9111. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9112. (dirp (if remp nil (file-directory-p file)))
  9113. (file (if (and dirp org-open-directory-means-index-dot-org)
  9114. (concat (file-name-as-directory file) "index.org")
  9115. file))
  9116. (a-m-a-p (assq 'auto-mode apps))
  9117. (dfile (downcase file))
  9118. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9119. (link (cond ((and (eq line nil)
  9120. (eq search nil))
  9121. file)
  9122. (line
  9123. (concat file "::" (number-to-string line)))
  9124. (search
  9125. (concat file "::" search))))
  9126. (dlink (downcase link))
  9127. (old-buffer (current-buffer))
  9128. (old-pos (point))
  9129. (old-mode major-mode)
  9130. ext cmd link-match-data)
  9131. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9132. (setq ext (match-string 1 dfile))
  9133. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9134. (setq ext (match-string 1 dfile))))
  9135. (cond
  9136. ((member in-emacs '((16) system))
  9137. (setq cmd (cdr (assoc 'system apps))))
  9138. (in-emacs (setq cmd 'emacs))
  9139. (t
  9140. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9141. (and dirp (cdr (assoc 'directory apps)))
  9142. ; first, try matching against apps-dlink
  9143. ; if we get a match here, store the match data for later
  9144. (let ((match (assoc-default dlink apps-dlink
  9145. 'string-match)))
  9146. (if match
  9147. (progn (setq link-match-data (match-data))
  9148. match)
  9149. (progn (setq in-emacs (or in-emacs line search))
  9150. nil))) ; if we have no match in apps-dlink,
  9151. ; always open the file in emacs if line or search
  9152. ; is given (for backwards compatibility)
  9153. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9154. 'string-match)
  9155. (cdr (assoc ext apps))
  9156. (cdr (assoc t apps))))))
  9157. (when (eq cmd 'system)
  9158. (setq cmd (cdr (assoc 'system apps))))
  9159. (when (eq cmd 'default)
  9160. (setq cmd (cdr (assoc t apps))))
  9161. (when (eq cmd 'mailcap)
  9162. (require 'mailcap)
  9163. (mailcap-parse-mailcaps)
  9164. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9165. (command (mailcap-mime-info mime-type)))
  9166. (if (stringp command)
  9167. (setq cmd command)
  9168. (setq cmd 'emacs))))
  9169. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9170. (not (file-exists-p file))
  9171. (not org-open-non-existing-files))
  9172. (error "No such file: %s" file))
  9173. (cond
  9174. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9175. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9176. (while (string-match "['\"]%s['\"]" cmd)
  9177. (setq cmd (replace-match "%s" t t cmd)))
  9178. (while (string-match "%s" cmd)
  9179. (setq cmd (replace-match
  9180. (save-match-data
  9181. (shell-quote-argument
  9182. (convert-standard-filename file)))
  9183. t t cmd)))
  9184. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9185. (save-match-data
  9186. (let ((match-index 1)
  9187. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9188. (set-match-data link-match-data)
  9189. (while (<= match-index number-of-groups)
  9190. (let ((regex (concat "%" (number-to-string match-index)))
  9191. (replace-with (match-string match-index dlink)))
  9192. (while (string-match regex cmd)
  9193. (setq cmd (replace-match replace-with t t cmd))))
  9194. (setq match-index (+ match-index 1)))))
  9195. (save-window-excursion
  9196. (start-process-shell-command cmd nil cmd)
  9197. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9198. ))
  9199. ((or (stringp cmd)
  9200. (eq cmd 'emacs))
  9201. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9202. (widen)
  9203. (if line (org-goto-line line)
  9204. (if search (org-link-search search))))
  9205. ((consp cmd)
  9206. (let ((file (convert-standard-filename file)))
  9207. (save-match-data
  9208. (set-match-data link-match-data)
  9209. (eval cmd))))
  9210. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9211. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9212. (or (not (equal old-buffer (current-buffer)))
  9213. (not (equal old-pos (point))))
  9214. (org-mark-ring-push old-pos old-buffer))))
  9215. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9216. "This function returns non-nil if `entry' uses a regular
  9217. expression which should be matched against the whole link by
  9218. org-open-file.
  9219. It assumes that is the case when the entry uses a regular
  9220. expression which has at least one grouping construct and the
  9221. action is either a lisp form or a command string containing
  9222. '%1', i.e. using at least one subexpression match as a
  9223. parameter."
  9224. (let ((selector (car entry))
  9225. (action (cdr entry)))
  9226. (if (stringp selector)
  9227. (and (> (regexp-opt-depth selector) 0)
  9228. (or (and (stringp action)
  9229. (string-match "%[0-9]" action))
  9230. (consp action)))
  9231. nil)))
  9232. (defun org-default-apps ()
  9233. "Return the default applications for this operating system."
  9234. (cond
  9235. ((eq system-type 'darwin)
  9236. org-file-apps-defaults-macosx)
  9237. ((eq system-type 'windows-nt)
  9238. org-file-apps-defaults-windowsnt)
  9239. (t org-file-apps-defaults-gnu)))
  9240. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9241. "Convert extensions to regular expressions in the cars of LIST.
  9242. Also, weed out any non-string entries, because the return value is used
  9243. only for regexp matching.
  9244. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9245. point to the symbol `emacs', indicating that the file should
  9246. be opened in Emacs."
  9247. (append
  9248. (delq nil
  9249. (mapcar (lambda (x)
  9250. (if (not (stringp (car x)))
  9251. nil
  9252. (if (string-match "\\W" (car x))
  9253. x
  9254. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9255. list))
  9256. (if add-auto-mode
  9257. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9258. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9259. (defun org-file-remote-p (file)
  9260. "Test whether FILE specifies a location on a remote system.
  9261. Return non-nil if the location is indeed remote.
  9262. For example, the filename \"/user@host:/foo\" specifies a location
  9263. on the system \"/user@host:\"."
  9264. (cond ((fboundp 'file-remote-p)
  9265. (file-remote-p file))
  9266. ((fboundp 'tramp-handle-file-remote-p)
  9267. (tramp-handle-file-remote-p file))
  9268. ((and (boundp 'ange-ftp-name-format)
  9269. (string-match (car ange-ftp-name-format) file))
  9270. t)
  9271. (t nil)))
  9272. ;;;; Refiling
  9273. (defun org-get-org-file ()
  9274. "Read a filename, with default directory `org-directory'."
  9275. (let ((default (or org-default-notes-file remember-data-file)))
  9276. (read-file-name (format "File name [%s]: " default)
  9277. (file-name-as-directory org-directory)
  9278. default)))
  9279. (defun org-notes-order-reversed-p ()
  9280. "Check if the current file should receive notes in reversed order."
  9281. (cond
  9282. ((not org-reverse-note-order) nil)
  9283. ((eq t org-reverse-note-order) t)
  9284. ((not (listp org-reverse-note-order)) nil)
  9285. (t (catch 'exit
  9286. (let ((all org-reverse-note-order)
  9287. entry)
  9288. (while (setq entry (pop all))
  9289. (if (string-match (car entry) buffer-file-name)
  9290. (throw 'exit (cdr entry))))
  9291. nil)))))
  9292. (defvar org-refile-target-table nil
  9293. "The list of refile targets, created by `org-refile'.")
  9294. (defvar org-agenda-new-buffers nil
  9295. "Buffers created to visit agenda files.")
  9296. (defvar org-refile-cache nil
  9297. "Cache for refile targets.")
  9298. (defvar org-refile-markers nil
  9299. "All the markers used for caching refile locations.")
  9300. (defun org-refile-marker (pos)
  9301. "Get a new refile marker, but only if caching is in use."
  9302. (if (not org-refile-use-cache)
  9303. pos
  9304. (let ((m (make-marker)))
  9305. (move-marker m pos)
  9306. (push m org-refile-markers)
  9307. m)))
  9308. (defun org-refile-cache-clear ()
  9309. "Clear the refile cache and disable all the markers."
  9310. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9311. (setq org-refile-markers nil)
  9312. (setq org-refile-cache nil)
  9313. (message "Refile cache has been cleared"))
  9314. (defun org-refile-cache-check-set (set)
  9315. "Check if all the markers in the cache still have live buffers."
  9316. (let (marker)
  9317. (catch 'exit
  9318. (while (and set (setq marker (nth 3 (pop set))))
  9319. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9320. (when (and marker (null (marker-buffer marker)))
  9321. (message "not found") (sit-for 3)
  9322. (throw 'exit nil)))
  9323. t)))
  9324. (defun org-refile-cache-put (set &rest identifiers)
  9325. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9326. (let* ((key (sha1 (prin1-to-string identifiers)))
  9327. (entry (assoc key org-refile-cache)))
  9328. (if entry
  9329. (setcdr entry set)
  9330. (push (cons key set) org-refile-cache))))
  9331. (defun org-refile-cache-get (&rest identifiers)
  9332. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9333. (cond
  9334. ((not org-refile-cache) nil)
  9335. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9336. (t
  9337. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9338. org-refile-cache))))
  9339. (and set (org-refile-cache-check-set set) set)))))
  9340. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9341. "Produce a table with refile targets."
  9342. (let ((case-fold-search nil)
  9343. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9344. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9345. targets tgs txt re files f desc descre fast-path-p level pos0)
  9346. (message "Getting targets...")
  9347. (with-current-buffer (or default-buffer (current-buffer))
  9348. (while (setq entry (pop entries))
  9349. (setq files (car entry) desc (cdr entry))
  9350. (setq fast-path-p nil)
  9351. (cond
  9352. ((null files) (setq files (list (current-buffer))))
  9353. ((eq files 'org-agenda-files)
  9354. (setq files (org-agenda-files 'unrestricted)))
  9355. ((and (symbolp files) (fboundp files))
  9356. (setq files (funcall files)))
  9357. ((and (symbolp files) (boundp files))
  9358. (setq files (symbol-value files))))
  9359. (if (stringp files) (setq files (list files)))
  9360. (cond
  9361. ((eq (car desc) :tag)
  9362. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9363. ((eq (car desc) :todo)
  9364. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9365. ((eq (car desc) :regexp)
  9366. (setq descre (cdr desc)))
  9367. ((eq (car desc) :level)
  9368. (setq descre (concat "^\\*\\{" (number-to-string
  9369. (if org-odd-levels-only
  9370. (1- (* 2 (cdr desc)))
  9371. (cdr desc)))
  9372. "\\}[ \t]")))
  9373. ((eq (car desc) :maxlevel)
  9374. (setq fast-path-p t)
  9375. (setq descre (concat "^\\*\\{1," (number-to-string
  9376. (if org-odd-levels-only
  9377. (1- (* 2 (cdr desc)))
  9378. (cdr desc)))
  9379. "\\}[ \t]")))
  9380. (t (error "Bad refiling target description %s" desc)))
  9381. (while (setq f (pop files))
  9382. (with-current-buffer
  9383. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9384. (or
  9385. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9386. (progn
  9387. (if (bufferp f) (setq f (buffer-file-name
  9388. (buffer-base-buffer f))))
  9389. (setq f (and f (expand-file-name f)))
  9390. (if (eq org-refile-use-outline-path 'file)
  9391. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9392. (save-excursion
  9393. (save-restriction
  9394. (widen)
  9395. (goto-char (point-min))
  9396. (while (re-search-forward descre nil t)
  9397. (goto-char (setq pos0 (point-at-bol)))
  9398. (catch 'next
  9399. (when org-refile-target-verify-function
  9400. (save-match-data
  9401. (or (funcall org-refile-target-verify-function)
  9402. (throw 'next t))))
  9403. (when (and (looking-at org-complex-heading-regexp)
  9404. (not (member (match-string 4) excluded-entries))
  9405. (match-string 4))
  9406. (setq level (org-reduced-level
  9407. (- (match-end 1) (match-beginning 1)))
  9408. txt (org-link-display-format (match-string 4))
  9409. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9410. re (format org-complex-heading-regexp-format
  9411. (regexp-quote (match-string 4))))
  9412. (when org-refile-use-outline-path
  9413. (setq txt (mapconcat
  9414. 'org-protect-slash
  9415. (append
  9416. (if (eq org-refile-use-outline-path
  9417. 'file)
  9418. (list (file-name-nondirectory
  9419. (buffer-file-name
  9420. (buffer-base-buffer))))
  9421. (if (eq org-refile-use-outline-path
  9422. 'full-file-path)
  9423. (list (buffer-file-name
  9424. (buffer-base-buffer)))))
  9425. (org-get-outline-path fast-path-p
  9426. level txt)
  9427. (list txt))
  9428. "/")))
  9429. (push (list txt f re (org-refile-marker (point)))
  9430. tgs)))
  9431. (when (= (point) pos0)
  9432. ;; verification function has not moved point
  9433. (goto-char (point-at-eol))))))))
  9434. (when org-refile-use-cache
  9435. (org-refile-cache-put tgs (buffer-file-name) descre))
  9436. (setq targets (append tgs targets))
  9437. ))))
  9438. (message "Getting targets...done")
  9439. (nreverse targets)))
  9440. (defun org-protect-slash (s)
  9441. (while (string-match "/" s)
  9442. (setq s (replace-match "\\" t t s)))
  9443. s)
  9444. (defvar org-olpa (make-vector 20 nil))
  9445. (defun org-get-outline-path (&optional fastp level heading)
  9446. "Return the outline path to the current entry, as a list.
  9447. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9448. routine which makes outline path derivations for an entire file,
  9449. avoiding backtracing. Refile target collection makes use of that."
  9450. (if fastp
  9451. (progn
  9452. (if (> level 19)
  9453. (error "Outline path failure, more than 19 levels"))
  9454. (loop for i from level upto 19 do
  9455. (aset org-olpa i nil))
  9456. (prog1
  9457. (delq nil (append org-olpa nil))
  9458. (aset org-olpa level heading)))
  9459. (let (rtn case-fold-search)
  9460. (save-excursion
  9461. (save-restriction
  9462. (widen)
  9463. (while (org-up-heading-safe)
  9464. (when (looking-at org-complex-heading-regexp)
  9465. (push (org-match-string-no-properties 4) rtn)))
  9466. rtn)))))
  9467. (defun org-format-outline-path (path &optional width prefix)
  9468. "Format the outline path PATH for display.
  9469. Width is the maximum number of characters that is available.
  9470. Prefix is a prefix to be included in the returned string,
  9471. such as the file name."
  9472. (setq width (or width 79))
  9473. (if prefix (setq width (- width (length prefix))))
  9474. (if (not path)
  9475. (or prefix "")
  9476. (let* ((nsteps (length path))
  9477. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9478. (maxwidth (if (<= total-width width)
  9479. 10000 ;; everything fits
  9480. ;; we need to shorten the level headings
  9481. (/ (- width nsteps) nsteps)))
  9482. (org-odd-levels-only nil)
  9483. (n 0)
  9484. (total (1+ (length prefix))))
  9485. (setq maxwidth (max maxwidth 10))
  9486. (concat prefix
  9487. (mapconcat
  9488. (lambda (h)
  9489. (setq n (1+ n))
  9490. (if (and (= n nsteps) (< maxwidth 10000))
  9491. (setq maxwidth (- total-width total)))
  9492. (if (< (length h) maxwidth)
  9493. (progn (setq total (+ total (length h) 1)) h)
  9494. (setq h (substring h 0 (- maxwidth 2))
  9495. total (+ total maxwidth 1))
  9496. (if (string-match "[ \t]+\\'" h)
  9497. (setq h (substring h 0 (match-beginning 0))))
  9498. (setq h (concat h "..")))
  9499. (org-add-props h nil 'face
  9500. (nth (% (1- n) org-n-level-faces)
  9501. org-level-faces))
  9502. h)
  9503. path "/")))))
  9504. (defun org-display-outline-path (&optional file current)
  9505. "Display the current outline path in the echo area."
  9506. (interactive "P")
  9507. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9508. (case-fold-search nil)
  9509. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9510. (if current (setq path (append path
  9511. (save-excursion
  9512. (org-back-to-heading t)
  9513. (if (looking-at org-complex-heading-regexp)
  9514. (list (match-string 4)))))))
  9515. (message "%s"
  9516. (org-format-outline-path
  9517. path
  9518. (1- (frame-width))
  9519. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9520. (defvar org-refile-history nil
  9521. "History for refiling operations.")
  9522. (defvar org-after-refile-insert-hook nil
  9523. "Hook run after `org-refile' has inserted its stuff at the new location.
  9524. Note that this is still *before* the stuff will be removed from
  9525. the *old* location.")
  9526. (defvar org-capture-last-stored-marker)
  9527. (defun org-refile (&optional goto default-buffer rfloc)
  9528. "Move the entry or entries at point to another heading.
  9529. The list of target headings is compiled using the information in
  9530. `org-refile-targets', which see.
  9531. At the target location, the entry is filed as a subitem of the target
  9532. heading. Depending on `org-reverse-note-order', the new subitem will
  9533. either be the first or the last subitem.
  9534. If there is an active region, all entries in that region will be moved.
  9535. However, the region must fulfill the requirement that the first heading
  9536. is the first one sets the top-level of the moved text - at most siblings
  9537. below it are allowed.
  9538. With prefix arg GOTO, the command will only visit the target location
  9539. and not actually move anything.
  9540. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9541. go to the location where the last refiling operation has put the subtree.
  9542. With a prefix argument of `2', refile to the running clock.
  9543. RFLOC can be a refile location obtained in a different way.
  9544. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9545. If you are using target caching (see `org-refile-use-cache'),
  9546. You have to clear the target cache in order to find new targets.
  9547. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9548. prefix argument (`C-u C-u C-u C-c C-w')."
  9549. (interactive "P")
  9550. (if (member goto '(0 (64)))
  9551. (org-refile-cache-clear)
  9552. (let* ((cbuf (current-buffer))
  9553. (regionp (org-region-active-p))
  9554. (region-start (and regionp (region-beginning)))
  9555. (region-end (and regionp (region-end)))
  9556. (region-length (and regionp (- region-end region-start)))
  9557. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9558. pos it nbuf file re level reversed)
  9559. (setq last-command nil)
  9560. (when regionp
  9561. (goto-char region-start)
  9562. (or (bolp) (goto-char (point-at-bol)))
  9563. (setq region-start (point))
  9564. (unless (or (org-kill-is-subtree-p
  9565. (buffer-substring region-start region-end))
  9566. (prog1 org-refile-active-region-within-subtree
  9567. (org-toggle-heading)))
  9568. (error "The region is not a (sequence of) subtree(s)")))
  9569. (if (equal goto '(16))
  9570. (org-refile-goto-last-stored)
  9571. (when (or
  9572. (and (equal goto 2)
  9573. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9574. (prog1
  9575. (setq it (list (or org-clock-heading "running clock")
  9576. (buffer-file-name
  9577. (marker-buffer org-clock-hd-marker))
  9578. ""
  9579. (marker-position org-clock-hd-marker)))
  9580. (setq goto nil)))
  9581. (setq it (or rfloc
  9582. (let (heading-text)
  9583. (save-excursion
  9584. (unless goto
  9585. (org-back-to-heading t)
  9586. (setq heading-text
  9587. (nth 4 (org-heading-components))))
  9588. (org-refile-get-location
  9589. (cond (goto "Goto")
  9590. (regionp "Refile region to")
  9591. (t (concat "Refile subtree \""
  9592. heading-text "\" to")))
  9593. default-buffer
  9594. (and (not (equal '(4) goto))
  9595. org-refile-allow-creating-parent-nodes)
  9596. goto))))))
  9597. (setq file (nth 1 it)
  9598. re (nth 2 it)
  9599. pos (nth 3 it))
  9600. (if (and (not goto)
  9601. pos
  9602. (equal (buffer-file-name) file)
  9603. (if regionp
  9604. (and (>= pos region-start)
  9605. (<= pos region-end))
  9606. (and (>= pos (point))
  9607. (< pos (save-excursion
  9608. (org-end-of-subtree t t))))))
  9609. (error "Cannot refile to position inside the tree or region"))
  9610. (setq nbuf (or (find-buffer-visiting file)
  9611. (find-file-noselect file)))
  9612. (if goto
  9613. (progn
  9614. (org-pop-to-buffer-same-window nbuf)
  9615. (goto-char pos)
  9616. (org-show-context 'org-goto))
  9617. (if regionp
  9618. (progn
  9619. (org-kill-new (buffer-substring region-start region-end))
  9620. (org-save-markers-in-region region-start region-end))
  9621. (org-copy-subtree 1 nil t))
  9622. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9623. (find-file-noselect file)))
  9624. (setq reversed (org-notes-order-reversed-p))
  9625. (save-excursion
  9626. (save-restriction
  9627. (widen)
  9628. (if pos
  9629. (progn
  9630. (goto-char pos)
  9631. (looking-at org-outline-regexp)
  9632. (setq level (org-get-valid-level (funcall outline-level) 1))
  9633. (goto-char
  9634. (if reversed
  9635. (or (outline-next-heading) (point-max))
  9636. (or (save-excursion (org-get-next-sibling))
  9637. (org-end-of-subtree t t)
  9638. (point-max)))))
  9639. (setq level 1)
  9640. (if (not reversed)
  9641. (goto-char (point-max))
  9642. (goto-char (point-min))
  9643. (or (outline-next-heading) (goto-char (point-max)))))
  9644. (if (not (bolp)) (newline))
  9645. (org-paste-subtree level)
  9646. (when org-log-refile
  9647. (org-add-log-setup 'refile nil nil 'findpos
  9648. org-log-refile)
  9649. (unless (eq org-log-refile 'note)
  9650. (save-excursion (org-add-log-note))))
  9651. (and org-auto-align-tags (org-set-tags nil t))
  9652. (bookmark-set "org-refile-last-stored")
  9653. ;; If we are refiling for capture, make sure that the
  9654. ;; last-capture pointers point here
  9655. (when (org-bound-and-true-p org-refile-for-capture)
  9656. (bookmark-set "org-capture-last-stored-marker")
  9657. (move-marker org-capture-last-stored-marker (point)))
  9658. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9659. (run-hooks 'org-after-refile-insert-hook))))
  9660. (if regionp
  9661. (delete-region (point) (+ (point) region-length))
  9662. (org-cut-subtree))
  9663. (when (featurep 'org-inlinetask)
  9664. (org-inlinetask-remove-END-maybe))
  9665. (setq org-markers-to-move nil)
  9666. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9667. (defun org-refile-goto-last-stored ()
  9668. "Go to the location where the last refile was stored."
  9669. (interactive)
  9670. (bookmark-jump "org-refile-last-stored")
  9671. (message "This is the location of the last refile"))
  9672. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9673. no-exclude)
  9674. "Prompt the user for a refile location, using PROMPT.
  9675. PROMPT should not be suffixed with a colon and a space, because
  9676. this function appends the default value from
  9677. `org-refile-history' automatically, if that is not empty.
  9678. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9679. this is used for the GOTO interface."
  9680. (let ((org-refile-targets org-refile-targets)
  9681. (org-refile-use-outline-path org-refile-use-outline-path)
  9682. excluded-entries)
  9683. (when (and (eq major-mode 'org-mode)
  9684. (not org-refile-use-cache)
  9685. (not no-exclude))
  9686. (org-map-tree
  9687. (lambda()
  9688. (setq excluded-entries
  9689. (append excluded-entries (list (org-get-heading t t)))))))
  9690. (setq org-refile-target-table
  9691. (org-refile-get-targets default-buffer excluded-entries)))
  9692. (unless org-refile-target-table
  9693. (error "No refile targets"))
  9694. (let* ((prompt (concat prompt
  9695. (and (car org-refile-history)
  9696. (concat " (default " (car org-refile-history) ")"))
  9697. ": "))
  9698. (cbuf (current-buffer))
  9699. (partial-completion-mode nil)
  9700. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9701. (cfunc (if (and org-refile-use-outline-path
  9702. org-outline-path-complete-in-steps)
  9703. 'org-olpath-completing-read
  9704. 'org-icompleting-read))
  9705. (extra (if org-refile-use-outline-path "/" ""))
  9706. (filename (and cfn (expand-file-name cfn)))
  9707. (tbl (mapcar
  9708. (lambda (x)
  9709. (if (and (not (member org-refile-use-outline-path
  9710. '(file full-file-path)))
  9711. (not (equal filename (nth 1 x))))
  9712. (cons (concat (car x) extra " ("
  9713. (file-name-nondirectory (nth 1 x)) ")")
  9714. (cdr x))
  9715. (cons (concat (car x) extra) (cdr x))))
  9716. org-refile-target-table))
  9717. (completion-ignore-case t)
  9718. pa answ parent-target child parent old-hist)
  9719. (setq old-hist org-refile-history)
  9720. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9721. nil 'org-refile-history (car org-refile-history)))
  9722. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9723. (org-refile-check-position pa)
  9724. (if pa
  9725. (progn
  9726. (when (or (not org-refile-history)
  9727. (not (eq old-hist org-refile-history))
  9728. (not (equal (car pa) (car org-refile-history))))
  9729. (setq org-refile-history
  9730. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9731. org-refile-history
  9732. (cdr org-refile-history))))
  9733. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9734. (pop org-refile-history)))
  9735. pa)
  9736. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9737. (progn
  9738. (setq parent (match-string 1 answ)
  9739. child (match-string 2 answ))
  9740. (setq parent-target (or (assoc parent tbl)
  9741. (assoc (concat parent "/") tbl)))
  9742. (when (and parent-target
  9743. (or (eq new-nodes t)
  9744. (and (eq new-nodes 'confirm)
  9745. (y-or-n-p (format "Create new node \"%s\"? "
  9746. child)))))
  9747. (org-refile-new-child parent-target child)))
  9748. (error "Invalid target location")))))
  9749. (defun org-refile-check-position (refile-pointer)
  9750. "Check if the refile pointer matches the readline to which it points."
  9751. (let* ((file (nth 1 refile-pointer))
  9752. (re (nth 2 refile-pointer))
  9753. (pos (nth 3 refile-pointer))
  9754. buffer)
  9755. (when (org-string-nw-p re)
  9756. (setq buffer (if (markerp pos)
  9757. (marker-buffer pos)
  9758. (or (find-buffer-visiting file)
  9759. (find-file-noselect file))))
  9760. (with-current-buffer buffer
  9761. (save-excursion
  9762. (save-restriction
  9763. (widen)
  9764. (goto-char pos)
  9765. (beginning-of-line 1)
  9766. (unless (org-looking-at-p re)
  9767. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9768. (defun org-refile-new-child (parent-target child)
  9769. "Use refile target PARENT-TARGET to add new CHILD below it."
  9770. (unless parent-target
  9771. (error "Cannot find parent for new node"))
  9772. (let ((file (nth 1 parent-target))
  9773. (pos (nth 3 parent-target))
  9774. level)
  9775. (with-current-buffer (or (find-buffer-visiting file)
  9776. (find-file-noselect file))
  9777. (save-excursion
  9778. (save-restriction
  9779. (widen)
  9780. (if pos
  9781. (goto-char pos)
  9782. (goto-char (point-max))
  9783. (if (not (bolp)) (newline)))
  9784. (when (looking-at org-outline-regexp)
  9785. (setq level (funcall outline-level))
  9786. (org-end-of-subtree t t))
  9787. (org-back-over-empty-lines)
  9788. (insert "\n" (make-string
  9789. (if pos (org-get-valid-level level 1) 1) ?*)
  9790. " " child "\n")
  9791. (beginning-of-line 0)
  9792. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9793. (defun org-olpath-completing-read (prompt collection &rest args)
  9794. "Read an outline path like a file name."
  9795. (let ((thetable collection)
  9796. (org-completion-use-ido nil) ; does not work with ido.
  9797. (org-completion-use-iswitchb nil)) ; or iswitchb
  9798. (apply
  9799. 'org-icompleting-read prompt
  9800. (lambda (string predicate &optional flag)
  9801. (let (rtn r f (l (length string)))
  9802. (cond
  9803. ((eq flag nil)
  9804. ;; try completion
  9805. (try-completion string thetable))
  9806. ((eq flag t)
  9807. ;; all-completions
  9808. (setq rtn (all-completions string thetable predicate))
  9809. (mapcar
  9810. (lambda (x)
  9811. (setq r (substring x l))
  9812. (if (string-match " ([^)]*)$" x)
  9813. (setq f (match-string 0 x))
  9814. (setq f ""))
  9815. (if (string-match "/" r)
  9816. (concat string (substring r 0 (match-end 0)) f)
  9817. x))
  9818. rtn))
  9819. ((eq flag 'lambda)
  9820. ;; exact match?
  9821. (assoc string thetable)))
  9822. ))
  9823. args)))
  9824. ;;;; Dynamic blocks
  9825. (defun org-find-dblock (name)
  9826. "Find the first dynamic block with name NAME in the buffer.
  9827. If not found, stay at current position and return nil."
  9828. (let (pos)
  9829. (save-excursion
  9830. (goto-char (point-min))
  9831. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9832. nil t)
  9833. (match-beginning 0))))
  9834. (if pos (goto-char pos))
  9835. pos))
  9836. (defconst org-dblock-start-re
  9837. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9838. "Matches the start line of a dynamic block, with parameters.")
  9839. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9840. "Matches the end of a dynamic block.")
  9841. (defun org-create-dblock (plist)
  9842. "Create a dynamic block section, with parameters taken from PLIST.
  9843. PLIST must contain a :name entry which is used as name of the block."
  9844. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9845. (end-of-line 1)
  9846. (newline))
  9847. (let ((col (current-column))
  9848. (name (plist-get plist :name)))
  9849. (insert "#+BEGIN: " name)
  9850. (while plist
  9851. (if (eq (car plist) :name)
  9852. (setq plist (cddr plist))
  9853. (insert " " (prin1-to-string (pop plist)))))
  9854. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9855. (beginning-of-line -2)))
  9856. (defun org-prepare-dblock ()
  9857. "Prepare dynamic block for refresh.
  9858. This empties the block, puts the cursor at the insert position and returns
  9859. the property list including an extra property :name with the block name."
  9860. (unless (looking-at org-dblock-start-re)
  9861. (error "Not at a dynamic block"))
  9862. (let* ((begdel (1+ (match-end 0)))
  9863. (name (org-no-properties (match-string 1)))
  9864. (params (append (list :name name)
  9865. (read (concat "(" (match-string 3) ")")))))
  9866. (save-excursion
  9867. (beginning-of-line 1)
  9868. (skip-chars-forward " \t")
  9869. (setq params (plist-put params :indentation-column (current-column))))
  9870. (unless (re-search-forward org-dblock-end-re nil t)
  9871. (error "Dynamic block not terminated"))
  9872. (setq params
  9873. (append params
  9874. (list :content (buffer-substring
  9875. begdel (match-beginning 0)))))
  9876. (delete-region begdel (match-beginning 0))
  9877. (goto-char begdel)
  9878. (open-line 1)
  9879. params))
  9880. (defun org-map-dblocks (&optional command)
  9881. "Apply COMMAND to all dynamic blocks in the current buffer.
  9882. If COMMAND is not given, use `org-update-dblock'."
  9883. (let ((cmd (or command 'org-update-dblock)))
  9884. (save-excursion
  9885. (goto-char (point-min))
  9886. (while (re-search-forward org-dblock-start-re nil t)
  9887. (goto-char (match-beginning 0))
  9888. (save-excursion
  9889. (condition-case nil
  9890. (funcall cmd)
  9891. (error (message "Error during update of dynamic block"))))
  9892. (unless (re-search-forward org-dblock-end-re nil t)
  9893. (error "Dynamic block not terminated"))))))
  9894. (defun org-dblock-update (&optional arg)
  9895. "User command for updating dynamic blocks.
  9896. Update the dynamic block at point. With prefix ARG, update all dynamic
  9897. blocks in the buffer."
  9898. (interactive "P")
  9899. (if arg
  9900. (org-update-all-dblocks)
  9901. (or (looking-at org-dblock-start-re)
  9902. (org-beginning-of-dblock))
  9903. (org-update-dblock)))
  9904. (defun org-update-dblock ()
  9905. "Update the dynamic block at point.
  9906. This means to empty the block, parse for parameters and then call
  9907. the correct writing function."
  9908. (interactive)
  9909. (save-window-excursion
  9910. (let* ((pos (point))
  9911. (line (org-current-line))
  9912. (params (org-prepare-dblock))
  9913. (name (plist-get params :name))
  9914. (indent (plist-get params :indentation-column))
  9915. (cmd (intern (concat "org-dblock-write:" name))))
  9916. (message "Updating dynamic block `%s' at line %d..." name line)
  9917. (funcall cmd params)
  9918. (message "Updating dynamic block `%s' at line %d...done" name line)
  9919. (goto-char pos)
  9920. (when (and indent (> indent 0))
  9921. (setq indent (make-string indent ?\ ))
  9922. (save-excursion
  9923. (org-beginning-of-dblock)
  9924. (forward-line 1)
  9925. (while (not (looking-at org-dblock-end-re))
  9926. (insert indent)
  9927. (beginning-of-line 2))
  9928. (when (looking-at org-dblock-end-re)
  9929. (and (looking-at "[ \t]+")
  9930. (replace-match ""))
  9931. (insert indent)))))))
  9932. (defun org-beginning-of-dblock ()
  9933. "Find the beginning of the dynamic block at point.
  9934. Error if there is no such block at point."
  9935. (let ((pos (point))
  9936. beg)
  9937. (end-of-line 1)
  9938. (if (and (re-search-backward org-dblock-start-re nil t)
  9939. (setq beg (match-beginning 0))
  9940. (re-search-forward org-dblock-end-re nil t)
  9941. (> (match-end 0) pos))
  9942. (goto-char beg)
  9943. (goto-char pos)
  9944. (error "Not in a dynamic block"))))
  9945. (defun org-update-all-dblocks ()
  9946. "Update all dynamic blocks in the buffer.
  9947. This function can be used in a hook."
  9948. (interactive)
  9949. (when (eq major-mode 'org-mode)
  9950. (org-map-dblocks 'org-update-dblock)))
  9951. ;;;; Completion
  9952. (defconst org-additional-option-like-keywords
  9953. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9954. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9955. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9956. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9957. "BEGIN:" "END:"
  9958. "ORGTBL" "TBLFM:" "TBLNAME:"
  9959. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9960. "BEGIN_QUOTE" "END_QUOTE"
  9961. "BEGIN_VERSE" "END_VERSE"
  9962. "BEGIN_CENTER" "END_CENTER"
  9963. "BEGIN_SRC" "END_SRC"
  9964. "BEGIN_RESULT" "END_RESULT"
  9965. "NAME:" "RESULTS:"
  9966. "HEADER:" "HEADERS:"
  9967. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9968. "CAPTION:" "LABEL:"
  9969. "SETUPFILE:"
  9970. "INCLUDE:"
  9971. "BIND:"
  9972. "MACRO:"))
  9973. (defcustom org-structure-template-alist
  9974. '(
  9975. ("s" "#+begin_src ?\n\n#+end_src"
  9976. "<src lang=\"?\">\n\n</src>")
  9977. ("e" "#+begin_example\n?\n#+end_example"
  9978. "<example>\n?\n</example>")
  9979. ("q" "#+begin_quote\n?\n#+end_quote"
  9980. "<quote>\n?\n</quote>")
  9981. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  9982. "<verse>\n?\n</verse>")
  9983. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  9984. "<center>\n?\n</center>")
  9985. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  9986. "<literal style=\"latex\">\n?\n</literal>")
  9987. ("L" "#+latex: "
  9988. "<literal style=\"latex\">?</literal>")
  9989. ("h" "#+begin_html\n?\n#+end_html"
  9990. "<literal style=\"html\">\n?\n</literal>")
  9991. ("H" "#+html: "
  9992. "<literal style=\"html\">?</literal>")
  9993. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9994. ("A" "#+ascii: ")
  9995. ("i" "#+index: ?"
  9996. "#+index: ?")
  9997. ("I" "#+include %file ?"
  9998. "<include file=%file markup=\"?\">")
  9999. )
  10000. "Structure completion elements.
  10001. This is a list of abbreviation keys and values. The value gets inserted
  10002. if you type `<' followed by the key and then press the completion key,
  10003. usually `M-TAB'. %file will be replaced by a file name after prompting
  10004. for the file using completion. The cursor will be placed at the position
  10005. of the `?` in the template.
  10006. There are two templates for each key, the first uses the original Org syntax,
  10007. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10008. the default when the /org-mtags.el/ module has been loaded. See also the
  10009. variable `org-mtags-prefer-muse-templates'.
  10010. This is an experimental feature, it is undecided if it is going to stay in."
  10011. :group 'org-completion
  10012. :type '(repeat
  10013. (string :tag "Key")
  10014. (string :tag "Template")
  10015. (string :tag "Muse Template")))
  10016. (defun org-try-structure-completion ()
  10017. "Try to complete a structure template before point.
  10018. This looks for strings like \"<e\" on an otherwise empty line and
  10019. expands them."
  10020. (let ((l (buffer-substring (point-at-bol) (point)))
  10021. a)
  10022. (when (and (looking-at "[ \t]*$")
  10023. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10024. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10025. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10026. (match-beginning 1)) a)
  10027. t)))
  10028. (defun org-complete-expand-structure-template (start cell)
  10029. "Expand a structure template."
  10030. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10031. (rpl (nth (if musep 2 1) cell))
  10032. (ind ""))
  10033. (delete-region start (point))
  10034. (when (string-match "\\`#\\+" rpl)
  10035. (cond
  10036. ((bolp))
  10037. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10038. (setq ind (buffer-substring (point-at-bol) (point))))
  10039. (t (newline))))
  10040. (setq start (point))
  10041. (if (string-match "%file" rpl)
  10042. (setq rpl (replace-match
  10043. (concat
  10044. "\""
  10045. (save-match-data
  10046. (abbreviate-file-name (read-file-name "Include file: ")))
  10047. "\"")
  10048. t t rpl)))
  10049. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10050. (concat "\n" ind)))
  10051. (insert rpl)
  10052. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10053. ;;;; TODO, DEADLINE, Comments
  10054. (defun org-toggle-comment ()
  10055. "Change the COMMENT state of an entry."
  10056. (interactive)
  10057. (save-excursion
  10058. (org-back-to-heading)
  10059. (let (case-fold-search)
  10060. (cond
  10061. ((looking-at (format org-heading-keyword-regexp-format
  10062. org-comment-string))
  10063. (goto-char (match-end 1))
  10064. (looking-at (concat " +" org-comment-string))
  10065. (replace-match "" t t)
  10066. (when (eolp) (insert " ")))
  10067. ((looking-at org-outline-regexp)
  10068. (goto-char (match-end 0))
  10069. (insert org-comment-string " "))))))
  10070. (defvar org-last-todo-state-is-todo nil
  10071. "This is non-nil when the last TODO state change led to a TODO state.
  10072. If the last change removed the TODO tag or switched to DONE, then
  10073. this is nil.")
  10074. (defvar org-setting-tags nil) ; dynamically skipped
  10075. (defvar org-todo-setup-filter-hook nil
  10076. "Hook for functions that pre-filter todo specs.
  10077. Each function takes a todo spec and returns either nil or the spec
  10078. transformed into canonical form." )
  10079. (defvar org-todo-get-default-hook nil
  10080. "Hook for functions that get a default item for todo.
  10081. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10082. nil or a string to be used for the todo mark." )
  10083. (defvar org-agenda-headline-snapshot-before-repeat)
  10084. (defun org-current-effective-time ()
  10085. "Return current time adjusted for `org-extend-today-until' variable"
  10086. (let* ((ct (org-current-time))
  10087. (dct (decode-time ct))
  10088. (ct1
  10089. (if (and org-use-effective-time
  10090. (< (nth 2 dct) org-extend-today-until))
  10091. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10092. ct)))
  10093. ct1))
  10094. (defun org-todo-yesterday (&optional arg)
  10095. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10096. (interactive "P")
  10097. (if (eq major-mode 'org-agenda-mode)
  10098. (apply 'org-agenda-todo-yesterday arg)
  10099. (let* ((hour (third (decode-time
  10100. (org-current-time))))
  10101. (org-extend-today-until (1+ hour)))
  10102. (org-todo arg))))
  10103. (defun org-todo (&optional arg)
  10104. "Change the TODO state of an item.
  10105. The state of an item is given by a keyword at the start of the heading,
  10106. like
  10107. *** TODO Write paper
  10108. *** DONE Call mom
  10109. The different keywords are specified in the variable `org-todo-keywords'.
  10110. By default the available states are \"TODO\" and \"DONE\".
  10111. So for this example: when the item starts with TODO, it is changed to DONE.
  10112. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10113. DONE are present, add TODO at the beginning of the heading.
  10114. With \\[universal-argument] prefix arg, use completion to determine the new \
  10115. state.
  10116. With numeric prefix arg, switch to that state.
  10117. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10118. keywords (nextset).
  10119. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10120. With a numeric prefix arg of 0, inhibit note taking for the change.
  10121. For calling through lisp, arg is also interpreted in the following way:
  10122. 'none -> empty state
  10123. \"\"(empty string) -> switch to empty state
  10124. 'done -> switch to DONE
  10125. 'nextset -> switch to the next set of keywords
  10126. 'previousset -> switch to the previous set of keywords
  10127. \"WAITING\" -> switch to the specified keyword, but only if it
  10128. really is a member of `org-todo-keywords'."
  10129. (interactive "P")
  10130. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10131. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10132. 'region-start-level 'region))
  10133. org-loop-over-headlines-in-active-region)
  10134. (org-map-entries
  10135. `(org-todo ,arg)
  10136. org-loop-over-headlines-in-active-region
  10137. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10138. (if (equal arg '(16)) (setq arg 'nextset))
  10139. (let ((org-blocker-hook org-blocker-hook)
  10140. (case-fold-search nil))
  10141. (when (equal arg '(64))
  10142. (setq arg nil org-blocker-hook nil))
  10143. (when (and org-blocker-hook
  10144. (or org-inhibit-blocking
  10145. (org-entry-get nil "NOBLOCKING")))
  10146. (setq org-blocker-hook nil))
  10147. (save-excursion
  10148. (catch 'exit
  10149. (org-back-to-heading t)
  10150. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10151. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10152. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10153. (let* ((match-data (match-data))
  10154. (startpos (point-at-bol))
  10155. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10156. (org-log-done org-log-done)
  10157. (org-log-repeat org-log-repeat)
  10158. (org-todo-log-states org-todo-log-states)
  10159. (org-inhibit-logging
  10160. (if (equal arg 0)
  10161. (progn (setq arg nil) 'note) org-inhibit-logging))
  10162. (this (match-string 1))
  10163. (hl-pos (match-beginning 0))
  10164. (head (org-get-todo-sequence-head this))
  10165. (ass (assoc head org-todo-kwd-alist))
  10166. (interpret (nth 1 ass))
  10167. (done-word (nth 3 ass))
  10168. (final-done-word (nth 4 ass))
  10169. (last-state (or this ""))
  10170. (completion-ignore-case t)
  10171. (member (member this org-todo-keywords-1))
  10172. (tail (cdr member))
  10173. (state (cond
  10174. ((and org-todo-key-trigger
  10175. (or (and (equal arg '(4))
  10176. (eq org-use-fast-todo-selection 'prefix))
  10177. (and (not arg) org-use-fast-todo-selection
  10178. (not (eq org-use-fast-todo-selection
  10179. 'prefix)))))
  10180. ;; Use fast selection
  10181. (org-fast-todo-selection))
  10182. ((and (equal arg '(4))
  10183. (or (not org-use-fast-todo-selection)
  10184. (not org-todo-key-trigger)))
  10185. ;; Read a state with completion
  10186. (org-icompleting-read
  10187. "State: " (mapcar (lambda(x) (list x))
  10188. org-todo-keywords-1)
  10189. nil t))
  10190. ((eq arg 'right)
  10191. (if this
  10192. (if tail (car tail) nil)
  10193. (car org-todo-keywords-1)))
  10194. ((eq arg 'left)
  10195. (if (equal member org-todo-keywords-1)
  10196. nil
  10197. (if this
  10198. (nth (- (length org-todo-keywords-1)
  10199. (length tail) 2)
  10200. org-todo-keywords-1)
  10201. (org-last org-todo-keywords-1))))
  10202. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10203. (setq arg nil))) ; hack to fall back to cycling
  10204. (arg
  10205. ;; user or caller requests a specific state
  10206. (cond
  10207. ((equal arg "") nil)
  10208. ((eq arg 'none) nil)
  10209. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10210. ((eq arg 'nextset)
  10211. (or (car (cdr (member head org-todo-heads)))
  10212. (car org-todo-heads)))
  10213. ((eq arg 'previousset)
  10214. (let ((org-todo-heads (reverse org-todo-heads)))
  10215. (or (car (cdr (member head org-todo-heads)))
  10216. (car org-todo-heads))))
  10217. ((car (member arg org-todo-keywords-1)))
  10218. ((stringp arg)
  10219. (error "State `%s' not valid in this file" arg))
  10220. ((nth (1- (prefix-numeric-value arg))
  10221. org-todo-keywords-1))))
  10222. ((null member) (or head (car org-todo-keywords-1)))
  10223. ((equal this final-done-word) nil) ;; -> make empty
  10224. ((null tail) nil) ;; -> first entry
  10225. ((memq interpret '(type priority))
  10226. (if (eq this-command last-command)
  10227. (car tail)
  10228. (if (> (length tail) 0)
  10229. (or done-word (car org-done-keywords))
  10230. nil)))
  10231. (t
  10232. (car tail))))
  10233. (state (or
  10234. (run-hook-with-args-until-success
  10235. 'org-todo-get-default-hook state last-state)
  10236. state))
  10237. (next (if state (concat " " state " ") " "))
  10238. (change-plist (list :type 'todo-state-change :from this :to state
  10239. :position startpos))
  10240. dolog now-done-p)
  10241. (when org-blocker-hook
  10242. (setq org-last-todo-state-is-todo
  10243. (not (member this org-done-keywords)))
  10244. (unless (save-excursion
  10245. (save-match-data
  10246. (org-with-wide-buffer
  10247. (run-hook-with-args-until-failure
  10248. 'org-blocker-hook change-plist))))
  10249. (if (org-called-interactively-p 'interactive)
  10250. (error "TODO state change from %s to %s blocked" this state)
  10251. ;; fail silently
  10252. (message "TODO state change from %s to %s blocked" this state)
  10253. (throw 'exit nil))))
  10254. (store-match-data match-data)
  10255. (replace-match next t t)
  10256. (unless (pos-visible-in-window-p hl-pos)
  10257. (message "TODO state changed to %s" (org-trim next)))
  10258. (unless head
  10259. (setq head (org-get-todo-sequence-head state)
  10260. ass (assoc head org-todo-kwd-alist)
  10261. interpret (nth 1 ass)
  10262. done-word (nth 3 ass)
  10263. final-done-word (nth 4 ass)))
  10264. (when (memq arg '(nextset previousset))
  10265. (message "Keyword-Set %d/%d: %s"
  10266. (- (length org-todo-sets) -1
  10267. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10268. (length org-todo-sets)
  10269. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10270. (setq org-last-todo-state-is-todo
  10271. (not (member state org-done-keywords)))
  10272. (setq now-done-p (and (member state org-done-keywords)
  10273. (not (member this org-done-keywords))))
  10274. (and logging (org-local-logging logging))
  10275. (when (and (or org-todo-log-states org-log-done)
  10276. (not (eq org-inhibit-logging t))
  10277. (not (memq arg '(nextset previousset))))
  10278. ;; we need to look at recording a time and note
  10279. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10280. (nth 2 (assoc this org-todo-log-states))))
  10281. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10282. (setq dolog 'time))
  10283. (when (and state
  10284. (member state org-not-done-keywords)
  10285. (not (member this org-not-done-keywords)))
  10286. ;; This is now a todo state and was not one before
  10287. ;; If there was a CLOSED time stamp, get rid of it.
  10288. (org-add-planning-info nil nil 'closed))
  10289. (when (and now-done-p org-log-done)
  10290. ;; It is now done, and it was not done before
  10291. (org-add-planning-info 'closed (org-current-effective-time))
  10292. (if (and (not dolog) (eq 'note org-log-done))
  10293. (org-add-log-setup 'done state this 'findpos 'note)))
  10294. (when (and state dolog)
  10295. ;; This is a non-nil state, and we need to log it
  10296. (org-add-log-setup 'state state this 'findpos dolog)))
  10297. ;; Fixup tag positioning
  10298. (org-todo-trigger-tag-changes state)
  10299. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10300. (when org-provide-todo-statistics
  10301. (org-update-parent-todo-statistics))
  10302. (run-hooks 'org-after-todo-state-change-hook)
  10303. (if (and arg (not (member state org-done-keywords)))
  10304. (setq head (org-get-todo-sequence-head state)))
  10305. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10306. ;; Do we need to trigger a repeat?
  10307. (when now-done-p
  10308. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10309. ;; This is for the agenda, take a snapshot of the headline.
  10310. (save-match-data
  10311. (setq org-agenda-headline-snapshot-before-repeat
  10312. (org-get-heading))))
  10313. (org-auto-repeat-maybe state))
  10314. ;; Fixup cursor location if close to the keyword
  10315. (if (and (outline-on-heading-p)
  10316. (not (bolp))
  10317. (save-excursion (beginning-of-line 1)
  10318. (looking-at org-todo-line-regexp))
  10319. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10320. (progn
  10321. (goto-char (or (match-end 2) (match-end 1)))
  10322. (and (looking-at " ") (just-one-space))))
  10323. (when org-trigger-hook
  10324. (save-excursion
  10325. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10326. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10327. "Block turning an entry into a TODO, using the hierarchy.
  10328. This checks whether the current task should be blocked from state
  10329. changes. Such blocking occurs when:
  10330. 1. The task has children which are not all in a completed state.
  10331. 2. A task has a parent with the property :ORDERED:, and there
  10332. are siblings prior to the current task with incomplete
  10333. status.
  10334. 3. The parent of the task is blocked because it has siblings that should
  10335. be done first, or is child of a block grandparent TODO entry."
  10336. (if (not org-enforce-todo-dependencies)
  10337. t ; if locally turned off don't block
  10338. (catch 'dont-block
  10339. ;; If this is not a todo state change, or if this entry is already DONE,
  10340. ;; do not block
  10341. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10342. (member (plist-get change-plist :from)
  10343. (cons 'done org-done-keywords))
  10344. (member (plist-get change-plist :to)
  10345. (cons 'todo org-not-done-keywords))
  10346. (not (plist-get change-plist :to)))
  10347. (throw 'dont-block t))
  10348. ;; If this task has children, and any are undone, it's blocked
  10349. (save-excursion
  10350. (org-back-to-heading t)
  10351. (let ((this-level (funcall outline-level)))
  10352. (outline-next-heading)
  10353. (let ((child-level (funcall outline-level)))
  10354. (while (and (not (eobp))
  10355. (> child-level this-level))
  10356. ;; this todo has children, check whether they are all
  10357. ;; completed
  10358. (if (and (not (org-entry-is-done-p))
  10359. (org-entry-is-todo-p))
  10360. (throw 'dont-block nil))
  10361. (outline-next-heading)
  10362. (setq child-level (funcall outline-level))))))
  10363. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10364. ;; any previous siblings are undone, it's blocked
  10365. (save-excursion
  10366. (org-back-to-heading t)
  10367. (let* ((pos (point))
  10368. (parent-pos (and (org-up-heading-safe) (point))))
  10369. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10370. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10371. (forward-line 1)
  10372. (re-search-forward org-not-done-heading-regexp pos t))
  10373. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10374. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10375. (while t
  10376. (goto-char parent-pos)
  10377. (if (not (looking-at org-not-done-heading-regexp))
  10378. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10379. (setq pos (point))
  10380. (setq parent-pos (and (org-up-heading-safe) (point)))
  10381. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10382. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10383. (forward-line 1)
  10384. (re-search-forward org-not-done-heading-regexp pos t))
  10385. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10386. (defcustom org-track-ordered-property-with-tag nil
  10387. "Should the ORDERED property also be shown as a tag?
  10388. The ORDERED property decides if an entry should require subtasks to be
  10389. completed in sequence. Since a property is not very visible, setting
  10390. this option means that toggling the ORDERED property with the command
  10391. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10392. not relevant for the behavior, but it makes things more visible.
  10393. Note that toggling the tag with tags commands will not change the property
  10394. and therefore not influence behavior!
  10395. This can be t, meaning the tag ORDERED should be used, It can also be a
  10396. string to select a different tag for this task."
  10397. :group 'org-todo
  10398. :type '(choice
  10399. (const :tag "No tracking" nil)
  10400. (const :tag "Track with ORDERED tag" t)
  10401. (string :tag "Use other tag")))
  10402. (defun org-toggle-ordered-property ()
  10403. "Toggle the ORDERED property of the current entry.
  10404. For better visibility, you can track the value of this property with a tag.
  10405. See variable `org-track-ordered-property-with-tag'."
  10406. (interactive)
  10407. (let* ((t1 org-track-ordered-property-with-tag)
  10408. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10409. (save-excursion
  10410. (org-back-to-heading)
  10411. (if (org-entry-get nil "ORDERED")
  10412. (progn
  10413. (org-delete-property "ORDERED")
  10414. (and tag (org-toggle-tag tag 'off))
  10415. (message "Subtasks can be completed in arbitrary order"))
  10416. (org-entry-put nil "ORDERED" "t")
  10417. (and tag (org-toggle-tag tag 'on))
  10418. (message "Subtasks must be completed in sequence")))))
  10419. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10420. (defun org-block-todo-from-checkboxes (change-plist)
  10421. "Block turning an entry into a TODO, using checkboxes.
  10422. This checks whether the current task should be blocked from state
  10423. changes because there are unchecked boxes in this entry."
  10424. (if (not org-enforce-todo-checkbox-dependencies)
  10425. t ; if locally turned off don't block
  10426. (catch 'dont-block
  10427. ;; If this is not a todo state change, or if this entry is already DONE,
  10428. ;; do not block
  10429. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10430. (member (plist-get change-plist :from)
  10431. (cons 'done org-done-keywords))
  10432. (member (plist-get change-plist :to)
  10433. (cons 'todo org-not-done-keywords))
  10434. (not (plist-get change-plist :to)))
  10435. (throw 'dont-block t))
  10436. ;; If this task has checkboxes that are not checked, it's blocked
  10437. (save-excursion
  10438. (org-back-to-heading t)
  10439. (let ((beg (point)) end)
  10440. (outline-next-heading)
  10441. (setq end (point))
  10442. (goto-char beg)
  10443. (if (org-list-search-forward
  10444. (concat (org-item-beginning-re)
  10445. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10446. "\\[[- ]\\]")
  10447. end t)
  10448. (progn
  10449. (if (boundp 'org-blocked-by-checkboxes)
  10450. (setq org-blocked-by-checkboxes t))
  10451. (throw 'dont-block nil)))))
  10452. t))) ; do not block
  10453. (defun org-entry-blocked-p ()
  10454. "Is the current entry blocked?"
  10455. (if (org-entry-get nil "NOBLOCKING")
  10456. nil ;; Never block this entry
  10457. (not
  10458. (run-hook-with-args-until-failure
  10459. 'org-blocker-hook
  10460. (list :type 'todo-state-change
  10461. :position (point)
  10462. :from 'todo
  10463. :to 'done)))))
  10464. (defun org-update-statistics-cookies (all)
  10465. "Update the statistics cookie, either from TODO or from checkboxes.
  10466. This should be called with the cursor in a line with a statistics cookie."
  10467. (interactive "P")
  10468. (if all
  10469. (progn
  10470. (org-update-checkbox-count 'all)
  10471. (org-map-entries 'org-update-parent-todo-statistics))
  10472. (if (not (org-at-heading-p))
  10473. (org-update-checkbox-count)
  10474. (let ((pos (move-marker (make-marker) (point)))
  10475. end l1 l2)
  10476. (ignore-errors (org-back-to-heading t))
  10477. (if (not (org-at-heading-p))
  10478. (org-update-checkbox-count)
  10479. (setq l1 (org-outline-level))
  10480. (setq end (save-excursion
  10481. (outline-next-heading)
  10482. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10483. (point)))
  10484. (if (and (save-excursion
  10485. (re-search-forward
  10486. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10487. (not (save-excursion (re-search-forward
  10488. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10489. (org-update-checkbox-count)
  10490. (if (and l2 (> l2 l1))
  10491. (progn
  10492. (goto-char end)
  10493. (org-update-parent-todo-statistics))
  10494. (goto-char pos)
  10495. (beginning-of-line 1)
  10496. (while (re-search-forward
  10497. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10498. (point-at-eol) t)
  10499. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10500. (goto-char pos)
  10501. (move-marker pos nil)))))
  10502. (defvar org-entry-property-inherited-from) ;; defined below
  10503. (defun org-update-parent-todo-statistics ()
  10504. "Update any statistics cookie in the parent of the current headline.
  10505. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10506. the entire subtree and this will travel up the hierarchy and update
  10507. statistics everywhere."
  10508. (let* ((prop (save-excursion (org-up-heading-safe)
  10509. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10510. (recursive (or (not org-hierarchical-todo-statistics)
  10511. (and prop (string-match "\\<recursive\\>" prop))))
  10512. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10513. 0))
  10514. (first t)
  10515. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10516. level ltoggle l1 new ndel
  10517. (cnt-all 0) (cnt-done 0) is-percent kwd
  10518. checkbox-beg ov ovs ove cookie-present)
  10519. (catch 'exit
  10520. (save-excursion
  10521. (beginning-of-line 1)
  10522. (setq ltoggle (funcall outline-level))
  10523. ;; Three situations are to consider:
  10524. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10525. ;; to the top-level ancestor on the headline;
  10526. ;; 2. If parent has "recursive" property, repeat up to the
  10527. ;; headline setting that property, taking inheritance into
  10528. ;; account;
  10529. ;; 3. Else, move up to direct parent and proceed only once.
  10530. (while (and (setq level (org-up-heading-safe))
  10531. (or recursive first)
  10532. (>= (point) lim))
  10533. (setq first nil cookie-present nil)
  10534. (unless (and level
  10535. (not (string-match
  10536. "\\<checkbox\\>"
  10537. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10538. "")))))
  10539. (throw 'exit nil))
  10540. (while (re-search-forward box-re (point-at-eol) t)
  10541. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10542. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10543. (save-match-data
  10544. (unless (outline-next-heading) (throw 'exit nil))
  10545. (while (and (looking-at org-complex-heading-regexp)
  10546. (> (setq l1 (length (match-string 1))) level))
  10547. (setq kwd (and (or recursive (= l1 ltoggle))
  10548. (match-string 2)))
  10549. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10550. (and (listp org-provide-todo-statistics)
  10551. (or (member kwd org-provide-todo-statistics)
  10552. (member kwd org-done-keywords))))
  10553. (setq cnt-all (1+ cnt-all))
  10554. (if (eq org-provide-todo-statistics t)
  10555. (and kwd (setq cnt-all (1+ cnt-all)))))
  10556. (and (member kwd org-done-keywords)
  10557. (setq cnt-done (1+ cnt-done)))
  10558. (outline-next-heading)))
  10559. (setq new
  10560. (if is-percent
  10561. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10562. (format "[%d/%d]" cnt-done cnt-all))
  10563. ndel (- (match-end 0) checkbox-beg))
  10564. ;; handle overlays when updating cookie from column view
  10565. (when (setq ov (car (overlays-at checkbox-beg)))
  10566. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10567. (delete-overlay ov))
  10568. (goto-char checkbox-beg)
  10569. (insert new)
  10570. (delete-region (point) (+ (point) ndel))
  10571. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10572. (when ov (move-overlay ov ovs ove)))
  10573. (when cookie-present
  10574. (run-hook-with-args 'org-after-todo-statistics-hook
  10575. cnt-done (- cnt-all cnt-done))))))
  10576. (run-hooks 'org-todo-statistics-hook)))
  10577. (defvar org-after-todo-statistics-hook nil
  10578. "Hook that is called after a TODO statistics cookie has been updated.
  10579. Each function is called with two arguments: the number of not-done entries
  10580. and the number of done entries.
  10581. For example, the following function, when added to this hook, will switch
  10582. an entry to DONE when all children are done, and back to TODO when new
  10583. entries are set to a TODO status. Note that this hook is only called
  10584. when there is a statistics cookie in the headline!
  10585. (defun org-summary-todo (n-done n-not-done)
  10586. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10587. (let (org-log-done org-log-states) ; turn off logging
  10588. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10589. ")
  10590. (defvar org-todo-statistics-hook nil
  10591. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10592. This hook runs even if there is no statistics cookie present, in which case
  10593. `org-after-todo-statistics-hook' would not run.")
  10594. (defun org-todo-trigger-tag-changes (state)
  10595. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10596. (let ((l org-todo-state-tags-triggers)
  10597. changes)
  10598. (when (or (not state) (equal state ""))
  10599. (setq changes (append changes (cdr (assoc "" l)))))
  10600. (when (and (stringp state) (> (length state) 0))
  10601. (setq changes (append changes (cdr (assoc state l)))))
  10602. (when (member state org-not-done-keywords)
  10603. (setq changes (append changes (cdr (assoc 'todo l)))))
  10604. (when (member state org-done-keywords)
  10605. (setq changes (append changes (cdr (assoc 'done l)))))
  10606. (dolist (c changes)
  10607. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10608. (defun org-local-logging (value)
  10609. "Get logging settings from a property VALUE."
  10610. (let* (words w a)
  10611. ;; directly set the variables, they are already local.
  10612. (setq org-log-done nil
  10613. org-log-repeat nil
  10614. org-todo-log-states nil)
  10615. (setq words (org-split-string value))
  10616. (while (setq w (pop words))
  10617. (cond
  10618. ((setq a (assoc w org-startup-options))
  10619. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10620. (set (nth 1 a) (nth 2 a))))
  10621. ((setq a (org-extract-log-state-settings w))
  10622. (and (member (car a) org-todo-keywords-1)
  10623. (push a org-todo-log-states)))))))
  10624. (defun org-get-todo-sequence-head (kwd)
  10625. "Return the head of the TODO sequence to which KWD belongs.
  10626. If KWD is not set, check if there is a text property remembering the
  10627. right sequence."
  10628. (let (p)
  10629. (cond
  10630. ((not kwd)
  10631. (or (get-text-property (point-at-bol) 'org-todo-head)
  10632. (progn
  10633. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10634. nil (point-at-eol)))
  10635. (get-text-property p 'org-todo-head))))
  10636. ((not (member kwd org-todo-keywords-1))
  10637. (car org-todo-keywords-1))
  10638. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10639. (defun org-fast-todo-selection ()
  10640. "Fast TODO keyword selection with single keys.
  10641. Returns the new TODO keyword, or nil if no state change should occur."
  10642. (let* ((fulltable org-todo-key-alist)
  10643. (done-keywords org-done-keywords) ;; needed for the faces.
  10644. (maxlen (apply 'max (mapcar
  10645. (lambda (x)
  10646. (if (stringp (car x)) (string-width (car x)) 0))
  10647. fulltable)))
  10648. (expert nil)
  10649. (fwidth (+ maxlen 3 1 3))
  10650. (ncol (/ (- (window-width) 4) fwidth))
  10651. tg cnt e c tbl
  10652. groups ingroup)
  10653. (save-excursion
  10654. (save-window-excursion
  10655. (if expert
  10656. (set-buffer (get-buffer-create " *Org todo*"))
  10657. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10658. (erase-buffer)
  10659. (org-set-local 'org-done-keywords done-keywords)
  10660. (setq tbl fulltable cnt 0)
  10661. (while (setq e (pop tbl))
  10662. (cond
  10663. ((equal e '(:startgroup))
  10664. (push '() groups) (setq ingroup t)
  10665. (when (not (= cnt 0))
  10666. (setq cnt 0)
  10667. (insert "\n"))
  10668. (insert "{ "))
  10669. ((equal e '(:endgroup))
  10670. (setq ingroup nil cnt 0)
  10671. (insert "}\n"))
  10672. ((equal e '(:newline))
  10673. (when (not (= cnt 0))
  10674. (setq cnt 0)
  10675. (insert "\n")
  10676. (setq e (car tbl))
  10677. (while (equal (car tbl) '(:newline))
  10678. (insert "\n")
  10679. (setq tbl (cdr tbl)))))
  10680. (t
  10681. (setq tg (car e) c (cdr e))
  10682. (if ingroup (push tg (car groups)))
  10683. (setq tg (org-add-props tg nil 'face
  10684. (org-get-todo-face tg)))
  10685. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10686. (insert "[" c "] " tg (make-string
  10687. (- fwidth 4 (length tg)) ?\ ))
  10688. (when (= (setq cnt (1+ cnt)) ncol)
  10689. (insert "\n")
  10690. (if ingroup (insert " "))
  10691. (setq cnt 0)))))
  10692. (insert "\n")
  10693. (goto-char (point-min))
  10694. (if (not expert) (org-fit-window-to-buffer))
  10695. (message "[a-z..]:Set [SPC]:clear")
  10696. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10697. (cond
  10698. ((or (= c ?\C-g)
  10699. (and (= c ?q) (not (rassoc c fulltable))))
  10700. (setq quit-flag t))
  10701. ((= c ?\ ) nil)
  10702. ((setq e (rassoc c fulltable) tg (car e))
  10703. tg)
  10704. (t (setq quit-flag t)))))))
  10705. (defun org-entry-is-todo-p ()
  10706. (member (org-get-todo-state) org-not-done-keywords))
  10707. (defun org-entry-is-done-p ()
  10708. (member (org-get-todo-state) org-done-keywords))
  10709. (defun org-get-todo-state ()
  10710. (save-excursion
  10711. (org-back-to-heading t)
  10712. (and (looking-at org-todo-line-regexp)
  10713. (match-end 2)
  10714. (match-string 2))))
  10715. (defun org-at-date-range-p (&optional inactive-ok)
  10716. "Is the cursor inside a date range?"
  10717. (interactive)
  10718. (save-excursion
  10719. (catch 'exit
  10720. (let ((pos (point)))
  10721. (skip-chars-backward "^[<\r\n")
  10722. (skip-chars-backward "<[")
  10723. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10724. (>= (match-end 0) pos)
  10725. (throw 'exit t))
  10726. (skip-chars-backward "^<[\r\n")
  10727. (skip-chars-backward "<[")
  10728. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10729. (>= (match-end 0) pos)
  10730. (throw 'exit t)))
  10731. nil)))
  10732. (defun org-get-repeat (&optional tagline)
  10733. "Check if there is a deadline/schedule with repeater in this entry."
  10734. (save-match-data
  10735. (save-excursion
  10736. (org-back-to-heading t)
  10737. (and (re-search-forward (if tagline
  10738. (concat tagline "\\s-*" org-repeat-re)
  10739. org-repeat-re)
  10740. (org-entry-end-position) t)
  10741. (match-string-no-properties 1)))))
  10742. (defvar org-last-changed-timestamp)
  10743. (defvar org-last-inserted-timestamp)
  10744. (defvar org-log-post-message)
  10745. (defvar org-log-note-purpose)
  10746. (defvar org-log-note-how)
  10747. (defvar org-log-note-extra)
  10748. (defun org-auto-repeat-maybe (done-word)
  10749. "Check if the current headline contains a repeated deadline/schedule.
  10750. If yes, set TODO state back to what it was and change the base date
  10751. of repeating deadline/scheduled time stamps to new date.
  10752. This function is run automatically after each state change to a DONE state."
  10753. ;; last-state is dynamically scoped into this function
  10754. (let* ((repeat (org-get-repeat))
  10755. (aa (assoc last-state org-todo-kwd-alist))
  10756. (interpret (nth 1 aa))
  10757. (head (nth 2 aa))
  10758. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10759. (msg "Entry repeats: ")
  10760. (org-log-done nil)
  10761. (org-todo-log-states nil)
  10762. re type n what ts time to-state)
  10763. (when repeat
  10764. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10765. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10766. org-todo-repeat-to-state))
  10767. (unless (and to-state (member to-state org-todo-keywords-1))
  10768. (setq to-state (if (eq interpret 'type) last-state head)))
  10769. (org-todo to-state)
  10770. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10771. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10772. (org-time-stamp-format t t))))
  10773. (when org-log-repeat
  10774. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10775. (memq 'org-add-log-note post-command-hook))
  10776. ;; OK, we are already setup for some record
  10777. (if (eq org-log-repeat 'note)
  10778. ;; make sure we take a note, not only a time stamp
  10779. (setq org-log-note-how 'note))
  10780. ;; Set up for taking a record
  10781. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10782. last-state
  10783. 'findpos org-log-repeat)))
  10784. (org-back-to-heading t)
  10785. (org-add-planning-info nil nil 'closed)
  10786. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10787. org-deadline-time-regexp "\\)\\|\\("
  10788. org-ts-regexp "\\)"))
  10789. (while (re-search-forward
  10790. re (save-excursion (outline-next-heading) (point)) t)
  10791. (setq type (if (match-end 1) org-scheduled-string
  10792. (if (match-end 3) org-deadline-string "Plain:"))
  10793. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10794. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10795. (setq n (string-to-number (match-string 2 ts))
  10796. what (match-string 3 ts))
  10797. (if (equal what "w") (setq n (* n 7) what "d"))
  10798. ;; Preparation, see if we need to modify the start date for the change
  10799. (when (match-end 1)
  10800. (setq time (save-match-data (org-time-string-to-time ts)))
  10801. (cond
  10802. ((equal (match-string 1 ts) ".")
  10803. ;; Shift starting date to today
  10804. (org-timestamp-change
  10805. (- (org-today) (time-to-days time))
  10806. 'day))
  10807. ((equal (match-string 1 ts) "+")
  10808. (let ((nshiftmax 10) (nshift 0))
  10809. (while (or (= nshift 0)
  10810. (<= (time-to-days time)
  10811. (time-to-days (current-time))))
  10812. (when (= (incf nshift) nshiftmax)
  10813. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10814. (error "Abort")))
  10815. (org-timestamp-change n (cdr (assoc what whata)))
  10816. (org-at-timestamp-p t)
  10817. (setq ts (match-string 1))
  10818. (setq time (save-match-data (org-time-string-to-time ts)))))
  10819. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10820. ;; rematch, so that we have everything in place for the real shift
  10821. (org-at-timestamp-p t)
  10822. (setq ts (match-string 1))
  10823. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10824. (org-timestamp-change n (cdr (assoc what whata)))
  10825. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10826. (setq org-log-post-message msg)
  10827. (message "%s" msg))))
  10828. (defun org-show-todo-tree (arg)
  10829. "Make a compact tree which shows all headlines marked with TODO.
  10830. The tree will show the lines where the regexp matches, and all higher
  10831. headlines above the match.
  10832. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10833. With a numeric prefix N, construct a sparse tree for the Nth element
  10834. of `org-todo-keywords-1'."
  10835. (interactive "P")
  10836. (let ((case-fold-search nil)
  10837. (kwd-re
  10838. (cond ((null arg) org-not-done-regexp)
  10839. ((equal arg '(4))
  10840. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10841. (mapcar 'list org-todo-keywords-1))))
  10842. (concat "\\("
  10843. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10844. "\\)\\>")))
  10845. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10846. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10847. org-todo-keywords-1)))
  10848. (t (error "Invalid prefix argument: %s" arg)))))
  10849. (message "%d TODO entries found"
  10850. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10851. (defun org-deadline (&optional remove time)
  10852. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10853. With argument REMOVE, remove any deadline from the item.
  10854. With argument TIME, set the deadline at the corresponding date. TIME
  10855. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10856. (interactive "P")
  10857. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10858. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10859. 'region-start-level 'region))
  10860. org-loop-over-headlines-in-active-region)
  10861. (org-map-entries
  10862. `(org-deadline ',remove ,time)
  10863. org-loop-over-headlines-in-active-region
  10864. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10865. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10866. (repeater (and old-date
  10867. (string-match
  10868. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10869. old-date)
  10870. (match-string 1 old-date))))
  10871. (if remove
  10872. (progn
  10873. (when (and old-date org-log-redeadline)
  10874. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10875. org-log-redeadline))
  10876. (org-remove-timestamp-with-keyword org-deadline-string)
  10877. (message "Item no longer has a deadline."))
  10878. (org-add-planning-info 'deadline time 'closed)
  10879. (when (and old-date org-log-redeadline
  10880. (not (equal old-date
  10881. (substring org-last-inserted-timestamp 1 -1))))
  10882. (org-add-log-setup 'redeadline nil old-date 'findpos
  10883. org-log-redeadline))
  10884. (when repeater
  10885. (save-excursion
  10886. (org-back-to-heading t)
  10887. (when (re-search-forward (concat org-deadline-string " "
  10888. org-last-inserted-timestamp)
  10889. (save-excursion
  10890. (outline-next-heading) (point)) t)
  10891. (goto-char (1- (match-end 0)))
  10892. (insert " " repeater)
  10893. (setq org-last-inserted-timestamp
  10894. (concat (substring org-last-inserted-timestamp 0 -1)
  10895. " " repeater
  10896. (substring org-last-inserted-timestamp -1))))))
  10897. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10898. (defun org-schedule (&optional remove time)
  10899. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10900. With argument REMOVE, remove any scheduling date from the item.
  10901. With argument TIME, scheduled at the corresponding date. TIME can
  10902. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10903. (interactive "P")
  10904. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10905. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10906. 'region-start-level 'region))
  10907. org-loop-over-headlines-in-active-region)
  10908. (org-map-entries
  10909. `(org-schedule ',remove ,time)
  10910. org-loop-over-headlines-in-active-region
  10911. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10912. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10913. (repeater (and old-date
  10914. (string-match
  10915. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10916. old-date)
  10917. (match-string 1 old-date))))
  10918. (if remove
  10919. (progn
  10920. (when (and old-date org-log-reschedule)
  10921. (org-add-log-setup 'delschedule nil old-date 'findpos
  10922. org-log-reschedule))
  10923. (org-remove-timestamp-with-keyword org-scheduled-string)
  10924. (message "Item is no longer scheduled."))
  10925. (org-add-planning-info 'scheduled time 'closed)
  10926. (when (and old-date org-log-reschedule
  10927. (not (equal old-date
  10928. (substring org-last-inserted-timestamp 1 -1))))
  10929. (org-add-log-setup 'reschedule nil old-date 'findpos
  10930. org-log-reschedule))
  10931. (when repeater
  10932. (save-excursion
  10933. (org-back-to-heading t)
  10934. (when (re-search-forward (concat org-scheduled-string " "
  10935. org-last-inserted-timestamp)
  10936. (save-excursion
  10937. (outline-next-heading) (point)) t)
  10938. (goto-char (1- (match-end 0)))
  10939. (insert " " repeater)
  10940. (setq org-last-inserted-timestamp
  10941. (concat (substring org-last-inserted-timestamp 0 -1)
  10942. " " repeater
  10943. (substring org-last-inserted-timestamp -1))))))
  10944. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10945. (defun org-get-scheduled-time (pom &optional inherit)
  10946. "Get the scheduled time as a time tuple, of a format suitable
  10947. for calling org-schedule with, or if there is no scheduling,
  10948. returns nil."
  10949. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10950. (when time
  10951. (apply 'encode-time (org-parse-time-string time)))))
  10952. (defun org-get-deadline-time (pom &optional inherit)
  10953. "Get the deadline as a time tuple, of a format suitable for
  10954. calling org-deadline with, or if there is no scheduling, returns
  10955. nil."
  10956. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10957. (when time
  10958. (apply 'encode-time (org-parse-time-string time)))))
  10959. (defun org-remove-timestamp-with-keyword (keyword)
  10960. "Remove all time stamps with KEYWORD in the current entry."
  10961. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10962. beg)
  10963. (save-excursion
  10964. (org-back-to-heading t)
  10965. (setq beg (point))
  10966. (outline-next-heading)
  10967. (while (re-search-backward re beg t)
  10968. (replace-match "")
  10969. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10970. (equal (char-before) ?\ ))
  10971. (backward-delete-char 1)
  10972. (if (string-match "^[ \t]*$" (buffer-substring
  10973. (point-at-bol) (point-at-eol)))
  10974. (delete-region (point-at-bol)
  10975. (min (point-max) (1+ (point-at-eol))))))))))
  10976. (defun org-add-planning-info (what &optional time &rest remove)
  10977. "Insert new timestamp with keyword in the line directly after the headline.
  10978. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  10979. If non is given, the user is prompted for a date.
  10980. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10981. be removed."
  10982. (interactive)
  10983. (let (org-time-was-given org-end-time-was-given ts
  10984. end default-time default-input)
  10985. (catch 'exit
  10986. (when (and (memq what '(scheduled deadline))
  10987. (or (not time)
  10988. (and (stringp time)
  10989. (string-match "^[-+]+[0-9]" time))))
  10990. ;; Try to get a default date/time from existing timestamp
  10991. (save-excursion
  10992. (org-back-to-heading t)
  10993. (setq end (save-excursion (outline-next-heading) (point)))
  10994. (when (re-search-forward (if (eq what 'scheduled)
  10995. org-scheduled-time-regexp
  10996. org-deadline-time-regexp)
  10997. end t)
  10998. (setq ts (match-string 1)
  10999. default-time
  11000. (apply 'encode-time (org-parse-time-string ts))
  11001. default-input (and ts (org-get-compact-tod ts))))))
  11002. (when what
  11003. (setq time
  11004. (if (stringp time)
  11005. ;; This is a string (relative or absolute), set proper date
  11006. (apply 'encode-time
  11007. (org-read-date-analyze
  11008. time default-time (decode-time default-time)))
  11009. ;; If necessary, get the time from the user
  11010. (or time (org-read-date nil 'to-time nil nil
  11011. default-time default-input)))))
  11012. (when (and org-insert-labeled-timestamps-at-point
  11013. (member what '(scheduled deadline)))
  11014. (insert
  11015. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11016. (org-insert-time-stamp time org-time-was-given
  11017. nil nil nil (list org-end-time-was-given))
  11018. (setq what nil))
  11019. (save-excursion
  11020. (save-restriction
  11021. (let (col list elt ts buffer-invisibility-spec)
  11022. (org-back-to-heading t)
  11023. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11024. (goto-char (match-end 1))
  11025. (setq col (current-column))
  11026. (goto-char (match-end 0))
  11027. (if (eobp) (insert "\n") (forward-char 1))
  11028. (when (and (not what)
  11029. (not (looking-at
  11030. (concat "[ \t]*"
  11031. org-keyword-time-not-clock-regexp))))
  11032. ;; Nothing to add, nothing to remove...... :-)
  11033. (throw 'exit nil))
  11034. (if (and (not (looking-at org-outline-regexp))
  11035. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11036. "[^\r\n]*"))
  11037. (not (equal (match-string 1) org-clock-string)))
  11038. (narrow-to-region (match-beginning 0) (match-end 0))
  11039. (insert-before-markers "\n")
  11040. (backward-char 1)
  11041. (narrow-to-region (point) (point))
  11042. (and org-adapt-indentation (org-indent-to-column col)))
  11043. ;; Check if we have to remove something.
  11044. (setq list (cons what remove))
  11045. (while list
  11046. (setq elt (pop list))
  11047. (when (or (and (eq elt 'scheduled)
  11048. (re-search-forward org-scheduled-time-regexp nil t))
  11049. (and (eq elt 'deadline)
  11050. (re-search-forward org-deadline-time-regexp nil t))
  11051. (and (eq elt 'closed)
  11052. (re-search-forward org-closed-time-regexp nil t)))
  11053. (replace-match "")
  11054. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11055. (and (looking-at "[ \t]+") (replace-match ""))
  11056. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11057. (when what
  11058. (insert
  11059. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11060. (cond ((eq what 'scheduled) org-scheduled-string)
  11061. ((eq what 'deadline) org-deadline-string)
  11062. ((eq what 'closed) org-closed-string))
  11063. " ")
  11064. (setq ts (org-insert-time-stamp
  11065. time
  11066. (or org-time-was-given
  11067. (and (eq what 'closed) org-log-done-with-time))
  11068. (eq what 'closed)
  11069. nil nil (list org-end-time-was-given)))
  11070. (insert
  11071. (if (not (or (bolp) (eq (char-before) ?\ )
  11072. (memq (char-after) '(32 10))
  11073. (eobp))) " " ""))
  11074. (end-of-line 1))
  11075. (goto-char (point-min))
  11076. (widen)
  11077. (if (and (looking-at "[ \t]*\n")
  11078. (equal (char-before) ?\n))
  11079. (delete-region (1- (point)) (point-at-eol)))
  11080. ts))))))
  11081. (defvar org-log-note-marker (make-marker))
  11082. (defvar org-log-note-purpose nil)
  11083. (defvar org-log-note-state nil)
  11084. (defvar org-log-note-previous-state nil)
  11085. (defvar org-log-note-how nil)
  11086. (defvar org-log-note-extra nil)
  11087. (defvar org-log-note-window-configuration nil)
  11088. (defvar org-log-note-return-to (make-marker))
  11089. (defvar org-log-note-effective-time nil
  11090. "Remembered current time so that dynamically scoped
  11091. `org-extend-today-until' affects tha timestamps in state change
  11092. log")
  11093. (defvar org-log-post-message nil
  11094. "Message to be displayed after a log note has been stored.
  11095. The auto-repeater uses this.")
  11096. (defun org-add-note ()
  11097. "Add a note to the current entry.
  11098. This is done in the same way as adding a state change note."
  11099. (interactive)
  11100. (org-add-log-setup 'note nil nil 'findpos nil))
  11101. (defvar org-property-end-re)
  11102. (defun org-add-log-setup (&optional purpose state prev-state
  11103. findpos how extra)
  11104. "Set up the post command hook to take a note.
  11105. If this is about to TODO state change, the new state is expected in STATE.
  11106. When FINDPOS is non-nil, find the correct position for the note in
  11107. the current entry. If not, assume that it can be inserted at point.
  11108. HOW is an indicator what kind of note should be created.
  11109. EXTRA is additional text that will be inserted into the notes buffer."
  11110. (let* ((org-log-into-drawer (org-log-into-drawer))
  11111. (drawer (cond ((stringp org-log-into-drawer)
  11112. org-log-into-drawer)
  11113. (org-log-into-drawer "LOGBOOK")
  11114. (t nil))))
  11115. (save-restriction
  11116. (save-excursion
  11117. (when findpos
  11118. (org-back-to-heading t)
  11119. (narrow-to-region (point) (save-excursion
  11120. (outline-next-heading) (point)))
  11121. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11122. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11123. "[^\r\n]*\\)?"))
  11124. (goto-char (match-end 0))
  11125. (cond
  11126. (drawer
  11127. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11128. nil t)
  11129. (progn
  11130. (goto-char (match-end 0))
  11131. (or org-log-states-order-reversed
  11132. (and (re-search-forward org-property-end-re nil t)
  11133. (goto-char (1- (match-beginning 0))))))
  11134. (insert "\n:" drawer ":\n:END:")
  11135. (beginning-of-line 0)
  11136. (org-indent-line-function)
  11137. (beginning-of-line 2)
  11138. (org-indent-line-function)
  11139. (end-of-line 0)))
  11140. ((and org-log-state-notes-insert-after-drawers
  11141. (save-excursion
  11142. (forward-line) (looking-at org-drawer-regexp)))
  11143. (forward-line)
  11144. (while (looking-at org-drawer-regexp)
  11145. (goto-char (match-end 0))
  11146. (re-search-forward org-property-end-re (point-max) t)
  11147. (forward-line))
  11148. (forward-line -1)))
  11149. (unless org-log-states-order-reversed
  11150. (and (= (char-after) ?\n) (forward-char 1))
  11151. (org-skip-over-state-notes)
  11152. (skip-chars-backward " \t\n\r")))
  11153. (move-marker org-log-note-marker (point))
  11154. (setq org-log-note-purpose purpose
  11155. org-log-note-state state
  11156. org-log-note-previous-state prev-state
  11157. org-log-note-how how
  11158. org-log-note-extra extra
  11159. org-log-note-effective-time (org-current-effective-time))
  11160. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11161. (defun org-skip-over-state-notes ()
  11162. "Skip past the list of State notes in an entry."
  11163. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11164. (when (ignore-errors (goto-char (org-in-item-p)))
  11165. (let* ((struct (org-list-struct))
  11166. (prevs (org-list-prevs-alist struct)))
  11167. (while (looking-at "[ \t]*- State")
  11168. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11169. (org-list-get-item-end (point) struct)))))))
  11170. (defun org-add-log-note (&optional purpose)
  11171. "Pop up a window for taking a note, and add this note later at point."
  11172. (remove-hook 'post-command-hook 'org-add-log-note)
  11173. (setq org-log-note-window-configuration (current-window-configuration))
  11174. (delete-other-windows)
  11175. (move-marker org-log-note-return-to (point))
  11176. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11177. (goto-char org-log-note-marker)
  11178. (org-switch-to-buffer-other-window "*Org Note*")
  11179. (erase-buffer)
  11180. (if (memq org-log-note-how '(time state))
  11181. (let (current-prefix-arg) (org-store-log-note))
  11182. (let ((org-inhibit-startup t)) (org-mode))
  11183. (insert (format "# Insert note for %s.
  11184. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11185. (cond
  11186. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11187. ((eq org-log-note-purpose 'done) "closed todo item")
  11188. ((eq org-log-note-purpose 'state)
  11189. (format "state change from \"%s\" to \"%s\""
  11190. (or org-log-note-previous-state "")
  11191. (or org-log-note-state "")))
  11192. ((eq org-log-note-purpose 'reschedule)
  11193. "rescheduling")
  11194. ((eq org-log-note-purpose 'delschedule)
  11195. "no longer scheduled")
  11196. ((eq org-log-note-purpose 'redeadline)
  11197. "changing deadline")
  11198. ((eq org-log-note-purpose 'deldeadline)
  11199. "removing deadline")
  11200. ((eq org-log-note-purpose 'refile)
  11201. "refiling")
  11202. ((eq org-log-note-purpose 'note)
  11203. "this entry")
  11204. (t (error "This should not happen")))))
  11205. (if org-log-note-extra (insert org-log-note-extra))
  11206. (org-set-local 'org-finish-function 'org-store-log-note)
  11207. (run-hooks 'org-log-buffer-setup-hook)))
  11208. (defvar org-note-abort nil) ; dynamically scoped
  11209. (defun org-store-log-note ()
  11210. "Finish taking a log note, and insert it to where it belongs."
  11211. (let ((txt (buffer-string))
  11212. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11213. lines ind bul)
  11214. (kill-buffer (current-buffer))
  11215. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11216. (setq txt (replace-match "" t t txt)))
  11217. (if (string-match "\\s-+\\'" txt)
  11218. (setq txt (replace-match "" t t txt)))
  11219. (setq lines (org-split-string txt "\n"))
  11220. (when (and note (string-match "\\S-" note))
  11221. (setq note
  11222. (org-replace-escapes
  11223. note
  11224. (list (cons "%u" (user-login-name))
  11225. (cons "%U" user-full-name)
  11226. (cons "%t" (format-time-string
  11227. (org-time-stamp-format 'long 'inactive)
  11228. org-log-note-effective-time))
  11229. (cons "%T" (format-time-string
  11230. (org-time-stamp-format 'long nil)
  11231. org-log-note-effective-time))
  11232. (cons "%d" (format-time-string
  11233. (org-time-stamp-format nil 'inactive)
  11234. org-log-note-effective-time))
  11235. (cons "%D" (format-time-string
  11236. (org-time-stamp-format nil nil)
  11237. org-log-note-effective-time))
  11238. (cons "%s" (if org-log-note-state
  11239. (concat "\"" org-log-note-state "\"")
  11240. ""))
  11241. (cons "%S" (if org-log-note-previous-state
  11242. (concat "\"" org-log-note-previous-state "\"")
  11243. "\"\"")))))
  11244. (if lines (setq note (concat note " \\\\")))
  11245. (push note lines))
  11246. (when (or current-prefix-arg org-note-abort)
  11247. (when org-log-into-drawer
  11248. (org-remove-empty-drawer-at
  11249. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11250. org-log-note-marker))
  11251. (setq lines nil))
  11252. (when lines
  11253. (with-current-buffer (marker-buffer org-log-note-marker)
  11254. (save-excursion
  11255. (goto-char org-log-note-marker)
  11256. (move-marker org-log-note-marker nil)
  11257. (end-of-line 1)
  11258. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11259. (setq ind (save-excursion
  11260. (if (ignore-errors (goto-char (org-in-item-p)))
  11261. (let ((struct (org-list-struct)))
  11262. (org-list-get-ind
  11263. (org-list-get-top-point struct) struct))
  11264. (skip-chars-backward " \r\t\n")
  11265. (cond
  11266. ((and (org-at-heading-p)
  11267. org-adapt-indentation)
  11268. (1+ (org-current-level)))
  11269. ((org-at-heading-p) 0)
  11270. (t (org-get-indentation))))))
  11271. (setq bul (org-list-bullet-string "-"))
  11272. (org-indent-line-to ind)
  11273. (insert bul (pop lines))
  11274. (let ((ind-body (+ (length bul) ind)))
  11275. (while lines
  11276. (insert "\n")
  11277. (org-indent-line-to ind-body)
  11278. (insert (pop lines))))
  11279. (message "Note stored")
  11280. (org-back-to-heading t)
  11281. (org-cycle-hide-drawers 'children)))))
  11282. (set-window-configuration org-log-note-window-configuration)
  11283. (with-current-buffer (marker-buffer org-log-note-return-to)
  11284. (goto-char org-log-note-return-to))
  11285. (move-marker org-log-note-return-to nil)
  11286. (and org-log-post-message (message "%s" org-log-post-message)))
  11287. (defun org-remove-empty-drawer-at (drawer pos)
  11288. "Remove an empty drawer DRAWER at position POS.
  11289. POS may also be a marker."
  11290. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11291. (save-excursion
  11292. (save-restriction
  11293. (widen)
  11294. (goto-char pos)
  11295. (if (org-in-regexp
  11296. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11297. (replace-match ""))))))
  11298. (defun org-sparse-tree (&optional arg)
  11299. "Create a sparse tree, prompt for the details.
  11300. This command can create sparse trees. You first need to select the type
  11301. of match used to create the tree:
  11302. t Show all TODO entries.
  11303. T Show entries with a specific TODO keyword.
  11304. m Show entries selected by a tags/property match.
  11305. p Enter a property name and its value (both with completion on existing
  11306. names/values) and show entries with that property.
  11307. r Show entries matching a regular expression (`/' can be used as well)
  11308. d Show deadlines due within `org-deadline-warning-days'.
  11309. b Show deadlines and scheduled items before a date.
  11310. a Show deadlines and scheduled items after a date."
  11311. (interactive "P")
  11312. (let (ans kwd value)
  11313. (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")
  11314. (setq ans (read-char-exclusive))
  11315. (cond
  11316. ((equal ans ?d)
  11317. (call-interactively 'org-check-deadlines))
  11318. ((equal ans ?b)
  11319. (call-interactively 'org-check-before-date))
  11320. ((equal ans ?a)
  11321. (call-interactively 'org-check-after-date))
  11322. ((equal ans ?D)
  11323. (call-interactively 'org-check-dates-range))
  11324. ((equal ans ?t)
  11325. (org-show-todo-tree nil))
  11326. ((equal ans ?T)
  11327. (org-show-todo-tree '(4)))
  11328. ((member ans '(?T ?m))
  11329. (call-interactively 'org-match-sparse-tree))
  11330. ((member ans '(?p ?P))
  11331. (setq kwd (org-icompleting-read "Property: "
  11332. (mapcar 'list (org-buffer-property-keys))))
  11333. (setq value (org-icompleting-read "Value: "
  11334. (mapcar 'list (org-property-values kwd))))
  11335. (unless (string-match "\\`{.*}\\'" value)
  11336. (setq value (concat "\"" value "\"")))
  11337. (org-match-sparse-tree arg (concat kwd "=" value)))
  11338. ((member ans '(?r ?R ?/))
  11339. (call-interactively 'org-occur))
  11340. (t (error "No such sparse tree command \"%c\"" ans)))))
  11341. (defvar org-occur-highlights nil
  11342. "List of overlays used for occur matches.")
  11343. (make-variable-buffer-local 'org-occur-highlights)
  11344. (defvar org-occur-parameters nil
  11345. "Parameters of the active org-occur calls.
  11346. This is a list, each call to org-occur pushes as cons cell,
  11347. containing the regular expression and the callback, onto the list.
  11348. The list can contain several entries if `org-occur' has been called
  11349. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11350. will only contain one set of parameters. When the highlights are
  11351. removed (for example with `C-c C-c', or with the next edit (depending
  11352. on `org-remove-highlights-with-change'), this variable is emptied
  11353. as well.")
  11354. (make-variable-buffer-local 'org-occur-parameters)
  11355. (defun org-occur (regexp &optional keep-previous callback)
  11356. "Make a compact tree which shows all matches of REGEXP.
  11357. The tree will show the lines where the regexp matches, and all higher
  11358. headlines above the match. It will also show the heading after the match,
  11359. to make sure editing the matching entry is easy.
  11360. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11361. call to `org-occur' will be kept, to allow stacking of calls to this
  11362. command.
  11363. If CALLBACK is non-nil, it is a function which is called to confirm
  11364. that the match should indeed be shown."
  11365. (interactive "sRegexp: \nP")
  11366. (when (equal regexp "")
  11367. (error "Regexp cannot be empty"))
  11368. (unless keep-previous
  11369. (org-remove-occur-highlights nil nil t))
  11370. (push (cons regexp callback) org-occur-parameters)
  11371. (let ((cnt 0))
  11372. (save-excursion
  11373. (goto-char (point-min))
  11374. (if (or (not keep-previous) ; do not want to keep
  11375. (not org-occur-highlights)) ; no previous matches
  11376. ;; hide everything
  11377. (org-overview))
  11378. (while (re-search-forward regexp nil t)
  11379. (when (or (not callback)
  11380. (save-match-data (funcall callback)))
  11381. (setq cnt (1+ cnt))
  11382. (when org-highlight-sparse-tree-matches
  11383. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11384. (org-show-context 'occur-tree))))
  11385. (when org-remove-highlights-with-change
  11386. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11387. nil 'local))
  11388. (unless org-sparse-tree-open-archived-trees
  11389. (org-hide-archived-subtrees (point-min) (point-max)))
  11390. (run-hooks 'org-occur-hook)
  11391. (if (org-called-interactively-p 'interactive)
  11392. (message "%d match(es) for regexp %s" cnt regexp))
  11393. cnt))
  11394. (defun org-occur-next-match (&optional n reset)
  11395. "Function for `next-error-function' to find sparse tree matches.
  11396. N is the number of matches to move, when negative move backwards.
  11397. RESET is entirely ignored - this function always goes back to the
  11398. starting point when no match is found."
  11399. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11400. (search-func (if (< n 0)
  11401. 'previous-single-char-property-change
  11402. 'next-single-char-property-change))
  11403. (n (abs n))
  11404. (pos (point))
  11405. p1)
  11406. (catch 'exit
  11407. (while (setq p1 (funcall search-func (point) 'org-type))
  11408. (when (equal p1 limit)
  11409. (goto-char pos)
  11410. (error "No more matches"))
  11411. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11412. (setq n (1- n))
  11413. (when (= n 0)
  11414. (goto-char p1)
  11415. (throw 'exit (point))))
  11416. (goto-char p1))
  11417. (goto-char p1)
  11418. (error "No more matches"))))
  11419. (defun org-show-context (&optional key)
  11420. "Make sure point and context are visible.
  11421. How much context is shown depends upon the variables
  11422. `org-show-hierarchy-above', `org-show-following-heading',
  11423. `org-show-entry-below' and `org-show-siblings'."
  11424. (let ((heading-p (org-at-heading-p t))
  11425. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11426. (following-p (org-get-alist-option org-show-following-heading key))
  11427. (entry-p (org-get-alist-option org-show-entry-below key))
  11428. (siblings-p (org-get-alist-option org-show-siblings key)))
  11429. (catch 'exit
  11430. ;; Show heading or entry text
  11431. (if (and heading-p (not entry-p))
  11432. (org-flag-heading nil) ; only show the heading
  11433. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11434. (org-show-hidden-entry))) ; show entire entry
  11435. (when following-p
  11436. ;; Show next sibling, or heading below text
  11437. (save-excursion
  11438. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11439. (org-flag-heading nil))))
  11440. (when siblings-p (org-show-siblings))
  11441. (when hierarchy-p
  11442. ;; show all higher headings, possibly with siblings
  11443. (save-excursion
  11444. (while (and (condition-case nil
  11445. (progn (org-up-heading-all 1) t)
  11446. (error nil))
  11447. (not (bobp)))
  11448. (org-flag-heading nil)
  11449. (when siblings-p (org-show-siblings))))))))
  11450. (defvar org-reveal-start-hook nil
  11451. "Hook run before revealing a location.")
  11452. (defun org-reveal (&optional siblings)
  11453. "Show current entry, hierarchy above it, and the following headline.
  11454. This can be used to show a consistent set of context around locations
  11455. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11456. not t for the search context.
  11457. With optional argument SIBLINGS, on each level of the hierarchy all
  11458. siblings are shown. This repairs the tree structure to what it would
  11459. look like when opened with hierarchical calls to `org-cycle'.
  11460. With double optional argument \\[universal-argument] \\[universal-argument], \
  11461. go to the parent and show the
  11462. entire tree."
  11463. (interactive "P")
  11464. (run-hooks 'org-reveal-start-hook)
  11465. (let ((org-show-hierarchy-above t)
  11466. (org-show-following-heading t)
  11467. (org-show-siblings (if siblings t org-show-siblings)))
  11468. (org-show-context nil))
  11469. (when (equal siblings '(16))
  11470. (save-excursion
  11471. (when (org-up-heading-safe)
  11472. (org-show-subtree)
  11473. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11474. (defun org-highlight-new-match (beg end)
  11475. "Highlight from BEG to END and mark the highlight is an occur headline."
  11476. (let ((ov (make-overlay beg end)))
  11477. (overlay-put ov 'face 'secondary-selection)
  11478. (overlay-put ov 'org-type 'org-occur)
  11479. (push ov org-occur-highlights)))
  11480. (defun org-remove-occur-highlights (&optional beg end noremove)
  11481. "Remove the occur highlights from the buffer.
  11482. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11483. from the `before-change-functions' in the current buffer."
  11484. (interactive)
  11485. (unless org-inhibit-highlight-removal
  11486. (mapc 'delete-overlay org-occur-highlights)
  11487. (setq org-occur-highlights nil)
  11488. (setq org-occur-parameters nil)
  11489. (unless noremove
  11490. (remove-hook 'before-change-functions
  11491. 'org-remove-occur-highlights 'local))))
  11492. ;;;; Priorities
  11493. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11494. "Regular expression matching the priority indicator.")
  11495. (defvar org-remove-priority-next-time nil)
  11496. (defun org-priority-up ()
  11497. "Increase the priority of the current item."
  11498. (interactive)
  11499. (org-priority 'up))
  11500. (defun org-priority-down ()
  11501. "Decrease the priority of the current item."
  11502. (interactive)
  11503. (org-priority 'down))
  11504. (defun org-priority (&optional action)
  11505. "Change the priority of an item by ARG.
  11506. ACTION can be `set', `up', `down', or a character."
  11507. (interactive)
  11508. (unless org-enable-priority-commands
  11509. (error "Priority commands are disabled"))
  11510. (setq action (or action 'set))
  11511. (let (current new news have remove)
  11512. (save-excursion
  11513. (org-back-to-heading t)
  11514. (if (looking-at org-priority-regexp)
  11515. (setq current (string-to-char (match-string 2))
  11516. have t))
  11517. (cond
  11518. ((eq action 'remove)
  11519. (setq remove t new ?\ ))
  11520. ((or (eq action 'set)
  11521. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11522. (if (not (eq action 'set))
  11523. (setq new action)
  11524. (message "Priority %c-%c, SPC to remove: "
  11525. org-highest-priority org-lowest-priority)
  11526. (save-match-data
  11527. (setq new (read-char-exclusive))))
  11528. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11529. (= (upcase org-lowest-priority) org-lowest-priority))
  11530. (setq new (upcase new)))
  11531. (cond ((equal new ?\ ) (setq remove t))
  11532. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11533. (error "Priority must be between `%c' and `%c'"
  11534. org-highest-priority org-lowest-priority))))
  11535. ((eq action 'up)
  11536. (setq new (if have
  11537. (1- current) ; normal cycling
  11538. ;; last priority was empty
  11539. (if (eq last-command this-command)
  11540. org-lowest-priority ; wrap around empty to lowest
  11541. ;; default
  11542. (if org-priority-start-cycle-with-default
  11543. org-default-priority
  11544. (1- org-default-priority))))))
  11545. ((eq action 'down)
  11546. (setq new (if have
  11547. (1+ current) ; normal cycling
  11548. ;; last priority was empty
  11549. (if (eq last-command this-command)
  11550. org-highest-priority ; wrap around empty to highest
  11551. ;; default
  11552. (if org-priority-start-cycle-with-default
  11553. org-default-priority
  11554. (1+ org-default-priority))))))
  11555. (t (error "Invalid action")))
  11556. (if (or (< (upcase new) org-highest-priority)
  11557. (> (upcase new) org-lowest-priority))
  11558. (if (and (memq action '(up down))
  11559. (not have) (not (eq last-command this-command)))
  11560. ;; `new' is from default priority
  11561. (error
  11562. "The default can not be set, see `org-default-priority' why")
  11563. ;; normal cycling: `new' is beyond highest/lowest priority
  11564. ;; and is wrapped around to the empty priority
  11565. (setq remove t)))
  11566. (setq news (format "%c" new))
  11567. (if have
  11568. (if remove
  11569. (replace-match "" t t nil 1)
  11570. (replace-match news t t nil 2))
  11571. (if remove
  11572. (error "No priority cookie found in line")
  11573. (let ((case-fold-search nil))
  11574. (looking-at org-todo-line-regexp))
  11575. (if (match-end 2)
  11576. (progn
  11577. (goto-char (match-end 2))
  11578. (insert " [#" news "]"))
  11579. (goto-char (match-beginning 3))
  11580. (insert "[#" news "] "))))
  11581. (org-preserve-lc (org-set-tags nil 'align)))
  11582. (if remove
  11583. (message "Priority removed")
  11584. (message "Priority of current item set to %s" news))))
  11585. (defun org-get-priority (s)
  11586. "Find priority cookie and return priority."
  11587. (if (functionp org-get-priority-function)
  11588. (funcall org-get-priority-function)
  11589. (save-match-data
  11590. (if (not (string-match org-priority-regexp s))
  11591. (* 1000 (- org-lowest-priority org-default-priority))
  11592. (* 1000 (- org-lowest-priority
  11593. (string-to-char (match-string 2 s))))))))
  11594. ;;;; Tags
  11595. (defvar org-agenda-archives-mode)
  11596. (defvar org-map-continue-from nil
  11597. "Position from where mapping should continue.
  11598. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11599. (defvar org-scanner-tags nil
  11600. "The current tag list while the tags scanner is running.")
  11601. (defvar org-trust-scanner-tags nil
  11602. "Should `org-get-tags-at' use the tags for the scanner.
  11603. This is for internal dynamical scoping only.
  11604. When this is non-nil, the function `org-get-tags-at' will return the value
  11605. of `org-scanner-tags' instead of building the list by itself. This
  11606. can lead to large speed-ups when the tags scanner is used in a file with
  11607. many entries, and when the list of tags is retrieved, for example to
  11608. obtain a list of properties. Building the tags list for each entry in such
  11609. a file becomes an N^2 operation - but with this variable set, it scales
  11610. as N.")
  11611. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11612. "Scan headline tags with inheritance and produce output ACTION.
  11613. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11614. or `agenda' to produce an entry list for an agenda view. It can also be
  11615. a Lisp form or a function that should be called at each matched headline, in
  11616. this case the return value is a list of all return values from these calls.
  11617. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11618. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11619. only lines with a not-done TODO keyword are included in the output.
  11620. This should be the same variable that was scoped into
  11621. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11622. START-LEVEL can be a string with asterisks, reducing the scope to
  11623. headlines matching this string."
  11624. (require 'org-agenda)
  11625. (let* ((re (concat "^"
  11626. (if start-level
  11627. ;; Get the correct level to match
  11628. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11629. org-outline-regexp)
  11630. " *\\(\\<\\("
  11631. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11632. (org-re
  11633. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11634. (props (list 'face 'default
  11635. 'done-face 'org-agenda-done
  11636. 'undone-face 'default
  11637. 'mouse-face 'highlight
  11638. 'org-not-done-regexp org-not-done-regexp
  11639. 'org-todo-regexp org-todo-regexp
  11640. 'org-complex-heading-regexp org-complex-heading-regexp
  11641. 'help-echo
  11642. (format "mouse-2 or RET jump to org file %s"
  11643. (abbreviate-file-name
  11644. (or (buffer-file-name (buffer-base-buffer))
  11645. (buffer-name (buffer-base-buffer)))))))
  11646. (case-fold-search nil)
  11647. (org-map-continue-from nil)
  11648. lspos tags tags-list
  11649. (tags-alist (list (cons 0 org-file-tags)))
  11650. (llast 0) rtn rtn1 level category i txt
  11651. todo marker entry priority)
  11652. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11653. (setq action (list 'lambda nil action)))
  11654. (save-excursion
  11655. (goto-char (point-min))
  11656. (when (eq action 'sparse-tree)
  11657. (org-overview)
  11658. (org-remove-occur-highlights))
  11659. (while (re-search-forward re nil t)
  11660. (setq org-map-continue-from nil)
  11661. (catch :skip
  11662. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11663. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11664. (goto-char (setq lspos (match-beginning 0)))
  11665. (setq level (org-reduced-level (funcall outline-level))
  11666. category (org-get-category))
  11667. (setq i llast llast level)
  11668. ;; remove tag lists from same and sublevels
  11669. (while (>= i level)
  11670. (when (setq entry (assoc i tags-alist))
  11671. (setq tags-alist (delete entry tags-alist)))
  11672. (setq i (1- i)))
  11673. ;; add the next tags
  11674. (when tags
  11675. (setq tags (org-split-string tags ":")
  11676. tags-alist
  11677. (cons (cons level tags) tags-alist)))
  11678. ;; compile tags for current headline
  11679. (setq tags-list
  11680. (if org-use-tag-inheritance
  11681. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11682. tags)
  11683. org-scanner-tags tags-list)
  11684. (when org-use-tag-inheritance
  11685. (setcdr (car tags-alist)
  11686. (mapcar (lambda (x)
  11687. (setq x (copy-sequence x))
  11688. (org-add-prop-inherited x))
  11689. (cdar tags-alist))))
  11690. (when (and tags org-use-tag-inheritance
  11691. (or (not (eq t org-use-tag-inheritance))
  11692. org-tags-exclude-from-inheritance))
  11693. ;; selective inheritance, remove uninherited ones
  11694. (setcdr (car tags-alist)
  11695. (org-remove-uninherited-tags (cdar tags-alist))))
  11696. (when (and
  11697. ;; eval matcher only when the todo condition is OK
  11698. (and (or (not todo-only) (member todo org-not-done-keywords))
  11699. (let ((case-fold-search t)) (eval matcher)))
  11700. ;; Call the skipper, but return t if it does not skip,
  11701. ;; so that the `and' form continues evaluating
  11702. (progn
  11703. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11704. t)
  11705. ;; Check if timestamps are deselecting this entry
  11706. (or (not todo-only)
  11707. (and (member todo org-not-done-keywords)
  11708. (or (not org-agenda-tags-todo-honor-ignore-options)
  11709. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11710. ;; Extra check for the archive tag
  11711. ;; FIXME: Does the skipper already do this????
  11712. (or
  11713. (not (member org-archive-tag tags-list))
  11714. ;; we have an archive tag, should we use this anyway?
  11715. (or (not org-agenda-skip-archived-trees)
  11716. (and (eq action 'agenda) org-agenda-archives-mode))))
  11717. ;; select this headline
  11718. (cond
  11719. ((eq action 'sparse-tree)
  11720. (and org-highlight-sparse-tree-matches
  11721. (org-get-heading) (match-end 0)
  11722. (org-highlight-new-match
  11723. (match-beginning 1) (match-end 1)))
  11724. (org-show-context 'tags-tree))
  11725. ((eq action 'agenda)
  11726. (setq txt (org-agenda-format-item
  11727. ""
  11728. (concat
  11729. (if (eq org-tags-match-list-sublevels 'indented)
  11730. (make-string (1- level) ?.) "")
  11731. (org-get-heading))
  11732. category
  11733. tags-list
  11734. )
  11735. priority (org-get-priority txt))
  11736. (goto-char lspos)
  11737. (setq marker (org-agenda-new-marker))
  11738. (org-add-props txt props
  11739. 'org-marker marker 'org-hd-marker marker 'org-category category
  11740. 'todo-state todo
  11741. 'priority priority 'type "tagsmatch")
  11742. (push txt rtn))
  11743. ((functionp action)
  11744. (setq org-map-continue-from nil)
  11745. (save-excursion
  11746. (setq rtn1 (funcall action))
  11747. (push rtn1 rtn)))
  11748. (t (error "Invalid action")))
  11749. ;; if we are to skip sublevels, jump to end of subtree
  11750. (unless org-tags-match-list-sublevels
  11751. (org-end-of-subtree t)
  11752. (backward-char 1))))
  11753. ;; Get the correct position from where to continue
  11754. (if org-map-continue-from
  11755. (goto-char org-map-continue-from)
  11756. (and (= (point) lspos) (end-of-line 1)))))
  11757. (when (and (eq action 'sparse-tree)
  11758. (not org-sparse-tree-open-archived-trees))
  11759. (org-hide-archived-subtrees (point-min) (point-max)))
  11760. (nreverse rtn)))
  11761. (defun org-remove-uninherited-tags (tags)
  11762. "Remove all tags that are not inherited from the list TAGS."
  11763. (cond
  11764. ((eq org-use-tag-inheritance t)
  11765. (if org-tags-exclude-from-inheritance
  11766. (org-delete-all org-tags-exclude-from-inheritance tags)
  11767. tags))
  11768. ((not org-use-tag-inheritance) nil)
  11769. ((stringp org-use-tag-inheritance)
  11770. (delq nil (mapcar
  11771. (lambda (x)
  11772. (if (and (string-match org-use-tag-inheritance x)
  11773. (not (member x org-tags-exclude-from-inheritance)))
  11774. x nil))
  11775. tags)))
  11776. ((listp org-use-tag-inheritance)
  11777. (delq nil (mapcar
  11778. (lambda (x)
  11779. (if (member x org-use-tag-inheritance) x nil))
  11780. tags)))))
  11781. (defun org-match-sparse-tree (&optional todo-only match)
  11782. "Create a sparse tree according to tags string MATCH.
  11783. MATCH can contain positive and negative selection of tags, like
  11784. \"+WORK+URGENT-WITHBOSS\".
  11785. If optional argument TODO-ONLY is non-nil, only select lines that are
  11786. also TODO lines."
  11787. (interactive "P")
  11788. (org-prepare-agenda-buffers (list (current-buffer)))
  11789. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11790. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11791. (defvar org-cached-props nil)
  11792. (defun org-cached-entry-get (pom property)
  11793. (if (or (eq t org-use-property-inheritance)
  11794. (and (stringp org-use-property-inheritance)
  11795. (string-match org-use-property-inheritance property))
  11796. (and (listp org-use-property-inheritance)
  11797. (member property org-use-property-inheritance)))
  11798. ;; Caching is not possible, check it directly
  11799. (org-entry-get pom property 'inherit)
  11800. ;; Get all properties, so that we can do complicated checks easily
  11801. (cdr (assoc property (or org-cached-props
  11802. (setq org-cached-props
  11803. (org-entry-properties pom)))))))
  11804. (defun org-global-tags-completion-table (&optional files)
  11805. "Return the list of all tags in all agenda buffer/files.
  11806. Optional FILES argument is a list of files to which can be used
  11807. instead of the agenda files."
  11808. (save-excursion
  11809. (org-uniquify
  11810. (delq nil
  11811. (apply 'append
  11812. (mapcar
  11813. (lambda (file)
  11814. (set-buffer (find-file-noselect file))
  11815. (append (org-get-buffer-tags)
  11816. (mapcar (lambda (x) (if (stringp (car-safe x))
  11817. (list (car-safe x)) nil))
  11818. org-tag-alist)))
  11819. (if (and files (car files))
  11820. files
  11821. (org-agenda-files))))))))
  11822. (defun org-make-tags-matcher (match)
  11823. "Create the TAGS/TODO matcher form for the selection string MATCH.
  11824. The variable `todo-only' is scoped dynamically into this function; it will be
  11825. set to t if the matcher restricts matching to TODO entries,
  11826. otherwise will not be touched.
  11827. Returns a cons of the selection string MATCH and the constructed
  11828. lisp form implementing the matcher. The matcher is to be
  11829. evaluated at an Org entry, with point on the headline,
  11830. and returns t if the entry matches the
  11831. selection string MATCH. The returned lisp form references
  11832. two variables with information about the entry, which must be
  11833. bound around the form's evaluation: todo, the TODO keyword at the
  11834. entry (or nil of none); and tags-list, the list of all tags at the
  11835. entry including inherited ones. Additionally, the category
  11836. of the entry (if any) must be specified as the text property
  11837. 'org-category on the headline.
  11838. See also `org-scan-tags'.
  11839. "
  11840. (declare (special todo-only))
  11841. (unless (boundp 'todo-only)
  11842. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  11843. (unless match
  11844. ;; Get a new match request, with completion
  11845. (let ((org-last-tags-completion-table
  11846. (org-global-tags-completion-table)))
  11847. (setq match (org-completing-read-no-i
  11848. "Match: " 'org-tags-completion-function nil nil nil
  11849. 'org-tags-history))))
  11850. ;; Parse the string and create a lisp form
  11851. (let ((match0 match)
  11852. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11853. minus tag mm
  11854. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11855. orterms term orlist re-p str-p level-p level-op time-p
  11856. prop-p pn pv po gv rest)
  11857. (if (string-match "/+" match)
  11858. ;; match contains also a todo-matching request
  11859. (progn
  11860. (setq tagsmatch (substring match 0 (match-beginning 0))
  11861. todomatch (substring match (match-end 0)))
  11862. (if (string-match "^!" todomatch)
  11863. (setq todo-only t todomatch (substring todomatch 1)))
  11864. (if (string-match "^\\s-*$" todomatch)
  11865. (setq todomatch nil)))
  11866. ;; only matching tags
  11867. (setq tagsmatch match todomatch nil))
  11868. ;; Make the tags matcher
  11869. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11870. (setq tagsmatcher t)
  11871. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11872. (while (setq term (pop orterms))
  11873. (while (and (equal (substring term -1) "\\") orterms)
  11874. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11875. (while (string-match re term)
  11876. (setq rest (substring term (match-end 0))
  11877. minus (and (match-end 1)
  11878. (equal (match-string 1 term) "-"))
  11879. tag (save-match-data (replace-regexp-in-string
  11880. "\\\\-" "-"
  11881. (match-string 2 term)))
  11882. re-p (equal (string-to-char tag) ?{)
  11883. level-p (match-end 4)
  11884. prop-p (match-end 5)
  11885. mm (cond
  11886. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11887. (level-p
  11888. (setq level-op (org-op-to-function (match-string 3 term)))
  11889. `(,level-op level ,(string-to-number
  11890. (match-string 4 term))))
  11891. (prop-p
  11892. (setq pn (match-string 5 term)
  11893. po (match-string 6 term)
  11894. pv (match-string 7 term)
  11895. re-p (equal (string-to-char pv) ?{)
  11896. str-p (equal (string-to-char pv) ?\")
  11897. time-p (save-match-data
  11898. (string-match "^\"[[<].*[]>]\"$" pv))
  11899. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11900. (if time-p (setq pv (org-matcher-time pv)))
  11901. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11902. (cond
  11903. ((equal pn "CATEGORY")
  11904. (setq gv '(get-text-property (point) 'org-category)))
  11905. ((equal pn "TODO")
  11906. (setq gv 'todo))
  11907. (t
  11908. (setq gv `(org-cached-entry-get nil ,pn))))
  11909. (if re-p
  11910. (if (eq po 'org<>)
  11911. `(not (string-match ,pv (or ,gv "")))
  11912. `(string-match ,pv (or ,gv "")))
  11913. (if str-p
  11914. `(,po (or ,gv "") ,pv)
  11915. `(,po (string-to-number (or ,gv ""))
  11916. ,(string-to-number pv) ))))
  11917. (t `(member ,tag tags-list)))
  11918. mm (if minus (list 'not mm) mm)
  11919. term rest)
  11920. (push mm tagsmatcher))
  11921. (push (if (> (length tagsmatcher) 1)
  11922. (cons 'and tagsmatcher)
  11923. (car tagsmatcher))
  11924. orlist)
  11925. (setq tagsmatcher nil))
  11926. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11927. (setq tagsmatcher
  11928. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11929. ;; Make the todo matcher
  11930. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11931. (setq todomatcher t)
  11932. (setq orterms (org-split-string todomatch "|") orlist nil)
  11933. (while (setq term (pop orterms))
  11934. (while (string-match re term)
  11935. (setq minus (and (match-end 1)
  11936. (equal (match-string 1 term) "-"))
  11937. kwd (match-string 2 term)
  11938. re-p (equal (string-to-char kwd) ?{)
  11939. term (substring term (match-end 0))
  11940. mm (if re-p
  11941. `(string-match ,(substring kwd 1 -1) todo)
  11942. (list 'equal 'todo kwd))
  11943. mm (if minus (list 'not mm) mm))
  11944. (push mm todomatcher))
  11945. (push (if (> (length todomatcher) 1)
  11946. (cons 'and todomatcher)
  11947. (car todomatcher))
  11948. orlist)
  11949. (setq todomatcher nil))
  11950. (setq todomatcher (if (> (length orlist) 1)
  11951. (cons 'or orlist) (car orlist))))
  11952. ;; Return the string and lisp forms of the matcher
  11953. (setq matcher (if todomatcher
  11954. (list 'and tagsmatcher todomatcher)
  11955. tagsmatcher))
  11956. (when todo-only
  11957. (setq matcher (list 'and '(member todo org-not-done-keywords)
  11958. matcher)))
  11959. (cons match0 matcher)))
  11960. (defun org-op-to-function (op &optional stringp)
  11961. "Turn an operator into the appropriate function."
  11962. (setq op
  11963. (cond
  11964. ((equal op "<" ) '(< string< org-time<))
  11965. ((equal op ">" ) '(> org-string> org-time>))
  11966. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11967. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11968. ((member op '("=" "==")) '(= string= org-time=))
  11969. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11970. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11971. (defun org<> (a b) (not (= a b)))
  11972. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11973. (defun org-string>= (a b) (not (string< a b)))
  11974. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11975. (defun org-string<> (a b) (not (string= a b)))
  11976. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11977. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11978. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11979. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11980. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11981. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11982. (defun org-2ft (s)
  11983. "Convert S to a floating point time.
  11984. If S is already a number, just return it. If it is a string, parse
  11985. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11986. (cond
  11987. ((numberp s) s)
  11988. ((stringp s)
  11989. (condition-case nil
  11990. (float-time (apply 'encode-time (org-parse-time-string s)))
  11991. (error 0.)))
  11992. (t 0.)))
  11993. (defun org-time-today ()
  11994. "Time in seconds today at 0:00.
  11995. Returns the float number of seconds since the beginning of the
  11996. epoch to the beginning of today (00:00)."
  11997. (float-time (apply 'encode-time
  11998. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11999. (defun org-matcher-time (s)
  12000. "Interpret a time comparison value."
  12001. (save-match-data
  12002. (cond
  12003. ((string= s "<now>") (float-time))
  12004. ((string= s "<today>") (org-time-today))
  12005. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12006. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12007. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  12008. (+ (org-time-today)
  12009. (* (string-to-number (match-string 1 s))
  12010. (cdr (assoc (match-string 2 s)
  12011. '(("d" . 86400.0) ("w" . 604800.0)
  12012. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12013. (t (org-2ft s)))))
  12014. (defun org-match-any-p (re list)
  12015. "Does re match any element of list?"
  12016. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12017. (delq nil list))
  12018. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12019. (defvar org-tags-overlay (make-overlay 1 1))
  12020. (org-detach-overlay org-tags-overlay)
  12021. (defun org-get-local-tags-at (&optional pos)
  12022. "Get a list of tags defined in the current headline."
  12023. (org-get-tags-at pos 'local))
  12024. (defun org-get-local-tags ()
  12025. "Get a list of tags defined in the current headline."
  12026. (org-get-tags-at nil 'local))
  12027. (defun org-get-tags-at (&optional pos local)
  12028. "Get a list of all headline tags applicable at POS.
  12029. POS defaults to point. If tags are inherited, the list contains
  12030. the targets in the same sequence as the headlines appear, i.e.
  12031. the tags of the current headline come last.
  12032. When LOCAL is non-nil, only return tags from the current headline,
  12033. ignore inherited ones."
  12034. (interactive)
  12035. (if (and org-trust-scanner-tags
  12036. (or (not pos) (equal pos (point)))
  12037. (not local))
  12038. org-scanner-tags
  12039. (let (tags ltags lastpos parent)
  12040. (save-excursion
  12041. (save-restriction
  12042. (widen)
  12043. (goto-char (or pos (point)))
  12044. (save-match-data
  12045. (catch 'done
  12046. (condition-case nil
  12047. (progn
  12048. (org-back-to-heading t)
  12049. (while (not (equal lastpos (point)))
  12050. (setq lastpos (point))
  12051. (when (looking-at
  12052. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12053. (setq ltags (org-split-string
  12054. (org-match-string-no-properties 1) ":"))
  12055. (when parent
  12056. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12057. (setq tags (append
  12058. (if parent
  12059. (org-remove-uninherited-tags ltags)
  12060. ltags)
  12061. tags)))
  12062. (or org-use-tag-inheritance (throw 'done t))
  12063. (if local (throw 'done t))
  12064. (or (org-up-heading-safe) (error nil))
  12065. (setq parent t)))
  12066. (error nil)))))
  12067. (if local
  12068. tags
  12069. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12070. (defun org-add-prop-inherited (s)
  12071. (add-text-properties 0 (length s) '(inherited t) s)
  12072. s)
  12073. (defun org-toggle-tag (tag &optional onoff)
  12074. "Toggle the tag TAG for the current line.
  12075. If ONOFF is `on' or `off', don't toggle but set to this state."
  12076. (let (res current)
  12077. (save-excursion
  12078. (org-back-to-heading t)
  12079. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12080. (point-at-eol) t)
  12081. (progn
  12082. (setq current (match-string 1))
  12083. (replace-match ""))
  12084. (setq current ""))
  12085. (setq current (nreverse (org-split-string current ":")))
  12086. (cond
  12087. ((eq onoff 'on)
  12088. (setq res t)
  12089. (or (member tag current) (push tag current)))
  12090. ((eq onoff 'off)
  12091. (or (not (member tag current)) (setq current (delete tag current))))
  12092. (t (if (member tag current)
  12093. (setq current (delete tag current))
  12094. (setq res t)
  12095. (push tag current))))
  12096. (end-of-line 1)
  12097. (if current
  12098. (progn
  12099. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12100. (org-set-tags nil t))
  12101. (delete-horizontal-space))
  12102. (run-hooks 'org-after-tags-change-hook))
  12103. res))
  12104. (defun org-align-tags-here (to-col)
  12105. ;; Assumes that this is a headline
  12106. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12107. (beginning-of-line 1)
  12108. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12109. (< pos (match-beginning 2)))
  12110. (progn
  12111. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12112. (goto-char (match-beginning 1))
  12113. (insert " ")
  12114. (delete-region (point) (1+ (match-beginning 2)))
  12115. (setq ncol (max (current-column)
  12116. (1+ col)
  12117. (if (> to-col 0)
  12118. to-col
  12119. (- (abs to-col) tags-l))))
  12120. (setq p (point))
  12121. (insert (make-string (- ncol (current-column)) ?\ ))
  12122. (setq ncol (current-column))
  12123. (when indent-tabs-mode (tabify p (point-at-eol)))
  12124. (org-move-to-column (min ncol col) t))
  12125. (goto-char pos))))
  12126. (defun org-set-tags-command (&optional arg just-align)
  12127. "Call the set-tags command for the current entry."
  12128. (interactive "P")
  12129. (if (org-at-heading-p)
  12130. (org-set-tags arg just-align)
  12131. (save-excursion
  12132. (org-back-to-heading t)
  12133. (org-set-tags arg just-align))))
  12134. (defun org-set-tags-to (data)
  12135. "Set the tags of the current entry to DATA, replacing the current tags.
  12136. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12137. If DATA is nil or the empty string, any tags will be removed."
  12138. (interactive "sTags: ")
  12139. (setq data
  12140. (cond
  12141. ((eq data nil) "")
  12142. ((equal data "") "")
  12143. ((stringp data)
  12144. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12145. ":"))
  12146. ((listp data)
  12147. (concat ":" (mapconcat 'identity data ":") ":"))
  12148. (t nil)))
  12149. (when data
  12150. (save-excursion
  12151. (org-back-to-heading t)
  12152. (when (looking-at org-complex-heading-regexp)
  12153. (if (match-end 5)
  12154. (progn
  12155. (goto-char (match-beginning 5))
  12156. (insert data)
  12157. (delete-region (point) (point-at-eol))
  12158. (org-set-tags nil 'align))
  12159. (goto-char (point-at-eol))
  12160. (insert " " data)
  12161. (org-set-tags nil 'align)))
  12162. (beginning-of-line 1)
  12163. (if (looking-at ".*?\\([ \t]+\\)$")
  12164. (delete-region (match-beginning 1) (match-end 1))))))
  12165. (defun org-align-all-tags ()
  12166. "Align the tags i all headings."
  12167. (interactive)
  12168. (save-excursion
  12169. (or (ignore-errors (org-back-to-heading t))
  12170. (outline-next-heading))
  12171. (if (org-at-heading-p)
  12172. (org-set-tags t)
  12173. (message "No headings"))))
  12174. (defvar org-indent-indentation-per-level)
  12175. (defun org-set-tags (&optional arg just-align)
  12176. "Set the tags for the current headline.
  12177. With prefix ARG, realign all tags in headings in the current buffer."
  12178. (interactive "P")
  12179. (let* ((re org-outline-regexp-bol)
  12180. (current (org-get-tags-string))
  12181. (col (current-column))
  12182. (org-setting-tags t)
  12183. table current-tags inherited-tags ; computed below when needed
  12184. tags p0 c0 c1 rpl di tc level)
  12185. (if arg
  12186. (save-excursion
  12187. (goto-char (point-min))
  12188. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12189. (while (re-search-forward re nil t)
  12190. (org-set-tags nil t)
  12191. (end-of-line 1)))
  12192. (message "All tags realigned to column %d" org-tags-column))
  12193. (if just-align
  12194. (setq tags current)
  12195. ;; Get a new set of tags from the user
  12196. (save-excursion
  12197. (setq table (append org-tag-persistent-alist
  12198. (or org-tag-alist (org-get-buffer-tags))
  12199. (and
  12200. org-complete-tags-always-offer-all-agenda-tags
  12201. (org-global-tags-completion-table
  12202. (org-agenda-files))))
  12203. org-last-tags-completion-table table
  12204. current-tags (org-split-string current ":")
  12205. inherited-tags (nreverse
  12206. (nthcdr (length current-tags)
  12207. (nreverse (org-get-tags-at))))
  12208. tags
  12209. (if (or (eq t org-use-fast-tag-selection)
  12210. (and org-use-fast-tag-selection
  12211. (delq nil (mapcar 'cdr table))))
  12212. (org-fast-tag-selection
  12213. current-tags inherited-tags table
  12214. (if org-fast-tag-selection-include-todo
  12215. org-todo-key-alist))
  12216. (let ((org-add-colon-after-tag-completion t))
  12217. (org-trim
  12218. (org-icompleting-read "Tags: "
  12219. 'org-tags-completion-function
  12220. nil nil current 'org-tags-history))))))
  12221. (while (string-match "[-+&]+" tags)
  12222. ;; No boolean logic, just a list
  12223. (setq tags (replace-match ":" t t tags))))
  12224. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12225. (if org-tags-sort-function
  12226. (setq tags (mapconcat 'identity
  12227. (sort (org-split-string
  12228. tags (org-re "[^[:alnum:]_@#%]+"))
  12229. org-tags-sort-function) ":")))
  12230. (if (string-match "\\`[\t ]*\\'" tags)
  12231. (setq tags "")
  12232. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12233. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12234. ;; Insert new tags at the correct column
  12235. (beginning-of-line 1)
  12236. (setq level (or (and (looking-at org-outline-regexp)
  12237. (- (match-end 0) (point) 1))
  12238. 1))
  12239. (cond
  12240. ((and (equal current "") (equal tags "")))
  12241. ((re-search-forward
  12242. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12243. (point-at-eol) t)
  12244. (if (equal tags "")
  12245. (setq rpl "")
  12246. (goto-char (match-beginning 0))
  12247. (setq c0 (current-column)
  12248. ;; compute offset for the case of org-indent-mode active
  12249. di (if org-indent-mode
  12250. (* (1- org-indent-indentation-per-level) (1- level))
  12251. 0)
  12252. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12253. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12254. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12255. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12256. (replace-match rpl t t)
  12257. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12258. tags)
  12259. (t (error "Tags alignment failed")))
  12260. (org-move-to-column col)
  12261. (unless just-align
  12262. (run-hooks 'org-after-tags-change-hook)))))
  12263. (defun org-change-tag-in-region (beg end tag off)
  12264. "Add or remove TAG for each entry in the region.
  12265. This works in the agenda, and also in an org-mode buffer."
  12266. (interactive
  12267. (list (region-beginning) (region-end)
  12268. (let ((org-last-tags-completion-table
  12269. (if (eq major-mode 'org-mode)
  12270. (org-get-buffer-tags)
  12271. (org-global-tags-completion-table))))
  12272. (org-icompleting-read
  12273. "Tag: " 'org-tags-completion-function nil nil nil
  12274. 'org-tags-history))
  12275. (progn
  12276. (message "[s]et or [r]emove? ")
  12277. (equal (read-char-exclusive) ?r))))
  12278. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12279. (let ((agendap (equal major-mode 'org-agenda-mode))
  12280. l1 l2 m buf pos newhead (cnt 0))
  12281. (goto-char end)
  12282. (setq l2 (1- (org-current-line)))
  12283. (goto-char beg)
  12284. (setq l1 (org-current-line))
  12285. (loop for l from l1 to l2 do
  12286. (org-goto-line l)
  12287. (setq m (get-text-property (point) 'org-hd-marker))
  12288. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12289. (and agendap m))
  12290. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12291. pos (if agendap m (point)))
  12292. (with-current-buffer buf
  12293. (save-excursion
  12294. (save-restriction
  12295. (goto-char pos)
  12296. (setq cnt (1+ cnt))
  12297. (org-toggle-tag tag (if off 'off 'on))
  12298. (setq newhead (org-get-heading)))))
  12299. (and agendap (org-agenda-change-all-lines newhead m))))
  12300. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12301. (defun org-tags-completion-function (string predicate &optional flag)
  12302. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12303. (confirm (lambda (x) (stringp (car x)))))
  12304. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12305. (setq s1 (match-string 1 string)
  12306. s2 (match-string 2 string))
  12307. (setq s1 "" s2 string))
  12308. (cond
  12309. ((eq flag nil)
  12310. ;; try completion
  12311. (setq rtn (try-completion s2 ctable confirm))
  12312. (if (stringp rtn)
  12313. (setq rtn
  12314. (concat s1 s2 (substring rtn (length s2))
  12315. (if (and org-add-colon-after-tag-completion
  12316. (assoc rtn ctable))
  12317. ":" ""))))
  12318. rtn)
  12319. ((eq flag t)
  12320. ;; all-completions
  12321. (all-completions s2 ctable confirm)
  12322. )
  12323. ((eq flag 'lambda)
  12324. ;; exact match?
  12325. (assoc s2 ctable)))
  12326. ))
  12327. (defun org-fast-tag-insert (kwd tags face &optional end)
  12328. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12329. (insert (format "%-12s" (concat kwd ":"))
  12330. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12331. (or end "")))
  12332. (defun org-fast-tag-show-exit (flag)
  12333. (save-excursion
  12334. (org-goto-line 3)
  12335. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12336. (replace-match ""))
  12337. (when flag
  12338. (end-of-line 1)
  12339. (org-move-to-column (- (window-width) 19) t)
  12340. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12341. (defun org-set-current-tags-overlay (current prefix)
  12342. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12343. (if (featurep 'xemacs)
  12344. (org-overlay-display org-tags-overlay (concat prefix s)
  12345. 'secondary-selection)
  12346. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12347. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12348. (defvar org-last-tag-selection-key nil)
  12349. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12350. "Fast tag selection with single keys.
  12351. CURRENT is the current list of tags in the headline, INHERITED is the
  12352. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12353. possibly with grouping information. TODO-TABLE is a similar table with
  12354. TODO keywords, should these have keys assigned to them.
  12355. If the keys are nil, a-z are automatically assigned.
  12356. Returns the new tags string, or nil to not change the current settings."
  12357. (let* ((fulltable (append table todo-table))
  12358. (maxlen (apply 'max (mapcar
  12359. (lambda (x)
  12360. (if (stringp (car x)) (string-width (car x)) 0))
  12361. fulltable)))
  12362. (buf (current-buffer))
  12363. (expert (eq org-fast-tag-selection-single-key 'expert))
  12364. (buffer-tags nil)
  12365. (fwidth (+ maxlen 3 1 3))
  12366. (ncol (/ (- (window-width) 4) fwidth))
  12367. (i-face 'org-done)
  12368. (c-face 'org-todo)
  12369. tg cnt e c char c1 c2 ntable tbl rtn
  12370. ov-start ov-end ov-prefix
  12371. (exit-after-next org-fast-tag-selection-single-key)
  12372. (done-keywords org-done-keywords)
  12373. groups ingroup)
  12374. (save-excursion
  12375. (beginning-of-line 1)
  12376. (if (looking-at
  12377. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12378. (setq ov-start (match-beginning 1)
  12379. ov-end (match-end 1)
  12380. ov-prefix "")
  12381. (setq ov-start (1- (point-at-eol))
  12382. ov-end (1+ ov-start))
  12383. (skip-chars-forward "^\n\r")
  12384. (setq ov-prefix
  12385. (concat
  12386. (buffer-substring (1- (point)) (point))
  12387. (if (> (current-column) org-tags-column)
  12388. " "
  12389. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12390. (move-overlay org-tags-overlay ov-start ov-end)
  12391. (save-window-excursion
  12392. (if expert
  12393. (set-buffer (get-buffer-create " *Org tags*"))
  12394. (delete-other-windows)
  12395. (split-window-vertically)
  12396. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12397. (erase-buffer)
  12398. (org-set-local 'org-done-keywords done-keywords)
  12399. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12400. (org-fast-tag-insert "Current" current c-face "\n\n")
  12401. (org-fast-tag-show-exit exit-after-next)
  12402. (org-set-current-tags-overlay current ov-prefix)
  12403. (setq tbl fulltable char ?a cnt 0)
  12404. (while (setq e (pop tbl))
  12405. (cond
  12406. ((equal (car e) :startgroup)
  12407. (push '() groups) (setq ingroup t)
  12408. (when (not (= cnt 0))
  12409. (setq cnt 0)
  12410. (insert "\n"))
  12411. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12412. ((equal (car e) :endgroup)
  12413. (setq ingroup nil cnt 0)
  12414. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12415. ((equal e '(:newline))
  12416. (when (not (= cnt 0))
  12417. (setq cnt 0)
  12418. (insert "\n")
  12419. (setq e (car tbl))
  12420. (while (equal (car tbl) '(:newline))
  12421. (insert "\n")
  12422. (setq tbl (cdr tbl)))))
  12423. (t
  12424. (setq tg (copy-sequence (car e)) c2 nil)
  12425. (if (cdr e)
  12426. (setq c (cdr e))
  12427. ;; automatically assign a character.
  12428. (setq c1 (string-to-char
  12429. (downcase (substring
  12430. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12431. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12432. (while (or (rassoc char ntable) (rassoc char table))
  12433. (setq char (1+ char)))
  12434. (setq c2 c1))
  12435. (setq c (or c2 char)))
  12436. (if ingroup (push tg (car groups)))
  12437. (setq tg (org-add-props tg nil 'face
  12438. (cond
  12439. ((not (assoc tg table))
  12440. (org-get-todo-face tg))
  12441. ((member tg current) c-face)
  12442. ((member tg inherited) i-face)
  12443. (t nil))))
  12444. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12445. (insert "[" c "] " tg (make-string
  12446. (- fwidth 4 (length tg)) ?\ ))
  12447. (push (cons tg c) ntable)
  12448. (when (= (setq cnt (1+ cnt)) ncol)
  12449. (insert "\n")
  12450. (if ingroup (insert " "))
  12451. (setq cnt 0)))))
  12452. (setq ntable (nreverse ntable))
  12453. (insert "\n")
  12454. (goto-char (point-min))
  12455. (if (not expert) (org-fit-window-to-buffer))
  12456. (setq rtn
  12457. (catch 'exit
  12458. (while t
  12459. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12460. (if (not groups) "no " "")
  12461. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12462. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12463. (setq org-last-tag-selection-key c)
  12464. (cond
  12465. ((= c ?\r) (throw 'exit t))
  12466. ((= c ?!)
  12467. (setq groups (not groups))
  12468. (goto-char (point-min))
  12469. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12470. ((= c ?\C-c)
  12471. (if (not expert)
  12472. (org-fast-tag-show-exit
  12473. (setq exit-after-next (not exit-after-next)))
  12474. (setq expert nil)
  12475. (delete-other-windows)
  12476. (set-window-buffer (split-window-vertically) " *Org tags*")
  12477. (org-switch-to-buffer-other-window " *Org tags*")
  12478. (org-fit-window-to-buffer)))
  12479. ((or (= c ?\C-g)
  12480. (and (= c ?q) (not (rassoc c ntable))))
  12481. (org-detach-overlay org-tags-overlay)
  12482. (setq quit-flag t))
  12483. ((= c ?\ )
  12484. (setq current nil)
  12485. (if exit-after-next (setq exit-after-next 'now)))
  12486. ((= c ?\t)
  12487. (condition-case nil
  12488. (setq tg (org-icompleting-read
  12489. "Tag: "
  12490. (or buffer-tags
  12491. (with-current-buffer buf
  12492. (org-get-buffer-tags)))))
  12493. (quit (setq tg "")))
  12494. (when (string-match "\\S-" tg)
  12495. (add-to-list 'buffer-tags (list tg))
  12496. (if (member tg current)
  12497. (setq current (delete tg current))
  12498. (push tg current)))
  12499. (if exit-after-next (setq exit-after-next 'now)))
  12500. ((setq e (rassoc c todo-table) tg (car e))
  12501. (with-current-buffer buf
  12502. (save-excursion (org-todo tg)))
  12503. (if exit-after-next (setq exit-after-next 'now)))
  12504. ((setq e (rassoc c ntable) tg (car e))
  12505. (if (member tg current)
  12506. (setq current (delete tg current))
  12507. (loop for g in groups do
  12508. (if (member tg g)
  12509. (mapc (lambda (x)
  12510. (setq current (delete x current)))
  12511. g)))
  12512. (push tg current))
  12513. (if exit-after-next (setq exit-after-next 'now))))
  12514. ;; Create a sorted list
  12515. (setq current
  12516. (sort current
  12517. (lambda (a b)
  12518. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12519. (if (eq exit-after-next 'now) (throw 'exit t))
  12520. (goto-char (point-min))
  12521. (beginning-of-line 2)
  12522. (delete-region (point) (point-at-eol))
  12523. (org-fast-tag-insert "Current" current c-face)
  12524. (org-set-current-tags-overlay current ov-prefix)
  12525. (while (re-search-forward
  12526. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12527. (setq tg (match-string 1))
  12528. (add-text-properties
  12529. (match-beginning 1) (match-end 1)
  12530. (list 'face
  12531. (cond
  12532. ((member tg current) c-face)
  12533. ((member tg inherited) i-face)
  12534. (t (get-text-property (match-beginning 1) 'face))))))
  12535. (goto-char (point-min)))))
  12536. (org-detach-overlay org-tags-overlay)
  12537. (if rtn
  12538. (mapconcat 'identity current ":")
  12539. nil))))
  12540. (defun org-get-tags-string ()
  12541. "Get the TAGS string in the current headline."
  12542. (unless (org-at-heading-p t)
  12543. (error "Not on a heading"))
  12544. (save-excursion
  12545. (beginning-of-line 1)
  12546. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12547. (org-match-string-no-properties 1)
  12548. "")))
  12549. (defun org-get-tags ()
  12550. "Get the list of tags specified in the current headline."
  12551. (org-split-string (org-get-tags-string) ":"))
  12552. (defun org-get-buffer-tags ()
  12553. "Get a table of all tags used in the buffer, for completion."
  12554. (let (tags)
  12555. (save-excursion
  12556. (goto-char (point-min))
  12557. (while (re-search-forward
  12558. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12559. (when (equal (char-after (point-at-bol 0)) ?*)
  12560. (mapc (lambda (x) (add-to-list 'tags x))
  12561. (org-split-string (org-match-string-no-properties 1) ":")))))
  12562. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12563. (mapcar 'list tags)))
  12564. ;;;; The mapping API
  12565. ;;;###autoload
  12566. (defun org-map-entries (func &optional match scope &rest skip)
  12567. "Call FUNC at each headline selected by MATCH in SCOPE.
  12568. FUNC is a function or a lisp form. The function will be called without
  12569. arguments, with the cursor positioned at the beginning of the headline.
  12570. The return values of all calls to the function will be collected and
  12571. returned as a list.
  12572. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12573. does not need to preserve point. After evaluation, the cursor will be
  12574. moved to the end of the line (presumably of the headline of the
  12575. processed entry) and search continues from there. Under some
  12576. circumstances, this may not produce the wanted results. For example,
  12577. if you have removed (e.g. archived) the current (sub)tree it could
  12578. mean that the next entry will be skipped entirely. In such cases, you
  12579. can specify the position from where search should continue by making
  12580. FUNC set the variable `org-map-continue-from' to the desired buffer
  12581. position.
  12582. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12583. Only headlines that are matched by this query will be considered during
  12584. the iteration. When MATCH is nil or t, all headlines will be
  12585. visited by the iteration.
  12586. SCOPE determines the scope of this command. It can be any of:
  12587. nil The current buffer, respecting the restriction if any
  12588. tree The subtree started with the entry at point
  12589. region The entries within the active region, if any
  12590. region-start-level
  12591. The entries within the active region, but only those at
  12592. the same level than the first one.
  12593. file The current buffer, without restriction
  12594. file-with-archives
  12595. The current buffer, and any archives associated with it
  12596. agenda All agenda files
  12597. agenda-with-archives
  12598. All agenda files with any archive files associated with them
  12599. \(file1 file2 ...)
  12600. If this is a list, all files in the list will be scanned
  12601. The remaining args are treated as settings for the skipping facilities of
  12602. the scanner. The following items can be given here:
  12603. archive skip trees with the archive tag.
  12604. comment skip trees with the COMMENT keyword
  12605. function or Emacs Lisp form:
  12606. will be used as value for `org-agenda-skip-function', so whenever
  12607. the function returns t, FUNC will not be called for that
  12608. entry and search will continue from the point where the
  12609. function leaves it.
  12610. If your function needs to retrieve the tags including inherited tags
  12611. at the *current* entry, you can use the value of the variable
  12612. `org-scanner-tags' which will be much faster than getting the value
  12613. with `org-get-tags-at'. If your function gets properties with
  12614. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12615. to t around the call to `org-entry-properties' to get the same speedup.
  12616. Note that if your function moves around to retrieve tags and properties at
  12617. a *different* entry, you cannot use these techniques."
  12618. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12619. (not (org-region-active-p)))
  12620. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12621. (org-agenda-skip-archived-trees (memq 'archive skip))
  12622. (org-agenda-skip-comment-trees (memq 'comment skip))
  12623. (org-agenda-skip-function
  12624. (car (org-delete-all '(comment archive) skip)))
  12625. (org-tags-match-list-sublevels t)
  12626. (start-level (eq scope 'region-start-level))
  12627. matcher file res
  12628. org-todo-keywords-for-agenda
  12629. org-done-keywords-for-agenda
  12630. org-todo-keyword-alist-for-agenda
  12631. org-drawers-for-agenda
  12632. org-tag-alist-for-agenda
  12633. todo-only)
  12634. (cond
  12635. ((eq match t) (setq matcher t))
  12636. ((eq match nil) (setq matcher t))
  12637. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12638. (save-excursion
  12639. (save-restriction
  12640. (cond ((eq scope 'tree)
  12641. (org-back-to-heading t)
  12642. (org-narrow-to-subtree)
  12643. (setq scope nil))
  12644. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12645. (org-region-active-p))
  12646. ;; If needed, set start-level to a string like "2"
  12647. (when start-level
  12648. (save-excursion
  12649. (goto-char (region-beginning))
  12650. (unless (org-at-heading-p) (outline-next-heading))
  12651. (setq start-level (org-current-level))))
  12652. (narrow-to-region (region-beginning)
  12653. (save-excursion
  12654. (goto-char (region-end))
  12655. (unless (and (bolp) (org-at-heading-p))
  12656. (outline-next-heading))
  12657. (point)))
  12658. (setq scope nil)))
  12659. (if (not scope)
  12660. (progn
  12661. (org-prepare-agenda-buffers
  12662. (list (buffer-file-name (current-buffer))))
  12663. (setq res (org-scan-tags func matcher todo-only start-level)))
  12664. ;; Get the right scope
  12665. (cond
  12666. ((and scope (listp scope) (symbolp (car scope)))
  12667. (setq scope (eval scope)))
  12668. ((eq scope 'agenda)
  12669. (setq scope (org-agenda-files t)))
  12670. ((eq scope 'agenda-with-archives)
  12671. (setq scope (org-agenda-files t))
  12672. (setq scope (org-add-archive-files scope)))
  12673. ((eq scope 'file)
  12674. (setq scope (list (buffer-file-name))))
  12675. ((eq scope 'file-with-archives)
  12676. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12677. (org-prepare-agenda-buffers scope)
  12678. (while (setq file (pop scope))
  12679. (with-current-buffer (org-find-base-buffer-visiting file)
  12680. (save-excursion
  12681. (save-restriction
  12682. (widen)
  12683. (goto-char (point-min))
  12684. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12685. res)))
  12686. ;;;; Properties
  12687. ;;; Setting and retrieving properties
  12688. (defconst org-special-properties
  12689. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12690. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12691. "The special properties valid in Org-mode.
  12692. These are properties that are not defined in the property drawer,
  12693. but in some other way.")
  12694. (defconst org-default-properties
  12695. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12696. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12697. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12698. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12699. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12700. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12701. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12702. "Some properties that are used by Org-mode for various purposes.
  12703. Being in this list makes sure that they are offered for completion.")
  12704. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12705. "Regular expression matching the first line of a property drawer.")
  12706. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12707. "Regular expression matching the last line of a property drawer.")
  12708. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12709. "Regular expression matching the first line of a property drawer.")
  12710. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12711. "Regular expression matching the first line of a property drawer.")
  12712. (defconst org-property-drawer-re
  12713. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12714. org-property-end-re "\\)\n?")
  12715. "Matches an entire property drawer.")
  12716. (defconst org-clock-drawer-re
  12717. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12718. org-property-end-re "\\)\n?")
  12719. "Matches an entire clock drawer.")
  12720. (defsubst org-re-property (property)
  12721. "Return a regexp matching PROPERTY.
  12722. Match group 1 will be set to the value "
  12723. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12724. (defun org-property-action ()
  12725. "Do an action on properties."
  12726. (interactive)
  12727. (let (c)
  12728. (org-at-property-p)
  12729. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12730. (setq c (read-char-exclusive))
  12731. (cond
  12732. ((equal c ?s)
  12733. (call-interactively 'org-set-property))
  12734. ((equal c ?d)
  12735. (call-interactively 'org-delete-property))
  12736. ((equal c ?D)
  12737. (call-interactively 'org-delete-property-globally))
  12738. ((equal c ?c)
  12739. (call-interactively 'org-compute-property-at-point))
  12740. (t (error "No such property action %c" c)))))
  12741. (defun org-set-effort (&optional value)
  12742. "Set the effort property of the current entry.
  12743. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12744. allowed value."
  12745. (interactive "P")
  12746. (if (equal value 0) (setq value 10))
  12747. (let* ((completion-ignore-case t)
  12748. (prop org-effort-property)
  12749. (cur (org-entry-get nil prop))
  12750. (allowed (org-property-get-allowed-values nil prop 'table))
  12751. (existing (mapcar 'list (org-property-values prop)))
  12752. rpl
  12753. (val (cond
  12754. ((stringp value) value)
  12755. ((and allowed (integerp value))
  12756. (or (car (nth (1- value) allowed))
  12757. (car (org-last allowed))))
  12758. (allowed
  12759. (message "Select 1-9,0, [RET%s]: %s"
  12760. (if cur (concat "=" cur) "")
  12761. (mapconcat 'car allowed " "))
  12762. (setq rpl (read-char-exclusive))
  12763. (if (equal rpl ?\r)
  12764. cur
  12765. (setq rpl (- rpl ?0))
  12766. (if (equal rpl 0) (setq rpl 10))
  12767. (if (and (> rpl 0) (<= rpl (length allowed)))
  12768. (car (nth (1- rpl) allowed))
  12769. (org-completing-read "Effort: " allowed nil))))
  12770. (t
  12771. (let (org-completion-use-ido org-completion-use-iswitchb)
  12772. (org-completing-read
  12773. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12774. (concat "[" cur "]") "")
  12775. ": ")
  12776. existing nil nil "" nil cur))))))
  12777. (unless (equal (org-entry-get nil prop) val)
  12778. (org-entry-put nil prop val))
  12779. (message "%s is now %s" prop val)))
  12780. (defun org-at-property-p ()
  12781. "Is cursor inside a property drawer?"
  12782. (save-excursion
  12783. (beginning-of-line 1)
  12784. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12785. (save-match-data ;; Used by calling procedures
  12786. (let ((p (point))
  12787. (range (unless (org-before-first-heading-p)
  12788. (org-get-property-block))))
  12789. (and range (<= (car range) p) (< p (cdr range))))))))
  12790. (defun org-get-property-block (&optional beg end force)
  12791. "Return the (beg . end) range of the body of the property drawer.
  12792. BEG and END can be beginning and end of subtree, if not given
  12793. they will be found.
  12794. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12795. (catch 'exit
  12796. (save-excursion
  12797. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12798. (end (or end (progn (outline-next-heading) (point)))))
  12799. (goto-char beg)
  12800. (if (re-search-forward org-property-start-re end t)
  12801. (setq beg (1+ (match-end 0)))
  12802. (if force
  12803. (save-excursion
  12804. (org-insert-property-drawer)
  12805. (setq end (progn (outline-next-heading) (point))))
  12806. (throw 'exit nil))
  12807. (goto-char beg)
  12808. (if (re-search-forward org-property-start-re end t)
  12809. (setq beg (1+ (match-end 0)))))
  12810. (if (re-search-forward org-property-end-re end t)
  12811. (setq end (match-beginning 0))
  12812. (or force (throw 'exit nil))
  12813. (goto-char beg)
  12814. (setq end beg)
  12815. (org-indent-line-function)
  12816. (insert ":END:\n"))
  12817. (cons beg end)))))
  12818. (defun org-entry-properties (&optional pom which specific)
  12819. "Get all properties of the entry at point-or-marker POM.
  12820. This includes the TODO keyword, the tags, time strings for deadline,
  12821. scheduled, and clocking, and any additional properties defined in the
  12822. entry. The return value is an alist, keys may occur multiple times
  12823. if the property key was used several times.
  12824. POM may also be nil, in which case the current entry is used.
  12825. If WHICH is nil or `all', get all properties. If WHICH is
  12826. `special' or `standard', only get that subclass. If WHICH
  12827. is a string only get exactly this property. SPECIFIC can be a string, the
  12828. specific property we are interested in. Specifying it can speed
  12829. things up because then unnecessary parsing is avoided."
  12830. (setq which (or which 'all))
  12831. (org-with-point-at pom
  12832. (let ((clockstr (substring org-clock-string 0 -1))
  12833. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12834. (case-fold-search nil)
  12835. beg end range props sum-props key key1 value string clocksum)
  12836. (save-excursion
  12837. (when (condition-case nil
  12838. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12839. (error nil))
  12840. (setq beg (point))
  12841. (setq sum-props (get-text-property (point) 'org-summaries))
  12842. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12843. (outline-next-heading)
  12844. (setq end (point))
  12845. (when (memq which '(all special))
  12846. ;; Get the special properties, like TODO and tags
  12847. (goto-char beg)
  12848. (when (and (or (not specific) (string= specific "TODO"))
  12849. (looking-at org-todo-line-regexp) (match-end 2))
  12850. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12851. (when (and (or (not specific) (string= specific "PRIORITY"))
  12852. (looking-at org-priority-regexp))
  12853. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12854. (when (or (not specific) (string= specific "FILE"))
  12855. (push (cons "FILE" buffer-file-name) props))
  12856. (when (and (or (not specific) (string= specific "TAGS"))
  12857. (setq value (org-get-tags-string))
  12858. (string-match "\\S-" value))
  12859. (push (cons "TAGS" value) props))
  12860. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12861. (setq value (org-get-tags-at)))
  12862. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12863. ":"))
  12864. props))
  12865. (when (or (not specific) (string= specific "BLOCKED"))
  12866. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12867. (when (or (not specific)
  12868. (member specific
  12869. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12870. "TIMESTAMP" "TIMESTAMP_IA")))
  12871. (catch 'match
  12872. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12873. (setq key (if (match-end 1)
  12874. (substring (org-match-string-no-properties 1)
  12875. 0 -1))
  12876. string (if (equal key clockstr)
  12877. (org-no-properties
  12878. (org-trim
  12879. (buffer-substring
  12880. (match-beginning 3) (goto-char
  12881. (point-at-eol)))))
  12882. (substring (org-match-string-no-properties 3)
  12883. 1 -1)))
  12884. ;; Get the correct property name from the key. This is
  12885. ;; necessary if the user has configured time keywords.
  12886. (setq key1 (concat key ":"))
  12887. (cond
  12888. ((not key)
  12889. (setq key
  12890. (if (= (char-after (match-beginning 3)) ?\[)
  12891. "TIMESTAMP_IA" "TIMESTAMP")))
  12892. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12893. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12894. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12895. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12896. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12897. (progn
  12898. (push (cons key string) props)
  12899. ;; no need to search further if match is found
  12900. (throw 'match t))
  12901. (when (or (equal key "CLOCK") (not (assoc key props)))
  12902. (push (cons key string) props))))))
  12903. )
  12904. (when (memq which '(all standard))
  12905. ;; Get the standard properties, like :PROP: ...
  12906. (setq range (org-get-property-block beg end))
  12907. (when range
  12908. (goto-char (car range))
  12909. (while (re-search-forward
  12910. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12911. (cdr range) t)
  12912. (setq key (org-match-string-no-properties 1)
  12913. value (org-trim (or (org-match-string-no-properties 2) "")))
  12914. (unless (member key excluded)
  12915. (push (cons key (or value "")) props)))))
  12916. (if clocksum
  12917. (push (cons "CLOCKSUM"
  12918. (org-columns-number-to-string (/ (float clocksum) 60.)
  12919. 'add_times))
  12920. props))
  12921. (unless (assoc "CATEGORY" props)
  12922. (push (cons "CATEGORY" (org-get-category)) props))
  12923. (append sum-props (nreverse props)))))))
  12924. (defun org-entry-get (pom property &optional inherit literal-nil)
  12925. "Get value of PROPERTY for entry at point-or-marker POM.
  12926. If INHERIT is non-nil and the entry does not have the property,
  12927. then also check higher levels of the hierarchy.
  12928. If INHERIT is the symbol `selective', use inheritance only if the setting
  12929. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12930. If the property is present but empty, the return value is the empty string.
  12931. If the property is not present at all, nil is returned.
  12932. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12933. do not interpret it as the list atom nil. This is used for inheritance
  12934. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12935. (org-with-point-at pom
  12936. (if (and inherit (if (eq inherit 'selective)
  12937. (org-property-inherit-p property)
  12938. t))
  12939. (org-entry-get-with-inheritance property literal-nil)
  12940. (if (member property org-special-properties)
  12941. ;; We need a special property. Use `org-entry-properties' to
  12942. ;; retrieve it, but specify the wanted property
  12943. (cdr (assoc property (org-entry-properties nil 'special property)))
  12944. (let ((range (unless (org-before-first-heading-p)
  12945. (org-get-property-block)))
  12946. (props (list (or (assoc property org-file-properties)
  12947. (assoc property org-global-properties)
  12948. (assoc property org-global-properties-fixed))))
  12949. val)
  12950. (flet ((ap (key)
  12951. (when (re-search-forward
  12952. (org-re-property key) (cdr range) t)
  12953. (setq props
  12954. (org-update-property-plist
  12955. key
  12956. (if (match-end 1)
  12957. (org-match-string-no-properties 1) "")
  12958. props)))))
  12959. (when (and range (goto-char (car range)))
  12960. (ap property)
  12961. (goto-char (car range))
  12962. (while (ap (concat property "+")))
  12963. (setq val (cdr (assoc property props)))
  12964. (when val (if literal-nil val (org-not-nil val))))))))))
  12965. (defun org-property-or-variable-value (var &optional inherit)
  12966. "Check if there is a property fixing the value of VAR.
  12967. If yes, return this value. If not, return the current value of the variable."
  12968. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12969. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12970. (read prop)
  12971. (symbol-value var))))
  12972. (defun org-entry-delete (pom property)
  12973. "Delete the property PROPERTY from entry at point-or-marker POM."
  12974. (org-with-point-at pom
  12975. (if (member property org-special-properties)
  12976. nil ; cannot delete these properties.
  12977. (let ((range (org-get-property-block)))
  12978. (if (and range
  12979. (goto-char (car range))
  12980. (re-search-forward
  12981. (org-re-property property)
  12982. (cdr range) t))
  12983. (progn
  12984. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12985. t)
  12986. nil)))))
  12987. ;; Multi-values properties are properties that contain multiple values
  12988. ;; These values are assumed to be single words, separated by whitespace.
  12989. (defun org-entry-add-to-multivalued-property (pom property value)
  12990. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12991. (let* ((old (org-entry-get pom property))
  12992. (values (and old (org-split-string old "[ \t]"))))
  12993. (setq value (org-entry-protect-space value))
  12994. (unless (member value values)
  12995. (setq values (cons value values))
  12996. (org-entry-put pom property
  12997. (mapconcat 'identity values " ")))))
  12998. (defun org-entry-remove-from-multivalued-property (pom property value)
  12999. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13000. (let* ((old (org-entry-get pom property))
  13001. (values (and old (org-split-string old "[ \t]"))))
  13002. (setq value (org-entry-protect-space value))
  13003. (when (member value values)
  13004. (setq values (delete value values))
  13005. (org-entry-put pom property
  13006. (mapconcat 'identity values " ")))))
  13007. (defun org-entry-member-in-multivalued-property (pom property value)
  13008. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13009. (let* ((old (org-entry-get pom property))
  13010. (values (and old (org-split-string old "[ \t]"))))
  13011. (setq value (org-entry-protect-space value))
  13012. (member value values)))
  13013. (defun org-entry-get-multivalued-property (pom property)
  13014. "Return a list of values in a multivalued property."
  13015. (let* ((value (org-entry-get pom property))
  13016. (values (and value (org-split-string value "[ \t]"))))
  13017. (mapcar 'org-entry-restore-space values)))
  13018. (defun org-entry-put-multivalued-property (pom property &rest values)
  13019. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13020. VALUES should be a list of strings. Spaces will be protected."
  13021. (org-entry-put pom property
  13022. (mapconcat 'org-entry-protect-space values " "))
  13023. (let* ((value (org-entry-get pom property))
  13024. (values (and value (org-split-string value "[ \t]"))))
  13025. (mapcar 'org-entry-restore-space values)))
  13026. (defun org-entry-protect-space (s)
  13027. "Protect spaces and newline in string S."
  13028. (while (string-match " " s)
  13029. (setq s (replace-match "%20" t t s)))
  13030. (while (string-match "\n" s)
  13031. (setq s (replace-match "%0A" t t s)))
  13032. s)
  13033. (defun org-entry-restore-space (s)
  13034. "Restore spaces and newline in string S."
  13035. (while (string-match "%20" s)
  13036. (setq s (replace-match " " t t s)))
  13037. (while (string-match "%0A" s)
  13038. (setq s (replace-match "\n" t t s)))
  13039. s)
  13040. (defvar org-entry-property-inherited-from (make-marker)
  13041. "Marker pointing to the entry from where a property was inherited.
  13042. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13043. location of the entry where the inheritance search matched. If there was
  13044. no match, the marker will point nowhere.
  13045. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13046. is set.")
  13047. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13048. "Get entry property, and search higher levels if not present.
  13049. The search will stop at the first ancestor which has the property defined.
  13050. If the value found is \"nil\", return nil to show that the property
  13051. should be considered as undefined (this is the meaning of nil here).
  13052. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13053. (move-marker org-entry-property-inherited-from nil)
  13054. (let (tmp)
  13055. (unless (org-before-first-heading-p)
  13056. (save-excursion
  13057. (save-restriction
  13058. (widen)
  13059. (catch 'ex
  13060. (while t
  13061. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13062. (org-back-to-heading t)
  13063. (move-marker org-entry-property-inherited-from (point))
  13064. (throw 'ex tmp))
  13065. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13066. (setq tmp (or tmp
  13067. (cdr (assoc property org-file-properties))
  13068. (cdr (assoc property org-global-properties))
  13069. (cdr (assoc property org-global-properties-fixed))))
  13070. (if literal-nil tmp (org-not-nil tmp))))
  13071. (defvar org-property-changed-functions nil
  13072. "Hook called when the value of a property has changed.
  13073. Each hook function should accept two arguments, the name of the property
  13074. and the new value.")
  13075. (defun org-entry-put (pom property value)
  13076. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13077. (org-with-point-at pom
  13078. (org-back-to-heading t)
  13079. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13080. range)
  13081. (cond
  13082. ((equal property "TODO")
  13083. (when (and (stringp value) (string-match "\\S-" value)
  13084. (not (member value org-todo-keywords-1)))
  13085. (error "\"%s\" is not a valid TODO state" value))
  13086. (if (or (not value)
  13087. (not (string-match "\\S-" value)))
  13088. (setq value 'none))
  13089. (org-todo value)
  13090. (org-set-tags nil 'align))
  13091. ((equal property "PRIORITY")
  13092. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13093. (string-to-char value) ?\ ))
  13094. (org-set-tags nil 'align))
  13095. ((equal property "SCHEDULED")
  13096. (if (re-search-forward org-scheduled-time-regexp end t)
  13097. (cond
  13098. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13099. ((eq value 'later) (org-timestamp-change 1 'day))
  13100. (t (call-interactively 'org-schedule)))
  13101. (call-interactively 'org-schedule)))
  13102. ((equal property "DEADLINE")
  13103. (if (re-search-forward org-deadline-time-regexp end t)
  13104. (cond
  13105. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13106. ((eq value 'later) (org-timestamp-change 1 'day))
  13107. (t (call-interactively 'org-deadline)))
  13108. (call-interactively 'org-deadline)))
  13109. ((member property org-special-properties)
  13110. (error "The %s property can not yet be set with `org-entry-put'"
  13111. property))
  13112. (t ; a non-special property
  13113. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13114. (setq range (org-get-property-block beg end 'force))
  13115. (goto-char (car range))
  13116. (if (re-search-forward
  13117. (org-re-property property) (cdr range) t)
  13118. (progn
  13119. (delete-region (match-beginning 0) (match-end 0))
  13120. (goto-char (match-beginning 0)))
  13121. (goto-char (cdr range))
  13122. (insert "\n")
  13123. (backward-char 1)
  13124. (org-indent-line-function))
  13125. (insert ":" property ":")
  13126. (and value (insert " " value))
  13127. (org-indent-line-function)))))
  13128. (run-hook-with-args 'org-property-changed-functions property value)))
  13129. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13130. "Get all property keys in the current buffer.
  13131. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13132. like tags and TODO state.
  13133. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13134. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13135. and others.
  13136. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13137. formats in the current buffer."
  13138. (let (rtn range cfmt s p)
  13139. (save-excursion
  13140. (save-restriction
  13141. (widen)
  13142. (goto-char (point-min))
  13143. (while (re-search-forward org-property-start-re nil t)
  13144. (setq range (org-get-property-block))
  13145. (goto-char (car range))
  13146. (while (re-search-forward
  13147. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13148. (cdr range) t)
  13149. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13150. (outline-next-heading))))
  13151. (when include-specials
  13152. (setq rtn (append org-special-properties rtn)))
  13153. (when include-defaults
  13154. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13155. (add-to-list 'rtn org-effort-property))
  13156. (when include-columns
  13157. (save-excursion
  13158. (save-restriction
  13159. (widen)
  13160. (goto-char (point-min))
  13161. (while (re-search-forward
  13162. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13163. nil t)
  13164. (setq cfmt (match-string 2) s 0)
  13165. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13166. cfmt s)
  13167. (setq s (match-end 0)
  13168. p (match-string 1 cfmt))
  13169. (unless (or (equal p "ITEM")
  13170. (member p org-special-properties))
  13171. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13172. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13173. (defun org-property-values (key)
  13174. "Return a list of all values of property KEY in the current buffer."
  13175. (save-excursion
  13176. (save-restriction
  13177. (widen)
  13178. (goto-char (point-min))
  13179. (let ((re (org-re-property key))
  13180. values)
  13181. (while (re-search-forward re nil t)
  13182. (add-to-list 'values (org-trim (match-string 1))))
  13183. (delete "" values)))))
  13184. (defun org-insert-property-drawer ()
  13185. "Insert a property drawer into the current entry."
  13186. (interactive)
  13187. (org-back-to-heading t)
  13188. (looking-at org-outline-regexp)
  13189. (let ((indent (if org-adapt-indentation
  13190. (- (match-end 0)(match-beginning 0))
  13191. 0))
  13192. (beg (point))
  13193. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13194. end hiddenp)
  13195. (outline-next-heading)
  13196. (setq end (point))
  13197. (goto-char beg)
  13198. (while (re-search-forward re end t))
  13199. (setq hiddenp (outline-invisible-p))
  13200. (end-of-line 1)
  13201. (and (equal (char-after) ?\n) (forward-char 1))
  13202. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13203. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13204. ;; just skip this line
  13205. (beginning-of-line 2)
  13206. ;; Drawer start, find the end
  13207. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13208. (beginning-of-line 1)))
  13209. (org-skip-over-state-notes)
  13210. (skip-chars-backward " \t\n\r")
  13211. (if (eq (char-before) ?*) (forward-char 1))
  13212. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13213. (beginning-of-line 0)
  13214. (org-indent-to-column indent)
  13215. (beginning-of-line 2)
  13216. (org-indent-to-column indent)
  13217. (beginning-of-line 0)
  13218. (if hiddenp
  13219. (save-excursion
  13220. (org-back-to-heading t)
  13221. (hide-entry))
  13222. (org-flag-drawer t))))
  13223. (defvar org-property-set-functions-alist nil
  13224. "Property set function alist.
  13225. Each entry should have the following format:
  13226. (PROPERTY . READ-FUNCTION)
  13227. The read function will be called with the same argument as
  13228. `org-completing-read'.")
  13229. (defun org-set-property-function (property)
  13230. "Get the function that should be used to set PROPERTY.
  13231. This is computed according to `org-property-set-functions-alist'."
  13232. (or (cdr (assoc property org-property-set-functions-alist))
  13233. 'org-completing-read))
  13234. (defun org-read-property-value (property)
  13235. "Read PROPERTY value from user."
  13236. (let* ((completion-ignore-case t)
  13237. (allowed (org-property-get-allowed-values nil property 'table))
  13238. (cur (org-entry-get nil property))
  13239. (prompt (concat property " value"
  13240. (if (and cur (string-match "\\S-" cur))
  13241. (concat " [" cur "]") "") ": "))
  13242. (set-function (org-set-property-function property))
  13243. (val (if allowed
  13244. (funcall set-function prompt allowed nil
  13245. (not (get-text-property 0 'org-unrestricted
  13246. (caar allowed))))
  13247. (let (org-completion-use-ido org-completion-use-iswitchb)
  13248. (funcall set-function prompt
  13249. (mapcar 'list (org-property-values property))
  13250. nil nil "" nil cur)))))
  13251. (if (equal val "")
  13252. cur
  13253. val)))
  13254. (defvar org-last-set-property nil)
  13255. (defun org-read-property-name ()
  13256. "Read a property name."
  13257. (let* ((completion-ignore-case t)
  13258. (keys (org-buffer-property-keys nil t t))
  13259. (default-prop (or (save-excursion
  13260. (save-match-data
  13261. (beginning-of-line)
  13262. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13263. (null (string= (match-string 1) "END"))
  13264. (match-string 1))))
  13265. org-last-set-property))
  13266. (property (org-icompleting-read
  13267. (concat "Property"
  13268. (if default-prop (concat " [" default-prop "]") "")
  13269. ": ")
  13270. (mapcar 'list keys)
  13271. nil nil nil nil
  13272. default-prop
  13273. )))
  13274. (if (member property keys)
  13275. property
  13276. (or (cdr (assoc (downcase property)
  13277. (mapcar (lambda (x) (cons (downcase x) x))
  13278. keys)))
  13279. property))))
  13280. (defun org-set-property (property value)
  13281. "In the current entry, set PROPERTY to VALUE.
  13282. When called interactively, this will prompt for a property name, offering
  13283. completion on existing and default properties. And then it will prompt
  13284. for a value, offering completion either on allowed values (via an inherited
  13285. xxx_ALL property) or on existing values in other instances of this property
  13286. in the current file."
  13287. (interactive (list nil nil))
  13288. (let* ((property (or property (org-read-property-name)))
  13289. (value (or value (org-read-property-value property)))
  13290. (fn (assoc property org-properties-postprocess-alist)))
  13291. (setq org-last-set-property property)
  13292. ;; Possibly postprocess the inserted value:
  13293. (when fn (setq value (funcall (cadr fn) value)))
  13294. (unless (equal (org-entry-get nil property) value)
  13295. (org-entry-put nil property value))))
  13296. (defun org-delete-property (property)
  13297. "In the current entry, delete PROPERTY."
  13298. (interactive
  13299. (let* ((completion-ignore-case t)
  13300. (prop (org-icompleting-read "Property: "
  13301. (org-entry-properties nil 'standard))))
  13302. (list prop)))
  13303. (message "Property %s %s" property
  13304. (if (org-entry-delete nil property)
  13305. "deleted"
  13306. "was not present in the entry")))
  13307. (defun org-delete-property-globally (property)
  13308. "Remove PROPERTY globally, from all entries."
  13309. (interactive
  13310. (let* ((completion-ignore-case t)
  13311. (prop (org-icompleting-read
  13312. "Globally remove property: "
  13313. (mapcar 'list (org-buffer-property-keys)))))
  13314. (list prop)))
  13315. (save-excursion
  13316. (save-restriction
  13317. (widen)
  13318. (goto-char (point-min))
  13319. (let ((cnt 0))
  13320. (while (re-search-forward
  13321. (org-re-property property)
  13322. nil t)
  13323. (setq cnt (1+ cnt))
  13324. (replace-match ""))
  13325. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13326. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13327. (defun org-compute-property-at-point ()
  13328. "Compute the property at point.
  13329. This looks for an enclosing column format, extracts the operator and
  13330. then applies it to the property in the column format's scope."
  13331. (interactive)
  13332. (unless (org-at-property-p)
  13333. (error "Not at a property"))
  13334. (let ((prop (org-match-string-no-properties 2)))
  13335. (org-columns-get-format-and-top-level)
  13336. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13337. (error "No operator defined for property %s" prop))
  13338. (org-columns-compute prop)))
  13339. (defvar org-property-allowed-value-functions nil
  13340. "Hook for functions supplying allowed values for a specific property.
  13341. The functions must take a single argument, the name of the property, and
  13342. return a flat list of allowed values. If \":ETC\" is one of
  13343. the values, this means that these values are intended as defaults for
  13344. completion, but that other values should be allowed too.
  13345. The functions must return nil if they are not responsible for this
  13346. property.")
  13347. (defun org-property-get-allowed-values (pom property &optional table)
  13348. "Get allowed values for the property PROPERTY.
  13349. When TABLE is non-nil, return an alist that can directly be used for
  13350. completion."
  13351. (let (vals)
  13352. (cond
  13353. ((equal property "TODO")
  13354. (setq vals (org-with-point-at pom
  13355. (append org-todo-keywords-1 '("")))))
  13356. ((equal property "PRIORITY")
  13357. (let ((n org-lowest-priority))
  13358. (while (>= n org-highest-priority)
  13359. (push (char-to-string n) vals)
  13360. (setq n (1- n)))))
  13361. ((member property org-special-properties))
  13362. ((setq vals (run-hook-with-args-until-success
  13363. 'org-property-allowed-value-functions property)))
  13364. (t
  13365. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13366. (when (and vals (string-match "\\S-" vals))
  13367. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13368. (setq vals (mapcar (lambda (x)
  13369. (cond ((stringp x) x)
  13370. ((numberp x) (number-to-string x))
  13371. ((symbolp x) (symbol-name x))
  13372. (t "???")))
  13373. vals)))))
  13374. (when (member ":ETC" vals)
  13375. (setq vals (remove ":ETC" vals))
  13376. (org-add-props (car vals) '(org-unrestricted t)))
  13377. (if table (mapcar 'list vals) vals)))
  13378. (defun org-property-previous-allowed-value (&optional previous)
  13379. "Switch to the next allowed value for this property."
  13380. (interactive)
  13381. (org-property-next-allowed-value t))
  13382. (defun org-property-next-allowed-value (&optional previous)
  13383. "Switch to the next allowed value for this property."
  13384. (interactive)
  13385. (unless (org-at-property-p)
  13386. (error "Not at a property"))
  13387. (let* ((key (match-string 2))
  13388. (value (match-string 3))
  13389. (allowed (or (org-property-get-allowed-values (point) key)
  13390. (and (member value '("[ ]" "[-]" "[X]"))
  13391. '("[ ]" "[X]"))))
  13392. nval)
  13393. (unless allowed
  13394. (error "Allowed values for this property have not been defined"))
  13395. (if previous (setq allowed (reverse allowed)))
  13396. (if (member value allowed)
  13397. (setq nval (car (cdr (member value allowed)))))
  13398. (setq nval (or nval (car allowed)))
  13399. (if (equal nval value)
  13400. (error "Only one allowed value for this property"))
  13401. (org-at-property-p)
  13402. (replace-match (concat " :" key ": " nval) t t)
  13403. (org-indent-line-function)
  13404. (beginning-of-line 1)
  13405. (skip-chars-forward " \t")
  13406. (run-hook-with-args 'org-property-changed-functions key nval)))
  13407. (defun org-find-olp (path &optional this-buffer)
  13408. "Return a marker pointing to the entry at outline path OLP.
  13409. If anything goes wrong, throw an error.
  13410. You can wrap this call to catch the error like this:
  13411. (condition-case msg
  13412. (org-mobile-locate-entry (match-string 4))
  13413. (error (nth 1 msg)))
  13414. The return value will then be either a string with the error message,
  13415. or a marker if everything is OK.
  13416. If THIS-BUFFER is set, the outline path does not contain a file,
  13417. only headings."
  13418. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13419. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13420. (level 1)
  13421. (lmin 1)
  13422. (lmax 1)
  13423. limit re end found pos heading cnt flevel)
  13424. (unless buffer (error "File not found :%s" file))
  13425. (with-current-buffer buffer
  13426. (save-excursion
  13427. (save-restriction
  13428. (widen)
  13429. (setq limit (point-max))
  13430. (goto-char (point-min))
  13431. (while (setq heading (pop path))
  13432. (setq re (format org-complex-heading-regexp-format
  13433. (regexp-quote heading)))
  13434. (setq cnt 0 pos (point))
  13435. (while (re-search-forward re end t)
  13436. (setq level (- (match-end 1) (match-beginning 1)))
  13437. (if (and (>= level lmin) (<= level lmax))
  13438. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13439. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13440. lmax heading))
  13441. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13442. lmax heading))
  13443. (goto-char found)
  13444. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13445. (setq end (save-excursion (org-end-of-subtree t t))))
  13446. (when (org-at-heading-p)
  13447. (move-marker (make-marker) (point))))))))
  13448. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13449. "Find node HEADING in BUFFER.
  13450. Return a marker to the heading if it was found, or nil if not.
  13451. If POS-ONLY is set, return just the position instead of a marker.
  13452. The heading text must match exact, but it may have a TODO keyword,
  13453. a priority cookie and tags in the standard locations."
  13454. (with-current-buffer (or buffer (current-buffer))
  13455. (save-excursion
  13456. (save-restriction
  13457. (widen)
  13458. (goto-char (point-min))
  13459. (let (case-fold-search)
  13460. (if (re-search-forward
  13461. (format org-complex-heading-regexp-format
  13462. (regexp-quote heading)) nil t)
  13463. (if pos-only
  13464. (match-beginning 0)
  13465. (move-marker (make-marker) (match-beginning 0)))))))))
  13466. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13467. "Find Org node headline HEADING in all .org files in directory DIR.
  13468. When the target headline is found, return a marker to this location."
  13469. (let ((files (directory-files (or dir default-directory)
  13470. nil "\\`[^.#].*\\.org\\'"))
  13471. file visiting m buffer)
  13472. (catch 'found
  13473. (while (setq file (pop files))
  13474. (message "trying %s" file)
  13475. (setq visiting (org-find-base-buffer-visiting file))
  13476. (setq buffer (or visiting (find-file-noselect file)))
  13477. (setq m (org-find-exact-headline-in-buffer
  13478. heading buffer))
  13479. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13480. (and m (throw 'found m))))))
  13481. (defun org-find-entry-with-id (ident)
  13482. "Locate the entry that contains the ID property with exact value IDENT.
  13483. IDENT can be a string, a symbol or a number, this function will search for
  13484. the string representation of it.
  13485. Return the position where this entry starts, or nil if there is no such entry."
  13486. (interactive "sID: ")
  13487. (let ((id (cond
  13488. ((stringp ident) ident)
  13489. ((symbol-name ident) (symbol-name ident))
  13490. ((numberp ident) (number-to-string ident))
  13491. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13492. (case-fold-search nil))
  13493. (save-excursion
  13494. (save-restriction
  13495. (widen)
  13496. (goto-char (point-min))
  13497. (when (re-search-forward
  13498. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13499. nil t)
  13500. (org-back-to-heading t)
  13501. (point))))))
  13502. ;;;; Timestamps
  13503. (defvar org-last-changed-timestamp nil)
  13504. (defvar org-last-inserted-timestamp nil
  13505. "The last time stamp inserted with `org-insert-time-stamp'.")
  13506. (defvar org-time-was-given) ; dynamically scoped parameter
  13507. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13508. (defvar org-ts-what) ; dynamically scoped parameter
  13509. (defun org-time-stamp (arg &optional inactive)
  13510. "Prompt for a date/time and insert a time stamp.
  13511. If the user specifies a time like HH:MM, or if this command is called
  13512. with a prefix argument, the time stamp will contain date and time.
  13513. Otherwise, only the date will be included. All parts of a date not
  13514. specified by the user will be filled in from the current date/time.
  13515. So if you press just return without typing anything, the time stamp
  13516. will represent the current date/time. If there is already a timestamp
  13517. at the cursor, it will be modified."
  13518. (interactive "P")
  13519. (let* ((ts nil)
  13520. (default-time
  13521. ;; Default time is either today, or, when entering a range,
  13522. ;; the range start.
  13523. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13524. (save-excursion
  13525. (re-search-backward
  13526. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13527. (- (point) 20) t)))
  13528. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13529. (current-time)))
  13530. (default-input (and ts (org-get-compact-tod ts)))
  13531. (repeater (save-excursion
  13532. (save-match-data
  13533. (beginning-of-line)
  13534. (when (re-search-forward
  13535. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13536. (save-excursion (progn (end-of-line) (point))) t)
  13537. (match-string 0)))))
  13538. org-time-was-given org-end-time-was-given time)
  13539. (cond
  13540. ((and (org-at-timestamp-p t)
  13541. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13542. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13543. (insert "--")
  13544. (setq time (let ((this-command this-command))
  13545. (org-read-date arg 'totime nil nil
  13546. default-time default-input)))
  13547. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13548. ((org-at-timestamp-p t)
  13549. (setq time (let ((this-command this-command))
  13550. (org-read-date arg 'totime nil nil default-time default-input)))
  13551. (when (org-at-timestamp-p t) ; just to get the match data
  13552. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13553. (replace-match "")
  13554. (setq org-last-changed-timestamp
  13555. (org-insert-time-stamp
  13556. time (or org-time-was-given arg)
  13557. inactive nil nil (list org-end-time-was-given)))
  13558. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13559. (setq org-last-changed-timestamp
  13560. (concat (substring org-last-inserted-timestamp 0 -1)
  13561. " " repeater ">"))))
  13562. (message "Timestamp updated"))
  13563. (t
  13564. (setq time (let ((this-command this-command))
  13565. (org-read-date arg 'totime nil nil default-time default-input)))
  13566. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13567. nil nil (list org-end-time-was-given))))))
  13568. ;; FIXME: can we use this for something else, like computing time differences?
  13569. (defun org-get-compact-tod (s)
  13570. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13571. (let* ((t1 (match-string 1 s))
  13572. (h1 (string-to-number (match-string 2 s)))
  13573. (m1 (string-to-number (match-string 3 s)))
  13574. (t2 (and (match-end 4) (match-string 5 s)))
  13575. (h2 (and t2 (string-to-number (match-string 6 s))))
  13576. (m2 (and t2 (string-to-number (match-string 7 s))))
  13577. dh dm)
  13578. (if (not t2)
  13579. t1
  13580. (setq dh (- h2 h1) dm (- m2 m1))
  13581. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13582. (concat t1 "+" (number-to-string dh)
  13583. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13584. (defun org-time-stamp-inactive (&optional arg)
  13585. "Insert an inactive time stamp.
  13586. An inactive time stamp is enclosed in square brackets instead of angle
  13587. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13588. does not link to the calendar and cannot be changed with the S-cursor keys.
  13589. So these are more for recording a certain time/date."
  13590. (interactive "P")
  13591. (org-time-stamp arg 'inactive))
  13592. (defvar org-date-ovl (make-overlay 1 1))
  13593. (overlay-put org-date-ovl 'face 'org-warning)
  13594. (org-detach-overlay org-date-ovl)
  13595. (defvar org-ans1) ; dynamically scoped parameter
  13596. (defvar org-ans2) ; dynamically scoped parameter
  13597. (defvar org-plain-time-of-day-regexp) ; defined below
  13598. (defvar org-overriding-default-time nil) ; dynamically scoped
  13599. (defvar org-read-date-overlay nil)
  13600. (defvar org-dcst nil) ; dynamically scoped
  13601. (defvar org-read-date-history nil)
  13602. (defvar org-read-date-final-answer nil)
  13603. (defvar org-read-date-analyze-futurep nil)
  13604. (defvar org-read-date-analyze-forced-year nil)
  13605. (defun org-read-date (&optional with-time to-time from-string prompt
  13606. default-time default-input)
  13607. "Read a date, possibly a time, and make things smooth for the user.
  13608. The prompt will suggest to enter an ISO date, but you can also enter anything
  13609. which will at least partially be understood by `parse-time-string'.
  13610. Unrecognized parts of the date will default to the current day, month, year,
  13611. hour and minute. If this command is called to replace a timestamp at point,
  13612. or to enter the second timestamp of a range, the default time is taken
  13613. from the existing stamp. Furthermore, the command prefers the future,
  13614. so if you are giving a date where the year is not given, and the day-month
  13615. combination is already past in the current year, it will assume you
  13616. mean next year. For details, see the manual. A few examples:
  13617. 3-2-5 --> 2003-02-05
  13618. feb 15 --> currentyear-02-15
  13619. 2/15 --> currentyear-02-15
  13620. sep 12 9 --> 2009-09-12
  13621. 12:45 --> today 12:45
  13622. 22 sept 0:34 --> currentyear-09-22 0:34
  13623. 12 --> currentyear-currentmonth-12
  13624. Fri --> nearest Friday (today or later)
  13625. etc.
  13626. Furthermore you can specify a relative date by giving, as the *first* thing
  13627. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13628. change in days weeks, months, years.
  13629. With a single plus or minus, the date is relative to today. With a double
  13630. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13631. +4d --> four days from today
  13632. +4 --> same as above
  13633. +2w --> two weeks from today
  13634. ++5 --> five days from default date
  13635. The function understands only English month and weekday abbreviations.
  13636. While prompting, a calendar is popped up - you can also select the
  13637. date with the mouse (button 1). The calendar shows a period of three
  13638. months. To scroll it to other months, use the keys `>' and `<'.
  13639. If you don't like the calendar, turn it off with
  13640. \(setq org-read-date-popup-calendar nil)
  13641. With optional argument TO-TIME, the date will immediately be converted
  13642. to an internal time.
  13643. With an optional argument WITH-TIME, the prompt will suggest to also
  13644. insert a time. Note that when WITH-TIME is not set, you can still
  13645. enter a time, and this function will inform the calling routine about
  13646. this change. The calling routine may then choose to change the format
  13647. used to insert the time stamp into the buffer to include the time.
  13648. With optional argument FROM-STRING, read from this string instead from
  13649. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13650. the time/date that is used for everything that is not specified by the
  13651. user."
  13652. (require 'parse-time)
  13653. (let* ((org-time-stamp-rounding-minutes
  13654. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13655. (org-dcst org-display-custom-times)
  13656. (ct (org-current-time))
  13657. (def (or org-overriding-default-time default-time ct))
  13658. (defdecode (decode-time def))
  13659. (dummy (progn
  13660. (when (< (nth 2 defdecode) org-extend-today-until)
  13661. (setcar (nthcdr 2 defdecode) -1)
  13662. (setcar (nthcdr 1 defdecode) 59)
  13663. (setq def (apply 'encode-time defdecode)
  13664. defdecode (decode-time def)))))
  13665. (calendar-frame-setup nil)
  13666. (calendar-setup nil)
  13667. (calendar-move-hook nil)
  13668. (calendar-view-diary-initially-flag nil)
  13669. (calendar-view-holidays-initially-flag nil)
  13670. (timestr (format-time-string
  13671. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13672. (prompt (concat (if prompt (concat prompt " ") "")
  13673. (format "Date+time [%s]: " timestr)))
  13674. ans (org-ans0 "") org-ans1 org-ans2 final)
  13675. (cond
  13676. (from-string (setq ans from-string))
  13677. (org-read-date-popup-calendar
  13678. (save-excursion
  13679. (save-window-excursion
  13680. (calendar)
  13681. (unwind-protect
  13682. (progn
  13683. (calendar-forward-day (- (time-to-days def)
  13684. (calendar-absolute-from-gregorian
  13685. (calendar-current-date))))
  13686. (org-eval-in-calendar nil t)
  13687. (let* ((old-map (current-local-map))
  13688. (map (copy-keymap calendar-mode-map))
  13689. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13690. (org-defkey map (kbd "RET") 'org-calendar-select)
  13691. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13692. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13693. (org-defkey minibuffer-local-map [(meta shift left)]
  13694. (lambda () (interactive)
  13695. (org-eval-in-calendar '(calendar-backward-month 1))))
  13696. (org-defkey minibuffer-local-map [(meta shift right)]
  13697. (lambda () (interactive)
  13698. (org-eval-in-calendar '(calendar-forward-month 1))))
  13699. (org-defkey minibuffer-local-map [(meta shift up)]
  13700. (lambda () (interactive)
  13701. (org-eval-in-calendar '(calendar-backward-year 1))))
  13702. (org-defkey minibuffer-local-map [(meta shift down)]
  13703. (lambda () (interactive)
  13704. (org-eval-in-calendar '(calendar-forward-year 1))))
  13705. (org-defkey minibuffer-local-map [?\e (shift left)]
  13706. (lambda () (interactive)
  13707. (org-eval-in-calendar '(calendar-backward-month 1))))
  13708. (org-defkey minibuffer-local-map [?\e (shift right)]
  13709. (lambda () (interactive)
  13710. (org-eval-in-calendar '(calendar-forward-month 1))))
  13711. (org-defkey minibuffer-local-map [?\e (shift up)]
  13712. (lambda () (interactive)
  13713. (org-eval-in-calendar '(calendar-backward-year 1))))
  13714. (org-defkey minibuffer-local-map [?\e (shift down)]
  13715. (lambda () (interactive)
  13716. (org-eval-in-calendar '(calendar-forward-year 1))))
  13717. (org-defkey minibuffer-local-map [(shift up)]
  13718. (lambda () (interactive)
  13719. (org-eval-in-calendar '(calendar-backward-week 1))))
  13720. (org-defkey minibuffer-local-map [(shift down)]
  13721. (lambda () (interactive)
  13722. (org-eval-in-calendar '(calendar-forward-week 1))))
  13723. (org-defkey minibuffer-local-map [(shift left)]
  13724. (lambda () (interactive)
  13725. (org-eval-in-calendar '(calendar-backward-day 1))))
  13726. (org-defkey minibuffer-local-map [(shift right)]
  13727. (lambda () (interactive)
  13728. (org-eval-in-calendar '(calendar-forward-day 1))))
  13729. (org-defkey minibuffer-local-map ">"
  13730. (lambda () (interactive)
  13731. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13732. (org-defkey minibuffer-local-map "<"
  13733. (lambda () (interactive)
  13734. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13735. (org-defkey minibuffer-local-map "\C-v"
  13736. (lambda () (interactive)
  13737. (org-eval-in-calendar
  13738. '(calendar-scroll-left-three-months 1))))
  13739. (org-defkey minibuffer-local-map "\M-v"
  13740. (lambda () (interactive)
  13741. (org-eval-in-calendar
  13742. '(calendar-scroll-right-three-months 1))))
  13743. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13744. (unwind-protect
  13745. (progn
  13746. (use-local-map map)
  13747. (add-hook 'post-command-hook 'org-read-date-display)
  13748. (setq org-ans0 (read-string prompt default-input
  13749. 'org-read-date-history nil))
  13750. ;; org-ans0: from prompt
  13751. ;; org-ans1: from mouse click
  13752. ;; org-ans2: from calendar motion
  13753. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13754. (remove-hook 'post-command-hook 'org-read-date-display)
  13755. (use-local-map old-map)
  13756. (when org-read-date-overlay
  13757. (delete-overlay org-read-date-overlay)
  13758. (setq org-read-date-overlay nil)))))
  13759. (bury-buffer "*Calendar*")))))
  13760. (t ; Naked prompt only
  13761. (unwind-protect
  13762. (setq ans (read-string prompt default-input
  13763. 'org-read-date-history timestr))
  13764. (when org-read-date-overlay
  13765. (delete-overlay org-read-date-overlay)
  13766. (setq org-read-date-overlay nil)))))
  13767. (setq final (org-read-date-analyze ans def defdecode))
  13768. (when org-read-date-analyze-forced-year
  13769. (message "Year was forced into %s"
  13770. (if org-read-date-force-compatible-dates
  13771. "compatible range (1970-2037)"
  13772. "range representable on this machine"))
  13773. (ding))
  13774. ;; One round trip to get rid of 34th of August and stuff like that....
  13775. (setq final (decode-time (apply 'encode-time final)))
  13776. (setq org-read-date-final-answer ans)
  13777. (if to-time
  13778. (apply 'encode-time final)
  13779. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13780. (format "%04d-%02d-%02d %02d:%02d"
  13781. (nth 5 final) (nth 4 final) (nth 3 final)
  13782. (nth 2 final) (nth 1 final))
  13783. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13784. (defvar def)
  13785. (defvar defdecode)
  13786. (defvar with-time)
  13787. (defun org-read-date-display ()
  13788. "Display the current date prompt interpretation in the minibuffer."
  13789. (when org-read-date-display-live
  13790. (when org-read-date-overlay
  13791. (delete-overlay org-read-date-overlay))
  13792. (let ((p (point)))
  13793. (end-of-line 1)
  13794. (while (not (equal (buffer-substring
  13795. (max (point-min) (- (point) 4)) (point))
  13796. " "))
  13797. (insert " "))
  13798. (goto-char p))
  13799. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13800. " " (or org-ans1 org-ans2)))
  13801. (org-end-time-was-given nil)
  13802. (f (org-read-date-analyze ans def defdecode))
  13803. (fmts (if org-dcst
  13804. org-time-stamp-custom-formats
  13805. org-time-stamp-formats))
  13806. (fmt (if (or with-time
  13807. (and (boundp 'org-time-was-given) org-time-was-given))
  13808. (cdr fmts)
  13809. (car fmts)))
  13810. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13811. (when (and org-end-time-was-given
  13812. (string-match org-plain-time-of-day-regexp txt))
  13813. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13814. org-end-time-was-given
  13815. (substring txt (match-end 0)))))
  13816. (when org-read-date-analyze-futurep
  13817. (setq txt (concat txt " (=>F)")))
  13818. (setq org-read-date-overlay
  13819. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13820. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13821. (defun org-read-date-analyze (ans def defdecode)
  13822. "Analyze the combined answer of the date prompt."
  13823. ;; FIXME: cleanup and comment
  13824. (let ((nowdecode (decode-time (current-time)))
  13825. delta deltan deltaw deltadef year month day
  13826. hour minute second wday pm h2 m2 tl wday1
  13827. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13828. (setq org-read-date-analyze-futurep nil
  13829. org-read-date-analyze-forced-year nil)
  13830. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13831. (setq ans "+0"))
  13832. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13833. (setq ans (replace-match "" t t ans)
  13834. deltan (car delta)
  13835. deltaw (nth 1 delta)
  13836. deltadef (nth 2 delta)))
  13837. ;; Check if there is an iso week date in there
  13838. ;; If yes, store the info and postpone interpreting it until the rest
  13839. ;; of the parsing is done
  13840. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13841. (setq iso-year (if (match-end 1)
  13842. (org-small-year-to-year
  13843. (string-to-number (match-string 1 ans))))
  13844. iso-weekday (if (match-end 3)
  13845. (string-to-number (match-string 3 ans)))
  13846. iso-week (string-to-number (match-string 2 ans)))
  13847. (setq ans (replace-match "" t t ans)))
  13848. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13849. (when (string-match
  13850. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13851. (setq year (if (match-end 2)
  13852. (string-to-number (match-string 2 ans))
  13853. (progn (setq kill-year t)
  13854. (string-to-number (format-time-string "%Y"))))
  13855. month (string-to-number (match-string 3 ans))
  13856. day (string-to-number (match-string 4 ans)))
  13857. (if (< year 100) (setq year (+ 2000 year)))
  13858. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13859. t nil ans)))
  13860. ;; Help matching dotted european dates
  13861. (when (string-match
  13862. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13863. (setq year (if (match-end 3)
  13864. (string-to-number (match-string 3 ans))
  13865. (progn (setq kill-year t)
  13866. (string-to-number (format-time-string "%Y"))))
  13867. day (string-to-number (match-string 1 ans))
  13868. month (string-to-number (match-string 2 ans))
  13869. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13870. t nil ans)))
  13871. ;; Help matching american dates, like 5/30 or 5/30/7
  13872. (when (string-match
  13873. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13874. (setq year (if (match-end 4)
  13875. (string-to-number (match-string 4 ans))
  13876. (progn (setq kill-year t)
  13877. (string-to-number (format-time-string "%Y"))))
  13878. month (string-to-number (match-string 1 ans))
  13879. day (string-to-number (match-string 2 ans)))
  13880. (if (< year 100) (setq year (+ 2000 year)))
  13881. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13882. t nil ans)))
  13883. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13884. ;; If there is a time with am/pm, and *no* time without it, we convert
  13885. ;; so that matching will be successful.
  13886. (loop for i from 1 to 2 do ; twice, for end time as well
  13887. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13888. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13889. (setq hour (string-to-number (match-string 1 ans))
  13890. minute (if (match-end 3)
  13891. (string-to-number (match-string 3 ans))
  13892. 0)
  13893. pm (equal ?p
  13894. (string-to-char (downcase (match-string 4 ans)))))
  13895. (if (and (= hour 12) (not pm))
  13896. (setq hour 0)
  13897. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13898. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13899. t t ans))))
  13900. ;; Check if a time range is given as a duration
  13901. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13902. (setq hour (string-to-number (match-string 1 ans))
  13903. h2 (+ hour (string-to-number (match-string 3 ans)))
  13904. minute (string-to-number (match-string 2 ans))
  13905. m2 (+ minute (if (match-end 5) (string-to-number
  13906. (match-string 5 ans))0)))
  13907. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13908. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13909. t t ans)))
  13910. ;; Check if there is a time range
  13911. (when (boundp 'org-end-time-was-given)
  13912. (setq org-time-was-given nil)
  13913. (when (and (string-match org-plain-time-of-day-regexp ans)
  13914. (match-end 8))
  13915. (setq org-end-time-was-given (match-string 8 ans))
  13916. (setq ans (concat (substring ans 0 (match-beginning 7))
  13917. (substring ans (match-end 7))))))
  13918. (setq tl (parse-time-string ans)
  13919. day (or (nth 3 tl) (nth 3 defdecode))
  13920. month (or (nth 4 tl)
  13921. (if (and org-read-date-prefer-future
  13922. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13923. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13924. (nth 4 defdecode)))
  13925. year (or (and (not kill-year) (nth 5 tl))
  13926. (if (and org-read-date-prefer-future
  13927. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13928. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13929. (nth 5 defdecode)))
  13930. hour (or (nth 2 tl) (nth 2 defdecode))
  13931. minute (or (nth 1 tl) (nth 1 defdecode))
  13932. second (or (nth 0 tl) 0)
  13933. wday (nth 6 tl))
  13934. (when (and (eq org-read-date-prefer-future 'time)
  13935. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13936. (equal day (nth 3 nowdecode))
  13937. (equal month (nth 4 nowdecode))
  13938. (equal year (nth 5 nowdecode))
  13939. (nth 2 tl)
  13940. (or (< (nth 2 tl) (nth 2 nowdecode))
  13941. (and (= (nth 2 tl) (nth 2 nowdecode))
  13942. (nth 1 tl)
  13943. (< (nth 1 tl) (nth 1 nowdecode)))))
  13944. (setq day (1+ day)
  13945. futurep t))
  13946. ;; Special date definitions below
  13947. (cond
  13948. (iso-week
  13949. ;; There was an iso week
  13950. (require 'cal-iso)
  13951. (setq futurep nil)
  13952. (setq year (or iso-year year)
  13953. day (or iso-weekday wday 1)
  13954. wday nil ; to make sure that the trigger below does not match
  13955. iso-date (calendar-gregorian-from-absolute
  13956. (calendar-absolute-from-iso
  13957. (list iso-week day year))))
  13958. ; FIXME: Should we also push ISO weeks into the future?
  13959. ; (when (and org-read-date-prefer-future
  13960. ; (not iso-year)
  13961. ; (< (calendar-absolute-from-gregorian iso-date)
  13962. ; (time-to-days (current-time))))
  13963. ; (setq year (1+ year)
  13964. ; iso-date (calendar-gregorian-from-absolute
  13965. ; (calendar-absolute-from-iso
  13966. ; (list iso-week day year)))))
  13967. (setq month (car iso-date)
  13968. year (nth 2 iso-date)
  13969. day (nth 1 iso-date)))
  13970. (deltan
  13971. (setq futurep nil)
  13972. (unless deltadef
  13973. (let ((now (decode-time (current-time))))
  13974. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13975. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13976. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13977. ((equal deltaw "m") (setq month (+ month deltan)))
  13978. ((equal deltaw "y") (setq year (+ year deltan)))))
  13979. ((and wday (not (nth 3 tl)))
  13980. (setq futurep nil)
  13981. ;; Weekday was given, but no day, so pick that day in the week
  13982. ;; on or after the derived date.
  13983. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13984. (unless (equal wday wday1)
  13985. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13986. (if (and (boundp 'org-time-was-given)
  13987. (nth 2 tl))
  13988. (setq org-time-was-given t))
  13989. (if (< year 100) (setq year (+ 2000 year)))
  13990. ;; Check of the date is representable
  13991. (if org-read-date-force-compatible-dates
  13992. (progn
  13993. (if (< year 1970)
  13994. (setq year 1970 org-read-date-analyze-forced-year t))
  13995. (if (> year 2037)
  13996. (setq year 2037 org-read-date-analyze-forced-year t)))
  13997. (condition-case nil
  13998. (ignore (encode-time second minute hour day month year))
  13999. (error
  14000. (setq year (nth 5 defdecode))
  14001. (setq org-read-date-analyze-forced-year t))))
  14002. (setq org-read-date-analyze-futurep futurep)
  14003. (list second minute hour day month year)))
  14004. (defvar parse-time-weekdays)
  14005. (defun org-read-date-get-relative (s today default)
  14006. "Check string S for special relative date string.
  14007. TODAY and DEFAULT are internal times, for today and for a default.
  14008. Return shift list (N what def-flag)
  14009. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14010. N is the number of WHATs to shift.
  14011. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14012. the DEFAULT date rather than TODAY."
  14013. (require 'parse-time)
  14014. (when (and
  14015. (string-match
  14016. (concat
  14017. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14018. "\\([0-9]+\\)?"
  14019. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14020. "\\([ \t]\\|$\\)") s)
  14021. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14022. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14023. (string-to-char (substring (match-string 1 s) -1))
  14024. ?+))
  14025. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14026. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14027. (what (if (match-end 3) (match-string 3 s) "d"))
  14028. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14029. (date (if rel default today))
  14030. (wday (nth 6 (decode-time date)))
  14031. delta)
  14032. (if wday1
  14033. (progn
  14034. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14035. (if (= dir ?-) (setq delta (- delta 7)))
  14036. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14037. (list delta "d" rel))
  14038. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14039. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14040. "Turn a user-specified date into the internal representation.
  14041. The internal representation needed by the calendar is (month day year).
  14042. This is a wrapper to handle the brain-dead convention in calendar that
  14043. user function argument order change dependent on argument order."
  14044. (if (boundp 'calendar-date-style)
  14045. (cond
  14046. ((eq calendar-date-style 'american)
  14047. (list arg1 arg2 arg3))
  14048. ((eq calendar-date-style 'european)
  14049. (list arg2 arg1 arg3))
  14050. ((eq calendar-date-style 'iso)
  14051. (list arg2 arg3 arg1)))
  14052. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14053. (if (org-bound-and-true-p european-calendar-style)
  14054. (list arg2 arg1 arg3)
  14055. (list arg1 arg2 arg3)))))
  14056. (defun org-eval-in-calendar (form &optional keepdate)
  14057. "Eval FORM in the calendar window and return to current window.
  14058. Also, store the cursor date in variable org-ans2."
  14059. (let ((sf (selected-frame))
  14060. (sw (selected-window)))
  14061. (select-window (get-buffer-window "*Calendar*" t))
  14062. (eval form)
  14063. (when (and (not keepdate) (calendar-cursor-to-date))
  14064. (let* ((date (calendar-cursor-to-date))
  14065. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14066. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14067. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14068. (select-window sw)
  14069. (org-select-frame-set-input-focus sf)))
  14070. (defun org-calendar-select ()
  14071. "Return to `org-read-date' with the date currently selected.
  14072. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14073. (interactive)
  14074. (when (calendar-cursor-to-date)
  14075. (let* ((date (calendar-cursor-to-date))
  14076. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14077. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14078. (if (active-minibuffer-window) (exit-minibuffer))))
  14079. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14080. "Insert a date stamp for the date given by the internal TIME.
  14081. WITH-HM means use the stamp format that includes the time of the day.
  14082. INACTIVE means use square brackets instead of angular ones, so that the
  14083. stamp will not contribute to the agenda.
  14084. PRE and POST are optional strings to be inserted before and after the
  14085. stamp.
  14086. The command returns the inserted time stamp."
  14087. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14088. stamp)
  14089. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14090. (insert-before-markers (or pre ""))
  14091. (when (listp extra)
  14092. (setq extra (car extra))
  14093. (if (and (stringp extra)
  14094. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14095. (setq extra (format "-%02d:%02d"
  14096. (string-to-number (match-string 1 extra))
  14097. (string-to-number (match-string 2 extra))))
  14098. (setq extra nil)))
  14099. (when extra
  14100. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14101. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14102. (insert-before-markers (or post ""))
  14103. (setq org-last-inserted-timestamp stamp)))
  14104. (defun org-toggle-time-stamp-overlays ()
  14105. "Toggle the use of custom time stamp formats."
  14106. (interactive)
  14107. (setq org-display-custom-times (not org-display-custom-times))
  14108. (unless org-display-custom-times
  14109. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14110. (while (setq p (next-single-property-change p 'display))
  14111. (if (and (get-text-property p 'display)
  14112. (eq (get-text-property p 'face) 'org-date))
  14113. (remove-text-properties
  14114. p (setq p (next-single-property-change p 'display))
  14115. '(display t))))
  14116. (set-buffer-modified-p bmp)))
  14117. (if (featurep 'xemacs)
  14118. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14119. (org-restart-font-lock)
  14120. (setq org-table-may-need-update t)
  14121. (if org-display-custom-times
  14122. (message "Time stamps are overlaid with custom format")
  14123. (message "Time stamp overlays removed")))
  14124. (defun org-display-custom-time (beg end)
  14125. "Overlay modified time stamp format over timestamp between BEG and END."
  14126. (let* ((ts (buffer-substring beg end))
  14127. t1 w1 with-hm tf time str w2 (off 0))
  14128. (save-match-data
  14129. (setq t1 (org-parse-time-string ts t))
  14130. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14131. (setq off (- (match-end 0) (match-beginning 0)))))
  14132. (setq end (- end off))
  14133. (setq w1 (- end beg)
  14134. with-hm (and (nth 1 t1) (nth 2 t1))
  14135. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14136. time (org-fix-decoded-time t1)
  14137. str (org-add-props
  14138. (format-time-string
  14139. (substring tf 1 -1) (apply 'encode-time time))
  14140. nil 'mouse-face 'highlight)
  14141. w2 (length str))
  14142. (if (not (= w2 w1))
  14143. (add-text-properties (1+ beg) (+ 2 beg)
  14144. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14145. (if (featurep 'xemacs)
  14146. (progn
  14147. (put-text-property beg end 'invisible t)
  14148. (put-text-property beg end 'end-glyph (make-glyph str)))
  14149. (put-text-property beg end 'display str))))
  14150. (defun org-translate-time (string)
  14151. "Translate all timestamps in STRING to custom format.
  14152. But do this only if the variable `org-display-custom-times' is set."
  14153. (when org-display-custom-times
  14154. (save-match-data
  14155. (let* ((start 0)
  14156. (re org-ts-regexp-both)
  14157. t1 with-hm inactive tf time str beg end)
  14158. (while (setq start (string-match re string start))
  14159. (setq beg (match-beginning 0)
  14160. end (match-end 0)
  14161. t1 (save-match-data
  14162. (org-parse-time-string (substring string beg end) t))
  14163. with-hm (and (nth 1 t1) (nth 2 t1))
  14164. inactive (equal (substring string beg (1+ beg)) "[")
  14165. tf (funcall (if with-hm 'cdr 'car)
  14166. org-time-stamp-custom-formats)
  14167. time (org-fix-decoded-time t1)
  14168. str (format-time-string
  14169. (concat
  14170. (if inactive "[" "<") (substring tf 1 -1)
  14171. (if inactive "]" ">"))
  14172. (apply 'encode-time time))
  14173. string (replace-match str t t string)
  14174. start (+ start (length str)))))))
  14175. string)
  14176. (defun org-fix-decoded-time (time)
  14177. "Set 0 instead of nil for the first 6 elements of time.
  14178. Don't touch the rest."
  14179. (let ((n 0))
  14180. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14181. (defun org-days-to-time (timestamp-string)
  14182. "Difference between TIMESTAMP-STRING and now in days."
  14183. (- (time-to-days (org-time-string-to-time timestamp-string))
  14184. (time-to-days (current-time))))
  14185. (defun org-deadline-close (timestamp-string &optional ndays)
  14186. "Is the time in TIMESTAMP-STRING close to the current date?"
  14187. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14188. (and (< (org-days-to-time timestamp-string) ndays)
  14189. (not (org-entry-is-done-p))))
  14190. (defun org-get-wdays (ts)
  14191. "Get the deadline lead time appropriate for timestring TS."
  14192. (cond
  14193. ((<= org-deadline-warning-days 0)
  14194. ;; 0 or negative, enforce this value no matter what
  14195. (- org-deadline-warning-days))
  14196. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14197. ;; lead time is specified.
  14198. (floor (* (string-to-number (match-string 1 ts))
  14199. (cdr (assoc (match-string 2 ts)
  14200. '(("d" . 1) ("w" . 7)
  14201. ("m" . 30.4) ("y" . 365.25)))))))
  14202. ;; go for the default.
  14203. (t org-deadline-warning-days)))
  14204. (defun org-calendar-select-mouse (ev)
  14205. "Return to `org-read-date' with the date currently selected.
  14206. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14207. (interactive "e")
  14208. (mouse-set-point ev)
  14209. (when (calendar-cursor-to-date)
  14210. (let* ((date (calendar-cursor-to-date))
  14211. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14212. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14213. (if (active-minibuffer-window) (exit-minibuffer))))
  14214. (defun org-check-deadlines (ndays)
  14215. "Check if there are any deadlines due or past due.
  14216. A deadline is considered due if it happens within `org-deadline-warning-days'
  14217. days from today's date. If the deadline appears in an entry marked DONE,
  14218. it is not shown. The prefix arg NDAYS can be used to test that many
  14219. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14220. (interactive "P")
  14221. (let* ((org-warn-days
  14222. (cond
  14223. ((equal ndays '(4)) 100000)
  14224. (ndays (prefix-numeric-value ndays))
  14225. (t (abs org-deadline-warning-days))))
  14226. (case-fold-search nil)
  14227. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14228. (callback
  14229. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14230. (message "%d deadlines past-due or due within %d days"
  14231. (org-occur regexp nil callback)
  14232. org-warn-days)))
  14233. (defun org-check-before-date (date)
  14234. "Check if there are deadlines or scheduled entries before DATE."
  14235. (interactive (list (org-read-date)))
  14236. (let ((case-fold-search nil)
  14237. (regexp (concat "\\<\\(" org-deadline-string
  14238. "\\|" org-scheduled-string
  14239. "\\) *<\\([^>]+\\)>"))
  14240. (callback
  14241. (lambda () (time-less-p
  14242. (org-time-string-to-time (match-string 2))
  14243. (org-time-string-to-time date)))))
  14244. (message "%d entries before %s"
  14245. (org-occur regexp nil callback) date)))
  14246. (defun org-check-after-date (date)
  14247. "Check if there are deadlines or scheduled entries after DATE."
  14248. (interactive (list (org-read-date)))
  14249. (let ((case-fold-search nil)
  14250. (regexp (concat "\\<\\(" org-deadline-string
  14251. "\\|" org-scheduled-string
  14252. "\\) *<\\([^>]+\\)>"))
  14253. (callback
  14254. (lambda () (not
  14255. (time-less-p
  14256. (org-time-string-to-time (match-string 2))
  14257. (org-time-string-to-time date))))))
  14258. (message "%d entries after %s"
  14259. (org-occur regexp nil callback) date)))
  14260. (defun org-check-dates-range (start-date end-date)
  14261. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14262. (interactive (list (org-read-date nil nil nil "Range starts")
  14263. (org-read-date nil nil nil "Range end")))
  14264. (let ((case-fold-search nil)
  14265. (regexp (concat "\\<\\(" org-deadline-string
  14266. "\\|" org-scheduled-string
  14267. "\\) *<\\([^>]+\\)>"))
  14268. (callback
  14269. (lambda ()
  14270. (let ((match (match-string 2)))
  14271. (and
  14272. (not (time-less-p
  14273. (org-time-string-to-time match)
  14274. (org-time-string-to-time start-date)))
  14275. (time-less-p
  14276. (org-time-string-to-time match)
  14277. (org-time-string-to-time end-date)))))))
  14278. (message "%d entries between %s and %s"
  14279. (org-occur regexp nil callback) start-date end-date)))
  14280. (defun org-evaluate-time-range (&optional to-buffer)
  14281. "Evaluate a time range by computing the difference between start and end.
  14282. Normally the result is just printed in the echo area, but with prefix arg
  14283. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14284. If the time range is actually in a table, the result is inserted into the
  14285. next column.
  14286. For time difference computation, a year is assumed to be exactly 365
  14287. days in order to avoid rounding problems."
  14288. (interactive "P")
  14289. (or
  14290. (org-clock-update-time-maybe)
  14291. (save-excursion
  14292. (unless (org-at-date-range-p t)
  14293. (goto-char (point-at-bol))
  14294. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14295. (if (not (org-at-date-range-p t))
  14296. (error "Not at a time-stamp range, and none found in current line")))
  14297. (let* ((ts1 (match-string 1))
  14298. (ts2 (match-string 2))
  14299. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14300. (match-end (match-end 0))
  14301. (time1 (org-time-string-to-time ts1))
  14302. (time2 (org-time-string-to-time ts2))
  14303. (t1 (org-float-time time1))
  14304. (t2 (org-float-time time2))
  14305. (diff (abs (- t2 t1)))
  14306. (negative (< (- t2 t1) 0))
  14307. ;; (ys (floor (* 365 24 60 60)))
  14308. (ds (* 24 60 60))
  14309. (hs (* 60 60))
  14310. (fy "%dy %dd %02d:%02d")
  14311. (fy1 "%dy %dd")
  14312. (fd "%dd %02d:%02d")
  14313. (fd1 "%dd")
  14314. (fh "%02d:%02d")
  14315. y d h m align)
  14316. (if havetime
  14317. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14318. y 0
  14319. d (floor (/ diff ds)) diff (mod diff ds)
  14320. h (floor (/ diff hs)) diff (mod diff hs)
  14321. m (floor (/ diff 60)))
  14322. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14323. y 0
  14324. d (floor (+ (/ diff ds) 0.5))
  14325. h 0 m 0))
  14326. (if (not to-buffer)
  14327. (message "%s" (org-make-tdiff-string y d h m))
  14328. (if (org-at-table-p)
  14329. (progn
  14330. (goto-char match-end)
  14331. (setq align t)
  14332. (and (looking-at " *|") (goto-char (match-end 0))))
  14333. (goto-char match-end))
  14334. (if (looking-at
  14335. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14336. (replace-match ""))
  14337. (if negative (insert " -"))
  14338. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14339. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14340. (insert " " (format fh h m))))
  14341. (if align (org-table-align))
  14342. (message "Time difference inserted")))))
  14343. (defun org-make-tdiff-string (y d h m)
  14344. (let ((fmt "")
  14345. (l nil))
  14346. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14347. l (push y l)))
  14348. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14349. l (push d l)))
  14350. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14351. l (push h l)))
  14352. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14353. l (push m l)))
  14354. (apply 'format fmt (nreverse l))))
  14355. (defun org-time-string-to-time (s &optional buffer pos)
  14356. (condition-case errdata
  14357. (apply 'encode-time (org-parse-time-string s))
  14358. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14359. s (if (not (and buffer pos))
  14360. ""
  14361. (format " at %d in buffer `%s'" pos buffer))
  14362. (cdr errdata)))))
  14363. (defun org-time-string-to-seconds (s)
  14364. (org-float-time (org-time-string-to-time s)))
  14365. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14366. "Convert a time stamp to an absolute day number.
  14367. If there is a specifier for a cyclic time stamp, get the closest date to
  14368. DAYNR.
  14369. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14370. The variable date is bound by the calendar when this is called."
  14371. (cond
  14372. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14373. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14374. daynr
  14375. (+ daynr 1000)))
  14376. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14377. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14378. (time-to-days (current-time))) (match-string 0 s)
  14379. prefer show-all))
  14380. (t (time-to-days
  14381. (condition-case errdata
  14382. (apply 'encode-time (org-parse-time-string s))
  14383. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14384. s (if (not (and buffer pos))
  14385. ""
  14386. (format " at %d in buffer `%s'" pos buffer))
  14387. (cdr errdata))))))))
  14388. (defun org-days-to-iso-week (days)
  14389. "Return the iso week number."
  14390. (require 'cal-iso)
  14391. (car (calendar-iso-from-absolute days)))
  14392. (defun org-small-year-to-year (year)
  14393. "Convert 2-digit years into 4-digit years.
  14394. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14395. The year 2000 cannot be abbreviated. Any year larger than 99
  14396. is returned unchanged."
  14397. (if (< year 38)
  14398. (setq year (+ 2000 year))
  14399. (if (< year 100)
  14400. (setq year (+ 1900 year))))
  14401. year)
  14402. (defun org-time-from-absolute (d)
  14403. "Return the time corresponding to date D.
  14404. D may be an absolute day number, or a calendar-type list (month day year)."
  14405. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14406. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14407. (defun org-calendar-holiday ()
  14408. "List of holidays, for Diary display in Org-mode."
  14409. (require 'holidays)
  14410. (let ((hl (funcall
  14411. (if (fboundp 'calendar-check-holidays)
  14412. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14413. (if hl (mapconcat 'identity hl "; "))))
  14414. (defun org-diary-sexp-entry (sexp entry date)
  14415. "Process a SEXP diary ENTRY for DATE."
  14416. (require 'diary-lib)
  14417. (let ((result (if calendar-debug-sexp
  14418. (let ((stack-trace-on-error t))
  14419. (eval (car (read-from-string sexp))))
  14420. (condition-case nil
  14421. (eval (car (read-from-string sexp)))
  14422. (error
  14423. (beep)
  14424. (message "Bad sexp at line %d in %s: %s"
  14425. (org-current-line)
  14426. (buffer-file-name) sexp)
  14427. (sleep-for 2))))))
  14428. (cond ((stringp result) (split-string result "; "))
  14429. ((and (consp result)
  14430. (not (consp (cdr result)))
  14431. (stringp (cdr result))) (cdr result))
  14432. ((and (consp result)
  14433. (stringp (car result))) result)
  14434. (result entry)
  14435. (t nil))))
  14436. (defun org-diary-to-ical-string (frombuf)
  14437. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14438. This uses the icalendar.el library."
  14439. (let* ((tmpdir (if (featurep 'xemacs)
  14440. (temp-directory)
  14441. temporary-file-directory))
  14442. (tmpfile (make-temp-name
  14443. (expand-file-name "orgics" tmpdir)))
  14444. buf rtn b e)
  14445. (with-current-buffer frombuf
  14446. (icalendar-export-region (point-min) (point-max) tmpfile)
  14447. (setq buf (find-buffer-visiting tmpfile))
  14448. (set-buffer buf)
  14449. (goto-char (point-min))
  14450. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14451. (setq b (match-beginning 0)))
  14452. (goto-char (point-max))
  14453. (if (re-search-backward "^END:VEVENT" nil t)
  14454. (setq e (match-end 0)))
  14455. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14456. (kill-buffer buf)
  14457. (delete-file tmpfile)
  14458. rtn))
  14459. (defun org-closest-date (start current change prefer show-all)
  14460. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14461. When PREFER is `past', return a date that is either CURRENT or past.
  14462. When PREFER is `future', return a date that is either CURRENT or future.
  14463. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14464. ;; Make the proper lists from the dates
  14465. (catch 'exit
  14466. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14467. dn dw sday cday n1 n2 n0
  14468. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14469. (setq start (org-date-to-gregorian start)
  14470. current (org-date-to-gregorian
  14471. (if show-all
  14472. current
  14473. (time-to-days (current-time))))
  14474. sday (calendar-absolute-from-gregorian start)
  14475. cday (calendar-absolute-from-gregorian current))
  14476. (if (<= cday sday) (throw 'exit sday))
  14477. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14478. (setq dn (string-to-number (match-string 1 change))
  14479. dw (cdr (assoc (match-string 2 change) a1)))
  14480. (error "Invalid change specifier: %s" change))
  14481. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14482. (cond
  14483. ((eq dw 'day)
  14484. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14485. n2 (+ n1 dn)))
  14486. ((eq dw 'year)
  14487. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14488. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14489. (setq date1 (list m d y1)
  14490. n1 (calendar-absolute-from-gregorian date1)
  14491. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14492. n2 (calendar-absolute-from-gregorian date2)))
  14493. ((eq dw 'month)
  14494. ;; approx number of month between the two dates
  14495. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14496. ;; How often does dn fit in there?
  14497. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14498. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14499. m (+ m nm)
  14500. ny (floor (/ m 12))
  14501. y (+ y ny)
  14502. m (- m (* ny 12)))
  14503. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14504. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14505. (setq m2 (+ m dn) y2 y)
  14506. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14507. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14508. (while (<= n2 cday)
  14509. (setq n1 n2 m m2 y y2)
  14510. (setq m2 (+ m dn) y2 y)
  14511. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14512. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14513. ;; Make sure n1 is the earlier date
  14514. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14515. (if show-all
  14516. (cond
  14517. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14518. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14519. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14520. (cond
  14521. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14522. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14523. (t (if (= cday n1) n1 n2)))))))
  14524. (defun org-date-to-gregorian (date)
  14525. "Turn any specification of DATE into a Gregorian date for the calendar."
  14526. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14527. ((and (listp date) (= (length date) 3)) date)
  14528. ((stringp date)
  14529. (setq date (org-parse-time-string date))
  14530. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14531. ((listp date)
  14532. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14533. (defun org-parse-time-string (s &optional nodefault)
  14534. "Parse the standard Org-mode time string.
  14535. This should be a lot faster than the normal `parse-time-string'.
  14536. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14537. hour and minute fields will be nil if not given."
  14538. (if (string-match org-ts-regexp0 s)
  14539. (list 0
  14540. (if (or (match-beginning 8) (not nodefault))
  14541. (string-to-number (or (match-string 8 s) "0")))
  14542. (if (or (match-beginning 7) (not nodefault))
  14543. (string-to-number (or (match-string 7 s) "0")))
  14544. (string-to-number (match-string 4 s))
  14545. (string-to-number (match-string 3 s))
  14546. (string-to-number (match-string 2 s))
  14547. nil nil nil)
  14548. (error "Not a standard Org-mode time string: %s" s)))
  14549. (defun org-timestamp-up (&optional arg)
  14550. "Increase the date item at the cursor by one.
  14551. If the cursor is on the year, change the year. If it is on the month,
  14552. the day or the time, change that.
  14553. With prefix ARG, change by that many units."
  14554. (interactive "p")
  14555. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14556. (defun org-timestamp-down (&optional arg)
  14557. "Decrease the date item at the cursor by one.
  14558. If the cursor is on the year, change the year. If it is on the month,
  14559. the day or the time, change that.
  14560. With prefix ARG, change by that many units."
  14561. (interactive "p")
  14562. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14563. (defun org-timestamp-up-day (&optional arg)
  14564. "Increase the date in the time stamp by one day.
  14565. With prefix ARG, change that many days."
  14566. (interactive "p")
  14567. (if (and (not (org-at-timestamp-p t))
  14568. (org-at-heading-p))
  14569. (org-todo 'up)
  14570. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14571. (defun org-timestamp-down-day (&optional arg)
  14572. "Decrease the date in the time stamp by one day.
  14573. With prefix ARG, change that many days."
  14574. (interactive "p")
  14575. (if (and (not (org-at-timestamp-p t))
  14576. (org-at-heading-p))
  14577. (org-todo 'down)
  14578. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14579. (defun org-at-timestamp-p (&optional inactive-ok)
  14580. "Determine if the cursor is in or at a timestamp."
  14581. (interactive)
  14582. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14583. (pos (point))
  14584. (ans (or (looking-at tsr)
  14585. (save-excursion
  14586. (skip-chars-backward "^[<\n\r\t")
  14587. (if (> (point) (point-min)) (backward-char 1))
  14588. (and (looking-at tsr)
  14589. (> (- (match-end 0) pos) -1))))))
  14590. (and ans
  14591. (boundp 'org-ts-what)
  14592. (setq org-ts-what
  14593. (cond
  14594. ((= pos (match-beginning 0)) 'bracket)
  14595. ;; Point is considered to be "on the bracket" whether
  14596. ;; it's really on it or right after it.
  14597. ((or (= pos (1- (match-end 0)))
  14598. (= pos (match-end 0))) 'bracket)
  14599. ((org-pos-in-match-range pos 2) 'year)
  14600. ((org-pos-in-match-range pos 3) 'month)
  14601. ((org-pos-in-match-range pos 7) 'hour)
  14602. ((org-pos-in-match-range pos 8) 'minute)
  14603. ((or (org-pos-in-match-range pos 4)
  14604. (org-pos-in-match-range pos 5)) 'day)
  14605. ((and (> pos (or (match-end 8) (match-end 5)))
  14606. (< pos (match-end 0)))
  14607. (- pos (or (match-end 8) (match-end 5))))
  14608. (t 'day))))
  14609. ans))
  14610. (defun org-toggle-timestamp-type ()
  14611. "Toggle the type (<active> or [inactive]) of a time stamp."
  14612. (interactive)
  14613. (when (org-at-timestamp-p t)
  14614. (let ((beg (match-beginning 0)) (end (match-end 0))
  14615. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14616. (save-excursion
  14617. (goto-char beg)
  14618. (while (re-search-forward "[][<>]" end t)
  14619. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14620. t t)))
  14621. (message "Timestamp is now %sactive"
  14622. (if (equal (char-after beg) ?<) "" "in")))))
  14623. (defun org-timestamp-change (n &optional what updown)
  14624. "Change the date in the time stamp at point.
  14625. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14626. `year', `minute', `second'. If WHAT is not given, the cursor position
  14627. in the timestamp determines what will be changed."
  14628. (let ((origin (point)) origin-cat
  14629. with-hm inactive
  14630. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14631. org-ts-what
  14632. extra rem
  14633. ts time time0)
  14634. (if (not (org-at-timestamp-p t))
  14635. (error "Not at a timestamp"))
  14636. (if (and (not what) (eq org-ts-what 'bracket))
  14637. (org-toggle-timestamp-type)
  14638. ;; Point isn't on brackets. Remember the part of the time-stamp
  14639. ;; the point was in. Indeed, size of time-stamps may change,
  14640. ;; but point must be kept in the same category nonetheless.
  14641. (setq origin-cat org-ts-what)
  14642. (if (and (not what) (not (eq org-ts-what 'day))
  14643. org-display-custom-times
  14644. (get-text-property (point) 'display)
  14645. (not (get-text-property (1- (point)) 'display)))
  14646. (setq org-ts-what 'day))
  14647. (setq org-ts-what (or what org-ts-what)
  14648. inactive (= (char-after (match-beginning 0)) ?\[)
  14649. ts (match-string 0))
  14650. (replace-match "")
  14651. (if (string-match
  14652. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14653. ts)
  14654. (setq extra (match-string 1 ts)))
  14655. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14656. (setq with-hm t))
  14657. (setq time0 (org-parse-time-string ts))
  14658. (when (and updown
  14659. (eq org-ts-what 'minute)
  14660. (not current-prefix-arg))
  14661. ;; This looks like s-up and s-down. Change by one rounding step.
  14662. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14663. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14664. (setcar (cdr time0) (+ (nth 1 time0)
  14665. (if (> n 0) (- rem) (- dm rem))))))
  14666. (setq time
  14667. (encode-time (or (car time0) 0)
  14668. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14669. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14670. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14671. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14672. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14673. (nthcdr 6 time0)))
  14674. (when (and (member org-ts-what '(hour minute))
  14675. extra
  14676. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14677. (setq extra (org-modify-ts-extra
  14678. extra
  14679. (if (eq org-ts-what 'hour) 2 5)
  14680. n dm)))
  14681. (when (integerp org-ts-what)
  14682. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14683. (if (eq what 'calendar)
  14684. (let ((cal-date (org-get-date-from-calendar)))
  14685. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14686. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14687. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14688. (setcar time0 (or (car time0) 0))
  14689. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14690. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14691. (setq time (apply 'encode-time time0))))
  14692. ;; Insert the new time-stamp, and ensure point stays in the same
  14693. ;; category as before (i.e. not after the last position in that
  14694. ;; category).
  14695. (let ((pos (point)))
  14696. ;; Stay before inserted string. `save-excursion' is of no use.
  14697. (setq org-last-changed-timestamp
  14698. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14699. (goto-char pos))
  14700. (save-match-data
  14701. (looking-at org-ts-regexp3)
  14702. (goto-char (cond
  14703. ;; `day' category ends before `hour' if any, or at
  14704. ;; the end of the day name.
  14705. ((eq origin-cat 'day)
  14706. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14707. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14708. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14709. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14710. ;; `year' and `month' have both fixed size: point
  14711. ;; couldn't have moved into another part.
  14712. (t origin))))
  14713. ;; Update clock if on a CLOCK line.
  14714. (org-clock-update-time-maybe)
  14715. ;; Try to recenter the calendar window, if any.
  14716. (if (and org-calendar-follow-timestamp-change
  14717. (get-buffer-window "*Calendar*" t)
  14718. (memq org-ts-what '(day month year)))
  14719. (org-recenter-calendar (time-to-days time))))))
  14720. (defun org-modify-ts-extra (s pos n dm)
  14721. "Change the different parts of the lead-time and repeat fields in timestamp."
  14722. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14723. ng h m new rem)
  14724. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14725. (cond
  14726. ((or (org-pos-in-match-range pos 2)
  14727. (org-pos-in-match-range pos 3))
  14728. (setq m (string-to-number (match-string 3 s))
  14729. h (string-to-number (match-string 2 s)))
  14730. (if (org-pos-in-match-range pos 2)
  14731. (setq h (+ h n))
  14732. (setq n (* dm (org-no-warnings (signum n))))
  14733. (when (not (= 0 (setq rem (% m dm))))
  14734. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14735. (setq m (+ m n)))
  14736. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14737. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14738. (setq h (min 24 (max 0 h)))
  14739. (setq ng 1 new (format "-%02d:%02d" h m)))
  14740. ((org-pos-in-match-range pos 6)
  14741. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14742. ((org-pos-in-match-range pos 5)
  14743. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14744. ((org-pos-in-match-range pos 9)
  14745. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14746. ((org-pos-in-match-range pos 8)
  14747. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14748. (when ng
  14749. (setq s (concat
  14750. (substring s 0 (match-beginning ng))
  14751. new
  14752. (substring s (match-end ng))))))
  14753. s))
  14754. (defun org-recenter-calendar (date)
  14755. "If the calendar is visible, recenter it to DATE."
  14756. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14757. (when cwin
  14758. (let ((calendar-move-hook nil))
  14759. (with-selected-window cwin
  14760. (calendar-goto-date (if (listp date) date
  14761. (calendar-gregorian-from-absolute date))))))))
  14762. (defun org-goto-calendar (&optional arg)
  14763. "Go to the Emacs calendar at the current date.
  14764. If there is a time stamp in the current line, go to that date.
  14765. A prefix ARG can be used to force the current date."
  14766. (interactive "P")
  14767. (let ((tsr org-ts-regexp) diff
  14768. (calendar-move-hook nil)
  14769. (calendar-view-holidays-initially-flag nil)
  14770. (calendar-view-diary-initially-flag nil))
  14771. (if (or (org-at-timestamp-p)
  14772. (save-excursion
  14773. (beginning-of-line 1)
  14774. (looking-at (concat ".*" tsr))))
  14775. (let ((d1 (time-to-days (current-time)))
  14776. (d2 (time-to-days
  14777. (org-time-string-to-time (match-string 1)))))
  14778. (setq diff (- d2 d1))))
  14779. (calendar)
  14780. (calendar-goto-today)
  14781. (if (and diff (not arg)) (calendar-forward-day diff))))
  14782. (defun org-get-date-from-calendar ()
  14783. "Return a list (month day year) of date at point in calendar."
  14784. (with-current-buffer "*Calendar*"
  14785. (save-match-data
  14786. (calendar-cursor-to-date))))
  14787. (defun org-date-from-calendar ()
  14788. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14789. If there is already a time stamp at the cursor position, update it."
  14790. (interactive)
  14791. (if (org-at-timestamp-p t)
  14792. (org-timestamp-change 0 'calendar)
  14793. (let ((cal-date (org-get-date-from-calendar)))
  14794. (org-insert-time-stamp
  14795. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14796. (defun org-minutes-to-hh:mm-string (m)
  14797. "Compute H:MM from a number of minutes."
  14798. (let ((h (/ m 60)))
  14799. (setq m (- m (* 60 h)))
  14800. (format org-time-clocksum-format h m)))
  14801. (defun org-hh:mm-string-to-minutes (s)
  14802. "Convert a string H:MM to a number of minutes.
  14803. If the string is just a number, interpret it as minutes.
  14804. In fact, the first hh:mm or number in the string will be taken,
  14805. there can be extra stuff in the string.
  14806. If no number is found, the return value is 0."
  14807. (cond
  14808. ((integerp s) s)
  14809. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14810. (+ (* (string-to-number (match-string 1 s)) 60)
  14811. (string-to-number (match-string 2 s))))
  14812. ((string-match "\\([0-9]+\\)" s)
  14813. (string-to-number (match-string 1 s)))
  14814. (t 0)))
  14815. (defcustom org-effort-durations
  14816. `(("h" . 60)
  14817. ("d" . ,(* 60 8))
  14818. ("w" . ,(* 60 8 5))
  14819. ("m" . ,(* 60 8 5 4))
  14820. ("y" . ,(* 60 8 5 40)))
  14821. "Conversion factor to minutes for an effort modifier.
  14822. Each entry has the form (MODIFIER . MINUTES).
  14823. In an effort string, a number followed by MODIFIER is multiplied
  14824. by the specified number of MINUTES to obtain an effort in
  14825. minutes.
  14826. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14827. effort string \"2hours\" is equivalent to 120 minutes."
  14828. :group 'org-agenda
  14829. :type '(alist :key-type (string :tag "Modifier")
  14830. :value-type (number :tag "Minutes")))
  14831. (defun org-duration-string-to-minutes (s)
  14832. "Convert a duration string S to minutes.
  14833. A bare number is interpreted as minutes, modifiers can be set by
  14834. customizing `org-effort-durations' (which see).
  14835. Entries containing a colon are interpreted as H:MM by
  14836. `org-hh:mm-string-to-minutes'."
  14837. (let ((result 0)
  14838. (re (concat "\\([0-9]+\\) *\\("
  14839. (regexp-opt (mapcar 'car org-effort-durations))
  14840. "\\)")))
  14841. (while (string-match re s)
  14842. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14843. (string-to-number (match-string 1 s))))
  14844. (setq s (replace-match "" nil t s)))
  14845. (incf result (org-hh:mm-string-to-minutes s))
  14846. result))
  14847. ;;;; Files
  14848. (defun org-save-all-org-buffers ()
  14849. "Save all Org-mode buffers without user confirmation."
  14850. (interactive)
  14851. (message "Saving all Org-mode buffers...")
  14852. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14853. (when (featurep 'org-id) (org-id-locations-save))
  14854. (message "Saving all Org-mode buffers... done"))
  14855. (defun org-revert-all-org-buffers ()
  14856. "Revert all Org-mode buffers.
  14857. Prompt for confirmation when there are unsaved changes.
  14858. Be sure you know what you are doing before letting this function
  14859. overwrite your changes.
  14860. This function is useful in a setup where one tracks org files
  14861. with a version control system, to revert on one machine after pulling
  14862. changes from another. I believe the procedure must be like this:
  14863. 1. M-x org-save-all-org-buffers
  14864. 2. Pull changes from the other machine, resolve conflicts
  14865. 3. M-x org-revert-all-org-buffers"
  14866. (interactive)
  14867. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14868. (error "Abort"))
  14869. (save-excursion
  14870. (save-window-excursion
  14871. (mapc
  14872. (lambda (b)
  14873. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14874. (with-current-buffer b buffer-file-name))
  14875. (org-pop-to-buffer-same-window b)
  14876. (revert-buffer t 'no-confirm)))
  14877. (buffer-list))
  14878. (when (and (featurep 'org-id) org-id-track-globally)
  14879. (org-id-locations-load)))))
  14880. ;;;; Agenda files
  14881. ;;;###autoload
  14882. (defun org-switchb (&optional arg)
  14883. "Switch between Org buffers.
  14884. With one prefix argument, restrict available buffers to files.
  14885. With two prefix arguments, restrict available buffers to agenda files.
  14886. Defaults to `iswitchb' for buffer name completion.
  14887. Set `org-completion-use-ido' to make it use ido instead."
  14888. (interactive "P")
  14889. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14890. ((equal arg '(16)) (org-buffer-list 'agenda))
  14891. (t (org-buffer-list))))
  14892. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14893. (org-completion-use-ido org-completion-use-ido))
  14894. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14895. (setq org-completion-use-iswitchb t))
  14896. (org-pop-to-buffer-same-window
  14897. (org-icompleting-read "Org buffer: "
  14898. (mapcar 'list (mapcar 'buffer-name blist))
  14899. nil t))))
  14900. ;;; Define some older names previously used for this functionality
  14901. ;;;###autoload
  14902. (defalias 'org-ido-switchb 'org-switchb)
  14903. ;;;###autoload
  14904. (defalias 'org-iswitchb 'org-switchb)
  14905. (defun org-buffer-list (&optional predicate exclude-tmp)
  14906. "Return a list of Org buffers.
  14907. PREDICATE can be `export', `files' or `agenda'.
  14908. export restrict the list to Export buffers.
  14909. files restrict the list to buffers visiting Org files.
  14910. agenda restrict the list to buffers visiting agenda files.
  14911. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14912. (let* ((bfn nil)
  14913. (agenda-files (and (eq predicate 'agenda)
  14914. (mapcar 'file-truename (org-agenda-files t))))
  14915. (filter
  14916. (cond
  14917. ((eq predicate 'files)
  14918. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14919. ((eq predicate 'export)
  14920. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14921. ((eq predicate 'agenda)
  14922. (lambda (b)
  14923. (with-current-buffer b
  14924. (and (eq major-mode 'org-mode)
  14925. (setq bfn (buffer-file-name b))
  14926. (member (file-truename bfn) agenda-files)))))
  14927. (t (lambda (b) (with-current-buffer b
  14928. (or (eq major-mode 'org-mode)
  14929. (string-match "\*Org .*Export"
  14930. (buffer-name b)))))))))
  14931. (delq nil
  14932. (mapcar
  14933. (lambda(b)
  14934. (if (and (funcall filter b)
  14935. (or (not exclude-tmp)
  14936. (not (string-match "tmp" (buffer-name b)))))
  14937. b
  14938. nil))
  14939. (buffer-list)))))
  14940. (defun org-agenda-files (&optional unrestricted archives)
  14941. "Get the list of agenda files.
  14942. Optional UNRESTRICTED means return the full list even if a restriction
  14943. is currently in place.
  14944. When ARCHIVES is t, include all archive files that are really being
  14945. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14946. `org-agenda-archives-mode' is t."
  14947. (let ((files
  14948. (cond
  14949. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14950. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14951. ((listp org-agenda-files) org-agenda-files)
  14952. (t (error "Invalid value of `org-agenda-files'")))))
  14953. (setq files (apply 'append
  14954. (mapcar (lambda (f)
  14955. (if (file-directory-p f)
  14956. (directory-files
  14957. f t org-agenda-file-regexp)
  14958. (list f)))
  14959. files)))
  14960. (when org-agenda-skip-unavailable-files
  14961. (setq files (delq nil
  14962. (mapcar (function
  14963. (lambda (file)
  14964. (and (file-readable-p file) file)))
  14965. files))))
  14966. (when (or (eq archives t)
  14967. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14968. (setq files (org-add-archive-files files)))
  14969. files))
  14970. (defun org-agenda-file-p (&optional file)
  14971. "Return non-nil, if FILE is an agenda file.
  14972. If FILE is omitted, use the file associated with the current
  14973. buffer."
  14974. (member (or file (buffer-file-name))
  14975. (org-agenda-files t)))
  14976. (defun org-edit-agenda-file-list ()
  14977. "Edit the list of agenda files.
  14978. Depending on setup, this either uses customize to edit the variable
  14979. `org-agenda-files', or it visits the file that is holding the list. In the
  14980. latter case, the buffer is set up in a way that saving it automatically kills
  14981. the buffer and restores the previous window configuration."
  14982. (interactive)
  14983. (if (stringp org-agenda-files)
  14984. (let ((cw (current-window-configuration)))
  14985. (find-file org-agenda-files)
  14986. (org-set-local 'org-window-configuration cw)
  14987. (org-add-hook 'after-save-hook
  14988. (lambda ()
  14989. (set-window-configuration
  14990. (prog1 org-window-configuration
  14991. (kill-buffer (current-buffer))))
  14992. (org-install-agenda-files-menu)
  14993. (message "New agenda file list installed"))
  14994. nil 'local)
  14995. (message "%s" (substitute-command-keys
  14996. "Edit list and finish with \\[save-buffer]")))
  14997. (customize-variable 'org-agenda-files)))
  14998. (defun org-store-new-agenda-file-list (list)
  14999. "Set new value for the agenda file list and save it correctly."
  15000. (if (stringp org-agenda-files)
  15001. (let ((fe (org-read-agenda-file-list t)) b u)
  15002. (while (setq b (find-buffer-visiting org-agenda-files))
  15003. (kill-buffer b))
  15004. (with-temp-file org-agenda-files
  15005. (insert
  15006. (mapconcat
  15007. (lambda (f) ;; Keep un-expanded entries.
  15008. (if (setq u (assoc f fe))
  15009. (cdr u)
  15010. f))
  15011. list "\n")
  15012. "\n")))
  15013. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15014. (org-insert-mode-line-in-empty-file nil))
  15015. (setq org-agenda-files list)
  15016. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15017. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15018. "Read the list of agenda files from a file.
  15019. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15020. filenames, used by `org-store-new-agenda-file-list' to write back
  15021. un-expanded file names."
  15022. (when (file-directory-p org-agenda-files)
  15023. (error "`org-agenda-files' cannot be a single directory"))
  15024. (when (stringp org-agenda-files)
  15025. (with-temp-buffer
  15026. (insert-file-contents org-agenda-files)
  15027. (mapcar
  15028. (lambda (f)
  15029. (let ((e (expand-file-name (substitute-in-file-name f)
  15030. org-directory)))
  15031. (if pair-with-expansion
  15032. (cons e f)
  15033. e)))
  15034. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15035. ;;;###autoload
  15036. (defun org-cycle-agenda-files ()
  15037. "Cycle through the files in `org-agenda-files'.
  15038. If the current buffer visits an agenda file, find the next one in the list.
  15039. If the current buffer does not, find the first agenda file."
  15040. (interactive)
  15041. (let* ((fs (org-agenda-files t))
  15042. (files (append fs (list (car fs))))
  15043. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15044. file)
  15045. (unless files (error "No agenda files"))
  15046. (catch 'exit
  15047. (while (setq file (pop files))
  15048. (if (equal (file-truename file) tcf)
  15049. (when (car files)
  15050. (find-file (car files))
  15051. (throw 'exit t))))
  15052. (find-file (car fs)))
  15053. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15054. (defun org-agenda-file-to-front (&optional to-end)
  15055. "Move/add the current file to the top of the agenda file list.
  15056. If the file is not present in the list, it is added to the front. If it is
  15057. present, it is moved there. With optional argument TO-END, add/move to the
  15058. end of the list."
  15059. (interactive "P")
  15060. (let ((org-agenda-skip-unavailable-files nil)
  15061. (file-alist (mapcar (lambda (x)
  15062. (cons (file-truename x) x))
  15063. (org-agenda-files t)))
  15064. (ctf (file-truename buffer-file-name))
  15065. x had)
  15066. (setq x (assoc ctf file-alist) had x)
  15067. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15068. (if to-end
  15069. (setq file-alist (append (delq x file-alist) (list x)))
  15070. (setq file-alist (cons x (delq x file-alist))))
  15071. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15072. (org-install-agenda-files-menu)
  15073. (message "File %s to %s of agenda file list"
  15074. (if had "moved" "added") (if to-end "end" "front"))))
  15075. (defun org-remove-file (&optional file)
  15076. "Remove current file from the list of files in variable `org-agenda-files'.
  15077. These are the files which are being checked for agenda entries.
  15078. Optional argument FILE means use this file instead of the current."
  15079. (interactive)
  15080. (let* ((org-agenda-skip-unavailable-files nil)
  15081. (file (or file buffer-file-name))
  15082. (true-file (file-truename file))
  15083. (afile (abbreviate-file-name file))
  15084. (files (delq nil (mapcar
  15085. (lambda (x)
  15086. (if (equal true-file
  15087. (file-truename x))
  15088. nil x))
  15089. (org-agenda-files t)))))
  15090. (if (not (= (length files) (length (org-agenda-files t))))
  15091. (progn
  15092. (org-store-new-agenda-file-list files)
  15093. (org-install-agenda-files-menu)
  15094. (message "Removed file: %s" afile))
  15095. (message "File was not in list: %s (not removed)" afile))))
  15096. (defun org-file-menu-entry (file)
  15097. (vector file (list 'find-file file) t))
  15098. (defun org-check-agenda-file (file)
  15099. "Make sure FILE exists. If not, ask user what to do."
  15100. (when (not (file-exists-p file))
  15101. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15102. (abbreviate-file-name file))
  15103. (let ((r (downcase (read-char-exclusive))))
  15104. (cond
  15105. ((equal r ?r)
  15106. (org-remove-file file)
  15107. (throw 'nextfile t))
  15108. (t (error "Abort"))))))
  15109. (defun org-get-agenda-file-buffer (file)
  15110. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15111. it to the list of buffers which might be released later."
  15112. (let ((buf (org-find-base-buffer-visiting file)))
  15113. (if buf
  15114. buf ; just return it
  15115. ;; Make a new buffer and remember it
  15116. (setq buf (find-file-noselect file))
  15117. (if buf (push buf org-agenda-new-buffers))
  15118. buf)))
  15119. (defun org-release-buffers (blist)
  15120. "Release all buffers in list, asking the user for confirmation when needed.
  15121. When a buffer is unmodified, it is just killed. When modified, it is saved
  15122. \(if the user agrees) and then killed."
  15123. (let (buf file)
  15124. (while (setq buf (pop blist))
  15125. (setq file (buffer-file-name buf))
  15126. (when (and (buffer-modified-p buf)
  15127. file
  15128. (y-or-n-p (format "Save file %s? " file)))
  15129. (with-current-buffer buf (save-buffer)))
  15130. (kill-buffer buf))))
  15131. (defun org-prepare-agenda-buffers (files)
  15132. "Create buffers for all agenda files, protect archived trees and comments."
  15133. (interactive)
  15134. (let ((pa '(:org-archived t))
  15135. (pc '(:org-comment t))
  15136. (pall '(:org-archived t :org-comment t))
  15137. (inhibit-read-only t)
  15138. (rea (concat ":" org-archive-tag ":"))
  15139. bmp file re)
  15140. (save-excursion
  15141. (save-restriction
  15142. (while (setq file (pop files))
  15143. (catch 'nextfile
  15144. (if (bufferp file)
  15145. (set-buffer file)
  15146. (org-check-agenda-file file)
  15147. (set-buffer (org-get-agenda-file-buffer file)))
  15148. (widen)
  15149. (setq bmp (buffer-modified-p))
  15150. (org-refresh-category-properties)
  15151. (setq org-todo-keywords-for-agenda
  15152. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15153. (setq org-done-keywords-for-agenda
  15154. (append org-done-keywords-for-agenda org-done-keywords))
  15155. (setq org-todo-keyword-alist-for-agenda
  15156. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15157. (setq org-drawers-for-agenda
  15158. (append org-drawers-for-agenda org-drawers))
  15159. (setq org-tag-alist-for-agenda
  15160. (append org-tag-alist-for-agenda org-tag-alist))
  15161. (save-excursion
  15162. (remove-text-properties (point-min) (point-max) pall)
  15163. (when org-agenda-skip-archived-trees
  15164. (goto-char (point-min))
  15165. (while (re-search-forward rea nil t)
  15166. (if (org-at-heading-p t)
  15167. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15168. (goto-char (point-min))
  15169. (setq re (format org-heading-keyword-regexp-format
  15170. org-comment-string))
  15171. (while (re-search-forward re nil t)
  15172. (add-text-properties
  15173. (match-beginning 0) (org-end-of-subtree t) pc)))
  15174. (set-buffer-modified-p bmp)))))
  15175. (setq org-todo-keywords-for-agenda
  15176. (org-uniquify org-todo-keywords-for-agenda))
  15177. (setq org-todo-keyword-alist-for-agenda
  15178. (org-uniquify org-todo-keyword-alist-for-agenda)
  15179. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15180. ;;;; Embedded LaTeX
  15181. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15182. "Keymap for the minor `org-cdlatex-mode'.")
  15183. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15184. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15185. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15186. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15187. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15188. (defvar org-cdlatex-texmathp-advice-is-done nil
  15189. "Flag remembering if we have applied the advice to texmathp already.")
  15190. (define-minor-mode org-cdlatex-mode
  15191. "Toggle the minor `org-cdlatex-mode'.
  15192. This mode supports entering LaTeX environment and math in LaTeX fragments
  15193. in Org-mode.
  15194. \\{org-cdlatex-mode-map}"
  15195. nil " OCDL" nil
  15196. (when org-cdlatex-mode
  15197. (require 'cdlatex)
  15198. (run-hooks 'cdlatex-mode-hook)
  15199. (cdlatex-compute-tables))
  15200. (unless org-cdlatex-texmathp-advice-is-done
  15201. (setq org-cdlatex-texmathp-advice-is-done t)
  15202. (defadvice texmathp (around org-math-always-on activate)
  15203. "Always return t in org-mode buffers.
  15204. This is because we want to insert math symbols without dollars even outside
  15205. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15206. an embedded LaTeX fragment, let texmathp do its job.
  15207. \\[org-cdlatex-mode-map]"
  15208. (interactive)
  15209. (let (p)
  15210. (cond
  15211. ((not (eq major-mode 'org-mode)) ad-do-it)
  15212. ((eq this-command 'cdlatex-math-symbol)
  15213. (setq ad-return-value t
  15214. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15215. (t
  15216. (let ((p (org-inside-LaTeX-fragment-p)))
  15217. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15218. (setq ad-return-value t
  15219. texmathp-why '("Org-mode embedded math" . 0))
  15220. (if p ad-do-it)))))))))
  15221. (defun turn-on-org-cdlatex ()
  15222. "Unconditionally turn on `org-cdlatex-mode'."
  15223. (org-cdlatex-mode 1))
  15224. (defun org-inside-LaTeX-fragment-p ()
  15225. "Test if point is inside a LaTeX fragment.
  15226. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15227. sequence appearing also before point.
  15228. Even though the matchers for math are configurable, this function assumes
  15229. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15230. delimiters are skipped when they have been removed by customization.
  15231. The return value is nil, or a cons cell with the delimiter and the
  15232. position of this delimiter.
  15233. This function does a reasonably good job, but can locally be fooled by
  15234. for example currency specifications. For example it will assume being in
  15235. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15236. fragments that are properly closed, but during editing, we have to live
  15237. with the uncertainty caused by missing closing delimiters. This function
  15238. looks only before point, not after."
  15239. (catch 'exit
  15240. (let ((pos (point))
  15241. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15242. (lim (progn
  15243. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15244. (point)))
  15245. dd-on str (start 0) m re)
  15246. (goto-char pos)
  15247. (when dodollar
  15248. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15249. re (nth 1 (assoc "$" org-latex-regexps)))
  15250. (while (string-match re str start)
  15251. (cond
  15252. ((= (match-end 0) (length str))
  15253. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15254. ((= (match-end 0) (- (length str) 5))
  15255. (throw 'exit nil))
  15256. (t (setq start (match-end 0))))))
  15257. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15258. (goto-char pos)
  15259. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15260. (and (match-beginning 2) (throw 'exit nil))
  15261. ;; count $$
  15262. (while (re-search-backward "\\$\\$" lim t)
  15263. (setq dd-on (not dd-on)))
  15264. (goto-char pos)
  15265. (if dd-on (cons "$$" m))))))
  15266. (defun org-inside-latex-macro-p ()
  15267. "Is point inside a LaTeX macro or its arguments?"
  15268. (save-match-data
  15269. (org-in-regexp
  15270. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15271. (defun org-try-cdlatex-tab ()
  15272. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15273. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15274. - inside a LaTeX fragment, or
  15275. - after the first word in a line, where an abbreviation expansion could
  15276. insert a LaTeX environment."
  15277. (when org-cdlatex-mode
  15278. (cond
  15279. ;; Before any word on the line: No expansion possible.
  15280. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15281. ;; Just after first word on the line: Expand it. Make sure it
  15282. ;; cannot happen on headlines, though.
  15283. ((save-excursion
  15284. (skip-chars-backward "a-zA-Z0-9*")
  15285. (skip-chars-backward " \t")
  15286. (and (bolp) (not (org-at-heading-p))))
  15287. (cdlatex-tab) t)
  15288. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15289. (defun org-cdlatex-underscore-caret (&optional arg)
  15290. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15291. Revert to the normal definition outside of these fragments."
  15292. (interactive "P")
  15293. (if (org-inside-LaTeX-fragment-p)
  15294. (call-interactively 'cdlatex-sub-superscript)
  15295. (let (org-cdlatex-mode)
  15296. (call-interactively (key-binding (vector last-input-event))))))
  15297. (defun org-cdlatex-math-modify (&optional arg)
  15298. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15299. Revert to the normal definition outside of these fragments."
  15300. (interactive "P")
  15301. (if (org-inside-LaTeX-fragment-p)
  15302. (call-interactively 'cdlatex-math-modify)
  15303. (let (org-cdlatex-mode)
  15304. (call-interactively (key-binding (vector last-input-event))))))
  15305. (defvar org-latex-fragment-image-overlays nil
  15306. "List of overlays carrying the images of latex fragments.")
  15307. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15308. (defun org-remove-latex-fragment-image-overlays ()
  15309. "Remove all overlays with LaTeX fragment images in current buffer."
  15310. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15311. (setq org-latex-fragment-image-overlays nil))
  15312. (defun org-preview-latex-fragment (&optional subtree)
  15313. "Preview the LaTeX fragment at point, or all locally or globally.
  15314. If the cursor is in a LaTeX fragment, create the image and overlay
  15315. it over the source code. If there is no fragment at point, display
  15316. all fragments in the current text, from one headline to the next. With
  15317. prefix SUBTREE, display all fragments in the current subtree. With a
  15318. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15319. the cursor is before the first headline,
  15320. display all fragments in the buffer.
  15321. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15322. (interactive "P")
  15323. (unless buffer-file-name
  15324. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15325. (org-remove-latex-fragment-image-overlays)
  15326. (save-excursion
  15327. (save-restriction
  15328. (let (beg end at msg)
  15329. (cond
  15330. ((or (equal subtree '(16))
  15331. (not (save-excursion
  15332. (re-search-backward org-outline-regexp-bol nil t))))
  15333. (setq beg (point-min) end (point-max)
  15334. msg "Creating images for buffer...%s"))
  15335. ((equal subtree '(4))
  15336. (org-back-to-heading)
  15337. (setq beg (point) end (org-end-of-subtree t)
  15338. msg "Creating images for subtree...%s"))
  15339. (t
  15340. (if (setq at (org-inside-LaTeX-fragment-p))
  15341. (goto-char (max (point-min) (- (cdr at) 2)))
  15342. (org-back-to-heading))
  15343. (setq beg (point) end (progn (outline-next-heading) (point))
  15344. msg (if at "Creating image...%s"
  15345. "Creating images for entry...%s"))))
  15346. (message msg "")
  15347. (narrow-to-region beg end)
  15348. (goto-char beg)
  15349. (org-format-latex
  15350. (concat "ltxpng/" (file-name-sans-extension
  15351. (file-name-nondirectory
  15352. buffer-file-name)))
  15353. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15354. (message msg "done. Use `C-c C-c' to remove images.")))))
  15355. (defvar org-latex-regexps
  15356. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15357. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15358. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15359. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15360. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15361. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15362. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15363. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15364. "Regular expressions for matching embedded LaTeX.")
  15365. (defvar org-export-have-math nil) ;; dynamic scoping
  15366. (defun org-format-latex (prefix &optional dir overlays msg at
  15367. forbuffer processing-type)
  15368. "Replace LaTeX fragments with links to an image, and produce images.
  15369. Some of the options can be changed using the variable
  15370. `org-format-latex-options'."
  15371. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15372. (let* ((prefixnodir (file-name-nondirectory prefix))
  15373. (absprefix (expand-file-name prefix dir))
  15374. (todir (file-name-directory absprefix))
  15375. (opt org-format-latex-options)
  15376. (matchers (plist-get opt :matchers))
  15377. (re-list org-latex-regexps)
  15378. (org-format-latex-header-extra
  15379. (plist-get (org-infile-export-plist) :latex-header-extra))
  15380. (cnt 0) txt hash link beg end re e checkdir
  15381. executables-checked string
  15382. m n block-type block linkfile movefile ov)
  15383. ;; Check the different regular expressions
  15384. (while (setq e (pop re-list))
  15385. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15386. block (if block-type "\n\n" ""))
  15387. (when (member m matchers)
  15388. (goto-char (point-min))
  15389. (while (re-search-forward re nil t)
  15390. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15391. (not (get-text-property (match-beginning n)
  15392. 'org-protected))
  15393. (or (not overlays)
  15394. (not (eq (get-char-property (match-beginning n)
  15395. 'org-overlay-type)
  15396. 'org-latex-overlay))))
  15397. (setq org-export-have-math t)
  15398. (cond
  15399. ((eq processing-type 'verbatim)
  15400. ;; Leave the text verbatim, just protect it
  15401. (add-text-properties (match-beginning n) (match-end n)
  15402. '(org-protected t)))
  15403. ((eq processing-type 'mathjax)
  15404. ;; Prepare for MathJax processing
  15405. (setq string (match-string n))
  15406. (if (member m '("$" "$1"))
  15407. (save-excursion
  15408. (delete-region (match-beginning n) (match-end n))
  15409. (goto-char (match-beginning n))
  15410. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15411. "\\)")
  15412. '(org-protected t))))
  15413. (add-text-properties (match-beginning n) (match-end n)
  15414. '(org-protected t))))
  15415. ((eq processing-type 'dvipng)
  15416. ;; Process to an image
  15417. (setq txt (match-string n)
  15418. beg (match-beginning n) end (match-end n)
  15419. cnt (1+ cnt))
  15420. (let (print-length print-level) ; make sure full list is printed
  15421. (setq hash (sha1 (prin1-to-string
  15422. (list org-format-latex-header
  15423. org-format-latex-header-extra
  15424. org-export-latex-default-packages-alist
  15425. org-export-latex-packages-alist
  15426. org-format-latex-options
  15427. forbuffer txt)))
  15428. linkfile (format "%s_%s.png" prefix hash)
  15429. movefile (format "%s_%s.png" absprefix hash)))
  15430. (setq link (concat block "[[file:" linkfile "]]" block))
  15431. (if msg (message msg cnt))
  15432. (goto-char beg)
  15433. (unless checkdir ; make sure the directory exists
  15434. (setq checkdir t)
  15435. (or (file-directory-p todir) (make-directory todir t)))
  15436. (unless executables-checked
  15437. (org-check-external-command
  15438. "latex" "needed to convert LaTeX fragments to images")
  15439. (org-check-external-command
  15440. "dvipng" "needed to convert LaTeX fragments to images")
  15441. (setq executables-checked t))
  15442. (unless (file-exists-p movefile)
  15443. (org-create-formula-image
  15444. txt movefile opt forbuffer))
  15445. (if overlays
  15446. (progn
  15447. (mapc (lambda (o)
  15448. (if (eq (overlay-get o 'org-overlay-type)
  15449. 'org-latex-overlay)
  15450. (delete-overlay o)))
  15451. (overlays-in beg end))
  15452. (setq ov (make-overlay beg end))
  15453. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15454. (if (featurep 'xemacs)
  15455. (progn
  15456. (overlay-put ov 'invisible t)
  15457. (overlay-put
  15458. ov 'end-glyph
  15459. (make-glyph (vector 'png :file movefile))))
  15460. (overlay-put
  15461. ov 'display
  15462. (list 'image :type 'png :file movefile :ascent 'center)))
  15463. (push ov org-latex-fragment-image-overlays)
  15464. (goto-char end))
  15465. (delete-region beg end)
  15466. (insert (org-add-props link
  15467. (list 'org-latex-src
  15468. (replace-regexp-in-string
  15469. "\"" "" txt)
  15470. 'org-latex-src-embed-type
  15471. (if block-type 'paragraph 'character))))))
  15472. ((eq processing-type 'mathml)
  15473. ;; Process to MathML
  15474. (unless executables-checked
  15475. (unless (save-match-data (org-format-latex-mathml-available-p))
  15476. (error "LaTeX to MathML converter not configured"))
  15477. (setq executables-checked t))
  15478. (setq txt (match-string n)
  15479. beg (match-beginning n) end (match-end n)
  15480. cnt (1+ cnt))
  15481. (if msg (message msg cnt))
  15482. (goto-char beg)
  15483. (delete-region beg end)
  15484. (insert (org-format-latex-as-mathml
  15485. txt block-type prefix dir)))
  15486. (t
  15487. (error "Unknown conversion type %s for latex fragments"
  15488. processing-type)))))))))
  15489. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15490. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15491. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15492. sucessful, return the portion between \"<math...> </math>\"
  15493. elements otherwise return nil. When MATHML-FILE is specified,
  15494. write the results in to that file. When invoked as an
  15495. interactive command, prompt for LATEX-FRAG, with initial value
  15496. set to the current active region and echo the results for user
  15497. inspection."
  15498. (interactive (list (let ((frag (when (region-active-p)
  15499. (buffer-substring-no-properties
  15500. (region-beginning) (region-end)))))
  15501. (read-string "LaTeX Fragment: " frag nil frag))))
  15502. (unless latex-frag (error "Invalid latex-frag"))
  15503. (let* ((tmp-in-file (file-relative-name
  15504. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15505. (ignore (write-region latex-frag nil tmp-in-file))
  15506. (tmp-out-file (file-relative-name
  15507. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15508. (cmd (format-spec
  15509. org-latex-to-mathml-convert-command
  15510. `((?j . ,(shell-quote-argument
  15511. (expand-file-name org-latex-to-mathml-jar-file)))
  15512. (?I . ,(shell-quote-argument tmp-in-file))
  15513. (?o . ,(shell-quote-argument tmp-out-file)))))
  15514. mathml shell-command-output)
  15515. (when (org-called-interactively-p 'any)
  15516. (unless (org-format-latex-mathml-available-p)
  15517. (error "LaTeX to MathML converter not configured")))
  15518. (message "Running %s" cmd)
  15519. (setq shell-command-output (shell-command-to-string cmd))
  15520. (setq mathml
  15521. (when (file-readable-p tmp-out-file)
  15522. (with-current-buffer (find-file-noselect tmp-out-file t)
  15523. (goto-char (point-min))
  15524. (when (re-search-forward
  15525. (concat
  15526. (regexp-quote
  15527. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15528. "\\(.\\|\n\\)*"
  15529. (regexp-quote "</math>")) nil t)
  15530. (prog1 (match-string 0) (kill-buffer))))))
  15531. (cond
  15532. (mathml
  15533. (setq mathml
  15534. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15535. (when mathml-file
  15536. (write-region mathml nil mathml-file))
  15537. (when (org-called-interactively-p 'any)
  15538. (message mathml)))
  15539. ((message "LaTeX to MathML conversion failed")
  15540. (message shell-command-output)))
  15541. (delete-file tmp-in-file)
  15542. (when (file-exists-p tmp-out-file)
  15543. (delete-file tmp-out-file))
  15544. mathml))
  15545. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15546. prefix &optional dir)
  15547. "Use `org-create-math-formula' but check local cache first."
  15548. (let* ((absprefix (expand-file-name prefix dir))
  15549. (print-length nil) (print-level nil)
  15550. (formula-id (concat
  15551. "formula-"
  15552. (sha1
  15553. (prin1-to-string
  15554. (list latex-frag
  15555. org-latex-to-mathml-convert-command)))))
  15556. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15557. (formula-cache-dir (file-name-directory formula-cache)))
  15558. (unless (file-directory-p formula-cache-dir)
  15559. (make-directory formula-cache-dir t))
  15560. (unless (file-exists-p formula-cache)
  15561. (org-create-math-formula latex-frag formula-cache))
  15562. (if (file-exists-p formula-cache)
  15563. ;; Successful conversion. Return the link to MathML file.
  15564. (org-add-props
  15565. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15566. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15567. 'org-latex-src-embed-type (if latex-frag-type
  15568. 'paragraph 'character)))
  15569. ;; Failed conversion. Return the LaTeX fragment verbatim
  15570. (add-text-properties
  15571. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15572. latex-frag)))
  15573. ;; This function borrows from Ganesh Swami's latex2png.el
  15574. (defun org-create-formula-image (string tofile options buffer)
  15575. "This calls dvipng."
  15576. (require 'org-latex)
  15577. (let* ((tmpdir (if (featurep 'xemacs)
  15578. (temp-directory)
  15579. temporary-file-directory))
  15580. (texfilebase (make-temp-name
  15581. (expand-file-name "orgtex" tmpdir)))
  15582. (texfile (concat texfilebase ".tex"))
  15583. (dvifile (concat texfilebase ".dvi"))
  15584. (pngfile (concat texfilebase ".png"))
  15585. (fnh (if (featurep 'xemacs)
  15586. (font-height (face-font 'default))
  15587. (face-attribute 'default :height nil)))
  15588. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15589. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15590. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15591. "Black"))
  15592. (bg (or (plist-get options (if buffer :background :html-background))
  15593. "Transparent")))
  15594. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15595. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15596. (with-temp-file texfile
  15597. (insert (org-splice-latex-header
  15598. org-format-latex-header
  15599. org-export-latex-default-packages-alist
  15600. org-export-latex-packages-alist t
  15601. org-format-latex-header-extra))
  15602. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15603. (require 'org-latex)
  15604. (org-export-latex-fix-inputenc))
  15605. (let ((dir default-directory))
  15606. (condition-case nil
  15607. (progn
  15608. (cd tmpdir)
  15609. (call-process "latex" nil nil nil texfile))
  15610. (error nil))
  15611. (cd dir))
  15612. (if (not (file-exists-p dvifile))
  15613. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15614. (condition-case nil
  15615. (if (featurep 'xemacs)
  15616. (call-process "dvipng" nil nil nil
  15617. "-fg" fg "-bg" bg
  15618. "-T" "tight"
  15619. "-o" pngfile
  15620. dvifile)
  15621. (call-process "dvipng" nil nil nil
  15622. "-fg" fg "-bg" bg
  15623. "-D" dpi
  15624. ;;"-x" scale "-y" scale
  15625. "-T" "tight"
  15626. "-o" pngfile
  15627. dvifile))
  15628. (error nil))
  15629. (if (not (file-exists-p pngfile))
  15630. (if org-format-latex-signal-error
  15631. (error "Failed to create png file from %s" texfile)
  15632. (message "Failed to create png file from %s" texfile)
  15633. nil)
  15634. ;; Use the requested file name and clean up
  15635. (copy-file pngfile tofile 'replace)
  15636. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15637. (delete-file (concat texfilebase e)))
  15638. pngfile))))
  15639. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15640. "Fill a LaTeX header template TPL.
  15641. In the template, the following place holders will be recognized:
  15642. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15643. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15644. [PACKAGES] \\usepackage statements for PKG
  15645. [NO-PACKAGES] do not include PKG
  15646. [EXTRA] the string EXTRA
  15647. [NO-EXTRA] do not include EXTRA
  15648. For backward compatibility, if both the positive and the negative place
  15649. holder is missing, the positive one (without the \"NO-\") will be
  15650. assumed to be present at the end of the template.
  15651. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15652. EXTRA is a string.
  15653. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15654. (let (rpl (end ""))
  15655. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15656. (setq rpl (if (or (match-end 1) (not def-pkg))
  15657. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15658. tpl (replace-match rpl t t tpl))
  15659. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15660. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15661. (setq rpl (if (or (match-end 1) (not pkg))
  15662. "" (org-latex-packages-to-string pkg snippets-p t))
  15663. tpl (replace-match rpl t t tpl))
  15664. (if pkg (setq end
  15665. (concat end "\n"
  15666. (org-latex-packages-to-string pkg snippets-p)))))
  15667. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15668. (setq rpl (if (or (match-end 1) (not extra))
  15669. "" (concat extra "\n"))
  15670. tpl (replace-match rpl t t tpl))
  15671. (if (and extra (string-match "\\S-" extra))
  15672. (setq end (concat end "\n" extra))))
  15673. (if (string-match "\\S-" end)
  15674. (concat tpl "\n" end)
  15675. tpl)))
  15676. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15677. "Turn an alist of packages into a string with the \\usepackage macros."
  15678. (setq pkg (mapconcat (lambda(p)
  15679. (cond
  15680. ((stringp p) p)
  15681. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15682. (format "%% Package %s omitted" (cadr p)))
  15683. ((equal "" (car p))
  15684. (format "\\usepackage{%s}" (cadr p)))
  15685. (t
  15686. (format "\\usepackage[%s]{%s}"
  15687. (car p) (cadr p)))))
  15688. pkg
  15689. "\n"))
  15690. (if newline (concat pkg "\n") pkg))
  15691. (defun org-dvipng-color (attr)
  15692. "Return an rgb color specification for dvipng."
  15693. (apply 'format "rgb %s %s %s"
  15694. (mapcar 'org-normalize-color
  15695. (if (featurep 'xemacs)
  15696. (color-rgb-components
  15697. (face-property 'default
  15698. (cond ((eq attr :foreground) 'foreground)
  15699. ((eq attr :background) 'background))))
  15700. (color-values (face-attribute 'default attr nil))))))
  15701. (defun org-normalize-color (value)
  15702. "Return string to be used as color value for an RGB component."
  15703. (format "%g" (/ value 65535.0)))
  15704. ;; Image display
  15705. (defvar org-inline-image-overlays nil)
  15706. (make-variable-buffer-local 'org-inline-image-overlays)
  15707. (defun org-toggle-inline-images (&optional include-linked)
  15708. "Toggle the display of inline images.
  15709. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15710. (interactive "P")
  15711. (if org-inline-image-overlays
  15712. (progn
  15713. (org-remove-inline-images)
  15714. (message "Inline image display turned off"))
  15715. (org-display-inline-images include-linked)
  15716. (if org-inline-image-overlays
  15717. (message "%d images displayed inline"
  15718. (length org-inline-image-overlays))
  15719. (message "No images to display inline"))))
  15720. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15721. "Display inline images.
  15722. Normally only links without a description part are inlined, because this
  15723. is how it will work for export. When INCLUDE-LINKED is set, also links
  15724. with a description part will be inlined. This can be nice for a quick
  15725. look at those images, but it does not reflect what exported files will look
  15726. like.
  15727. When REFRESH is set, refresh existing images between BEG and END.
  15728. This will create new image displays only if necessary.
  15729. BEG and END default to the buffer boundaries."
  15730. (interactive "P")
  15731. (unless refresh
  15732. (org-remove-inline-images)
  15733. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15734. (save-excursion
  15735. (save-restriction
  15736. (widen)
  15737. (setq beg (or beg (point-min)) end (or end (point-max)))
  15738. (goto-char beg)
  15739. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15740. (substring (org-image-file-name-regexp) 0 -2)
  15741. "\\)\\]" (if include-linked "" "\\]")))
  15742. old file ov img)
  15743. (while (re-search-forward re end t)
  15744. (setq old (get-char-property-and-overlay (match-beginning 1)
  15745. 'org-image-overlay))
  15746. (setq file (expand-file-name
  15747. (concat (or (match-string 3) "") (match-string 4))))
  15748. (when (file-exists-p file)
  15749. (if (and (car-safe old) refresh)
  15750. (image-refresh (overlay-get (cdr old) 'display))
  15751. (setq img (save-match-data (create-image file)))
  15752. (when img
  15753. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15754. (overlay-put ov 'display img)
  15755. (overlay-put ov 'face 'default)
  15756. (overlay-put ov 'org-image-overlay t)
  15757. (overlay-put ov 'modification-hooks
  15758. (list 'org-display-inline-modification-hook))
  15759. (push ov org-inline-image-overlays)))))))))
  15760. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15761. "Remove inline-display overlay if a corresponding region is modified."
  15762. (let ((inhibit-modification-hooks t))
  15763. (when (and ov after)
  15764. (delete ov org-inline-image-overlays)
  15765. (delete-overlay ov))))
  15766. (defun org-remove-inline-images ()
  15767. "Remove inline display of images."
  15768. (interactive)
  15769. (mapc 'delete-overlay org-inline-image-overlays)
  15770. (setq org-inline-image-overlays nil))
  15771. ;;;; Key bindings
  15772. ;; Outline functions from `outline-mode-prefix-map'
  15773. ;; that can be remapped in Org:
  15774. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15775. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15776. (define-key org-mode-map [remap outline-forward-same-level]
  15777. 'org-forward-same-level)
  15778. (define-key org-mode-map [remap outline-backward-same-level]
  15779. 'org-backward-same-level)
  15780. (define-key org-mode-map [remap show-branches]
  15781. 'org-kill-note-or-show-branches)
  15782. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15783. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15784. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15785. ;; Outline functions from `outline-mode-prefix-map'
  15786. ;; that can not be remapped in Org:
  15787. ;; - the column "key binding" shows whether the Outline function is still
  15788. ;; available in Org mode on the same key that it has been bound to in
  15789. ;; Outline mode:
  15790. ;; - "overridden": key used for a different functionality in Org mode
  15791. ;; - else: key still bound to the same Outline function in Org mode
  15792. ;; | Outline function | key binding | Org replacement |
  15793. ;; |------------------------------------+-------------+-----------------------|
  15794. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15795. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15796. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15797. ;; | `hide-subtree' | overridden | visibility cycling |
  15798. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15799. ;; | `hide-body' | overridden | no replacement |
  15800. ;; | `show-all' | overridden | no replacement |
  15801. ;; | `hide-entry' | overridden | visibility cycling |
  15802. ;; | `show-entry' | overridden | no replacement |
  15803. ;; | `hide-leaves' | overridden | no replacement |
  15804. ;; | `hide-sublevels' | overridden | no replacement |
  15805. ;; | `hide-other' | overridden | no replacement |
  15806. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15807. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15808. ;; Make `C-c C-x' a prefix key
  15809. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15810. ;; TAB key with modifiers
  15811. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15812. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15813. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15814. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15815. ;; The following line is necessary under Suse GNU/Linux
  15816. (unless (featurep 'xemacs)
  15817. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15818. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15819. (define-key org-mode-map [backtab] 'org-shifttab)
  15820. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15821. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15822. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15823. ;; Cursor keys with modifiers
  15824. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15825. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15826. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15827. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15828. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15829. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15830. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15831. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15832. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15833. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15834. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15835. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15836. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15837. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15838. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15839. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15840. ;; Babel keys
  15841. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15842. (mapc (lambda (pair)
  15843. (define-key org-babel-map (car pair) (cdr pair)))
  15844. org-babel-key-bindings)
  15845. ;;; Extra keys for tty access.
  15846. ;; We only set them when really needed because otherwise the
  15847. ;; menus don't show the simple keys
  15848. (when (or org-use-extra-keys
  15849. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15850. (not window-system))
  15851. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15852. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15853. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15854. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15855. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15856. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15857. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15858. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15859. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15860. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15861. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15862. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15863. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15864. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15865. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15866. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15867. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15868. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15869. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15870. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15871. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15872. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15873. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15874. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15875. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15876. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15877. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15878. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15879. ;; All the other keys
  15880. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15881. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15882. (if (boundp 'narrow-map)
  15883. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15884. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15885. (if (boundp 'narrow-map)
  15886. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15887. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15888. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15889. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15890. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15891. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15892. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15893. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15894. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15895. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15896. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15897. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15898. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15899. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15900. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15901. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15902. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15903. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15904. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15905. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15906. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15907. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15908. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15909. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15910. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15911. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15912. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15913. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15914. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15915. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15916. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15917. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15918. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15919. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15920. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15921. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15922. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15923. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15924. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15925. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15926. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15927. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15928. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15929. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15930. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15931. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15932. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15933. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15934. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15935. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15936. (org-defkey org-mode-map "\C-m" 'org-return)
  15937. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15938. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15939. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15940. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15941. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15942. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15943. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15944. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15945. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15946. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15947. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15948. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15949. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15950. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15951. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15952. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15953. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15954. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15955. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15956. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15957. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15958. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15959. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15960. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15961. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15962. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15963. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15964. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15965. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15966. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15967. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15968. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15969. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15970. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15971. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15972. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15973. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15974. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15975. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15976. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15977. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15978. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15979. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15980. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15981. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15982. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15983. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15984. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15985. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15986. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15987. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15988. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15989. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15990. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15991. (when (featurep 'xemacs)
  15992. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15993. (defconst org-speed-commands-default
  15994. '(
  15995. ("Outline Navigation")
  15996. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15997. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15998. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15999. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16000. ("u" . (org-speed-move-safe 'outline-up-heading))
  16001. ("j" . org-goto)
  16002. ("g" . (org-refile t))
  16003. ("Outline Visibility")
  16004. ("c" . org-cycle)
  16005. ("C" . org-shifttab)
  16006. (" " . org-display-outline-path)
  16007. ("Outline Structure Editing")
  16008. ("U" . org-shiftmetaup)
  16009. ("D" . org-shiftmetadown)
  16010. ("r" . org-metaright)
  16011. ("l" . org-metaleft)
  16012. ("R" . org-shiftmetaright)
  16013. ("L" . org-shiftmetaleft)
  16014. ("i" . (progn (forward-char 1) (call-interactively
  16015. 'org-insert-heading-respect-content)))
  16016. ("^" . org-sort)
  16017. ("w" . org-refile)
  16018. ("a" . org-archive-subtree-default-with-confirmation)
  16019. ("." . org-mark-subtree)
  16020. ("Clock Commands")
  16021. ("I" . org-clock-in)
  16022. ("O" . org-clock-out)
  16023. ("Meta Data Editing")
  16024. ("t" . org-todo)
  16025. ("0" . (org-priority ?\ ))
  16026. ("1" . (org-priority ?A))
  16027. ("2" . (org-priority ?B))
  16028. ("3" . (org-priority ?C))
  16029. (";" . org-set-tags-command)
  16030. ("e" . org-set-effort)
  16031. ("Agenda Views etc")
  16032. ("v" . org-agenda)
  16033. ("/" . org-sparse-tree)
  16034. ("Misc")
  16035. ("o" . org-open-at-point)
  16036. ("?" . org-speed-command-help)
  16037. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16038. (">" . (org-agenda-remove-restriction-lock))
  16039. )
  16040. "The default speed commands.")
  16041. (defun org-print-speed-command (e)
  16042. (if (> (length (car e)) 1)
  16043. (progn
  16044. (princ "\n")
  16045. (princ (car e))
  16046. (princ "\n")
  16047. (princ (make-string (length (car e)) ?-))
  16048. (princ "\n"))
  16049. (princ (car e))
  16050. (princ " ")
  16051. (if (symbolp (cdr e))
  16052. (princ (symbol-name (cdr e)))
  16053. (prin1 (cdr e)))
  16054. (princ "\n")))
  16055. (defun org-speed-command-help ()
  16056. "Show the available speed commands."
  16057. (interactive)
  16058. (if (not org-use-speed-commands)
  16059. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16060. (with-output-to-temp-buffer "*Help*"
  16061. (princ "User-defined Speed commands\n===========================\n")
  16062. (mapc 'org-print-speed-command org-speed-commands-user)
  16063. (princ "\n")
  16064. (princ "Built-in Speed commands\n=======================\n")
  16065. (mapc 'org-print-speed-command org-speed-commands-default))
  16066. (with-current-buffer "*Help*"
  16067. (setq truncate-lines t))))
  16068. (defun org-speed-move-safe (cmd)
  16069. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16070. If not, return to the original position and throw an error."
  16071. (interactive)
  16072. (let ((pos (point)))
  16073. (call-interactively cmd)
  16074. (unless (and (bolp) (org-at-heading-p))
  16075. (goto-char pos)
  16076. (error "Boundary reached while executing %s" cmd))))
  16077. (defvar org-self-insert-command-undo-counter 0)
  16078. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16079. (defvar org-speed-command nil)
  16080. (defun org-speed-command-default-hook (keys)
  16081. "Hook for activating single-letter speed commands.
  16082. `org-speed-commands-default' specifies a minimal command set.
  16083. Use `org-speed-commands-user' for further customization."
  16084. (when (or (and (bolp) (looking-at org-outline-regexp))
  16085. (and (functionp org-use-speed-commands)
  16086. (funcall org-use-speed-commands)))
  16087. (cdr (assoc keys (append org-speed-commands-user
  16088. org-speed-commands-default)))))
  16089. (defun org-babel-speed-command-hook (keys)
  16090. "Hook for activating single-letter code block commands."
  16091. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16092. (cdr (assoc keys org-babel-key-bindings))))
  16093. (defcustom org-speed-command-hook
  16094. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16095. "Hook for activating speed commands at strategic locations.
  16096. Hook functions are called in sequence until a valid handler is
  16097. found.
  16098. Each hook takes a single argument, a user-pressed command key
  16099. which is also a `self-insert-command' from the global map.
  16100. Within the hook, examine the cursor position and the command key
  16101. and return nil or a valid handler as appropriate. Handler could
  16102. be one of an interactive command, a function, or a form.
  16103. Set `org-use-speed-commands' to non-nil value to enable this
  16104. hook. The default setting is `org-speed-command-default-hook'."
  16105. :group 'org-structure
  16106. :type 'hook)
  16107. (defun org-self-insert-command (N)
  16108. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16109. If the cursor is in a table looking at whitespace, the whitespace is
  16110. overwritten, and the table is not marked as requiring realignment."
  16111. (interactive "p")
  16112. (org-check-before-invisible-edit 'insert)
  16113. (cond
  16114. ((and org-use-speed-commands
  16115. (setq org-speed-command
  16116. (run-hook-with-args-until-success
  16117. 'org-speed-command-hook (this-command-keys))))
  16118. (cond
  16119. ((commandp org-speed-command)
  16120. (setq this-command org-speed-command)
  16121. (call-interactively org-speed-command))
  16122. ((functionp org-speed-command)
  16123. (funcall org-speed-command))
  16124. ((and org-speed-command (listp org-speed-command))
  16125. (eval org-speed-command))
  16126. (t (let (org-use-speed-commands)
  16127. (call-interactively 'org-self-insert-command)))))
  16128. ((and
  16129. (org-table-p)
  16130. (progn
  16131. ;; check if we blank the field, and if that triggers align
  16132. (and (featurep 'org-table) org-table-auto-blank-field
  16133. (member last-command
  16134. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16135. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16136. ;; got extra space, this field does not determine column width
  16137. (let (org-table-may-need-update) (org-table-blank-field))
  16138. ;; no extra space, this field may determine column width
  16139. (org-table-blank-field)))
  16140. t)
  16141. (eq N 1)
  16142. (looking-at "[^|\n]* |"))
  16143. (let (org-table-may-need-update)
  16144. (goto-char (1- (match-end 0)))
  16145. (delete-char -1)
  16146. (goto-char (match-beginning 0))
  16147. (self-insert-command N)))
  16148. (t
  16149. (setq org-table-may-need-update t)
  16150. (self-insert-command N)
  16151. (org-fix-tags-on-the-fly)
  16152. (if org-self-insert-cluster-for-undo
  16153. (if (not (eq last-command 'org-self-insert-command))
  16154. (setq org-self-insert-command-undo-counter 1)
  16155. (if (>= org-self-insert-command-undo-counter 20)
  16156. (setq org-self-insert-command-undo-counter 1)
  16157. (and (> org-self-insert-command-undo-counter 0)
  16158. buffer-undo-list (listp buffer-undo-list)
  16159. (not (cadr buffer-undo-list)) ; remove nil entry
  16160. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16161. (setq org-self-insert-command-undo-counter
  16162. (1+ org-self-insert-command-undo-counter))))))))
  16163. (defun org-check-before-invisible-edit (kind)
  16164. "Check is editing if kind KIND would be dangerous with invisible text around.
  16165. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16166. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16167. (if (and org-catch-invisible-edits
  16168. (or (not (boundp 'visible-mode)) (not visible-mode))
  16169. (or (get-char-property (point) 'invisible)
  16170. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16171. ;; OK, we need to take a closer look
  16172. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16173. (invisible-before-point (if (bobp) nil (get-char-property
  16174. (1- (point)) 'invisible)))
  16175. (border-and-ok-direction
  16176. (or
  16177. ;; Check if we are acting predictably before invisible text
  16178. (and invisible-at-point (not invisible-before-point)
  16179. (memq kind '(insert delete-backward)))
  16180. ;; Check if we are acting predictably after invisible text
  16181. ;; This works not well, and I have turned it off. It seems
  16182. ;; better to always show and stop after invisible text.
  16183. ;; (and (not invisible-at-point) invisible-before-point
  16184. ;; (memq kind '(insert delete)))
  16185. )))
  16186. (when (or (memq invisible-at-point '(outline org-hide-block))
  16187. (memq invisible-before-point '(outline org-hide-block)))
  16188. (if (eq org-catch-invisible-edits 'error)
  16189. (error "Editing in invisible areas is prohibited - make visible first"))
  16190. ;; Make the area visible
  16191. (save-excursion
  16192. (if invisible-before-point
  16193. (goto-char (previous-single-char-property-change
  16194. (point) 'invisible)))
  16195. (org-cycle))
  16196. (cond
  16197. ((eq org-catch-invisible-edits 'show)
  16198. ;; That's it, we do the edit after showing
  16199. (message
  16200. "Unfolding invisible region around point before editing")
  16201. (sit-for 1))
  16202. ((and (eq org-catch-invisible-edits 'smart)
  16203. border-and-ok-direction)
  16204. (message "Unfolding invisible region around point before editing"))
  16205. (t
  16206. ;; Don't do the edit, make the user repeat it in full visibility
  16207. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16208. (defun org-fix-tags-on-the-fly ()
  16209. (when (and (equal (char-after (point-at-bol)) ?*)
  16210. (org-at-heading-p))
  16211. (org-align-tags-here org-tags-column)))
  16212. (defun org-delete-backward-char (N)
  16213. "Like `delete-backward-char', insert whitespace at field end in tables.
  16214. When deleting backwards, in tables this function will insert whitespace in
  16215. front of the next \"|\" separator, to keep the table aligned. The table will
  16216. still be marked for re-alignment if the field did fill the entire column,
  16217. because, in this case the deletion might narrow the column."
  16218. (interactive "p")
  16219. (org-check-before-invisible-edit 'delete-backward)
  16220. (if (and (org-table-p)
  16221. (eq N 1)
  16222. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16223. (looking-at ".*?|"))
  16224. (let ((pos (point))
  16225. (noalign (looking-at "[^|\n\r]* |"))
  16226. (c org-table-may-need-update))
  16227. (backward-delete-char N)
  16228. (if (not overwrite-mode)
  16229. (progn
  16230. (skip-chars-forward "^|")
  16231. (insert " ")
  16232. (goto-char (1- pos))))
  16233. ;; noalign: if there were two spaces at the end, this field
  16234. ;; does not determine the width of the column.
  16235. (if noalign (setq org-table-may-need-update c)))
  16236. (backward-delete-char N)
  16237. (org-fix-tags-on-the-fly)))
  16238. (defun org-delete-char (N)
  16239. "Like `delete-char', but insert whitespace at field end in tables.
  16240. When deleting characters, in tables this function will insert whitespace in
  16241. front of the next \"|\" separator, to keep the table aligned. The table will
  16242. still be marked for re-alignment if the field did fill the entire column,
  16243. because, in this case the deletion might narrow the column."
  16244. (interactive "p")
  16245. (org-check-before-invisible-edit 'delete)
  16246. (if (and (org-table-p)
  16247. (not (bolp))
  16248. (not (= (char-after) ?|))
  16249. (eq N 1))
  16250. (if (looking-at ".*?|")
  16251. (let ((pos (point))
  16252. (noalign (looking-at "[^|\n\r]* |"))
  16253. (c org-table-may-need-update))
  16254. (replace-match (concat
  16255. (substring (match-string 0) 1 -1)
  16256. " |"))
  16257. (goto-char pos)
  16258. ;; noalign: if there were two spaces at the end, this field
  16259. ;; does not determine the width of the column.
  16260. (if noalign (setq org-table-may-need-update c)))
  16261. (delete-char N))
  16262. (delete-char N)
  16263. (org-fix-tags-on-the-fly)))
  16264. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16265. (put 'org-self-insert-command 'delete-selection t)
  16266. (put 'orgtbl-self-insert-command 'delete-selection t)
  16267. (put 'org-delete-char 'delete-selection 'supersede)
  16268. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16269. (put 'org-yank 'delete-selection 'yank)
  16270. ;; Make `flyspell-mode' delay after some commands
  16271. (put 'org-self-insert-command 'flyspell-delayed t)
  16272. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16273. (put 'org-delete-char 'flyspell-delayed t)
  16274. (put 'org-delete-backward-char 'flyspell-delayed t)
  16275. ;; Make pabbrev-mode expand after org-mode commands
  16276. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16277. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16278. ;; How to do this: Measure non-white length of current string
  16279. ;; If equal to column width, we should realign.
  16280. (defun org-remap (map &rest commands)
  16281. "In MAP, remap the functions given in COMMANDS.
  16282. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16283. (let (new old)
  16284. (while commands
  16285. (setq old (pop commands) new (pop commands))
  16286. (if (fboundp 'command-remapping)
  16287. (org-defkey map (vector 'remap old) new)
  16288. (substitute-key-definition old new map global-map)))))
  16289. (when (eq org-enable-table-editor 'optimized)
  16290. ;; If the user wants maximum table support, we need to hijack
  16291. ;; some standard editing functions
  16292. (org-remap org-mode-map
  16293. 'self-insert-command 'org-self-insert-command
  16294. 'delete-char 'org-delete-char
  16295. 'delete-backward-char 'org-delete-backward-char)
  16296. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16297. (defvar org-ctrl-c-ctrl-c-hook nil
  16298. "Hook for functions attaching themselves to `C-c C-c'.
  16299. This can be used to add additional functionality to the C-c C-c
  16300. key which executes context-dependent commands. This hook is run
  16301. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16302. run after the last test.
  16303. Each function will be called with no arguments. The function
  16304. must check if the context is appropriate for it to act. If yes,
  16305. it should do its thing and then return a non-nil value. If the
  16306. context is wrong, just do nothing and return nil.")
  16307. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16308. "Hook for functions attaching themselves to `C-c C-c'.
  16309. This can be used to add additional functionality to the C-c C-c
  16310. key which executes context-dependent commands. This hook is run
  16311. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16312. before the first test.
  16313. Each function will be called with no arguments. The function
  16314. must check if the context is appropriate for it to act. If yes,
  16315. it should do its thing and then return a non-nil value. If the
  16316. context is wrong, just do nothing and return nil.")
  16317. (defvar org-tab-first-hook nil
  16318. "Hook for functions to attach themselves to TAB.
  16319. See `org-ctrl-c-ctrl-c-hook' for more information.
  16320. This hook runs as the first action when TAB is pressed, even before
  16321. `org-cycle' messes around with the `outline-regexp' to cater for
  16322. inline tasks and plain list item folding.
  16323. If any function in this hook returns t, any other actions that
  16324. would have been caused by TAB (such as table field motion or visibility
  16325. cycling) will not occur.")
  16326. (defvar org-tab-after-check-for-table-hook nil
  16327. "Hook for functions to attach themselves to TAB.
  16328. See `org-ctrl-c-ctrl-c-hook' for more information.
  16329. This hook runs after it has been established that the cursor is not in a
  16330. table, but before checking if the cursor is in a headline or if global cycling
  16331. should be done.
  16332. If any function in this hook returns t, not other actions like visibility
  16333. cycling will be done.")
  16334. (defvar org-tab-after-check-for-cycling-hook nil
  16335. "Hook for functions to attach themselves to TAB.
  16336. See `org-ctrl-c-ctrl-c-hook' for more information.
  16337. This hook runs after it has been established that not table field motion and
  16338. not visibility should be done because of current context. This is probably
  16339. the place where a package like yasnippets can hook in.")
  16340. (defvar org-tab-before-tab-emulation-hook nil
  16341. "Hook for functions to attach themselves to TAB.
  16342. See `org-ctrl-c-ctrl-c-hook' for more information.
  16343. This hook runs after every other options for TAB have been exhausted, but
  16344. before indentation and \t insertion takes place.")
  16345. (defvar org-metaleft-hook nil
  16346. "Hook for functions attaching themselves to `M-left'.
  16347. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16348. (defvar org-metaright-hook nil
  16349. "Hook for functions attaching themselves to `M-right'.
  16350. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16351. (defvar org-metaup-hook nil
  16352. "Hook for functions attaching themselves to `M-up'.
  16353. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16354. (defvar org-metadown-hook nil
  16355. "Hook for functions attaching themselves to `M-down'.
  16356. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16357. (defvar org-shiftmetaleft-hook nil
  16358. "Hook for functions attaching themselves to `M-S-left'.
  16359. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16360. (defvar org-shiftmetaright-hook nil
  16361. "Hook for functions attaching themselves to `M-S-right'.
  16362. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16363. (defvar org-shiftmetaup-hook nil
  16364. "Hook for functions attaching themselves to `M-S-up'.
  16365. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16366. (defvar org-shiftmetadown-hook nil
  16367. "Hook for functions attaching themselves to `M-S-down'.
  16368. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16369. (defvar org-metareturn-hook nil
  16370. "Hook for functions attaching themselves to `M-RET'.
  16371. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16372. (defvar org-shiftup-hook nil
  16373. "Hook for functions attaching themselves to `S-up'.
  16374. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16375. (defvar org-shiftup-final-hook nil
  16376. "Hook for functions attaching themselves to `S-up'.
  16377. This one runs after all other options except shift-select have been excluded.
  16378. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16379. (defvar org-shiftdown-hook nil
  16380. "Hook for functions attaching themselves to `S-down'.
  16381. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16382. (defvar org-shiftdown-final-hook nil
  16383. "Hook for functions attaching themselves to `S-down'.
  16384. This one runs after all other options except shift-select have been excluded.
  16385. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16386. (defvar org-shiftleft-hook nil
  16387. "Hook for functions attaching themselves to `S-left'.
  16388. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16389. (defvar org-shiftleft-final-hook nil
  16390. "Hook for functions attaching themselves to `S-left'.
  16391. This one runs after all other options except shift-select have been excluded.
  16392. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16393. (defvar org-shiftright-hook nil
  16394. "Hook for functions attaching themselves to `S-right'.
  16395. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16396. (defvar org-shiftright-final-hook nil
  16397. "Hook for functions attaching themselves to `S-right'.
  16398. This one runs after all other options except shift-select have been excluded.
  16399. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16400. (defun org-modifier-cursor-error ()
  16401. "Throw an error, a modified cursor command was applied in wrong context."
  16402. (error "This command is active in special context like tables, headlines or items"))
  16403. (defun org-shiftselect-error ()
  16404. "Throw an error because Shift-Cursor command was applied in wrong context."
  16405. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16406. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16407. (error "This command works only in special context like headlines or timestamps")))
  16408. (defun org-call-for-shift-select (cmd)
  16409. (let ((this-command-keys-shift-translated t))
  16410. (call-interactively cmd)))
  16411. (defun org-shifttab (&optional arg)
  16412. "Global visibility cycling or move to previous table field.
  16413. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16414. on context.
  16415. See the individual commands for more information."
  16416. (interactive "P")
  16417. (cond
  16418. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16419. ((integerp arg)
  16420. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16421. (message "Content view to level: %d" arg)
  16422. (org-content (prefix-numeric-value arg2))
  16423. (setq org-cycle-global-status 'overview)))
  16424. (t (call-interactively 'org-global-cycle))))
  16425. (defun org-shiftmetaleft ()
  16426. "Promote subtree or delete table column.
  16427. Calls `org-promote-subtree', `org-outdent-item',
  16428. or `org-table-delete-column', depending on context.
  16429. See the individual commands for more information."
  16430. (interactive)
  16431. (cond
  16432. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16433. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16434. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16435. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16436. (t (org-modifier-cursor-error))))
  16437. (defun org-shiftmetaright ()
  16438. "Demote subtree or insert table column.
  16439. Calls `org-demote-subtree', `org-indent-item',
  16440. or `org-table-insert-column', depending on context.
  16441. See the individual commands for more information."
  16442. (interactive)
  16443. (cond
  16444. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16445. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16446. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16447. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16448. (t (org-modifier-cursor-error))))
  16449. (defun org-shiftmetaup (&optional arg)
  16450. "Move subtree up or kill table row.
  16451. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16452. `org-move-item-up' depending on context. See the individual commands
  16453. for more information."
  16454. (interactive "P")
  16455. (cond
  16456. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16457. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16458. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16459. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16460. (t (org-modifier-cursor-error))))
  16461. (defun org-shiftmetadown (&optional arg)
  16462. "Move subtree down or insert table row.
  16463. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16464. `org-move-item-down', depending on context. See the individual
  16465. commands for more information."
  16466. (interactive "P")
  16467. (cond
  16468. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16469. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16470. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16471. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16472. (t (org-modifier-cursor-error))))
  16473. (defsubst org-hidden-tree-error ()
  16474. (error
  16475. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16476. (defun org-metaleft (&optional arg)
  16477. "Promote heading or move table column to left.
  16478. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16479. With no specific context, calls the Emacs default `backward-word'.
  16480. See the individual commands for more information."
  16481. (interactive "P")
  16482. (cond
  16483. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16484. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16485. ((org-with-limited-levels
  16486. (or (org-at-heading-p)
  16487. (and (org-region-active-p)
  16488. (save-excursion
  16489. (goto-char (region-beginning))
  16490. (org-at-heading-p)))))
  16491. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16492. (call-interactively 'org-do-promote))
  16493. ;; At an inline task.
  16494. ((org-at-heading-p)
  16495. (call-interactively 'org-inlinetask-promote))
  16496. ((or (org-at-item-p)
  16497. (and (org-region-active-p)
  16498. (save-excursion
  16499. (goto-char (region-beginning))
  16500. (org-at-item-p))))
  16501. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16502. (call-interactively 'org-outdent-item))
  16503. (t (call-interactively 'backward-word))))
  16504. (defun org-metaright (&optional arg)
  16505. "Demote subtree or move table column to right.
  16506. Calls `org-do-demote' or `org-table-move-column', depending on context.
  16507. With no specific context, calls the Emacs default `forward-word'.
  16508. See the individual commands for more information."
  16509. (interactive "P")
  16510. (cond
  16511. ((run-hook-with-args-until-success 'org-metaright-hook))
  16512. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16513. ((org-with-limited-levels
  16514. (or (org-at-heading-p)
  16515. (and (org-region-active-p)
  16516. (save-excursion
  16517. (goto-char (region-beginning))
  16518. (org-at-heading-p)))))
  16519. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16520. (call-interactively 'org-do-demote))
  16521. ;; At an inline task.
  16522. ((org-at-heading-p)
  16523. (call-interactively 'org-inlinetask-demote))
  16524. ((or (org-at-item-p)
  16525. (and (org-region-active-p)
  16526. (save-excursion
  16527. (goto-char (region-beginning))
  16528. (org-at-item-p))))
  16529. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16530. (call-interactively 'org-indent-item))
  16531. (t (call-interactively 'forward-word))))
  16532. (defun org-check-for-hidden (what)
  16533. "Check if there are hidden headlines/items in the current visual line.
  16534. WHAT can be either `headlines' or `items'. If the current line is
  16535. an outline or item heading and it has a folded subtree below it,
  16536. this function returns t, nil otherwise."
  16537. (let ((re (cond
  16538. ((eq what 'headlines) org-outline-regexp-bol)
  16539. ((eq what 'items) (org-item-beginning-re))
  16540. (t (error "This should not happen"))))
  16541. beg end)
  16542. (save-excursion
  16543. (catch 'exit
  16544. (unless (org-region-active-p)
  16545. (setq beg (point-at-bol))
  16546. (beginning-of-line 2)
  16547. (while (and (not (eobp)) ;; this is like `next-line'
  16548. (get-char-property (1- (point)) 'invisible))
  16549. (beginning-of-line 2))
  16550. (setq end (point))
  16551. (goto-char beg)
  16552. (goto-char (point-at-eol))
  16553. (setq end (max end (point)))
  16554. (while (re-search-forward re end t)
  16555. (if (get-char-property (match-beginning 0) 'invisible)
  16556. (throw 'exit t))))
  16557. nil))))
  16558. (defun org-metaup (&optional arg)
  16559. "Move subtree up or move table row up.
  16560. Calls `org-move-subtree-up' or `org-table-move-row' or
  16561. `org-move-item-up', depending on context. See the individual commands
  16562. for more information."
  16563. (interactive "P")
  16564. (cond
  16565. ((run-hook-with-args-until-success 'org-metaup-hook))
  16566. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16567. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16568. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16569. (t (transpose-lines 1) (beginning-of-line -1))))
  16570. (defun org-metadown (&optional arg)
  16571. "Move subtree down or move table row down.
  16572. Calls `org-move-subtree-down' or `org-table-move-row' or
  16573. `org-move-item-down', depending on context. See the individual
  16574. commands for more information."
  16575. (interactive "P")
  16576. (cond
  16577. ((run-hook-with-args-until-success 'org-metadown-hook))
  16578. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16579. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16580. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16581. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16582. (defun org-shiftup (&optional arg)
  16583. "Increase item in timestamp or increase priority of current headline.
  16584. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16585. depending on context. See the individual commands for more information."
  16586. (interactive "P")
  16587. (cond
  16588. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16589. ((and org-support-shift-select (org-region-active-p))
  16590. (org-call-for-shift-select 'previous-line))
  16591. ((org-at-timestamp-p t)
  16592. (call-interactively (if org-edit-timestamp-down-means-later
  16593. 'org-timestamp-down 'org-timestamp-up)))
  16594. ((and (not (eq org-support-shift-select 'always))
  16595. org-enable-priority-commands
  16596. (org-at-heading-p))
  16597. (call-interactively 'org-priority-up))
  16598. ((and (not org-support-shift-select) (org-at-item-p))
  16599. (call-interactively 'org-previous-item))
  16600. ((org-clocktable-try-shift 'up arg))
  16601. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16602. (org-support-shift-select
  16603. (org-call-for-shift-select 'previous-line))
  16604. (t (org-shiftselect-error))))
  16605. (defun org-shiftdown (&optional arg)
  16606. "Decrease item in timestamp or decrease priority of current headline.
  16607. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16608. depending on context. See the individual commands for more information."
  16609. (interactive "P")
  16610. (cond
  16611. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16612. ((and org-support-shift-select (org-region-active-p))
  16613. (org-call-for-shift-select 'next-line))
  16614. ((org-at-timestamp-p t)
  16615. (call-interactively (if org-edit-timestamp-down-means-later
  16616. 'org-timestamp-up 'org-timestamp-down)))
  16617. ((and (not (eq org-support-shift-select 'always))
  16618. org-enable-priority-commands
  16619. (org-at-heading-p))
  16620. (call-interactively 'org-priority-down))
  16621. ((and (not org-support-shift-select) (org-at-item-p))
  16622. (call-interactively 'org-next-item))
  16623. ((org-clocktable-try-shift 'down arg))
  16624. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16625. (org-support-shift-select
  16626. (org-call-for-shift-select 'next-line))
  16627. (t (org-shiftselect-error))))
  16628. (defun org-shiftright (&optional arg)
  16629. "Cycle the thing at point or in the current line, depending on context.
  16630. Depending on context, this does one of the following:
  16631. - switch a timestamp at point one day into the future
  16632. - on a headline, switch to the next TODO keyword.
  16633. - on an item, switch entire list to the next bullet type
  16634. - on a property line, switch to the next allowed value
  16635. - on a clocktable definition line, move time block into the future"
  16636. (interactive "P")
  16637. (cond
  16638. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16639. ((and org-support-shift-select (org-region-active-p))
  16640. (org-call-for-shift-select 'forward-char))
  16641. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16642. ((and (not (eq org-support-shift-select 'always))
  16643. (org-at-heading-p))
  16644. (let ((org-inhibit-logging
  16645. (not org-treat-S-cursor-todo-selection-as-state-change))
  16646. (org-inhibit-blocking
  16647. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16648. (org-call-with-arg 'org-todo 'right)))
  16649. ((or (and org-support-shift-select
  16650. (not (eq org-support-shift-select 'always))
  16651. (org-at-item-bullet-p))
  16652. (and (not org-support-shift-select) (org-at-item-p)))
  16653. (org-call-with-arg 'org-cycle-list-bullet nil))
  16654. ((and (not (eq org-support-shift-select 'always))
  16655. (org-at-property-p))
  16656. (call-interactively 'org-property-next-allowed-value))
  16657. ((org-clocktable-try-shift 'right arg))
  16658. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16659. (org-support-shift-select
  16660. (org-call-for-shift-select 'forward-char))
  16661. (t (org-shiftselect-error))))
  16662. (defun org-shiftleft (&optional arg)
  16663. "Cycle the thing at point or in the current line, depending on context.
  16664. Depending on context, this does one of the following:
  16665. - switch a timestamp at point one day into the past
  16666. - on a headline, switch to the previous TODO keyword.
  16667. - on an item, switch entire list to the previous bullet type
  16668. - on a property line, switch to the previous allowed value
  16669. - on a clocktable definition line, move time block into the past"
  16670. (interactive "P")
  16671. (cond
  16672. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16673. ((and org-support-shift-select (org-region-active-p))
  16674. (org-call-for-shift-select 'backward-char))
  16675. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16676. ((and (not (eq org-support-shift-select 'always))
  16677. (org-at-heading-p))
  16678. (let ((org-inhibit-logging
  16679. (not org-treat-S-cursor-todo-selection-as-state-change))
  16680. (org-inhibit-blocking
  16681. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16682. (org-call-with-arg 'org-todo 'left)))
  16683. ((or (and org-support-shift-select
  16684. (not (eq org-support-shift-select 'always))
  16685. (org-at-item-bullet-p))
  16686. (and (not org-support-shift-select) (org-at-item-p)))
  16687. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16688. ((and (not (eq org-support-shift-select 'always))
  16689. (org-at-property-p))
  16690. (call-interactively 'org-property-previous-allowed-value))
  16691. ((org-clocktable-try-shift 'left arg))
  16692. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16693. (org-support-shift-select
  16694. (org-call-for-shift-select 'backward-char))
  16695. (t (org-shiftselect-error))))
  16696. (defun org-shiftcontrolright ()
  16697. "Switch to next TODO set."
  16698. (interactive)
  16699. (cond
  16700. ((and org-support-shift-select (org-region-active-p))
  16701. (org-call-for-shift-select 'forward-word))
  16702. ((and (not (eq org-support-shift-select 'always))
  16703. (org-at-heading-p))
  16704. (org-call-with-arg 'org-todo 'nextset))
  16705. (org-support-shift-select
  16706. (org-call-for-shift-select 'forward-word))
  16707. (t (org-shiftselect-error))))
  16708. (defun org-shiftcontrolleft ()
  16709. "Switch to previous TODO set."
  16710. (interactive)
  16711. (cond
  16712. ((and org-support-shift-select (org-region-active-p))
  16713. (org-call-for-shift-select 'backward-word))
  16714. ((and (not (eq org-support-shift-select 'always))
  16715. (org-at-heading-p))
  16716. (org-call-with-arg 'org-todo 'previousset))
  16717. (org-support-shift-select
  16718. (org-call-for-shift-select 'backward-word))
  16719. (t (org-shiftselect-error))))
  16720. (defun org-shiftcontrolup ()
  16721. "Change timestamps synchronously up in CLOCK log lines."
  16722. (interactive)
  16723. (cond ((and (not org-support-shift-select)
  16724. (org-at-clock-log-p)
  16725. (org-at-timestamp-p t))
  16726. (org-clock-timestamps-up))
  16727. (t (org-shiftselect-error))))
  16728. (defun org-shiftcontroldown ()
  16729. "Change timestamps synchronously down in CLOCK log lines."
  16730. (interactive)
  16731. (cond ((and (not org-support-shift-select)
  16732. (org-at-clock-log-p)
  16733. (org-at-timestamp-p t))
  16734. (org-clock-timestamps-down))
  16735. (t (org-shiftselect-error))))
  16736. (defun org-ctrl-c-ret ()
  16737. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16738. (interactive)
  16739. (cond
  16740. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16741. (t (call-interactively 'org-insert-heading))))
  16742. (defun org-find-visible ()
  16743. (let ((s (point)))
  16744. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16745. (get-char-property s 'invisible)))
  16746. s))
  16747. (defun org-find-invisible ()
  16748. (let ((s (point)))
  16749. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16750. (not (get-char-property s 'invisible))))
  16751. s))
  16752. (defun org-copy-visible (beg end)
  16753. "Copy the visible parts of the region."
  16754. (interactive "r")
  16755. (let (snippets s)
  16756. (save-excursion
  16757. (save-restriction
  16758. (narrow-to-region beg end)
  16759. (setq s (goto-char (point-min)))
  16760. (while (not (= (point) (point-max)))
  16761. (goto-char (org-find-invisible))
  16762. (push (buffer-substring s (point)) snippets)
  16763. (setq s (goto-char (org-find-visible))))))
  16764. (kill-new (apply 'concat (nreverse snippets)))))
  16765. (defun org-copy-special ()
  16766. "Copy region in table or copy current subtree.
  16767. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16768. See the individual commands for more information."
  16769. (interactive)
  16770. (call-interactively
  16771. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16772. (defun org-cut-special ()
  16773. "Cut region in table or cut current subtree.
  16774. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16775. See the individual commands for more information."
  16776. (interactive)
  16777. (call-interactively
  16778. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16779. (defun org-paste-special (arg)
  16780. "Paste rectangular region into table, or past subtree relative to level.
  16781. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16782. See the individual commands for more information."
  16783. (interactive "P")
  16784. (if (org-at-table-p)
  16785. (org-table-paste-rectangle)
  16786. (org-paste-subtree arg)))
  16787. (defun org-edit-special (&optional arg)
  16788. "Call a special editor for the stuff at point.
  16789. When at a table, call the formula editor with `org-table-edit-formulas'.
  16790. When at the first line of an src example, call `org-edit-src-code'.
  16791. When in an #+include line, visit the include file. Otherwise call
  16792. `ffap' to visit the file at point."
  16793. (interactive)
  16794. ;; possibly prep session before editing source
  16795. (when arg
  16796. (let* ((info (org-babel-get-src-block-info))
  16797. (lang (nth 0 info))
  16798. (params (nth 2 info))
  16799. (session (cdr (assoc :session params))))
  16800. (when (and info session) ;; we are in a source-code block with a session
  16801. (funcall
  16802. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16803. (cond ;; proceed with `org-edit-special'
  16804. ((save-excursion
  16805. (beginning-of-line 1)
  16806. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16807. (find-file (org-trim (match-string 1))))
  16808. ((org-edit-src-code))
  16809. ((org-edit-fixed-width-region))
  16810. ((org-at-table.el-p)
  16811. (org-edit-src-code))
  16812. ((or (org-at-table-p)
  16813. (save-excursion
  16814. (beginning-of-line 1)
  16815. (looking-at "[ \t]*#\\+TBLFM:")))
  16816. (call-interactively 'org-table-edit-formulas))
  16817. (t (call-interactively 'ffap))))
  16818. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16819. (defun org-ctrl-c-ctrl-c (&optional arg)
  16820. "Set tags in headline, or update according to changed information at point.
  16821. This command does many different things, depending on context:
  16822. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16823. this is what we do.
  16824. - If the cursor is on a statistics cookie, update it.
  16825. - If the cursor is in a headline, prompt for tags and insert them
  16826. into the current line, aligned to `org-tags-column'. When called
  16827. with prefix arg, realign all tags in the current buffer.
  16828. - If the cursor is in one of the special #+KEYWORD lines, this
  16829. triggers scanning the buffer for these lines and updating the
  16830. information.
  16831. - If the cursor is inside a table, realign the table. This command
  16832. works even if the automatic table editor has been turned off.
  16833. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16834. the entire table.
  16835. - If the cursor is at a footnote reference or definition, jump to
  16836. the corresponding definition or references, respectively.
  16837. - If the cursor is a the beginning of a dynamic block, update it.
  16838. - If the current buffer is a capture buffer, close note and file it.
  16839. - If the cursor is on a <<<target>>>, update radio targets and
  16840. corresponding links in this buffer.
  16841. - If the cursor is on a numbered item in a plain list, renumber the
  16842. ordered list.
  16843. - If the cursor is on a checkbox, toggle it.
  16844. - If the cursor is on a code block, evaluate it. The variable
  16845. `org-confirm-babel-evaluate' can be used to control prompting
  16846. before code block evaluation, by default every code block
  16847. evaluation requires confirmation. Code block evaluation can be
  16848. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16849. (interactive "P")
  16850. (let ((org-enable-table-editor t))
  16851. (cond
  16852. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16853. org-occur-highlights
  16854. org-latex-fragment-image-overlays)
  16855. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16856. (org-remove-occur-highlights)
  16857. (org-remove-latex-fragment-image-overlays)
  16858. (message "Temporary highlights/overlays removed from current buffer"))
  16859. ((and (local-variable-p 'org-finish-function (current-buffer))
  16860. (fboundp org-finish-function))
  16861. (funcall org-finish-function))
  16862. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16863. ((org-in-regexp org-ts-regexp-both)
  16864. (org-timestamp-change 0 'day))
  16865. ((or (looking-at org-property-start-re)
  16866. (org-at-property-p))
  16867. (call-interactively 'org-property-action))
  16868. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  16869. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16870. (or (org-at-heading-p) (org-at-item-p)))
  16871. (call-interactively 'org-update-statistics-cookies))
  16872. ((org-at-heading-p) (call-interactively 'org-set-tags))
  16873. ((org-at-table.el-p)
  16874. (message "Use C-c ' to edit table.el tables"))
  16875. ((org-at-table-p)
  16876. (org-table-maybe-eval-formula)
  16877. (if arg
  16878. (call-interactively 'org-table-recalculate)
  16879. (org-table-maybe-recalculate-line))
  16880. (call-interactively 'org-table-align)
  16881. (orgtbl-send-table 'maybe))
  16882. ((or (org-footnote-at-reference-p)
  16883. (org-footnote-at-definition-p))
  16884. (call-interactively 'org-footnote-action))
  16885. ((org-at-item-checkbox-p)
  16886. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16887. ;; list only if at top item.
  16888. (let* ((cbox (match-string 1))
  16889. (struct (org-list-struct))
  16890. (old-struct (copy-tree struct))
  16891. (parents (org-list-parents-alist struct))
  16892. (orderedp (org-entry-get nil "ORDERED"))
  16893. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16894. block-item)
  16895. ;; Use a light version of `org-toggle-checkbox' to avoid
  16896. ;; computing list structure twice.
  16897. (let ((new-box (cond
  16898. ((equal arg '(16)) "[-]")
  16899. ((equal arg '(4)) nil)
  16900. ((equal "[X]" cbox) "[ ]")
  16901. (t "[X]"))))
  16902. (if (and firstp arg)
  16903. ;; If at first item of sub-list, remove check-box from
  16904. ;; every item at the same level.
  16905. (mapc
  16906. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  16907. (org-list-get-all-items
  16908. (point-at-bol) struct (org-list-prevs-alist struct)))
  16909. (org-list-set-checkbox (point-at-bol) struct new-box)))
  16910. ;; Replicate `org-list-write-struct', while grabbing a return
  16911. ;; value from `org-list-struct-fix-box'.
  16912. (org-list-struct-fix-ind struct parents 2)
  16913. (org-list-struct-fix-item-end struct)
  16914. (let ((prevs (org-list-prevs-alist struct)))
  16915. (org-list-struct-fix-bul struct prevs)
  16916. (org-list-struct-fix-ind struct parents)
  16917. (setq block-item
  16918. (org-list-struct-fix-box struct parents prevs orderedp)))
  16919. (org-list-struct-apply-struct struct old-struct)
  16920. (org-update-checkbox-count-maybe)
  16921. (when block-item
  16922. (message
  16923. "Checkboxes were removed due to unchecked box at line %d"
  16924. (org-current-line block-item)))
  16925. (when firstp (org-list-send-list 'maybe))))
  16926. ((org-at-item-p)
  16927. ;; Cursor at an item: repair list. Do checkbox related actions
  16928. ;; only if function was called with an argument. Send list only
  16929. ;; if at top item.
  16930. (let* ((struct (org-list-struct))
  16931. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16932. old-struct)
  16933. (when arg
  16934. (setq old-struct (copy-tree struct))
  16935. (if firstp
  16936. ;; If at first item of sub-list, add check-box to every
  16937. ;; item at the same level.
  16938. (mapc
  16939. (lambda (pos)
  16940. (unless (org-list-get-checkbox pos struct)
  16941. (org-list-set-checkbox pos struct "[ ]")))
  16942. (org-list-get-all-items
  16943. (point-at-bol) struct (org-list-prevs-alist struct)))
  16944. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  16945. (org-list-write-struct
  16946. struct (org-list-parents-alist struct) old-struct)
  16947. (when arg (org-update-checkbox-count-maybe))
  16948. (when firstp (org-list-send-list 'maybe))))
  16949. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16950. ;; Dynamic block
  16951. (beginning-of-line 1)
  16952. (save-excursion (org-update-dblock)))
  16953. ((save-excursion
  16954. (beginning-of-line 1)
  16955. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16956. (cond
  16957. ((equal (match-string 1) "TBLFM")
  16958. ;; Recalculate the table before this line
  16959. (save-excursion
  16960. (beginning-of-line 1)
  16961. (skip-chars-backward " \r\n\t")
  16962. (if (org-at-table-p)
  16963. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16964. (t
  16965. (let ((org-inhibit-startup-visibility-stuff t)
  16966. (org-startup-align-all-tables nil))
  16967. (when (boundp 'org-table-coordinate-overlays)
  16968. (mapc 'delete-overlay org-table-coordinate-overlays)
  16969. (setq org-table-coordinate-overlays nil))
  16970. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16971. (message "Local setup has been refreshed"))))
  16972. ((org-clock-update-time-maybe))
  16973. (t
  16974. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  16975. (error "C-c C-c can do nothing useful at this location"))))))
  16976. (defun org-mode-restart ()
  16977. "Restart Org-mode, to scan again for special lines.
  16978. Also updates the keyword regular expressions."
  16979. (interactive)
  16980. (org-mode)
  16981. (message "Org-mode restarted"))
  16982. (defun org-kill-note-or-show-branches ()
  16983. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16984. (interactive)
  16985. (if (not org-finish-function)
  16986. (progn
  16987. (hide-subtree)
  16988. (call-interactively 'show-branches))
  16989. (let ((org-note-abort t))
  16990. (funcall org-finish-function))))
  16991. (defun org-return (&optional indent)
  16992. "Goto next table row or insert a newline.
  16993. Calls `org-table-next-row' or `newline', depending on context.
  16994. See the individual commands for more information."
  16995. (interactive)
  16996. (cond
  16997. ((bobp) (if indent (newline-and-indent) (newline)))
  16998. ((org-at-table-p)
  16999. (org-table-justify-field-maybe)
  17000. (call-interactively 'org-table-next-row))
  17001. ;; when `newline-and-indent' is called within a list, make sure
  17002. ;; text moved stays inside the item.
  17003. ((and (org-in-item-p) indent)
  17004. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17005. (progn
  17006. (save-match-data (newline))
  17007. (org-indent-line-to (length (match-string 0))))
  17008. (let ((ind (org-get-indentation)))
  17009. (newline)
  17010. (if (org-looking-back org-list-end-re)
  17011. (org-indent-line-function)
  17012. (org-indent-line-to ind)))))
  17013. ((and org-return-follows-link
  17014. (let ((tprop (get-text-property (point) 'face)))
  17015. (or (eq tprop 'org-link)
  17016. (and (listp tprop) (memq 'org-link tprop)))))
  17017. (call-interactively 'org-open-at-point))
  17018. ((and (org-at-heading-p)
  17019. (looking-at
  17020. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17021. (org-show-entry)
  17022. (end-of-line 1)
  17023. (newline))
  17024. (t (if indent (newline-and-indent) (newline)))))
  17025. (defun org-return-indent ()
  17026. "Goto next table row or insert a newline and indent.
  17027. Calls `org-table-next-row' or `newline-and-indent', depending on
  17028. context. See the individual commands for more information."
  17029. (interactive)
  17030. (org-return t))
  17031. (defun org-ctrl-c-star ()
  17032. "Compute table, or change heading status of lines.
  17033. Calls `org-table-recalculate' or `org-toggle-heading',
  17034. depending on context."
  17035. (interactive)
  17036. (cond
  17037. ((org-at-table-p)
  17038. (call-interactively 'org-table-recalculate))
  17039. (t
  17040. ;; Convert all lines in region to list items
  17041. (call-interactively 'org-toggle-heading))))
  17042. (defun org-ctrl-c-minus ()
  17043. "Insert separator line in table or modify bullet status of line.
  17044. Also turns a plain line or a region of lines into list items.
  17045. Calls `org-table-insert-hline', `org-toggle-item', or
  17046. `org-cycle-list-bullet', depending on context."
  17047. (interactive)
  17048. (cond
  17049. ((org-at-table-p)
  17050. (call-interactively 'org-table-insert-hline))
  17051. ((org-region-active-p)
  17052. (call-interactively 'org-toggle-item))
  17053. ((org-in-item-p)
  17054. (call-interactively 'org-cycle-list-bullet))
  17055. (t
  17056. (call-interactively 'org-toggle-item))))
  17057. (defun org-toggle-item (arg)
  17058. "Convert headings or normal lines to items, items to normal lines.
  17059. If there is no active region, only the current line is considered.
  17060. If the first non blank line in the region is an headline, convert
  17061. all headlines to items, shifting text accordingly.
  17062. If it is an item, convert all items to normal lines.
  17063. If it is normal text, change region into an item. With a prefix
  17064. argument ARG, change each line in region into an item."
  17065. (interactive "P")
  17066. (let ((shift-text
  17067. (function
  17068. ;; Shift text in current section to IND, from point to END.
  17069. ;; The function leaves point to END line.
  17070. (lambda (ind end)
  17071. (let ((min-i 1000) (end (copy-marker end)))
  17072. ;; First determine the minimum indentation (MIN-I) of
  17073. ;; the text.
  17074. (save-excursion
  17075. (catch 'exit
  17076. (while (< (point) end)
  17077. (let ((i (org-get-indentation)))
  17078. (cond
  17079. ;; Skip blank lines and inline tasks.
  17080. ((looking-at "^[ \t]*$"))
  17081. ((looking-at org-outline-regexp-bol))
  17082. ;; We can't find less than 0 indentation.
  17083. ((zerop i) (throw 'exit (setq min-i 0)))
  17084. ((< i min-i) (setq min-i i))))
  17085. (forward-line))))
  17086. ;; Then indent each line so that a line indented to
  17087. ;; MIN-I becomes indented to IND. Ignore blank lines
  17088. ;; and inline tasks in the process.
  17089. (let ((delta (- ind min-i)))
  17090. (while (< (point) end)
  17091. (unless (or (looking-at "^[ \t]*$")
  17092. (looking-at org-outline-regexp-bol))
  17093. (org-indent-line-to (+ (org-get-indentation) delta)))
  17094. (forward-line)))))))
  17095. (skip-blanks
  17096. (function
  17097. ;; Return beginning of first non-blank line, starting from
  17098. ;; line at POS.
  17099. (lambda (pos)
  17100. (save-excursion
  17101. (goto-char pos)
  17102. (skip-chars-forward " \r\t\n")
  17103. (point-at-bol)))))
  17104. beg end)
  17105. ;; Determine boundaries of changes.
  17106. (if (org-region-active-p)
  17107. (setq beg (funcall skip-blanks (region-beginning))
  17108. end (copy-marker (region-end)))
  17109. (setq beg (funcall skip-blanks (point-at-bol))
  17110. end (copy-marker (point-at-eol))))
  17111. ;; Depending on the starting line, choose an action on the text
  17112. ;; between BEG and END.
  17113. (org-with-limited-levels
  17114. (save-excursion
  17115. (goto-char beg)
  17116. (cond
  17117. ;; Case 1. Start at an item: de-itemize. Note that it only
  17118. ;; happens when a region is active: `org-ctrl-c-minus'
  17119. ;; would call `org-cycle-list-bullet' otherwise.
  17120. ((org-at-item-p)
  17121. (while (< (point) end)
  17122. (when (org-at-item-p)
  17123. (skip-chars-forward " \t")
  17124. (delete-region (point) (match-end 0)))
  17125. (forward-line)))
  17126. ;; Case 2. Start at an heading: convert to items.
  17127. ((org-at-heading-p)
  17128. (let* ((bul (org-list-bullet-string "-"))
  17129. (bul-len (length bul))
  17130. ;; Indentation of the first heading. It should be
  17131. ;; relative to the indentation of its parent, if any.
  17132. (start-ind (save-excursion
  17133. (cond
  17134. ((not org-adapt-indentation) 0)
  17135. ((not (outline-previous-heading)) 0)
  17136. (t (length (match-string 0))))))
  17137. ;; Level of first heading. Further headings will be
  17138. ;; compared to it to determine hierarchy in the list.
  17139. (ref-level (org-reduced-level (org-outline-level))))
  17140. (while (< (point) end)
  17141. (let* ((level (org-reduced-level (org-outline-level)))
  17142. (delta (max 0 (- level ref-level))))
  17143. ;; If current headline is less indented than the first
  17144. ;; one, set it as reference, in order to preserve
  17145. ;; subtrees.
  17146. (when (< level ref-level) (setq ref-level level))
  17147. (replace-match bul t t)
  17148. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17149. ;; Ensure all text down to END (or SECTION-END) belongs
  17150. ;; to the newly created item.
  17151. (let ((section-end (save-excursion
  17152. (or (outline-next-heading) (point)))))
  17153. (forward-line)
  17154. (funcall shift-text
  17155. (+ start-ind (* (1+ delta) bul-len))
  17156. (min end section-end)))))))
  17157. ;; Case 3. Normal line with ARG: turn each non-item line into
  17158. ;; an item.
  17159. (arg
  17160. (while (< (point) end)
  17161. (unless (or (org-at-heading-p) (org-at-item-p))
  17162. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17163. (replace-match
  17164. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17165. (forward-line)))
  17166. ;; Case 4. Normal line without ARG: make the first line of
  17167. ;; region an item, and shift indentation of others
  17168. ;; lines to set them as item's body.
  17169. (t (let* ((bul (org-list-bullet-string "-"))
  17170. (bul-len (length bul))
  17171. (ref-ind (org-get-indentation)))
  17172. (skip-chars-forward " \t")
  17173. (insert bul)
  17174. (forward-line)
  17175. (while (< (point) end)
  17176. ;; Ensure that lines less indented than first one
  17177. ;; still get included in item body.
  17178. (funcall shift-text
  17179. (+ ref-ind bul-len)
  17180. (min end (save-excursion (or (outline-next-heading)
  17181. (point)))))
  17182. (forward-line)))))))))
  17183. (defun org-toggle-heading (&optional nstars)
  17184. "Convert headings to normal text, or items or text to headings.
  17185. If there is no active region, only the current line is considered.
  17186. If the first non blank line is an headline, remove the stars from
  17187. all headlines in the region.
  17188. If it is a plain list item, turn all plain list items into headings.
  17189. If it is a normal line, turn each and every normal line (i.e. not
  17190. an heading or an item) in the region into a heading.
  17191. When converting a line into a heading, the number of stars is chosen
  17192. such that the lines become children of the current entry. However,
  17193. when a prefix argument is given, its value determines the number of
  17194. stars to add."
  17195. (interactive "P")
  17196. (let ((skip-blanks
  17197. (function
  17198. ;; Return beginning of first non-blank line, starting from
  17199. ;; line at POS.
  17200. (lambda (pos)
  17201. (save-excursion
  17202. (goto-char pos)
  17203. (skip-chars-forward " \r\t\n")
  17204. (point-at-bol)))))
  17205. beg end)
  17206. ;; Determine boundaries of changes. If region ends at a bol, do
  17207. ;; not consider the last line to be in the region.
  17208. (if (org-region-active-p)
  17209. (setq beg (funcall skip-blanks (region-beginning))
  17210. end (copy-marker (save-excursion
  17211. (goto-char (region-end))
  17212. (if (bolp) (point) (point-at-eol)))))
  17213. (setq beg (funcall skip-blanks (point-at-bol))
  17214. end (copy-marker (point-at-eol))))
  17215. ;; Ensure inline tasks don't count as headings.
  17216. (org-with-limited-levels
  17217. (save-excursion
  17218. (goto-char beg)
  17219. (cond
  17220. ;; Case 1. Started at an heading: de-star headings.
  17221. ((org-at-heading-p)
  17222. (while (< (point) end)
  17223. (when (org-at-heading-p t)
  17224. (looking-at org-outline-regexp) (replace-match ""))
  17225. (forward-line)))
  17226. ;; Case 2. Started at an item: change items into headlines.
  17227. ;; One star will be added by `org-list-to-subtree'.
  17228. ((org-at-item-p)
  17229. (let* ((stars (make-string
  17230. (if nstars
  17231. ;; subtract the star that will be added again by
  17232. ;; `org-list-to-subtree'
  17233. (1- (prefix-numeric-value current-prefix-arg))
  17234. (or (org-current-level) 0))
  17235. ?*))
  17236. (add-stars
  17237. (cond (nstars "") ; stars from prefix only
  17238. ((equal stars "") "") ; before first heading
  17239. (org-odd-levels-only "*") ; inside heading, odd
  17240. (t "")))) ; inside heading, oddeven
  17241. (while (< (point) end)
  17242. (when (org-at-item-p)
  17243. ;; Pay attention to cases when region ends before list.
  17244. (let* ((struct (org-list-struct))
  17245. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17246. (save-restriction
  17247. (narrow-to-region (point) list-end)
  17248. (insert
  17249. (org-list-to-subtree
  17250. (org-list-parse-list t)
  17251. '(:istart (concat stars add-stars (funcall get-stars depth))
  17252. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17253. (forward-line))))
  17254. ;; Case 3. Started at normal text: make every line an heading,
  17255. ;; skipping headlines and items.
  17256. (t (let* ((stars (make-string
  17257. (if nstars
  17258. (prefix-numeric-value current-prefix-arg)
  17259. (or (org-current-level) 0))
  17260. ?*))
  17261. (add-stars
  17262. (cond (nstars "") ; stars from prefix only
  17263. ((equal stars "") "*") ; before first heading
  17264. (org-odd-levels-only "**") ; inside heading, odd
  17265. (t "*"))) ; inside heading, oddeven
  17266. (rpl (concat stars add-stars " ")))
  17267. (while (< (point) end)
  17268. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17269. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17270. (replace-match (concat rpl (match-string 2))))
  17271. (forward-line)))))))))
  17272. (defun org-meta-return (&optional arg)
  17273. "Insert a new heading or wrap a region in a table.
  17274. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17275. See the individual commands for more information."
  17276. (interactive "P")
  17277. (cond
  17278. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17279. ((org-at-table-p)
  17280. (call-interactively 'org-table-wrap-region))
  17281. (t (call-interactively 'org-insert-heading))))
  17282. ;;; Menu entries
  17283. ;; Define the Org-mode menus
  17284. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17285. '("Tbl"
  17286. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17287. ["Next Field" org-cycle (org-at-table-p)]
  17288. ["Previous Field" org-shifttab (org-at-table-p)]
  17289. ["Next Row" org-return (org-at-table-p)]
  17290. "--"
  17291. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17292. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17293. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17294. "--"
  17295. ("Column"
  17296. ["Move Column Left" org-metaleft (org-at-table-p)]
  17297. ["Move Column Right" org-metaright (org-at-table-p)]
  17298. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17299. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17300. ("Row"
  17301. ["Move Row Up" org-metaup (org-at-table-p)]
  17302. ["Move Row Down" org-metadown (org-at-table-p)]
  17303. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17304. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17305. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17306. "--"
  17307. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17308. ("Rectangle"
  17309. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17310. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17311. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17312. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17313. "--"
  17314. ("Calculate"
  17315. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17316. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17317. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17318. "--"
  17319. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17320. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17321. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17322. "--"
  17323. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17324. "--"
  17325. ["Sum Column/Rectangle" org-table-sum
  17326. (or (org-at-table-p) (org-region-active-p))]
  17327. ["Which Column?" org-table-current-column (org-at-table-p)])
  17328. ["Debug Formulas"
  17329. org-table-toggle-formula-debugger
  17330. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17331. ["Show Col/Row Numbers"
  17332. org-table-toggle-coordinate-overlays
  17333. :style toggle
  17334. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17335. "--"
  17336. ["Create" org-table-create (and (not (org-at-table-p))
  17337. org-enable-table-editor)]
  17338. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17339. ["Import from File" org-table-import (not (org-at-table-p))]
  17340. ["Export to File" org-table-export (org-at-table-p)]
  17341. "--"
  17342. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17343. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17344. '("Org"
  17345. ("Show/Hide"
  17346. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17347. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17348. ["Sparse Tree..." org-sparse-tree t]
  17349. ["Reveal Context" org-reveal t]
  17350. ["Show All" show-all t]
  17351. "--"
  17352. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17353. "--"
  17354. ["New Heading" org-insert-heading t]
  17355. ("Navigate Headings"
  17356. ["Up" outline-up-heading t]
  17357. ["Next" outline-next-visible-heading t]
  17358. ["Previous" outline-previous-visible-heading t]
  17359. ["Next Same Level" outline-forward-same-level t]
  17360. ["Previous Same Level" outline-backward-same-level t]
  17361. "--"
  17362. ["Jump" org-goto t])
  17363. ("Edit Structure"
  17364. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17365. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17366. "--"
  17367. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17368. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17369. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17370. "--"
  17371. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17372. "--"
  17373. ["Copy visible text" org-copy-visible t]
  17374. "--"
  17375. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17376. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17377. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17378. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17379. "--"
  17380. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17381. "--"
  17382. ["Convert to odd levels" org-convert-to-odd-levels t]
  17383. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17384. ("Editing"
  17385. ["Emphasis..." org-emphasize t]
  17386. ["Edit Source Example" org-edit-special t]
  17387. "--"
  17388. ["Footnote new/jump" org-footnote-action t]
  17389. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17390. ("Archive"
  17391. ["Archive (default method)" org-archive-subtree-default t]
  17392. "--"
  17393. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17394. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17395. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17396. )
  17397. "--"
  17398. ("Hyperlinks"
  17399. ["Store Link (Global)" org-store-link t]
  17400. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17401. ["Insert Link" org-insert-link t]
  17402. ["Follow Link" org-open-at-point t]
  17403. "--"
  17404. ["Next link" org-next-link t]
  17405. ["Previous link" org-previous-link t]
  17406. "--"
  17407. ["Descriptive Links"
  17408. org-toggle-link-display
  17409. :style radio
  17410. :selected org-descriptive-links
  17411. ]
  17412. ["Literal Links"
  17413. org-toggle-link-display
  17414. :style radio
  17415. :selected (not org-descriptive-links)])
  17416. "--"
  17417. ("TODO Lists"
  17418. ["TODO/DONE/-" org-todo t]
  17419. ("Select keyword"
  17420. ["Next keyword" org-shiftright (org-at-heading-p)]
  17421. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17422. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17423. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17424. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17425. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17426. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17427. "--"
  17428. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17429. :selected org-enforce-todo-dependencies :style toggle :active t]
  17430. "Settings for tree at point"
  17431. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17432. :selected (org-entry-get nil "ORDERED")
  17433. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17434. ["Do Children parallel" org-toggle-ordered-property :style radio
  17435. :selected (not (org-entry-get nil "ORDERED"))
  17436. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17437. "--"
  17438. ["Set Priority" org-priority t]
  17439. ["Priority Up" org-shiftup t]
  17440. ["Priority Down" org-shiftdown t]
  17441. "--"
  17442. ["Get news from all feeds" org-feed-update-all t]
  17443. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17444. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17445. ("TAGS and Properties"
  17446. ["Set Tags" org-set-tags-command t]
  17447. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17448. "--"
  17449. ["Set property" org-set-property t]
  17450. ["Column view of properties" org-columns t]
  17451. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17452. ("Dates and Scheduling"
  17453. ["Timestamp" org-time-stamp t]
  17454. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17455. ("Change Date"
  17456. ["1 Day Later" org-shiftright t]
  17457. ["1 Day Earlier" org-shiftleft t]
  17458. ["1 ... Later" org-shiftup t]
  17459. ["1 ... Earlier" org-shiftdown t])
  17460. ["Compute Time Range" org-evaluate-time-range t]
  17461. ["Schedule Item" org-schedule t]
  17462. ["Deadline" org-deadline t]
  17463. "--"
  17464. ["Custom time format" org-toggle-time-stamp-overlays
  17465. :style radio :selected org-display-custom-times]
  17466. "--"
  17467. ["Goto Calendar" org-goto-calendar t]
  17468. ["Date from Calendar" org-date-from-calendar t]
  17469. "--"
  17470. ["Start/Restart Timer" org-timer-start t]
  17471. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17472. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17473. ["Insert Timer String" org-timer t]
  17474. ["Insert Timer Item" org-timer-item t])
  17475. ("Logging work"
  17476. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17477. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17478. ["Clock out" org-clock-out t]
  17479. ["Clock cancel" org-clock-cancel t]
  17480. "--"
  17481. ["Mark as default task" org-clock-mark-default-task t]
  17482. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17483. ["Goto running clock" org-clock-goto t]
  17484. "--"
  17485. ["Display times" org-clock-display t]
  17486. ["Create clock table" org-clock-report t]
  17487. "--"
  17488. ["Record DONE time"
  17489. (progn (setq org-log-done (not org-log-done))
  17490. (message "Switching to %s will %s record a timestamp"
  17491. (car org-done-keywords)
  17492. (if org-log-done "automatically" "not")))
  17493. :style toggle :selected org-log-done])
  17494. "--"
  17495. ["Agenda Command..." org-agenda t]
  17496. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17497. ("File List for Agenda")
  17498. ("Special views current file"
  17499. ["TODO Tree" org-show-todo-tree t]
  17500. ["Check Deadlines" org-check-deadlines t]
  17501. ["Timeline" org-timeline t]
  17502. ["Tags/Property tree" org-match-sparse-tree t])
  17503. "--"
  17504. ["Export/Publish..." org-export t]
  17505. ("LaTeX"
  17506. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17507. :selected org-cdlatex-mode]
  17508. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17509. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17510. ["Modify math symbol" org-cdlatex-math-modify
  17511. (org-inside-LaTeX-fragment-p)]
  17512. ["Insert citation" org-reftex-citation t]
  17513. "--"
  17514. ["Template for BEAMER" org-insert-beamer-options-template t])
  17515. "--"
  17516. ("MobileOrg"
  17517. ["Push Files and Views" org-mobile-push t]
  17518. ["Get Captured and Flagged" org-mobile-pull t]
  17519. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17520. "--"
  17521. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17522. "--"
  17523. ("Documentation"
  17524. ["Show Version" org-version t]
  17525. ["Info Documentation" org-info t])
  17526. ("Customize"
  17527. ["Browse Org Group" org-customize t]
  17528. "--"
  17529. ["Expand This Menu" org-create-customize-menu
  17530. (fboundp 'customize-menu-create)])
  17531. ["Send bug report" org-submit-bug-report t]
  17532. "--"
  17533. ("Refresh/Reload"
  17534. ["Refresh setup current buffer" org-mode-restart t]
  17535. ["Reload Org (after update)" org-reload t]
  17536. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17537. ))
  17538. (defun org-info (&optional node)
  17539. "Read documentation for Org-mode in the info system.
  17540. With optional NODE, go directly to that node."
  17541. (interactive)
  17542. (info (format "(org)%s" (or node ""))))
  17543. ;;;###autoload
  17544. (defun org-submit-bug-report ()
  17545. "Submit a bug report on Org-mode via mail.
  17546. Don't hesitate to report any problems or inaccurate documentation.
  17547. If you don't have setup sending mail from (X)Emacs, please copy the
  17548. output buffer into your mail program, as it gives us important
  17549. information about your Org-mode version and configuration."
  17550. (interactive)
  17551. (require 'reporter)
  17552. (org-load-modules-maybe)
  17553. (org-require-autoloaded-modules)
  17554. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17555. (reporter-submit-bug-report
  17556. "emacs-orgmode@gnu.org"
  17557. (org-version)
  17558. (let (list)
  17559. (save-window-excursion
  17560. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17561. (delete-other-windows)
  17562. (erase-buffer)
  17563. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17564. We would like to add your full Org-mode and Outline configuration to the
  17565. bug report. This greatly simplifies the work of the maintainer and
  17566. other experts on the mailing list.
  17567. HOWEVER, some variables you have customized may contain private
  17568. information. The names of customers, colleagues, or friends, might
  17569. appear in the form of file names, tags, todo states, or search strings.
  17570. If you answer yes to the prompt, you might want to check and remove
  17571. such private information before sending the email.")
  17572. (add-text-properties (point-min) (point-max) '(face org-warning))
  17573. (when (yes-or-no-p "Include your Org-mode configuration ")
  17574. (mapatoms
  17575. (lambda (v)
  17576. (and (boundp v)
  17577. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17578. (or (and (symbol-value v)
  17579. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17580. (and
  17581. (get v 'custom-type) (get v 'standard-value)
  17582. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17583. (push v list)))))
  17584. (kill-buffer (get-buffer "*Warn about privacy*"))
  17585. list))
  17586. nil nil
  17587. "Remember to cover the basics, that is, what you expected to happen and
  17588. what in fact did happen. You don't know how to make a good report? See
  17589. http://orgmode.org/manual/Feedback.html#Feedback
  17590. Your bug report will be posted to the Org-mode mailing list.
  17591. ------------------------------------------------------------------------")
  17592. (save-excursion
  17593. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17594. (replace-match "\\1Bug: \\3 [\\2]")))))
  17595. (defun org-install-agenda-files-menu ()
  17596. (let ((bl (buffer-list)))
  17597. (save-excursion
  17598. (while bl
  17599. (set-buffer (pop bl))
  17600. (if (eq major-mode 'org-mode) (setq bl nil)))
  17601. (when (eq major-mode 'org-mode)
  17602. (easy-menu-change
  17603. '("Org") "File List for Agenda"
  17604. (append
  17605. (list
  17606. ["Edit File List" (org-edit-agenda-file-list) t]
  17607. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17608. ["Remove Current File from List" org-remove-file t]
  17609. ["Cycle through agenda files" org-cycle-agenda-files t]
  17610. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17611. "--")
  17612. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17613. ;;;; Documentation
  17614. ;;;###autoload
  17615. (defun org-require-autoloaded-modules ()
  17616. (interactive)
  17617. (mapc 'require
  17618. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17619. org-docbook org-exp org-html org-icalendar
  17620. org-id org-latex
  17621. org-publish org-remember org-table
  17622. org-timer org-xoxo)))
  17623. ;;;###autoload
  17624. (defun org-reload (&optional uncompiled)
  17625. "Reload all org lisp files.
  17626. With prefix arg UNCOMPILED, load the uncompiled versions."
  17627. (interactive "P")
  17628. (require 'find-func)
  17629. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17630. (dir-org (file-name-directory (org-find-library-name "org")))
  17631. (dir-org-contrib (ignore-errors
  17632. (file-name-directory
  17633. (org-find-library-name "org-contribdir"))))
  17634. (babel-files
  17635. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17636. (append (list nil "comint" "eval" "exp" "keys"
  17637. "lob" "ref" "table" "tangle")
  17638. (delq nil
  17639. (mapcar
  17640. (lambda (lang)
  17641. (when (cdr lang) (symbol-name (car lang))))
  17642. org-babel-load-languages)))))
  17643. (files
  17644. (append (directory-files dir-org t file-re)
  17645. babel-files
  17646. (and dir-org-contrib
  17647. (directory-files dir-org-contrib t file-re))))
  17648. (remove-re (concat (if (featurep 'xemacs)
  17649. "org-colview" "org-colview-xemacs")
  17650. "\\'")))
  17651. (setq files (mapcar 'file-name-sans-extension files))
  17652. (setq files (mapcar
  17653. (lambda (x) (if (string-match remove-re x) nil x))
  17654. files))
  17655. (setq files (delq nil files))
  17656. (mapc
  17657. (lambda (f)
  17658. (when (featurep (intern (file-name-nondirectory f)))
  17659. (if (and (not uncompiled)
  17660. (file-exists-p (concat f ".elc")))
  17661. (load (concat f ".elc") nil nil t)
  17662. (load (concat f ".el") nil nil t))))
  17663. files))
  17664. (org-version))
  17665. ;;;###autoload
  17666. (defun org-customize ()
  17667. "Call the customize function with org as argument."
  17668. (interactive)
  17669. (org-load-modules-maybe)
  17670. (org-require-autoloaded-modules)
  17671. (customize-browse 'org))
  17672. (defun org-create-customize-menu ()
  17673. "Create a full customization menu for Org-mode, insert it into the menu."
  17674. (interactive)
  17675. (org-load-modules-maybe)
  17676. (org-require-autoloaded-modules)
  17677. (if (fboundp 'customize-menu-create)
  17678. (progn
  17679. (easy-menu-change
  17680. '("Org") "Customize"
  17681. `(["Browse Org group" org-customize t]
  17682. "--"
  17683. ,(customize-menu-create 'org)
  17684. ["Set" Custom-set t]
  17685. ["Save" Custom-save t]
  17686. ["Reset to Current" Custom-reset-current t]
  17687. ["Reset to Saved" Custom-reset-saved t]
  17688. ["Reset to Standard Settings" Custom-reset-standard t]))
  17689. (message "\"Org\"-menu now contains full customization menu"))
  17690. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17691. ;;;; Miscellaneous stuff
  17692. ;;; Generally useful functions
  17693. (defun org-get-at-bol (property)
  17694. "Get text property PROPERTY at beginning of line."
  17695. (get-text-property (point-at-bol) property))
  17696. (defun org-find-text-property-in-string (prop s)
  17697. "Return the first non-nil value of property PROP in string S."
  17698. (or (get-text-property 0 prop s)
  17699. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17700. prop s)))
  17701. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17702. "Display the given MESSAGE as a warning."
  17703. (if (fboundp 'display-warning)
  17704. (display-warning 'org message
  17705. (if (featurep 'xemacs) 'warning :warning))
  17706. (let ((buf (get-buffer-create "*Org warnings*")))
  17707. (with-current-buffer buf
  17708. (goto-char (point-max))
  17709. (insert "Warning (Org): " message)
  17710. (unless (bolp)
  17711. (newline)))
  17712. (display-buffer buf)
  17713. (sit-for 0))))
  17714. (defun org-eval (form)
  17715. "Eval FORM and return result."
  17716. (condition-case error
  17717. (eval form)
  17718. (error (format "%%![Error: %s]" error))))
  17719. (defun org-in-clocktable-p ()
  17720. "Check if the cursor is in a clocktable."
  17721. (let ((pos (point)) start)
  17722. (save-excursion
  17723. (end-of-line 1)
  17724. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  17725. (setq start (match-beginning 0))
  17726. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  17727. (>= (match-end 0) pos)
  17728. start))))
  17729. (defun org-in-commented-line ()
  17730. "Is point in a line starting with `#'?"
  17731. (equal (char-after (point-at-bol)) ?#))
  17732. (defun org-in-indented-comment-line ()
  17733. "Is point in a line starting with `#' after some white space?"
  17734. (save-excursion
  17735. (save-match-data
  17736. (goto-char (point-at-bol))
  17737. (looking-at "[ \t]*#"))))
  17738. (defun org-in-verbatim-emphasis ()
  17739. (save-match-data
  17740. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17741. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17742. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17743. (if (and marker (marker-buffer marker)
  17744. (buffer-live-p (marker-buffer marker)))
  17745. (progn
  17746. (org-pop-to-buffer-same-window (marker-buffer marker))
  17747. (if (or (> marker (point-max)) (< marker (point-min)))
  17748. (widen))
  17749. (goto-char marker)
  17750. (org-show-context 'org-goto))
  17751. (if bookmark
  17752. (bookmark-jump bookmark)
  17753. (error "Cannot find location"))))
  17754. (defun org-quote-csv-field (s)
  17755. "Quote field for inclusion in CSV material."
  17756. (if (string-match "[\",]" s)
  17757. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17758. s))
  17759. (defun org-force-self-insert (N)
  17760. "Needed to enforce self-insert under remapping."
  17761. (interactive "p")
  17762. (self-insert-command N))
  17763. (defun org-string-width (s)
  17764. "Compute width of string, ignoring invisible characters.
  17765. This ignores character with invisibility property `org-link', and also
  17766. characters with property `org-cwidth', because these will become invisible
  17767. upon the next fontification round."
  17768. (let (b l)
  17769. (when (or (eq t buffer-invisibility-spec)
  17770. (assq 'org-link buffer-invisibility-spec))
  17771. (while (setq b (text-property-any 0 (length s)
  17772. 'invisible 'org-link s))
  17773. (setq s (concat (substring s 0 b)
  17774. (substring s (or (next-single-property-change
  17775. b 'invisible s) (length s)))))))
  17776. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17777. (setq s (concat (substring s 0 b)
  17778. (substring s (or (next-single-property-change
  17779. b 'org-cwidth s) (length s))))))
  17780. (setq l (string-width s) b -1)
  17781. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17782. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17783. l))
  17784. (defun org-shorten-string (s maxlength)
  17785. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17786. If the string is shorter or has length MAXLENGTH, just return the
  17787. original string. If it is longer, the functions finds a space in the
  17788. string, breaks this string off at that locations and adds three dots
  17789. as ellipsis. Including the ellipsis, the string will not be longer
  17790. than MAXLENGTH. If finding a good breaking point in the string does
  17791. not work, the string is just chopped off in the middle of a word
  17792. if necessary."
  17793. (if (<= (length s) maxlength)
  17794. s
  17795. (let* ((n (max (- maxlength 4) 1))
  17796. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17797. (if (string-match re s)
  17798. (concat (match-string 1 s) "...")
  17799. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17800. (defun org-get-indentation (&optional line)
  17801. "Get the indentation of the current line, interpreting tabs.
  17802. When LINE is given, assume it represents a line and compute its indentation."
  17803. (if line
  17804. (if (string-match "^ *" (org-remove-tabs line))
  17805. (match-end 0))
  17806. (save-excursion
  17807. (beginning-of-line 1)
  17808. (skip-chars-forward " \t")
  17809. (current-column))))
  17810. (defun org-get-string-indentation (s)
  17811. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17812. (let ((n -1) (i 0) (w tab-width) c)
  17813. (catch 'exit
  17814. (while (< (setq n (1+ n)) (length s))
  17815. (setq c (aref s n))
  17816. (cond ((= c ?\ ) (setq i (1+ i)))
  17817. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17818. (t (throw 'exit t)))))
  17819. i))
  17820. (defun org-remove-tabs (s &optional width)
  17821. "Replace tabulators in S with spaces.
  17822. Assumes that s is a single line, starting in column 0."
  17823. (setq width (or width tab-width))
  17824. (while (string-match "\t" s)
  17825. (setq s (replace-match
  17826. (make-string
  17827. (- (* width (/ (+ (match-beginning 0) width) width))
  17828. (match-beginning 0)) ?\ )
  17829. t t s)))
  17830. s)
  17831. (defun org-fix-indentation (line ind)
  17832. "Fix indentation in LINE.
  17833. IND is a cons cell with target and minimum indentation.
  17834. If the current indentation in LINE is smaller than the minimum,
  17835. leave it alone. If it is larger than ind, set it to the target."
  17836. (let* ((l (org-remove-tabs line))
  17837. (i (org-get-indentation l))
  17838. (i1 (car ind)) (i2 (cdr ind)))
  17839. (if (>= i i2) (setq l (substring line i2)))
  17840. (if (> i1 0)
  17841. (concat (make-string i1 ?\ ) l)
  17842. l)))
  17843. (defun org-remove-indentation (code &optional n)
  17844. "Remove the maximum common indentation from the lines in CODE.
  17845. N may optionally be the number of spaces to remove."
  17846. (with-temp-buffer
  17847. (insert code)
  17848. (org-do-remove-indentation n)
  17849. (buffer-string)))
  17850. (defun org-do-remove-indentation (&optional n)
  17851. "Remove the maximum common indentation from the buffer."
  17852. (untabify (point-min) (point-max))
  17853. (let ((min 10000) re)
  17854. (if n
  17855. (setq min n)
  17856. (goto-char (point-min))
  17857. (while (re-search-forward "^ *[^ \n]" nil t)
  17858. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17859. (unless (or (= min 0) (= min 10000))
  17860. (setq re (format "^ \\{%d\\}" min))
  17861. (goto-char (point-min))
  17862. (while (re-search-forward re nil t)
  17863. (replace-match "")
  17864. (end-of-line 1))
  17865. min)))
  17866. (defun org-fill-template (template alist)
  17867. "Find each %key of ALIST in TEMPLATE and replace it."
  17868. (let ((case-fold-search nil)
  17869. entry key value)
  17870. (setq alist (sort (copy-sequence alist)
  17871. (lambda (a b) (< (length (car a)) (length (car b))))))
  17872. (while (setq entry (pop alist))
  17873. (setq template
  17874. (replace-regexp-in-string
  17875. (concat "%" (regexp-quote (car entry)))
  17876. (cdr entry) template t t)))
  17877. template))
  17878. (defun org-base-buffer (buffer)
  17879. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17880. (if (not buffer)
  17881. buffer
  17882. (or (buffer-base-buffer buffer)
  17883. buffer)))
  17884. (defun org-trim (s)
  17885. "Remove whitespace at beginning and end of string."
  17886. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17887. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17888. s)
  17889. (defun org-wrap (string &optional width lines)
  17890. "Wrap string to either a number of lines, or a width in characters.
  17891. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17892. that costs. If there is a word longer than WIDTH, the text is actually
  17893. wrapped to the length of that word.
  17894. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17895. many lines, whatever width that takes.
  17896. The return value is a list of lines, without newlines at the end."
  17897. (let* ((words (org-split-string string "[ \t\n]+"))
  17898. (maxword (apply 'max (mapcar 'org-string-width words)))
  17899. w ll)
  17900. (cond (width
  17901. (org-do-wrap words (max maxword width)))
  17902. (lines
  17903. (setq w maxword)
  17904. (setq ll (org-do-wrap words maxword))
  17905. (if (<= (length ll) lines)
  17906. ll
  17907. (setq ll words)
  17908. (while (> (length ll) lines)
  17909. (setq w (1+ w))
  17910. (setq ll (org-do-wrap words w)))
  17911. ll))
  17912. (t (error "Cannot wrap this")))))
  17913. (defun org-do-wrap (words width)
  17914. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17915. (let (lines line)
  17916. (while words
  17917. (setq line (pop words))
  17918. (while (and words (< (+ (length line) (length (car words))) width))
  17919. (setq line (concat line " " (pop words))))
  17920. (setq lines (push line lines)))
  17921. (nreverse lines)))
  17922. (defun org-split-string (string &optional separators)
  17923. "Splits STRING into substrings at SEPARATORS.
  17924. No empty strings are returned if there are matches at the beginning
  17925. and end of string."
  17926. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17927. (start 0)
  17928. notfirst
  17929. (list nil))
  17930. (while (and (string-match rexp string
  17931. (if (and notfirst
  17932. (= start (match-beginning 0))
  17933. (< start (length string)))
  17934. (1+ start) start))
  17935. (< (match-beginning 0) (length string)))
  17936. (setq notfirst t)
  17937. (or (eq (match-beginning 0) 0)
  17938. (and (eq (match-beginning 0) (match-end 0))
  17939. (eq (match-beginning 0) start))
  17940. (setq list
  17941. (cons (substring string start (match-beginning 0))
  17942. list)))
  17943. (setq start (match-end 0)))
  17944. (or (eq start (length string))
  17945. (setq list
  17946. (cons (substring string start)
  17947. list)))
  17948. (nreverse list)))
  17949. (defun org-quote-vert (s)
  17950. "Replace \"|\" with \"\\vert\"."
  17951. (while (string-match "|" s)
  17952. (setq s (replace-match "\\vert" t t s)))
  17953. s)
  17954. (defun org-uuidgen-p (s)
  17955. "Is S an ID created by UUIDGEN?"
  17956. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17957. (defun org-context ()
  17958. "Return a list of contexts of the current cursor position.
  17959. If several contexts apply, all are returned.
  17960. Each context entry is a list with a symbol naming the context, and
  17961. two positions indicating start and end of the context. Possible
  17962. contexts are:
  17963. :headline anywhere in a headline
  17964. :headline-stars on the leading stars in a headline
  17965. :todo-keyword on a TODO keyword (including DONE) in a headline
  17966. :tags on the TAGS in a headline
  17967. :priority on the priority cookie in a headline
  17968. :item on the first line of a plain list item
  17969. :item-bullet on the bullet/number of a plain list item
  17970. :checkbox on the checkbox in a plain list item
  17971. :table in an org-mode table
  17972. :table-special on a special filed in a table
  17973. :table-table in a table.el table
  17974. :link on a hyperlink
  17975. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17976. :target on a <<target>>
  17977. :radio-target on a <<<radio-target>>>
  17978. :latex-fragment on a LaTeX fragment
  17979. :latex-preview on a LaTeX fragment with overlaid preview image
  17980. This function expects the position to be visible because it uses font-lock
  17981. faces as a help to recognize the following contexts: :table-special, :link,
  17982. and :keyword."
  17983. (let* ((f (get-text-property (point) 'face))
  17984. (faces (if (listp f) f (list f)))
  17985. (p (point)) clist o)
  17986. ;; First the large context
  17987. (cond
  17988. ((org-at-heading-p t)
  17989. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17990. (when (progn
  17991. (beginning-of-line 1)
  17992. (looking-at org-todo-line-tags-regexp))
  17993. (push (org-point-in-group p 1 :headline-stars) clist)
  17994. (push (org-point-in-group p 2 :todo-keyword) clist)
  17995. (push (org-point-in-group p 4 :tags) clist))
  17996. (goto-char p)
  17997. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17998. (if (looking-at "\\[#[A-Z0-9]\\]")
  17999. (push (org-point-in-group p 0 :priority) clist)))
  18000. ((org-at-item-p)
  18001. (push (org-point-in-group p 2 :item-bullet) clist)
  18002. (push (list :item (point-at-bol)
  18003. (save-excursion (org-end-of-item) (point)))
  18004. clist)
  18005. (and (org-at-item-checkbox-p)
  18006. (push (org-point-in-group p 0 :checkbox) clist)))
  18007. ((org-at-table-p)
  18008. (push (list :table (org-table-begin) (org-table-end)) clist)
  18009. (if (memq 'org-formula faces)
  18010. (push (list :table-special
  18011. (previous-single-property-change p 'face)
  18012. (next-single-property-change p 'face)) clist)))
  18013. ((org-at-table-p 'any)
  18014. (push (list :table-table) clist)))
  18015. (goto-char p)
  18016. ;; Now the small context
  18017. (cond
  18018. ((org-at-timestamp-p)
  18019. (push (org-point-in-group p 0 :timestamp) clist))
  18020. ((memq 'org-link faces)
  18021. (push (list :link
  18022. (previous-single-property-change p 'face)
  18023. (next-single-property-change p 'face)) clist))
  18024. ((memq 'org-special-keyword faces)
  18025. (push (list :keyword
  18026. (previous-single-property-change p 'face)
  18027. (next-single-property-change p 'face)) clist))
  18028. ((org-at-target-p)
  18029. (push (org-point-in-group p 0 :target) clist)
  18030. (goto-char (1- (match-beginning 0)))
  18031. (if (looking-at org-radio-target-regexp)
  18032. (push (org-point-in-group p 0 :radio-target) clist))
  18033. (goto-char p))
  18034. ((setq o (car (delq nil
  18035. (mapcar
  18036. (lambda (x)
  18037. (if (memq x org-latex-fragment-image-overlays) x))
  18038. (overlays-at (point))))))
  18039. (push (list :latex-fragment
  18040. (overlay-start o) (overlay-end o)) clist)
  18041. (push (list :latex-preview
  18042. (overlay-start o) (overlay-end o)) clist))
  18043. ((org-inside-LaTeX-fragment-p)
  18044. ;; FIXME: positions wrong.
  18045. (push (list :latex-fragment (point) (point)) clist)))
  18046. (setq clist (nreverse (delq nil clist)))
  18047. clist))
  18048. ;; FIXME: Compare with at-regexp-p Do we need both?
  18049. (defun org-in-regexp (re &optional nlines visually)
  18050. "Check if point is inside a match of regexp.
  18051. Normally only the current line is checked, but you can include NLINES extra
  18052. lines both before and after point into the search.
  18053. If VISUALLY is set, require that the cursor is not after the match but
  18054. really on, so that the block visually is on the match."
  18055. (catch 'exit
  18056. (let ((pos (point))
  18057. (eol (point-at-eol (+ 1 (or nlines 0))))
  18058. (inc (if visually 1 0)))
  18059. (save-excursion
  18060. (beginning-of-line (- 1 (or nlines 0)))
  18061. (while (re-search-forward re eol t)
  18062. (if (and (<= (match-beginning 0) pos)
  18063. (>= (+ inc (match-end 0)) pos))
  18064. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18065. (defun org-at-regexp-p (regexp)
  18066. "Is point inside a match of REGEXP in the current line?"
  18067. (catch 'exit
  18068. (save-excursion
  18069. (let ((pos (point)) (end (point-at-eol)))
  18070. (beginning-of-line 1)
  18071. (while (re-search-forward regexp end t)
  18072. (if (and (<= (match-beginning 0) pos)
  18073. (>= (match-end 0) pos))
  18074. (throw 'exit t)))
  18075. nil))))
  18076. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18077. "Non-nil when point is between matches of START-RE and END-RE.
  18078. Also return a non-nil value when point is on one of the matches.
  18079. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18080. buffer positions. Default values are the positions of headlines
  18081. surrounding the point.
  18082. The functions returns a cons cell whose car (resp. cdr) is the
  18083. position before START-RE (resp. after END-RE)."
  18084. (save-match-data
  18085. (let ((pos (point))
  18086. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18087. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18088. beg end)
  18089. (save-excursion
  18090. ;; Point is on a block when on START-RE or if START-RE can be
  18091. ;; found before it...
  18092. (and (or (org-at-regexp-p start-re)
  18093. (re-search-backward start-re limit-up t))
  18094. (setq beg (match-beginning 0))
  18095. ;; ... and END-RE after it...
  18096. (goto-char (match-end 0))
  18097. (re-search-forward end-re limit-down t)
  18098. (> (setq end (match-end 0)) pos)
  18099. ;; ... without another START-RE in-between.
  18100. (goto-char (match-beginning 0))
  18101. (not (re-search-backward start-re (1+ beg) t))
  18102. ;; Return value.
  18103. (cons beg end))))))
  18104. (defun org-in-block-p (names)
  18105. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18106. NAMES is a list of strings containing names of blocks.
  18107. Return first block name matched, or nil. Beware that in case of
  18108. nested blocks, the returned name may not belong to the closest
  18109. block from point."
  18110. (save-match-data
  18111. (catch 'exit
  18112. (let ((case-fold-search t)
  18113. (lim-up (save-excursion (outline-previous-heading)))
  18114. (lim-down (save-excursion (outline-next-heading))))
  18115. (mapc (lambda (name)
  18116. (let ((n (regexp-quote name)))
  18117. (when (org-between-regexps-p
  18118. (concat "^[ \t]*#\\+begin_" n)
  18119. (concat "^[ \t]*#\\+end_" n)
  18120. lim-up lim-down)
  18121. (throw 'exit n))))
  18122. names))
  18123. nil)))
  18124. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18125. "Call `multi-occur' with buffers for all agenda files."
  18126. (interactive "sOrg-files matching: \np")
  18127. (let* ((files (org-agenda-files))
  18128. (tnames (mapcar 'file-truename files))
  18129. (extra org-agenda-text-search-extra-files)
  18130. f)
  18131. (when (eq (car extra) 'agenda-archives)
  18132. (setq extra (cdr extra))
  18133. (setq files (org-add-archive-files files)))
  18134. (while (setq f (pop extra))
  18135. (unless (member (file-truename f) tnames)
  18136. (add-to-list 'files f 'append)
  18137. (add-to-list 'tnames (file-truename f) 'append)))
  18138. (multi-occur
  18139. (mapcar (lambda (x)
  18140. (with-current-buffer
  18141. (or (get-file-buffer x) (find-file-noselect x))
  18142. (widen)
  18143. (current-buffer)))
  18144. files)
  18145. regexp)))
  18146. (if (boundp 'occur-mode-find-occurrence-hook)
  18147. ;; Emacs 23
  18148. (add-hook 'occur-mode-find-occurrence-hook
  18149. (lambda ()
  18150. (when (eq major-mode 'org-mode)
  18151. (org-reveal))))
  18152. ;; Emacs 22
  18153. (defadvice occur-mode-goto-occurrence
  18154. (after org-occur-reveal activate)
  18155. (and (eq major-mode 'org-mode) (org-reveal)))
  18156. (defadvice occur-mode-goto-occurrence-other-window
  18157. (after org-occur-reveal activate)
  18158. (and (eq major-mode 'org-mode) (org-reveal)))
  18159. (defadvice occur-mode-display-occurrence
  18160. (after org-occur-reveal activate)
  18161. (when (eq major-mode 'org-mode)
  18162. (let ((pos (occur-mode-find-occurrence)))
  18163. (with-current-buffer (marker-buffer pos)
  18164. (save-excursion
  18165. (goto-char pos)
  18166. (org-reveal)))))))
  18167. (defun org-occur-link-in-agenda-files ()
  18168. "Create a link and search for it in the agendas.
  18169. The link is not stored in `org-stored-links', it is just created
  18170. for the search purpose."
  18171. (interactive)
  18172. (let ((link (condition-case nil
  18173. (org-store-link nil)
  18174. (error "Unable to create a link to here"))))
  18175. (org-occur-in-agenda-files (regexp-quote link))))
  18176. (defun org-uniquify (list)
  18177. "Remove duplicate elements from LIST."
  18178. (let (res)
  18179. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18180. res))
  18181. (defun org-delete-all (elts list)
  18182. "Remove all elements in ELTS from LIST."
  18183. (while elts
  18184. (setq list (delete (pop elts) list)))
  18185. list)
  18186. (defun org-count (cl-item cl-seq)
  18187. "Count the number of occurrences of ITEM in SEQ.
  18188. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18189. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18190. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18191. (while (< cl-start cl-end)
  18192. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18193. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18194. (setq cl-start (1+ cl-start)))
  18195. cl-count))
  18196. (defun org-remove-if (predicate seq)
  18197. "Remove everything from SEQ that fulfills PREDICATE."
  18198. (let (res e)
  18199. (while seq
  18200. (setq e (pop seq))
  18201. (if (not (funcall predicate e)) (push e res)))
  18202. (nreverse res)))
  18203. (defun org-remove-if-not (predicate seq)
  18204. "Remove everything from SEQ that does not fulfill PREDICATE."
  18205. (let (res e)
  18206. (while seq
  18207. (setq e (pop seq))
  18208. (if (funcall predicate e) (push e res)))
  18209. (nreverse res)))
  18210. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18211. "Reduce two-argument FUNCTION across SEQ.
  18212. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18213. \":initial-value\" removed."
  18214. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18215. (cadr (memq :initial-value cl-keys)))
  18216. (cl-seq (pop cl-seq))
  18217. (t (funcall cl-func)))))
  18218. (while cl-seq
  18219. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18220. cl-accum))
  18221. (defun org-back-over-empty-lines ()
  18222. "Move backwards over whitespace, to the beginning of the first empty line.
  18223. Returns the number of empty lines passed."
  18224. (let ((pos (point)))
  18225. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18226. (skip-chars-backward " \t\n\r")
  18227. (unless (eobp)
  18228. (forward-line -1)))
  18229. (beginning-of-line 2)
  18230. (goto-char (min (point) pos))
  18231. (count-lines (point) pos)))
  18232. (defun org-skip-whitespace ()
  18233. (skip-chars-forward " \t\n\r"))
  18234. (defun org-point-in-group (point group &optional context)
  18235. "Check if POINT is in match-group GROUP.
  18236. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18237. match. If the match group does not exist or point is not inside it,
  18238. return nil."
  18239. (and (match-beginning group)
  18240. (>= point (match-beginning group))
  18241. (<= point (match-end group))
  18242. (if context
  18243. (list context (match-beginning group) (match-end group))
  18244. t)))
  18245. (defun org-switch-to-buffer-other-window (&rest args)
  18246. "Switch to buffer in a second window on the current frame.
  18247. In particular, do not allow pop-up frames.
  18248. Returns the newly created buffer."
  18249. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18250. special-display-function)
  18251. (apply 'switch-to-buffer-other-window args)))
  18252. (defun org-combine-plists (&rest plists)
  18253. "Create a single property list from all plists in PLISTS.
  18254. The process starts by copying the first list, and then setting properties
  18255. from the other lists. Settings in the last list are the most significant
  18256. ones and overrule settings in the other lists."
  18257. (let ((rtn (copy-sequence (pop plists)))
  18258. p v ls)
  18259. (while plists
  18260. (setq ls (pop plists))
  18261. (while ls
  18262. (setq p (pop ls) v (pop ls))
  18263. (setq rtn (plist-put rtn p v))))
  18264. rtn))
  18265. (defun org-move-line-down (arg)
  18266. "Move the current line down. With prefix argument, move it past ARG lines."
  18267. (interactive "p")
  18268. (let ((col (current-column))
  18269. beg end pos)
  18270. (beginning-of-line 1) (setq beg (point))
  18271. (beginning-of-line 2) (setq end (point))
  18272. (beginning-of-line (+ 1 arg))
  18273. (setq pos (move-marker (make-marker) (point)))
  18274. (insert (delete-and-extract-region beg end))
  18275. (goto-char pos)
  18276. (org-move-to-column col)))
  18277. (defun org-move-line-up (arg)
  18278. "Move the current line up. With prefix argument, move it past ARG lines."
  18279. (interactive "p")
  18280. (let ((col (current-column))
  18281. beg end pos)
  18282. (beginning-of-line 1) (setq beg (point))
  18283. (beginning-of-line 2) (setq end (point))
  18284. (beginning-of-line (- arg))
  18285. (setq pos (move-marker (make-marker) (point)))
  18286. (insert (delete-and-extract-region beg end))
  18287. (goto-char pos)
  18288. (org-move-to-column col)))
  18289. (defun org-replace-escapes (string table)
  18290. "Replace %-escapes in STRING with values in TABLE.
  18291. TABLE is an association list with keys like \"%a\" and string values.
  18292. The sequences in STRING may contain normal field width and padding information,
  18293. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18294. so values can contain further %-escapes if they are define later in TABLE."
  18295. (let ((tbl (copy-alist table))
  18296. (case-fold-search nil)
  18297. (pchg 0)
  18298. e re rpl)
  18299. (while (setq e (pop tbl))
  18300. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18301. (when (and (cdr e) (string-match re (cdr e)))
  18302. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18303. (safe "SREF"))
  18304. (add-text-properties 0 3 (list 'sref sref) safe)
  18305. (setcdr e (replace-match safe t t (cdr e)))))
  18306. (while (string-match re string)
  18307. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18308. (cdr e)))
  18309. (setq string (replace-match rpl t t string))))
  18310. (while (setq pchg (next-property-change pchg string))
  18311. (let ((sref (get-text-property pchg 'sref string)))
  18312. (when (and sref (string-match "SREF" string pchg))
  18313. (setq string (replace-match sref t t string)))))
  18314. string))
  18315. (defun org-sublist (list start end)
  18316. "Return a section of LIST, from START to END.
  18317. Counting starts at 1."
  18318. (let (rtn (c start))
  18319. (setq list (nthcdr (1- start) list))
  18320. (while (and list (<= c end))
  18321. (push (pop list) rtn)
  18322. (setq c (1+ c)))
  18323. (nreverse rtn)))
  18324. (defun org-find-base-buffer-visiting (file)
  18325. "Like `find-buffer-visiting' but always return the base buffer and
  18326. not an indirect buffer."
  18327. (let ((buf (or (get-file-buffer file)
  18328. (find-buffer-visiting file))))
  18329. (if buf
  18330. (or (buffer-base-buffer buf) buf)
  18331. nil)))
  18332. (defun org-image-file-name-regexp (&optional extensions)
  18333. "Return regexp matching the file names of images.
  18334. If EXTENSIONS is given, only match these."
  18335. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18336. (image-file-name-regexp)
  18337. (let ((image-file-name-extensions
  18338. (or extensions
  18339. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18340. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18341. (concat "\\."
  18342. (regexp-opt (nconc (mapcar 'upcase
  18343. image-file-name-extensions)
  18344. image-file-name-extensions)
  18345. t)
  18346. "\\'"))))
  18347. (defun org-file-image-p (file &optional extensions)
  18348. "Return non-nil if FILE is an image."
  18349. (save-match-data
  18350. (string-match (org-image-file-name-regexp extensions) file)))
  18351. (defun org-get-cursor-date ()
  18352. "Return the date at cursor in as a time.
  18353. This works in the calendar and in the agenda, anywhere else it just
  18354. returns the current time."
  18355. (let (date day defd)
  18356. (cond
  18357. ((eq major-mode 'calendar-mode)
  18358. (setq date (calendar-cursor-to-date)
  18359. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18360. ((eq major-mode 'org-agenda-mode)
  18361. (setq day (get-text-property (point) 'day))
  18362. (if day
  18363. (setq date (calendar-gregorian-from-absolute day)
  18364. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18365. (nth 2 date))))))
  18366. (or defd (current-time))))
  18367. (defvar org-agenda-action-marker (make-marker)
  18368. "Marker pointing to the entry for the next agenda action.")
  18369. (defun org-mark-entry-for-agenda-action ()
  18370. "Mark the current entry as target of an agenda action.
  18371. Agenda actions are actions executed from the agenda with the key `k',
  18372. which make use of the date at the cursor."
  18373. (interactive)
  18374. (move-marker org-agenda-action-marker
  18375. (save-excursion (org-back-to-heading t) (point))
  18376. (current-buffer))
  18377. (message
  18378. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18379. (defun org-mark-subtree ()
  18380. "Mark the current subtree.
  18381. This puts point at the start of the current subtree, and mark at the end.
  18382. If point is in an inline task, mark that task instead."
  18383. (interactive)
  18384. (let ((inline-task-p
  18385. (and (featurep 'org-inlinetask)
  18386. (org-inlinetask-in-task-p)))
  18387. (beg))
  18388. ;; Get beginning of subtree
  18389. (cond
  18390. (inline-task-p (org-inlinetask-goto-beginning))
  18391. ((org-at-heading-p) (beginning-of-line))
  18392. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18393. (setq beg (point))
  18394. ;; Get end of it
  18395. (if inline-task-p
  18396. (org-inlinetask-goto-end)
  18397. (org-end-of-subtree))
  18398. ;; Mark zone
  18399. (push-mark (point) nil t)
  18400. (goto-char beg)))
  18401. ;;; Paragraph filling stuff.
  18402. ;; We want this to be just right, so use the full arsenal.
  18403. (defun org-indent-line-function ()
  18404. "Indent line depending on context."
  18405. (interactive)
  18406. (let* ((pos (point))
  18407. (itemp (org-at-item-p))
  18408. (case-fold-search t)
  18409. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18410. (inline-task-p (and (featurep 'org-inlinetask)
  18411. (org-inlinetask-in-task-p)))
  18412. (inline-re (and inline-task-p
  18413. (org-inlinetask-outline-regexp)))
  18414. column)
  18415. (beginning-of-line 1)
  18416. (cond
  18417. ;; Comments
  18418. ((looking-at "# ") (setq column 0))
  18419. ;; Headings
  18420. ((looking-at org-outline-regexp) (setq column 0))
  18421. ;; Included files
  18422. ((looking-at "#\\+include:") (setq column 0))
  18423. ;; Footnote definition
  18424. ((looking-at org-footnote-definition-re) (setq column 0))
  18425. ;; Literal examples
  18426. ((looking-at "[ \t]*:\\( \\|$\\)")
  18427. (setq column (org-get-indentation))) ; do nothing
  18428. ;; Lists
  18429. ((ignore-errors (goto-char (org-in-item-p)))
  18430. (setq column (if itemp
  18431. (org-get-indentation)
  18432. (org-list-item-body-column (point))))
  18433. (goto-char pos))
  18434. ;; Drawers
  18435. ((and (looking-at "[ \t]*:END:")
  18436. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18437. (save-excursion
  18438. (goto-char (1- (match-beginning 1)))
  18439. (setq column (current-column))))
  18440. ;; Special blocks
  18441. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18442. (save-excursion
  18443. (re-search-backward
  18444. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18445. (setq column (org-get-indentation (match-string 0))))
  18446. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18447. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18448. (save-excursion
  18449. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18450. (setq column
  18451. (cond ((equal (downcase (match-string 1)) "src")
  18452. ;; src blocks: let `org-edit-src-exit' handle them
  18453. (org-get-indentation))
  18454. ((equal (downcase (match-string 1)) "example")
  18455. (max (org-get-indentation)
  18456. (org-get-indentation (match-string 0))))
  18457. (t
  18458. (org-get-indentation (match-string 0))))))
  18459. ;; This line has nothing special, look at the previous relevant
  18460. ;; line to compute indentation
  18461. (t
  18462. (beginning-of-line 0)
  18463. (while (and (not (bobp))
  18464. (not (looking-at org-drawer-regexp))
  18465. ;; When point started in an inline task, do not move
  18466. ;; above task starting line.
  18467. (not (and inline-task-p (looking-at inline-re)))
  18468. ;; Skip drawers, blocks, empty lines, verbatim,
  18469. ;; comments, tables, footnotes definitions, lists,
  18470. ;; inline tasks.
  18471. (or (and (looking-at "[ \t]*:END:")
  18472. (re-search-backward org-drawer-regexp nil t))
  18473. (and (looking-at "[ \t]*#\\+end_")
  18474. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18475. (looking-at "[ \t]*[\n:#|]")
  18476. (looking-at org-footnote-definition-re)
  18477. (and (ignore-errors (goto-char (org-in-item-p)))
  18478. (goto-char
  18479. (org-list-get-top-point (org-list-struct))))
  18480. (and (not inline-task-p)
  18481. (featurep 'org-inlinetask)
  18482. (org-inlinetask-in-task-p)
  18483. (or (org-inlinetask-goto-beginning) t))))
  18484. (beginning-of-line 0))
  18485. (cond
  18486. ;; There was an heading above.
  18487. ((looking-at "\\*+[ \t]+")
  18488. (if (not org-adapt-indentation)
  18489. (setq column 0)
  18490. (goto-char (match-end 0))
  18491. (setq column (current-column))))
  18492. ;; A drawer had started and is unfinished
  18493. ((looking-at org-drawer-regexp)
  18494. (goto-char (1- (match-beginning 1)))
  18495. (setq column (current-column)))
  18496. ;; Else, nothing noticeable found: get indentation and go on.
  18497. (t (setq column (org-get-indentation))))))
  18498. ;; Now apply indentation and move cursor accordingly
  18499. (goto-char pos)
  18500. (if (<= (current-column) (current-indentation))
  18501. (org-indent-line-to column)
  18502. (save-excursion (org-indent-line-to column)))
  18503. ;; Special polishing for properties, see `org-property-format'
  18504. (setq column (current-column))
  18505. (beginning-of-line 1)
  18506. (if (looking-at
  18507. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18508. (replace-match (concat (match-string 1)
  18509. (format org-property-format
  18510. (match-string 2) (match-string 3)))
  18511. t t))
  18512. (org-move-to-column column)))
  18513. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18514. "Variable to store copy of `adaptive-fill-regexp'.
  18515. Since `adaptive-fill-regexp' is set to never match, we need to
  18516. store a backup of its value before entering `org-mode' so that
  18517. the functionality can be provided as a fall-back.")
  18518. (defun org-set-autofill-regexps ()
  18519. (interactive)
  18520. ;; In the paragraph separator we include headlines, because filling
  18521. ;; text in a line directly attached to a headline would otherwise
  18522. ;; fill the headline as well.
  18523. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18524. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18525. ;; The paragraph starter includes hand-formatted lists.
  18526. (org-set-local
  18527. 'paragraph-start
  18528. (concat
  18529. "\f" "\\|"
  18530. "[ ]*$" "\\|"
  18531. org-outline-regexp "\\|"
  18532. "[ \t]*#" "\\|"
  18533. (org-item-re) "\\|"
  18534. "[ \t]*[:|]" "\\|"
  18535. "\\$\\$" "\\|"
  18536. "\\\\\\(begin\\|end\\|[][]\\)"))
  18537. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18538. ;; But only if the user has not turned off tables or fixed-width regions
  18539. (org-set-local
  18540. 'auto-fill-inhibit-regexp
  18541. (concat org-outline-regexp
  18542. "\\|#\\+"
  18543. "\\|[ \t]*" org-keyword-time-regexp
  18544. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18545. (concat
  18546. "\\|[ \t]*["
  18547. (if org-enable-table-editor "|" "")
  18548. (if org-enable-fixed-width-editor ":" "")
  18549. "]"))))
  18550. ;; We use our own fill-paragraph function, to make sure that tables
  18551. ;; and fixed-width regions are not wrapped. That function will pass
  18552. ;; through to `fill-paragraph' when appropriate.
  18553. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18554. ;; Prevent auto-fill from inserting unwanted new items.
  18555. (if (boundp 'fill-nobreak-predicate)
  18556. (org-set-local 'fill-nobreak-predicate
  18557. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  18558. fill-nobreak-predicate
  18559. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate))))
  18560. ;; Adaptive filling: To get full control, first make sure that
  18561. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18562. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18563. (org-set-local 'org-adaptive-fill-regexp-backup
  18564. adaptive-fill-regexp))
  18565. (org-set-local 'adaptive-fill-regexp "\000")
  18566. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18567. (org-set-local 'adaptive-fill-function
  18568. 'org-adaptive-fill-function)
  18569. (org-set-local
  18570. 'align-mode-rules-list
  18571. '((org-in-buffer-settings
  18572. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18573. (modes . '(org-mode))))))
  18574. (defun org-fill-item-nobreak-p ()
  18575. "Non-nil when a line break at point would insert a new item."
  18576. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18577. (defun org-fill-paragraph (&optional justify)
  18578. "Re-align a table, pass through to fill-paragraph if no table."
  18579. (let ((table-p (org-at-table-p))
  18580. (table.el-p (org-at-table.el-p))
  18581. (itemp (org-in-item-p)))
  18582. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18583. (save-excursion (goto-char (point-at-bol))
  18584. (looking-at org-outline-regexp)))
  18585. t) ; skip headlines
  18586. (table.el-p t) ; skip table.el tables
  18587. (table-p (org-table-align) t) ; align Org tables
  18588. (itemp ; align text in items
  18589. (let* ((struct (save-excursion (goto-char itemp)
  18590. (org-list-struct)))
  18591. (parents (org-list-parents-alist struct))
  18592. (children (org-list-get-children itemp struct parents))
  18593. beg end prev next prefix)
  18594. ;; Determine in which part of item point is: before
  18595. ;; first child, after last child, between two
  18596. ;; sub-lists, or simply in item if there's no child.
  18597. (cond
  18598. ((not children)
  18599. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18600. beg itemp
  18601. end (org-list-get-item-end itemp struct)))
  18602. ((< (point) (setq next (car children)))
  18603. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18604. beg itemp
  18605. end next))
  18606. ((> (point) (setq prev (car (last children))))
  18607. (setq beg (org-list-get-item-end prev struct)
  18608. end (org-list-get-item-end itemp struct)
  18609. prefix (save-excursion
  18610. (goto-char beg)
  18611. (skip-chars-forward " \t")
  18612. (make-string (current-column) ?\ ))))
  18613. (t (catch 'exit
  18614. (while (setq next (pop children))
  18615. (if (> (point) next)
  18616. (setq prev next)
  18617. (setq beg (org-list-get-item-end prev struct)
  18618. end next
  18619. prefix (save-excursion
  18620. (goto-char beg)
  18621. (skip-chars-forward " \t")
  18622. (make-string (current-column) ?\ )))
  18623. (throw 'exit nil))))))
  18624. ;; Use `fill-paragraph' with buffer narrowed to item
  18625. ;; without any child, and with our computed PREFIX.
  18626. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18627. (save-restriction
  18628. (narrow-to-region beg end)
  18629. (save-excursion (fill-paragraph justify)))) t))
  18630. ;; Special case where point is not in a list but is on
  18631. ;; a paragraph adjacent to a list: make sure this paragraph
  18632. ;; doesn't get merged with the end of the list by narrowing
  18633. ;; buffer first.
  18634. ((save-excursion (forward-paragraph -1)
  18635. (setq itemp (org-in-item-p)))
  18636. (let ((struct (save-excursion (goto-char itemp)
  18637. (org-list-struct))))
  18638. (save-restriction
  18639. (narrow-to-region (org-list-get-bottom-point struct)
  18640. (save-excursion (forward-paragraph 1)
  18641. (point)))
  18642. (fill-paragraph justify) t)))
  18643. ;; Else simply call `fill-paragraph'.
  18644. (t nil))))
  18645. ;; For reference, this is the default value of adaptive-fill-regexp
  18646. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18647. (defun org-adaptive-fill-function ()
  18648. "Return a fill prefix for org-mode files."
  18649. (let (itemp)
  18650. (save-excursion
  18651. (cond
  18652. ;; Comment line
  18653. ((looking-at "#[ \t]+")
  18654. (match-string-no-properties 0))
  18655. ;; Plain list item
  18656. ((org-at-item-p)
  18657. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18658. ;; Point is in a list after `backward-paragraph': original
  18659. ;; point wasn't in the list, or filling would have been taken
  18660. ;; care of by `org-auto-fill-function', but the list and the
  18661. ;; real paragraph are not separated by a blank line. Thus, move
  18662. ;; point after the list to go back to real paragraph and
  18663. ;; determine fill-prefix.
  18664. ((setq itemp (org-in-item-p))
  18665. (goto-char itemp)
  18666. (let* ((struct (org-list-struct))
  18667. (bottom (org-list-get-bottom-point struct)))
  18668. (goto-char bottom)
  18669. (make-string (org-get-indentation) ?\ )))
  18670. ;; Other text
  18671. ((looking-at org-adaptive-fill-regexp-backup)
  18672. (match-string-no-properties 0))))))
  18673. (defun org-auto-fill-function ()
  18674. "Auto-fill function."
  18675. (let (itemp prefix)
  18676. ;; When in a list, compute an appropriate fill-prefix and make
  18677. ;; sure it will be used by `do-auto-fill'.
  18678. (if (setq itemp (org-in-item-p))
  18679. (progn
  18680. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18681. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18682. (do-auto-fill)))
  18683. ;; Else just use `do-auto-fill'.
  18684. (do-auto-fill))))
  18685. ;;; Other stuff.
  18686. (defun org-toggle-fixed-width-section (arg)
  18687. "Toggle the fixed-width export.
  18688. If there is no active region, the QUOTE keyword at the current headline is
  18689. inserted or removed. When present, it causes the text between this headline
  18690. and the next to be exported as fixed-width text, and unmodified.
  18691. If there is an active region, this command adds or removes a colon as the
  18692. first character of this line. If the first character of a line is a colon,
  18693. this line is also exported in fixed-width font."
  18694. (interactive "P")
  18695. (let* ((cc 0)
  18696. (regionp (org-region-active-p))
  18697. (beg (if regionp (region-beginning) (point)))
  18698. (end (if regionp (region-end)))
  18699. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18700. (case-fold-search nil)
  18701. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18702. off)
  18703. (if regionp
  18704. (save-excursion
  18705. (goto-char beg)
  18706. (setq cc (current-column))
  18707. (beginning-of-line 1)
  18708. (setq off (looking-at re))
  18709. (while (> nlines 0)
  18710. (setq nlines (1- nlines))
  18711. (beginning-of-line 1)
  18712. (cond
  18713. (arg
  18714. (org-move-to-column cc t)
  18715. (insert ": \n")
  18716. (forward-line -1))
  18717. ((and off (looking-at re))
  18718. (replace-match "" t t nil 1))
  18719. ((not off) (org-move-to-column cc t) (insert ": ")))
  18720. (forward-line 1)))
  18721. (save-excursion
  18722. (org-back-to-heading)
  18723. (cond
  18724. ((looking-at (format org-heading-keyword-regexp-format
  18725. org-quote-string))
  18726. (goto-char (match-end 1))
  18727. (looking-at (concat " +" org-quote-string))
  18728. (replace-match "" t t)
  18729. (when (eolp) (insert " ")))
  18730. ((looking-at org-outline-regexp)
  18731. (goto-char (match-end 0))
  18732. (insert org-quote-string " ")))))))
  18733. (defun org-reftex-citation ()
  18734. "Use reftex-citation to insert a citation into the buffer.
  18735. This looks for a line like
  18736. #+BIBLIOGRAPHY: foo plain option:-d
  18737. and derives from it that foo.bib is the bibliography file relevant
  18738. for this document. It then installs the necessary environment for RefTeX
  18739. to work in this buffer and calls `reftex-citation' to insert a citation
  18740. into the buffer.
  18741. Export of such citations to both LaTeX and HTML is handled by the contributed
  18742. package org-exp-bibtex by Taru Karttunen."
  18743. (interactive)
  18744. (let ((reftex-docstruct-symbol 'rds)
  18745. (reftex-cite-format "\\cite{%l}")
  18746. rds bib)
  18747. (save-excursion
  18748. (save-restriction
  18749. (widen)
  18750. (let ((case-fold-search t)
  18751. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18752. (if (not (save-excursion
  18753. (or (re-search-forward re nil t)
  18754. (re-search-backward re nil t))))
  18755. (error "No bibliography defined in file")
  18756. (setq bib (concat (match-string 1) ".bib")
  18757. rds (list (list 'bib bib)))))))
  18758. (call-interactively 'reftex-citation)))
  18759. ;;;; Functions extending outline functionality
  18760. (defun org-beginning-of-line (&optional arg)
  18761. "Go to the beginning of the current line. If that is invisible, continue
  18762. to a visible line beginning. This makes the function of C-a more intuitive.
  18763. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18764. first attempt, and only move to after the tags when the cursor is already
  18765. beyond the end of the headline."
  18766. (interactive "P")
  18767. (let ((pos (point))
  18768. (special (if (consp org-special-ctrl-a/e)
  18769. (car org-special-ctrl-a/e)
  18770. org-special-ctrl-a/e))
  18771. refpos)
  18772. (if (org-bound-and-true-p line-move-visual)
  18773. (beginning-of-visual-line 1)
  18774. (beginning-of-line 1))
  18775. (if (and arg (fboundp 'move-beginning-of-line))
  18776. (call-interactively 'move-beginning-of-line)
  18777. (if (bobp)
  18778. nil
  18779. (backward-char 1)
  18780. (if (org-truely-invisible-p)
  18781. (while (and (not (bobp)) (org-truely-invisible-p))
  18782. (backward-char 1)
  18783. (beginning-of-line 1))
  18784. (forward-char 1))))
  18785. (when special
  18786. (cond
  18787. ((and (looking-at org-complex-heading-regexp)
  18788. (= (char-after (match-end 1)) ?\ ))
  18789. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18790. (point-at-eol)))
  18791. (goto-char
  18792. (if (eq special t)
  18793. (cond ((> pos refpos) refpos)
  18794. ((= pos (point)) refpos)
  18795. (t (point)))
  18796. (cond ((> pos (point)) (point))
  18797. ((not (eq last-command this-command)) (point))
  18798. (t refpos)))))
  18799. ((org-at-item-p)
  18800. ;; Being at an item and not looking at an the item means point
  18801. ;; was previously moved to beginning of a visual line, whiche
  18802. ;; doesn't contain the item. Therefore, do nothing special,
  18803. ;; just stay here.
  18804. (when (looking-at org-list-full-item-re)
  18805. ;; Set special position at first white space character after
  18806. ;; bullet, and check-box, if any.
  18807. (let ((after-bullet
  18808. (let ((box (match-end 3)))
  18809. (if (not box) (match-end 1)
  18810. (let ((after (char-after box)))
  18811. (if (and after (= after ? )) (1+ box) box))))))
  18812. ;; Special case: Move point to special position when
  18813. ;; currently after it or at beginning of line.
  18814. (if (eq special t)
  18815. (when (or (> pos after-bullet) (= (point) pos))
  18816. (goto-char after-bullet))
  18817. ;; Reversed case: Move point to special position when
  18818. ;; point was already at beginning of line and command is
  18819. ;; repeated.
  18820. (when (and (= (point) pos) (eq last-command this-command))
  18821. (goto-char after-bullet))))))))
  18822. (org-no-warnings
  18823. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18824. (defun org-end-of-line (&optional arg)
  18825. "Go to the end of the line.
  18826. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18827. first attempt, and only move to after the tags when the cursor is already
  18828. beyond the end of the headline."
  18829. (interactive "P")
  18830. (let ((special (if (consp org-special-ctrl-a/e)
  18831. (cdr org-special-ctrl-a/e)
  18832. org-special-ctrl-a/e)))
  18833. (cond
  18834. ((or (not special) arg
  18835. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  18836. (call-interactively
  18837. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18838. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18839. (t 'end-of-line))))
  18840. ((org-at-heading-p)
  18841. (let ((pos (point)))
  18842. (beginning-of-line 1)
  18843. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18844. (if (eq special t)
  18845. (if (or (< pos (match-beginning 1))
  18846. (= pos (match-end 0)))
  18847. (goto-char (match-beginning 1))
  18848. (goto-char (match-end 0)))
  18849. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18850. (goto-char (match-end 0))
  18851. (goto-char (match-beginning 1))))
  18852. (call-interactively (if (fboundp 'move-end-of-line)
  18853. 'move-end-of-line
  18854. 'end-of-line)))))
  18855. ((org-at-drawer-p)
  18856. (move-end-of-line 1)
  18857. (when (overlays-at (1- (point))) (backward-char 1)))
  18858. ;; At an item: Move before any hidden text.
  18859. (t (call-interactively
  18860. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18861. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18862. (t 'end-of-line)))))
  18863. (org-no-warnings
  18864. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18865. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18866. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18867. (defun org-backward-sentence (&optional arg)
  18868. "Go to beginning of sentence, or beginning of table field.
  18869. This will call `backward-sentence' or `org-table-beginning-of-field',
  18870. depending on context."
  18871. (interactive "P")
  18872. (cond
  18873. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18874. (t (call-interactively 'backward-sentence))))
  18875. (defun org-forward-sentence (&optional arg)
  18876. "Go to end of sentence, or end of table field.
  18877. This will call `forward-sentence' or `org-table-end-of-field',
  18878. depending on context."
  18879. (interactive "P")
  18880. (cond
  18881. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18882. (t (call-interactively 'forward-sentence))))
  18883. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18884. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18885. (defun org-kill-line (&optional arg)
  18886. "Kill line, to tags or end of line."
  18887. (interactive "P")
  18888. (cond
  18889. ((or (not org-special-ctrl-k)
  18890. (bolp)
  18891. (not (org-at-heading-p)))
  18892. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18893. org-ctrl-k-protect-subtree)
  18894. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18895. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18896. (error "C-k aborted - would kill hidden subtree")))
  18897. (call-interactively 'kill-line))
  18898. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18899. (kill-region (point) (match-beginning 1))
  18900. (org-set-tags nil t))
  18901. (t (kill-region (point) (point-at-eol)))))
  18902. (define-key org-mode-map "\C-k" 'org-kill-line)
  18903. (defun org-yank (&optional arg)
  18904. "Yank. If the kill is a subtree, treat it specially.
  18905. This command will look at the current kill and check if is a single
  18906. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18907. cursor is at the beginning of a line or after the stars of a currently
  18908. empty headline, then the yank is handled specially. How exactly depends
  18909. on the value of the following variables, both set by default.
  18910. org-yank-folded-subtrees
  18911. When set, the subtree(s) will be folded after insertion, but only
  18912. if doing so would now swallow text after the yanked text.
  18913. org-yank-adjusted-subtrees
  18914. When set, the subtree will be promoted or demoted in order to
  18915. fit into the local outline tree structure, which means that the level
  18916. will be adjusted so that it becomes the smaller one of the two
  18917. *visible* surrounding headings.
  18918. Any prefix to this command will cause `yank' to be called directly with
  18919. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18920. will just
  18921. plainly yank the text as it is.
  18922. \[1] The test checks if the first non-white line is a heading
  18923. and if there are no other headings with fewer stars."
  18924. (interactive "P")
  18925. (org-yank-generic 'yank arg))
  18926. (defun org-yank-generic (command arg)
  18927. "Perform some yank-like command.
  18928. This function implements the behavior described in the `org-yank'
  18929. documentation. However, it has been generalized to work for any
  18930. interactive command with similar behavior."
  18931. ;; pretend to be command COMMAND
  18932. (setq this-command command)
  18933. (if arg
  18934. (call-interactively command)
  18935. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18936. (and (org-kill-is-subtree-p)
  18937. (or (bolp)
  18938. (and (looking-at "[ \t]*$")
  18939. (string-match
  18940. "\\`\\*+\\'"
  18941. (buffer-substring (point-at-bol) (point)))))))
  18942. swallowp)
  18943. (cond
  18944. ((and subtreep org-yank-folded-subtrees)
  18945. (let ((beg (point))
  18946. end)
  18947. (if (and subtreep org-yank-adjusted-subtrees)
  18948. (org-paste-subtree nil nil 'for-yank)
  18949. (call-interactively command))
  18950. (setq end (point))
  18951. (goto-char beg)
  18952. (when (and (bolp) subtreep
  18953. (not (setq swallowp
  18954. (org-yank-folding-would-swallow-text beg end))))
  18955. (org-with-limited-levels
  18956. (or (looking-at org-outline-regexp)
  18957. (re-search-forward org-outline-regexp-bol end t))
  18958. (while (and (< (point) end) (looking-at org-outline-regexp))
  18959. (hide-subtree)
  18960. (org-cycle-show-empty-lines 'folded)
  18961. (condition-case nil
  18962. (outline-forward-same-level 1)
  18963. (error (goto-char end))))))
  18964. (when swallowp
  18965. (message
  18966. "Inserted text not folded because that would swallow text"))
  18967. (goto-char end)
  18968. (skip-chars-forward " \t\n\r")
  18969. (beginning-of-line 1)
  18970. (push-mark beg 'nomsg)))
  18971. ((and subtreep org-yank-adjusted-subtrees)
  18972. (let ((beg (point-at-bol)))
  18973. (org-paste-subtree nil nil 'for-yank)
  18974. (push-mark beg 'nomsg)))
  18975. (t
  18976. (call-interactively command))))))
  18977. (defun org-yank-folding-would-swallow-text (beg end)
  18978. "Would hide-subtree at BEG swallow any text after END?"
  18979. (let (level)
  18980. (org-with-limited-levels
  18981. (save-excursion
  18982. (goto-char beg)
  18983. (when (or (looking-at org-outline-regexp)
  18984. (re-search-forward org-outline-regexp-bol end t))
  18985. (setq level (org-outline-level)))
  18986. (goto-char end)
  18987. (skip-chars-forward " \t\r\n\v\f")
  18988. (if (or (eobp)
  18989. (and (bolp) (looking-at org-outline-regexp)
  18990. (<= (org-outline-level) level)))
  18991. nil ; Nothing would be swallowed
  18992. t))))) ; something would swallow
  18993. (define-key org-mode-map "\C-y" 'org-yank)
  18994. (defun org-truely-invisible-p ()
  18995. "Check if point is at a character currently not visible.
  18996. This version does not only check the character property, but also
  18997. `visible-mode'."
  18998. ;; Early versions of noutline don't have `outline-invisible-p'.
  18999. (if (org-bound-and-true-p visible-mode)
  19000. nil
  19001. (outline-invisible-p)))
  19002. (defun org-invisible-p2 ()
  19003. "Check if point is at a character currently not visible."
  19004. (save-excursion
  19005. (if (and (eolp) (not (bobp))) (backward-char 1))
  19006. ;; Early versions of noutline don't have `outline-invisible-p'.
  19007. (outline-invisible-p)))
  19008. (defun org-back-to-heading (&optional invisible-ok)
  19009. "Call `outline-back-to-heading', but provide a better error message."
  19010. (condition-case nil
  19011. (outline-back-to-heading invisible-ok)
  19012. (error (error "Before first headline at position %d in buffer %s"
  19013. (point) (current-buffer)))))
  19014. (defun org-beginning-of-defun ()
  19015. "Go to the beginning of the subtree, i.e. back to the heading."
  19016. (org-back-to-heading))
  19017. (defun org-end-of-defun ()
  19018. "Go to the end of the subtree."
  19019. (org-end-of-subtree nil t))
  19020. (defun org-before-first-heading-p ()
  19021. "Before first heading?"
  19022. (save-excursion
  19023. (end-of-line)
  19024. (null (re-search-backward org-outline-regexp-bol nil t))))
  19025. (defun org-at-heading-p (&optional ignored)
  19026. (outline-on-heading-p t))
  19027. ;; Compatibility alias with Org versions < 7.8.03
  19028. (defalias 'org-on-heading-p 'org-at-heading-p)
  19029. (defun org-at-drawer-p nil
  19030. "Whether point is at a drawer."
  19031. (save-excursion
  19032. (move-beginning-of-line 1)
  19033. (looking-at org-drawer-regexp)))
  19034. (defun org-point-at-end-of-empty-headline ()
  19035. "If point is at the end of an empty headline, return t, else nil.
  19036. If the heading only contains a TODO keyword, it is still still considered
  19037. empty."
  19038. (and (looking-at "[ \t]*$")
  19039. (save-excursion
  19040. (beginning-of-line 1)
  19041. (let ((case-fold-search nil))
  19042. (looking-at org-todo-line-regexp)))
  19043. (string= (match-string 3) "")))
  19044. (defun org-at-heading-or-item-p ()
  19045. (or (org-at-heading-p) (org-at-item-p)))
  19046. (defun org-at-target-p ()
  19047. (or (org-in-regexp org-radio-target-regexp)
  19048. (org-in-regexp org-target-regexp)))
  19049. ;; Compatibility alias with Org versions < 7.8.03
  19050. (defalias 'org-on-target-p 'org-at-target-p)
  19051. (defun org-up-heading-all (arg)
  19052. "Move to the heading line of which the present line is a subheading.
  19053. This function considers both visible and invisible heading lines.
  19054. With argument, move up ARG levels."
  19055. (if (fboundp 'outline-up-heading-all)
  19056. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19057. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19058. (defun org-up-heading-safe ()
  19059. "Move to the heading line of which the present line is a subheading.
  19060. This version will not throw an error. It will return the level of the
  19061. headline found, or nil if no higher level is found.
  19062. Also, this function will be a lot faster than `outline-up-heading',
  19063. because it relies on stars being the outline starters. This can really
  19064. make a significant difference in outlines with very many siblings."
  19065. (let (start-level re)
  19066. (org-back-to-heading t)
  19067. (setq start-level (funcall outline-level))
  19068. (if (equal start-level 1)
  19069. nil
  19070. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19071. (if (re-search-backward re nil t)
  19072. (funcall outline-level)))))
  19073. (defun org-first-sibling-p ()
  19074. "Is this heading the first child of its parents?"
  19075. (interactive)
  19076. (let ((re org-outline-regexp-bol)
  19077. level l)
  19078. (unless (org-at-heading-p t)
  19079. (error "Not at a heading"))
  19080. (setq level (funcall outline-level))
  19081. (save-excursion
  19082. (if (not (re-search-backward re nil t))
  19083. t
  19084. (setq l (funcall outline-level))
  19085. (< l level)))))
  19086. (defun org-goto-sibling (&optional previous)
  19087. "Goto the next sibling, even if it is invisible.
  19088. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19089. when a sibling was found. When none is found, return nil and don't
  19090. move point."
  19091. (let ((fun (if previous 're-search-backward 're-search-forward))
  19092. (pos (point))
  19093. (re org-outline-regexp-bol)
  19094. level l)
  19095. (when (condition-case nil (org-back-to-heading t) (error nil))
  19096. (setq level (funcall outline-level))
  19097. (catch 'exit
  19098. (or previous (forward-char 1))
  19099. (while (funcall fun re nil t)
  19100. (setq l (funcall outline-level))
  19101. (when (< l level) (goto-char pos) (throw 'exit nil))
  19102. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19103. (goto-char pos)
  19104. nil))))
  19105. (defun org-show-siblings ()
  19106. "Show all siblings of the current headline."
  19107. (save-excursion
  19108. (while (org-goto-sibling) (org-flag-heading nil)))
  19109. (save-excursion
  19110. (while (org-goto-sibling 'previous)
  19111. (org-flag-heading nil))))
  19112. (defun org-goto-first-child ()
  19113. "Goto the first child, even if it is invisible.
  19114. Return t when a child was found. Otherwise don't move point and
  19115. return nil."
  19116. (let (level (pos (point)) (re org-outline-regexp-bol))
  19117. (when (condition-case nil (org-back-to-heading t) (error nil))
  19118. (setq level (outline-level))
  19119. (forward-char 1)
  19120. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19121. (progn (goto-char (match-beginning 0)) t)
  19122. (goto-char pos) nil))))
  19123. (defun org-show-hidden-entry ()
  19124. "Show an entry where even the heading is hidden."
  19125. (save-excursion
  19126. (org-show-entry)))
  19127. (defun org-flag-heading (flag &optional entry)
  19128. "Flag the current heading. FLAG non-nil means make invisible.
  19129. When ENTRY is non-nil, show the entire entry."
  19130. (save-excursion
  19131. (org-back-to-heading t)
  19132. ;; Check if we should show the entire entry
  19133. (if entry
  19134. (progn
  19135. (org-show-entry)
  19136. (save-excursion
  19137. (and (outline-next-heading)
  19138. (org-flag-heading nil))))
  19139. (outline-flag-region (max (point-min) (1- (point)))
  19140. (save-excursion (outline-end-of-heading) (point))
  19141. flag))))
  19142. (defun org-get-next-sibling ()
  19143. "Move to next heading of the same level, and return point.
  19144. If there is no such heading, return nil.
  19145. This is like outline-next-sibling, but invisible headings are ok."
  19146. (let ((level (funcall outline-level)))
  19147. (outline-next-heading)
  19148. (while (and (not (eobp)) (> (funcall outline-level) level))
  19149. (outline-next-heading))
  19150. (if (or (eobp) (< (funcall outline-level) level))
  19151. nil
  19152. (point))))
  19153. (defun org-get-last-sibling ()
  19154. "Move to previous heading of the same level, and return point.
  19155. If there is no such heading, return nil."
  19156. (let ((opoint (point))
  19157. (level (funcall outline-level)))
  19158. (outline-previous-heading)
  19159. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19160. (while (and (> (funcall outline-level) level)
  19161. (not (bobp)))
  19162. (outline-previous-heading))
  19163. (if (< (funcall outline-level) level)
  19164. nil
  19165. (point)))))
  19166. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19167. ;; This contains an exact copy of the original function, but it uses
  19168. ;; `org-back-to-heading', to make it work also in invisible
  19169. ;; trees. And is uses an invisible-OK argument.
  19170. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19171. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19172. ;; with many children and grandchildren etc, this can be much faster
  19173. ;; than the outline version.
  19174. (org-back-to-heading invisible-OK)
  19175. (let ((first t)
  19176. (level (funcall outline-level)))
  19177. (if (and (eq major-mode 'org-mode) (< level 1000))
  19178. ;; A true heading (not a plain list item), in Org-mode
  19179. ;; This means we can easily find the end by looking
  19180. ;; only for the right number of stars. Using a regexp to do
  19181. ;; this is so much faster than using a Lisp loop.
  19182. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19183. (forward-char 1)
  19184. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19185. ;; something else, do it the slow way
  19186. (while (and (not (eobp))
  19187. (or first (> (funcall outline-level) level)))
  19188. (setq first nil)
  19189. (outline-next-heading)))
  19190. (unless to-heading
  19191. (if (memq (preceding-char) '(?\n ?\^M))
  19192. (progn
  19193. ;; Go to end of line before heading
  19194. (forward-char -1)
  19195. (if (memq (preceding-char) '(?\n ?\^M))
  19196. ;; leave blank line before heading
  19197. (forward-char -1))))))
  19198. (point))
  19199. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19200. "Use Org version in org-mode, for dramatic speed-up."
  19201. (if (eq major-mode 'org-mode)
  19202. (progn
  19203. (org-end-of-subtree nil t)
  19204. (unless (eobp) (backward-char 1)))
  19205. ad-do-it))
  19206. (defun org-end-of-meta-data-and-drawers ()
  19207. "Jump to the first text after meta data and drawers in the current entry.
  19208. This will move over empty lines, lines with planning time stamps,
  19209. clocking lines, and drawers."
  19210. (org-back-to-heading t)
  19211. (let ((end (save-excursion (outline-next-heading) (point)))
  19212. (re (concat "\\(" org-drawer-regexp "\\)"
  19213. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19214. (forward-line 1)
  19215. (while (re-search-forward re end t)
  19216. (if (not (match-end 1))
  19217. ;; empty or planning line
  19218. (forward-line 1)
  19219. ;; a drawer, find the end
  19220. (re-search-forward "^[ \t]*:END:" end 'move)
  19221. (forward-line 1)))
  19222. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19223. (point)))
  19224. (defun org-forward-same-level (arg &optional invisible-ok)
  19225. "Move forward to the arg'th subheading at same level as this one.
  19226. Stop at the first and last subheadings of a superior heading.
  19227. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19228. it wil also look at invisible ones."
  19229. (interactive "p")
  19230. (org-back-to-heading invisible-ok)
  19231. (org-at-heading-p)
  19232. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19233. (re (format "^\\*\\{1,%d\\} " level))
  19234. l)
  19235. (forward-char 1)
  19236. (while (> arg 0)
  19237. (while (and (re-search-forward re nil 'move)
  19238. (setq l (- (match-end 0) (match-beginning 0) 1))
  19239. (= l level)
  19240. (not invisible-ok)
  19241. (progn (backward-char 1) (outline-invisible-p)))
  19242. (if (< l level) (setq arg 1)))
  19243. (setq arg (1- arg)))
  19244. (beginning-of-line 1)))
  19245. (defun org-backward-same-level (arg &optional invisible-ok)
  19246. "Move backward to the arg'th subheading at same level as this one.
  19247. Stop at the first and last subheadings of a superior heading."
  19248. (interactive "p")
  19249. (org-back-to-heading)
  19250. (org-at-heading-p)
  19251. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19252. (re (format "^\\*\\{1,%d\\} " level))
  19253. l)
  19254. (while (> arg 0)
  19255. (while (and (re-search-backward re nil 'move)
  19256. (setq l (- (match-end 0) (match-beginning 0) 1))
  19257. (= l level)
  19258. (not invisible-ok)
  19259. (outline-invisible-p))
  19260. (if (< l level) (setq arg 1)))
  19261. (setq arg (1- arg)))))
  19262. (defun org-show-subtree ()
  19263. "Show everything after this heading at deeper levels."
  19264. (interactive)
  19265. (outline-flag-region
  19266. (point)
  19267. (save-excursion
  19268. (org-end-of-subtree t t))
  19269. nil))
  19270. (defun org-show-entry ()
  19271. "Show the body directly following this heading.
  19272. Show the heading too, if it is currently invisible."
  19273. (interactive)
  19274. (save-excursion
  19275. (condition-case nil
  19276. (progn
  19277. (org-back-to-heading t)
  19278. (outline-flag-region
  19279. (max (point-min) (1- (point)))
  19280. (save-excursion
  19281. (if (re-search-forward
  19282. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19283. (match-beginning 1)
  19284. (point-max)))
  19285. nil)
  19286. (org-cycle-hide-drawers 'children))
  19287. (error nil))))
  19288. (defun org-make-options-regexp (kwds &optional extra)
  19289. "Make a regular expression for keyword lines."
  19290. (concat
  19291. "^"
  19292. "#?[ \t]*\\+\\("
  19293. (mapconcat 'regexp-quote kwds "\\|")
  19294. (if extra (concat "\\|" extra))
  19295. "\\):[ \t]*"
  19296. "\\(.*\\)"))
  19297. ;; Make isearch reveal the necessary context
  19298. (defun org-isearch-end ()
  19299. "Reveal context after isearch exits."
  19300. (when isearch-success ; only if search was successful
  19301. (if (featurep 'xemacs)
  19302. ;; Under XEmacs, the hook is run in the correct place,
  19303. ;; we directly show the context.
  19304. (org-show-context 'isearch)
  19305. ;; In Emacs the hook runs *before* restoring the overlays.
  19306. ;; So we have to use a one-time post-command-hook to do this.
  19307. ;; (Emacs 22 has a special variable, see function `org-mode')
  19308. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19309. isearch-mode-end-hook-quit)
  19310. ;; Only when the isearch was not quitted.
  19311. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19312. 'append 'local)))))
  19313. (defun org-isearch-post-command ()
  19314. "Remove self from hook, and show context."
  19315. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19316. (org-show-context 'isearch))
  19317. ;;;; Integration with and fixes for other packages
  19318. ;;; Imenu support
  19319. (defvar org-imenu-markers nil
  19320. "All markers currently used by Imenu.")
  19321. (make-variable-buffer-local 'org-imenu-markers)
  19322. (defun org-imenu-new-marker (&optional pos)
  19323. "Return a new marker for use by Imenu, and remember the marker."
  19324. (let ((m (make-marker)))
  19325. (move-marker m (or pos (point)))
  19326. (push m org-imenu-markers)
  19327. m))
  19328. (defun org-imenu-get-tree ()
  19329. "Produce the index for Imenu."
  19330. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19331. (setq org-imenu-markers nil)
  19332. (let* ((n org-imenu-depth)
  19333. (re (concat "^" (org-get-limited-outline-regexp)))
  19334. (subs (make-vector (1+ n) nil))
  19335. (last-level 0)
  19336. m level head)
  19337. (save-excursion
  19338. (save-restriction
  19339. (widen)
  19340. (goto-char (point-max))
  19341. (while (re-search-backward re nil t)
  19342. (setq level (org-reduced-level (funcall outline-level)))
  19343. (when (and (<= level n)
  19344. (looking-at org-complex-heading-regexp))
  19345. (setq head (org-link-display-format
  19346. (org-match-string-no-properties 4))
  19347. m (org-imenu-new-marker))
  19348. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19349. (if (>= level last-level)
  19350. (push (cons head m) (aref subs level))
  19351. (push (cons head (aref subs (1+ level))) (aref subs level))
  19352. (loop for i from (1+ level) to n do (aset subs i nil)))
  19353. (setq last-level level)))))
  19354. (aref subs 1)))
  19355. (eval-after-load "imenu"
  19356. '(progn
  19357. (add-hook 'imenu-after-jump-hook
  19358. (lambda ()
  19359. (if (eq major-mode 'org-mode)
  19360. (org-show-context 'org-goto))))))
  19361. (defun org-link-display-format (link)
  19362. "Replace a link with either the description, or the link target
  19363. if no description is present"
  19364. (save-match-data
  19365. (if (string-match org-bracket-link-analytic-regexp link)
  19366. (replace-match (if (match-end 5)
  19367. (match-string 5 link)
  19368. (concat (match-string 1 link)
  19369. (match-string 3 link)))
  19370. nil t link)
  19371. link)))
  19372. (defun org-toggle-link-display ()
  19373. "Toggle the literal or descriptive display of links."
  19374. (interactive)
  19375. (if org-descriptive-links
  19376. (progn (org-remove-from-invisibility-spec '(org-link))
  19377. (org-restart-font-lock)
  19378. (setq org-descriptive-links nil))
  19379. (progn (add-to-invisibility-spec '(org-link))
  19380. (org-restart-font-lock)
  19381. (setq org-descriptive-links t))))
  19382. ;; Speedbar support
  19383. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19384. "Overlay marking the agenda restriction line in speedbar.")
  19385. (overlay-put org-speedbar-restriction-lock-overlay
  19386. 'face 'org-agenda-restriction-lock)
  19387. (overlay-put org-speedbar-restriction-lock-overlay
  19388. 'help-echo "Agendas are currently limited to this item.")
  19389. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19390. (defun org-speedbar-set-agenda-restriction ()
  19391. "Restrict future agenda commands to the location at point in speedbar.
  19392. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19393. (interactive)
  19394. (require 'org-agenda)
  19395. (let (p m tp np dir txt)
  19396. (cond
  19397. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19398. 'org-imenu t))
  19399. (setq m (get-text-property p 'org-imenu-marker))
  19400. (with-current-buffer (marker-buffer m)
  19401. (goto-char m)
  19402. (org-agenda-set-restriction-lock 'subtree)))
  19403. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19404. 'speedbar-function 'speedbar-find-file))
  19405. (setq tp (previous-single-property-change
  19406. (1+ p) 'speedbar-function)
  19407. np (next-single-property-change
  19408. tp 'speedbar-function)
  19409. dir (speedbar-line-directory)
  19410. txt (buffer-substring-no-properties (or tp (point-min))
  19411. (or np (point-max))))
  19412. (with-current-buffer (find-file-noselect
  19413. (let ((default-directory dir))
  19414. (expand-file-name txt)))
  19415. (unless (eq major-mode 'org-mode)
  19416. (error "Cannot restrict to non-Org-mode file"))
  19417. (org-agenda-set-restriction-lock 'file)))
  19418. (t (error "Don't know how to restrict Org-mode's agenda")))
  19419. (move-overlay org-speedbar-restriction-lock-overlay
  19420. (point-at-bol) (point-at-eol))
  19421. (setq current-prefix-arg nil)
  19422. (org-agenda-maybe-redo)))
  19423. (eval-after-load "speedbar"
  19424. '(progn
  19425. (speedbar-add-supported-extension ".org")
  19426. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19427. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19428. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19429. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19430. (add-hook 'speedbar-visiting-tag-hook
  19431. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19432. ;;; Fixes and Hacks for problems with other packages
  19433. ;; Make flyspell not check words in links, to not mess up our keymap
  19434. (defun org-mode-flyspell-verify ()
  19435. "Don't let flyspell put overlays at active buttons, or on
  19436. {todo,all-time,additional-option-like}-keywords."
  19437. (let ((pos (max (1- (point)) (point-min)))
  19438. (word (thing-at-point 'word)))
  19439. (and (not (get-text-property pos 'keymap))
  19440. (not (get-text-property pos 'org-no-flyspell))
  19441. (not (member word org-todo-keywords-1))
  19442. (not (member word org-all-time-keywords))
  19443. (not (member word org-additional-option-like-keywords)))))
  19444. (defun org-remove-flyspell-overlays-in (beg end)
  19445. "Remove flyspell overlays in region."
  19446. (and (org-bound-and-true-p flyspell-mode)
  19447. (fboundp 'flyspell-delete-region-overlays)
  19448. (flyspell-delete-region-overlays beg end))
  19449. (add-text-properties beg end '(org-no-flyspell t)))
  19450. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19451. (eval-after-load "bookmark"
  19452. '(if (boundp 'bookmark-after-jump-hook)
  19453. ;; We can use the hook
  19454. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19455. ;; Hook not available, use advice
  19456. (defadvice bookmark-jump (after org-make-visible activate)
  19457. "Make the position visible."
  19458. (org-bookmark-jump-unhide))))
  19459. ;; Make sure saveplace shows the location if it was hidden
  19460. (eval-after-load "saveplace"
  19461. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19462. "Make the position visible."
  19463. (org-bookmark-jump-unhide)))
  19464. ;; Make sure ecb shows the location if it was hidden
  19465. (eval-after-load "ecb"
  19466. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19467. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19468. (if (eq major-mode 'org-mode)
  19469. (org-show-context))))
  19470. (defun org-bookmark-jump-unhide ()
  19471. "Unhide the current position, to show the bookmark location."
  19472. (and (eq major-mode 'org-mode)
  19473. (or (outline-invisible-p)
  19474. (save-excursion (goto-char (max (point-min) (1- (point))))
  19475. (outline-invisible-p)))
  19476. (org-show-context 'bookmark-jump)))
  19477. ;; Make session.el ignore our circular variable
  19478. (eval-after-load "session"
  19479. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19480. ;;;; Experimental code
  19481. (defun org-closed-in-range ()
  19482. "Sparse tree of items closed in a certain time range.
  19483. Still experimental, may disappear in the future."
  19484. (interactive)
  19485. ;; Get the time interval from the user.
  19486. (let* ((time1 (org-float-time
  19487. (org-read-date nil 'to-time nil "Starting date: ")))
  19488. (time2 (org-float-time
  19489. (org-read-date nil 'to-time nil "End date:")))
  19490. ;; callback function
  19491. (callback (lambda ()
  19492. (let ((time
  19493. (org-float-time
  19494. (apply 'encode-time
  19495. (org-parse-time-string
  19496. (match-string 1))))))
  19497. ;; check if time in interval
  19498. (and (>= time time1) (<= time time2))))))
  19499. ;; make tree, check each match with the callback
  19500. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19501. ;;;; Finish up
  19502. (provide 'org)
  19503. (run-hooks 'org-load-hook)
  19504. ;;; org.el ends here