org.el 799 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233
  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.03
  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. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  100. (declare-function org-at-clock-log-p "org-clock" ())
  101. (declare-function org-clock-timestamps-up "org-clock" ())
  102. (declare-function org-clock-timestamps-down "org-clock" ())
  103. ;; babel
  104. (require 'ob)
  105. (require 'ob-table)
  106. (require 'ob-lob)
  107. (require 'ob-ref)
  108. (require 'ob-tangle)
  109. (require 'ob-comint)
  110. (require 'ob-keys)
  111. ;; load languages based on value of `org-babel-load-languages'
  112. (defvar org-babel-load-languages)
  113. ;;;###autoload
  114. (defun org-babel-do-load-languages (sym value)
  115. "Load the languages defined in `org-babel-load-languages'."
  116. (set-default sym value)
  117. (mapc (lambda (pair)
  118. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  119. (if active
  120. (progn
  121. (require (intern (concat "ob-" lang))))
  122. (progn
  123. (funcall 'fmakunbound
  124. (intern (concat "org-babel-execute:" lang)))
  125. (funcall 'fmakunbound
  126. (intern (concat "org-babel-expand-body:" lang)))))))
  127. org-babel-load-languages))
  128. (defcustom org-babel-load-languages '((emacs-lisp . t))
  129. "Languages which can be evaluated in Org-mode buffers.
  130. This list can be used to load support for any of the languages
  131. below, note that each language will depend on a different set of
  132. system executables and/or Emacs modes. When a language is
  133. \"loaded\", then code blocks in that language can be evaluated
  134. with `org-babel-execute-src-block' bound by default to C-c
  135. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  136. be set to remove code block evaluation from the C-c C-c
  137. keybinding. By default only Emacs Lisp (which has no
  138. requirements) is loaded."
  139. :group 'org-babel
  140. :set 'org-babel-do-load-languages
  141. :type '(alist :tag "Babel Languages"
  142. :key-type
  143. (choice
  144. (const :tag "Awk" awk)
  145. (const :tag "C" C)
  146. (const :tag "R" R)
  147. (const :tag "Asymptote" asymptote)
  148. (const :tag "Calc" calc)
  149. (const :tag "Clojure" clojure)
  150. (const :tag "CSS" css)
  151. (const :tag "Ditaa" ditaa)
  152. (const :tag "Dot" dot)
  153. (const :tag "Emacs Lisp" emacs-lisp)
  154. (const :tag "Fortran" fortran)
  155. (const :tag "Gnuplot" gnuplot)
  156. (const :tag "Haskell" haskell)
  157. (const :tag "Java" java)
  158. (const :tag "Javascript" js)
  159. (const :tag "Latex" latex)
  160. (const :tag "Ledger" ledger)
  161. (const :tag "Lilypond" lilypond)
  162. (const :tag "Maxima" maxima)
  163. (const :tag "Matlab" matlab)
  164. (const :tag "Mscgen" mscgen)
  165. (const :tag "Ocaml" ocaml)
  166. (const :tag "Octave" octave)
  167. (const :tag "Org" org)
  168. (const :tag "Perl" perl)
  169. (const :tag "Pico Lisp" picolisp)
  170. (const :tag "PlantUML" plantuml)
  171. (const :tag "Python" python)
  172. (const :tag "Ruby" ruby)
  173. (const :tag "Sass" sass)
  174. (const :tag "Scheme" scheme)
  175. (const :tag "Screen" screen)
  176. (const :tag "Shell Script" sh)
  177. (const :tag "Shen" shen)
  178. (const :tag "Sql" sql)
  179. (const :tag "Sqlite" sqlite))
  180. :value-type (boolean :tag "Activate" :value t)))
  181. ;;;; Customization variables
  182. (defcustom org-clone-delete-id nil
  183. "Remove ID property of clones of a subtree.
  184. When non-nil, clones of a subtree don't inherit the ID property.
  185. Otherwise they inherit the ID property with a new unique
  186. identifier."
  187. :type 'boolean
  188. :group 'org-id)
  189. ;;; Version
  190. (defconst org-version "7.8.03"
  191. "The version number of the file org.el.")
  192. ;;;###autoload
  193. (defun org-version (&optional here)
  194. "Show the org-mode version in the echo area.
  195. With prefix arg HERE, insert it at point."
  196. (interactive "P")
  197. (let* ((origin default-directory)
  198. (version org-version)
  199. (git-version)
  200. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  201. (when (and (file-exists-p (expand-file-name ".git" dir))
  202. (executable-find "git"))
  203. (unwind-protect
  204. (progn
  205. (cd dir)
  206. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  207. (with-current-buffer "*Shell Command Output*"
  208. (goto-char (point-min))
  209. (setq git-version (buffer-substring (point) (point-at-eol))))
  210. (subst-char-in-string ?- ?. git-version t)
  211. (when (string-match "\\S-"
  212. (shell-command-to-string
  213. "git diff-index --name-only HEAD --"))
  214. (setq git-version (concat git-version ".dirty")))
  215. (setq version (concat version " (" git-version ")"))))
  216. (cd origin)))
  217. (setq version (format "Org-mode version %s" version))
  218. (if here (insert version))
  219. (message version)))
  220. ;;; Compatibility constants
  221. ;;; The custom variables
  222. (defgroup org nil
  223. "Outline-based notes management and organizer."
  224. :tag "Org"
  225. :group 'outlines
  226. :group 'calendar)
  227. (defcustom org-mode-hook nil
  228. "Mode hook for Org-mode, run after the mode was turned on."
  229. :group 'org
  230. :type 'hook)
  231. (defcustom org-load-hook nil
  232. "Hook that is run after org.el has been loaded."
  233. :group 'org
  234. :type 'hook)
  235. (defcustom org-log-buffer-setup-hook nil
  236. "Hook that is run after an Org log buffer is created."
  237. :group 'org
  238. :type 'hook)
  239. (defvar org-modules) ; defined below
  240. (defvar org-modules-loaded nil
  241. "Have the modules been loaded already?")
  242. (defun org-load-modules-maybe (&optional force)
  243. "Load all extensions listed in `org-modules'."
  244. (when (or force (not org-modules-loaded))
  245. (mapc (lambda (ext)
  246. (condition-case nil (require ext)
  247. (error (message "Problems while trying to load feature `%s'" ext))))
  248. org-modules)
  249. (setq org-modules-loaded t)))
  250. (defun org-set-modules (var value)
  251. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  252. (set var value)
  253. (when (featurep 'org)
  254. (org-load-modules-maybe 'force)))
  255. (when (org-bound-and-true-p org-modules)
  256. (let ((a (member 'org-infojs org-modules)))
  257. (and a (setcar a 'org-jsinfo))))
  258. (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)
  259. "Modules that should always be loaded together with org.el.
  260. If a description starts with <C>, the file is not part of Emacs
  261. and loading it will require that you have downloaded and properly installed
  262. the org-mode distribution.
  263. You can also use this system to load external packages (i.e. neither Org
  264. core modules, nor modules from the CONTRIB directory). Just add symbols
  265. to the end of the list. If the package is called org-xyz.el, then you need
  266. to add the symbol `xyz', and the package must have a call to
  267. (provide 'org-xyz)"
  268. :group 'org
  269. :set 'org-set-modules
  270. :type
  271. '(set :greedy t
  272. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  273. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  274. (const :tag " crypt: Encryption of subtrees" org-crypt)
  275. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  276. (const :tag " docview: Links to doc-view buffers" org-docview)
  277. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  278. (const :tag " id: Global IDs for identifying entries" org-id)
  279. (const :tag " info: Links to Info nodes" org-info)
  280. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  281. (const :tag " habit: Track your consistency with habits" org-habit)
  282. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  283. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  284. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  285. (const :tag " mew Links to Mew folders/messages" org-mew)
  286. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  287. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  288. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  289. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  290. (const :tag " vm: Links to VM folders/messages" org-vm)
  291. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  292. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  293. (const :tag " mouse: Additional mouse support" org-mouse)
  294. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  295. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  296. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  297. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  298. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  299. (const :tag "C collector: Collect properties into tables" org-collector)
  300. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  301. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  302. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  303. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  304. (const :tag "C eval: Include command output as text" org-eval)
  305. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  306. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  307. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  308. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  309. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  310. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  311. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  312. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  313. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  314. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  315. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  316. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  317. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  318. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  319. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  320. (const :tag "C registry: A registry for Org-mode links" org-registry)
  321. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  322. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  323. (const :tag "C secretary: Team management with org-mode" org-secretary)
  324. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  325. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  326. (const :tag "C track: Keep up with Org-mode development" org-track)
  327. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  328. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  329. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  330. (defcustom org-support-shift-select nil
  331. "Non-nil means make shift-cursor commands select text when possible.
  332. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  333. start selecting a region, or enlarge regions started in this way.
  334. In Org-mode, in special contexts, these same keys are used for
  335. other purposes, important enough to compete with shift selection.
  336. Org tries to balance these needs by supporting `shift-select-mode'
  337. outside these special contexts, under control of this variable.
  338. The default of this variable is nil, to avoid confusing behavior. Shifted
  339. cursor keys will then execute Org commands in the following contexts:
  340. - on a headline, changing TODO state (left/right) and priority (up/down)
  341. - on a time stamp, changing the time
  342. - in a plain list item, changing the bullet type
  343. - in a property definition line, switching between allowed values
  344. - in the BEGIN line of a clock table (changing the time block).
  345. Outside these contexts, the commands will throw an error.
  346. When this variable is t and the cursor is not in a special
  347. context, Org-mode will support shift-selection for making and
  348. enlarging regions. To make this more effective, the bullet
  349. cycling will no longer happen anywhere in an item line, but only
  350. if the cursor is exactly on the bullet.
  351. If you set this variable to the symbol `always', then the keys
  352. will not be special in headlines, property lines, and item lines,
  353. to make shift selection work there as well. If this is what you
  354. want, you can use the following alternative commands: `C-c C-t'
  355. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  356. can be used to switch TODO sets, `C-c -' to cycle item bullet
  357. types, and properties can be edited by hand or in column view.
  358. However, when the cursor is on a timestamp, shift-cursor commands
  359. will still edit the time stamp - this is just too good to give up.
  360. XEmacs user should have this variable set to nil, because
  361. `shift-select-mode' is in Emacs 23 or later only."
  362. :group 'org
  363. :type '(choice
  364. (const :tag "Never" nil)
  365. (const :tag "When outside special context" t)
  366. (const :tag "Everywhere except timestamps" always)))
  367. (defcustom org-loop-over-headlines-in-active-region nil
  368. "Shall some commands act upon headlines in the active region?
  369. When set to `t', some commands will be performed in all headlines
  370. within the active region.
  371. When set to `start-level', some commands will be performed in all
  372. headlines within the active region, provided that these headlines
  373. are of the same level than the first one.
  374. When set to a string, those commands will be performed on the
  375. matching headlines within the active region. Such string must be
  376. a tags/property/todo match as it is used in the agenda tags view.
  377. The list of commands is: `org-schedule', `org-deadline',
  378. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  379. `org-archive-to-archive-sibling'. The archiving commands skip
  380. already archived entries."
  381. :type '(choice (const :tag "Don't loop" nil)
  382. (const :tag "All headlines in active region" t)
  383. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  384. (string :tag "Tags/Property/Todo matcher"))
  385. :group 'org-todo
  386. :group 'org-archive)
  387. (defgroup org-startup nil
  388. "Options concerning startup of Org-mode."
  389. :tag "Org Startup"
  390. :group 'org)
  391. (defcustom org-startup-folded t
  392. "Non-nil means entering Org-mode will switch to OVERVIEW.
  393. This can also be configured on a per-file basis by adding one of
  394. the following lines anywhere in the buffer:
  395. #+STARTUP: fold (or `overview', this is equivalent)
  396. #+STARTUP: nofold (or `showall', this is equivalent)
  397. #+STARTUP: content
  398. #+STARTUP: showeverything"
  399. :group 'org-startup
  400. :type '(choice
  401. (const :tag "nofold: show all" nil)
  402. (const :tag "fold: overview" t)
  403. (const :tag "content: all headlines" content)
  404. (const :tag "show everything, even drawers" showeverything)))
  405. (defcustom org-startup-truncated t
  406. "Non-nil means entering Org-mode will set `truncate-lines'.
  407. This is useful since some lines containing links can be very long and
  408. uninteresting. Also tables look terrible when wrapped."
  409. :group 'org-startup
  410. :type 'boolean)
  411. (defcustom org-startup-indented nil
  412. "Non-nil means turn on `org-indent-mode' on startup.
  413. This can also be configured on a per-file basis by adding one of
  414. the following lines anywhere in the buffer:
  415. #+STARTUP: indent
  416. #+STARTUP: noindent"
  417. :group 'org-structure
  418. :type '(choice
  419. (const :tag "Not" nil)
  420. (const :tag "Globally (slow on startup in large files)" t)))
  421. (defcustom org-use-sub-superscripts t
  422. "Non-nil means interpret \"_\" and \"^\" for export.
  423. When this option is turned on, you can use TeX-like syntax for sub- and
  424. superscripts. Several characters after \"_\" or \"^\" will be
  425. considered as a single item - so grouping with {} is normally not
  426. needed. For example, the following things will be parsed as single
  427. sub- or superscripts.
  428. 10^24 or 10^tau several digits will be considered 1 item.
  429. 10^-12 or 10^-tau a leading sign with digits or a word
  430. x^2-y^3 will be read as x^2 - y^3, because items are
  431. terminated by almost any nonword/nondigit char.
  432. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  433. Still, ambiguity is possible - so when in doubt use {} to enclose the
  434. sub/superscript. If you set this variable to the symbol `{}',
  435. the braces are *required* in order to trigger interpretations as
  436. sub/superscript. This can be helpful in documents that need \"_\"
  437. frequently in plain text.
  438. Not all export backends support this, but HTML does.
  439. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  440. :group 'org-startup
  441. :group 'org-export-translation
  442. :type '(choice
  443. (const :tag "Always interpret" t)
  444. (const :tag "Only with braces" {})
  445. (const :tag "Never interpret" nil)))
  446. (if (fboundp 'defvaralias)
  447. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  448. (defcustom org-startup-with-beamer-mode nil
  449. "Non-nil means turn on `org-beamer-mode' on startup.
  450. This can also be configured on a per-file basis by adding one of
  451. the following lines anywhere in the buffer:
  452. #+STARTUP: beamer"
  453. :group 'org-startup
  454. :type 'boolean)
  455. (defcustom org-startup-align-all-tables nil
  456. "Non-nil means align all tables when visiting a file.
  457. This is useful when the column width in tables is forced with <N> cookies
  458. in table fields. Such tables will look correct only after the first re-align.
  459. This can also be configured on a per-file basis by adding one of
  460. the following lines anywhere in the buffer:
  461. #+STARTUP: align
  462. #+STARTUP: noalign"
  463. :group 'org-startup
  464. :type 'boolean)
  465. (defcustom org-startup-with-inline-images nil
  466. "Non-nil means show inline images when loading a new Org file.
  467. This can also be configured on a per-file basis by adding one of
  468. the following lines anywhere in the buffer:
  469. #+STARTUP: inlineimages
  470. #+STARTUP: noinlineimages"
  471. :group 'org-startup
  472. :type 'boolean)
  473. (defcustom org-insert-mode-line-in-empty-file nil
  474. "Non-nil means insert the first line setting Org-mode in empty files.
  475. When the function `org-mode' is called interactively in an empty file, this
  476. normally means that the file name does not automatically trigger Org-mode.
  477. To ensure that the file will always be in Org-mode in the future, a
  478. line enforcing Org-mode will be inserted into the buffer, if this option
  479. has been set."
  480. :group 'org-startup
  481. :type 'boolean)
  482. (defcustom org-replace-disputed-keys nil
  483. "Non-nil means use alternative key bindings for some keys.
  484. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  485. These keys are also used by other packages like shift-selection-mode'
  486. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  487. If you want to use Org-mode together with one of these other modes,
  488. or more generally if you would like to move some Org-mode commands to
  489. other keys, set this variable and configure the keys with the variable
  490. `org-disputed-keys'.
  491. This option is only relevant at load-time of Org-mode, and must be set
  492. *before* org.el is loaded. Changing it requires a restart of Emacs to
  493. become effective."
  494. :group 'org-startup
  495. :type 'boolean)
  496. (defcustom org-use-extra-keys nil
  497. "Non-nil means use extra key sequence definitions for certain commands.
  498. This happens automatically if you run XEmacs or if `window-system'
  499. is nil. This variable lets you do the same manually. You must
  500. set it before loading org.
  501. Example: on Carbon Emacs 22 running graphically, with an external
  502. keyboard on a Powerbook, the default way of setting M-left might
  503. not work for either Alt or ESC. Setting this variable will make
  504. it work for ESC."
  505. :group 'org-startup
  506. :type 'boolean)
  507. (if (fboundp 'defvaralias)
  508. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  509. (defcustom org-disputed-keys
  510. '(([(shift up)] . [(meta p)])
  511. ([(shift down)] . [(meta n)])
  512. ([(shift left)] . [(meta -)])
  513. ([(shift right)] . [(meta +)])
  514. ([(control shift right)] . [(meta shift +)])
  515. ([(control shift left)] . [(meta shift -)]))
  516. "Keys for which Org-mode and other modes compete.
  517. This is an alist, cars are the default keys, second element specifies
  518. the alternative to use when `org-replace-disputed-keys' is t.
  519. Keys can be specified in any syntax supported by `define-key'.
  520. The value of this option takes effect only at Org-mode's startup,
  521. therefore you'll have to restart Emacs to apply it after changing."
  522. :group 'org-startup
  523. :type 'alist)
  524. (defun org-key (key)
  525. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  526. Or return the original if not disputed.
  527. Also apply the translations defined in `org-xemacs-key-equivalents'."
  528. (when org-replace-disputed-keys
  529. (let* ((nkey (key-description key))
  530. (x (org-find-if (lambda (x)
  531. (equal (key-description (car x)) nkey))
  532. org-disputed-keys)))
  533. (setq key (if x (cdr x) key))))
  534. (when (featurep 'xemacs)
  535. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  536. key)
  537. (defun org-find-if (predicate seq)
  538. (catch 'exit
  539. (while seq
  540. (if (funcall predicate (car seq))
  541. (throw 'exit (car seq))
  542. (pop seq)))))
  543. (defun org-defkey (keymap key def)
  544. "Define a key, possibly translated, as returned by `org-key'."
  545. (define-key keymap (org-key key) def))
  546. (defcustom org-ellipsis nil
  547. "The ellipsis to use in the Org-mode outline.
  548. When nil, just use the standard three dots. When a string, use that instead,
  549. When a face, use the standard 3 dots, but with the specified face.
  550. The change affects only Org-mode (which will then use its own display table).
  551. Changing this requires executing `M-x org-mode' in a buffer to become
  552. effective."
  553. :group 'org-startup
  554. :type '(choice (const :tag "Default" nil)
  555. (face :tag "Face" :value org-warning)
  556. (string :tag "String" :value "...#")))
  557. (defvar org-display-table nil
  558. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  559. (defgroup org-keywords nil
  560. "Keywords in Org-mode."
  561. :tag "Org Keywords"
  562. :group 'org)
  563. (defcustom org-deadline-string "DEADLINE:"
  564. "String to mark deadline entries.
  565. A deadline is this string, followed by a time stamp. Should be a word,
  566. terminated by a colon. You can insert a schedule keyword and
  567. a timestamp with \\[org-deadline].
  568. Changes become only effective after restarting Emacs."
  569. :group 'org-keywords
  570. :type 'string)
  571. (defcustom org-scheduled-string "SCHEDULED:"
  572. "String to mark scheduled TODO entries.
  573. A schedule is this string, followed by a time stamp. Should be a word,
  574. terminated by a colon. You can insert a schedule keyword and
  575. a timestamp with \\[org-schedule].
  576. Changes become only effective after restarting Emacs."
  577. :group 'org-keywords
  578. :type 'string)
  579. (defcustom org-closed-string "CLOSED:"
  580. "String used as the prefix for timestamps logging closing a TODO entry."
  581. :group 'org-keywords
  582. :type 'string)
  583. (defcustom org-clock-string "CLOCK:"
  584. "String used as prefix for timestamps clocking work hours on an item."
  585. :group 'org-keywords
  586. :type 'string)
  587. (defcustom org-comment-string "COMMENT"
  588. "Entries starting with this keyword will never be exported.
  589. An entry can be toggled between COMMENT and normal with
  590. \\[org-toggle-comment].
  591. Changes become only effective after restarting Emacs."
  592. :group 'org-keywords
  593. :type 'string)
  594. (defcustom org-quote-string "QUOTE"
  595. "Entries starting with this keyword will be exported in fixed-width font.
  596. Quoting applies only to the text in the entry following the headline, and does
  597. not extend beyond the next headline, even if that is lower level.
  598. An entry can be toggled between QUOTE and normal with
  599. \\[org-toggle-fixed-width-section]."
  600. :group 'org-keywords
  601. :type 'string)
  602. (defconst org-repeat-re
  603. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  604. "Regular expression for specifying repeated events.
  605. After a match, group 1 contains the repeat expression.")
  606. (defgroup org-structure nil
  607. "Options concerning the general structure of Org-mode files."
  608. :tag "Org Structure"
  609. :group 'org)
  610. (defgroup org-reveal-location nil
  611. "Options about how to make context of a location visible."
  612. :tag "Org Reveal Location"
  613. :group 'org-structure)
  614. (defconst org-context-choice
  615. '(choice
  616. (const :tag "Always" t)
  617. (const :tag "Never" nil)
  618. (repeat :greedy t :tag "Individual contexts"
  619. (cons
  620. (choice :tag "Context"
  621. (const agenda)
  622. (const org-goto)
  623. (const occur-tree)
  624. (const tags-tree)
  625. (const link-search)
  626. (const mark-goto)
  627. (const bookmark-jump)
  628. (const isearch)
  629. (const default))
  630. (boolean))))
  631. "Contexts for the reveal options.")
  632. (defcustom org-show-hierarchy-above '((default . t))
  633. "Non-nil means show full hierarchy when revealing a location.
  634. Org-mode often shows locations in an org-mode file which might have
  635. been invisible before. When this is set, the hierarchy of headings
  636. above the exposed location is shown.
  637. Turning this off for example for sparse trees makes them very compact.
  638. Instead of t, this can also be an alist specifying this option for different
  639. contexts. Valid contexts are
  640. agenda when exposing an entry from the agenda
  641. org-goto when using the command `org-goto' on key C-c C-j
  642. occur-tree when using the command `org-occur' on key C-c /
  643. tags-tree when constructing a sparse tree based on tags matches
  644. link-search when exposing search matches associated with a link
  645. mark-goto when exposing the jump goal of a mark
  646. bookmark-jump when exposing a bookmark location
  647. isearch when exiting from an incremental search
  648. default default for all contexts not set explicitly"
  649. :group 'org-reveal-location
  650. :type org-context-choice)
  651. (defcustom org-show-following-heading '((default . nil))
  652. "Non-nil means show following heading when revealing a location.
  653. Org-mode often shows locations in an org-mode file which might have
  654. been invisible before. When this is set, the heading following the
  655. match is shown.
  656. Turning this off for example for sparse trees makes them very compact,
  657. but makes it harder to edit the location of the match. In such a case,
  658. use the command \\[org-reveal] to show more context.
  659. Instead of t, this can also be an alist specifying this option for different
  660. contexts. See `org-show-hierarchy-above' for valid contexts."
  661. :group 'org-reveal-location
  662. :type org-context-choice)
  663. (defcustom org-show-siblings '((default . nil) (isearch t))
  664. "Non-nil means show all sibling heading when revealing a location.
  665. Org-mode often shows locations in an org-mode file which might have
  666. been invisible before. When this is set, the sibling of the current entry
  667. heading are all made visible. If `org-show-hierarchy-above' is t,
  668. the same happens on each level of the hierarchy above the current entry.
  669. By default this is on for the isearch context, off for all other contexts.
  670. Turning this off for example for sparse trees makes them very compact,
  671. but makes it harder to edit the location of the match. In such a case,
  672. use the command \\[org-reveal] to show more context.
  673. Instead of t, this can also be an alist specifying this option for different
  674. contexts. See `org-show-hierarchy-above' for valid contexts."
  675. :group 'org-reveal-location
  676. :type org-context-choice)
  677. (defcustom org-show-entry-below '((default . nil))
  678. "Non-nil means show the entry below a headline when revealing a location.
  679. Org-mode often shows locations in an org-mode file which might have
  680. been invisible before. When this is set, the text below the headline that is
  681. exposed is also shown.
  682. By default this is off for all contexts.
  683. Instead of t, this can also be an alist specifying this option for different
  684. contexts. See `org-show-hierarchy-above' for valid contexts."
  685. :group 'org-reveal-location
  686. :type org-context-choice)
  687. (defcustom org-indirect-buffer-display 'other-window
  688. "How should indirect tree buffers be displayed?
  689. This applies to indirect buffers created with the commands
  690. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  691. Valid values are:
  692. current-window Display in the current window
  693. other-window Just display in another window.
  694. dedicated-frame Create one new frame, and re-use it each time.
  695. new-frame Make a new frame each time. Note that in this case
  696. previously-made indirect buffers are kept, and you need to
  697. kill these buffers yourself."
  698. :group 'org-structure
  699. :group 'org-agenda-windows
  700. :type '(choice
  701. (const :tag "In current window" current-window)
  702. (const :tag "In current frame, other window" other-window)
  703. (const :tag "Each time a new frame" new-frame)
  704. (const :tag "One dedicated frame" dedicated-frame)))
  705. (defcustom org-use-speed-commands nil
  706. "Non-nil means activate single letter commands at beginning of a headline.
  707. This may also be a function to test for appropriate locations where speed
  708. commands should be active."
  709. :group 'org-structure
  710. :type '(choice
  711. (const :tag "Never" nil)
  712. (const :tag "At beginning of headline stars" t)
  713. (function)))
  714. (defcustom org-speed-commands-user nil
  715. "Alist of additional speed commands.
  716. This list will be checked before `org-speed-commands-default'
  717. when the variable `org-use-speed-commands' is non-nil
  718. and when the cursor is at the beginning of a headline.
  719. The car if each entry is a string with a single letter, which must
  720. be assigned to `self-insert-command' in the global map.
  721. The cdr is either a command to be called interactively, a function
  722. to be called, or a form to be evaluated.
  723. An entry that is just a list with a single string will be interpreted
  724. as a descriptive headline that will be added when listing the speed
  725. commands in the Help buffer using the `?' speed command."
  726. :group 'org-structure
  727. :type '(repeat :value ("k" . ignore)
  728. (choice :value ("k" . ignore)
  729. (list :tag "Descriptive Headline" (string :tag "Headline"))
  730. (cons :tag "Letter and Command"
  731. (string :tag "Command letter")
  732. (choice
  733. (function)
  734. (sexp))))))
  735. (defgroup org-cycle nil
  736. "Options concerning visibility cycling in Org-mode."
  737. :tag "Org Cycle"
  738. :group 'org-structure)
  739. (defcustom org-cycle-skip-children-state-if-no-children t
  740. "Non-nil means skip CHILDREN state in entries that don't have any."
  741. :group 'org-cycle
  742. :type 'boolean)
  743. (defcustom org-cycle-max-level nil
  744. "Maximum level which should still be subject to visibility cycling.
  745. Levels higher than this will, for cycling, be treated as text, not a headline.
  746. When `org-odd-levels-only' is set, a value of N in this variable actually
  747. means 2N-1 stars as the limiting headline.
  748. When nil, cycle all levels.
  749. Note that the limiting level of cycling is also influenced by
  750. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  751. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  752. than its value."
  753. :group 'org-cycle
  754. :type '(choice
  755. (const :tag "No limit" nil)
  756. (integer :tag "Maximum level")))
  757. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  758. "Names of drawers. Drawers are not opened by cycling on the headline above.
  759. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  760. this:
  761. :DRAWERNAME:
  762. .....
  763. :END:
  764. The drawer \"PROPERTIES\" is special for capturing properties through
  765. the property API.
  766. Drawers can be defined on the per-file basis with a line like:
  767. #+DRAWERS: HIDDEN STATE PROPERTIES"
  768. :group 'org-structure
  769. :group 'org-cycle
  770. :type '(repeat (string :tag "Drawer Name")))
  771. (defcustom org-hide-block-startup nil
  772. "Non-nil means entering Org-mode will fold all blocks.
  773. This can also be set in on a per-file basis with
  774. #+STARTUP: hideblocks
  775. #+STARTUP: showblocks"
  776. :group 'org-startup
  777. :group 'org-cycle
  778. :type 'boolean)
  779. (defcustom org-cycle-global-at-bob nil
  780. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  781. This makes it possible to do global cycling without having to use S-TAB or
  782. \\[universal-argument] TAB. For this special case to work, the first line \
  783. of the buffer
  784. must not be a headline - it may be empty or some other text. When used in
  785. this way, `org-cycle-hook' is disables temporarily, to make sure the
  786. cursor stays at the beginning of the buffer.
  787. When this option is nil, don't do anything special at the beginning
  788. of the buffer."
  789. :group 'org-cycle
  790. :type 'boolean)
  791. (defcustom org-cycle-level-after-item/entry-creation t
  792. "Non-nil means cycle entry level or item indentation in new empty entries.
  793. When the cursor is at the end of an empty headline, i.e with only stars
  794. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  795. and then all possible ancestor states, before returning to the original state.
  796. This makes data entry extremely fast: M-RET to create a new headline,
  797. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  798. When the cursor is at the end of an empty plain list item, one TAB will
  799. make it a subitem, two or more tabs will back up to make this an item
  800. higher up in the item hierarchy."
  801. :group 'org-cycle
  802. :type 'boolean)
  803. (defcustom org-cycle-emulate-tab t
  804. "Where should `org-cycle' emulate TAB.
  805. nil Never
  806. white Only in completely white lines
  807. whitestart Only at the beginning of lines, before the first non-white char
  808. t Everywhere except in headlines
  809. exc-hl-bol Everywhere except at the start of a headline
  810. If TAB is used in a place where it does not emulate TAB, the current subtree
  811. visibility is cycled."
  812. :group 'org-cycle
  813. :type '(choice (const :tag "Never" nil)
  814. (const :tag "Only in completely white lines" white)
  815. (const :tag "Before first char in a line" whitestart)
  816. (const :tag "Everywhere except in headlines" t)
  817. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  818. ))
  819. (defcustom org-cycle-separator-lines 2
  820. "Number of empty lines needed to keep an empty line between collapsed trees.
  821. If you leave an empty line between the end of a subtree and the following
  822. headline, this empty line is hidden when the subtree is folded.
  823. Org-mode will leave (exactly) one empty line visible if the number of
  824. empty lines is equal or larger to the number given in this variable.
  825. So the default 2 means at least 2 empty lines after the end of a subtree
  826. are needed to produce free space between a collapsed subtree and the
  827. following headline.
  828. If the number is negative, and the number of empty lines is at least -N,
  829. all empty lines are shown.
  830. Special case: when 0, never leave empty lines in collapsed view."
  831. :group 'org-cycle
  832. :type 'integer)
  833. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  834. (defcustom org-pre-cycle-hook nil
  835. "Hook that is run before visibility cycling is happening.
  836. The function(s) in this hook must accept a single argument which indicates
  837. the new state that will be set right after running this hook. The
  838. argument is a symbol. Before a global state change, it can have the values
  839. `overview', `content', or `all'. Before a local state change, it can have
  840. the values `folded', `children', or `subtree'."
  841. :group 'org-cycle
  842. :type 'hook)
  843. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  844. org-cycle-hide-drawers
  845. org-cycle-show-empty-lines
  846. org-optimize-window-after-visibility-change)
  847. "Hook that is run after `org-cycle' has changed the buffer visibility.
  848. The function(s) in this hook must accept a single argument which indicates
  849. the new state that was set by the most recent `org-cycle' command. The
  850. argument is a symbol. After a global state change, it can have the values
  851. `overview', `content', or `all'. After a local state change, it can have
  852. the values `folded', `children', or `subtree'."
  853. :group 'org-cycle
  854. :type 'hook)
  855. (defgroup org-edit-structure nil
  856. "Options concerning structure editing in Org-mode."
  857. :tag "Org Edit Structure"
  858. :group 'org-structure)
  859. (defcustom org-odd-levels-only nil
  860. "Non-nil means skip even levels and only use odd levels for the outline.
  861. This has the effect that two stars are being added/taken away in
  862. promotion/demotion commands. It also influences how levels are
  863. handled by the exporters.
  864. Changing it requires restart of `font-lock-mode' to become effective
  865. for fontification also in regions already fontified.
  866. You may also set this on a per-file basis by adding one of the following
  867. lines to the buffer:
  868. #+STARTUP: odd
  869. #+STARTUP: oddeven"
  870. :group 'org-edit-structure
  871. :group 'org-appearance
  872. :type 'boolean)
  873. (defcustom org-adapt-indentation t
  874. "Non-nil means adapt indentation to outline node level.
  875. When this variable is set, Org assumes that you write outlines by
  876. indenting text in each node to align with the headline (after the stars).
  877. The following issues are influenced by this variable:
  878. - When this is set and the *entire* text in an entry is indented, the
  879. indentation is increased by one space in a demotion command, and
  880. decreased by one in a promotion command. If any line in the entry
  881. body starts with text at column 0, indentation is not changed at all.
  882. - Property drawers and planning information is inserted indented when
  883. this variable s set. When nil, they will not be indented.
  884. - TAB indents a line relative to context. The lines below a headline
  885. will be indented when this variable is set.
  886. Note that this is all about true indentation, by adding and removing
  887. space characters. See also `org-indent.el' which does level-dependent
  888. indentation in a virtual way, i.e. at display time in Emacs."
  889. :group 'org-edit-structure
  890. :type 'boolean)
  891. (defcustom org-special-ctrl-a/e nil
  892. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  893. When t, `C-a' will bring back the cursor to the beginning of the
  894. headline text, i.e. after the stars and after a possible TODO keyword.
  895. In an item, this will be the position after the bullet.
  896. When the cursor is already at that position, another `C-a' will bring
  897. it to the beginning of the line.
  898. `C-e' will jump to the end of the headline, ignoring the presence of tags
  899. in the headline. A second `C-e' will then jump to the true end of the
  900. line, after any tags. This also means that, when this variable is
  901. non-nil, `C-e' also will never jump beyond the end of the heading of a
  902. folded section, i.e. not after the ellipses.
  903. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  904. going to the true line boundary first. Only a directly following, identical
  905. keypress will bring the cursor to the special positions.
  906. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  907. set separately."
  908. :group 'org-edit-structure
  909. :type '(choice
  910. (const :tag "off" nil)
  911. (const :tag "on: after stars/bullet and before tags first" t)
  912. (const :tag "reversed: true line boundary first" reversed)
  913. (cons :tag "Set C-a and C-e separately"
  914. (choice :tag "Special C-a"
  915. (const :tag "off" nil)
  916. (const :tag "on: after stars/bullet first" t)
  917. (const :tag "reversed: before stars/bullet first" reversed))
  918. (choice :tag "Special C-e"
  919. (const :tag "off" nil)
  920. (const :tag "on: before tags first" t)
  921. (const :tag "reversed: after tags first" reversed)))))
  922. (if (fboundp 'defvaralias)
  923. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  924. (defcustom org-special-ctrl-k nil
  925. "Non-nil means `C-k' will behave specially in headlines.
  926. When nil, `C-k' will call the default `kill-line' command.
  927. When t, the following will happen while the cursor is in the headline:
  928. - When the cursor is at the beginning of a headline, kill the entire
  929. line and possible the folded subtree below the line.
  930. - When in the middle of the headline text, kill the headline up to the tags.
  931. - When after the headline text, kill the tags."
  932. :group 'org-edit-structure
  933. :type 'boolean)
  934. (defcustom org-ctrl-k-protect-subtree nil
  935. "Non-nil means, do not delete a hidden subtree with C-k.
  936. When set to the symbol `error', simply throw an error when C-k is
  937. used to kill (part-of) a headline that has hidden text behind it.
  938. Any other non-nil value will result in a query to the user, if it is
  939. OK to kill that hidden subtree. When nil, kill without remorse."
  940. :group 'org-edit-structure
  941. :type '(choice
  942. (const :tag "Do not protect hidden subtrees" nil)
  943. (const :tag "Protect hidden subtrees with a security query" t)
  944. (const :tag "Never kill a hidden subtree with C-k" error)))
  945. (defcustom org-catch-invisible-edits nil
  946. "Check if in invisible region before inserting or deleting a character.
  947. Valid values are:
  948. nil Do not check, so just do invisible edits.
  949. error Throw an error and do nothing.
  950. show Make point visible, and do the requested edit.
  951. show-and-error Make point visible, then throw an error and abort the edit.
  952. smart Make point visible, and do insertion/deletion if it is
  953. adjacent to visible text and the change feels predictable.
  954. Never delete a previously invisible character or add in the
  955. middle or right after an invisible region. Basically, this
  956. allows insertion and backward-delete right before ellipses.
  957. FIXME: maybe in this case we should not even show?"
  958. :group 'org-edit-structure
  959. :type '(choice
  960. (const :tag "Do not check" nil)
  961. (const :tag "Throw error when trying to edit" error)
  962. (const :tag "Unhide, but do not do the edit" show-and-error)
  963. (const :tag "Show invisible part and do the edit" show)
  964. (const :tag "Be smart and do the right thing" smart)))
  965. (defcustom org-yank-folded-subtrees t
  966. "Non-nil means when yanking subtrees, fold them.
  967. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  968. it starts with a heading and all other headings in it are either children
  969. or siblings, then fold all the subtrees. However, do this only if no
  970. text after the yank would be swallowed into a folded tree by this action."
  971. :group 'org-edit-structure
  972. :type 'boolean)
  973. (defcustom org-yank-adjusted-subtrees nil
  974. "Non-nil means when yanking subtrees, adjust the level.
  975. With this setting, `org-paste-subtree' is used to insert the subtree, see
  976. this function for details."
  977. :group 'org-edit-structure
  978. :type 'boolean)
  979. (defcustom org-M-RET-may-split-line '((default . t))
  980. "Non-nil means M-RET will split the line at the cursor position.
  981. When nil, it will go to the end of the line before making a
  982. new line.
  983. You may also set this option in a different way for different
  984. contexts. Valid contexts are:
  985. headline when creating a new headline
  986. item when creating a new item
  987. table in a table field
  988. default the value to be used for all contexts not explicitly
  989. customized"
  990. :group 'org-structure
  991. :group 'org-table
  992. :type '(choice
  993. (const :tag "Always" t)
  994. (const :tag "Never" nil)
  995. (repeat :greedy t :tag "Individual contexts"
  996. (cons
  997. (choice :tag "Context"
  998. (const headline)
  999. (const item)
  1000. (const table)
  1001. (const default))
  1002. (boolean)))))
  1003. (defcustom org-insert-heading-respect-content nil
  1004. "Non-nil means insert new headings after the current subtree.
  1005. When nil, the new heading is created directly after the current line.
  1006. The commands \\[org-insert-heading-respect-content] and
  1007. \\[org-insert-todo-heading-respect-content] turn this variable on
  1008. for the duration of the command."
  1009. :group 'org-structure
  1010. :type 'boolean)
  1011. (defcustom org-blank-before-new-entry '((heading . auto)
  1012. (plain-list-item . auto))
  1013. "Should `org-insert-heading' leave a blank line before new heading/item?
  1014. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1015. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1016. which case Org will look at the surrounding headings/items and try to
  1017. make an intelligent decision whether to insert a blank line or not.
  1018. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1019. set, the setting here is ignored and no empty line is inserted, to avoid
  1020. breaking the list structure."
  1021. :group 'org-edit-structure
  1022. :type '(list
  1023. (cons (const heading)
  1024. (choice (const :tag "Never" nil)
  1025. (const :tag "Always" t)
  1026. (const :tag "Auto" auto)))
  1027. (cons (const plain-list-item)
  1028. (choice (const :tag "Never" nil)
  1029. (const :tag "Always" t)
  1030. (const :tag "Auto" auto)))))
  1031. (defcustom org-insert-heading-hook nil
  1032. "Hook being run after inserting a new heading."
  1033. :group 'org-edit-structure
  1034. :type 'hook)
  1035. (defcustom org-enable-fixed-width-editor t
  1036. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1037. This currently only means they are never auto-wrapped.
  1038. When nil, such lines will be treated like ordinary lines.
  1039. See also the QUOTE keyword."
  1040. :group 'org-edit-structure
  1041. :type 'boolean)
  1042. (defcustom org-goto-auto-isearch t
  1043. "Non-nil means typing characters in `org-goto' starts incremental search."
  1044. :group 'org-edit-structure
  1045. :type 'boolean)
  1046. (defgroup org-sparse-trees nil
  1047. "Options concerning sparse trees in Org-mode."
  1048. :tag "Org Sparse Trees"
  1049. :group 'org-structure)
  1050. (defcustom org-highlight-sparse-tree-matches t
  1051. "Non-nil means highlight all matches that define a sparse tree.
  1052. The highlights will automatically disappear the next time the buffer is
  1053. changed by an edit command."
  1054. :group 'org-sparse-trees
  1055. :type 'boolean)
  1056. (defcustom org-remove-highlights-with-change t
  1057. "Non-nil means any change to the buffer will remove temporary highlights.
  1058. Such highlights are created by `org-occur' and `org-clock-display'.
  1059. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1060. The highlights created by `org-preview-latex-fragment' always need
  1061. `C-c C-c' to be removed."
  1062. :group 'org-sparse-trees
  1063. :group 'org-time
  1064. :type 'boolean)
  1065. (defcustom org-occur-hook '(org-first-headline-recenter)
  1066. "Hook that is run after `org-occur' has constructed a sparse tree.
  1067. This can be used to recenter the window to show as much of the structure
  1068. as possible."
  1069. :group 'org-sparse-trees
  1070. :type 'hook)
  1071. (defgroup org-imenu-and-speedbar nil
  1072. "Options concerning imenu and speedbar in Org-mode."
  1073. :tag "Org Imenu and Speedbar"
  1074. :group 'org-structure)
  1075. (defcustom org-imenu-depth 2
  1076. "The maximum level for Imenu access to Org-mode headlines.
  1077. This also applied for speedbar access."
  1078. :group 'org-imenu-and-speedbar
  1079. :type 'integer)
  1080. (defgroup org-table nil
  1081. "Options concerning tables in Org-mode."
  1082. :tag "Org Table"
  1083. :group 'org)
  1084. (defcustom org-enable-table-editor 'optimized
  1085. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1086. When nil, such lines will be treated like ordinary lines.
  1087. When equal to the symbol `optimized', the table editor will be optimized to
  1088. do the following:
  1089. - Automatic overwrite mode in front of whitespace in table fields.
  1090. This makes the structure of the table stay in tact as long as the edited
  1091. field does not exceed the column width.
  1092. - Minimize the number of realigns. Normally, the table is aligned each time
  1093. TAB or RET are pressed to move to another field. With optimization this
  1094. happens only if changes to a field might have changed the column width.
  1095. Optimization requires replacing the functions `self-insert-command',
  1096. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1097. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1098. very good at guessing when a re-align will be necessary, but you can always
  1099. force one with \\[org-ctrl-c-ctrl-c].
  1100. If you would like to use the optimized version in Org-mode, but the
  1101. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1102. This variable can be used to turn on and off the table editor during a session,
  1103. but in order to toggle optimization, a restart is required.
  1104. See also the variable `org-table-auto-blank-field'."
  1105. :group 'org-table
  1106. :type '(choice
  1107. (const :tag "off" nil)
  1108. (const :tag "on" t)
  1109. (const :tag "on, optimized" optimized)))
  1110. (defcustom org-self-insert-cluster-for-undo t
  1111. "Non-nil means cluster self-insert commands for undo when possible.
  1112. If this is set, then, like in the Emacs command loop, 20 consecutive
  1113. characters will be undone together.
  1114. This is configurable, because there is some impact on typing performance."
  1115. :group 'org-table
  1116. :type 'boolean)
  1117. (defcustom org-table-tab-recognizes-table.el t
  1118. "Non-nil means TAB will automatically notice a table.el table.
  1119. When it sees such a table, it moves point into it and - if necessary -
  1120. calls `table-recognize-table'."
  1121. :group 'org-table-editing
  1122. :type 'boolean)
  1123. (defgroup org-link nil
  1124. "Options concerning links in Org-mode."
  1125. :tag "Org Link"
  1126. :group 'org)
  1127. (defvar org-link-abbrev-alist-local nil
  1128. "Buffer-local version of `org-link-abbrev-alist', which see.
  1129. The value of this is taken from the #+LINK lines.")
  1130. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1131. (defcustom org-link-abbrev-alist nil
  1132. "Alist of link abbreviations.
  1133. The car of each element is a string, to be replaced at the start of a link.
  1134. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1135. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1136. [[linkkey:tag][description]]
  1137. The 'linkkey' must be a word word, starting with a letter, followed
  1138. by letters, numbers, '-' or '_'.
  1139. If REPLACE is a string, the tag will simply be appended to create the link.
  1140. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1141. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1142. be inserted at that point (see the function `url-hexify-string').
  1143. REPLACE may also be a function that will be called with the tag as the
  1144. only argument to create the link, which should be returned as a string.
  1145. See the manual for examples."
  1146. :group 'org-link
  1147. :type '(repeat
  1148. (cons
  1149. (string :tag "Protocol")
  1150. (choice
  1151. (string :tag "Format")
  1152. (function)))))
  1153. (defcustom org-descriptive-links t
  1154. "Non-nil means Org will display descriptive links.
  1155. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1156. \"Org Website\", hiding the link itself and just displaying its
  1157. description. When set to `nil', Org will display the full links
  1158. literally.
  1159. You can interactively set the value of this variable by calling
  1160. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1161. :group 'org-link
  1162. :type 'boolean)
  1163. (defcustom org-link-file-path-type 'adaptive
  1164. "How the path name in file links should be stored.
  1165. Valid values are:
  1166. relative Relative to the current directory, i.e. the directory of the file
  1167. into which the link is being inserted.
  1168. absolute Absolute path, if possible with ~ for home directory.
  1169. noabbrev Absolute path, no abbreviation of home directory.
  1170. adaptive Use relative path for files in the current directory and sub-
  1171. directories of it. For other files, use an absolute path."
  1172. :group 'org-link
  1173. :type '(choice
  1174. (const relative)
  1175. (const absolute)
  1176. (const noabbrev)
  1177. (const adaptive)))
  1178. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1179. "Types of links that should be activated in Org-mode files.
  1180. This is a list of symbols, each leading to the activation of a certain link
  1181. type. In principle, it does not hurt to turn on most link types - there may
  1182. be a small gain when turning off unused link types. The types are:
  1183. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1184. angle Links in angular brackets that may contain whitespace like
  1185. <bbdb:Carsten Dominik>.
  1186. plain Plain links in normal text, no whitespace, like http://google.com.
  1187. radio Text that is matched by a radio target, see manual for details.
  1188. tag Tag settings in a headline (link to tag search).
  1189. date Time stamps (link to calendar).
  1190. footnote Footnote labels.
  1191. Changing this variable requires a restart of Emacs to become effective."
  1192. :group 'org-link
  1193. :type '(set :greedy t
  1194. (const :tag "Double bracket links" bracket)
  1195. (const :tag "Angular bracket links" angle)
  1196. (const :tag "Plain text links" plain)
  1197. (const :tag "Radio target matches" radio)
  1198. (const :tag "Tags" tag)
  1199. (const :tag "Timestamps" date)
  1200. (const :tag "Footnotes" footnote)))
  1201. (defcustom org-make-link-description-function nil
  1202. "Function to use to generate link descriptions from links.
  1203. If nil the link location will be used. This function must take
  1204. two parameters; the first is the link and the second the
  1205. description `org-insert-link' has generated, and should return the
  1206. description to use."
  1207. :group 'org-link
  1208. :type 'function)
  1209. (defgroup org-link-store nil
  1210. "Options concerning storing links in Org-mode."
  1211. :tag "Org Store Link"
  1212. :group 'org-link)
  1213. (defcustom org-email-link-description-format "Email %c: %.30s"
  1214. "Format of the description part of a link to an email or usenet message.
  1215. The following %-escapes will be replaced by corresponding information:
  1216. %F full \"From\" field
  1217. %f name, taken from \"From\" field, address if no name
  1218. %T full \"To\" field
  1219. %t first name in \"To\" field, address if no name
  1220. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1221. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1222. %s subject
  1223. %d date
  1224. %m message-id.
  1225. You may use normal field width specification between the % and the letter.
  1226. This is for example useful to limit the length of the subject.
  1227. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1228. :group 'org-link-store
  1229. :type 'string)
  1230. (defcustom org-from-is-user-regexp
  1231. (let (r1 r2)
  1232. (when (and user-mail-address (not (string= user-mail-address "")))
  1233. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1234. (when (and user-full-name (not (string= user-full-name "")))
  1235. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1236. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1237. "Regexp matched against the \"From:\" header of an email or usenet message.
  1238. It should match if the message is from the user him/herself."
  1239. :group 'org-link-store
  1240. :type 'regexp)
  1241. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1242. "Non-nil means storing a link to an Org file will use entry IDs.
  1243. Note that before this variable is even considered, org-id must be loaded,
  1244. so please customize `org-modules' and turn it on.
  1245. The variable can have the following values:
  1246. t Create an ID if needed to make a link to the current entry.
  1247. create-if-interactive
  1248. If `org-store-link' is called directly (interactively, as a user
  1249. command), do create an ID to support the link. But when doing the
  1250. job for remember, only use the ID if it already exists. The
  1251. purpose of this setting is to avoid proliferation of unwanted
  1252. IDs, just because you happen to be in an Org file when you
  1253. call `org-remember' that automatically and preemptively
  1254. creates a link. If you do want to get an ID link in a remember
  1255. template to an entry not having an ID, create it first by
  1256. explicitly creating a link to it, using `C-c C-l' first.
  1257. create-if-interactive-and-no-custom-id
  1258. Like create-if-interactive, but do not create an ID if there is
  1259. a CUSTOM_ID property defined in the entry. This is the default.
  1260. use-existing
  1261. Use existing ID, do not create one.
  1262. nil Never use an ID to make a link, instead link using a text search for
  1263. the headline text."
  1264. :group 'org-link-store
  1265. :type '(choice
  1266. (const :tag "Create ID to make link" t)
  1267. (const :tag "Create if storing link interactively"
  1268. create-if-interactive)
  1269. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1270. create-if-interactive-and-no-custom-id)
  1271. (const :tag "Only use existing" use-existing)
  1272. (const :tag "Do not use ID to create link" nil)))
  1273. (defcustom org-context-in-file-links t
  1274. "Non-nil means file links from `org-store-link' contain context.
  1275. A search string will be added to the file name with :: as separator and
  1276. used to find the context when the link is activated by the command
  1277. `org-open-at-point'. When this option is t, the entire active region
  1278. will be placed in the search string of the file link. If set to a
  1279. positive integer, only the first n lines of context will be stored.
  1280. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1281. negates this setting for the duration of the command."
  1282. :group 'org-link-store
  1283. :type '(choice boolean integer))
  1284. (defcustom org-keep-stored-link-after-insertion nil
  1285. "Non-nil means keep link in list for entire session.
  1286. The command `org-store-link' adds a link pointing to the current
  1287. location to an internal list. These links accumulate during a session.
  1288. The command `org-insert-link' can be used to insert links into any
  1289. Org-mode file (offering completion for all stored links). When this
  1290. option is nil, every link which has been inserted once using \\[org-insert-link]
  1291. will be removed from the list, to make completing the unused links
  1292. more efficient."
  1293. :group 'org-link-store
  1294. :type 'boolean)
  1295. (defgroup org-link-follow nil
  1296. "Options concerning following links in Org-mode."
  1297. :tag "Org Follow Link"
  1298. :group 'org-link)
  1299. (defcustom org-link-translation-function nil
  1300. "Function to translate links with different syntax to Org syntax.
  1301. This can be used to translate links created for example by the Planner
  1302. or emacs-wiki packages to Org syntax.
  1303. The function must accept two parameters, a TYPE containing the link
  1304. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1305. which is everything after the link protocol. It should return a cons
  1306. with possibly modified values of type and path.
  1307. Org contains a function for this, so if you set this variable to
  1308. `org-translate-link-from-planner', you should be able follow many
  1309. links created by planner."
  1310. :group 'org-link-follow
  1311. :type 'function)
  1312. (defcustom org-follow-link-hook nil
  1313. "Hook that is run after a link has been followed."
  1314. :group 'org-link-follow
  1315. :type 'hook)
  1316. (defcustom org-tab-follows-link nil
  1317. "Non-nil means on links TAB will follow the link.
  1318. Needs to be set before org.el is loaded.
  1319. This really should not be used, it does not make sense, and the
  1320. implementation is bad."
  1321. :group 'org-link-follow
  1322. :type 'boolean)
  1323. (defcustom org-return-follows-link nil
  1324. "Non-nil means on links RET will follow the link."
  1325. :group 'org-link-follow
  1326. :type 'boolean)
  1327. (defcustom org-mouse-1-follows-link
  1328. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1329. "Non-nil means mouse-1 on a link will follow the link.
  1330. A longer mouse click will still set point. Does not work on XEmacs.
  1331. Needs to be set before org.el is loaded."
  1332. :group 'org-link-follow
  1333. :type 'boolean)
  1334. (defcustom org-mark-ring-length 4
  1335. "Number of different positions to be recorded in the ring.
  1336. Changing this requires a restart of Emacs to work correctly."
  1337. :group 'org-link-follow
  1338. :type 'integer)
  1339. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1340. "Non-nil means internal links in Org files must exactly match a headline.
  1341. When nil, the link search tries to match a phrase with all words
  1342. in the search text."
  1343. :group 'org-link-follow
  1344. :type '(choice
  1345. (const :tag "Use fuzzy text search" nil)
  1346. (const :tag "Match only exact headline" t)
  1347. (const :tag "Match exact headline or query to create it"
  1348. query-to-create)))
  1349. (defcustom org-link-frame-setup
  1350. '((vm . vm-visit-folder-other-frame)
  1351. (gnus . org-gnus-no-new-news)
  1352. (file . find-file-other-window)
  1353. (wl . wl-other-frame))
  1354. "Setup the frame configuration for following links.
  1355. When following a link with Emacs, it may often be useful to display
  1356. this link in another window or frame. This variable can be used to
  1357. set this up for the different types of links.
  1358. For VM, use any of
  1359. `vm-visit-folder'
  1360. `vm-visit-folder-other-window'
  1361. `vm-visit-folder-other-frame'
  1362. For Gnus, use any of
  1363. `gnus'
  1364. `gnus-other-frame'
  1365. `org-gnus-no-new-news'
  1366. For FILE, use any of
  1367. `find-file'
  1368. `find-file-other-window'
  1369. `find-file-other-frame'
  1370. For Wanderlust use any of
  1371. `wl'
  1372. `wl-other-frame'
  1373. For the calendar, use the variable `calendar-setup'.
  1374. For BBDB, it is currently only possible to display the matches in
  1375. another window."
  1376. :group 'org-link-follow
  1377. :type '(list
  1378. (cons (const vm)
  1379. (choice
  1380. (const vm-visit-folder)
  1381. (const vm-visit-folder-other-window)
  1382. (const vm-visit-folder-other-frame)))
  1383. (cons (const gnus)
  1384. (choice
  1385. (const gnus)
  1386. (const gnus-other-frame)
  1387. (const org-gnus-no-new-news)))
  1388. (cons (const file)
  1389. (choice
  1390. (const find-file)
  1391. (const find-file-other-window)
  1392. (const find-file-other-frame)))
  1393. (cons (const wl)
  1394. (choice
  1395. (const wl)
  1396. (const wl-other-frame)))))
  1397. (defcustom org-display-internal-link-with-indirect-buffer nil
  1398. "Non-nil means use indirect buffer to display infile links.
  1399. Activating internal links (from one location in a file to another location
  1400. in the same file) normally just jumps to the location. When the link is
  1401. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1402. is displayed in
  1403. another window. When this option is set, the other window actually displays
  1404. an indirect buffer clone of the current buffer, to avoid any visibility
  1405. changes to the current buffer."
  1406. :group 'org-link-follow
  1407. :type 'boolean)
  1408. (defcustom org-open-non-existing-files nil
  1409. "Non-nil means `org-open-file' will open non-existing files.
  1410. When nil, an error will be generated.
  1411. This variable applies only to external applications because they
  1412. might choke on non-existing files. If the link is to a file that
  1413. will be opened in Emacs, the variable is ignored."
  1414. :group 'org-link-follow
  1415. :type 'boolean)
  1416. (defcustom org-open-directory-means-index-dot-org nil
  1417. "Non-nil means a link to a directory really means to index.org.
  1418. When nil, following a directory link will run dired or open a finder/explorer
  1419. window on that directory."
  1420. :group 'org-link-follow
  1421. :type 'boolean)
  1422. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1423. "Function and arguments to call for following mailto links.
  1424. This is a list with the first element being a Lisp function, and the
  1425. remaining elements being arguments to the function. In string arguments,
  1426. %a will be replaced by the address, and %s will be replaced by the subject
  1427. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1428. :group 'org-link-follow
  1429. :type '(choice
  1430. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1431. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1432. (const :tag "message-mail" (message-mail "%a" "%s"))
  1433. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1434. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1435. "Non-nil means ask for confirmation before executing shell links.
  1436. Shell links can be dangerous: just think about a link
  1437. [[shell:rm -rf ~/*][Google Search]]
  1438. This link would show up in your Org-mode document as \"Google Search\",
  1439. but really it would remove your entire home directory.
  1440. Therefore we advise against setting this variable to nil.
  1441. Just change it to `y-or-n-p' if you want to confirm with a
  1442. single keystroke rather than having to type \"yes\"."
  1443. :group 'org-link-follow
  1444. :type '(choice
  1445. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1446. (const :tag "with y-or-n (faster)" y-or-n-p)
  1447. (const :tag "no confirmation (dangerous)" nil)))
  1448. (put 'org-confirm-shell-link-function
  1449. 'safe-local-variable
  1450. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1451. (defcustom org-confirm-shell-link-not-regexp ""
  1452. "A regexp to skip confirmation for shell links."
  1453. :group 'org-link-follow
  1454. :type 'regexp)
  1455. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1456. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1457. Elisp links can be dangerous: just think about a link
  1458. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1459. This link would show up in your Org-mode document as \"Google Search\",
  1460. but really it would remove your entire home directory.
  1461. Therefore we advise against setting this variable to nil.
  1462. Just change it to `y-or-n-p' if you want to confirm with a
  1463. single keystroke rather than having to type \"yes\"."
  1464. :group 'org-link-follow
  1465. :type '(choice
  1466. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1467. (const :tag "with y-or-n (faster)" y-or-n-p)
  1468. (const :tag "no confirmation (dangerous)" nil)))
  1469. (put 'org-confirm-shell-link-function
  1470. 'safe-local-variable
  1471. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1472. (defcustom org-confirm-elisp-link-not-regexp ""
  1473. "A regexp to skip confirmation for Elisp links."
  1474. :group 'org-link-follow
  1475. :type 'regexp)
  1476. (defconst org-file-apps-defaults-gnu
  1477. '((remote . emacs)
  1478. (system . mailcap)
  1479. (t . mailcap))
  1480. "Default file applications on a UNIX or GNU/Linux system.
  1481. See `org-file-apps'.")
  1482. (defconst org-file-apps-defaults-macosx
  1483. '((remote . emacs)
  1484. (t . "open %s")
  1485. (system . "open %s")
  1486. ("ps.gz" . "gv %s")
  1487. ("eps.gz" . "gv %s")
  1488. ("dvi" . "xdvi %s")
  1489. ("fig" . "xfig %s"))
  1490. "Default file applications on a MacOS X system.
  1491. The system \"open\" is known as a default, but we use X11 applications
  1492. for some files for which the OS does not have a good default.
  1493. See `org-file-apps'.")
  1494. (defconst org-file-apps-defaults-windowsnt
  1495. (list
  1496. '(remote . emacs)
  1497. (cons t
  1498. (list (if (featurep 'xemacs)
  1499. 'mswindows-shell-execute
  1500. 'w32-shell-execute)
  1501. "open" 'file))
  1502. (cons 'system
  1503. (list (if (featurep 'xemacs)
  1504. 'mswindows-shell-execute
  1505. 'w32-shell-execute)
  1506. "open" 'file)))
  1507. "Default file applications on a Windows NT system.
  1508. The system \"open\" is used for most files.
  1509. See `org-file-apps'.")
  1510. (defcustom org-file-apps
  1511. '(
  1512. (auto-mode . emacs)
  1513. ("\\.mm\\'" . default)
  1514. ("\\.x?html?\\'" . default)
  1515. ("\\.pdf\\'" . default)
  1516. )
  1517. "External applications for opening `file:path' items in a document.
  1518. Org-mode uses system defaults for different file types, but
  1519. you can use this variable to set the application for a given file
  1520. extension. The entries in this list are cons cells where the car identifies
  1521. files and the cdr the corresponding command. Possible values for the
  1522. file identifier are
  1523. \"string\" A string as a file identifier can be interpreted in different
  1524. ways, depending on its contents:
  1525. - Alphanumeric characters only:
  1526. Match links with this file extension.
  1527. Example: (\"pdf\" . \"evince %s\")
  1528. to open PDFs with evince.
  1529. - Regular expression: Match links where the
  1530. filename matches the regexp. If you want to
  1531. use groups here, use shy groups.
  1532. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1533. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1534. to open *.html and *.xhtml with firefox.
  1535. - Regular expression which contains (non-shy) groups:
  1536. Match links where the whole link, including \"::\", and
  1537. anything after that, matches the regexp.
  1538. In a custom command string, %1, %2, etc. are replaced with
  1539. the parts of the link that were matched by the groups.
  1540. For backwards compatibility, if a command string is given
  1541. that does not use any of the group matches, this case is
  1542. handled identically to the second one (i.e. match against
  1543. file name only).
  1544. In a custom lisp form, you can access the group matches with
  1545. (match-string n link).
  1546. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1547. to open [[file:document.pdf::5]] with evince at page 5.
  1548. `directory' Matches a directory
  1549. `remote' Matches a remote file, accessible through tramp or efs.
  1550. Remote files most likely should be visited through Emacs
  1551. because external applications cannot handle such paths.
  1552. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1553. so all files Emacs knows how to handle. Using this with
  1554. command `emacs' will open most files in Emacs. Beware that this
  1555. will also open html files inside Emacs, unless you add
  1556. (\"html\" . default) to the list as well.
  1557. t Default for files not matched by any of the other options.
  1558. `system' The system command to open files, like `open' on Windows
  1559. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1560. that will be selected if you call `C-c C-o' with a double
  1561. \\[universal-argument] \\[universal-argument] prefix.
  1562. Possible values for the command are:
  1563. `emacs' The file will be visited by the current Emacs process.
  1564. `default' Use the default application for this file type, which is the
  1565. association for t in the list, most likely in the system-specific
  1566. part.
  1567. This can be used to overrule an unwanted setting in the
  1568. system-specific variable.
  1569. `system' Use the system command for opening files, like \"open\".
  1570. This command is specified by the entry whose car is `system'.
  1571. Most likely, the system-specific version of this variable
  1572. does define this command, but you can overrule/replace it
  1573. here.
  1574. string A command to be executed by a shell; %s will be replaced
  1575. by the path to the file.
  1576. sexp A Lisp form which will be evaluated. The file path will
  1577. be available in the Lisp variable `file'.
  1578. For more examples, see the system specific constants
  1579. `org-file-apps-defaults-macosx'
  1580. `org-file-apps-defaults-windowsnt'
  1581. `org-file-apps-defaults-gnu'."
  1582. :group 'org-link-follow
  1583. :type '(repeat
  1584. (cons (choice :value ""
  1585. (string :tag "Extension")
  1586. (const :tag "System command to open files" system)
  1587. (const :tag "Default for unrecognized files" t)
  1588. (const :tag "Remote file" remote)
  1589. (const :tag "Links to a directory" directory)
  1590. (const :tag "Any files that have Emacs modes"
  1591. auto-mode))
  1592. (choice :value ""
  1593. (const :tag "Visit with Emacs" emacs)
  1594. (const :tag "Use default" default)
  1595. (const :tag "Use the system command" system)
  1596. (string :tag "Command")
  1597. (sexp :tag "Lisp form")))))
  1598. (defgroup org-refile nil
  1599. "Options concerning refiling entries in Org-mode."
  1600. :tag "Org Refile"
  1601. :group 'org)
  1602. (defcustom org-directory "~/org"
  1603. "Directory with org files.
  1604. This is just a default location to look for Org files. There is no need
  1605. at all to put your files into this directory. It is only used in the
  1606. following situations:
  1607. 1. When a remember template specifies a target file that is not an
  1608. absolute path. The path will then be interpreted relative to
  1609. `org-directory'
  1610. 2. When a remember note is filed away in an interactive way (when exiting the
  1611. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1612. with `org-directory' as the default path."
  1613. :group 'org-refile
  1614. :group 'org-remember
  1615. :type 'directory)
  1616. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1617. "Default target for storing notes.
  1618. Used as a fall back file for org-remember.el and org-capture.el, for
  1619. templates that do not specify a target file."
  1620. :group 'org-refile
  1621. :group 'org-remember
  1622. :type '(choice
  1623. (const :tag "Default from remember-data-file" nil)
  1624. file))
  1625. (defcustom org-goto-interface 'outline
  1626. "The default interface to be used for `org-goto'.
  1627. Allowed values are:
  1628. outline The interface shows an outline of the relevant file
  1629. and the correct heading is found by moving through
  1630. the outline or by searching with incremental search.
  1631. outline-path-completion Headlines in the current buffer are offered via
  1632. completion. This is the interface also used by
  1633. the refile command."
  1634. :group 'org-refile
  1635. :type '(choice
  1636. (const :tag "Outline" outline)
  1637. (const :tag "Outline-path-completion" outline-path-completion)))
  1638. (defcustom org-goto-max-level 5
  1639. "Maximum target level when running `org-goto' with refile interface."
  1640. :group 'org-refile
  1641. :type 'integer)
  1642. (defcustom org-reverse-note-order nil
  1643. "Non-nil means store new notes at the beginning of a file or entry.
  1644. When nil, new notes will be filed to the end of a file or entry.
  1645. This can also be a list with cons cells of regular expressions that
  1646. are matched against file names, and values."
  1647. :group 'org-remember
  1648. :group 'org-refile
  1649. :type '(choice
  1650. (const :tag "Reverse always" t)
  1651. (const :tag "Reverse never" nil)
  1652. (repeat :tag "By file name regexp"
  1653. (cons regexp boolean))))
  1654. (defcustom org-log-refile nil
  1655. "Information to record when a task is refiled.
  1656. Possible values are:
  1657. nil Don't add anything
  1658. time Add a time stamp to the task
  1659. note Prompt for a note and add it with template `org-log-note-headings'
  1660. This option can also be set with on a per-file-basis with
  1661. #+STARTUP: nologrefile
  1662. #+STARTUP: logrefile
  1663. #+STARTUP: lognoterefile
  1664. You can have local logging settings for a subtree by setting the LOGGING
  1665. property to one or more of these keywords.
  1666. When bulk-refiling from the agenda, the value `note' is forbidden and
  1667. will temporarily be changed to `time'."
  1668. :group 'org-refile
  1669. :group 'org-progress
  1670. :type '(choice
  1671. (const :tag "No logging" nil)
  1672. (const :tag "Record timestamp" time)
  1673. (const :tag "Record timestamp with note." note)))
  1674. (defcustom org-refile-targets nil
  1675. "Targets for refiling entries with \\[org-refile].
  1676. This is a list of cons cells. Each cell contains:
  1677. - a specification of the files to be considered, either a list of files,
  1678. or a symbol whose function or variable value will be used to retrieve
  1679. a file name or a list of file names. If you use `org-agenda-files' for
  1680. that, all agenda files will be scanned for targets. Nil means consider
  1681. headings in the current buffer.
  1682. - A specification of how to find candidate refile targets. This may be
  1683. any of:
  1684. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1685. This tag has to be present in all target headlines, inheritance will
  1686. not be considered.
  1687. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1688. todo keyword.
  1689. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1690. headlines that are refiling targets.
  1691. - a cons cell (:level . N). Any headline of level N is considered a target.
  1692. Note that, when `org-odd-levels-only' is set, level corresponds to
  1693. order in hierarchy, not to the number of stars.
  1694. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1695. Note that, when `org-odd-levels-only' is set, level corresponds to
  1696. order in hierarchy, not to the number of stars.
  1697. Each element of this list generates a set of possible targets.
  1698. The union of these sets is presented (with completion) to
  1699. the user by `org-refile'.
  1700. You can set the variable `org-refile-target-verify-function' to a function
  1701. to verify each headline found by the simple criteria above.
  1702. When this variable is nil, all top-level headlines in the current buffer
  1703. are used, equivalent to the value `((nil . (:level . 1))'."
  1704. :group 'org-refile
  1705. :type '(repeat
  1706. (cons
  1707. (choice :value org-agenda-files
  1708. (const :tag "All agenda files" org-agenda-files)
  1709. (const :tag "Current buffer" nil)
  1710. (function) (variable) (file))
  1711. (choice :tag "Identify target headline by"
  1712. (cons :tag "Specific tag" (const :value :tag) (string))
  1713. (cons :tag "TODO keyword" (const :value :todo) (string))
  1714. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1715. (cons :tag "Level number" (const :value :level) (integer))
  1716. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1717. (defcustom org-refile-target-verify-function nil
  1718. "Function to verify if the headline at point should be a refile target.
  1719. The function will be called without arguments, with point at the
  1720. beginning of the headline. It should return t and leave point
  1721. where it is if the headline is a valid target for refiling.
  1722. If the target should not be selected, the function must return nil.
  1723. In addition to this, it may move point to a place from where the search
  1724. should be continued. For example, the function may decide that the entire
  1725. subtree of the current entry should be excluded and move point to the end
  1726. of the subtree."
  1727. :group 'org-refile
  1728. :type 'function)
  1729. (defcustom org-refile-use-cache nil
  1730. "Non-nil means cache refile targets to speed up the process.
  1731. The cache for a particular file will be updated automatically when
  1732. the buffer has been killed, or when any of the marker used for flagging
  1733. refile targets no longer points at a live buffer.
  1734. If you have added new entries to a buffer that might themselves be targets,
  1735. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1736. find that easier, `C-u C-u C-u C-c C-w'."
  1737. :group 'org-refile
  1738. :type 'boolean)
  1739. (defcustom org-refile-use-outline-path nil
  1740. "Non-nil means provide refile targets as paths.
  1741. So a level 3 headline will be available as level1/level2/level3.
  1742. When the value is `file', also include the file name (without directory)
  1743. into the path. In this case, you can also stop the completion after
  1744. the file name, to get entries inserted as top level in the file.
  1745. When `full-file-path', include the full file path."
  1746. :group 'org-refile
  1747. :type '(choice
  1748. (const :tag "Not" nil)
  1749. (const :tag "Yes" t)
  1750. (const :tag "Start with file name" file)
  1751. (const :tag "Start with full file path" full-file-path)))
  1752. (defcustom org-outline-path-complete-in-steps t
  1753. "Non-nil means complete the outline path in hierarchical steps.
  1754. When Org-mode uses the refile interface to select an outline path
  1755. \(see variable `org-refile-use-outline-path'), the completion of
  1756. the path can be done is a single go, or if can be done in steps down
  1757. the headline hierarchy. Going in steps is probably the best if you
  1758. do not use a special completion package like `ido' or `icicles'.
  1759. However, when using these packages, going in one step can be very
  1760. fast, while still showing the whole path to the entry."
  1761. :group 'org-refile
  1762. :type 'boolean)
  1763. (defcustom org-refile-allow-creating-parent-nodes nil
  1764. "Non-nil means allow to create new nodes as refile targets.
  1765. New nodes are then created by adding \"/new node name\" to the completion
  1766. of an existing node. When the value of this variable is `confirm',
  1767. new node creation must be confirmed by the user (recommended)
  1768. When nil, the completion must match an existing entry.
  1769. Note that, if the new heading is not seen by the criteria
  1770. listed in `org-refile-targets', multiple instances of the same
  1771. heading would be created by trying again to file under the new
  1772. heading."
  1773. :group 'org-refile
  1774. :type '(choice
  1775. (const :tag "Never" nil)
  1776. (const :tag "Always" t)
  1777. (const :tag "Prompt for confirmation" confirm)))
  1778. (defcustom org-refile-active-region-within-subtree nil
  1779. "Non-nil means also refile active region within a subtree.
  1780. By default `org-refile' doesn't allow refiling regions if they
  1781. don't contain a set of subtrees, but it might be convenient to
  1782. do so sometimes: in that case, the first line of the region is
  1783. converted to a headline before refiling."
  1784. :group 'org-refile
  1785. :type 'boolean)
  1786. (defgroup org-todo nil
  1787. "Options concerning TODO items in Org-mode."
  1788. :tag "Org TODO"
  1789. :group 'org)
  1790. (defgroup org-progress nil
  1791. "Options concerning Progress logging in Org-mode."
  1792. :tag "Org Progress"
  1793. :group 'org-time)
  1794. (defvar org-todo-interpretation-widgets
  1795. '((:tag "Sequence (cycling hits every state)" sequence)
  1796. (:tag "Type (cycling directly to DONE)" type))
  1797. "The available interpretation symbols for customizing `org-todo-keywords'.
  1798. Interested libraries should add to this list.")
  1799. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1800. "List of TODO entry keyword sequences and their interpretation.
  1801. \\<org-mode-map>This is a list of sequences.
  1802. Each sequence starts with a symbol, either `sequence' or `type',
  1803. indicating if the keywords should be interpreted as a sequence of
  1804. action steps, or as different types of TODO items. The first
  1805. keywords are states requiring action - these states will select a headline
  1806. for inclusion into the global TODO list Org-mode produces. If one of
  1807. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1808. signify that no further action is necessary. If \"|\" is not found,
  1809. the last keyword is treated as the only DONE state of the sequence.
  1810. The command \\[org-todo] cycles an entry through these states, and one
  1811. additional state where no keyword is present. For details about this
  1812. cycling, see the manual.
  1813. TODO keywords and interpretation can also be set on a per-file basis with
  1814. the special #+SEQ_TODO and #+TYP_TODO lines.
  1815. Each keyword can optionally specify a character for fast state selection
  1816. \(in combination with the variable `org-use-fast-todo-selection')
  1817. and specifiers for state change logging, using the same syntax
  1818. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1819. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1820. indicates to record a time stamp each time this state is selected.
  1821. Each keyword may also specify if a timestamp or a note should be
  1822. recorded when entering or leaving the state, by adding additional
  1823. characters in the parenthesis after the keyword. This looks like this:
  1824. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1825. record only the time of the state change. With X and Y being either
  1826. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1827. Y when leaving the state if and only if the *target* state does not
  1828. define X. You may omit any of the fast-selection key or X or /Y,
  1829. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1830. For backward compatibility, this variable may also be just a list
  1831. of keywords - in this case the interpretation (sequence or type) will be
  1832. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1833. :group 'org-todo
  1834. :group 'org-keywords
  1835. :type '(choice
  1836. (repeat :tag "Old syntax, just keywords"
  1837. (string :tag "Keyword"))
  1838. (repeat :tag "New syntax"
  1839. (cons
  1840. (choice
  1841. :tag "Interpretation"
  1842. ;;Quick and dirty way to see
  1843. ;;`org-todo-interpretations'. This takes the
  1844. ;;place of item arguments
  1845. :convert-widget
  1846. (lambda (widget)
  1847. (widget-put widget
  1848. :args (mapcar
  1849. #'(lambda (x)
  1850. (widget-convert
  1851. (cons 'const x)))
  1852. org-todo-interpretation-widgets))
  1853. widget))
  1854. (repeat
  1855. (string :tag "Keyword"))))))
  1856. (defvar org-todo-keywords-1 nil
  1857. "All TODO and DONE keywords active in a buffer.")
  1858. (make-variable-buffer-local 'org-todo-keywords-1)
  1859. (defvar org-todo-keywords-for-agenda nil)
  1860. (defvar org-done-keywords-for-agenda nil)
  1861. (defvar org-drawers-for-agenda nil)
  1862. (defvar org-todo-keyword-alist-for-agenda nil)
  1863. (defvar org-tag-alist-for-agenda nil)
  1864. (defvar org-agenda-contributing-files nil)
  1865. (defvar org-not-done-keywords nil)
  1866. (make-variable-buffer-local 'org-not-done-keywords)
  1867. (defvar org-done-keywords nil)
  1868. (make-variable-buffer-local 'org-done-keywords)
  1869. (defvar org-todo-heads nil)
  1870. (make-variable-buffer-local 'org-todo-heads)
  1871. (defvar org-todo-sets nil)
  1872. (make-variable-buffer-local 'org-todo-sets)
  1873. (defvar org-todo-log-states nil)
  1874. (make-variable-buffer-local 'org-todo-log-states)
  1875. (defvar org-todo-kwd-alist nil)
  1876. (make-variable-buffer-local 'org-todo-kwd-alist)
  1877. (defvar org-todo-key-alist nil)
  1878. (make-variable-buffer-local 'org-todo-key-alist)
  1879. (defvar org-todo-key-trigger nil)
  1880. (make-variable-buffer-local 'org-todo-key-trigger)
  1881. (defcustom org-todo-interpretation 'sequence
  1882. "Controls how TODO keywords are interpreted.
  1883. This variable is in principle obsolete and is only used for
  1884. backward compatibility, if the interpretation of todo keywords is
  1885. not given already in `org-todo-keywords'. See that variable for
  1886. more information."
  1887. :group 'org-todo
  1888. :group 'org-keywords
  1889. :type '(choice (const sequence)
  1890. (const type)))
  1891. (defcustom org-use-fast-todo-selection t
  1892. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1893. This variable describes if and under what circumstances the cycling
  1894. mechanism for TODO keywords will be replaced by a single-key, direct
  1895. selection scheme.
  1896. When nil, fast selection is never used.
  1897. When the symbol `prefix', it will be used when `org-todo' is called with
  1898. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1899. in an agenda buffer.
  1900. When t, fast selection is used by default. In this case, the prefix
  1901. argument forces cycling instead.
  1902. In all cases, the special interface is only used if access keys have actually
  1903. been assigned by the user, i.e. if keywords in the configuration are followed
  1904. by a letter in parenthesis, like TODO(t)."
  1905. :group 'org-todo
  1906. :type '(choice
  1907. (const :tag "Never" nil)
  1908. (const :tag "By default" t)
  1909. (const :tag "Only with C-u C-c C-t" prefix)))
  1910. (defcustom org-provide-todo-statistics t
  1911. "Non-nil means update todo statistics after insert and toggle.
  1912. ALL-HEADLINES means update todo statistics by including headlines
  1913. with no TODO keyword as well, counting them as not done.
  1914. A list of TODO keywords means the same, but skip keywords that are
  1915. not in this list.
  1916. When this is set, todo statistics is updated in the parent of the
  1917. current entry each time a todo state is changed."
  1918. :group 'org-todo
  1919. :type '(choice
  1920. (const :tag "Yes, only for TODO entries" t)
  1921. (const :tag "Yes, including all entries" 'all-headlines)
  1922. (repeat :tag "Yes, for TODOs in this list"
  1923. (string :tag "TODO keyword"))
  1924. (other :tag "No TODO statistics" nil)))
  1925. (defcustom org-hierarchical-todo-statistics t
  1926. "Non-nil means TODO statistics covers just direct children.
  1927. When nil, all entries in the subtree are considered.
  1928. This has only an effect if `org-provide-todo-statistics' is set.
  1929. To set this to nil for only a single subtree, use a COOKIE_DATA
  1930. property and include the word \"recursive\" into the value."
  1931. :group 'org-todo
  1932. :type 'boolean)
  1933. (defcustom org-after-todo-state-change-hook nil
  1934. "Hook which is run after the state of a TODO item was changed.
  1935. The new state (a string with a TODO keyword, or nil) is available in the
  1936. Lisp variable `state'."
  1937. :group 'org-todo
  1938. :type 'hook)
  1939. (defvar org-blocker-hook nil
  1940. "Hook for functions that are allowed to block a state change.
  1941. Each function gets as its single argument a property list, see
  1942. `org-trigger-hook' for more information about this list.
  1943. If any of the functions in this hook returns nil, the state change
  1944. is blocked.")
  1945. (defvar org-trigger-hook nil
  1946. "Hook for functions that are triggered by a state change.
  1947. Each function gets as its single argument a property list with at least
  1948. the following elements:
  1949. (:type type-of-change :position pos-at-entry-start
  1950. :from old-state :to new-state)
  1951. Depending on the type, more properties may be present.
  1952. This mechanism is currently implemented for:
  1953. TODO state changes
  1954. ------------------
  1955. :type todo-state-change
  1956. :from previous state (keyword as a string), or nil, or a symbol
  1957. 'todo' or 'done', to indicate the general type of state.
  1958. :to new state, like in :from")
  1959. (defcustom org-enforce-todo-dependencies nil
  1960. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1961. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1962. be blocked if any prior sibling is not yet done.
  1963. Finally, if the parent is blocked because of ordered siblings of its own,
  1964. the child will also be blocked.
  1965. This variable needs to be set before org.el is loaded, and you need to
  1966. restart Emacs after a change to make the change effective. The only way
  1967. to change is while Emacs is running is through the customize interface."
  1968. :set (lambda (var val)
  1969. (set var val)
  1970. (if val
  1971. (add-hook 'org-blocker-hook
  1972. 'org-block-todo-from-children-or-siblings-or-parent)
  1973. (remove-hook 'org-blocker-hook
  1974. 'org-block-todo-from-children-or-siblings-or-parent)))
  1975. :group 'org-todo
  1976. :type 'boolean)
  1977. (defcustom org-enforce-todo-checkbox-dependencies nil
  1978. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1979. When this is nil, checkboxes have no influence on switching TODO states.
  1980. When non-nil, you first need to check off all check boxes before the TODO
  1981. entry can be switched to DONE.
  1982. This variable needs to be set before org.el is loaded, and you need to
  1983. restart Emacs after a change to make the change effective. The only way
  1984. to change is while Emacs is running is through the customize interface."
  1985. :set (lambda (var val)
  1986. (set var val)
  1987. (if val
  1988. (add-hook 'org-blocker-hook
  1989. 'org-block-todo-from-checkboxes)
  1990. (remove-hook 'org-blocker-hook
  1991. 'org-block-todo-from-checkboxes)))
  1992. :group 'org-todo
  1993. :type 'boolean)
  1994. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1995. "Non-nil means inserting a TODO heading is treated as state change.
  1996. So when the command \\[org-insert-todo-heading] is used, state change
  1997. logging will apply if appropriate. When nil, the new TODO item will
  1998. be inserted directly, and no logging will take place."
  1999. :group 'org-todo
  2000. :type 'boolean)
  2001. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2002. "Non-nil means switching TODO states with S-cursor counts as state change.
  2003. This is the default behavior. However, setting this to nil allows a
  2004. convenient way to select a TODO state and bypass any logging associated
  2005. with that."
  2006. :group 'org-todo
  2007. :type 'boolean)
  2008. (defcustom org-todo-state-tags-triggers nil
  2009. "Tag changes that should be triggered by TODO state changes.
  2010. This is a list. Each entry is
  2011. (state-change (tag . flag) .......)
  2012. State-change can be a string with a state, and empty string to indicate the
  2013. state that has no TODO keyword, or it can be one of the symbols `todo'
  2014. or `done', meaning any not-done or done state, respectively."
  2015. :group 'org-todo
  2016. :group 'org-tags
  2017. :type '(repeat
  2018. (cons (choice :tag "When changing to"
  2019. (const :tag "Not-done state" todo)
  2020. (const :tag "Done state" done)
  2021. (string :tag "State"))
  2022. (repeat
  2023. (cons :tag "Tag action"
  2024. (string :tag "Tag")
  2025. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2026. (defcustom org-log-done nil
  2027. "Information to record when a task moves to the DONE state.
  2028. Possible values are:
  2029. nil Don't add anything, just change the keyword
  2030. time Add a time stamp to the task
  2031. note Prompt for a note and add it with template `org-log-note-headings'
  2032. This option can also be set with on a per-file-basis with
  2033. #+STARTUP: nologdone
  2034. #+STARTUP: logdone
  2035. #+STARTUP: lognotedone
  2036. You can have local logging settings for a subtree by setting the LOGGING
  2037. property to one or more of these keywords."
  2038. :group 'org-todo
  2039. :group 'org-progress
  2040. :type '(choice
  2041. (const :tag "No logging" nil)
  2042. (const :tag "Record CLOSED timestamp" time)
  2043. (const :tag "Record CLOSED timestamp with note." note)))
  2044. ;; Normalize old uses of org-log-done.
  2045. (cond
  2046. ((eq org-log-done t) (setq org-log-done 'time))
  2047. ((and (listp org-log-done) (memq 'done org-log-done))
  2048. (setq org-log-done 'note)))
  2049. (defcustom org-log-reschedule nil
  2050. "Information to record when the scheduling date of a tasks is modified.
  2051. Possible values are:
  2052. nil Don't add anything, just change the date
  2053. time Add a time stamp to the task
  2054. note Prompt for a note and add it with template `org-log-note-headings'
  2055. This option can also be set with on a per-file-basis with
  2056. #+STARTUP: nologreschedule
  2057. #+STARTUP: logreschedule
  2058. #+STARTUP: lognotereschedule"
  2059. :group 'org-todo
  2060. :group 'org-progress
  2061. :type '(choice
  2062. (const :tag "No logging" nil)
  2063. (const :tag "Record timestamp" time)
  2064. (const :tag "Record timestamp with note." note)))
  2065. (defcustom org-log-redeadline nil
  2066. "Information to record when the deadline date of a tasks is modified.
  2067. Possible values are:
  2068. nil Don't add anything, just change the date
  2069. time Add a time stamp to the task
  2070. note Prompt for a note and add it with template `org-log-note-headings'
  2071. This option can also be set with on a per-file-basis with
  2072. #+STARTUP: nologredeadline
  2073. #+STARTUP: logredeadline
  2074. #+STARTUP: lognoteredeadline
  2075. You can have local logging settings for a subtree by setting the LOGGING
  2076. property to one or more of these keywords."
  2077. :group 'org-todo
  2078. :group 'org-progress
  2079. :type '(choice
  2080. (const :tag "No logging" nil)
  2081. (const :tag "Record timestamp" time)
  2082. (const :tag "Record timestamp with note." note)))
  2083. (defcustom org-log-note-clock-out nil
  2084. "Non-nil means record a note when clocking out of an item.
  2085. This can also be configured on a per-file basis by adding one of
  2086. the following lines anywhere in the buffer:
  2087. #+STARTUP: lognoteclock-out
  2088. #+STARTUP: nolognoteclock-out"
  2089. :group 'org-todo
  2090. :group 'org-progress
  2091. :type 'boolean)
  2092. (defcustom org-log-done-with-time t
  2093. "Non-nil means the CLOSED time stamp will contain date and time.
  2094. When nil, only the date will be recorded."
  2095. :group 'org-progress
  2096. :type 'boolean)
  2097. (defcustom org-log-note-headings
  2098. '((done . "CLOSING NOTE %t")
  2099. (state . "State %-12s from %-12S %t")
  2100. (note . "Note taken on %t")
  2101. (reschedule . "Rescheduled from %S on %t")
  2102. (delschedule . "Not scheduled, was %S on %t")
  2103. (redeadline . "New deadline from %S on %t")
  2104. (deldeadline . "Removed deadline, was %S on %t")
  2105. (refile . "Refiled on %t")
  2106. (clock-out . ""))
  2107. "Headings for notes added to entries.
  2108. The value is an alist, with the car being a symbol indicating the note
  2109. context, and the cdr is the heading to be used. The heading may also be the
  2110. empty string.
  2111. %t in the heading will be replaced by a time stamp.
  2112. %T will be an active time stamp instead the default inactive one
  2113. %d will be replaced by a short-format time stamp.
  2114. %D will be replaced by an active short-format time stamp.
  2115. %s will be replaced by the new TODO state, in double quotes.
  2116. %S will be replaced by the old TODO state, in double quotes.
  2117. %u will be replaced by the user name.
  2118. %U will be replaced by the full user name.
  2119. In fact, it is not a good idea to change the `state' entry, because
  2120. agenda log mode depends on the format of these entries."
  2121. :group 'org-todo
  2122. :group 'org-progress
  2123. :type '(list :greedy t
  2124. (cons (const :tag "Heading when closing an item" done) string)
  2125. (cons (const :tag
  2126. "Heading when changing todo state (todo sequence only)"
  2127. state) string)
  2128. (cons (const :tag "Heading when just taking a note" note) string)
  2129. (cons (const :tag "Heading when clocking out" clock-out) string)
  2130. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2131. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2132. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2133. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2134. (cons (const :tag "Heading when refiling" refile) string)))
  2135. (unless (assq 'note org-log-note-headings)
  2136. (push '(note . "%t") org-log-note-headings))
  2137. (defcustom org-log-into-drawer nil
  2138. "Non-nil means insert state change notes and time stamps into a drawer.
  2139. When nil, state changes notes will be inserted after the headline and
  2140. any scheduling and clock lines, but not inside a drawer.
  2141. The value of this variable should be the name of the drawer to use.
  2142. LOGBOOK is proposed as the default drawer for this purpose, you can
  2143. also set this to a string to define the drawer of your choice.
  2144. A value of t is also allowed, representing \"LOGBOOK\".
  2145. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2146. will be ignored.
  2147. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2148. a subtree."
  2149. :group 'org-todo
  2150. :group 'org-progress
  2151. :type '(choice
  2152. (const :tag "Not into a drawer" nil)
  2153. (const :tag "LOGBOOK" t)
  2154. (string :tag "Other")))
  2155. (if (fboundp 'defvaralias)
  2156. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2157. (defun org-log-into-drawer ()
  2158. "Return the value of `org-log-into-drawer', but let properties overrule.
  2159. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2160. used instead of the default value."
  2161. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2162. (cond
  2163. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2164. ((equal p "t") "LOGBOOK")
  2165. (t p))))
  2166. (defcustom org-log-state-notes-insert-after-drawers nil
  2167. "Non-nil means insert state change notes after any drawers in entry.
  2168. Only the drawers that *immediately* follow the headline and the
  2169. deadline/scheduled line are skipped.
  2170. When nil, insert notes right after the heading and perhaps the line
  2171. with deadline/scheduling if present.
  2172. This variable will have no effect if `org-log-into-drawer' is
  2173. set."
  2174. :group 'org-todo
  2175. :group 'org-progress
  2176. :type 'boolean)
  2177. (defcustom org-log-states-order-reversed t
  2178. "Non-nil means the latest state note will be directly after heading.
  2179. When nil, the state change notes will be ordered according to time."
  2180. :group 'org-todo
  2181. :group 'org-progress
  2182. :type 'boolean)
  2183. (defcustom org-todo-repeat-to-state nil
  2184. "The TODO state to which a repeater should return the repeating task.
  2185. By default this is the first task in a TODO sequence, or the previous state
  2186. in a TODO_TYP set. But you can specify another task here.
  2187. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2188. :group 'org-todo
  2189. :type '(choice (const :tag "Head of sequence" nil)
  2190. (string :tag "Specific state")))
  2191. (defcustom org-log-repeat 'time
  2192. "Non-nil means record moving through the DONE state when triggering repeat.
  2193. An auto-repeating task is immediately switched back to TODO when
  2194. marked DONE. If you are not logging state changes (by adding \"@\"
  2195. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2196. record a closing note, there will be no record of the task moving
  2197. through DONE. This variable forces taking a note anyway.
  2198. nil Don't force a record
  2199. time Record a time stamp
  2200. note Record a note
  2201. This option can also be set with on a per-file-basis with
  2202. #+STARTUP: logrepeat
  2203. #+STARTUP: lognoterepeat
  2204. #+STARTUP: nologrepeat
  2205. You can have local logging settings for a subtree by setting the LOGGING
  2206. property to one or more of these keywords."
  2207. :group 'org-todo
  2208. :group 'org-progress
  2209. :type '(choice
  2210. (const :tag "Don't force a record" nil)
  2211. (const :tag "Force recording the DONE state" time)
  2212. (const :tag "Force recording a note with the DONE state" note)))
  2213. (defgroup org-priorities nil
  2214. "Priorities in Org-mode."
  2215. :tag "Org Priorities"
  2216. :group 'org-todo)
  2217. (defcustom org-enable-priority-commands t
  2218. "Non-nil means priority commands are active.
  2219. When nil, these commands will be disabled, so that you never accidentally
  2220. set a priority."
  2221. :group 'org-priorities
  2222. :type 'boolean)
  2223. (defcustom org-highest-priority ?A
  2224. "The highest priority of TODO items. A character like ?A, ?B etc.
  2225. Must have a smaller ASCII number than `org-lowest-priority'."
  2226. :group 'org-priorities
  2227. :type 'character)
  2228. (defcustom org-lowest-priority ?C
  2229. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2230. Must have a larger ASCII number than `org-highest-priority'."
  2231. :group 'org-priorities
  2232. :type 'character)
  2233. (defcustom org-default-priority ?B
  2234. "The default priority of TODO items.
  2235. This is the priority an item gets if no explicit priority is given.
  2236. When starting to cycle on an empty priority the first step in the cycle
  2237. depends on `org-priority-start-cycle-with-default'. The resulting first
  2238. step priority must not exceed the range from `org-highest-priority' to
  2239. `org-lowest-priority' which means that `org-default-priority' has to be
  2240. in this range exclusive or inclusive the range boundaries. Else the
  2241. first step refuses to set the default and the second will fall back
  2242. to (depending on the command used) the highest or lowest priority."
  2243. :group 'org-priorities
  2244. :type 'character)
  2245. (defcustom org-priority-start-cycle-with-default t
  2246. "Non-nil means start with default priority when starting to cycle.
  2247. When this is nil, the first step in the cycle will be (depending on the
  2248. command used) one higher or lower than the default priority.
  2249. See also `org-default-priority'."
  2250. :group 'org-priorities
  2251. :type 'boolean)
  2252. (defcustom org-get-priority-function nil
  2253. "Function to extract the priority from a string.
  2254. The string is normally the headline. If this is nil Org computes the
  2255. priority from the priority cookie like [#A] in the headline. It returns
  2256. an integer, increasing by 1000 for each priority level.
  2257. The user can set a different function here, which should take a string
  2258. as an argument and return the numeric priority."
  2259. :group 'org-priorities
  2260. :type 'function)
  2261. (defgroup org-time nil
  2262. "Options concerning time stamps and deadlines in Org-mode."
  2263. :tag "Org Time"
  2264. :group 'org)
  2265. (defcustom org-insert-labeled-timestamps-at-point nil
  2266. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2267. When nil, these labeled time stamps are forces into the second line of an
  2268. entry, just after the headline. When scheduling from the global TODO list,
  2269. the time stamp will always be forced into the second line."
  2270. :group 'org-time
  2271. :type 'boolean)
  2272. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2273. "Formats for `format-time-string' which are used for time stamps.
  2274. It is not recommended to change this constant.")
  2275. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2276. "Number of minutes to round time stamps to.
  2277. These are two values, the first applies when first creating a time stamp.
  2278. The second applies when changing it with the commands `S-up' and `S-down'.
  2279. When changing the time stamp, this means that it will change in steps
  2280. of N minutes, as given by the second value.
  2281. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2282. numbers should be factors of 60, so for example 5, 10, 15.
  2283. When this is larger than 1, you can still force an exact time stamp by using
  2284. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2285. and by using a prefix arg to `S-up/down' to specify the exact number
  2286. of minutes to shift."
  2287. :group 'org-time
  2288. :get #'(lambda (var) ; Make sure both elements are there
  2289. (if (integerp (default-value var))
  2290. (list (default-value var) 5)
  2291. (default-value var)))
  2292. :type '(list
  2293. (integer :tag "when inserting times")
  2294. (integer :tag "when modifying times")))
  2295. ;; Normalize old customizations of this variable.
  2296. (when (integerp org-time-stamp-rounding-minutes)
  2297. (setq org-time-stamp-rounding-minutes
  2298. (list org-time-stamp-rounding-minutes
  2299. org-time-stamp-rounding-minutes)))
  2300. (defcustom org-display-custom-times nil
  2301. "Non-nil means overlay custom formats over all time stamps.
  2302. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2303. To turn this on on a per-file basis, insert anywhere in the file:
  2304. #+STARTUP: customtime"
  2305. :group 'org-time
  2306. :set 'set-default
  2307. :type 'sexp)
  2308. (make-variable-buffer-local 'org-display-custom-times)
  2309. (defcustom org-time-stamp-custom-formats
  2310. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2311. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2312. These are overlaid over the default ISO format if the variable
  2313. `org-display-custom-times' is set. Time like %H:%M should be at the
  2314. end of the second format. The custom formats are also honored by export
  2315. commands, if custom time display is turned on at the time of export."
  2316. :group 'org-time
  2317. :type 'sexp)
  2318. (defun org-time-stamp-format (&optional long inactive)
  2319. "Get the right format for a time string."
  2320. (let ((f (if long (cdr org-time-stamp-formats)
  2321. (car org-time-stamp-formats))))
  2322. (if inactive
  2323. (concat "[" (substring f 1 -1) "]")
  2324. f)))
  2325. (defcustom org-time-clocksum-format "%d:%02d"
  2326. "The format string used when creating CLOCKSUM lines.
  2327. This is also used when org-mode generates a time duration."
  2328. :group 'org-time
  2329. :type 'string)
  2330. (defcustom org-time-clocksum-use-fractional nil
  2331. "If non-nil, \\[org-clock-display] uses fractional times.
  2332. org-mode generates a time duration."
  2333. :group 'org-time
  2334. :type 'boolean)
  2335. (defcustom org-time-clocksum-fractional-format "%.2f"
  2336. "The format string used when creating CLOCKSUM lines, or when
  2337. org-mode generates a time duration."
  2338. :group 'org-time
  2339. :type 'string)
  2340. (defcustom org-deadline-warning-days 14
  2341. "No. of days before expiration during which a deadline becomes active.
  2342. This variable governs the display in sparse trees and in the agenda.
  2343. When 0 or negative, it means use this number (the absolute value of it)
  2344. even if a deadline has a different individual lead time specified.
  2345. Custom commands can set this variable in the options section."
  2346. :group 'org-time
  2347. :group 'org-agenda-daily/weekly
  2348. :type 'integer)
  2349. (defcustom org-read-date-prefer-future t
  2350. "Non-nil means assume future for incomplete date input from user.
  2351. This affects the following situations:
  2352. 1. The user gives a month but not a year.
  2353. For example, if it is April and you enter \"feb 2\", this will be read
  2354. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2355. 2. The user gives a day, but no month.
  2356. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2357. read this as the third of *next* month. However, if you enter \"17\",
  2358. it will be considered as *this* month.
  2359. If you set this variable to the symbol `time', then also the following
  2360. will work:
  2361. 3. If the user gives a time, but no day. If the time is before now,
  2362. to will be interpreted as tomorrow.
  2363. Currently none of this works for ISO week specifications.
  2364. When this option is nil, the current day, month and year will always be
  2365. used as defaults.
  2366. See also `org-agenda-jump-prefer-future'."
  2367. :group 'org-time
  2368. :type '(choice
  2369. (const :tag "Never" nil)
  2370. (const :tag "Check month and day" t)
  2371. (const :tag "Check month, day, and time" time)))
  2372. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2373. "Should the agenda jump command prefer the future for incomplete dates?
  2374. The default is to do the same as configured in `org-read-date-prefer-future'.
  2375. But you can also set a deviating value here.
  2376. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2377. :group 'org-agenda
  2378. :group 'org-time
  2379. :type '(choice
  2380. (const :tag "Use org-read-date-prefer-future"
  2381. org-read-date-prefer-future)
  2382. (const :tag "Never" nil)
  2383. (const :tag "Always" t)))
  2384. (defcustom org-read-date-force-compatible-dates t
  2385. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2386. Depending on the system Emacs is running on, certain dates cannot
  2387. be represented with the type used internally to represent time.
  2388. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2389. correctly. Some systems allow for earlier dates, some for later,
  2390. some for both. One way to find out it to insert any date into an
  2391. Org buffer, putting the cursor on the year and hitting S-up and
  2392. S-down to test the range.
  2393. When this variable is set to t, the date/time prompt will not let
  2394. you specify dates outside the 1970-2037 range, so it is certain that
  2395. these dates will work in whatever version of Emacs you are
  2396. running, and also that you can move a file from one Emacs implementation
  2397. to another. WHenever Org is forcing the year for you, it will display
  2398. a message and beep.
  2399. When this variable is nil, Org will check if the date is
  2400. representable in the specific Emacs implementation you are using.
  2401. If not, it will force a year, usually the current year, and beep
  2402. to remind you. Currently this setting is not recommended because
  2403. the likelihood that you will open your Org files in an Emacs that
  2404. has limited date range is not negligible.
  2405. A workaround for this problem is to use diary sexp dates for time
  2406. stamps outside of this range."
  2407. :group 'org-time
  2408. :type 'boolean)
  2409. (defcustom org-read-date-display-live t
  2410. "Non-nil means display current interpretation of date prompt live.
  2411. This display will be in an overlay, in the minibuffer."
  2412. :group 'org-time
  2413. :type 'boolean)
  2414. (defcustom org-read-date-popup-calendar t
  2415. "Non-nil means pop up a calendar when prompting for a date.
  2416. In the calendar, the date can be selected with mouse-1. However, the
  2417. minibuffer will also be active, and you can simply enter the date as well.
  2418. When nil, only the minibuffer will be available."
  2419. :group 'org-time
  2420. :type 'boolean)
  2421. (if (fboundp 'defvaralias)
  2422. (defvaralias 'org-popup-calendar-for-date-prompt
  2423. 'org-read-date-popup-calendar))
  2424. (defcustom org-read-date-minibuffer-setup-hook nil
  2425. "Hook to be used to set up keys for the date/time interface.
  2426. Add key definitions to `minibuffer-local-map', which will be a temporary
  2427. copy."
  2428. :group 'org-time
  2429. :type 'hook)
  2430. (defcustom org-extend-today-until 0
  2431. "The hour when your day really ends. Must be an integer.
  2432. This has influence for the following applications:
  2433. - When switching the agenda to \"today\". It it is still earlier than
  2434. the time given here, the day recognized as TODAY is actually yesterday.
  2435. - When a date is read from the user and it is still before the time given
  2436. here, the current date and time will be assumed to be yesterday, 23:59.
  2437. Also, timestamps inserted in remember templates follow this rule.
  2438. IMPORTANT: This is a feature whose implementation is and likely will
  2439. remain incomplete. Really, it is only here because past midnight seems to
  2440. be the favorite working time of John Wiegley :-)"
  2441. :group 'org-time
  2442. :type 'integer)
  2443. (defcustom org-use-effective-time nil
  2444. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2445. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2446. \"effective time\" of any timestamps between midnight and 8am will be
  2447. 23:59 of the previous day."
  2448. :group 'boolean
  2449. :type 'integer)
  2450. (defcustom org-edit-timestamp-down-means-later nil
  2451. "Non-nil means S-down will increase the time in a time stamp.
  2452. When nil, S-up will increase."
  2453. :group 'org-time
  2454. :type 'boolean)
  2455. (defcustom org-calendar-follow-timestamp-change t
  2456. "Non-nil means make the calendar window follow timestamp changes.
  2457. When a timestamp is modified and the calendar window is visible, it will be
  2458. moved to the new date."
  2459. :group 'org-time
  2460. :type 'boolean)
  2461. (defgroup org-tags nil
  2462. "Options concerning tags in Org-mode."
  2463. :tag "Org Tags"
  2464. :group 'org)
  2465. (defcustom org-tag-alist nil
  2466. "List of tags allowed in Org-mode files.
  2467. When this list is nil, Org-mode will base TAG input on what is already in the
  2468. buffer.
  2469. The value of this variable is an alist, the car of each entry must be a
  2470. keyword as a string, the cdr may be a character that is used to select
  2471. that tag through the fast-tag-selection interface.
  2472. See the manual for details."
  2473. :group 'org-tags
  2474. :type '(repeat
  2475. (choice
  2476. (cons (string :tag "Tag name")
  2477. (character :tag "Access char"))
  2478. (list :tag "Start radio group"
  2479. (const :startgroup)
  2480. (option (string :tag "Group description")))
  2481. (list :tag "End radio group"
  2482. (const :endgroup)
  2483. (option (string :tag "Group description")))
  2484. (const :tag "New line" (:newline)))))
  2485. (defcustom org-tag-persistent-alist nil
  2486. "List of tags that will always appear in all Org-mode files.
  2487. This is in addition to any in buffer settings or customizations
  2488. of `org-tag-alist'.
  2489. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2490. The value of this variable is an alist, the car of each entry must be a
  2491. keyword as a string, the cdr may be a character that is used to select
  2492. that tag through the fast-tag-selection interface.
  2493. See the manual for details.
  2494. To disable these tags on a per-file basis, insert anywhere in the file:
  2495. #+STARTUP: noptag"
  2496. :group 'org-tags
  2497. :type '(repeat
  2498. (choice
  2499. (cons (string :tag "Tag name")
  2500. (character :tag "Access char"))
  2501. (const :tag "Start radio group" (:startgroup))
  2502. (const :tag "End radio group" (:endgroup))
  2503. (const :tag "New line" (:newline)))))
  2504. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2505. "If non-nil, always offer completion for all tags of all agenda files.
  2506. Instead of customizing this variable directly, you might want to
  2507. set it locally for capture buffers, because there no list of
  2508. tags in that file can be created dynamically (there are none).
  2509. (add-hook 'org-capture-mode-hook
  2510. (lambda ()
  2511. (set (make-local-variable
  2512. 'org-complete-tags-always-offer-all-agenda-tags)
  2513. t)))"
  2514. :group 'org-tags
  2515. :type 'boolean)
  2516. (defvar org-file-tags nil
  2517. "List of tags that can be inherited by all entries in the file.
  2518. The tags will be inherited if the variable `org-use-tag-inheritance'
  2519. says they should be.
  2520. This variable is populated from #+FILETAGS lines.")
  2521. (defcustom org-use-fast-tag-selection 'auto
  2522. "Non-nil means use fast tag selection scheme.
  2523. This is a special interface to select and deselect tags with single keys.
  2524. When nil, fast selection is never used.
  2525. When the symbol `auto', fast selection is used if and only if selection
  2526. characters for tags have been configured, either through the variable
  2527. `org-tag-alist' or through a #+TAGS line in the buffer.
  2528. When t, fast selection is always used and selection keys are assigned
  2529. automatically if necessary."
  2530. :group 'org-tags
  2531. :type '(choice
  2532. (const :tag "Always" t)
  2533. (const :tag "Never" nil)
  2534. (const :tag "When selection characters are configured" 'auto)))
  2535. (defcustom org-fast-tag-selection-single-key nil
  2536. "Non-nil means fast tag selection exits after first change.
  2537. When nil, you have to press RET to exit it.
  2538. During fast tag selection, you can toggle this flag with `C-c'.
  2539. This variable can also have the value `expert'. In this case, the window
  2540. displaying the tags menu is not even shown, until you press C-c again."
  2541. :group 'org-tags
  2542. :type '(choice
  2543. (const :tag "No" nil)
  2544. (const :tag "Yes" t)
  2545. (const :tag "Expert" expert)))
  2546. (defvar org-fast-tag-selection-include-todo nil
  2547. "Non-nil means fast tags selection interface will also offer TODO states.
  2548. This is an undocumented feature, you should not rely on it.")
  2549. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2550. "The column to which tags should be indented in a headline.
  2551. If this number is positive, it specifies the column. If it is negative,
  2552. it means that the tags should be flushright to that column. For example,
  2553. -80 works well for a normal 80 character screen.
  2554. When 0, place tags directly after headline text, with only one space in
  2555. between."
  2556. :group 'org-tags
  2557. :type 'integer)
  2558. (defcustom org-auto-align-tags t
  2559. "Non-nil keeps tags aligned when modifying headlines.
  2560. Some operations (i.e. demoting) change the length of a headline and
  2561. therefore shift the tags around. With this option turned on, after
  2562. each such operation the tags are again aligned to `org-tags-column'."
  2563. :group 'org-tags
  2564. :type 'boolean)
  2565. (defcustom org-use-tag-inheritance t
  2566. "Non-nil means tags in levels apply also for sublevels.
  2567. When nil, only the tags directly given in a specific line apply there.
  2568. This may also be a list of tags that should be inherited, or a regexp that
  2569. matches tags that should be inherited. Additional control is possible
  2570. with the variable `org-tags-exclude-from-inheritance' which gives an
  2571. explicit list of tags to be excluded from inheritance., even if the value of
  2572. `org-use-tag-inheritance' would select it for inheritance.
  2573. If this option is t, a match early-on in a tree can lead to a large
  2574. number of matches in the subtree when constructing the agenda or creating
  2575. a sparse tree. If you only want to see the first match in a tree during
  2576. a search, check out the variable `org-tags-match-list-sublevels'."
  2577. :group 'org-tags
  2578. :type '(choice
  2579. (const :tag "Not" nil)
  2580. (const :tag "Always" t)
  2581. (repeat :tag "Specific tags" (string :tag "Tag"))
  2582. (regexp :tag "Tags matched by regexp")))
  2583. (defcustom org-tags-exclude-from-inheritance nil
  2584. "List of tags that should never be inherited.
  2585. This is a way to exclude a few tags from inheritance. For way to do
  2586. the opposite, to actively allow inheritance for selected tags,
  2587. see the variable `org-use-tag-inheritance'."
  2588. :group 'org-tags
  2589. :type '(repeat (string :tag "Tag")))
  2590. (defun org-tag-inherit-p (tag)
  2591. "Check if TAG is one that should be inherited."
  2592. (cond
  2593. ((member tag org-tags-exclude-from-inheritance) nil)
  2594. ((eq org-use-tag-inheritance t) t)
  2595. ((not org-use-tag-inheritance) nil)
  2596. ((stringp org-use-tag-inheritance)
  2597. (string-match org-use-tag-inheritance tag))
  2598. ((listp org-use-tag-inheritance)
  2599. (member tag org-use-tag-inheritance))
  2600. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2601. (defcustom org-tags-match-list-sublevels t
  2602. "Non-nil means list also sublevels of headlines matching a search.
  2603. This variable applies to tags/property searches, and also to stuck
  2604. projects because this search is based on a tags match as well.
  2605. When set to the symbol `indented', sublevels are indented with
  2606. leading dots.
  2607. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2608. the sublevels of a headline matching a tag search often also match
  2609. the same search. Listing all of them can create very long lists.
  2610. Setting this variable to nil causes subtrees of a match to be skipped.
  2611. This variable is semi-obsolete and probably should always be true. It
  2612. is better to limit inheritance to certain tags using the variables
  2613. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2614. :group 'org-tags
  2615. :type '(choice
  2616. (const :tag "No, don't list them" nil)
  2617. (const :tag "Yes, do list them" t)
  2618. (const :tag "List them, indented with leading dots" indented)))
  2619. (defcustom org-tags-sort-function nil
  2620. "When set, tags are sorted using this function as a comparator."
  2621. :group 'org-tags
  2622. :type '(choice
  2623. (const :tag "No sorting" nil)
  2624. (const :tag "Alphabetical" string<)
  2625. (const :tag "Reverse alphabetical" string>)
  2626. (function :tag "Custom function" nil)))
  2627. (defvar org-tags-history nil
  2628. "History of minibuffer reads for tags.")
  2629. (defvar org-last-tags-completion-table nil
  2630. "The last used completion table for tags.")
  2631. (defvar org-after-tags-change-hook nil
  2632. "Hook that is run after the tags in a line have changed.")
  2633. (defgroup org-properties nil
  2634. "Options concerning properties in Org-mode."
  2635. :tag "Org Properties"
  2636. :group 'org)
  2637. (defcustom org-property-format "%-10s %s"
  2638. "How property key/value pairs should be formatted by `indent-line'.
  2639. When `indent-line' hits a property definition, it will format the line
  2640. according to this format, mainly to make sure that the values are
  2641. lined-up with respect to each other."
  2642. :group 'org-properties
  2643. :type 'string)
  2644. (defcustom org-properties-postprocess-alist nil
  2645. "Alist of properties and functions to adjust inserted values.
  2646. Elements of this alist must be of the form
  2647. ([string] [function])
  2648. where [string] must be a property name and [function] must be a
  2649. lambda expression: this lambda expression must take one argument,
  2650. the value to adjust, and return the new value as a string.
  2651. For example, this element will allow the property \"Remaining\"
  2652. to be updated wrt the relation between the \"Effort\" property
  2653. and the clock summary:
  2654. ((\"Remaining\" (lambda(value)
  2655. (let ((clocksum (org-clock-sum-current-item))
  2656. (effort (org-duration-string-to-minutes
  2657. (org-entry-get (point) \"Effort\"))))
  2658. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2659. :group 'org-properties
  2660. :type 'alist)
  2661. (defcustom org-use-property-inheritance nil
  2662. "Non-nil means properties apply also for sublevels.
  2663. This setting is chiefly used during property searches. Turning it on can
  2664. cause significant overhead when doing a search, which is why it is not
  2665. on by default.
  2666. When nil, only the properties directly given in the current entry count.
  2667. When t, every property is inherited. The value may also be a list of
  2668. properties that should have inheritance, or a regular expression matching
  2669. properties that should be inherited.
  2670. However, note that some special properties use inheritance under special
  2671. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2672. and the properties ending in \"_ALL\" when they are used as descriptor
  2673. for valid values of a property.
  2674. Note for programmers:
  2675. When querying an entry with `org-entry-get', you can control if inheritance
  2676. should be used. By default, `org-entry-get' looks only at the local
  2677. properties. You can request inheritance by setting the inherit argument
  2678. to t (to force inheritance) or to `selective' (to respect the setting
  2679. in this variable)."
  2680. :group 'org-properties
  2681. :type '(choice
  2682. (const :tag "Not" nil)
  2683. (const :tag "Always" t)
  2684. (repeat :tag "Specific properties" (string :tag "Property"))
  2685. (regexp :tag "Properties matched by regexp")))
  2686. (defun org-property-inherit-p (property)
  2687. "Check if PROPERTY is one that should be inherited."
  2688. (cond
  2689. ((eq org-use-property-inheritance t) t)
  2690. ((not org-use-property-inheritance) nil)
  2691. ((stringp org-use-property-inheritance)
  2692. (string-match org-use-property-inheritance property))
  2693. ((listp org-use-property-inheritance)
  2694. (member property org-use-property-inheritance))
  2695. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2696. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2697. "The default column format, if no other format has been defined.
  2698. This variable can be set on the per-file basis by inserting a line
  2699. #+COLUMNS: %25ITEM ....."
  2700. :group 'org-properties
  2701. :type 'string)
  2702. (defcustom org-columns-ellipses ".."
  2703. "The ellipses to be used when a field in column view is truncated.
  2704. When this is the empty string, as many characters as possible are shown,
  2705. but then there will be no visual indication that the field has been truncated.
  2706. When this is a string of length N, the last N characters of a truncated
  2707. field are replaced by this string. If the column is narrower than the
  2708. ellipses string, only part of the ellipses string will be shown."
  2709. :group 'org-properties
  2710. :type 'string)
  2711. (defcustom org-columns-modify-value-for-display-function nil
  2712. "Function that modifies values for display in column view.
  2713. For example, it can be used to cut out a certain part from a time stamp.
  2714. The function must take 2 arguments:
  2715. column-title The title of the column (*not* the property name)
  2716. value The value that should be modified.
  2717. The function should return the value that should be displayed,
  2718. or nil if the normal value should be used."
  2719. :group 'org-properties
  2720. :type 'function)
  2721. (defcustom org-effort-property "Effort"
  2722. "The property that is being used to keep track of effort estimates.
  2723. Effort estimates given in this property need to have the format H:MM."
  2724. :group 'org-properties
  2725. :group 'org-progress
  2726. :type '(string :tag "Property"))
  2727. (defconst org-global-properties-fixed
  2728. '(("VISIBILITY_ALL" . "folded children content all")
  2729. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2730. "List of property/value pairs that can be inherited by any entry.
  2731. These are fixed values, for the preset properties. The user variable
  2732. that can be used to add to this list is `org-global-properties'.
  2733. The entries in this list are cons cells where the car is a property
  2734. name and cdr is a string with the value. If the value represents
  2735. multiple items like an \"_ALL\" property, separate the items by
  2736. spaces.")
  2737. (defcustom org-global-properties nil
  2738. "List of property/value pairs that can be inherited by any entry.
  2739. This list will be combined with the constant `org-global-properties-fixed'.
  2740. The entries in this list are cons cells where the car is a property
  2741. name and cdr is a string with the value.
  2742. You can set buffer-local values for the same purpose in the variable
  2743. `org-file-properties' this by adding lines like
  2744. #+PROPERTY: NAME VALUE"
  2745. :group 'org-properties
  2746. :type '(repeat
  2747. (cons (string :tag "Property")
  2748. (string :tag "Value"))))
  2749. (defvar org-file-properties nil
  2750. "List of property/value pairs that can be inherited by any entry.
  2751. Valid for the current buffer.
  2752. This variable is populated from #+PROPERTY lines.")
  2753. (make-variable-buffer-local 'org-file-properties)
  2754. (defgroup org-agenda nil
  2755. "Options concerning agenda views in Org-mode."
  2756. :tag "Org Agenda"
  2757. :group 'org)
  2758. (defvar org-category nil
  2759. "Variable used by org files to set a category for agenda display.
  2760. Such files should use a file variable to set it, for example
  2761. # -*- mode: org; org-category: \"ELisp\"
  2762. or contain a special line
  2763. #+CATEGORY: ELisp
  2764. If the file does not specify a category, then file's base name
  2765. is used instead.")
  2766. (make-variable-buffer-local 'org-category)
  2767. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2768. (defcustom org-agenda-files nil
  2769. "The files to be used for agenda display.
  2770. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2771. \\[org-remove-file]. You can also use customize to edit the list.
  2772. If an entry is a directory, all files in that directory that are matched by
  2773. `org-agenda-file-regexp' will be part of the file list.
  2774. If the value of the variable is not a list but a single file name, then
  2775. the list of agenda files is actually stored and maintained in that file, one
  2776. agenda file per line. In this file paths can be given relative to
  2777. `org-directory'. Tilde expansion and environment variable substitution
  2778. are also made."
  2779. :group 'org-agenda
  2780. :type '(choice
  2781. (repeat :tag "List of files and directories" file)
  2782. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2783. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2784. "Regular expression to match files for `org-agenda-files'.
  2785. If any element in the list in that variable contains a directory instead
  2786. of a normal file, all files in that directory that are matched by this
  2787. regular expression will be included."
  2788. :group 'org-agenda
  2789. :type 'regexp)
  2790. (defcustom org-agenda-text-search-extra-files nil
  2791. "List of extra files to be searched by text search commands.
  2792. These files will be search in addition to the agenda files by the
  2793. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2794. Note that these files will only be searched for text search commands,
  2795. not for the other agenda views like todo lists, tag searches or the weekly
  2796. agenda. This variable is intended to list notes and possibly archive files
  2797. that should also be searched by these two commands.
  2798. In fact, if the first element in the list is the symbol `agenda-archives',
  2799. than all archive files of all agenda files will be added to the search
  2800. scope."
  2801. :group 'org-agenda
  2802. :type '(set :greedy t
  2803. (const :tag "Agenda Archives" agenda-archives)
  2804. (repeat :inline t (file))))
  2805. (if (fboundp 'defvaralias)
  2806. (defvaralias 'org-agenda-multi-occur-extra-files
  2807. 'org-agenda-text-search-extra-files))
  2808. (defcustom org-agenda-skip-unavailable-files nil
  2809. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2810. A nil value means to remove them, after a query, from the list."
  2811. :group 'org-agenda
  2812. :type 'boolean)
  2813. (defcustom org-calendar-to-agenda-key [?c]
  2814. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2815. The command `org-calendar-goto-agenda' will be bound to this key. The
  2816. default is the character `c' because then `c' can be used to switch back and
  2817. forth between agenda and calendar."
  2818. :group 'org-agenda
  2819. :type 'sexp)
  2820. (defcustom org-calendar-agenda-action-key [?k]
  2821. "The key to be installed in `calendar-mode-map' for agenda-action.
  2822. The command `org-agenda-action' will be bound to this key. The
  2823. default is the character `k' because we use the same key in the agenda."
  2824. :group 'org-agenda
  2825. :type 'sexp)
  2826. (defcustom org-calendar-insert-diary-entry-key [?i]
  2827. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2828. This option is irrelevant until `org-agenda-diary-file' has been configured
  2829. to point to an Org-mode file. When that is the case, the command
  2830. `org-agenda-diary-entry' will be bound to the key given here, by default
  2831. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2832. if you want to continue doing this, you need to change this to a different
  2833. key."
  2834. :group 'org-agenda
  2835. :type 'sexp)
  2836. (defcustom org-agenda-diary-file 'diary-file
  2837. "File to which to add new entries with the `i' key in agenda and calendar.
  2838. When this is the symbol `diary-file', the functionality in the Emacs
  2839. calendar will be used to add entries to the `diary-file'. But when this
  2840. points to a file, `org-agenda-diary-entry' will be used instead."
  2841. :group 'org-agenda
  2842. :type '(choice
  2843. (const :tag "The standard Emacs diary file" diary-file)
  2844. (file :tag "Special Org file diary entries")))
  2845. (eval-after-load "calendar"
  2846. '(progn
  2847. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2848. 'org-calendar-goto-agenda)
  2849. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2850. 'org-agenda-action)
  2851. (add-hook 'calendar-mode-hook
  2852. (lambda ()
  2853. (unless (eq org-agenda-diary-file 'diary-file)
  2854. (define-key calendar-mode-map
  2855. org-calendar-insert-diary-entry-key
  2856. 'org-agenda-diary-entry))))))
  2857. (defgroup org-latex nil
  2858. "Options for embedding LaTeX code into Org-mode."
  2859. :tag "Org LaTeX"
  2860. :group 'org)
  2861. (defcustom org-format-latex-options
  2862. '(:foreground default :background default :scale 1.0
  2863. :html-foreground "Black" :html-background "Transparent"
  2864. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2865. "Options for creating images from LaTeX fragments.
  2866. This is a property list with the following properties:
  2867. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2868. `default' means use the foreground of the default face.
  2869. :background the background color, or \"Transparent\".
  2870. `default' means use the background of the default face.
  2871. :scale a scaling factor for the size of the images, to get more pixels
  2872. :html-foreground, :html-background, :html-scale
  2873. the same numbers for HTML export.
  2874. :matchers a list indicating which matchers should be used to
  2875. find LaTeX fragments. Valid members of this list are:
  2876. \"begin\" find environments
  2877. \"$1\" find single characters surrounded by $.$
  2878. \"$\" find math expressions surrounded by $...$
  2879. \"$$\" find math expressions surrounded by $$....$$
  2880. \"\\(\" find math expressions surrounded by \\(...\\)
  2881. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2882. :group 'org-latex
  2883. :type 'plist)
  2884. (defcustom org-format-latex-signal-error t
  2885. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2886. When nil, just push out a message."
  2887. :group 'org-latex
  2888. :type 'boolean)
  2889. (defcustom org-latex-to-mathml-jar-file nil
  2890. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2891. Use this to specify additional executable file say a jar file.
  2892. When using MathToWeb as the converter, specify the full-path to
  2893. your mathtoweb.jar file."
  2894. :group 'org-latex
  2895. :type '(choice
  2896. (const :tag "None" nil)
  2897. (file :tag "JAR file" :must-match t)))
  2898. (defcustom org-latex-to-mathml-convert-command nil
  2899. "Command to convert LaTeX fragments to MathML.
  2900. Replace format-specifiers in the command as noted below and use
  2901. `shell-command' to convert LaTeX to MathML.
  2902. %j: Executable file in fully expanded form as specified by
  2903. `org-latex-to-mathml-jar-file'.
  2904. %I: Input LaTeX file in fully expanded form
  2905. %o: Output MathML file
  2906. This command is used by `org-create-math-formula'.
  2907. When using MathToWeb as the converter, set this to
  2908. \"java -jar %j -unicode -force -df %o %I\"."
  2909. :group 'org-latex
  2910. :type '(choice
  2911. (const :tag "None" nil)
  2912. (string :tag "\nShell command")))
  2913. (defun org-format-latex-mathml-available-p ()
  2914. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2915. (save-match-data
  2916. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2917. org-latex-to-mathml-convert-command)
  2918. (let ((executable (car (split-string
  2919. org-latex-to-mathml-convert-command))))
  2920. (when (executable-find executable)
  2921. (if (string-match
  2922. "%j" org-latex-to-mathml-convert-command)
  2923. (file-readable-p org-latex-to-mathml-jar-file)
  2924. t))))))
  2925. (defcustom org-format-latex-header "\\documentclass{article}
  2926. \\usepackage[usenames]{color}
  2927. \\usepackage{amsmath}
  2928. \\usepackage[mathscr]{eucal}
  2929. \\pagestyle{empty} % do not remove
  2930. \[PACKAGES]
  2931. \[DEFAULT-PACKAGES]
  2932. % The settings below are copied from fullpage.sty
  2933. \\setlength{\\textwidth}{\\paperwidth}
  2934. \\addtolength{\\textwidth}{-3cm}
  2935. \\setlength{\\oddsidemargin}{1.5cm}
  2936. \\addtolength{\\oddsidemargin}{-2.54cm}
  2937. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2938. \\setlength{\\textheight}{\\paperheight}
  2939. \\addtolength{\\textheight}{-\\headheight}
  2940. \\addtolength{\\textheight}{-\\headsep}
  2941. \\addtolength{\\textheight}{-\\footskip}
  2942. \\addtolength{\\textheight}{-3cm}
  2943. \\setlength{\\topmargin}{1.5cm}
  2944. \\addtolength{\\topmargin}{-2.54cm}"
  2945. "The document header used for processing LaTeX fragments.
  2946. It is imperative that this header make sure that no page number
  2947. appears on the page. The package defined in the variables
  2948. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2949. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2950. will be appended."
  2951. :group 'org-latex
  2952. :type 'string)
  2953. (defvar org-format-latex-header-extra nil)
  2954. (defun org-set-packages-alist (var val)
  2955. "Set the packages alist and make sure it has 3 elements per entry."
  2956. (set var (mapcar (lambda (x)
  2957. (if (and (consp x) (= (length x) 2))
  2958. (list (car x) (nth 1 x) t)
  2959. x))
  2960. val)))
  2961. (defun org-get-packages-alist (var)
  2962. "Get the packages alist and make sure it has 3 elements per entry."
  2963. (mapcar (lambda (x)
  2964. (if (and (consp x) (= (length x) 2))
  2965. (list (car x) (nth 1 x) t)
  2966. x))
  2967. (default-value var)))
  2968. ;; The following variables are defined here because is it also used
  2969. ;; when formatting latex fragments. Originally it was part of the
  2970. ;; LaTeX exporter, which is why the name includes "export".
  2971. (defcustom org-export-latex-default-packages-alist
  2972. '(("AUTO" "inputenc" t)
  2973. ("T1" "fontenc" t)
  2974. ("" "fixltx2e" nil)
  2975. ("" "graphicx" t)
  2976. ("" "longtable" nil)
  2977. ("" "float" nil)
  2978. ("" "wrapfig" nil)
  2979. ("" "soul" t)
  2980. ("" "textcomp" t)
  2981. ("" "marvosym" t)
  2982. ("" "wasysym" t)
  2983. ("" "latexsym" t)
  2984. ("" "amssymb" t)
  2985. ("" "hyperref" nil)
  2986. "\\tolerance=1000"
  2987. )
  2988. "Alist of default packages to be inserted in the header.
  2989. Change this only if one of the packages here causes an incompatibility
  2990. with another package you are using.
  2991. The packages in this list are needed by one part or another of Org-mode
  2992. to function properly.
  2993. - inputenc, fontenc: for basic font and character selection
  2994. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2995. for interpreting the entities in `org-entities'. You can skip some of these
  2996. packages if you don't use any of the symbols in it.
  2997. - graphicx: for including images
  2998. - float, wrapfig: for figure placement
  2999. - longtable: for long tables
  3000. - hyperref: for cross references
  3001. Therefore you should not modify this variable unless you know what you
  3002. are doing. The one reason to change it anyway is that you might be loading
  3003. some other package that conflicts with one of the default packages.
  3004. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3005. If SNIPPET-FLAG is t, the package also needs to be included when
  3006. compiling LaTeX snippets into images for inclusion into HTML."
  3007. :group 'org-export-latex
  3008. :set 'org-set-packages-alist
  3009. :get 'org-get-packages-alist
  3010. :type '(repeat
  3011. (choice
  3012. (list :tag "options/package pair"
  3013. (string :tag "options")
  3014. (string :tag "package")
  3015. (boolean :tag "Snippet"))
  3016. (string :tag "A line of LaTeX"))))
  3017. (defcustom org-export-latex-packages-alist nil
  3018. "Alist of packages to be inserted in every LaTeX header.
  3019. These will be inserted after `org-export-latex-default-packages-alist'.
  3020. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3021. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3022. turning LaTeX snippets into images for inclusion into HTML.
  3023. Make sure that you only list packages here which:
  3024. - you want in every file
  3025. - do not conflict with the default packages in
  3026. `org-export-latex-default-packages-alist'
  3027. - do not conflict with the setup in `org-format-latex-header'."
  3028. :group 'org-export-latex
  3029. :set 'org-set-packages-alist
  3030. :get 'org-get-packages-alist
  3031. :type '(repeat
  3032. (choice
  3033. (list :tag "options/package pair"
  3034. (string :tag "options")
  3035. (string :tag "package")
  3036. (boolean :tag "Snippet"))
  3037. (string :tag "A line of LaTeX"))))
  3038. (defgroup org-appearance nil
  3039. "Settings for Org-mode appearance."
  3040. :tag "Org Appearance"
  3041. :group 'org)
  3042. (defcustom org-level-color-stars-only nil
  3043. "Non-nil means fontify only the stars in each headline.
  3044. When nil, the entire headline is fontified.
  3045. Changing it requires restart of `font-lock-mode' to become effective
  3046. also in regions already fontified."
  3047. :group 'org-appearance
  3048. :type 'boolean)
  3049. (defcustom org-hide-leading-stars nil
  3050. "Non-nil means hide the first N-1 stars in a headline.
  3051. This works by using the face `org-hide' for these stars. This
  3052. face is white for a light background, and black for a dark
  3053. background. You may have to customize the face `org-hide' to
  3054. make this work.
  3055. Changing it requires restart of `font-lock-mode' to become effective
  3056. also in regions already fontified.
  3057. You may also set this on a per-file basis by adding one of the following
  3058. lines to the buffer:
  3059. #+STARTUP: hidestars
  3060. #+STARTUP: showstars"
  3061. :group 'org-appearance
  3062. :type 'boolean)
  3063. (defcustom org-hidden-keywords nil
  3064. "List of symbols corresponding to keywords to be hidden the org buffer.
  3065. For example, a value '(title) for this list will make the document's title
  3066. appear in the buffer without the initial #+TITLE: keyword."
  3067. :group 'org-appearance
  3068. :type '(set (const :tag "#+AUTHOR" author)
  3069. (const :tag "#+DATE" date)
  3070. (const :tag "#+EMAIL" email)
  3071. (const :tag "#+TITLE" title)))
  3072. (defcustom org-fontify-done-headline nil
  3073. "Non-nil means change the face of a headline if it is marked DONE.
  3074. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3075. When this is non-nil, the headline after the keyword is set to the
  3076. `org-headline-done' as an additional indication."
  3077. :group 'org-appearance
  3078. :type 'boolean)
  3079. (defcustom org-fontify-emphasized-text t
  3080. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3081. Changing this variable requires a restart of Emacs to take effect."
  3082. :group 'org-appearance
  3083. :type 'boolean)
  3084. (defcustom org-fontify-whole-heading-line nil
  3085. "Non-nil means fontify the whole line for headings.
  3086. This is useful when setting a background color for the
  3087. org-level-* faces."
  3088. :group 'org-appearance
  3089. :type 'boolean)
  3090. (defcustom org-highlight-latex-fragments-and-specials nil
  3091. "Non-nil means fontify what is treated specially by the exporters."
  3092. :group 'org-appearance
  3093. :type 'boolean)
  3094. (defcustom org-hide-emphasis-markers nil
  3095. "Non-nil mean font-lock should hide the emphasis marker characters."
  3096. :group 'org-appearance
  3097. :type 'boolean)
  3098. (defcustom org-pretty-entities nil
  3099. "Non-nil means show entities as UTF8 characters.
  3100. When nil, the \\name form remains in the buffer."
  3101. :group 'org-appearance
  3102. :type 'boolean)
  3103. (defcustom org-pretty-entities-include-sub-superscripts t
  3104. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3105. :group 'org-appearance
  3106. :type 'boolean)
  3107. (defvar org-emph-re nil
  3108. "Regular expression for matching emphasis.
  3109. After a match, the match groups contain these elements:
  3110. 0 The match of the full regular expression, including the characters
  3111. before and after the proper match
  3112. 1 The character before the proper match, or empty at beginning of line
  3113. 2 The proper match, including the leading and trailing markers
  3114. 3 The leading marker like * or /, indicating the type of highlighting
  3115. 4 The text between the emphasis markers, not including the markers
  3116. 5 The character after the match, empty at the end of a line")
  3117. (defvar org-verbatim-re nil
  3118. "Regular expression for matching verbatim text.")
  3119. (defvar org-emphasis-regexp-components) ; defined just below
  3120. (defvar org-emphasis-alist) ; defined just below
  3121. (defun org-set-emph-re (var val)
  3122. "Set variable and compute the emphasis regular expression."
  3123. (set var val)
  3124. (when (and (boundp 'org-emphasis-alist)
  3125. (boundp 'org-emphasis-regexp-components)
  3126. org-emphasis-alist org-emphasis-regexp-components)
  3127. (let* ((e org-emphasis-regexp-components)
  3128. (pre (car e))
  3129. (post (nth 1 e))
  3130. (border (nth 2 e))
  3131. (body (nth 3 e))
  3132. (nl (nth 4 e))
  3133. (body1 (concat body "*?"))
  3134. (markers (mapconcat 'car org-emphasis-alist ""))
  3135. (vmarkers (mapconcat
  3136. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3137. org-emphasis-alist "")))
  3138. ;; make sure special characters appear at the right position in the class
  3139. (if (string-match "\\^" markers)
  3140. (setq markers (concat (replace-match "" t t markers) "^")))
  3141. (if (string-match "-" markers)
  3142. (setq markers (concat (replace-match "" t t markers) "-")))
  3143. (if (string-match "\\^" vmarkers)
  3144. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3145. (if (string-match "-" vmarkers)
  3146. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3147. (if (> nl 0)
  3148. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3149. (int-to-string nl) "\\}")))
  3150. ;; Make the regexp
  3151. (setq org-emph-re
  3152. (concat "\\([" pre "]\\|^\\)"
  3153. "\\("
  3154. "\\([" markers "]\\)"
  3155. "\\("
  3156. "[^" border "]\\|"
  3157. "[^" border "]"
  3158. body1
  3159. "[^" border "]"
  3160. "\\)"
  3161. "\\3\\)"
  3162. "\\([" post "]\\|$\\)"))
  3163. (setq org-verbatim-re
  3164. (concat "\\([" pre "]\\|^\\)"
  3165. "\\("
  3166. "\\([" vmarkers "]\\)"
  3167. "\\("
  3168. "[^" border "]\\|"
  3169. "[^" border "]"
  3170. body1
  3171. "[^" border "]"
  3172. "\\)"
  3173. "\\3\\)"
  3174. "\\([" post "]\\|$\\)")))))
  3175. (defcustom org-emphasis-regexp-components
  3176. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3177. "Components used to build the regular expression for emphasis.
  3178. This is a list with five entries. Terminology: In an emphasis string
  3179. like \" *strong word* \", we call the initial space PREMATCH, the final
  3180. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3181. and \"trong wor\" is the body. The different components in this variable
  3182. specify what is allowed/forbidden in each part:
  3183. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3184. post Chars allowed as postmatch. End of line will be allowed too.
  3185. border The chars *forbidden* as border characters.
  3186. body-regexp A regexp like \".\" to match a body character. Don't use
  3187. non-shy groups here, and don't allow newline here.
  3188. newline The maximum number of newlines allowed in an emphasis exp.
  3189. Use customize to modify this, or restart Emacs after changing it."
  3190. :group 'org-appearance
  3191. :set 'org-set-emph-re
  3192. :type '(list
  3193. (sexp :tag "Allowed chars in pre ")
  3194. (sexp :tag "Allowed chars in post ")
  3195. (sexp :tag "Forbidden chars in border ")
  3196. (sexp :tag "Regexp for body ")
  3197. (integer :tag "number of newlines allowed")
  3198. (option (boolean :tag "Please ignore this button"))))
  3199. (defcustom org-emphasis-alist
  3200. `(("*" bold "<b>" "</b>")
  3201. ("/" italic "<i>" "</i>")
  3202. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3203. ("=" org-code "<code>" "</code>" verbatim)
  3204. ("~" org-verbatim "<code>" "</code>" verbatim)
  3205. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3206. "<del>" "</del>")
  3207. )
  3208. "Special syntax for emphasized text.
  3209. Text starting and ending with a special character will be emphasized, for
  3210. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3211. characters, the face to be used by font-lock for highlighting in Org-mode
  3212. Emacs buffers, and the HTML tags to be used for this.
  3213. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3214. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3215. Use customize to modify this, or restart Emacs after changing it."
  3216. :group 'org-appearance
  3217. :set 'org-set-emph-re
  3218. :type '(repeat
  3219. (list
  3220. (string :tag "Marker character")
  3221. (choice
  3222. (face :tag "Font-lock-face")
  3223. (plist :tag "Face property list"))
  3224. (string :tag "HTML start tag")
  3225. (string :tag "HTML end tag")
  3226. (option (const verbatim)))))
  3227. (defvar org-protecting-blocks
  3228. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3229. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3230. This is needed for font-lock setup.")
  3231. ;;; Miscellaneous options
  3232. (defgroup org-completion nil
  3233. "Completion in Org-mode."
  3234. :tag "Org Completion"
  3235. :group 'org)
  3236. (defcustom org-completion-use-ido nil
  3237. "Non-nil means use ido completion wherever possible.
  3238. Note that `ido-mode' must be active for this variable to be relevant.
  3239. If you decide to turn this variable on, you might well want to turn off
  3240. `org-outline-path-complete-in-steps'.
  3241. See also `org-completion-use-iswitchb'."
  3242. :group 'org-completion
  3243. :type 'boolean)
  3244. (defcustom org-completion-use-iswitchb nil
  3245. "Non-nil means use iswitchb completion wherever possible.
  3246. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3247. If you decide to turn this variable on, you might well want to turn off
  3248. `org-outline-path-complete-in-steps'.
  3249. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3250. :group 'org-completion
  3251. :type 'boolean)
  3252. (defcustom org-completion-fallback-command 'hippie-expand
  3253. "The expansion command called by \\[pcomplete] in normal context.
  3254. Normal means, no org-mode-specific context."
  3255. :group 'org-completion
  3256. :type 'function)
  3257. ;;; Functions and variables from their packages
  3258. ;; Declared here to avoid compiler warnings
  3259. ;; XEmacs only
  3260. (defvar outline-mode-menu-heading)
  3261. (defvar outline-mode-menu-show)
  3262. (defvar outline-mode-menu-hide)
  3263. (defvar zmacs-regions) ; XEmacs regions
  3264. ;; Emacs only
  3265. (defvar mark-active)
  3266. ;; Various packages
  3267. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3268. (declare-function calendar-forward-day "cal-move" (arg))
  3269. (declare-function calendar-goto-date "cal-move" (date))
  3270. (declare-function calendar-goto-today "cal-move" ())
  3271. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3272. (defvar calc-embedded-close-formula)
  3273. (defvar calc-embedded-open-formula)
  3274. (declare-function cdlatex-tab "ext:cdlatex" ())
  3275. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3276. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3277. (defvar font-lock-unfontify-region-function)
  3278. (declare-function iswitchb-read-buffer "iswitchb"
  3279. (prompt &optional default require-match start matches-set))
  3280. (defvar iswitchb-temp-buflist)
  3281. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3282. (defvar org-agenda-tags-todo-honor-ignore-options)
  3283. (declare-function org-agenda-skip "org-agenda" ())
  3284. (declare-function
  3285. org-agenda-format-item "org-agenda"
  3286. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3287. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3288. (declare-function org-agenda-change-all-lines "org-agenda"
  3289. (newhead hdmarker &optional fixface just-this))
  3290. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3291. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3292. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3293. (beg end))
  3294. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3295. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3296. "org-agenda" (&optional end))
  3297. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3298. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3299. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3300. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3301. (declare-function org-indent-mode "org-indent" (&optional arg))
  3302. (declare-function parse-time-string "parse-time" (string))
  3303. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3304. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3305. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3306. (defvar remember-data-file)
  3307. (defvar texmathp-why)
  3308. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3309. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3310. (defvar w3m-current-url)
  3311. (defvar w3m-current-title)
  3312. (defvar org-latex-regexps)
  3313. ;;; Autoload and prepare some org modules
  3314. ;; Some table stuff that needs to be defined here, because it is used
  3315. ;; by the functions setting up org-mode or checking for table context.
  3316. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3317. "Detect an org-type or table-type table.")
  3318. (defconst org-table-line-regexp "^[ \t]*|"
  3319. "Detect an org-type table line.")
  3320. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3321. "Detect an org-type table line.")
  3322. (defconst org-table-hline-regexp "^[ \t]*|-"
  3323. "Detect an org-type table hline.")
  3324. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3325. "Detect a table-type table hline.")
  3326. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3327. "Detect the first line outside a table when searching from within it.
  3328. This works for both table types.")
  3329. ;; Autoload the functions in org-table.el that are needed by functions here.
  3330. (eval-and-compile
  3331. (org-autoload "org-table"
  3332. '(org-table-align org-table-begin org-table-blank-field
  3333. org-table-convert org-table-convert-region org-table-copy-down
  3334. org-table-copy-region org-table-create
  3335. org-table-create-or-convert-from-region
  3336. org-table-create-with-table.el org-table-current-dline
  3337. org-table-cut-region org-table-delete-column org-table-edit-field
  3338. org-table-edit-formulas org-table-end org-table-eval-formula
  3339. org-table-export org-table-field-info
  3340. org-table-get-stored-formulas org-table-goto-column
  3341. org-table-hline-and-move org-table-import org-table-insert-column
  3342. org-table-insert-hline org-table-insert-row org-table-iterate
  3343. org-table-justify-field-maybe org-table-kill-row
  3344. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3345. org-table-move-column org-table-move-column-left
  3346. org-table-move-column-right org-table-move-row
  3347. org-table-move-row-down org-table-move-row-up
  3348. org-table-next-field org-table-next-row org-table-paste-rectangle
  3349. org-table-previous-field org-table-recalculate
  3350. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3351. org-table-toggle-coordinate-overlays
  3352. org-table-toggle-formula-debugger org-table-wrap-region
  3353. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3354. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3355. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3356. (defun org-at-table-p (&optional table-type)
  3357. "Return t if the cursor is inside an org-type table.
  3358. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3359. (if org-enable-table-editor
  3360. (save-excursion
  3361. (beginning-of-line 1)
  3362. (looking-at (if table-type org-table-any-line-regexp
  3363. org-table-line-regexp)))
  3364. nil))
  3365. (defsubst org-table-p () (org-at-table-p))
  3366. (defun org-at-table.el-p ()
  3367. "Return t if and only if we are at a table.el table."
  3368. (and (org-at-table-p 'any)
  3369. (save-excursion
  3370. (goto-char (org-table-begin 'any))
  3371. (looking-at org-table1-hline-regexp))))
  3372. (defun org-table-recognize-table.el ()
  3373. "If there is a table.el table nearby, recognize it and move into it."
  3374. (if org-table-tab-recognizes-table.el
  3375. (if (org-at-table.el-p)
  3376. (progn
  3377. (beginning-of-line 1)
  3378. (if (looking-at org-table-dataline-regexp)
  3379. nil
  3380. (if (looking-at org-table1-hline-regexp)
  3381. (progn
  3382. (beginning-of-line 2)
  3383. (if (looking-at org-table-any-border-regexp)
  3384. (beginning-of-line -1)))))
  3385. (if (re-search-forward "|" (org-table-end t) t)
  3386. (progn
  3387. (require 'table)
  3388. (if (table--at-cell-p (point))
  3389. t
  3390. (message "recognizing table.el table...")
  3391. (table-recognize-table)
  3392. (message "recognizing table.el table...done")))
  3393. (error "This should not happen"))
  3394. t)
  3395. nil)
  3396. nil))
  3397. (defun org-at-table-hline-p ()
  3398. "Return t if the cursor is inside a hline in a table."
  3399. (if org-enable-table-editor
  3400. (save-excursion
  3401. (beginning-of-line 1)
  3402. (looking-at org-table-hline-regexp))
  3403. nil))
  3404. (defvar org-table-clean-did-remove-column nil)
  3405. (defun org-table-map-tables (function &optional quietly)
  3406. "Apply FUNCTION to the start of all tables in the buffer."
  3407. (save-excursion
  3408. (save-restriction
  3409. (widen)
  3410. (goto-char (point-min))
  3411. (while (re-search-forward org-table-any-line-regexp nil t)
  3412. (unless quietly
  3413. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3414. (beginning-of-line 1)
  3415. (when (looking-at org-table-line-regexp)
  3416. (save-excursion (funcall function))
  3417. (or (looking-at org-table-line-regexp)
  3418. (forward-char 1)))
  3419. (re-search-forward org-table-any-border-regexp nil 1))))
  3420. (unless quietly (message "Mapping tables: done")))
  3421. ;; Declare and autoload functions from org-exp.el & Co
  3422. (declare-function org-default-export-plist "org-exp")
  3423. (declare-function org-infile-export-plist "org-exp")
  3424. (declare-function org-get-current-options "org-exp")
  3425. (eval-and-compile
  3426. (org-autoload "org-exp"
  3427. '(org-export org-export-visible
  3428. org-insert-export-options-template
  3429. org-table-clean-before-export))
  3430. (org-autoload "org-ascii"
  3431. '(org-export-as-ascii org-export-ascii-preprocess
  3432. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3433. org-export-region-as-ascii))
  3434. (org-autoload "org-latex"
  3435. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3436. org-replace-region-by-latex org-export-region-as-latex
  3437. org-export-as-latex org-export-as-pdf
  3438. org-export-as-pdf-and-open))
  3439. (org-autoload "org-html"
  3440. '(org-export-as-html-and-open
  3441. org-export-as-html-batch org-export-as-html-to-buffer
  3442. org-replace-region-by-html org-export-region-as-html
  3443. org-export-as-html))
  3444. (org-autoload "org-docbook"
  3445. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3446. org-replace-region-by-docbook org-export-region-as-docbook
  3447. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3448. org-export-as-docbook))
  3449. (org-autoload "org-icalendar"
  3450. '(org-export-icalendar-this-file
  3451. org-export-icalendar-all-agenda-files
  3452. org-export-icalendar-combine-agenda-files))
  3453. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3454. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3455. ;; Declare and autoload functions from org-agenda.el
  3456. (eval-and-compile
  3457. (org-autoload "org-agenda"
  3458. '(org-agenda org-agenda-list org-search-view
  3459. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3460. org-diary org-agenda-to-appt
  3461. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3462. ;; Autoload org-remember
  3463. (eval-and-compile
  3464. (org-autoload "org-remember"
  3465. '(org-remember-insinuate org-remember-annotation
  3466. org-remember-apply-template org-remember org-remember-handler)))
  3467. (eval-and-compile
  3468. (org-autoload "org-capture"
  3469. '(org-capture org-capture-insert-template-here
  3470. org-capture-import-remember-templates)))
  3471. ;; Autoload org-clock.el
  3472. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3473. (beg end))
  3474. (declare-function org-clock-update-mode-line "org-clock" ())
  3475. (declare-function org-resolve-clocks "org-clock"
  3476. (&optional also-non-dangling-p prompt last-valid))
  3477. (defvar org-clock-start-time)
  3478. (defvar org-clock-marker (make-marker)
  3479. "Marker recording the last clock-in.")
  3480. (defvar org-clock-hd-marker (make-marker)
  3481. "Marker recording the last clock-in, but the headline position.")
  3482. (defvar org-clock-heading ""
  3483. "The heading of the current clock entry.")
  3484. (defun org-clock-is-active ()
  3485. "Return non-nil if clock is currently running.
  3486. The return value is actually the clock marker."
  3487. (marker-buffer org-clock-marker))
  3488. (eval-and-compile
  3489. (org-autoload
  3490. "org-clock"
  3491. '(org-clock-in org-clock-out org-clock-cancel
  3492. org-clock-goto org-clock-sum org-clock-display
  3493. org-clock-remove-overlays org-clock-report
  3494. org-clocktable-shift org-dblock-write:clocktable
  3495. org-get-clocktable org-resolve-clocks)))
  3496. (defun org-clock-update-time-maybe ()
  3497. "If this is a CLOCK line, update it and return t.
  3498. Otherwise, return nil."
  3499. (interactive)
  3500. (save-excursion
  3501. (beginning-of-line 1)
  3502. (skip-chars-forward " \t")
  3503. (when (looking-at org-clock-string)
  3504. (let ((re (concat "[ \t]*" org-clock-string
  3505. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3506. "\\([ \t]*=>.*\\)?\\)?"))
  3507. ts te h m s neg)
  3508. (cond
  3509. ((not (looking-at re))
  3510. nil)
  3511. ((not (match-end 2))
  3512. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3513. (> org-clock-marker (point))
  3514. (<= org-clock-marker (point-at-eol)))
  3515. ;; The clock is running here
  3516. (setq org-clock-start-time
  3517. (apply 'encode-time
  3518. (org-parse-time-string (match-string 1))))
  3519. (org-clock-update-mode-line)))
  3520. (t
  3521. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3522. (end-of-line 1)
  3523. (setq ts (match-string 1)
  3524. te (match-string 3))
  3525. (setq s (- (org-float-time
  3526. (apply 'encode-time (org-parse-time-string te)))
  3527. (org-float-time
  3528. (apply 'encode-time (org-parse-time-string ts))))
  3529. neg (< s 0)
  3530. s (abs s)
  3531. h (floor (/ s 3600))
  3532. s (- s (* 3600 h))
  3533. m (floor (/ s 60))
  3534. s (- s (* 60 s)))
  3535. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3536. t))))))
  3537. (defun org-check-running-clock ()
  3538. "Check if the current buffer contains the running clock.
  3539. If yes, offer to stop it and to save the buffer with the changes."
  3540. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3541. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3542. (buffer-name))))
  3543. (org-clock-out)
  3544. (when (y-or-n-p "Save changed buffer?")
  3545. (save-buffer))))
  3546. (defun org-clocktable-try-shift (dir n)
  3547. "Check if this line starts a clock table, if yes, shift the time block."
  3548. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3549. (org-clocktable-shift dir n)))
  3550. ;; Autoload org-timer.el
  3551. (eval-and-compile
  3552. (org-autoload
  3553. "org-timer"
  3554. '(org-timer-start org-timer org-timer-item
  3555. org-timer-change-times-in-region
  3556. org-timer-set-timer
  3557. org-timer-reset-timers
  3558. org-timer-show-remaining-time)))
  3559. ;; Autoload org-feed.el
  3560. (eval-and-compile
  3561. (org-autoload
  3562. "org-feed"
  3563. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3564. ;; Autoload org-indent.el
  3565. ;; Define the variable already here, to make sure we have it.
  3566. (defvar org-indent-mode nil
  3567. "Non-nil if Org-Indent mode is enabled.
  3568. Use the command `org-indent-mode' to change this variable.")
  3569. (eval-and-compile
  3570. (org-autoload
  3571. "org-indent"
  3572. '(org-indent-mode)))
  3573. ;; Autoload org-mobile.el
  3574. (eval-and-compile
  3575. (org-autoload
  3576. "org-mobile"
  3577. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3578. ;; Autoload archiving code
  3579. ;; The stuff that is needed for cycling and tags has to be defined here.
  3580. (defgroup org-archive nil
  3581. "Options concerning archiving in Org-mode."
  3582. :tag "Org Archive"
  3583. :group 'org-structure)
  3584. (defcustom org-archive-location "%s_archive::"
  3585. "The location where subtrees should be archived.
  3586. The value of this variable is a string, consisting of two parts,
  3587. separated by a double-colon. The first part is a filename and
  3588. the second part is a headline.
  3589. When the filename is omitted, archiving happens in the same file.
  3590. %s in the filename will be replaced by the current file
  3591. name (without the directory part). Archiving to a different file
  3592. is useful to keep archived entries from contributing to the
  3593. Org-mode Agenda.
  3594. The archived entries will be filed as subtrees of the specified
  3595. headline. When the headline is omitted, the subtrees are simply
  3596. filed away at the end of the file, as top-level entries. Also in
  3597. the heading you can use %s to represent the file name, this can be
  3598. useful when using the same archive for a number of different files.
  3599. Here are a few examples:
  3600. \"%s_archive::\"
  3601. If the current file is Projects.org, archive in file
  3602. Projects.org_archive, as top-level trees. This is the default.
  3603. \"::* Archived Tasks\"
  3604. Archive in the current file, under the top-level headline
  3605. \"* Archived Tasks\".
  3606. \"~/org/archive.org::\"
  3607. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3608. \"~/org/archive.org::* From %s\"
  3609. Archive in file ~/org/archive.org (absolute path), under headlines
  3610. \"From FILENAME\" where file name is the current file name.
  3611. \"basement::** Finished Tasks\"
  3612. Archive in file ./basement (relative path), as level 3 trees
  3613. below the level 2 heading \"** Finished Tasks\".
  3614. You may set this option on a per-file basis by adding to the buffer a
  3615. line like
  3616. #+ARCHIVE: basement::** Finished Tasks
  3617. You may also define it locally for a subtree by setting an ARCHIVE property
  3618. in the entry. If such a property is found in an entry, or anywhere up
  3619. the hierarchy, it will be used."
  3620. :group 'org-archive
  3621. :type 'string)
  3622. (defcustom org-archive-tag "ARCHIVE"
  3623. "The tag that marks a subtree as archived.
  3624. An archived subtree does not open during visibility cycling, and does
  3625. not contribute to the agenda listings.
  3626. After changing this, font-lock must be restarted in the relevant buffers to
  3627. get the proper fontification."
  3628. :group 'org-archive
  3629. :group 'org-keywords
  3630. :type 'string)
  3631. (defcustom org-agenda-skip-archived-trees t
  3632. "Non-nil means the agenda will skip any items located in archived trees.
  3633. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3634. variable is no longer recommended, you should leave it at the value t.
  3635. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3636. :group 'org-archive
  3637. :group 'org-agenda-skip
  3638. :type 'boolean)
  3639. (defcustom org-columns-skip-archived-trees t
  3640. "Non-nil means ignore archived trees when creating column view."
  3641. :group 'org-archive
  3642. :group 'org-properties
  3643. :type 'boolean)
  3644. (defcustom org-cycle-open-archived-trees nil
  3645. "Non-nil means `org-cycle' will open archived trees.
  3646. An archived tree is a tree marked with the tag ARCHIVE.
  3647. When nil, archived trees will stay folded. You can still open them with
  3648. normal outline commands like `show-all', but not with the cycling commands."
  3649. :group 'org-archive
  3650. :group 'org-cycle
  3651. :type 'boolean)
  3652. (defcustom org-sparse-tree-open-archived-trees nil
  3653. "Non-nil means sparse tree construction shows matches in archived trees.
  3654. When nil, matches in these trees are highlighted, but the trees are kept in
  3655. collapsed state."
  3656. :group 'org-archive
  3657. :group 'org-sparse-trees
  3658. :type 'boolean)
  3659. (defun org-cycle-hide-archived-subtrees (state)
  3660. "Re-hide all archived subtrees after a visibility state change."
  3661. (when (and (not org-cycle-open-archived-trees)
  3662. (not (memq state '(overview folded))))
  3663. (save-excursion
  3664. (let* ((globalp (memq state '(contents all)))
  3665. (beg (if globalp (point-min) (point)))
  3666. (end (if globalp (point-max) (org-end-of-subtree t))))
  3667. (org-hide-archived-subtrees beg end)
  3668. (goto-char beg)
  3669. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3670. (message "%s" (substitute-command-keys
  3671. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3672. (defun org-force-cycle-archived ()
  3673. "Cycle subtree even if it is archived."
  3674. (interactive)
  3675. (setq this-command 'org-cycle)
  3676. (let ((org-cycle-open-archived-trees t))
  3677. (call-interactively 'org-cycle)))
  3678. (defun org-hide-archived-subtrees (beg end)
  3679. "Re-hide all archived subtrees after a visibility state change."
  3680. (save-excursion
  3681. (let* ((re (concat ":" org-archive-tag ":")))
  3682. (goto-char beg)
  3683. (while (re-search-forward re end t)
  3684. (when (org-at-heading-p)
  3685. (org-flag-subtree t)
  3686. (org-end-of-subtree t))))))
  3687. (defun org-flag-subtree (flag)
  3688. (save-excursion
  3689. (org-back-to-heading t)
  3690. (outline-end-of-heading)
  3691. (outline-flag-region (point)
  3692. (progn (org-end-of-subtree t) (point))
  3693. flag)))
  3694. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3695. (eval-and-compile
  3696. (org-autoload "org-archive"
  3697. '(org-add-archive-files org-archive-subtree
  3698. org-archive-to-archive-sibling org-toggle-archive-tag
  3699. org-archive-subtree-default
  3700. org-archive-subtree-default-with-confirmation)))
  3701. ;; Autoload Column View Code
  3702. (declare-function org-columns-number-to-string "org-colview")
  3703. (declare-function org-columns-get-format-and-top-level "org-colview")
  3704. (declare-function org-columns-compute "org-colview")
  3705. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3706. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3707. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3708. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3709. ;; Autoload ID code
  3710. (declare-function org-id-store-link "org-id")
  3711. (declare-function org-id-locations-load "org-id")
  3712. (declare-function org-id-locations-save "org-id")
  3713. (defvar org-id-track-globally)
  3714. (org-autoload "org-id"
  3715. '(org-id-get-create org-id-new org-id-copy org-id-get
  3716. org-id-get-with-outline-path-completion
  3717. org-id-get-with-outline-drilling org-id-store-link
  3718. org-id-goto org-id-find org-id-store-link))
  3719. ;; Autoload Plotting Code
  3720. (org-autoload "org-plot"
  3721. '(org-plot/gnuplot))
  3722. ;;; Variables for pre-computed regular expressions, all buffer local
  3723. (defvar org-drawer-regexp nil
  3724. "Matches first line of a hidden block.")
  3725. (make-variable-buffer-local 'org-drawer-regexp)
  3726. (defvar org-todo-regexp nil
  3727. "Matches any of the TODO state keywords.")
  3728. (make-variable-buffer-local 'org-todo-regexp)
  3729. (defvar org-not-done-regexp nil
  3730. "Matches any of the TODO state keywords except the last one.")
  3731. (make-variable-buffer-local 'org-not-done-regexp)
  3732. (defvar org-not-done-heading-regexp nil
  3733. "Matches a TODO headline that is not done.")
  3734. (make-variable-buffer-local 'org-not-done-regexp)
  3735. (defvar org-todo-line-regexp nil
  3736. "Matches a headline and puts TODO state into group 2 if present.")
  3737. (make-variable-buffer-local 'org-todo-line-regexp)
  3738. (defvar org-complex-heading-regexp nil
  3739. "Matches a headline and puts everything into groups:
  3740. group 1: the stars
  3741. group 2: The todo keyword, maybe
  3742. group 3: Priority cookie
  3743. group 4: True headline
  3744. group 5: Tags")
  3745. (make-variable-buffer-local 'org-complex-heading-regexp)
  3746. (defvar org-complex-heading-regexp-format nil
  3747. "Printf format to make regexp to match an exact headline.
  3748. This regexp will match the headline of any node which has the
  3749. exact headline text that is put into the format, but may have any
  3750. TODO state, priority and tags.")
  3751. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3752. (defvar org-todo-line-tags-regexp nil
  3753. "Matches a headline and puts TODO state into group 2 if present.
  3754. Also put tags into group 4 if tags are present.")
  3755. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3756. (defvar org-ds-keyword-length 12
  3757. "Maximum length of the Deadline and SCHEDULED keywords.")
  3758. (make-variable-buffer-local 'org-ds-keyword-length)
  3759. (defvar org-deadline-regexp nil
  3760. "Matches the DEADLINE keyword.")
  3761. (make-variable-buffer-local 'org-deadline-regexp)
  3762. (defvar org-deadline-time-regexp nil
  3763. "Matches the DEADLINE keyword together with a time stamp.")
  3764. (make-variable-buffer-local 'org-deadline-time-regexp)
  3765. (defvar org-deadline-line-regexp nil
  3766. "Matches the DEADLINE keyword and the rest of the line.")
  3767. (make-variable-buffer-local 'org-deadline-line-regexp)
  3768. (defvar org-scheduled-regexp nil
  3769. "Matches the SCHEDULED keyword.")
  3770. (make-variable-buffer-local 'org-scheduled-regexp)
  3771. (defvar org-scheduled-time-regexp nil
  3772. "Matches the SCHEDULED keyword together with a time stamp.")
  3773. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3774. (defvar org-closed-time-regexp nil
  3775. "Matches the CLOSED keyword together with a time stamp.")
  3776. (make-variable-buffer-local 'org-closed-time-regexp)
  3777. (defvar org-keyword-time-regexp nil
  3778. "Matches any of the 4 keywords, together with the time stamp.")
  3779. (make-variable-buffer-local 'org-keyword-time-regexp)
  3780. (defvar org-keyword-time-not-clock-regexp nil
  3781. "Matches any of the 3 keywords, together with the time stamp.")
  3782. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3783. (defvar org-maybe-keyword-time-regexp nil
  3784. "Matches a timestamp, possibly preceded by a keyword.")
  3785. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3786. (defvar org-planning-or-clock-line-re nil
  3787. "Matches a line with planning or clock info.")
  3788. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3789. (defvar org-all-time-keywords nil
  3790. "List of time keywords.")
  3791. (make-variable-buffer-local 'org-all-time-keywords)
  3792. (defconst org-plain-time-of-day-regexp
  3793. (concat
  3794. "\\(\\<[012]?[0-9]"
  3795. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3796. "\\(--?"
  3797. "\\(\\<[012]?[0-9]"
  3798. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3799. "\\)?")
  3800. "Regular expression to match a plain time or time range.
  3801. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3802. groups carry important information:
  3803. 0 the full match
  3804. 1 the first time, range or not
  3805. 8 the second time, if it is a range.")
  3806. (defconst org-plain-time-extension-regexp
  3807. (concat
  3808. "\\(\\<[012]?[0-9]"
  3809. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3810. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3811. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3812. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3813. groups carry important information:
  3814. 0 the full match
  3815. 7 hours of duration
  3816. 9 minutes of duration")
  3817. (defconst org-stamp-time-of-day-regexp
  3818. (concat
  3819. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3820. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3821. "\\(--?"
  3822. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3823. "Regular expression to match a timestamp time or time range.
  3824. After a match, the following groups carry important information:
  3825. 0 the full match
  3826. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3827. 2 the first time, range or not
  3828. 4 the second time, if it is a range.")
  3829. (defconst org-startup-options
  3830. '(("fold" org-startup-folded t)
  3831. ("overview" org-startup-folded t)
  3832. ("nofold" org-startup-folded nil)
  3833. ("showall" org-startup-folded nil)
  3834. ("showeverything" org-startup-folded showeverything)
  3835. ("content" org-startup-folded content)
  3836. ("indent" org-startup-indented t)
  3837. ("noindent" org-startup-indented nil)
  3838. ("hidestars" org-hide-leading-stars t)
  3839. ("showstars" org-hide-leading-stars nil)
  3840. ("odd" org-odd-levels-only t)
  3841. ("oddeven" org-odd-levels-only nil)
  3842. ("align" org-startup-align-all-tables t)
  3843. ("noalign" org-startup-align-all-tables nil)
  3844. ("inlineimages" org-startup-with-inline-images t)
  3845. ("noinlineimages" org-startup-with-inline-images nil)
  3846. ("customtime" org-display-custom-times t)
  3847. ("logdone" org-log-done time)
  3848. ("lognotedone" org-log-done note)
  3849. ("nologdone" org-log-done nil)
  3850. ("lognoteclock-out" org-log-note-clock-out t)
  3851. ("nolognoteclock-out" org-log-note-clock-out nil)
  3852. ("logrepeat" org-log-repeat state)
  3853. ("lognoterepeat" org-log-repeat note)
  3854. ("nologrepeat" org-log-repeat nil)
  3855. ("logreschedule" org-log-reschedule time)
  3856. ("lognotereschedule" org-log-reschedule note)
  3857. ("nologreschedule" org-log-reschedule nil)
  3858. ("logredeadline" org-log-redeadline time)
  3859. ("lognoteredeadline" org-log-redeadline note)
  3860. ("nologredeadline" org-log-redeadline nil)
  3861. ("logrefile" org-log-refile time)
  3862. ("lognoterefile" org-log-refile note)
  3863. ("nologrefile" org-log-refile nil)
  3864. ("fninline" org-footnote-define-inline t)
  3865. ("nofninline" org-footnote-define-inline nil)
  3866. ("fnlocal" org-footnote-section nil)
  3867. ("fnauto" org-footnote-auto-label t)
  3868. ("fnprompt" org-footnote-auto-label nil)
  3869. ("fnconfirm" org-footnote-auto-label confirm)
  3870. ("fnplain" org-footnote-auto-label plain)
  3871. ("fnadjust" org-footnote-auto-adjust t)
  3872. ("nofnadjust" org-footnote-auto-adjust nil)
  3873. ("constcgs" constants-unit-system cgs)
  3874. ("constSI" constants-unit-system SI)
  3875. ("noptag" org-tag-persistent-alist nil)
  3876. ("hideblocks" org-hide-block-startup t)
  3877. ("nohideblocks" org-hide-block-startup nil)
  3878. ("beamer" org-startup-with-beamer-mode t)
  3879. ("entitiespretty" org-pretty-entities t)
  3880. ("entitiesplain" org-pretty-entities nil))
  3881. "Variable associated with STARTUP options for org-mode.
  3882. Each element is a list of three items: The startup options as written
  3883. in the #+STARTUP line, the corresponding variable, and the value to
  3884. set this variable to if the option is found. An optional forth element PUSH
  3885. means to push this value onto the list in the variable.")
  3886. (defun org-update-property-plist (key val props)
  3887. "Update PROPS with KEY and VAL."
  3888. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3889. (key (if appending (substring key 0 (- (length key) 1)) key))
  3890. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3891. (previous (cdr (assoc key props))))
  3892. (if appending
  3893. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3894. (cons (cons key val) remainder))))
  3895. (defconst org-block-regexp
  3896. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3897. "Regular expression for hiding blocks.")
  3898. (defconst org-heading-keyword-regexp-format
  3899. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3900. "Printf format for a regexp matching an headline with some keyword.
  3901. This regexp will match the headline of any node which has the
  3902. exact keyword that is put into the format. The keyword isn't in
  3903. any group by default, but the stars and the body are.")
  3904. (defconst org-heading-keyword-maybe-regexp-format
  3905. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3906. "Printf format for a regexp matching an headline, possibly with some keyword.
  3907. This regexp can match any headline with the specified keyword, or
  3908. without a keyword. The keyword isn't in any group by default,
  3909. but the stars and the body are.")
  3910. (defun org-set-regexps-and-options ()
  3911. "Precompute regular expressions for current buffer."
  3912. (when (eq major-mode 'org-mode)
  3913. (org-set-local 'org-todo-kwd-alist nil)
  3914. (org-set-local 'org-todo-key-alist nil)
  3915. (org-set-local 'org-todo-key-trigger nil)
  3916. (org-set-local 'org-todo-keywords-1 nil)
  3917. (org-set-local 'org-done-keywords nil)
  3918. (org-set-local 'org-todo-heads nil)
  3919. (org-set-local 'org-todo-sets nil)
  3920. (org-set-local 'org-todo-log-states nil)
  3921. (org-set-local 'org-file-properties nil)
  3922. (org-set-local 'org-file-tags nil)
  3923. (let ((re (org-make-options-regexp
  3924. '("CATEGORY" "TODO" "COLUMNS"
  3925. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3926. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3927. "OPTIONS")
  3928. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3929. (splitre "[ \t]+")
  3930. (scripts org-use-sub-superscripts)
  3931. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3932. tail sep kws1 prio props ftags drawers beamer-p
  3933. ext-setup-or-nil setup-contents (start 0))
  3934. (save-excursion
  3935. (save-restriction
  3936. (widen)
  3937. (goto-char (point-min))
  3938. (while (or (and ext-setup-or-nil
  3939. (string-match re ext-setup-or-nil start)
  3940. (setq start (match-end 0)))
  3941. (and (setq ext-setup-or-nil nil start 0)
  3942. (re-search-forward re nil t)))
  3943. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3944. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3945. (if (stringp value) (setq value (org-trim value)))
  3946. (cond
  3947. ((equal key "CATEGORY")
  3948. (setq cat value))
  3949. ((member key '("SEQ_TODO" "TODO"))
  3950. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3951. ((equal key "TYP_TODO")
  3952. (push (cons 'type (org-split-string value splitre)) kwds))
  3953. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3954. ;; general TODO-like setup
  3955. (push (cons (intern (downcase (match-string 1 key)))
  3956. (org-split-string value splitre)) kwds))
  3957. ((equal key "TAGS")
  3958. (setq tags (append tags (if tags '("\\n") nil)
  3959. (org-split-string value splitre))))
  3960. ((equal key "COLUMNS")
  3961. (org-set-local 'org-columns-default-format value))
  3962. ((equal key "LINK")
  3963. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3964. (push (cons (match-string 1 value)
  3965. (org-trim (match-string 2 value)))
  3966. links)))
  3967. ((equal key "PRIORITIES")
  3968. (setq prio (org-split-string value " +")))
  3969. ((equal key "PROPERTY")
  3970. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3971. (setq props (org-update-property-plist (match-string 1 value)
  3972. (match-string 2 value)
  3973. props))))
  3974. ((equal key "FILETAGS")
  3975. (when (string-match "\\S-" value)
  3976. (setq ftags
  3977. (append
  3978. ftags
  3979. (apply 'append
  3980. (mapcar (lambda (x) (org-split-string x ":"))
  3981. (org-split-string value)))))))
  3982. ((equal key "DRAWERS")
  3983. (setq drawers (org-split-string value splitre)))
  3984. ((equal key "CONSTANTS")
  3985. (setq const (append const (org-split-string value splitre))))
  3986. ((equal key "STARTUP")
  3987. (let ((opts (org-split-string value splitre))
  3988. l var val)
  3989. (while (setq l (pop opts))
  3990. (when (setq l (assoc l org-startup-options))
  3991. (setq var (nth 1 l) val (nth 2 l))
  3992. (if (not (nth 3 l))
  3993. (set (make-local-variable var) val)
  3994. (if (not (listp (symbol-value var)))
  3995. (set (make-local-variable var) nil))
  3996. (set (make-local-variable var) (symbol-value var))
  3997. (add-to-list var val))))))
  3998. ((equal key "ARCHIVE")
  3999. (setq arch value)
  4000. (remove-text-properties 0 (length arch)
  4001. '(face t fontified t) arch))
  4002. ((equal key "LATEX_CLASS")
  4003. (setq beamer-p (equal value "beamer")))
  4004. ((equal key "OPTIONS")
  4005. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4006. (setq scripts (read (match-string 2 value)))))
  4007. ((equal key "SETUPFILE")
  4008. (setq setup-contents (org-file-contents
  4009. (expand-file-name
  4010. (org-remove-double-quotes value))
  4011. 'noerror))
  4012. (if (not ext-setup-or-nil)
  4013. (setq ext-setup-or-nil setup-contents start 0)
  4014. (setq ext-setup-or-nil
  4015. (concat (substring ext-setup-or-nil 0 start)
  4016. "\n" setup-contents "\n"
  4017. (substring ext-setup-or-nil start)))))))
  4018. ;; search for property blocks
  4019. (goto-char (point-min))
  4020. (while (re-search-forward org-block-regexp nil t)
  4021. (when (equal "PROPERTY" (upcase (match-string 1)))
  4022. (setq value (replace-regexp-in-string
  4023. "[\n\r]" " " (match-string 4)))
  4024. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4025. (setq props (org-update-property-plist (match-string 1 value)
  4026. (match-string 2 value)
  4027. props)))))))
  4028. (org-set-local 'org-use-sub-superscripts scripts)
  4029. (when cat
  4030. (org-set-local 'org-category (intern cat))
  4031. (push (cons "CATEGORY" cat) props))
  4032. (when prio
  4033. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4034. (setq prio (mapcar 'string-to-char prio))
  4035. (org-set-local 'org-highest-priority (nth 0 prio))
  4036. (org-set-local 'org-lowest-priority (nth 1 prio))
  4037. (org-set-local 'org-default-priority (nth 2 prio)))
  4038. (and props (org-set-local 'org-file-properties (nreverse props)))
  4039. (and ftags (org-set-local 'org-file-tags
  4040. (mapcar 'org-add-prop-inherited ftags)))
  4041. (and drawers (org-set-local 'org-drawers drawers))
  4042. (and arch (org-set-local 'org-archive-location arch))
  4043. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4044. ;; Process the TODO keywords
  4045. (unless kwds
  4046. ;; Use the global values as if they had been given locally.
  4047. (setq kwds (default-value 'org-todo-keywords))
  4048. (if (stringp (car kwds))
  4049. (setq kwds (list (cons org-todo-interpretation
  4050. (default-value 'org-todo-keywords)))))
  4051. (setq kwds (reverse kwds)))
  4052. (setq kwds (nreverse kwds))
  4053. (let (inter kws kw)
  4054. (while (setq kws (pop kwds))
  4055. (let ((kws (or
  4056. (run-hook-with-args-until-success
  4057. 'org-todo-setup-filter-hook kws)
  4058. kws)))
  4059. (setq inter (pop kws) sep (member "|" kws)
  4060. kws0 (delete "|" (copy-sequence kws))
  4061. kwsa nil
  4062. kws1 (mapcar
  4063. (lambda (x)
  4064. ;; 1 2
  4065. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4066. (progn
  4067. (setq kw (match-string 1 x)
  4068. key (and (match-end 2) (match-string 2 x))
  4069. log (org-extract-log-state-settings x))
  4070. (push (cons kw (and key (string-to-char key))) kwsa)
  4071. (and log (push log org-todo-log-states))
  4072. kw)
  4073. (error "Invalid TODO keyword %s" x)))
  4074. kws0)
  4075. kwsa (if kwsa (append '((:startgroup))
  4076. (nreverse kwsa)
  4077. '((:endgroup))))
  4078. hw (car kws1)
  4079. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4080. tail (list inter hw (car dws) (org-last dws))))
  4081. (add-to-list 'org-todo-heads hw 'append)
  4082. (push kws1 org-todo-sets)
  4083. (setq org-done-keywords (append org-done-keywords dws nil))
  4084. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4085. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4086. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4087. (setq org-todo-sets (nreverse org-todo-sets)
  4088. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4089. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4090. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4091. ;; Process the constants
  4092. (when const
  4093. (let (e cst)
  4094. (while (setq e (pop const))
  4095. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4096. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4097. (setq org-table-formula-constants-local cst)))
  4098. ;; Process the tags.
  4099. (when tags
  4100. (let (e tgs)
  4101. (while (setq e (pop tags))
  4102. (cond
  4103. ((equal e "{") (push '(:startgroup) tgs))
  4104. ((equal e "}") (push '(:endgroup) tgs))
  4105. ((equal e "\\n") (push '(:newline) tgs))
  4106. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4107. (push (cons (match-string 1 e)
  4108. (string-to-char (match-string 2 e)))
  4109. tgs))
  4110. (t (push (list e) tgs))))
  4111. (org-set-local 'org-tag-alist nil)
  4112. (while (setq e (pop tgs))
  4113. (or (and (stringp (car e))
  4114. (assoc (car e) org-tag-alist))
  4115. (push e org-tag-alist)))))
  4116. ;; Compute the regular expressions and other local variables.
  4117. ;; Using `org-outline-regexp-bol' would complicate them much,
  4118. ;; because of the fixed white space at the end of that string.
  4119. (if (not org-done-keywords)
  4120. (setq org-done-keywords (and org-todo-keywords-1
  4121. (list (org-last org-todo-keywords-1)))))
  4122. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4123. (length org-scheduled-string)
  4124. (length org-clock-string)
  4125. (length org-closed-string)))
  4126. org-drawer-regexp
  4127. (concat "^[ \t]*:\\("
  4128. (mapconcat 'regexp-quote org-drawers "\\|")
  4129. "\\):[ \t]*$")
  4130. org-not-done-keywords
  4131. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4132. org-todo-regexp
  4133. (concat "\\("
  4134. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4135. "\\)")
  4136. org-not-done-regexp
  4137. (concat "\\("
  4138. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4139. "\\)")
  4140. org-not-done-heading-regexp
  4141. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4142. org-todo-line-regexp
  4143. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4144. org-complex-heading-regexp
  4145. (concat "^\\(\\*+\\)"
  4146. "\\(?: +" org-todo-regexp "\\)?"
  4147. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4148. "\\(?: +\\(.*?\\)\\)?"
  4149. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4150. "[ \t]*$")
  4151. org-complex-heading-regexp-format
  4152. (concat "^\\(\\*+\\)"
  4153. "\\(?: +" org-todo-regexp "\\)?"
  4154. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4155. "\\(?: +"
  4156. ;; Stats cookies can be stuck to body.
  4157. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4158. "\\(%s\\)"
  4159. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4160. "\\)"
  4161. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4162. "[ \t]*$")
  4163. org-todo-line-tags-regexp
  4164. (concat "^\\(\\*+\\)"
  4165. "\\(?: +" org-todo-regexp "\\)?"
  4166. "\\(?: +\\(.*?\\)\\)?"
  4167. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4168. "[ \t]*$")
  4169. org-deadline-regexp (concat "\\<" org-deadline-string)
  4170. org-deadline-time-regexp
  4171. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4172. org-deadline-line-regexp
  4173. (concat "\\<\\(" org-deadline-string "\\).*")
  4174. org-scheduled-regexp
  4175. (concat "\\<" org-scheduled-string)
  4176. org-scheduled-time-regexp
  4177. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4178. org-closed-time-regexp
  4179. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4180. org-keyword-time-regexp
  4181. (concat "\\<\\(" org-scheduled-string
  4182. "\\|" org-deadline-string
  4183. "\\|" org-closed-string
  4184. "\\|" org-clock-string "\\)"
  4185. " *[[<]\\([^]>]+\\)[]>]")
  4186. org-keyword-time-not-clock-regexp
  4187. (concat "\\<\\(" org-scheduled-string
  4188. "\\|" org-deadline-string
  4189. "\\|" org-closed-string
  4190. "\\)"
  4191. " *[[<]\\([^]>]+\\)[]>]")
  4192. org-maybe-keyword-time-regexp
  4193. (concat "\\(\\<\\(" org-scheduled-string
  4194. "\\|" org-deadline-string
  4195. "\\|" org-closed-string
  4196. "\\|" org-clock-string "\\)\\)?"
  4197. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4198. org-planning-or-clock-line-re
  4199. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4200. "\\|" org-deadline-string
  4201. "\\|" org-closed-string "\\|" org-clock-string
  4202. "\\)\\>\\)")
  4203. org-all-time-keywords
  4204. (mapcar (lambda (w) (substring w 0 -1))
  4205. (list org-scheduled-string org-deadline-string
  4206. org-clock-string org-closed-string))
  4207. )
  4208. (org-compute-latex-and-specials-regexp)
  4209. (org-set-font-lock-defaults))))
  4210. (defun org-file-contents (file &optional noerror)
  4211. "Return the contents of FILE, as a string."
  4212. (if (or (not file)
  4213. (not (file-readable-p file)))
  4214. (if noerror
  4215. (progn
  4216. (message "Cannot read file \"%s\"" file)
  4217. (ding) (sit-for 2)
  4218. "")
  4219. (error "Cannot read file \"%s\"" file))
  4220. (with-temp-buffer
  4221. (insert-file-contents file)
  4222. (buffer-string))))
  4223. (defun org-extract-log-state-settings (x)
  4224. "Extract the log state setting from a TODO keyword string.
  4225. This will extract info from a string like \"WAIT(w@/!)\"."
  4226. (let (kw key log1 log2)
  4227. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4228. (setq kw (match-string 1 x)
  4229. key (and (match-end 2) (match-string 2 x))
  4230. log1 (and (match-end 3) (match-string 3 x))
  4231. log2 (and (match-end 4) (match-string 4 x)))
  4232. (and (or log1 log2)
  4233. (list kw
  4234. (and log1 (if (equal log1 "!") 'time 'note))
  4235. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4236. (defun org-remove-keyword-keys (list)
  4237. "Remove a pair of parenthesis at the end of each string in LIST."
  4238. (mapcar (lambda (x)
  4239. (if (string-match "(.*)$" x)
  4240. (substring x 0 (match-beginning 0))
  4241. x))
  4242. list))
  4243. (defun org-assign-fast-keys (alist)
  4244. "Assign fast keys to a keyword-key alist.
  4245. Respect keys that are already there."
  4246. (let (new e (alt ?0))
  4247. (while (setq e (pop alist))
  4248. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4249. (cdr e)) ;; Key already assigned.
  4250. (push e new)
  4251. (let ((clist (string-to-list (downcase (car e))))
  4252. (used (append new alist)))
  4253. (when (= (car clist) ?@)
  4254. (pop clist))
  4255. (while (and clist (rassoc (car clist) used))
  4256. (pop clist))
  4257. (unless clist
  4258. (while (rassoc alt used)
  4259. (incf alt)))
  4260. (push (cons (car e) (or (car clist) alt)) new))))
  4261. (nreverse new)))
  4262. ;;; Some variables used in various places
  4263. (defvar org-window-configuration nil
  4264. "Used in various places to store a window configuration.")
  4265. (defvar org-selected-window nil
  4266. "Used in various places to store a window configuration.")
  4267. (defvar org-finish-function nil
  4268. "Function to be called when `C-c C-c' is used.
  4269. This is for getting out of special buffers like remember.")
  4270. ;; FIXME: Occasionally check by commenting these, to make sure
  4271. ;; no other functions uses these, forgetting to let-bind them.
  4272. (defvar entry)
  4273. (defvar last-state)
  4274. (defvar date)
  4275. ;; Defined somewhere in this file, but used before definition.
  4276. (defvar org-entities) ;; defined in org-entities.el
  4277. (defvar org-struct-menu)
  4278. (defvar org-org-menu)
  4279. (defvar org-tbl-menu)
  4280. ;;;; Define the Org-mode
  4281. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4282. (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"))
  4283. ;; We use a before-change function to check if a table might need
  4284. ;; an update.
  4285. (defvar org-table-may-need-update t
  4286. "Indicates that a table might need an update.
  4287. This variable is set by `org-before-change-function'.
  4288. `org-table-align' sets it back to nil.")
  4289. (defun org-before-change-function (beg end)
  4290. "Every change indicates that a table might need an update."
  4291. (setq org-table-may-need-update t))
  4292. (defvar org-mode-map)
  4293. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4294. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4295. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4296. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4297. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4298. (defvar org-table-buffer-is-an nil)
  4299. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4300. ;; some places -- e.g. see the macro org-with-limited-levels.
  4301. ;;
  4302. ;; In Org buffers, the value of `outline-regexp' is that of
  4303. ;; `org-outline-regexp'. The only function still directly relying on
  4304. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4305. ;; job when `orgstruct-mode' is active.
  4306. (defvar org-outline-regexp "\\*+ "
  4307. "Regexp to match Org headlines.")
  4308. (defconst org-outline-regexp-bol "^\\*+ "
  4309. "Regexp to match Org headlines.
  4310. This is similar to `org-outline-regexp' but additionally makes
  4311. sure that we are at the beginning of the line.")
  4312. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4313. "Matches an headline, putting stars and text into groups.
  4314. Stars are put in group 1 and the trimmed body in group 2.")
  4315. ;;;###autoload
  4316. (define-derived-mode org-mode outline-mode "Org"
  4317. "Outline-based notes management and organizer, alias
  4318. \"Carsten's outline-mode for keeping track of everything.\"
  4319. Org-mode develops organizational tasks around a NOTES file which
  4320. contains information about projects as plain text. Org-mode is
  4321. implemented on top of outline-mode, which is ideal to keep the content
  4322. of large files well structured. It supports ToDo items, deadlines and
  4323. time stamps, which magically appear in the diary listing of the Emacs
  4324. calendar. Tables are easily created with a built-in table editor.
  4325. Plain text URL-like links connect to websites, emails (VM), Usenet
  4326. messages (Gnus), BBDB entries, and any files related to the project.
  4327. For printing and sharing of notes, an Org-mode file (or a part of it)
  4328. can be exported as a structured ASCII or HTML file.
  4329. The following commands are available:
  4330. \\{org-mode-map}"
  4331. ;; Get rid of Outline menus, they are not needed
  4332. ;; Need to do this here because define-derived-mode sets up
  4333. ;; the keymap so late. Still, it is a waste to call this each time
  4334. ;; we switch another buffer into org-mode.
  4335. (if (featurep 'xemacs)
  4336. (when (boundp 'outline-mode-menu-heading)
  4337. ;; Assume this is Greg's port, it uses easymenu
  4338. (easy-menu-remove outline-mode-menu-heading)
  4339. (easy-menu-remove outline-mode-menu-show)
  4340. (easy-menu-remove outline-mode-menu-hide))
  4341. (define-key org-mode-map [menu-bar headings] 'undefined)
  4342. (define-key org-mode-map [menu-bar hide] 'undefined)
  4343. (define-key org-mode-map [menu-bar show] 'undefined))
  4344. (org-load-modules-maybe)
  4345. (easy-menu-add org-org-menu)
  4346. (easy-menu-add org-tbl-menu)
  4347. (org-install-agenda-files-menu)
  4348. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4349. (add-to-invisibility-spec '(org-cwidth))
  4350. (add-to-invisibility-spec '(org-hide-block . t))
  4351. (when (featurep 'xemacs)
  4352. (org-set-local 'line-move-ignore-invisible t))
  4353. (org-set-local 'outline-regexp org-outline-regexp)
  4354. (org-set-local 'outline-level 'org-outline-level)
  4355. (setq bidi-paragraph-direction 'left-to-right)
  4356. (when (and org-ellipsis
  4357. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4358. (fboundp 'make-glyph-code))
  4359. (unless org-display-table
  4360. (setq org-display-table (make-display-table)))
  4361. (set-display-table-slot
  4362. org-display-table 4
  4363. (vconcat (mapcar
  4364. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4365. org-ellipsis)))
  4366. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4367. (setq buffer-display-table org-display-table))
  4368. (org-set-regexps-and-options)
  4369. (when (and org-tag-faces (not org-tags-special-faces-re))
  4370. ;; tag faces set outside customize.... force initialization.
  4371. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4372. ;; Calc embedded
  4373. (org-set-local 'calc-embedded-open-mode "# ")
  4374. (modify-syntax-entry ?@ "w")
  4375. (if org-startup-truncated (setq truncate-lines t))
  4376. (org-set-local 'font-lock-unfontify-region-function
  4377. 'org-unfontify-region)
  4378. ;; Activate before-change-function
  4379. (org-set-local 'org-table-may-need-update t)
  4380. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4381. 'local)
  4382. ;; Check for running clock before killing a buffer
  4383. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4384. ;; Paragraphs and auto-filling
  4385. (org-set-autofill-regexps)
  4386. (setq indent-line-function 'org-indent-line-function)
  4387. (org-update-radio-target-regexp)
  4388. ;; Beginning/end of defun
  4389. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4390. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4391. ;; Next error for sparse trees
  4392. (org-set-local 'next-error-function 'org-occur-next-match)
  4393. ;; Make sure dependence stuff works reliably, even for users who set it
  4394. ;; too late :-(
  4395. (if org-enforce-todo-dependencies
  4396. (add-hook 'org-blocker-hook
  4397. 'org-block-todo-from-children-or-siblings-or-parent)
  4398. (remove-hook 'org-blocker-hook
  4399. 'org-block-todo-from-children-or-siblings-or-parent))
  4400. (if org-enforce-todo-checkbox-dependencies
  4401. (add-hook 'org-blocker-hook
  4402. 'org-block-todo-from-checkboxes)
  4403. (remove-hook 'org-blocker-hook
  4404. 'org-block-todo-from-checkboxes))
  4405. ;; Comment characters
  4406. (org-set-local 'comment-start "#")
  4407. (org-set-local 'comment-padding " ")
  4408. ;; Align options lines
  4409. (org-set-local
  4410. 'align-mode-rules-list
  4411. '((org-in-buffer-settings
  4412. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4413. (modes . '(org-mode)))))
  4414. ;; Imenu
  4415. (org-set-local 'imenu-create-index-function
  4416. 'org-imenu-get-tree)
  4417. ;; Make isearch reveal context
  4418. (if (or (featurep 'xemacs)
  4419. (not (boundp 'outline-isearch-open-invisible-function)))
  4420. ;; Emacs 21 and XEmacs make use of the hook
  4421. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4422. ;; Emacs 22 deals with this through a special variable
  4423. (org-set-local 'outline-isearch-open-invisible-function
  4424. (lambda (&rest ignore) (org-show-context 'isearch))))
  4425. ;; Turn on org-beamer-mode?
  4426. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4427. ;; Setup the pcomplete hooks
  4428. (set (make-local-variable 'pcomplete-command-completion-function)
  4429. 'org-pcomplete-initial)
  4430. (set (make-local-variable 'pcomplete-command-name-function)
  4431. 'org-command-at-point)
  4432. (set (make-local-variable 'pcomplete-default-completion-function)
  4433. 'ignore)
  4434. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4435. 'org-parse-arguments)
  4436. (set (make-local-variable 'pcomplete-termination-string) "")
  4437. (set (make-local-variable 'face-remapping-alist)
  4438. '((default org-default)))
  4439. ;; If empty file that did not turn on org-mode automatically, make it to.
  4440. (if (and org-insert-mode-line-in-empty-file
  4441. (org-called-interactively-p 'any)
  4442. (= (point-min) (point-max)))
  4443. (insert "# -*- mode: org -*-\n\n"))
  4444. (unless org-inhibit-startup
  4445. (when org-startup-align-all-tables
  4446. (let ((bmp (buffer-modified-p)))
  4447. (org-table-map-tables 'org-table-align 'quietly)
  4448. (set-buffer-modified-p bmp)))
  4449. (when org-startup-with-inline-images
  4450. (org-display-inline-images))
  4451. (when org-startup-indented
  4452. (require 'org-indent)
  4453. (org-indent-mode 1))
  4454. (unless org-inhibit-startup-visibility-stuff
  4455. (org-set-startup-visibility))))
  4456. (when (fboundp 'abbrev-table-put)
  4457. (abbrev-table-put org-mode-abbrev-table
  4458. :parents (list text-mode-abbrev-table)))
  4459. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4460. (defun org-current-time ()
  4461. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4462. (if (> (car org-time-stamp-rounding-minutes) 1)
  4463. (let ((r (car org-time-stamp-rounding-minutes))
  4464. (time (decode-time)))
  4465. (apply 'encode-time
  4466. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4467. (nthcdr 2 time))))
  4468. (current-time)))
  4469. (defun org-today ()
  4470. "Return today date, considering `org-extend-today-until'."
  4471. (time-to-days
  4472. (time-subtract (current-time)
  4473. (list 0 (* 3600 org-extend-today-until) 0))))
  4474. ;;;; Font-Lock stuff, including the activators
  4475. (defvar org-mouse-map (make-sparse-keymap))
  4476. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4477. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4478. (when org-mouse-1-follows-link
  4479. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4480. (when org-tab-follows-link
  4481. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4482. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4483. (require 'font-lock)
  4484. (defconst org-non-link-chars "]\t\n\r<>")
  4485. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4486. "shell" "elisp" "doi" "message"))
  4487. (defvar org-link-types-re nil
  4488. "Matches a link that has a url-like prefix like \"http:\"")
  4489. (defvar org-link-re-with-space nil
  4490. "Matches a link with spaces, optional angular brackets around it.")
  4491. (defvar org-link-re-with-space2 nil
  4492. "Matches a link with spaces, optional angular brackets around it.")
  4493. (defvar org-link-re-with-space3 nil
  4494. "Matches a link with spaces, only for internal part in bracket links.")
  4495. (defvar org-angle-link-re nil
  4496. "Matches link with angular brackets, spaces are allowed.")
  4497. (defvar org-plain-link-re nil
  4498. "Matches plain link, without spaces.")
  4499. (defvar org-bracket-link-regexp nil
  4500. "Matches a link in double brackets.")
  4501. (defvar org-bracket-link-analytic-regexp nil
  4502. "Regular expression used to analyze links.
  4503. Here is what the match groups contain after a match:
  4504. 1: http:
  4505. 2: http
  4506. 3: path
  4507. 4: [desc]
  4508. 5: desc")
  4509. (defvar org-bracket-link-analytic-regexp++ nil
  4510. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4511. (defvar org-any-link-re nil
  4512. "Regular expression matching any link.")
  4513. (defcustom org-match-sexp-depth 3
  4514. "Number of stacked braces for sub/superscript matching.
  4515. This has to be set before loading org.el to be effective."
  4516. :group 'org-export-translation ; ??????????????????????????/
  4517. :type 'integer)
  4518. (defun org-create-multibrace-regexp (left right n)
  4519. "Create a regular expression which will match a balanced sexp.
  4520. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4521. as single character strings.
  4522. The regexp returned will match the entire expression including the
  4523. delimiters. It will also define a single group which contains the
  4524. match except for the outermost delimiters. The maximum depth of
  4525. stacked delimiters is N. Escaping delimiters is not possible."
  4526. (let* ((nothing (concat "[^" left right "]*?"))
  4527. (or "\\|")
  4528. (re nothing)
  4529. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4530. (while (> n 1)
  4531. (setq n (1- n)
  4532. re (concat re or next)
  4533. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4534. (concat left "\\(" re "\\)" right)))
  4535. (defvar org-match-substring-regexp
  4536. (concat
  4537. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4538. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4539. "\\|"
  4540. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4541. "\\|"
  4542. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4543. "The regular expression matching a sub- or superscript.")
  4544. (defvar org-match-substring-with-braces-regexp
  4545. (concat
  4546. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4547. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4548. "\\)")
  4549. "The regular expression matching a sub- or superscript, forcing braces.")
  4550. (defun org-make-link-regexps ()
  4551. "Update the link regular expressions.
  4552. This should be called after the variable `org-link-types' has changed."
  4553. (setq org-link-types-re
  4554. (concat
  4555. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4556. org-link-re-with-space
  4557. (concat
  4558. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4559. "\\([^" org-non-link-chars " ]"
  4560. "[^" org-non-link-chars "]*"
  4561. "[^" org-non-link-chars " ]\\)>?")
  4562. org-link-re-with-space2
  4563. (concat
  4564. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4565. "\\([^" org-non-link-chars " ]"
  4566. "[^\t\n\r]*"
  4567. "[^" org-non-link-chars " ]\\)>?")
  4568. org-link-re-with-space3
  4569. (concat
  4570. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4571. "\\([^" org-non-link-chars " ]"
  4572. "[^\t\n\r]*\\)")
  4573. org-angle-link-re
  4574. (concat
  4575. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4576. "\\([^" org-non-link-chars " ]"
  4577. "[^" org-non-link-chars "]*"
  4578. "\\)>")
  4579. org-plain-link-re
  4580. (concat
  4581. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4582. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4583. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4584. org-bracket-link-regexp
  4585. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4586. org-bracket-link-analytic-regexp
  4587. (concat
  4588. "\\[\\["
  4589. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4590. "\\([^]]+\\)"
  4591. "\\]"
  4592. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4593. "\\]")
  4594. org-bracket-link-analytic-regexp++
  4595. (concat
  4596. "\\[\\["
  4597. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4598. "\\([^]]+\\)"
  4599. "\\]"
  4600. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4601. "\\]")
  4602. org-any-link-re
  4603. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4604. org-angle-link-re "\\)\\|\\("
  4605. org-plain-link-re "\\)")))
  4606. (org-make-link-regexps)
  4607. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4608. "Regular expression for fast time stamp matching.")
  4609. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4610. "Regular expression for fast time stamp matching.")
  4611. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4612. "Regular expression matching time strings for analysis.
  4613. This one does not require the space after the date, so it can be used
  4614. on a string that terminates immediately after the date.")
  4615. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4616. "Regular expression matching time strings for analysis.")
  4617. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4618. "Regular expression matching time stamps, with groups.")
  4619. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4620. "Regular expression matching time stamps (also [..]), with groups.")
  4621. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4622. "Regular expression matching a time stamp range.")
  4623. (defconst org-tr-regexp-both
  4624. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4625. "Regular expression matching a time stamp range.")
  4626. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4627. org-ts-regexp "\\)?")
  4628. "Regular expression matching a time stamp or time stamp range.")
  4629. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4630. org-ts-regexp-both "\\)?")
  4631. "Regular expression matching a time stamp or time stamp range.
  4632. The time stamps may be either active or inactive.")
  4633. (defvar org-emph-face nil)
  4634. (defun org-do-emphasis-faces (limit)
  4635. "Run through the buffer and add overlays to emphasized strings."
  4636. (let (rtn a)
  4637. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4638. (if (not (= (char-after (match-beginning 3))
  4639. (char-after (match-beginning 4))))
  4640. (progn
  4641. (setq rtn t)
  4642. (setq a (assoc (match-string 3) org-emphasis-alist))
  4643. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4644. 'face
  4645. (nth 1 a))
  4646. (and (nth 4 a)
  4647. (org-remove-flyspell-overlays-in
  4648. (match-beginning 0) (match-end 0)))
  4649. (add-text-properties (match-beginning 2) (match-end 2)
  4650. '(font-lock-multiline t org-emphasis t))
  4651. (when org-hide-emphasis-markers
  4652. (add-text-properties (match-end 4) (match-beginning 5)
  4653. '(invisible org-link))
  4654. (add-text-properties (match-beginning 3) (match-end 3)
  4655. '(invisible org-link)))))
  4656. (backward-char 1))
  4657. rtn))
  4658. (defun org-emphasize (&optional char)
  4659. "Insert or change an emphasis, i.e. a font like bold or italic.
  4660. If there is an active region, change that region to a new emphasis.
  4661. If there is no region, just insert the marker characters and position
  4662. the cursor between them.
  4663. CHAR should be either the marker character, or the first character of the
  4664. HTML tag associated with that emphasis. If CHAR is a space, the means
  4665. to remove the emphasis of the selected region.
  4666. If char is not given (for example in an interactive call) it
  4667. will be prompted for."
  4668. (interactive)
  4669. (let ((eal org-emphasis-alist) e det
  4670. (erc org-emphasis-regexp-components)
  4671. (prompt "")
  4672. (string "") beg end move tag c s)
  4673. (if (org-region-active-p)
  4674. (setq beg (region-beginning) end (region-end)
  4675. string (buffer-substring beg end))
  4676. (setq move t))
  4677. (while (setq e (pop eal))
  4678. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4679. c (aref tag 0))
  4680. (push (cons c (string-to-char (car e))) det)
  4681. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4682. (substring tag 1)))))
  4683. (setq det (nreverse det))
  4684. (unless char
  4685. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4686. (setq char (read-char-exclusive)))
  4687. (setq char (or (cdr (assoc char det)) char))
  4688. (if (equal char ?\ )
  4689. (setq s "" move nil)
  4690. (unless (assoc (char-to-string char) org-emphasis-alist)
  4691. (error "No such emphasis marker: \"%c\"" char))
  4692. (setq s (char-to-string char)))
  4693. (while (and (> (length string) 1)
  4694. (equal (substring string 0 1) (substring string -1))
  4695. (assoc (substring string 0 1) org-emphasis-alist))
  4696. (setq string (substring string 1 -1)))
  4697. (setq string (concat s string s))
  4698. (if beg (delete-region beg end))
  4699. (unless (or (bolp)
  4700. (string-match (concat "[" (nth 0 erc) "\n]")
  4701. (char-to-string (char-before (point)))))
  4702. (insert " "))
  4703. (unless (or (eobp)
  4704. (string-match (concat "[" (nth 1 erc) "\n]")
  4705. (char-to-string (char-after (point)))))
  4706. (insert " ") (backward-char 1))
  4707. (insert string)
  4708. (and move (backward-char 1))))
  4709. (defconst org-nonsticky-props
  4710. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4711. (defsubst org-rear-nonsticky-at (pos)
  4712. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4713. (defun org-activate-plain-links (limit)
  4714. "Run through the buffer and add overlays to links."
  4715. (catch 'exit
  4716. (let (f)
  4717. (if (re-search-forward org-plain-link-re limit t)
  4718. (progn
  4719. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4720. (setq f (get-text-property (match-beginning 0) 'face))
  4721. (if (or (eq f 'org-tag)
  4722. (and (listp f) (memq 'org-tag f)))
  4723. nil
  4724. (add-text-properties (match-beginning 0) (match-end 0)
  4725. (list 'mouse-face 'highlight
  4726. 'face 'org-link
  4727. 'keymap org-mouse-map))
  4728. (org-rear-nonsticky-at (match-end 0)))
  4729. t)))))
  4730. (defun org-activate-code (limit)
  4731. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4732. (progn
  4733. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4734. (remove-text-properties (match-beginning 0) (match-end 0)
  4735. '(display t invisible t intangible t))
  4736. t)))
  4737. (defcustom org-src-fontify-natively nil
  4738. "When non-nil, fontify code in code blocks."
  4739. :type 'boolean
  4740. :group 'org-appearance
  4741. :group 'org-babel)
  4742. (defun org-fontify-meta-lines-and-blocks (limit)
  4743. (condition-case nil
  4744. (org-fontify-meta-lines-and-blocks-1 limit)
  4745. (error (message "org-mode fontification error"))))
  4746. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4747. "Fontify #+ lines and blocks, in the correct ways."
  4748. (let ((case-fold-search t))
  4749. (if (re-search-forward
  4750. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4751. limit t)
  4752. (let ((beg (match-beginning 0))
  4753. (block-start (match-end 0))
  4754. (block-end nil)
  4755. (lang (match-string 7))
  4756. (beg1 (line-beginning-position 2))
  4757. (dc1 (downcase (match-string 2)))
  4758. (dc3 (downcase (match-string 3)))
  4759. end end1 quoting block-type ovl)
  4760. (cond
  4761. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4762. ;; a single line of backend-specific content
  4763. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4764. (remove-text-properties (match-beginning 0) (match-end 0)
  4765. '(display t invisible t intangible t))
  4766. (add-text-properties (match-beginning 1) (match-end 3)
  4767. '(font-lock-fontified t face org-meta-line))
  4768. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4769. '(font-lock-fontified t face org-block))
  4770. ; for backend-specific code
  4771. t)
  4772. ((and (match-end 4) (equal dc3 "begin"))
  4773. ;; Truly a block
  4774. (setq block-type (downcase (match-string 5))
  4775. quoting (member block-type org-protecting-blocks))
  4776. (when (re-search-forward
  4777. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4778. nil t) ;; on purpose, we look further than LIMIT
  4779. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4780. (setq block-end (match-beginning 0))
  4781. (when quoting
  4782. (remove-text-properties beg end
  4783. '(display t invisible t intangible t)))
  4784. (add-text-properties
  4785. beg end
  4786. '(font-lock-fontified t font-lock-multiline t))
  4787. (add-text-properties beg beg1 '(face org-meta-line))
  4788. (add-text-properties end1 (min (point-max) (1+ end))
  4789. '(face org-meta-line)) ; for end_src
  4790. (cond
  4791. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4792. (org-src-font-lock-fontify-block lang block-start block-end)
  4793. ;; remove old background overlays
  4794. (mapc (lambda (ov)
  4795. (if (eq (overlay-get ov 'face) 'org-block-background)
  4796. (delete-overlay ov)))
  4797. (overlays-at (/ (+ beg1 block-end) 2)))
  4798. ;; add a background overlay
  4799. (setq ovl (make-overlay beg1 block-end))
  4800. (overlay-put ovl 'face 'org-block-background)
  4801. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4802. (quoting
  4803. (add-text-properties beg1 (min (point-max) (1+ end1))
  4804. '(face org-block))) ; end of source block
  4805. ((not org-fontify-quote-and-verse-blocks))
  4806. ((string= block-type "quote")
  4807. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4808. ((string= block-type "verse")
  4809. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4810. (add-text-properties beg beg1 '(face org-block-begin-line))
  4811. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4812. t))
  4813. ((member dc1 '("title:" "author:" "email:" "date:"))
  4814. (add-text-properties
  4815. beg (match-end 3)
  4816. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4817. '(font-lock-fontified t invisible t)
  4818. '(font-lock-fontified t face org-document-info-keyword)))
  4819. (add-text-properties
  4820. (match-beginning 6) (match-end 6)
  4821. (if (string-equal dc1 "title:")
  4822. '(font-lock-fontified t face org-document-title)
  4823. '(font-lock-fontified t face org-document-info))))
  4824. ((not (member (char-after beg) '(?\ ?\t)))
  4825. ;; just any other in-buffer setting, but not indented
  4826. (add-text-properties
  4827. beg (1+ (match-end 0))
  4828. '(font-lock-fontified t face org-meta-line))
  4829. t)
  4830. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4831. "orgtbl:" "tblfm:" "tblname:" "results:"
  4832. "call:" "header:" "headers:" "name:"))
  4833. (and (match-end 4) (equal dc3 "attr")))
  4834. (add-text-properties
  4835. beg (match-end 0)
  4836. '(font-lock-fontified t face org-meta-line))
  4837. t)
  4838. ((member dc3 '(" " ""))
  4839. (add-text-properties
  4840. beg (match-end 0)
  4841. '(font-lock-fontified t face font-lock-comment-face)))
  4842. (t nil))))))
  4843. (defun org-strip-protective-commas (beg end)
  4844. "Strip protective commas between BEG and END in the current buffer."
  4845. (interactive "r")
  4846. (save-excursion
  4847. (save-match-data
  4848. (goto-char beg)
  4849. (let ((front-line (save-excursion
  4850. (re-search-forward
  4851. "[^[:space:]]" end t)
  4852. (goto-char (match-beginning 0))
  4853. (current-column))))
  4854. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4855. (goto-char (match-beginning 1))
  4856. (when (= (current-column) front-line)
  4857. (replace-match "" nil nil nil 1)))))))
  4858. (defun org-activate-angle-links (limit)
  4859. "Run through the buffer and add overlays to links."
  4860. (if (re-search-forward org-angle-link-re limit t)
  4861. (progn
  4862. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4863. (add-text-properties (match-beginning 0) (match-end 0)
  4864. (list 'mouse-face 'highlight
  4865. 'keymap org-mouse-map))
  4866. (org-rear-nonsticky-at (match-end 0))
  4867. t)))
  4868. (defun org-activate-footnote-links (limit)
  4869. "Run through the buffer and add overlays to footnotes."
  4870. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4871. (when fn
  4872. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4873. (org-remove-flyspell-overlays-in beg end)
  4874. (add-text-properties beg end
  4875. (list 'mouse-face 'highlight
  4876. 'keymap org-mouse-map
  4877. 'help-echo
  4878. (if (= (point-at-bol) beg)
  4879. "Footnote definition"
  4880. "Footnote reference")
  4881. 'font-lock-fontified t
  4882. 'font-lock-multiline t
  4883. 'face 'org-footnote))))))
  4884. (defun org-activate-bracket-links (limit)
  4885. "Run through the buffer and add overlays to bracketed links."
  4886. (if (re-search-forward org-bracket-link-regexp limit t)
  4887. (let* ((help (concat "LINK: "
  4888. (org-match-string-no-properties 1)))
  4889. ;; FIXME: above we should remove the escapes.
  4890. ;; but that requires another match, protecting match data,
  4891. ;; a lot of overhead for font-lock.
  4892. (ip (org-maybe-intangible
  4893. (list 'invisible 'org-link
  4894. 'keymap org-mouse-map 'mouse-face 'highlight
  4895. 'font-lock-multiline t 'help-echo help)))
  4896. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4897. 'font-lock-multiline t 'help-echo help)))
  4898. ;; We need to remove the invisible property here. Table narrowing
  4899. ;; may have made some of this invisible.
  4900. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4901. (remove-text-properties (match-beginning 0) (match-end 0)
  4902. '(invisible nil))
  4903. (if (match-end 3)
  4904. (progn
  4905. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4906. (org-rear-nonsticky-at (match-beginning 3))
  4907. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4908. (org-rear-nonsticky-at (match-end 3))
  4909. (add-text-properties (match-end 3) (match-end 0) ip)
  4910. (org-rear-nonsticky-at (match-end 0)))
  4911. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4912. (org-rear-nonsticky-at (match-beginning 1))
  4913. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4914. (org-rear-nonsticky-at (match-end 1))
  4915. (add-text-properties (match-end 1) (match-end 0) ip)
  4916. (org-rear-nonsticky-at (match-end 0)))
  4917. t)))
  4918. (defun org-activate-dates (limit)
  4919. "Run through the buffer and add overlays to dates."
  4920. (if (re-search-forward org-tsr-regexp-both limit t)
  4921. (progn
  4922. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4923. (add-text-properties (match-beginning 0) (match-end 0)
  4924. (list 'mouse-face 'highlight
  4925. 'keymap org-mouse-map))
  4926. (org-rear-nonsticky-at (match-end 0))
  4927. (when org-display-custom-times
  4928. (if (match-end 3)
  4929. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4930. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4931. t)))
  4932. (defvar org-target-link-regexp nil
  4933. "Regular expression matching radio targets in plain text.")
  4934. (make-variable-buffer-local 'org-target-link-regexp)
  4935. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4936. "Regular expression matching a link target.")
  4937. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4938. "Regular expression matching a radio target.")
  4939. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4940. "Regular expression matching any target.")
  4941. (defun org-activate-target-links (limit)
  4942. "Run through the buffer and add overlays to target matches."
  4943. (when org-target-link-regexp
  4944. (let ((case-fold-search t))
  4945. (if (re-search-forward org-target-link-regexp limit t)
  4946. (progn
  4947. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4948. (add-text-properties (match-beginning 0) (match-end 0)
  4949. (list 'mouse-face 'highlight
  4950. 'keymap org-mouse-map
  4951. 'help-echo "Radio target link"
  4952. 'org-linked-text t))
  4953. (org-rear-nonsticky-at (match-end 0))
  4954. t)))))
  4955. (defun org-update-radio-target-regexp ()
  4956. "Find all radio targets in this file and update the regular expression."
  4957. (interactive)
  4958. (when (memq 'radio org-activate-links)
  4959. (setq org-target-link-regexp
  4960. (org-make-target-link-regexp (org-all-targets 'radio)))
  4961. (org-restart-font-lock)))
  4962. (defun org-hide-wide-columns (limit)
  4963. (let (s e)
  4964. (setq s (text-property-any (point) (or limit (point-max))
  4965. 'org-cwidth t))
  4966. (when s
  4967. (setq e (next-single-property-change s 'org-cwidth))
  4968. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4969. (goto-char e)
  4970. t)))
  4971. (defvar org-latex-and-specials-regexp nil
  4972. "Regular expression for highlighting export special stuff.")
  4973. (defvar org-match-substring-regexp)
  4974. (defvar org-match-substring-with-braces-regexp)
  4975. ;; This should be with the exporter code, but we also use if for font-locking
  4976. (defconst org-export-html-special-string-regexps
  4977. '(("\\\\-" . "&shy;")
  4978. ("---\\([^-]\\)" . "&mdash;\\1")
  4979. ("--\\([^-]\\)" . "&ndash;\\1")
  4980. ("\\.\\.\\." . "&hellip;"))
  4981. "Regular expressions for special string conversion.")
  4982. (defun org-compute-latex-and-specials-regexp ()
  4983. "Compute regular expression for stuff treated specially by exporters."
  4984. (if (not org-highlight-latex-fragments-and-specials)
  4985. (org-set-local 'org-latex-and-specials-regexp nil)
  4986. (require 'org-exp)
  4987. (let*
  4988. ((matchers (plist-get org-format-latex-options :matchers))
  4989. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4990. org-latex-regexps)))
  4991. (org-export-allow-BIND nil)
  4992. (options (org-combine-plists (org-default-export-plist)
  4993. (org-infile-export-plist)))
  4994. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4995. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4996. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4997. (org-export-html-expand (plist-get options :expand-quoted-html))
  4998. (org-export-with-special-strings (plist-get options :special-strings))
  4999. (re-sub
  5000. (cond
  5001. ((equal org-export-with-sub-superscripts '{})
  5002. (list org-match-substring-with-braces-regexp))
  5003. (org-export-with-sub-superscripts
  5004. (list org-match-substring-regexp))
  5005. (t nil)))
  5006. (re-latex
  5007. (if org-export-with-LaTeX-fragments
  5008. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5009. (re-macros
  5010. (if org-export-with-TeX-macros
  5011. (list (concat "\\\\"
  5012. (regexp-opt
  5013. (append
  5014. (delq nil
  5015. (mapcar 'car-safe
  5016. (append org-entities-user
  5017. org-entities)))
  5018. (if (boundp 'org-latex-entities)
  5019. (mapcar (lambda (x)
  5020. (or (car-safe x) x))
  5021. org-latex-entities)
  5022. nil))
  5023. 'words))) ; FIXME
  5024. ))
  5025. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5026. (re-special (if org-export-with-special-strings
  5027. (mapcar (lambda (x) (car x))
  5028. org-export-html-special-string-regexps)))
  5029. (re-rest
  5030. (delq nil
  5031. (list
  5032. (if org-export-html-expand "@<[^>\n]+>")
  5033. ))))
  5034. (org-set-local
  5035. 'org-latex-and-specials-regexp
  5036. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5037. re-rest) "\\|")))))
  5038. (defun org-do-latex-and-special-faces (limit)
  5039. "Run through the buffer and add overlays to links."
  5040. (when org-latex-and-specials-regexp
  5041. (let (rtn d)
  5042. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5043. limit t))
  5044. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5045. 'face))
  5046. '(org-code org-verbatim underline)))
  5047. (progn
  5048. (setq rtn t
  5049. d (cond ((member (char-after (1+ (match-beginning 0)))
  5050. '(?_ ?^)) 1)
  5051. (t 0)))
  5052. (font-lock-prepend-text-property
  5053. (+ d (match-beginning 0)) (match-end 0)
  5054. 'face 'org-latex-and-export-specials)
  5055. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5056. '(font-lock-multiline t)))))
  5057. rtn)))
  5058. (defun org-restart-font-lock ()
  5059. "Restart `font-lock-mode', to force refontification."
  5060. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5061. (font-lock-mode -1)
  5062. (font-lock-mode 1)))
  5063. (defun org-all-targets (&optional radio)
  5064. "Return a list of all targets in this file.
  5065. With optional argument RADIO, only find radio targets."
  5066. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5067. rtn)
  5068. (save-excursion
  5069. (goto-char (point-min))
  5070. (while (re-search-forward re nil t)
  5071. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5072. rtn)))
  5073. (defun org-make-target-link-regexp (targets)
  5074. "Make regular expression matching all strings in TARGETS.
  5075. The regular expression finds the targets also if there is a line break
  5076. between words."
  5077. (and targets
  5078. (concat
  5079. "\\<\\("
  5080. (mapconcat
  5081. (lambda (x)
  5082. (setq x (regexp-quote x))
  5083. (while (string-match " +" x)
  5084. (setq x (replace-match "\\s-+" t t x)))
  5085. x)
  5086. targets
  5087. "\\|")
  5088. "\\)\\>")))
  5089. (defun org-activate-tags (limit)
  5090. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5091. (progn
  5092. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5093. (add-text-properties (match-beginning 1) (match-end 1)
  5094. (list 'mouse-face 'highlight
  5095. 'keymap org-mouse-map))
  5096. (org-rear-nonsticky-at (match-end 1))
  5097. t)))
  5098. (defun org-outline-level ()
  5099. "Compute the outline level of the heading at point.
  5100. This function assumes that the cursor is at the beginning of a line matched
  5101. by `outline-regexp'. Otherwise it returns garbage.
  5102. If this is called at a normal headline, the level is the number of stars.
  5103. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5104. (save-excursion
  5105. (looking-at org-outline-regexp)
  5106. (1- (- (match-end 0) (match-beginning 0)))))
  5107. (defvar org-font-lock-keywords nil)
  5108. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5109. "Regular expression matching a property line.")
  5110. (defvar org-font-lock-hook nil
  5111. "Functions to be called for special font lock stuff.")
  5112. (defvar org-font-lock-set-keywords-hook nil
  5113. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5114. This is called after `org-font-lock-extra-keywords' is defined, but before
  5115. it is installed to be used by font lock. This can be useful if something
  5116. needs to be inserted at a specific position in the font-lock sequence.")
  5117. (defun org-font-lock-hook (limit)
  5118. (run-hook-with-args 'org-font-lock-hook limit))
  5119. (defun org-set-font-lock-defaults ()
  5120. (let* ((em org-fontify-emphasized-text)
  5121. (lk org-activate-links)
  5122. (org-font-lock-extra-keywords
  5123. (list
  5124. ;; Call the hook
  5125. '(org-font-lock-hook)
  5126. ;; Headlines
  5127. `(,(if org-fontify-whole-heading-line
  5128. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5129. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5130. (1 (org-get-level-face 1))
  5131. (2 (org-get-level-face 2))
  5132. (3 (org-get-level-face 3)))
  5133. ;; Table lines
  5134. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5135. (1 'org-table t))
  5136. ;; Table internals
  5137. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5138. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5139. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5140. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5141. ;; Drawers
  5142. (list org-drawer-regexp '(0 'org-special-keyword t))
  5143. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5144. ;; Properties
  5145. (list org-property-re
  5146. '(1 'org-special-keyword t)
  5147. '(3 'org-property-value t))
  5148. ;; Links
  5149. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5150. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5151. (if (memq 'plain lk) '(org-activate-plain-links))
  5152. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5153. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5154. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5155. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5156. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5157. '(org-hide-wide-columns (0 nil append))
  5158. ;; TODO keyword
  5159. (list (format org-heading-keyword-regexp-format
  5160. org-todo-regexp)
  5161. '(2 (org-get-todo-face 2) t))
  5162. ;; DONE
  5163. (if org-fontify-done-headline
  5164. (list (format org-heading-keyword-regexp-format
  5165. (concat
  5166. "\\(?:"
  5167. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5168. "\\)"))
  5169. '(2 'org-headline-done t))
  5170. nil)
  5171. ;; Priorities
  5172. '(org-font-lock-add-priority-faces)
  5173. ;; Tags
  5174. '(org-font-lock-add-tag-faces)
  5175. ;; Special keywords
  5176. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5177. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5178. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5179. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5180. ;; Emphasis
  5181. (if em
  5182. (if (featurep 'xemacs)
  5183. '(org-do-emphasis-faces (0 nil append))
  5184. '(org-do-emphasis-faces)))
  5185. ;; Checkboxes
  5186. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5187. 1 'org-checkbox prepend)
  5188. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5189. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5190. (0 (org-get-checkbox-statistics-face) t)))
  5191. ;; Description list items
  5192. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5193. 1 'bold prepend)
  5194. ;; ARCHIVEd headings
  5195. (list (concat
  5196. org-outline-regexp-bol
  5197. "\\(.*:" org-archive-tag ":.*\\)")
  5198. '(1 'org-archived prepend))
  5199. ;; Specials
  5200. '(org-do-latex-and-special-faces)
  5201. '(org-fontify-entities)
  5202. '(org-raise-scripts)
  5203. ;; Code
  5204. '(org-activate-code (1 'org-code t))
  5205. ;; COMMENT
  5206. (list (format org-heading-keyword-regexp-format
  5207. (concat "\\("
  5208. org-comment-string "\\|" org-quote-string
  5209. "\\)"))
  5210. '(2 'org-special-keyword t))
  5211. '("^#.*" (0 'font-lock-comment-face t))
  5212. ;; Blocks and meta lines
  5213. '(org-fontify-meta-lines-and-blocks)
  5214. )))
  5215. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5216. (run-hooks 'org-font-lock-set-keywords-hook)
  5217. ;; Now set the full font-lock-keywords
  5218. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5219. (org-set-local 'font-lock-defaults
  5220. '(org-font-lock-keywords t nil nil backward-paragraph))
  5221. (kill-local-variable 'font-lock-keywords) nil))
  5222. (defun org-toggle-pretty-entities ()
  5223. "Toggle the composition display of entities as UTF8 characters."
  5224. (interactive)
  5225. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5226. (org-restart-font-lock)
  5227. (if org-pretty-entities
  5228. (message "Entities are displayed as UTF8 characters")
  5229. (save-restriction
  5230. (widen)
  5231. (org-decompose-region (point-min) (point-max))
  5232. (message "Entities are displayed plain"))))
  5233. (defun org-fontify-entities (limit)
  5234. "Find an entity to fontify."
  5235. (let (ee)
  5236. (when org-pretty-entities
  5237. (catch 'match
  5238. (while (re-search-forward
  5239. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5240. limit t)
  5241. (if (and (not (org-in-indented-comment-line))
  5242. (setq ee (org-entity-get (match-string 1)))
  5243. (= (length (nth 6 ee)) 1))
  5244. (progn
  5245. (add-text-properties
  5246. (match-beginning 0) (match-end 1)
  5247. (list 'font-lock-fontified t))
  5248. (compose-region (match-beginning 0) (match-end 1)
  5249. (nth 6 ee) nil)
  5250. (backward-char 1)
  5251. (throw 'match t))))
  5252. nil))))
  5253. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5254. "Fontify string S like in Org-mode."
  5255. (with-temp-buffer
  5256. (insert s)
  5257. (let ((org-odd-levels-only odd-levels))
  5258. (org-mode)
  5259. (font-lock-fontify-buffer)
  5260. (buffer-string))))
  5261. (defvar org-m nil)
  5262. (defvar org-l nil)
  5263. (defvar org-f nil)
  5264. (defun org-get-level-face (n)
  5265. "Get the right face for match N in font-lock matching of headlines."
  5266. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5267. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5268. (if org-cycle-level-faces
  5269. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5270. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5271. (cond
  5272. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5273. ((eq n 2) org-f)
  5274. (t (if org-level-color-stars-only nil org-f))))
  5275. (defun org-get-todo-face (kwd)
  5276. "Get the right face for a TODO keyword KWD.
  5277. If KWD is a number, get the corresponding match group."
  5278. (if (numberp kwd) (setq kwd (match-string kwd)))
  5279. (or (org-face-from-face-or-color
  5280. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5281. (and (member kwd org-done-keywords) 'org-done)
  5282. 'org-todo))
  5283. (defun org-face-from-face-or-color (context inherit face-or-color)
  5284. "Create a face list that inherits INHERIT, but sets the foreground color.
  5285. When FACE-OR-COLOR is not a string, just return it."
  5286. (if (stringp face-or-color)
  5287. (list :inherit inherit
  5288. (cdr (assoc context org-faces-easy-properties))
  5289. face-or-color)
  5290. face-or-color))
  5291. (defun org-font-lock-add-tag-faces (limit)
  5292. "Add the special tag faces."
  5293. (when (and org-tag-faces org-tags-special-faces-re)
  5294. (while (re-search-forward org-tags-special-faces-re limit t)
  5295. (add-text-properties (match-beginning 1) (match-end 1)
  5296. (list 'face (org-get-tag-face 1)
  5297. 'font-lock-fontified t))
  5298. (backward-char 1))))
  5299. (defun org-font-lock-add-priority-faces (limit)
  5300. "Add the special priority faces."
  5301. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5302. (add-text-properties
  5303. (match-beginning 0) (match-end 0)
  5304. (list 'face (or (org-face-from-face-or-color
  5305. 'priority 'org-special-keyword
  5306. (cdr (assoc (char-after (match-beginning 1))
  5307. org-priority-faces)))
  5308. 'org-special-keyword)
  5309. 'font-lock-fontified t))))
  5310. (defun org-get-tag-face (kwd)
  5311. "Get the right face for a TODO keyword KWD.
  5312. If KWD is a number, get the corresponding match group."
  5313. (if (numberp kwd) (setq kwd (match-string kwd)))
  5314. (or (org-face-from-face-or-color
  5315. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5316. 'org-tag))
  5317. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5318. "Remove fontification and activation overlays from links."
  5319. (font-lock-default-unfontify-region beg end)
  5320. (let* ((buffer-undo-list t)
  5321. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5322. (inhibit-modification-hooks t)
  5323. deactivate-mark buffer-file-name buffer-file-truename)
  5324. (org-decompose-region beg end)
  5325. (remove-text-properties beg end
  5326. '(mouse-face t keymap t org-linked-text t
  5327. invisible t intangible t
  5328. org-no-flyspell t org-emphasis t))
  5329. (org-remove-font-lock-display-properties beg end)))
  5330. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5331. ((raise 0.3) (height 0.7))
  5332. ((raise -0.5))
  5333. ((raise 0.5)))
  5334. "Display properties for showing superscripts and subscripts.")
  5335. (defun org-remove-font-lock-display-properties (beg end)
  5336. "Remove specific display properties that have been added by font lock.
  5337. The will remove the raise properties that are used to show superscripts
  5338. and subscripts."
  5339. (let (next prop)
  5340. (while (< beg end)
  5341. (setq next (next-single-property-change beg 'display nil end)
  5342. prop (get-text-property beg 'display))
  5343. (if (member prop org-script-display)
  5344. (put-text-property beg next 'display nil))
  5345. (setq beg next))))
  5346. (defun org-raise-scripts (limit)
  5347. "Add raise properties to sub/superscripts."
  5348. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5349. (if (re-search-forward
  5350. (if (eq org-use-sub-superscripts t)
  5351. org-match-substring-regexp
  5352. org-match-substring-with-braces-regexp)
  5353. limit t)
  5354. (let* ((pos (point)) table-p comment-p
  5355. (mpos (match-beginning 3))
  5356. (emph-p (get-text-property mpos 'org-emphasis))
  5357. (link-p (get-text-property mpos 'mouse-face))
  5358. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5359. (goto-char (point-at-bol))
  5360. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5361. comment-p (org-looking-at-p "[ \t]*#"))
  5362. (goto-char pos)
  5363. ;; FIXME: Should we go back one character here, for a_b^c
  5364. ;; (goto-char (1- pos)) ;????????????????????
  5365. (if (or comment-p emph-p link-p keyw-p)
  5366. t
  5367. (put-text-property (match-beginning 3) (match-end 0)
  5368. 'display
  5369. (if (equal (char-after (match-beginning 2)) ?^)
  5370. (nth (if table-p 3 1) org-script-display)
  5371. (nth (if table-p 2 0) org-script-display)))
  5372. (add-text-properties (match-beginning 2) (match-end 2)
  5373. (list 'invisible t
  5374. 'org-dwidth t 'org-dwidth-n 1))
  5375. (if (and (eq (char-after (match-beginning 3)) ?{)
  5376. (eq (char-before (match-end 3)) ?}))
  5377. (progn
  5378. (add-text-properties
  5379. (match-beginning 3) (1+ (match-beginning 3))
  5380. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5381. (add-text-properties
  5382. (1- (match-end 3)) (match-end 3)
  5383. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5384. t)))))
  5385. ;;;; Visibility cycling, including org-goto and indirect buffer
  5386. ;;; Cycling
  5387. (defvar org-cycle-global-status nil)
  5388. (make-variable-buffer-local 'org-cycle-global-status)
  5389. (defvar org-cycle-subtree-status nil)
  5390. (make-variable-buffer-local 'org-cycle-subtree-status)
  5391. ;;;###autoload
  5392. (defvar org-inlinetask-min-level)
  5393. (defun org-cycle (&optional arg)
  5394. "TAB-action and visibility cycling for Org-mode.
  5395. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5396. is outline visibility cycling, but it also invokes other actions
  5397. in special contexts.
  5398. - When this function is called with a prefix argument, rotate the entire
  5399. buffer through 3 states (global cycling)
  5400. 1. OVERVIEW: Show only top-level headlines.
  5401. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5402. 3. SHOW ALL: Show everything.
  5403. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5404. determined by the variable `org-startup-folded', and by any VISIBILITY
  5405. properties in the buffer.
  5406. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5407. including any drawers.
  5408. - When inside a table, re-align the table and move to the next field.
  5409. - When point is at the beginning of a headline, rotate the subtree started
  5410. by this line through 3 different states (local cycling)
  5411. 1. FOLDED: Only the main headline is shown.
  5412. 2. CHILDREN: The main headline and the direct children are shown.
  5413. From this state, you can move to one of the children
  5414. and zoom in further.
  5415. 3. SUBTREE: Show the entire subtree, including body text.
  5416. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5417. - When point is at the beginning of an empty headline and the variable
  5418. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5419. of the headline by demoting and promoting it to likely levels. This
  5420. speeds up creation document structure by pressing TAB once or several
  5421. times right after creating a new headline.
  5422. - When there is a numeric prefix, go up to a heading with level ARG, do
  5423. a `show-subtree' and return to the previous cursor position. If ARG
  5424. is negative, go up that many levels.
  5425. - When point is not at the beginning of a headline, execute the global
  5426. binding for TAB, which is re-indenting the line. See the option
  5427. `org-cycle-emulate-tab' for details.
  5428. - Special case: if point is at the beginning of the buffer and there is
  5429. no headline in line 1, this function will act as if called with prefix arg
  5430. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5431. But only if also the variable `org-cycle-global-at-bob' is t."
  5432. (interactive "P")
  5433. (org-load-modules-maybe)
  5434. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5435. (and org-cycle-level-after-item/entry-creation
  5436. (or (org-cycle-level)
  5437. (org-cycle-item-indentation))))
  5438. (let* ((limit-level
  5439. (or org-cycle-max-level
  5440. (and (boundp 'org-inlinetask-min-level)
  5441. org-inlinetask-min-level
  5442. (1- org-inlinetask-min-level))))
  5443. (nstars (and limit-level
  5444. (if org-odd-levels-only
  5445. (and limit-level (1- (* limit-level 2)))
  5446. limit-level)))
  5447. (org-outline-regexp
  5448. (if (not (eq major-mode 'org-mode))
  5449. outline-regexp
  5450. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5451. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5452. (not (looking-at org-outline-regexp))))
  5453. (org-cycle-hook
  5454. (if bob-special
  5455. (delq 'org-optimize-window-after-visibility-change
  5456. (copy-sequence org-cycle-hook))
  5457. org-cycle-hook))
  5458. (pos (point)))
  5459. (if (or bob-special (equal arg '(4)))
  5460. ;; special case: use global cycling
  5461. (setq arg t))
  5462. (cond
  5463. ((equal arg '(16))
  5464. (setq last-command 'dummy)
  5465. (org-set-startup-visibility)
  5466. (message "Startup visibility, plus VISIBILITY properties"))
  5467. ((equal arg '(64))
  5468. (show-all)
  5469. (message "Entire buffer visible, including drawers"))
  5470. ;; Table: enter it or move to the next field.
  5471. ((org-at-table-p 'any)
  5472. (if (org-at-table.el-p)
  5473. (message "Use C-c ' to edit table.el tables")
  5474. (if arg (org-table-edit-field t)
  5475. (org-table-justify-field-maybe)
  5476. (call-interactively 'org-table-next-field))))
  5477. ((run-hook-with-args-until-success
  5478. 'org-tab-after-check-for-table-hook))
  5479. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5480. ((eq arg t) (org-cycle-internal-global))
  5481. ;; Drawers: delegate to `org-flag-drawer'.
  5482. ((and org-drawers org-drawer-regexp
  5483. (save-excursion
  5484. (beginning-of-line 1)
  5485. (looking-at org-drawer-regexp)))
  5486. (org-flag-drawer ; toggle block visibility
  5487. (not (get-char-property (match-end 0) 'invisible))))
  5488. ;; Show-subtree, ARG levels up from here.
  5489. ((integerp arg)
  5490. (save-excursion
  5491. (org-back-to-heading)
  5492. (outline-up-heading (if (< arg 0) (- arg)
  5493. (- (funcall outline-level) arg)))
  5494. (org-show-subtree)))
  5495. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5496. ((and (featurep 'org-inlinetask)
  5497. (org-inlinetask-at-task-p)
  5498. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5499. (org-inlinetask-toggle-visibility))
  5500. ((org-try-cdlatex-tab))
  5501. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5502. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5503. (save-excursion (beginning-of-line 1)
  5504. (looking-at org-outline-regexp)))
  5505. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5506. (org-cycle-internal-local))
  5507. ;; From there: TAB emulation and template completion.
  5508. (buffer-read-only (org-back-to-heading))
  5509. ((run-hook-with-args-until-success
  5510. 'org-tab-after-check-for-cycling-hook))
  5511. ((org-try-structure-completion))
  5512. ((run-hook-with-args-until-success
  5513. 'org-tab-before-tab-emulation-hook))
  5514. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5515. (or (not (bolp))
  5516. (not (looking-at org-outline-regexp))))
  5517. (call-interactively (global-key-binding "\t")))
  5518. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5519. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5520. (or (and (eq org-cycle-emulate-tab 'white)
  5521. (= (match-end 0) (point-at-eol)))
  5522. (and (eq org-cycle-emulate-tab 'whitestart)
  5523. (>= (match-end 0) pos))))
  5524. t
  5525. (eq org-cycle-emulate-tab t))
  5526. (call-interactively (global-key-binding "\t")))
  5527. (t (save-excursion
  5528. (org-back-to-heading)
  5529. (org-cycle)))))))
  5530. (defun org-cycle-internal-global ()
  5531. "Do the global cycling action."
  5532. (cond
  5533. ((and (eq last-command this-command)
  5534. (eq org-cycle-global-status 'overview))
  5535. ;; We just created the overview - now do table of contents
  5536. ;; This can be slow in very large buffers, so indicate action
  5537. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5538. (message "CONTENTS...")
  5539. (org-content)
  5540. (message "CONTENTS...done")
  5541. (setq org-cycle-global-status 'contents)
  5542. (run-hook-with-args 'org-cycle-hook 'contents))
  5543. ((and (eq last-command this-command)
  5544. (eq org-cycle-global-status 'contents))
  5545. ;; We just showed the table of contents - now show everything
  5546. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5547. (show-all)
  5548. (message "SHOW ALL")
  5549. (setq org-cycle-global-status 'all)
  5550. (run-hook-with-args 'org-cycle-hook 'all))
  5551. (t
  5552. ;; Default action: go to overview
  5553. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5554. (org-overview)
  5555. (message "OVERVIEW")
  5556. (setq org-cycle-global-status 'overview)
  5557. (run-hook-with-args 'org-cycle-hook 'overview))))
  5558. (defun org-cycle-internal-local ()
  5559. "Do the local cycling action."
  5560. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5561. ;; First, determine end of headline (EOH), end of subtree or item
  5562. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5563. (save-excursion
  5564. (if (org-at-item-p)
  5565. (progn
  5566. (beginning-of-line)
  5567. (setq struct (org-list-struct))
  5568. (setq eoh (point-at-eol))
  5569. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5570. (setq has-children (org-list-has-child-p (point) struct)))
  5571. (org-back-to-heading)
  5572. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5573. (setq eos (save-excursion
  5574. (org-end-of-subtree t)
  5575. (unless (eobp)
  5576. (skip-chars-forward " \t\n"))
  5577. (if (eobp) (point) (1- (point)))))
  5578. (setq has-children
  5579. (or (save-excursion
  5580. (let ((level (funcall outline-level)))
  5581. (outline-next-heading)
  5582. (and (org-at-heading-p t)
  5583. (> (funcall outline-level) level))))
  5584. (save-excursion
  5585. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5586. ;; Determine end invisible part of buffer (EOL)
  5587. (beginning-of-line 2)
  5588. ;; XEmacs doesn't have `next-single-char-property-change'
  5589. (if (featurep 'xemacs)
  5590. (while (and (not (eobp)) ;; this is like `next-line'
  5591. (get-char-property (1- (point)) 'invisible))
  5592. (beginning-of-line 2))
  5593. (while (and (not (eobp)) ;; this is like `next-line'
  5594. (get-char-property (1- (point)) 'invisible))
  5595. (goto-char (next-single-char-property-change (point) 'invisible))
  5596. (and (eolp) (beginning-of-line 2))))
  5597. (setq eol (point)))
  5598. ;; Find out what to do next and set `this-command'
  5599. (cond
  5600. ((= eos eoh)
  5601. ;; Nothing is hidden behind this heading
  5602. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5603. (message "EMPTY ENTRY")
  5604. (setq org-cycle-subtree-status nil)
  5605. (save-excursion
  5606. (goto-char eos)
  5607. (outline-next-heading)
  5608. (if (outline-invisible-p) (org-flag-heading nil))))
  5609. ((and (or (>= eol eos)
  5610. (not (string-match "\\S-" (buffer-substring eol eos))))
  5611. (or has-children
  5612. (not (setq children-skipped
  5613. org-cycle-skip-children-state-if-no-children))))
  5614. ;; Entire subtree is hidden in one line: children view
  5615. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5616. (if (org-at-item-p)
  5617. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5618. (org-show-entry)
  5619. (org-with-limited-levels (show-children))
  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. org-refile-allow-creating-parent-nodes
  9595. goto))))))
  9596. (setq file (nth 1 it)
  9597. re (nth 2 it)
  9598. pos (nth 3 it))
  9599. (if (and (not goto)
  9600. pos
  9601. (equal (buffer-file-name) file)
  9602. (if regionp
  9603. (and (>= pos region-start)
  9604. (<= pos region-end))
  9605. (and (>= pos (point))
  9606. (< pos (save-excursion
  9607. (org-end-of-subtree t t))))))
  9608. (error "Cannot refile to position inside the tree or region"))
  9609. (setq nbuf (or (find-buffer-visiting file)
  9610. (find-file-noselect file)))
  9611. (if goto
  9612. (progn
  9613. (org-pop-to-buffer-same-window nbuf)
  9614. (goto-char pos)
  9615. (org-show-context 'org-goto))
  9616. (if regionp
  9617. (progn
  9618. (org-kill-new (buffer-substring region-start region-end))
  9619. (org-save-markers-in-region region-start region-end))
  9620. (org-copy-subtree 1 nil t))
  9621. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9622. (find-file-noselect file)))
  9623. (setq reversed (org-notes-order-reversed-p))
  9624. (save-excursion
  9625. (save-restriction
  9626. (widen)
  9627. (if pos
  9628. (progn
  9629. (goto-char pos)
  9630. (looking-at org-outline-regexp)
  9631. (setq level (org-get-valid-level (funcall outline-level) 1))
  9632. (goto-char
  9633. (if reversed
  9634. (or (outline-next-heading) (point-max))
  9635. (or (save-excursion (org-get-next-sibling))
  9636. (org-end-of-subtree t t)
  9637. (point-max)))))
  9638. (setq level 1)
  9639. (if (not reversed)
  9640. (goto-char (point-max))
  9641. (goto-char (point-min))
  9642. (or (outline-next-heading) (goto-char (point-max)))))
  9643. (if (not (bolp)) (newline))
  9644. (org-paste-subtree level)
  9645. (when org-log-refile
  9646. (org-add-log-setup 'refile nil nil 'findpos
  9647. org-log-refile)
  9648. (unless (eq org-log-refile 'note)
  9649. (save-excursion (org-add-log-note))))
  9650. (and org-auto-align-tags (org-set-tags nil t))
  9651. (bookmark-set "org-refile-last-stored")
  9652. ;; If we are refiling for capture, make sure that the
  9653. ;; last-capture pointers point here
  9654. (when (org-bound-and-true-p org-refile-for-capture)
  9655. (bookmark-set "org-capture-last-stored-marker")
  9656. (move-marker org-capture-last-stored-marker (point)))
  9657. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9658. (run-hooks 'org-after-refile-insert-hook))))
  9659. (if regionp
  9660. (delete-region (point) (+ (point) region-length))
  9661. (org-cut-subtree))
  9662. (when (featurep 'org-inlinetask)
  9663. (org-inlinetask-remove-END-maybe))
  9664. (setq org-markers-to-move nil)
  9665. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9666. (defun org-refile-goto-last-stored ()
  9667. "Go to the location where the last refile was stored."
  9668. (interactive)
  9669. (bookmark-jump "org-refile-last-stored")
  9670. (message "This is the location of the last refile"))
  9671. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9672. no-exclude)
  9673. "Prompt the user for a refile location, using PROMPT.
  9674. PROMPT should not be suffixed with a colon and a space, because
  9675. this function appends the default value from
  9676. `org-refile-history' automatically, if that is not empty.
  9677. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9678. this is used for the GOTO interface."
  9679. (let ((org-refile-targets org-refile-targets)
  9680. (org-refile-use-outline-path org-refile-use-outline-path)
  9681. excluded-entries)
  9682. (when (and (eq major-mode 'org-mode)
  9683. (not org-refile-use-cache)
  9684. (not no-exclude))
  9685. (org-map-tree
  9686. (lambda()
  9687. (setq excluded-entries
  9688. (append excluded-entries (list (org-get-heading t t)))))))
  9689. (setq org-refile-target-table
  9690. (org-refile-get-targets default-buffer excluded-entries)))
  9691. (unless org-refile-target-table
  9692. (error "No refile targets"))
  9693. (let* ((prompt (concat prompt
  9694. (and (car org-refile-history)
  9695. (concat " (default " (car org-refile-history) ")"))
  9696. ": "))
  9697. (cbuf (current-buffer))
  9698. (partial-completion-mode nil)
  9699. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9700. (cfunc (if (and org-refile-use-outline-path
  9701. org-outline-path-complete-in-steps)
  9702. 'org-olpath-completing-read
  9703. 'org-icompleting-read))
  9704. (extra (if org-refile-use-outline-path "/" ""))
  9705. (filename (and cfn (expand-file-name cfn)))
  9706. (tbl (mapcar
  9707. (lambda (x)
  9708. (if (and (not (member org-refile-use-outline-path
  9709. '(file full-file-path)))
  9710. (not (equal filename (nth 1 x))))
  9711. (cons (concat (car x) extra " ("
  9712. (file-name-nondirectory (nth 1 x)) ")")
  9713. (cdr x))
  9714. (cons (concat (car x) extra) (cdr x))))
  9715. org-refile-target-table))
  9716. (completion-ignore-case t)
  9717. pa answ parent-target child parent old-hist)
  9718. (setq old-hist org-refile-history)
  9719. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9720. nil 'org-refile-history (car org-refile-history)))
  9721. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9722. (org-refile-check-position pa)
  9723. (if pa
  9724. (progn
  9725. (when (or (not org-refile-history)
  9726. (not (eq old-hist org-refile-history))
  9727. (not (equal (car pa) (car org-refile-history))))
  9728. (setq org-refile-history
  9729. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9730. org-refile-history
  9731. (cdr org-refile-history))))
  9732. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9733. (pop org-refile-history)))
  9734. pa)
  9735. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9736. (progn
  9737. (setq parent (match-string 1 answ)
  9738. child (match-string 2 answ))
  9739. (setq parent-target (or (assoc parent tbl)
  9740. (assoc (concat parent "/") tbl)))
  9741. (when (and parent-target
  9742. (or (eq new-nodes t)
  9743. (and (eq new-nodes 'confirm)
  9744. (y-or-n-p (format "Create new node \"%s\"? "
  9745. child)))))
  9746. (org-refile-new-child parent-target child)))
  9747. (error "Invalid target location")))))
  9748. (defun org-refile-check-position (refile-pointer)
  9749. "Check if the refile pointer matches the readline to which it points."
  9750. (let* ((file (nth 1 refile-pointer))
  9751. (re (nth 2 refile-pointer))
  9752. (pos (nth 3 refile-pointer))
  9753. buffer)
  9754. (when (org-string-nw-p re)
  9755. (setq buffer (if (markerp pos)
  9756. (marker-buffer pos)
  9757. (or (find-buffer-visiting file)
  9758. (find-file-noselect file))))
  9759. (with-current-buffer buffer
  9760. (save-excursion
  9761. (save-restriction
  9762. (widen)
  9763. (goto-char pos)
  9764. (beginning-of-line 1)
  9765. (unless (org-looking-at-p re)
  9766. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9767. (defun org-refile-new-child (parent-target child)
  9768. "Use refile target PARENT-TARGET to add new CHILD below it."
  9769. (unless parent-target
  9770. (error "Cannot find parent for new node"))
  9771. (let ((file (nth 1 parent-target))
  9772. (pos (nth 3 parent-target))
  9773. level)
  9774. (with-current-buffer (or (find-buffer-visiting file)
  9775. (find-file-noselect file))
  9776. (save-excursion
  9777. (save-restriction
  9778. (widen)
  9779. (if pos
  9780. (goto-char pos)
  9781. (goto-char (point-max))
  9782. (if (not (bolp)) (newline)))
  9783. (when (looking-at org-outline-regexp)
  9784. (setq level (funcall outline-level))
  9785. (org-end-of-subtree t t))
  9786. (org-back-over-empty-lines)
  9787. (insert "\n" (make-string
  9788. (if pos (org-get-valid-level level 1) 1) ?*)
  9789. " " child "\n")
  9790. (beginning-of-line 0)
  9791. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9792. (defun org-olpath-completing-read (prompt collection &rest args)
  9793. "Read an outline path like a file name."
  9794. (let ((thetable collection)
  9795. (org-completion-use-ido nil) ; does not work with ido.
  9796. (org-completion-use-iswitchb nil)) ; or iswitchb
  9797. (apply
  9798. 'org-icompleting-read prompt
  9799. (lambda (string predicate &optional flag)
  9800. (let (rtn r f (l (length string)))
  9801. (cond
  9802. ((eq flag nil)
  9803. ;; try completion
  9804. (try-completion string thetable))
  9805. ((eq flag t)
  9806. ;; all-completions
  9807. (setq rtn (all-completions string thetable predicate))
  9808. (mapcar
  9809. (lambda (x)
  9810. (setq r (substring x l))
  9811. (if (string-match " ([^)]*)$" x)
  9812. (setq f (match-string 0 x))
  9813. (setq f ""))
  9814. (if (string-match "/" r)
  9815. (concat string (substring r 0 (match-end 0)) f)
  9816. x))
  9817. rtn))
  9818. ((eq flag 'lambda)
  9819. ;; exact match?
  9820. (assoc string thetable)))
  9821. ))
  9822. args)))
  9823. ;;;; Dynamic blocks
  9824. (defun org-find-dblock (name)
  9825. "Find the first dynamic block with name NAME in the buffer.
  9826. If not found, stay at current position and return nil."
  9827. (let (pos)
  9828. (save-excursion
  9829. (goto-char (point-min))
  9830. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9831. nil t)
  9832. (match-beginning 0))))
  9833. (if pos (goto-char pos))
  9834. pos))
  9835. (defconst org-dblock-start-re
  9836. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9837. "Matches the start line of a dynamic block, with parameters.")
  9838. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9839. "Matches the end of a dynamic block.")
  9840. (defun org-create-dblock (plist)
  9841. "Create a dynamic block section, with parameters taken from PLIST.
  9842. PLIST must contain a :name entry which is used as name of the block."
  9843. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9844. (end-of-line 1)
  9845. (newline))
  9846. (let ((col (current-column))
  9847. (name (plist-get plist :name)))
  9848. (insert "#+BEGIN: " name)
  9849. (while plist
  9850. (if (eq (car plist) :name)
  9851. (setq plist (cddr plist))
  9852. (insert " " (prin1-to-string (pop plist)))))
  9853. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9854. (beginning-of-line -2)))
  9855. (defun org-prepare-dblock ()
  9856. "Prepare dynamic block for refresh.
  9857. This empties the block, puts the cursor at the insert position and returns
  9858. the property list including an extra property :name with the block name."
  9859. (unless (looking-at org-dblock-start-re)
  9860. (error "Not at a dynamic block"))
  9861. (let* ((begdel (1+ (match-end 0)))
  9862. (name (org-no-properties (match-string 1)))
  9863. (params (append (list :name name)
  9864. (read (concat "(" (match-string 3) ")")))))
  9865. (save-excursion
  9866. (beginning-of-line 1)
  9867. (skip-chars-forward " \t")
  9868. (setq params (plist-put params :indentation-column (current-column))))
  9869. (unless (re-search-forward org-dblock-end-re nil t)
  9870. (error "Dynamic block not terminated"))
  9871. (setq params
  9872. (append params
  9873. (list :content (buffer-substring
  9874. begdel (match-beginning 0)))))
  9875. (delete-region begdel (match-beginning 0))
  9876. (goto-char begdel)
  9877. (open-line 1)
  9878. params))
  9879. (defun org-map-dblocks (&optional command)
  9880. "Apply COMMAND to all dynamic blocks in the current buffer.
  9881. If COMMAND is not given, use `org-update-dblock'."
  9882. (let ((cmd (or command 'org-update-dblock)))
  9883. (save-excursion
  9884. (goto-char (point-min))
  9885. (while (re-search-forward org-dblock-start-re nil t)
  9886. (goto-char (match-beginning 0))
  9887. (save-excursion
  9888. (condition-case nil
  9889. (funcall cmd)
  9890. (error (message "Error during update of dynamic block"))))
  9891. (unless (re-search-forward org-dblock-end-re nil t)
  9892. (error "Dynamic block not terminated"))))))
  9893. (defun org-dblock-update (&optional arg)
  9894. "User command for updating dynamic blocks.
  9895. Update the dynamic block at point. With prefix ARG, update all dynamic
  9896. blocks in the buffer."
  9897. (interactive "P")
  9898. (if arg
  9899. (org-update-all-dblocks)
  9900. (or (looking-at org-dblock-start-re)
  9901. (org-beginning-of-dblock))
  9902. (org-update-dblock)))
  9903. (defun org-update-dblock ()
  9904. "Update the dynamic block at point.
  9905. This means to empty the block, parse for parameters and then call
  9906. the correct writing function."
  9907. (interactive)
  9908. (save-window-excursion
  9909. (let* ((pos (point))
  9910. (line (org-current-line))
  9911. (params (org-prepare-dblock))
  9912. (name (plist-get params :name))
  9913. (indent (plist-get params :indentation-column))
  9914. (cmd (intern (concat "org-dblock-write:" name))))
  9915. (message "Updating dynamic block `%s' at line %d..." name line)
  9916. (funcall cmd params)
  9917. (message "Updating dynamic block `%s' at line %d...done" name line)
  9918. (goto-char pos)
  9919. (when (and indent (> indent 0))
  9920. (setq indent (make-string indent ?\ ))
  9921. (save-excursion
  9922. (org-beginning-of-dblock)
  9923. (forward-line 1)
  9924. (while (not (looking-at org-dblock-end-re))
  9925. (insert indent)
  9926. (beginning-of-line 2))
  9927. (when (looking-at org-dblock-end-re)
  9928. (and (looking-at "[ \t]+")
  9929. (replace-match ""))
  9930. (insert indent)))))))
  9931. (defun org-beginning-of-dblock ()
  9932. "Find the beginning of the dynamic block at point.
  9933. Error if there is no such block at point."
  9934. (let ((pos (point))
  9935. beg)
  9936. (end-of-line 1)
  9937. (if (and (re-search-backward org-dblock-start-re nil t)
  9938. (setq beg (match-beginning 0))
  9939. (re-search-forward org-dblock-end-re nil t)
  9940. (> (match-end 0) pos))
  9941. (goto-char beg)
  9942. (goto-char pos)
  9943. (error "Not in a dynamic block"))))
  9944. (defun org-update-all-dblocks ()
  9945. "Update all dynamic blocks in the buffer.
  9946. This function can be used in a hook."
  9947. (interactive)
  9948. (when (eq major-mode 'org-mode)
  9949. (org-map-dblocks 'org-update-dblock)))
  9950. ;;;; Completion
  9951. (defconst org-additional-option-like-keywords
  9952. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9953. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9954. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9955. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9956. "BEGIN:" "END:"
  9957. "ORGTBL" "TBLFM:" "TBLNAME:"
  9958. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9959. "BEGIN_QUOTE" "END_QUOTE"
  9960. "BEGIN_VERSE" "END_VERSE"
  9961. "BEGIN_CENTER" "END_CENTER"
  9962. "BEGIN_SRC" "END_SRC"
  9963. "BEGIN_RESULT" "END_RESULT"
  9964. "NAME:" "RESULTS:"
  9965. "HEADER:" "HEADERS:"
  9966. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9967. "CAPTION:" "LABEL:"
  9968. "SETUPFILE:"
  9969. "INCLUDE:"
  9970. "BIND:"
  9971. "MACRO:"))
  9972. (defcustom org-structure-template-alist
  9973. '(
  9974. ("s" "#+begin_src ?\n\n#+end_src"
  9975. "<src lang=\"?\">\n\n</src>")
  9976. ("e" "#+begin_example\n?\n#+end_example"
  9977. "<example>\n?\n</example>")
  9978. ("q" "#+begin_quote\n?\n#+end_quote"
  9979. "<quote>\n?\n</quote>")
  9980. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  9981. "<verse>\n?\n</verse>")
  9982. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  9983. "<center>\n?\n</center>")
  9984. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  9985. "<literal style=\"latex\">\n?\n</literal>")
  9986. ("L" "#+latex: "
  9987. "<literal style=\"latex\">?</literal>")
  9988. ("h" "#+begin_html\n?\n#+end_html"
  9989. "<literal style=\"html\">\n?\n</literal>")
  9990. ("H" "#+html: "
  9991. "<literal style=\"html\">?</literal>")
  9992. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9993. ("A" "#+ascii: ")
  9994. ("i" "#+index: ?"
  9995. "#+index: ?")
  9996. ("I" "#+include %file ?"
  9997. "<include file=%file markup=\"?\">")
  9998. )
  9999. "Structure completion elements.
  10000. This is a list of abbreviation keys and values. The value gets inserted
  10001. if you type `<' followed by the key and then press the completion key,
  10002. usually `M-TAB'. %file will be replaced by a file name after prompting
  10003. for the file using completion. The cursor will be placed at the position
  10004. of the `?` in the template.
  10005. There are two templates for each key, the first uses the original Org syntax,
  10006. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10007. the default when the /org-mtags.el/ module has been loaded. See also the
  10008. variable `org-mtags-prefer-muse-templates'.
  10009. This is an experimental feature, it is undecided if it is going to stay in."
  10010. :group 'org-completion
  10011. :type '(repeat
  10012. (string :tag "Key")
  10013. (string :tag "Template")
  10014. (string :tag "Muse Template")))
  10015. (defun org-try-structure-completion ()
  10016. "Try to complete a structure template before point.
  10017. This looks for strings like \"<e\" on an otherwise empty line and
  10018. expands them."
  10019. (let ((l (buffer-substring (point-at-bol) (point)))
  10020. a)
  10021. (when (and (looking-at "[ \t]*$")
  10022. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10023. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10024. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10025. (match-beginning 1)) a)
  10026. t)))
  10027. (defun org-complete-expand-structure-template (start cell)
  10028. "Expand a structure template."
  10029. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10030. (rpl (nth (if musep 2 1) cell))
  10031. (ind ""))
  10032. (delete-region start (point))
  10033. (when (string-match "\\`#\\+" rpl)
  10034. (cond
  10035. ((bolp))
  10036. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10037. (setq ind (buffer-substring (point-at-bol) (point))))
  10038. (t (newline))))
  10039. (setq start (point))
  10040. (if (string-match "%file" rpl)
  10041. (setq rpl (replace-match
  10042. (concat
  10043. "\""
  10044. (save-match-data
  10045. (abbreviate-file-name (read-file-name "Include file: ")))
  10046. "\"")
  10047. t t rpl)))
  10048. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10049. (concat "\n" ind)))
  10050. (insert rpl)
  10051. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10052. ;;;; TODO, DEADLINE, Comments
  10053. (defun org-toggle-comment ()
  10054. "Change the COMMENT state of an entry."
  10055. (interactive)
  10056. (save-excursion
  10057. (org-back-to-heading)
  10058. (let (case-fold-search)
  10059. (cond
  10060. ((looking-at (format org-heading-keyword-regexp-format
  10061. org-comment-string))
  10062. (goto-char (match-end 1))
  10063. (looking-at (concat " +" org-comment-string))
  10064. (replace-match "" t t)
  10065. (when (eolp) (insert " ")))
  10066. ((looking-at org-outline-regexp)
  10067. (goto-char (match-end 0))
  10068. (insert org-comment-string " "))))))
  10069. (defvar org-last-todo-state-is-todo nil
  10070. "This is non-nil when the last TODO state change led to a TODO state.
  10071. If the last change removed the TODO tag or switched to DONE, then
  10072. this is nil.")
  10073. (defvar org-setting-tags nil) ; dynamically skipped
  10074. (defvar org-todo-setup-filter-hook nil
  10075. "Hook for functions that pre-filter todo specs.
  10076. Each function takes a todo spec and returns either nil or the spec
  10077. transformed into canonical form." )
  10078. (defvar org-todo-get-default-hook nil
  10079. "Hook for functions that get a default item for todo.
  10080. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10081. nil or a string to be used for the todo mark." )
  10082. (defvar org-agenda-headline-snapshot-before-repeat)
  10083. (defun org-current-effective-time ()
  10084. "Return current time adjusted for `org-extend-today-until' variable"
  10085. (let* ((ct (org-current-time))
  10086. (dct (decode-time ct))
  10087. (ct1
  10088. (if (and org-use-effective-time
  10089. (< (nth 2 dct) org-extend-today-until))
  10090. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10091. ct)))
  10092. ct1))
  10093. (defun org-todo-yesterday (&optional arg)
  10094. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10095. (interactive "P")
  10096. (if (eq major-mode 'org-agenda-mode)
  10097. (apply 'org-agenda-todo-yesterday arg)
  10098. (let* ((hour (third (decode-time
  10099. (org-current-time))))
  10100. (org-extend-today-until (1+ hour)))
  10101. (org-todo arg))))
  10102. (defun org-todo (&optional arg)
  10103. "Change the TODO state of an item.
  10104. The state of an item is given by a keyword at the start of the heading,
  10105. like
  10106. *** TODO Write paper
  10107. *** DONE Call mom
  10108. The different keywords are specified in the variable `org-todo-keywords'.
  10109. By default the available states are \"TODO\" and \"DONE\".
  10110. So for this example: when the item starts with TODO, it is changed to DONE.
  10111. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10112. DONE are present, add TODO at the beginning of the heading.
  10113. With \\[universal-argument] prefix arg, use completion to determine the new \
  10114. state.
  10115. With numeric prefix arg, switch to that state.
  10116. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10117. keywords (nextset).
  10118. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10119. With a numeric prefix arg of 0, inhibit note taking for the change.
  10120. For calling through lisp, arg is also interpreted in the following way:
  10121. 'none -> empty state
  10122. \"\"(empty string) -> switch to empty state
  10123. 'done -> switch to DONE
  10124. 'nextset -> switch to the next set of keywords
  10125. 'previousset -> switch to the previous set of keywords
  10126. \"WAITING\" -> switch to the specified keyword, but only if it
  10127. really is a member of `org-todo-keywords'."
  10128. (interactive "P")
  10129. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10130. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10131. 'region-start-level 'region))
  10132. org-loop-over-headlines-in-active-region)
  10133. (org-map-entries
  10134. `(org-todo ,arg)
  10135. org-loop-over-headlines-in-active-region
  10136. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10137. (if (equal arg '(16)) (setq arg 'nextset))
  10138. (let ((org-blocker-hook org-blocker-hook)
  10139. (case-fold-search nil))
  10140. (when (equal arg '(64))
  10141. (setq arg nil org-blocker-hook nil))
  10142. (when (and org-blocker-hook
  10143. (or org-inhibit-blocking
  10144. (org-entry-get nil "NOBLOCKING")))
  10145. (setq org-blocker-hook nil))
  10146. (save-excursion
  10147. (catch 'exit
  10148. (org-back-to-heading t)
  10149. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10150. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10151. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10152. (let* ((match-data (match-data))
  10153. (startpos (point-at-bol))
  10154. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10155. (org-log-done org-log-done)
  10156. (org-log-repeat org-log-repeat)
  10157. (org-todo-log-states org-todo-log-states)
  10158. (org-inhibit-logging
  10159. (if (equal arg 0)
  10160. (progn (setq arg nil) 'note) org-inhibit-logging))
  10161. (this (match-string 1))
  10162. (hl-pos (match-beginning 0))
  10163. (head (org-get-todo-sequence-head this))
  10164. (ass (assoc head org-todo-kwd-alist))
  10165. (interpret (nth 1 ass))
  10166. (done-word (nth 3 ass))
  10167. (final-done-word (nth 4 ass))
  10168. (last-state (or this ""))
  10169. (completion-ignore-case t)
  10170. (member (member this org-todo-keywords-1))
  10171. (tail (cdr member))
  10172. (state (cond
  10173. ((and org-todo-key-trigger
  10174. (or (and (equal arg '(4))
  10175. (eq org-use-fast-todo-selection 'prefix))
  10176. (and (not arg) org-use-fast-todo-selection
  10177. (not (eq org-use-fast-todo-selection
  10178. 'prefix)))))
  10179. ;; Use fast selection
  10180. (org-fast-todo-selection))
  10181. ((and (equal arg '(4))
  10182. (or (not org-use-fast-todo-selection)
  10183. (not org-todo-key-trigger)))
  10184. ;; Read a state with completion
  10185. (org-icompleting-read
  10186. "State: " (mapcar (lambda(x) (list x))
  10187. org-todo-keywords-1)
  10188. nil t))
  10189. ((eq arg 'right)
  10190. (if this
  10191. (if tail (car tail) nil)
  10192. (car org-todo-keywords-1)))
  10193. ((eq arg 'left)
  10194. (if (equal member org-todo-keywords-1)
  10195. nil
  10196. (if this
  10197. (nth (- (length org-todo-keywords-1)
  10198. (length tail) 2)
  10199. org-todo-keywords-1)
  10200. (org-last org-todo-keywords-1))))
  10201. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10202. (setq arg nil))) ; hack to fall back to cycling
  10203. (arg
  10204. ;; user or caller requests a specific state
  10205. (cond
  10206. ((equal arg "") nil)
  10207. ((eq arg 'none) nil)
  10208. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10209. ((eq arg 'nextset)
  10210. (or (car (cdr (member head org-todo-heads)))
  10211. (car org-todo-heads)))
  10212. ((eq arg 'previousset)
  10213. (let ((org-todo-heads (reverse org-todo-heads)))
  10214. (or (car (cdr (member head org-todo-heads)))
  10215. (car org-todo-heads))))
  10216. ((car (member arg org-todo-keywords-1)))
  10217. ((stringp arg)
  10218. (error "State `%s' not valid in this file" arg))
  10219. ((nth (1- (prefix-numeric-value arg))
  10220. org-todo-keywords-1))))
  10221. ((null member) (or head (car org-todo-keywords-1)))
  10222. ((equal this final-done-word) nil) ;; -> make empty
  10223. ((null tail) nil) ;; -> first entry
  10224. ((memq interpret '(type priority))
  10225. (if (eq this-command last-command)
  10226. (car tail)
  10227. (if (> (length tail) 0)
  10228. (or done-word (car org-done-keywords))
  10229. nil)))
  10230. (t
  10231. (car tail))))
  10232. (state (or
  10233. (run-hook-with-args-until-success
  10234. 'org-todo-get-default-hook state last-state)
  10235. state))
  10236. (next (if state (concat " " state " ") " "))
  10237. (change-plist (list :type 'todo-state-change :from this :to state
  10238. :position startpos))
  10239. dolog now-done-p)
  10240. (when org-blocker-hook
  10241. (setq org-last-todo-state-is-todo
  10242. (not (member this org-done-keywords)))
  10243. (unless (save-excursion
  10244. (save-match-data
  10245. (org-with-wide-buffer
  10246. (run-hook-with-args-until-failure
  10247. 'org-blocker-hook change-plist))))
  10248. (if (org-called-interactively-p 'interactive)
  10249. (error "TODO state change from %s to %s blocked" this state)
  10250. ;; fail silently
  10251. (message "TODO state change from %s to %s blocked" this state)
  10252. (throw 'exit nil))))
  10253. (store-match-data match-data)
  10254. (replace-match next t t)
  10255. (unless (pos-visible-in-window-p hl-pos)
  10256. (message "TODO state changed to %s" (org-trim next)))
  10257. (unless head
  10258. (setq head (org-get-todo-sequence-head state)
  10259. ass (assoc head org-todo-kwd-alist)
  10260. interpret (nth 1 ass)
  10261. done-word (nth 3 ass)
  10262. final-done-word (nth 4 ass)))
  10263. (when (memq arg '(nextset previousset))
  10264. (message "Keyword-Set %d/%d: %s"
  10265. (- (length org-todo-sets) -1
  10266. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10267. (length org-todo-sets)
  10268. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10269. (setq org-last-todo-state-is-todo
  10270. (not (member state org-done-keywords)))
  10271. (setq now-done-p (and (member state org-done-keywords)
  10272. (not (member this org-done-keywords))))
  10273. (and logging (org-local-logging logging))
  10274. (when (and (or org-todo-log-states org-log-done)
  10275. (not (eq org-inhibit-logging t))
  10276. (not (memq arg '(nextset previousset))))
  10277. ;; we need to look at recording a time and note
  10278. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10279. (nth 2 (assoc this org-todo-log-states))))
  10280. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10281. (setq dolog 'time))
  10282. (when (and state
  10283. (member state org-not-done-keywords)
  10284. (not (member this org-not-done-keywords)))
  10285. ;; This is now a todo state and was not one before
  10286. ;; If there was a CLOSED time stamp, get rid of it.
  10287. (org-add-planning-info nil nil 'closed))
  10288. (when (and now-done-p org-log-done)
  10289. ;; It is now done, and it was not done before
  10290. (org-add-planning-info 'closed (org-current-effective-time))
  10291. (if (and (not dolog) (eq 'note org-log-done))
  10292. (org-add-log-setup 'done state this 'findpos 'note)))
  10293. (when (and state dolog)
  10294. ;; This is a non-nil state, and we need to log it
  10295. (org-add-log-setup 'state state this 'findpos dolog)))
  10296. ;; Fixup tag positioning
  10297. (org-todo-trigger-tag-changes state)
  10298. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10299. (when org-provide-todo-statistics
  10300. (org-update-parent-todo-statistics))
  10301. (run-hooks 'org-after-todo-state-change-hook)
  10302. (if (and arg (not (member state org-done-keywords)))
  10303. (setq head (org-get-todo-sequence-head state)))
  10304. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10305. ;; Do we need to trigger a repeat?
  10306. (when now-done-p
  10307. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10308. ;; This is for the agenda, take a snapshot of the headline.
  10309. (save-match-data
  10310. (setq org-agenda-headline-snapshot-before-repeat
  10311. (org-get-heading))))
  10312. (org-auto-repeat-maybe state))
  10313. ;; Fixup cursor location if close to the keyword
  10314. (if (and (outline-on-heading-p)
  10315. (not (bolp))
  10316. (save-excursion (beginning-of-line 1)
  10317. (looking-at org-todo-line-regexp))
  10318. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10319. (progn
  10320. (goto-char (or (match-end 2) (match-end 1)))
  10321. (and (looking-at " ") (just-one-space))))
  10322. (when org-trigger-hook
  10323. (save-excursion
  10324. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10325. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10326. "Block turning an entry into a TODO, using the hierarchy.
  10327. This checks whether the current task should be blocked from state
  10328. changes. Such blocking occurs when:
  10329. 1. The task has children which are not all in a completed state.
  10330. 2. A task has a parent with the property :ORDERED:, and there
  10331. are siblings prior to the current task with incomplete
  10332. status.
  10333. 3. The parent of the task is blocked because it has siblings that should
  10334. be done first, or is child of a block grandparent TODO entry."
  10335. (if (not org-enforce-todo-dependencies)
  10336. t ; if locally turned off don't block
  10337. (catch 'dont-block
  10338. ;; If this is not a todo state change, or if this entry is already DONE,
  10339. ;; do not block
  10340. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10341. (member (plist-get change-plist :from)
  10342. (cons 'done org-done-keywords))
  10343. (member (plist-get change-plist :to)
  10344. (cons 'todo org-not-done-keywords))
  10345. (not (plist-get change-plist :to)))
  10346. (throw 'dont-block t))
  10347. ;; If this task has children, and any are undone, it's blocked
  10348. (save-excursion
  10349. (org-back-to-heading t)
  10350. (let ((this-level (funcall outline-level)))
  10351. (outline-next-heading)
  10352. (let ((child-level (funcall outline-level)))
  10353. (while (and (not (eobp))
  10354. (> child-level this-level))
  10355. ;; this todo has children, check whether they are all
  10356. ;; completed
  10357. (if (and (not (org-entry-is-done-p))
  10358. (org-entry-is-todo-p))
  10359. (throw 'dont-block nil))
  10360. (outline-next-heading)
  10361. (setq child-level (funcall outline-level))))))
  10362. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10363. ;; any previous siblings are undone, it's blocked
  10364. (save-excursion
  10365. (org-back-to-heading t)
  10366. (let* ((pos (point))
  10367. (parent-pos (and (org-up-heading-safe) (point))))
  10368. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10369. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10370. (forward-line 1)
  10371. (re-search-forward org-not-done-heading-regexp pos t))
  10372. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10373. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10374. (while t
  10375. (goto-char parent-pos)
  10376. (if (not (looking-at org-not-done-heading-regexp))
  10377. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10378. (setq pos (point))
  10379. (setq parent-pos (and (org-up-heading-safe) (point)))
  10380. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10381. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10382. (forward-line 1)
  10383. (re-search-forward org-not-done-heading-regexp pos t))
  10384. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10385. (defcustom org-track-ordered-property-with-tag nil
  10386. "Should the ORDERED property also be shown as a tag?
  10387. The ORDERED property decides if an entry should require subtasks to be
  10388. completed in sequence. Since a property is not very visible, setting
  10389. this option means that toggling the ORDERED property with the command
  10390. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10391. not relevant for the behavior, but it makes things more visible.
  10392. Note that toggling the tag with tags commands will not change the property
  10393. and therefore not influence behavior!
  10394. This can be t, meaning the tag ORDERED should be used, It can also be a
  10395. string to select a different tag for this task."
  10396. :group 'org-todo
  10397. :type '(choice
  10398. (const :tag "No tracking" nil)
  10399. (const :tag "Track with ORDERED tag" t)
  10400. (string :tag "Use other tag")))
  10401. (defun org-toggle-ordered-property ()
  10402. "Toggle the ORDERED property of the current entry.
  10403. For better visibility, you can track the value of this property with a tag.
  10404. See variable `org-track-ordered-property-with-tag'."
  10405. (interactive)
  10406. (let* ((t1 org-track-ordered-property-with-tag)
  10407. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10408. (save-excursion
  10409. (org-back-to-heading)
  10410. (if (org-entry-get nil "ORDERED")
  10411. (progn
  10412. (org-delete-property "ORDERED")
  10413. (and tag (org-toggle-tag tag 'off))
  10414. (message "Subtasks can be completed in arbitrary order"))
  10415. (org-entry-put nil "ORDERED" "t")
  10416. (and tag (org-toggle-tag tag 'on))
  10417. (message "Subtasks must be completed in sequence")))))
  10418. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10419. (defun org-block-todo-from-checkboxes (change-plist)
  10420. "Block turning an entry into a TODO, using checkboxes.
  10421. This checks whether the current task should be blocked from state
  10422. changes because there are unchecked boxes in this entry."
  10423. (if (not org-enforce-todo-checkbox-dependencies)
  10424. t ; if locally turned off don't block
  10425. (catch 'dont-block
  10426. ;; If this is not a todo state change, or if this entry is already DONE,
  10427. ;; do not block
  10428. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10429. (member (plist-get change-plist :from)
  10430. (cons 'done org-done-keywords))
  10431. (member (plist-get change-plist :to)
  10432. (cons 'todo org-not-done-keywords))
  10433. (not (plist-get change-plist :to)))
  10434. (throw 'dont-block t))
  10435. ;; If this task has checkboxes that are not checked, it's blocked
  10436. (save-excursion
  10437. (org-back-to-heading t)
  10438. (let ((beg (point)) end)
  10439. (outline-next-heading)
  10440. (setq end (point))
  10441. (goto-char beg)
  10442. (if (org-list-search-forward
  10443. (concat (org-item-beginning-re)
  10444. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10445. "\\[[- ]\\]")
  10446. end t)
  10447. (progn
  10448. (if (boundp 'org-blocked-by-checkboxes)
  10449. (setq org-blocked-by-checkboxes t))
  10450. (throw 'dont-block nil)))))
  10451. t))) ; do not block
  10452. (defun org-entry-blocked-p ()
  10453. "Is the current entry blocked?"
  10454. (if (org-entry-get nil "NOBLOCKING")
  10455. nil ;; Never block this entry
  10456. (not
  10457. (run-hook-with-args-until-failure
  10458. 'org-blocker-hook
  10459. (list :type 'todo-state-change
  10460. :position (point)
  10461. :from 'todo
  10462. :to 'done)))))
  10463. (defun org-update-statistics-cookies (all)
  10464. "Update the statistics cookie, either from TODO or from checkboxes.
  10465. This should be called with the cursor in a line with a statistics cookie."
  10466. (interactive "P")
  10467. (if all
  10468. (progn
  10469. (org-update-checkbox-count 'all)
  10470. (org-map-entries 'org-update-parent-todo-statistics))
  10471. (if (not (org-at-heading-p))
  10472. (org-update-checkbox-count)
  10473. (let ((pos (move-marker (make-marker) (point)))
  10474. end l1 l2)
  10475. (ignore-errors (org-back-to-heading t))
  10476. (if (not (org-at-heading-p))
  10477. (org-update-checkbox-count)
  10478. (setq l1 (org-outline-level))
  10479. (setq end (save-excursion
  10480. (outline-next-heading)
  10481. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10482. (point)))
  10483. (if (and (save-excursion
  10484. (re-search-forward
  10485. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10486. (not (save-excursion (re-search-forward
  10487. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10488. (org-update-checkbox-count)
  10489. (if (and l2 (> l2 l1))
  10490. (progn
  10491. (goto-char end)
  10492. (org-update-parent-todo-statistics))
  10493. (goto-char pos)
  10494. (beginning-of-line 1)
  10495. (while (re-search-forward
  10496. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10497. (point-at-eol) t)
  10498. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10499. (goto-char pos)
  10500. (move-marker pos nil)))))
  10501. (defvar org-entry-property-inherited-from) ;; defined below
  10502. (defun org-update-parent-todo-statistics ()
  10503. "Update any statistics cookie in the parent of the current headline.
  10504. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10505. the entire subtree and this will travel up the hierarchy and update
  10506. statistics everywhere."
  10507. (let* ((prop (save-excursion (org-up-heading-safe)
  10508. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10509. (recursive (or (not org-hierarchical-todo-statistics)
  10510. (and prop (string-match "\\<recursive\\>" prop))))
  10511. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10512. 0))
  10513. (first t)
  10514. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10515. level ltoggle l1 new ndel
  10516. (cnt-all 0) (cnt-done 0) is-percent kwd
  10517. checkbox-beg ov ovs ove cookie-present)
  10518. (catch 'exit
  10519. (save-excursion
  10520. (beginning-of-line 1)
  10521. (setq ltoggle (funcall outline-level))
  10522. ;; Three situations are to consider:
  10523. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10524. ;; to the top-level ancestor on the headline;
  10525. ;; 2. If parent has "recursive" property, repeat up to the
  10526. ;; headline setting that property, taking inheritance into
  10527. ;; account;
  10528. ;; 3. Else, move up to direct parent and proceed only once.
  10529. (while (and (setq level (org-up-heading-safe))
  10530. (or recursive first)
  10531. (>= (point) lim))
  10532. (setq first nil cookie-present nil)
  10533. (unless (and level
  10534. (not (string-match
  10535. "\\<checkbox\\>"
  10536. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10537. "")))))
  10538. (throw 'exit nil))
  10539. (while (re-search-forward box-re (point-at-eol) t)
  10540. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10541. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10542. (save-match-data
  10543. (unless (outline-next-heading) (throw 'exit nil))
  10544. (while (and (looking-at org-complex-heading-regexp)
  10545. (> (setq l1 (length (match-string 1))) level))
  10546. (setq kwd (and (or recursive (= l1 ltoggle))
  10547. (match-string 2)))
  10548. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10549. (and (listp org-provide-todo-statistics)
  10550. (or (member kwd org-provide-todo-statistics)
  10551. (member kwd org-done-keywords))))
  10552. (setq cnt-all (1+ cnt-all))
  10553. (if (eq org-provide-todo-statistics t)
  10554. (and kwd (setq cnt-all (1+ cnt-all)))))
  10555. (and (member kwd org-done-keywords)
  10556. (setq cnt-done (1+ cnt-done)))
  10557. (outline-next-heading)))
  10558. (setq new
  10559. (if is-percent
  10560. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10561. (format "[%d/%d]" cnt-done cnt-all))
  10562. ndel (- (match-end 0) checkbox-beg))
  10563. ;; handle overlays when updating cookie from column view
  10564. (when (setq ov (car (overlays-at checkbox-beg)))
  10565. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10566. (delete-overlay ov))
  10567. (goto-char checkbox-beg)
  10568. (insert new)
  10569. (delete-region (point) (+ (point) ndel))
  10570. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10571. (when ov (move-overlay ov ovs ove)))
  10572. (when cookie-present
  10573. (run-hook-with-args 'org-after-todo-statistics-hook
  10574. cnt-done (- cnt-all cnt-done))))))
  10575. (run-hooks 'org-todo-statistics-hook)))
  10576. (defvar org-after-todo-statistics-hook nil
  10577. "Hook that is called after a TODO statistics cookie has been updated.
  10578. Each function is called with two arguments: the number of not-done entries
  10579. and the number of done entries.
  10580. For example, the following function, when added to this hook, will switch
  10581. an entry to DONE when all children are done, and back to TODO when new
  10582. entries are set to a TODO status. Note that this hook is only called
  10583. when there is a statistics cookie in the headline!
  10584. (defun org-summary-todo (n-done n-not-done)
  10585. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10586. (let (org-log-done org-log-states) ; turn off logging
  10587. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10588. ")
  10589. (defvar org-todo-statistics-hook nil
  10590. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10591. This hook runs even if there is no statistics cookie present, in which case
  10592. `org-after-todo-statistics-hook' would not run.")
  10593. (defun org-todo-trigger-tag-changes (state)
  10594. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10595. (let ((l org-todo-state-tags-triggers)
  10596. changes)
  10597. (when (or (not state) (equal state ""))
  10598. (setq changes (append changes (cdr (assoc "" l)))))
  10599. (when (and (stringp state) (> (length state) 0))
  10600. (setq changes (append changes (cdr (assoc state l)))))
  10601. (when (member state org-not-done-keywords)
  10602. (setq changes (append changes (cdr (assoc 'todo l)))))
  10603. (when (member state org-done-keywords)
  10604. (setq changes (append changes (cdr (assoc 'done l)))))
  10605. (dolist (c changes)
  10606. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10607. (defun org-local-logging (value)
  10608. "Get logging settings from a property VALUE."
  10609. (let* (words w a)
  10610. ;; directly set the variables, they are already local.
  10611. (setq org-log-done nil
  10612. org-log-repeat nil
  10613. org-todo-log-states nil)
  10614. (setq words (org-split-string value))
  10615. (while (setq w (pop words))
  10616. (cond
  10617. ((setq a (assoc w org-startup-options))
  10618. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10619. (set (nth 1 a) (nth 2 a))))
  10620. ((setq a (org-extract-log-state-settings w))
  10621. (and (member (car a) org-todo-keywords-1)
  10622. (push a org-todo-log-states)))))))
  10623. (defun org-get-todo-sequence-head (kwd)
  10624. "Return the head of the TODO sequence to which KWD belongs.
  10625. If KWD is not set, check if there is a text property remembering the
  10626. right sequence."
  10627. (let (p)
  10628. (cond
  10629. ((not kwd)
  10630. (or (get-text-property (point-at-bol) 'org-todo-head)
  10631. (progn
  10632. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10633. nil (point-at-eol)))
  10634. (get-text-property p 'org-todo-head))))
  10635. ((not (member kwd org-todo-keywords-1))
  10636. (car org-todo-keywords-1))
  10637. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10638. (defun org-fast-todo-selection ()
  10639. "Fast TODO keyword selection with single keys.
  10640. Returns the new TODO keyword, or nil if no state change should occur."
  10641. (let* ((fulltable org-todo-key-alist)
  10642. (done-keywords org-done-keywords) ;; needed for the faces.
  10643. (maxlen (apply 'max (mapcar
  10644. (lambda (x)
  10645. (if (stringp (car x)) (string-width (car x)) 0))
  10646. fulltable)))
  10647. (expert nil)
  10648. (fwidth (+ maxlen 3 1 3))
  10649. (ncol (/ (- (window-width) 4) fwidth))
  10650. tg cnt e c tbl
  10651. groups ingroup)
  10652. (save-excursion
  10653. (save-window-excursion
  10654. (if expert
  10655. (set-buffer (get-buffer-create " *Org todo*"))
  10656. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10657. (erase-buffer)
  10658. (org-set-local 'org-done-keywords done-keywords)
  10659. (setq tbl fulltable cnt 0)
  10660. (while (setq e (pop tbl))
  10661. (cond
  10662. ((equal e '(:startgroup))
  10663. (push '() groups) (setq ingroup t)
  10664. (when (not (= cnt 0))
  10665. (setq cnt 0)
  10666. (insert "\n"))
  10667. (insert "{ "))
  10668. ((equal e '(:endgroup))
  10669. (setq ingroup nil cnt 0)
  10670. (insert "}\n"))
  10671. ((equal e '(:newline))
  10672. (when (not (= cnt 0))
  10673. (setq cnt 0)
  10674. (insert "\n")
  10675. (setq e (car tbl))
  10676. (while (equal (car tbl) '(:newline))
  10677. (insert "\n")
  10678. (setq tbl (cdr tbl)))))
  10679. (t
  10680. (setq tg (car e) c (cdr e))
  10681. (if ingroup (push tg (car groups)))
  10682. (setq tg (org-add-props tg nil 'face
  10683. (org-get-todo-face tg)))
  10684. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10685. (insert "[" c "] " tg (make-string
  10686. (- fwidth 4 (length tg)) ?\ ))
  10687. (when (= (setq cnt (1+ cnt)) ncol)
  10688. (insert "\n")
  10689. (if ingroup (insert " "))
  10690. (setq cnt 0)))))
  10691. (insert "\n")
  10692. (goto-char (point-min))
  10693. (if (not expert) (org-fit-window-to-buffer))
  10694. (message "[a-z..]:Set [SPC]:clear")
  10695. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10696. (cond
  10697. ((or (= c ?\C-g)
  10698. (and (= c ?q) (not (rassoc c fulltable))))
  10699. (setq quit-flag t))
  10700. ((= c ?\ ) nil)
  10701. ((setq e (rassoc c fulltable) tg (car e))
  10702. tg)
  10703. (t (setq quit-flag t)))))))
  10704. (defun org-entry-is-todo-p ()
  10705. (member (org-get-todo-state) org-not-done-keywords))
  10706. (defun org-entry-is-done-p ()
  10707. (member (org-get-todo-state) org-done-keywords))
  10708. (defun org-get-todo-state ()
  10709. (save-excursion
  10710. (org-back-to-heading t)
  10711. (and (looking-at org-todo-line-regexp)
  10712. (match-end 2)
  10713. (match-string 2))))
  10714. (defun org-at-date-range-p (&optional inactive-ok)
  10715. "Is the cursor inside a date range?"
  10716. (interactive)
  10717. (save-excursion
  10718. (catch 'exit
  10719. (let ((pos (point)))
  10720. (skip-chars-backward "^[<\r\n")
  10721. (skip-chars-backward "<[")
  10722. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10723. (>= (match-end 0) pos)
  10724. (throw 'exit t))
  10725. (skip-chars-backward "^<[\r\n")
  10726. (skip-chars-backward "<[")
  10727. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10728. (>= (match-end 0) pos)
  10729. (throw 'exit t)))
  10730. nil)))
  10731. (defun org-get-repeat (&optional tagline)
  10732. "Check if there is a deadline/schedule with repeater in this entry."
  10733. (save-match-data
  10734. (save-excursion
  10735. (org-back-to-heading t)
  10736. (and (re-search-forward (if tagline
  10737. (concat tagline "\\s-*" org-repeat-re)
  10738. org-repeat-re)
  10739. (org-entry-end-position) t)
  10740. (match-string-no-properties 1)))))
  10741. (defvar org-last-changed-timestamp)
  10742. (defvar org-last-inserted-timestamp)
  10743. (defvar org-log-post-message)
  10744. (defvar org-log-note-purpose)
  10745. (defvar org-log-note-how)
  10746. (defvar org-log-note-extra)
  10747. (defun org-auto-repeat-maybe (done-word)
  10748. "Check if the current headline contains a repeated deadline/schedule.
  10749. If yes, set TODO state back to what it was and change the base date
  10750. of repeating deadline/scheduled time stamps to new date.
  10751. This function is run automatically after each state change to a DONE state."
  10752. ;; last-state is dynamically scoped into this function
  10753. (let* ((repeat (org-get-repeat))
  10754. (aa (assoc last-state org-todo-kwd-alist))
  10755. (interpret (nth 1 aa))
  10756. (head (nth 2 aa))
  10757. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10758. (msg "Entry repeats: ")
  10759. (org-log-done nil)
  10760. (org-todo-log-states nil)
  10761. re type n what ts time to-state)
  10762. (when repeat
  10763. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10764. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10765. org-todo-repeat-to-state))
  10766. (unless (and to-state (member to-state org-todo-keywords-1))
  10767. (setq to-state (if (eq interpret 'type) last-state head)))
  10768. (org-todo to-state)
  10769. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10770. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10771. (org-time-stamp-format t t))))
  10772. (when org-log-repeat
  10773. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10774. (memq 'org-add-log-note post-command-hook))
  10775. ;; OK, we are already setup for some record
  10776. (if (eq org-log-repeat 'note)
  10777. ;; make sure we take a note, not only a time stamp
  10778. (setq org-log-note-how 'note))
  10779. ;; Set up for taking a record
  10780. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10781. last-state
  10782. 'findpos org-log-repeat)))
  10783. (org-back-to-heading t)
  10784. (org-add-planning-info nil nil 'closed)
  10785. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10786. org-deadline-time-regexp "\\)\\|\\("
  10787. org-ts-regexp "\\)"))
  10788. (while (re-search-forward
  10789. re (save-excursion (outline-next-heading) (point)) t)
  10790. (setq type (if (match-end 1) org-scheduled-string
  10791. (if (match-end 3) org-deadline-string "Plain:"))
  10792. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10793. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10794. (setq n (string-to-number (match-string 2 ts))
  10795. what (match-string 3 ts))
  10796. (if (equal what "w") (setq n (* n 7) what "d"))
  10797. ;; Preparation, see if we need to modify the start date for the change
  10798. (when (match-end 1)
  10799. (setq time (save-match-data (org-time-string-to-time ts)))
  10800. (cond
  10801. ((equal (match-string 1 ts) ".")
  10802. ;; Shift starting date to today
  10803. (org-timestamp-change
  10804. (- (org-today) (time-to-days time))
  10805. 'day))
  10806. ((equal (match-string 1 ts) "+")
  10807. (let ((nshiftmax 10) (nshift 0))
  10808. (while (or (= nshift 0)
  10809. (<= (time-to-days time)
  10810. (time-to-days (current-time))))
  10811. (when (= (incf nshift) nshiftmax)
  10812. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10813. (error "Abort")))
  10814. (org-timestamp-change n (cdr (assoc what whata)))
  10815. (org-at-timestamp-p t)
  10816. (setq ts (match-string 1))
  10817. (setq time (save-match-data (org-time-string-to-time ts)))))
  10818. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10819. ;; rematch, so that we have everything in place for the real shift
  10820. (org-at-timestamp-p t)
  10821. (setq ts (match-string 1))
  10822. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10823. (org-timestamp-change n (cdr (assoc what whata)))
  10824. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10825. (setq org-log-post-message msg)
  10826. (message "%s" msg))))
  10827. (defun org-show-todo-tree (arg)
  10828. "Make a compact tree which shows all headlines marked with TODO.
  10829. The tree will show the lines where the regexp matches, and all higher
  10830. headlines above the match.
  10831. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10832. With a numeric prefix N, construct a sparse tree for the Nth element
  10833. of `org-todo-keywords-1'."
  10834. (interactive "P")
  10835. (let ((case-fold-search nil)
  10836. (kwd-re
  10837. (cond ((null arg) org-not-done-regexp)
  10838. ((equal arg '(4))
  10839. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10840. (mapcar 'list org-todo-keywords-1))))
  10841. (concat "\\("
  10842. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10843. "\\)\\>")))
  10844. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10845. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10846. org-todo-keywords-1)))
  10847. (t (error "Invalid prefix argument: %s" arg)))))
  10848. (message "%d TODO entries found"
  10849. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10850. (defun org-deadline (&optional remove time)
  10851. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10852. With argument REMOVE, remove any deadline from the item.
  10853. With argument TIME, set the deadline at the corresponding date. TIME
  10854. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10855. (interactive "P")
  10856. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10857. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10858. 'region-start-level 'region))
  10859. org-loop-over-headlines-in-active-region)
  10860. (org-map-entries
  10861. `(org-deadline ',remove ,time)
  10862. org-loop-over-headlines-in-active-region
  10863. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10864. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10865. (repeater (and old-date
  10866. (string-match
  10867. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10868. old-date)
  10869. (match-string 1 old-date))))
  10870. (if remove
  10871. (progn
  10872. (when (and old-date org-log-redeadline)
  10873. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10874. org-log-redeadline))
  10875. (org-remove-timestamp-with-keyword org-deadline-string)
  10876. (message "Item no longer has a deadline."))
  10877. (org-add-planning-info 'deadline time 'closed)
  10878. (when (and old-date org-log-redeadline
  10879. (not (equal old-date
  10880. (substring org-last-inserted-timestamp 1 -1))))
  10881. (org-add-log-setup 'redeadline nil old-date 'findpos
  10882. org-log-redeadline))
  10883. (when repeater
  10884. (save-excursion
  10885. (org-back-to-heading t)
  10886. (when (re-search-forward (concat org-deadline-string " "
  10887. org-last-inserted-timestamp)
  10888. (save-excursion
  10889. (outline-next-heading) (point)) t)
  10890. (goto-char (1- (match-end 0)))
  10891. (insert " " repeater)
  10892. (setq org-last-inserted-timestamp
  10893. (concat (substring org-last-inserted-timestamp 0 -1)
  10894. " " repeater
  10895. (substring org-last-inserted-timestamp -1))))))
  10896. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10897. (defun org-schedule (&optional remove time)
  10898. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10899. With argument REMOVE, remove any scheduling date from the item.
  10900. With argument TIME, scheduled at the corresponding date. TIME can
  10901. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10902. (interactive "P")
  10903. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10904. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10905. 'region-start-level 'region))
  10906. org-loop-over-headlines-in-active-region)
  10907. (org-map-entries
  10908. `(org-schedule ',remove ,time)
  10909. org-loop-over-headlines-in-active-region
  10910. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10911. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10912. (repeater (and old-date
  10913. (string-match
  10914. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10915. old-date)
  10916. (match-string 1 old-date))))
  10917. (if remove
  10918. (progn
  10919. (when (and old-date org-log-reschedule)
  10920. (org-add-log-setup 'delschedule nil old-date 'findpos
  10921. org-log-reschedule))
  10922. (org-remove-timestamp-with-keyword org-scheduled-string)
  10923. (message "Item is no longer scheduled."))
  10924. (org-add-planning-info 'scheduled time 'closed)
  10925. (when (and old-date org-log-reschedule
  10926. (not (equal old-date
  10927. (substring org-last-inserted-timestamp 1 -1))))
  10928. (org-add-log-setup 'reschedule nil old-date 'findpos
  10929. org-log-reschedule))
  10930. (when repeater
  10931. (save-excursion
  10932. (org-back-to-heading t)
  10933. (when (re-search-forward (concat org-scheduled-string " "
  10934. org-last-inserted-timestamp)
  10935. (save-excursion
  10936. (outline-next-heading) (point)) t)
  10937. (goto-char (1- (match-end 0)))
  10938. (insert " " repeater)
  10939. (setq org-last-inserted-timestamp
  10940. (concat (substring org-last-inserted-timestamp 0 -1)
  10941. " " repeater
  10942. (substring org-last-inserted-timestamp -1))))))
  10943. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10944. (defun org-get-scheduled-time (pom &optional inherit)
  10945. "Get the scheduled time as a time tuple, of a format suitable
  10946. for calling org-schedule with, or if there is no scheduling,
  10947. returns nil."
  10948. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10949. (when time
  10950. (apply 'encode-time (org-parse-time-string time)))))
  10951. (defun org-get-deadline-time (pom &optional inherit)
  10952. "Get the deadline as a time tuple, of a format suitable for
  10953. calling org-deadline with, or if there is no scheduling, returns
  10954. nil."
  10955. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10956. (when time
  10957. (apply 'encode-time (org-parse-time-string time)))))
  10958. (defun org-remove-timestamp-with-keyword (keyword)
  10959. "Remove all time stamps with KEYWORD in the current entry."
  10960. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10961. beg)
  10962. (save-excursion
  10963. (org-back-to-heading t)
  10964. (setq beg (point))
  10965. (outline-next-heading)
  10966. (while (re-search-backward re beg t)
  10967. (replace-match "")
  10968. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10969. (equal (char-before) ?\ ))
  10970. (backward-delete-char 1)
  10971. (if (string-match "^[ \t]*$" (buffer-substring
  10972. (point-at-bol) (point-at-eol)))
  10973. (delete-region (point-at-bol)
  10974. (min (point-max) (1+ (point-at-eol))))))))))
  10975. (defun org-add-planning-info (what &optional time &rest remove)
  10976. "Insert new timestamp with keyword in the line directly after the headline.
  10977. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  10978. If non is given, the user is prompted for a date.
  10979. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10980. be removed."
  10981. (interactive)
  10982. (let (org-time-was-given org-end-time-was-given ts
  10983. end default-time default-input)
  10984. (catch 'exit
  10985. (when (and (memq what '(scheduled deadline))
  10986. (or (not time)
  10987. (and (stringp time)
  10988. (string-match "^[-+]+[0-9]" time))))
  10989. ;; Try to get a default date/time from existing timestamp
  10990. (save-excursion
  10991. (org-back-to-heading t)
  10992. (setq end (save-excursion (outline-next-heading) (point)))
  10993. (when (re-search-forward (if (eq what 'scheduled)
  10994. org-scheduled-time-regexp
  10995. org-deadline-time-regexp)
  10996. end t)
  10997. (setq ts (match-string 1)
  10998. default-time
  10999. (apply 'encode-time (org-parse-time-string ts))
  11000. default-input (and ts (org-get-compact-tod ts))))))
  11001. (when what
  11002. (setq time
  11003. (if (stringp time)
  11004. ;; This is a string (relative or absolute), set proper date
  11005. (apply 'encode-time
  11006. (org-read-date-analyze
  11007. time default-time (decode-time default-time)))
  11008. ;; If necessary, get the time from the user
  11009. (or time (org-read-date nil 'to-time nil nil
  11010. default-time default-input)))))
  11011. (when (and org-insert-labeled-timestamps-at-point
  11012. (member what '(scheduled deadline)))
  11013. (insert
  11014. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11015. (org-insert-time-stamp time org-time-was-given
  11016. nil nil nil (list org-end-time-was-given))
  11017. (setq what nil))
  11018. (save-excursion
  11019. (save-restriction
  11020. (let (col list elt ts buffer-invisibility-spec)
  11021. (org-back-to-heading t)
  11022. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11023. (goto-char (match-end 1))
  11024. (setq col (current-column))
  11025. (goto-char (match-end 0))
  11026. (if (eobp) (insert "\n") (forward-char 1))
  11027. (when (and (not what)
  11028. (not (looking-at
  11029. (concat "[ \t]*"
  11030. org-keyword-time-not-clock-regexp))))
  11031. ;; Nothing to add, nothing to remove...... :-)
  11032. (throw 'exit nil))
  11033. (if (and (not (looking-at org-outline-regexp))
  11034. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11035. "[^\r\n]*"))
  11036. (not (equal (match-string 1) org-clock-string)))
  11037. (narrow-to-region (match-beginning 0) (match-end 0))
  11038. (insert-before-markers "\n")
  11039. (backward-char 1)
  11040. (narrow-to-region (point) (point))
  11041. (and org-adapt-indentation (org-indent-to-column col)))
  11042. ;; Check if we have to remove something.
  11043. (setq list (cons what remove))
  11044. (while list
  11045. (setq elt (pop list))
  11046. (when (or (and (eq elt 'scheduled)
  11047. (re-search-forward org-scheduled-time-regexp nil t))
  11048. (and (eq elt 'deadline)
  11049. (re-search-forward org-deadline-time-regexp nil t))
  11050. (and (eq elt 'closed)
  11051. (re-search-forward org-closed-time-regexp nil t)))
  11052. (replace-match "")
  11053. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11054. (and (looking-at "[ \t]+") (replace-match ""))
  11055. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11056. (when what
  11057. (insert
  11058. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11059. (cond ((eq what 'scheduled) org-scheduled-string)
  11060. ((eq what 'deadline) org-deadline-string)
  11061. ((eq what 'closed) org-closed-string))
  11062. " ")
  11063. (setq ts (org-insert-time-stamp
  11064. time
  11065. (or org-time-was-given
  11066. (and (eq what 'closed) org-log-done-with-time))
  11067. (eq what 'closed)
  11068. nil nil (list org-end-time-was-given)))
  11069. (insert
  11070. (if (not (or (bolp) (eq (char-before) ?\ )
  11071. (memq (char-after) '(32 10))
  11072. (eobp))) " " ""))
  11073. (end-of-line 1))
  11074. (goto-char (point-min))
  11075. (widen)
  11076. (if (and (looking-at "[ \t]*\n")
  11077. (equal (char-before) ?\n))
  11078. (delete-region (1- (point)) (point-at-eol)))
  11079. ts))))))
  11080. (defvar org-log-note-marker (make-marker))
  11081. (defvar org-log-note-purpose nil)
  11082. (defvar org-log-note-state nil)
  11083. (defvar org-log-note-previous-state nil)
  11084. (defvar org-log-note-how nil)
  11085. (defvar org-log-note-extra nil)
  11086. (defvar org-log-note-window-configuration nil)
  11087. (defvar org-log-note-return-to (make-marker))
  11088. (defvar org-log-note-effective-time nil
  11089. "Remembered current time so that dynamically scoped
  11090. `org-extend-today-until' affects tha timestamps in state change
  11091. log")
  11092. (defvar org-log-post-message nil
  11093. "Message to be displayed after a log note has been stored.
  11094. The auto-repeater uses this.")
  11095. (defun org-add-note ()
  11096. "Add a note to the current entry.
  11097. This is done in the same way as adding a state change note."
  11098. (interactive)
  11099. (org-add-log-setup 'note nil nil 'findpos nil))
  11100. (defvar org-property-end-re)
  11101. (defun org-add-log-setup (&optional purpose state prev-state
  11102. findpos how extra)
  11103. "Set up the post command hook to take a note.
  11104. If this is about to TODO state change, the new state is expected in STATE.
  11105. When FINDPOS is non-nil, find the correct position for the note in
  11106. the current entry. If not, assume that it can be inserted at point.
  11107. HOW is an indicator what kind of note should be created.
  11108. EXTRA is additional text that will be inserted into the notes buffer."
  11109. (let* ((org-log-into-drawer (org-log-into-drawer))
  11110. (drawer (cond ((stringp org-log-into-drawer)
  11111. org-log-into-drawer)
  11112. (org-log-into-drawer "LOGBOOK")
  11113. (t nil))))
  11114. (save-restriction
  11115. (save-excursion
  11116. (when findpos
  11117. (org-back-to-heading t)
  11118. (narrow-to-region (point) (save-excursion
  11119. (outline-next-heading) (point)))
  11120. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11121. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11122. "[^\r\n]*\\)?"))
  11123. (goto-char (match-end 0))
  11124. (cond
  11125. (drawer
  11126. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11127. nil t)
  11128. (progn
  11129. (goto-char (match-end 0))
  11130. (or org-log-states-order-reversed
  11131. (and (re-search-forward org-property-end-re nil t)
  11132. (goto-char (1- (match-beginning 0))))))
  11133. (insert "\n:" drawer ":\n:END:")
  11134. (beginning-of-line 0)
  11135. (org-indent-line-function)
  11136. (beginning-of-line 2)
  11137. (org-indent-line-function)
  11138. (end-of-line 0)))
  11139. ((and org-log-state-notes-insert-after-drawers
  11140. (save-excursion
  11141. (forward-line) (looking-at org-drawer-regexp)))
  11142. (forward-line)
  11143. (while (looking-at org-drawer-regexp)
  11144. (goto-char (match-end 0))
  11145. (re-search-forward org-property-end-re (point-max) t)
  11146. (forward-line))
  11147. (forward-line -1)))
  11148. (unless org-log-states-order-reversed
  11149. (and (= (char-after) ?\n) (forward-char 1))
  11150. (org-skip-over-state-notes)
  11151. (skip-chars-backward " \t\n\r")))
  11152. (move-marker org-log-note-marker (point))
  11153. (setq org-log-note-purpose purpose
  11154. org-log-note-state state
  11155. org-log-note-previous-state prev-state
  11156. org-log-note-how how
  11157. org-log-note-extra extra
  11158. org-log-note-effective-time (org-current-effective-time))
  11159. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11160. (defun org-skip-over-state-notes ()
  11161. "Skip past the list of State notes in an entry."
  11162. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11163. (when (ignore-errors (goto-char (org-in-item-p)))
  11164. (let* ((struct (org-list-struct))
  11165. (prevs (org-list-prevs-alist struct)))
  11166. (while (looking-at "[ \t]*- State")
  11167. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11168. (org-list-get-item-end (point) struct)))))))
  11169. (defun org-add-log-note (&optional purpose)
  11170. "Pop up a window for taking a note, and add this note later at point."
  11171. (remove-hook 'post-command-hook 'org-add-log-note)
  11172. (setq org-log-note-window-configuration (current-window-configuration))
  11173. (delete-other-windows)
  11174. (move-marker org-log-note-return-to (point))
  11175. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11176. (goto-char org-log-note-marker)
  11177. (org-switch-to-buffer-other-window "*Org Note*")
  11178. (erase-buffer)
  11179. (if (memq org-log-note-how '(time state))
  11180. (let (current-prefix-arg) (org-store-log-note))
  11181. (let ((org-inhibit-startup t)) (org-mode))
  11182. (insert (format "# Insert note for %s.
  11183. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11184. (cond
  11185. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11186. ((eq org-log-note-purpose 'done) "closed todo item")
  11187. ((eq org-log-note-purpose 'state)
  11188. (format "state change from \"%s\" to \"%s\""
  11189. (or org-log-note-previous-state "")
  11190. (or org-log-note-state "")))
  11191. ((eq org-log-note-purpose 'reschedule)
  11192. "rescheduling")
  11193. ((eq org-log-note-purpose 'delschedule)
  11194. "no longer scheduled")
  11195. ((eq org-log-note-purpose 'redeadline)
  11196. "changing deadline")
  11197. ((eq org-log-note-purpose 'deldeadline)
  11198. "removing deadline")
  11199. ((eq org-log-note-purpose 'refile)
  11200. "refiling")
  11201. ((eq org-log-note-purpose 'note)
  11202. "this entry")
  11203. (t (error "This should not happen")))))
  11204. (if org-log-note-extra (insert org-log-note-extra))
  11205. (org-set-local 'org-finish-function 'org-store-log-note)
  11206. (run-hooks 'org-log-buffer-setup-hook)))
  11207. (defvar org-note-abort nil) ; dynamically scoped
  11208. (defun org-store-log-note ()
  11209. "Finish taking a log note, and insert it to where it belongs."
  11210. (let ((txt (buffer-string))
  11211. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11212. lines ind bul)
  11213. (kill-buffer (current-buffer))
  11214. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11215. (setq txt (replace-match "" t t txt)))
  11216. (if (string-match "\\s-+\\'" txt)
  11217. (setq txt (replace-match "" t t txt)))
  11218. (setq lines (org-split-string txt "\n"))
  11219. (when (and note (string-match "\\S-" note))
  11220. (setq note
  11221. (org-replace-escapes
  11222. note
  11223. (list (cons "%u" (user-login-name))
  11224. (cons "%U" user-full-name)
  11225. (cons "%t" (format-time-string
  11226. (org-time-stamp-format 'long 'inactive)
  11227. org-log-note-effective-time))
  11228. (cons "%T" (format-time-string
  11229. (org-time-stamp-format 'long nil)
  11230. org-log-note-effective-time))
  11231. (cons "%d" (format-time-string
  11232. (org-time-stamp-format nil 'inactive)
  11233. org-log-note-effective-time))
  11234. (cons "%D" (format-time-string
  11235. (org-time-stamp-format nil nil)
  11236. org-log-note-effective-time))
  11237. (cons "%s" (if org-log-note-state
  11238. (concat "\"" org-log-note-state "\"")
  11239. ""))
  11240. (cons "%S" (if org-log-note-previous-state
  11241. (concat "\"" org-log-note-previous-state "\"")
  11242. "\"\"")))))
  11243. (if lines (setq note (concat note " \\\\")))
  11244. (push note lines))
  11245. (when (or current-prefix-arg org-note-abort)
  11246. (when org-log-into-drawer
  11247. (org-remove-empty-drawer-at
  11248. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11249. org-log-note-marker))
  11250. (setq lines nil))
  11251. (when lines
  11252. (with-current-buffer (marker-buffer org-log-note-marker)
  11253. (save-excursion
  11254. (goto-char org-log-note-marker)
  11255. (move-marker org-log-note-marker nil)
  11256. (end-of-line 1)
  11257. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11258. (setq ind (save-excursion
  11259. (if (ignore-errors (goto-char (org-in-item-p)))
  11260. (let ((struct (org-list-struct)))
  11261. (org-list-get-ind
  11262. (org-list-get-top-point struct) struct))
  11263. (skip-chars-backward " \r\t\n")
  11264. (cond
  11265. ((and (org-at-heading-p)
  11266. org-adapt-indentation)
  11267. (1+ (org-current-level)))
  11268. ((org-at-heading-p) 0)
  11269. (t (org-get-indentation))))))
  11270. (setq bul (org-list-bullet-string "-"))
  11271. (org-indent-line-to ind)
  11272. (insert bul (pop lines))
  11273. (let ((ind-body (+ (length bul) ind)))
  11274. (while lines
  11275. (insert "\n")
  11276. (org-indent-line-to ind-body)
  11277. (insert (pop lines))))
  11278. (message "Note stored")
  11279. (org-back-to-heading t)
  11280. (org-cycle-hide-drawers 'children)))))
  11281. (set-window-configuration org-log-note-window-configuration)
  11282. (with-current-buffer (marker-buffer org-log-note-return-to)
  11283. (goto-char org-log-note-return-to))
  11284. (move-marker org-log-note-return-to nil)
  11285. (and org-log-post-message (message "%s" org-log-post-message)))
  11286. (defun org-remove-empty-drawer-at (drawer pos)
  11287. "Remove an empty drawer DRAWER at position POS.
  11288. POS may also be a marker."
  11289. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11290. (save-excursion
  11291. (save-restriction
  11292. (widen)
  11293. (goto-char pos)
  11294. (if (org-in-regexp
  11295. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11296. (replace-match ""))))))
  11297. (defun org-sparse-tree (&optional arg)
  11298. "Create a sparse tree, prompt for the details.
  11299. This command can create sparse trees. You first need to select the type
  11300. of match used to create the tree:
  11301. t Show all TODO entries.
  11302. T Show entries with a specific TODO keyword.
  11303. m Show entries selected by a tags/property match.
  11304. p Enter a property name and its value (both with completion on existing
  11305. names/values) and show entries with that property.
  11306. r Show entries matching a regular expression (`/' can be used as well)
  11307. d Show deadlines due within `org-deadline-warning-days'.
  11308. b Show deadlines and scheduled items before a date.
  11309. a Show deadlines and scheduled items after a date."
  11310. (interactive "P")
  11311. (let (ans kwd value)
  11312. (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")
  11313. (setq ans (read-char-exclusive))
  11314. (cond
  11315. ((equal ans ?d)
  11316. (call-interactively 'org-check-deadlines))
  11317. ((equal ans ?b)
  11318. (call-interactively 'org-check-before-date))
  11319. ((equal ans ?a)
  11320. (call-interactively 'org-check-after-date))
  11321. ((equal ans ?D)
  11322. (call-interactively 'org-check-dates-range))
  11323. ((equal ans ?t)
  11324. (org-show-todo-tree nil))
  11325. ((equal ans ?T)
  11326. (org-show-todo-tree '(4)))
  11327. ((member ans '(?T ?m))
  11328. (call-interactively 'org-match-sparse-tree))
  11329. ((member ans '(?p ?P))
  11330. (setq kwd (org-icompleting-read "Property: "
  11331. (mapcar 'list (org-buffer-property-keys))))
  11332. (setq value (org-icompleting-read "Value: "
  11333. (mapcar 'list (org-property-values kwd))))
  11334. (unless (string-match "\\`{.*}\\'" value)
  11335. (setq value (concat "\"" value "\"")))
  11336. (org-match-sparse-tree arg (concat kwd "=" value)))
  11337. ((member ans '(?r ?R ?/))
  11338. (call-interactively 'org-occur))
  11339. (t (error "No such sparse tree command \"%c\"" ans)))))
  11340. (defvar org-occur-highlights nil
  11341. "List of overlays used for occur matches.")
  11342. (make-variable-buffer-local 'org-occur-highlights)
  11343. (defvar org-occur-parameters nil
  11344. "Parameters of the active org-occur calls.
  11345. This is a list, each call to org-occur pushes as cons cell,
  11346. containing the regular expression and the callback, onto the list.
  11347. The list can contain several entries if `org-occur' has been called
  11348. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11349. will only contain one set of parameters. When the highlights are
  11350. removed (for example with `C-c C-c', or with the next edit (depending
  11351. on `org-remove-highlights-with-change'), this variable is emptied
  11352. as well.")
  11353. (make-variable-buffer-local 'org-occur-parameters)
  11354. (defun org-occur (regexp &optional keep-previous callback)
  11355. "Make a compact tree which shows all matches of REGEXP.
  11356. The tree will show the lines where the regexp matches, and all higher
  11357. headlines above the match. It will also show the heading after the match,
  11358. to make sure editing the matching entry is easy.
  11359. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11360. call to `org-occur' will be kept, to allow stacking of calls to this
  11361. command.
  11362. If CALLBACK is non-nil, it is a function which is called to confirm
  11363. that the match should indeed be shown."
  11364. (interactive "sRegexp: \nP")
  11365. (when (equal regexp "")
  11366. (error "Regexp cannot be empty"))
  11367. (unless keep-previous
  11368. (org-remove-occur-highlights nil nil t))
  11369. (push (cons regexp callback) org-occur-parameters)
  11370. (let ((cnt 0))
  11371. (save-excursion
  11372. (goto-char (point-min))
  11373. (if (or (not keep-previous) ; do not want to keep
  11374. (not org-occur-highlights)) ; no previous matches
  11375. ;; hide everything
  11376. (org-overview))
  11377. (while (re-search-forward regexp nil t)
  11378. (when (or (not callback)
  11379. (save-match-data (funcall callback)))
  11380. (setq cnt (1+ cnt))
  11381. (when org-highlight-sparse-tree-matches
  11382. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11383. (org-show-context 'occur-tree))))
  11384. (when org-remove-highlights-with-change
  11385. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11386. nil 'local))
  11387. (unless org-sparse-tree-open-archived-trees
  11388. (org-hide-archived-subtrees (point-min) (point-max)))
  11389. (run-hooks 'org-occur-hook)
  11390. (if (org-called-interactively-p 'interactive)
  11391. (message "%d match(es) for regexp %s" cnt regexp))
  11392. cnt))
  11393. (defun org-occur-next-match (&optional n reset)
  11394. "Function for `next-error-function' to find sparse tree matches.
  11395. N is the number of matches to move, when negative move backwards.
  11396. RESET is entirely ignored - this function always goes back to the
  11397. starting point when no match is found."
  11398. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11399. (search-func (if (< n 0)
  11400. 'previous-single-char-property-change
  11401. 'next-single-char-property-change))
  11402. (n (abs n))
  11403. (pos (point))
  11404. p1)
  11405. (catch 'exit
  11406. (while (setq p1 (funcall search-func (point) 'org-type))
  11407. (when (equal p1 limit)
  11408. (goto-char pos)
  11409. (error "No more matches"))
  11410. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11411. (setq n (1- n))
  11412. (when (= n 0)
  11413. (goto-char p1)
  11414. (throw 'exit (point))))
  11415. (goto-char p1))
  11416. (goto-char p1)
  11417. (error "No more matches"))))
  11418. (defun org-show-context (&optional key)
  11419. "Make sure point and context are visible.
  11420. How much context is shown depends upon the variables
  11421. `org-show-hierarchy-above', `org-show-following-heading',
  11422. `org-show-entry-below' and `org-show-siblings'."
  11423. (let ((heading-p (org-at-heading-p t))
  11424. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11425. (following-p (org-get-alist-option org-show-following-heading key))
  11426. (entry-p (org-get-alist-option org-show-entry-below key))
  11427. (siblings-p (org-get-alist-option org-show-siblings key)))
  11428. (catch 'exit
  11429. ;; Show heading or entry text
  11430. (if (and heading-p (not entry-p))
  11431. (org-flag-heading nil) ; only show the heading
  11432. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11433. (org-show-hidden-entry))) ; show entire entry
  11434. (when following-p
  11435. ;; Show next sibling, or heading below text
  11436. (save-excursion
  11437. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11438. (org-flag-heading nil))))
  11439. (when siblings-p (org-show-siblings))
  11440. (when hierarchy-p
  11441. ;; show all higher headings, possibly with siblings
  11442. (save-excursion
  11443. (while (and (condition-case nil
  11444. (progn (org-up-heading-all 1) t)
  11445. (error nil))
  11446. (not (bobp)))
  11447. (org-flag-heading nil)
  11448. (when siblings-p (org-show-siblings))))))))
  11449. (defvar org-reveal-start-hook nil
  11450. "Hook run before revealing a location.")
  11451. (defun org-reveal (&optional siblings)
  11452. "Show current entry, hierarchy above it, and the following headline.
  11453. This can be used to show a consistent set of context around locations
  11454. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11455. not t for the search context.
  11456. With optional argument SIBLINGS, on each level of the hierarchy all
  11457. siblings are shown. This repairs the tree structure to what it would
  11458. look like when opened with hierarchical calls to `org-cycle'.
  11459. With double optional argument \\[universal-argument] \\[universal-argument], \
  11460. go to the parent and show the
  11461. entire tree."
  11462. (interactive "P")
  11463. (run-hooks 'org-reveal-start-hook)
  11464. (let ((org-show-hierarchy-above t)
  11465. (org-show-following-heading t)
  11466. (org-show-siblings (if siblings t org-show-siblings)))
  11467. (org-show-context nil))
  11468. (when (equal siblings '(16))
  11469. (save-excursion
  11470. (when (org-up-heading-safe)
  11471. (org-show-subtree)
  11472. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11473. (defun org-highlight-new-match (beg end)
  11474. "Highlight from BEG to END and mark the highlight is an occur headline."
  11475. (let ((ov (make-overlay beg end)))
  11476. (overlay-put ov 'face 'secondary-selection)
  11477. (overlay-put ov 'org-type 'org-occur)
  11478. (push ov org-occur-highlights)))
  11479. (defun org-remove-occur-highlights (&optional beg end noremove)
  11480. "Remove the occur highlights from the buffer.
  11481. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11482. from the `before-change-functions' in the current buffer."
  11483. (interactive)
  11484. (unless org-inhibit-highlight-removal
  11485. (mapc 'delete-overlay org-occur-highlights)
  11486. (setq org-occur-highlights nil)
  11487. (setq org-occur-parameters nil)
  11488. (unless noremove
  11489. (remove-hook 'before-change-functions
  11490. 'org-remove-occur-highlights 'local))))
  11491. ;;;; Priorities
  11492. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11493. "Regular expression matching the priority indicator.")
  11494. (defvar org-remove-priority-next-time nil)
  11495. (defun org-priority-up ()
  11496. "Increase the priority of the current item."
  11497. (interactive)
  11498. (org-priority 'up))
  11499. (defun org-priority-down ()
  11500. "Decrease the priority of the current item."
  11501. (interactive)
  11502. (org-priority 'down))
  11503. (defun org-priority (&optional action)
  11504. "Change the priority of an item by ARG.
  11505. ACTION can be `set', `up', `down', or a character."
  11506. (interactive)
  11507. (unless org-enable-priority-commands
  11508. (error "Priority commands are disabled"))
  11509. (setq action (or action 'set))
  11510. (let (current new news have remove)
  11511. (save-excursion
  11512. (org-back-to-heading t)
  11513. (if (looking-at org-priority-regexp)
  11514. (setq current (string-to-char (match-string 2))
  11515. have t))
  11516. (cond
  11517. ((eq action 'remove)
  11518. (setq remove t new ?\ ))
  11519. ((or (eq action 'set)
  11520. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11521. (if (not (eq action 'set))
  11522. (setq new action)
  11523. (message "Priority %c-%c, SPC to remove: "
  11524. org-highest-priority org-lowest-priority)
  11525. (save-match-data
  11526. (setq new (read-char-exclusive))))
  11527. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11528. (= (upcase org-lowest-priority) org-lowest-priority))
  11529. (setq new (upcase new)))
  11530. (cond ((equal new ?\ ) (setq remove t))
  11531. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11532. (error "Priority must be between `%c' and `%c'"
  11533. org-highest-priority org-lowest-priority))))
  11534. ((eq action 'up)
  11535. (setq new (if have
  11536. (1- current) ; normal cycling
  11537. ;; last priority was empty
  11538. (if (eq last-command this-command)
  11539. org-lowest-priority ; wrap around empty to lowest
  11540. ;; default
  11541. (if org-priority-start-cycle-with-default
  11542. org-default-priority
  11543. (1- org-default-priority))))))
  11544. ((eq action 'down)
  11545. (setq new (if have
  11546. (1+ current) ; normal cycling
  11547. ;; last priority was empty
  11548. (if (eq last-command this-command)
  11549. org-highest-priority ; wrap around empty to highest
  11550. ;; default
  11551. (if org-priority-start-cycle-with-default
  11552. org-default-priority
  11553. (1+ org-default-priority))))))
  11554. (t (error "Invalid action")))
  11555. (if (or (< (upcase new) org-highest-priority)
  11556. (> (upcase new) org-lowest-priority))
  11557. (if (and (memq action '(up down))
  11558. (not have) (not (eq last-command this-command)))
  11559. ;; `new' is from default priority
  11560. (error
  11561. "The default can not be set, see `org-default-priority' why")
  11562. ;; normal cycling: `new' is beyond highest/lowest priority
  11563. ;; and is wrapped around to the empty priority
  11564. (setq remove t)))
  11565. (setq news (format "%c" new))
  11566. (if have
  11567. (if remove
  11568. (replace-match "" t t nil 1)
  11569. (replace-match news t t nil 2))
  11570. (if remove
  11571. (error "No priority cookie found in line")
  11572. (let ((case-fold-search nil))
  11573. (looking-at org-todo-line-regexp))
  11574. (if (match-end 2)
  11575. (progn
  11576. (goto-char (match-end 2))
  11577. (insert " [#" news "]"))
  11578. (goto-char (match-beginning 3))
  11579. (insert "[#" news "] "))))
  11580. (org-preserve-lc (org-set-tags nil 'align)))
  11581. (if remove
  11582. (message "Priority removed")
  11583. (message "Priority of current item set to %s" news))))
  11584. (defun org-get-priority (s)
  11585. "Find priority cookie and return priority."
  11586. (if (functionp org-get-priority-function)
  11587. (funcall org-get-priority-function)
  11588. (save-match-data
  11589. (if (not (string-match org-priority-regexp s))
  11590. (* 1000 (- org-lowest-priority org-default-priority))
  11591. (* 1000 (- org-lowest-priority
  11592. (string-to-char (match-string 2 s))))))))
  11593. ;;;; Tags
  11594. (defvar org-agenda-archives-mode)
  11595. (defvar org-map-continue-from nil
  11596. "Position from where mapping should continue.
  11597. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11598. (defvar org-scanner-tags nil
  11599. "The current tag list while the tags scanner is running.")
  11600. (defvar org-trust-scanner-tags nil
  11601. "Should `org-get-tags-at' use the tags for the scanner.
  11602. This is for internal dynamical scoping only.
  11603. When this is non-nil, the function `org-get-tags-at' will return the value
  11604. of `org-scanner-tags' instead of building the list by itself. This
  11605. can lead to large speed-ups when the tags scanner is used in a file with
  11606. many entries, and when the list of tags is retrieved, for example to
  11607. obtain a list of properties. Building the tags list for each entry in such
  11608. a file becomes an N^2 operation - but with this variable set, it scales
  11609. as N.")
  11610. (defun org-scan-tags (action matcher &optional todo-only start-level)
  11611. "Scan headline tags with inheritance and produce output ACTION.
  11612. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11613. or `agenda' to produce an entry list for an agenda view. It can also be
  11614. a Lisp form or a function that should be called at each matched headline, in
  11615. this case the return value is a list of all return values from these calls.
  11616. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11617. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11618. only lines with a TODO keyword are included in the output.
  11619. START-LEVEL can be a string with asterisks, reducing the scope to
  11620. headlines matching this string."
  11621. (require 'org-agenda)
  11622. (let* ((re (concat "^"
  11623. (if start-level
  11624. ;; Get the correct level to match
  11625. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11626. org-outline-regexp)
  11627. " *\\(\\<\\("
  11628. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11629. (org-re
  11630. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11631. (props (list 'face 'default
  11632. 'done-face 'org-agenda-done
  11633. 'undone-face 'default
  11634. 'mouse-face 'highlight
  11635. 'org-not-done-regexp org-not-done-regexp
  11636. 'org-todo-regexp org-todo-regexp
  11637. 'org-complex-heading-regexp org-complex-heading-regexp
  11638. 'help-echo
  11639. (format "mouse-2 or RET jump to org file %s"
  11640. (abbreviate-file-name
  11641. (or (buffer-file-name (buffer-base-buffer))
  11642. (buffer-name (buffer-base-buffer)))))))
  11643. (case-fold-search nil)
  11644. (org-map-continue-from nil)
  11645. lspos tags tags-list
  11646. (tags-alist (list (cons 0 org-file-tags)))
  11647. (llast 0) rtn rtn1 level category i txt
  11648. todo marker entry priority)
  11649. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11650. (setq action (list 'lambda nil action)))
  11651. (save-excursion
  11652. (goto-char (point-min))
  11653. (when (eq action 'sparse-tree)
  11654. (org-overview)
  11655. (org-remove-occur-highlights))
  11656. (while (re-search-forward re nil t)
  11657. (setq org-map-continue-from nil)
  11658. (catch :skip
  11659. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11660. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11661. (goto-char (setq lspos (match-beginning 0)))
  11662. (setq level (org-reduced-level (funcall outline-level))
  11663. category (org-get-category))
  11664. (setq i llast llast level)
  11665. ;; remove tag lists from same and sublevels
  11666. (while (>= i level)
  11667. (when (setq entry (assoc i tags-alist))
  11668. (setq tags-alist (delete entry tags-alist)))
  11669. (setq i (1- i)))
  11670. ;; add the next tags
  11671. (when tags
  11672. (setq tags (org-split-string tags ":")
  11673. tags-alist
  11674. (cons (cons level tags) tags-alist)))
  11675. ;; compile tags for current headline
  11676. (setq tags-list
  11677. (if org-use-tag-inheritance
  11678. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11679. tags)
  11680. org-scanner-tags tags-list)
  11681. (when org-use-tag-inheritance
  11682. (setcdr (car tags-alist)
  11683. (mapcar (lambda (x)
  11684. (setq x (copy-sequence x))
  11685. (org-add-prop-inherited x))
  11686. (cdar tags-alist))))
  11687. (when (and tags org-use-tag-inheritance
  11688. (or (not (eq t org-use-tag-inheritance))
  11689. org-tags-exclude-from-inheritance))
  11690. ;; selective inheritance, remove uninherited ones
  11691. (setcdr (car tags-alist)
  11692. (org-remove-uninherited-tags (cdar tags-alist))))
  11693. (when (and
  11694. ;; eval matcher only when the todo condition is OK
  11695. (and (or (not todo-only) (member todo org-not-done-keywords))
  11696. (let ((case-fold-search t)) (eval matcher)))
  11697. ;; Call the skipper, but return t if it does not skip,
  11698. ;; so that the `and' form continues evaluating
  11699. (progn
  11700. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11701. t)
  11702. ;; Check if timestamps are deselecting this entry
  11703. (or (not todo-only)
  11704. (and (member todo org-not-done-keywords)
  11705. (or (not org-agenda-tags-todo-honor-ignore-options)
  11706. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11707. ;; Extra check for the archive tag
  11708. ;; FIXME: Does the skipper already do this????
  11709. (or
  11710. (not (member org-archive-tag tags-list))
  11711. ;; we have an archive tag, should we use this anyway?
  11712. (or (not org-agenda-skip-archived-trees)
  11713. (and (eq action 'agenda) org-agenda-archives-mode))))
  11714. ;; select this headline
  11715. (cond
  11716. ((eq action 'sparse-tree)
  11717. (and org-highlight-sparse-tree-matches
  11718. (org-get-heading) (match-end 0)
  11719. (org-highlight-new-match
  11720. (match-beginning 1) (match-end 1)))
  11721. (org-show-context 'tags-tree))
  11722. ((eq action 'agenda)
  11723. (setq txt (org-agenda-format-item
  11724. ""
  11725. (concat
  11726. (if (eq org-tags-match-list-sublevels 'indented)
  11727. (make-string (1- level) ?.) "")
  11728. (org-get-heading))
  11729. category
  11730. tags-list
  11731. )
  11732. priority (org-get-priority txt))
  11733. (goto-char lspos)
  11734. (setq marker (org-agenda-new-marker))
  11735. (org-add-props txt props
  11736. 'org-marker marker 'org-hd-marker marker 'org-category category
  11737. 'todo-state todo
  11738. 'priority priority 'type "tagsmatch")
  11739. (push txt rtn))
  11740. ((functionp action)
  11741. (setq org-map-continue-from nil)
  11742. (save-excursion
  11743. (setq rtn1 (funcall action))
  11744. (push rtn1 rtn)))
  11745. (t (error "Invalid action")))
  11746. ;; if we are to skip sublevels, jump to end of subtree
  11747. (unless org-tags-match-list-sublevels
  11748. (org-end-of-subtree t)
  11749. (backward-char 1))))
  11750. ;; Get the correct position from where to continue
  11751. (if org-map-continue-from
  11752. (goto-char org-map-continue-from)
  11753. (and (= (point) lspos) (end-of-line 1)))))
  11754. (when (and (eq action 'sparse-tree)
  11755. (not org-sparse-tree-open-archived-trees))
  11756. (org-hide-archived-subtrees (point-min) (point-max)))
  11757. (nreverse rtn)))
  11758. (defun org-remove-uninherited-tags (tags)
  11759. "Remove all tags that are not inherited from the list TAGS."
  11760. (cond
  11761. ((eq org-use-tag-inheritance t)
  11762. (if org-tags-exclude-from-inheritance
  11763. (org-delete-all org-tags-exclude-from-inheritance tags)
  11764. tags))
  11765. ((not org-use-tag-inheritance) nil)
  11766. ((stringp org-use-tag-inheritance)
  11767. (delq nil (mapcar
  11768. (lambda (x)
  11769. (if (and (string-match org-use-tag-inheritance x)
  11770. (not (member x org-tags-exclude-from-inheritance)))
  11771. x nil))
  11772. tags)))
  11773. ((listp org-use-tag-inheritance)
  11774. (delq nil (mapcar
  11775. (lambda (x)
  11776. (if (member x org-use-tag-inheritance) x nil))
  11777. tags)))))
  11778. (defvar todo-only) ;; dynamically scoped
  11779. (defun org-match-sparse-tree (&optional todo-only match)
  11780. "Create a sparse tree according to tags string MATCH.
  11781. MATCH can contain positive and negative selection of tags, like
  11782. \"+WORK+URGENT-WITHBOSS\".
  11783. If optional argument TODO-ONLY is non-nil, only select lines that are
  11784. also TODO lines."
  11785. (interactive "P")
  11786. (org-prepare-agenda-buffers (list (current-buffer)))
  11787. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11788. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11789. (defvar org-cached-props nil)
  11790. (defun org-cached-entry-get (pom property)
  11791. (if (or (eq t org-use-property-inheritance)
  11792. (and (stringp org-use-property-inheritance)
  11793. (string-match org-use-property-inheritance property))
  11794. (and (listp org-use-property-inheritance)
  11795. (member property org-use-property-inheritance)))
  11796. ;; Caching is not possible, check it directly
  11797. (org-entry-get pom property 'inherit)
  11798. ;; Get all properties, so that we can do complicated checks easily
  11799. (cdr (assoc property (or org-cached-props
  11800. (setq org-cached-props
  11801. (org-entry-properties pom)))))))
  11802. (defun org-global-tags-completion-table (&optional files)
  11803. "Return the list of all tags in all agenda buffer/files.
  11804. Optional FILES argument is a list of files to which can be used
  11805. instead of the agenda files."
  11806. (save-excursion
  11807. (org-uniquify
  11808. (delq nil
  11809. (apply 'append
  11810. (mapcar
  11811. (lambda (file)
  11812. (set-buffer (find-file-noselect file))
  11813. (append (org-get-buffer-tags)
  11814. (mapcar (lambda (x) (if (stringp (car-safe x))
  11815. (list (car-safe x)) nil))
  11816. org-tag-alist)))
  11817. (if (and files (car files))
  11818. files
  11819. (org-agenda-files))))))))
  11820. (defun org-make-tags-matcher (match)
  11821. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11822. ;; todo-only is scoped dynamically into this function, and the function
  11823. ;; may change it if the matcher asks for it.
  11824. (unless match
  11825. ;; Get a new match request, with completion
  11826. (let ((org-last-tags-completion-table
  11827. (org-global-tags-completion-table)))
  11828. (setq match (org-completing-read-no-i
  11829. "Match: " 'org-tags-completion-function nil nil nil
  11830. 'org-tags-history))))
  11831. ;; Parse the string and create a lisp form
  11832. (let ((match0 match)
  11833. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11834. minus tag mm
  11835. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11836. orterms term orlist re-p str-p level-p level-op time-p
  11837. prop-p pn pv po gv rest)
  11838. (if (string-match "/+" match)
  11839. ;; match contains also a todo-matching request
  11840. (progn
  11841. (setq tagsmatch (substring match 0 (match-beginning 0))
  11842. todomatch (substring match (match-end 0)))
  11843. (if (string-match "^!" todomatch)
  11844. (setq todo-only t todomatch (substring todomatch 1)))
  11845. (if (string-match "^\\s-*$" todomatch)
  11846. (setq todomatch nil)))
  11847. ;; only matching tags
  11848. (setq tagsmatch match todomatch nil))
  11849. ;; Make the tags matcher
  11850. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11851. (setq tagsmatcher t)
  11852. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11853. (while (setq term (pop orterms))
  11854. (while (and (equal (substring term -1) "\\") orterms)
  11855. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11856. (while (string-match re term)
  11857. (setq rest (substring term (match-end 0))
  11858. minus (and (match-end 1)
  11859. (equal (match-string 1 term) "-"))
  11860. tag (save-match-data (replace-regexp-in-string
  11861. "\\\\-" "-"
  11862. (match-string 2 term)))
  11863. re-p (equal (string-to-char tag) ?{)
  11864. level-p (match-end 4)
  11865. prop-p (match-end 5)
  11866. mm (cond
  11867. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11868. (level-p
  11869. (setq level-op (org-op-to-function (match-string 3 term)))
  11870. `(,level-op level ,(string-to-number
  11871. (match-string 4 term))))
  11872. (prop-p
  11873. (setq pn (match-string 5 term)
  11874. po (match-string 6 term)
  11875. pv (match-string 7 term)
  11876. re-p (equal (string-to-char pv) ?{)
  11877. str-p (equal (string-to-char pv) ?\")
  11878. time-p (save-match-data
  11879. (string-match "^\"[[<].*[]>]\"$" pv))
  11880. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11881. (if time-p (setq pv (org-matcher-time pv)))
  11882. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11883. (cond
  11884. ((equal pn "CATEGORY")
  11885. (setq gv '(get-text-property (point) 'org-category)))
  11886. ((equal pn "TODO")
  11887. (setq gv 'todo))
  11888. (t
  11889. (setq gv `(org-cached-entry-get nil ,pn))))
  11890. (if re-p
  11891. (if (eq po 'org<>)
  11892. `(not (string-match ,pv (or ,gv "")))
  11893. `(string-match ,pv (or ,gv "")))
  11894. (if str-p
  11895. `(,po (or ,gv "") ,pv)
  11896. `(,po (string-to-number (or ,gv ""))
  11897. ,(string-to-number pv) ))))
  11898. (t `(member ,tag tags-list)))
  11899. mm (if minus (list 'not mm) mm)
  11900. term rest)
  11901. (push mm tagsmatcher))
  11902. (push (if (> (length tagsmatcher) 1)
  11903. (cons 'and tagsmatcher)
  11904. (car tagsmatcher))
  11905. orlist)
  11906. (setq tagsmatcher nil))
  11907. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11908. (setq tagsmatcher
  11909. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11910. ;; Make the todo matcher
  11911. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11912. (setq todomatcher t)
  11913. (setq orterms (org-split-string todomatch "|") orlist nil)
  11914. (while (setq term (pop orterms))
  11915. (while (string-match re term)
  11916. (setq minus (and (match-end 1)
  11917. (equal (match-string 1 term) "-"))
  11918. kwd (match-string 2 term)
  11919. re-p (equal (string-to-char kwd) ?{)
  11920. term (substring term (match-end 0))
  11921. mm (if re-p
  11922. `(string-match ,(substring kwd 1 -1) todo)
  11923. (list 'equal 'todo kwd))
  11924. mm (if minus (list 'not mm) mm))
  11925. (push mm todomatcher))
  11926. (push (if (> (length todomatcher) 1)
  11927. (cons 'and todomatcher)
  11928. (car todomatcher))
  11929. orlist)
  11930. (setq todomatcher nil))
  11931. (setq todomatcher (if (> (length orlist) 1)
  11932. (cons 'or orlist) (car orlist))))
  11933. ;; Return the string and lisp forms of the matcher
  11934. (setq matcher (if todomatcher
  11935. (list 'and tagsmatcher todomatcher)
  11936. tagsmatcher))
  11937. (cons match0 matcher)))
  11938. (defun org-op-to-function (op &optional stringp)
  11939. "Turn an operator into the appropriate function."
  11940. (setq op
  11941. (cond
  11942. ((equal op "<" ) '(< string< org-time<))
  11943. ((equal op ">" ) '(> org-string> org-time>))
  11944. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11945. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11946. ((member op '("=" "==")) '(= string= org-time=))
  11947. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11948. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11949. (defun org<> (a b) (not (= a b)))
  11950. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11951. (defun org-string>= (a b) (not (string< a b)))
  11952. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11953. (defun org-string<> (a b) (not (string= a b)))
  11954. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11955. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11956. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11957. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11958. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11959. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11960. (defun org-2ft (s)
  11961. "Convert S to a floating point time.
  11962. If S is already a number, just return it. If it is a string, parse
  11963. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11964. (cond
  11965. ((numberp s) s)
  11966. ((stringp s)
  11967. (condition-case nil
  11968. (float-time (apply 'encode-time (org-parse-time-string s)))
  11969. (error 0.)))
  11970. (t 0.)))
  11971. (defun org-time-today ()
  11972. "Time in seconds today at 0:00.
  11973. Returns the float number of seconds since the beginning of the
  11974. epoch to the beginning of today (00:00)."
  11975. (float-time (apply 'encode-time
  11976. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11977. (defun org-matcher-time (s)
  11978. "Interpret a time comparison value."
  11979. (save-match-data
  11980. (cond
  11981. ((string= s "<now>") (float-time))
  11982. ((string= s "<today>") (org-time-today))
  11983. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11984. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11985. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11986. (+ (org-time-today)
  11987. (* (string-to-number (match-string 1 s))
  11988. (cdr (assoc (match-string 2 s)
  11989. '(("d" . 86400.0) ("w" . 604800.0)
  11990. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11991. (t (org-2ft s)))))
  11992. (defun org-match-any-p (re list)
  11993. "Does re match any element of list?"
  11994. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11995. (delq nil list))
  11996. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11997. (defvar org-tags-overlay (make-overlay 1 1))
  11998. (org-detach-overlay org-tags-overlay)
  11999. (defun org-get-local-tags-at (&optional pos)
  12000. "Get a list of tags defined in the current headline."
  12001. (org-get-tags-at pos 'local))
  12002. (defun org-get-local-tags ()
  12003. "Get a list of tags defined in the current headline."
  12004. (org-get-tags-at nil 'local))
  12005. (defun org-get-tags-at (&optional pos local)
  12006. "Get a list of all headline tags applicable at POS.
  12007. POS defaults to point. If tags are inherited, the list contains
  12008. the targets in the same sequence as the headlines appear, i.e.
  12009. the tags of the current headline come last.
  12010. When LOCAL is non-nil, only return tags from the current headline,
  12011. ignore inherited ones."
  12012. (interactive)
  12013. (if (and org-trust-scanner-tags
  12014. (or (not pos) (equal pos (point)))
  12015. (not local))
  12016. org-scanner-tags
  12017. (let (tags ltags lastpos parent)
  12018. (save-excursion
  12019. (save-restriction
  12020. (widen)
  12021. (goto-char (or pos (point)))
  12022. (save-match-data
  12023. (catch 'done
  12024. (condition-case nil
  12025. (progn
  12026. (org-back-to-heading t)
  12027. (while (not (equal lastpos (point)))
  12028. (setq lastpos (point))
  12029. (when (looking-at
  12030. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12031. (setq ltags (org-split-string
  12032. (org-match-string-no-properties 1) ":"))
  12033. (when parent
  12034. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12035. (setq tags (append
  12036. (if parent
  12037. (org-remove-uninherited-tags ltags)
  12038. ltags)
  12039. tags)))
  12040. (or org-use-tag-inheritance (throw 'done t))
  12041. (if local (throw 'done t))
  12042. (or (org-up-heading-safe) (error nil))
  12043. (setq parent t)))
  12044. (error nil)))))
  12045. (if local
  12046. tags
  12047. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12048. (defun org-add-prop-inherited (s)
  12049. (add-text-properties 0 (length s) '(inherited t) s)
  12050. s)
  12051. (defun org-toggle-tag (tag &optional onoff)
  12052. "Toggle the tag TAG for the current line.
  12053. If ONOFF is `on' or `off', don't toggle but set to this state."
  12054. (let (res current)
  12055. (save-excursion
  12056. (org-back-to-heading t)
  12057. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12058. (point-at-eol) t)
  12059. (progn
  12060. (setq current (match-string 1))
  12061. (replace-match ""))
  12062. (setq current ""))
  12063. (setq current (nreverse (org-split-string current ":")))
  12064. (cond
  12065. ((eq onoff 'on)
  12066. (setq res t)
  12067. (or (member tag current) (push tag current)))
  12068. ((eq onoff 'off)
  12069. (or (not (member tag current)) (setq current (delete tag current))))
  12070. (t (if (member tag current)
  12071. (setq current (delete tag current))
  12072. (setq res t)
  12073. (push tag current))))
  12074. (end-of-line 1)
  12075. (if current
  12076. (progn
  12077. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12078. (org-set-tags nil t))
  12079. (delete-horizontal-space))
  12080. (run-hooks 'org-after-tags-change-hook))
  12081. res))
  12082. (defun org-align-tags-here (to-col)
  12083. ;; Assumes that this is a headline
  12084. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12085. (beginning-of-line 1)
  12086. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12087. (< pos (match-beginning 2)))
  12088. (progn
  12089. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12090. (goto-char (match-beginning 1))
  12091. (insert " ")
  12092. (delete-region (point) (1+ (match-beginning 2)))
  12093. (setq ncol (max (current-column)
  12094. (1+ col)
  12095. (if (> to-col 0)
  12096. to-col
  12097. (- (abs to-col) tags-l))))
  12098. (setq p (point))
  12099. (insert (make-string (- ncol (current-column)) ?\ ))
  12100. (setq ncol (current-column))
  12101. (when indent-tabs-mode (tabify p (point-at-eol)))
  12102. (org-move-to-column (min ncol col) t))
  12103. (goto-char pos))))
  12104. (defun org-set-tags-command (&optional arg just-align)
  12105. "Call the set-tags command for the current entry."
  12106. (interactive "P")
  12107. (if (org-at-heading-p)
  12108. (org-set-tags arg just-align)
  12109. (save-excursion
  12110. (org-back-to-heading t)
  12111. (org-set-tags arg just-align))))
  12112. (defun org-set-tags-to (data)
  12113. "Set the tags of the current entry to DATA, replacing the current tags.
  12114. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12115. If DATA is nil or the empty string, any tags will be removed."
  12116. (interactive "sTags: ")
  12117. (setq data
  12118. (cond
  12119. ((eq data nil) "")
  12120. ((equal data "") "")
  12121. ((stringp data)
  12122. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12123. ":"))
  12124. ((listp data)
  12125. (concat ":" (mapconcat 'identity data ":") ":"))
  12126. (t nil)))
  12127. (when data
  12128. (save-excursion
  12129. (org-back-to-heading t)
  12130. (when (looking-at org-complex-heading-regexp)
  12131. (if (match-end 5)
  12132. (progn
  12133. (goto-char (match-beginning 5))
  12134. (insert data)
  12135. (delete-region (point) (point-at-eol))
  12136. (org-set-tags nil 'align))
  12137. (goto-char (point-at-eol))
  12138. (insert " " data)
  12139. (org-set-tags nil 'align)))
  12140. (beginning-of-line 1)
  12141. (if (looking-at ".*?\\([ \t]+\\)$")
  12142. (delete-region (match-beginning 1) (match-end 1))))))
  12143. (defun org-align-all-tags ()
  12144. "Align the tags i all headings."
  12145. (interactive)
  12146. (save-excursion
  12147. (or (ignore-errors (org-back-to-heading t))
  12148. (outline-next-heading))
  12149. (if (org-at-heading-p)
  12150. (org-set-tags t)
  12151. (message "No headings"))))
  12152. (defvar org-indent-indentation-per-level)
  12153. (defun org-set-tags (&optional arg just-align)
  12154. "Set the tags for the current headline.
  12155. With prefix ARG, realign all tags in headings in the current buffer."
  12156. (interactive "P")
  12157. (let* ((re org-outline-regexp-bol)
  12158. (current (org-get-tags-string))
  12159. (col (current-column))
  12160. (org-setting-tags t)
  12161. table current-tags inherited-tags ; computed below when needed
  12162. tags p0 c0 c1 rpl di tc level)
  12163. (if arg
  12164. (save-excursion
  12165. (goto-char (point-min))
  12166. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12167. (while (re-search-forward re nil t)
  12168. (org-set-tags nil t)
  12169. (end-of-line 1)))
  12170. (message "All tags realigned to column %d" org-tags-column))
  12171. (if just-align
  12172. (setq tags current)
  12173. ;; Get a new set of tags from the user
  12174. (save-excursion
  12175. (setq table (append org-tag-persistent-alist
  12176. (or org-tag-alist (org-get-buffer-tags))
  12177. (and
  12178. org-complete-tags-always-offer-all-agenda-tags
  12179. (org-global-tags-completion-table
  12180. (org-agenda-files))))
  12181. org-last-tags-completion-table table
  12182. current-tags (org-split-string current ":")
  12183. inherited-tags (nreverse
  12184. (nthcdr (length current-tags)
  12185. (nreverse (org-get-tags-at))))
  12186. tags
  12187. (if (or (eq t org-use-fast-tag-selection)
  12188. (and org-use-fast-tag-selection
  12189. (delq nil (mapcar 'cdr table))))
  12190. (org-fast-tag-selection
  12191. current-tags inherited-tags table
  12192. (if org-fast-tag-selection-include-todo
  12193. org-todo-key-alist))
  12194. (let ((org-add-colon-after-tag-completion t))
  12195. (org-trim
  12196. (org-icompleting-read "Tags: "
  12197. 'org-tags-completion-function
  12198. nil nil current 'org-tags-history))))))
  12199. (while (string-match "[-+&]+" tags)
  12200. ;; No boolean logic, just a list
  12201. (setq tags (replace-match ":" t t tags))))
  12202. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12203. (if org-tags-sort-function
  12204. (setq tags (mapconcat 'identity
  12205. (sort (org-split-string
  12206. tags (org-re "[^[:alnum:]_@#%]+"))
  12207. org-tags-sort-function) ":")))
  12208. (if (string-match "\\`[\t ]*\\'" tags)
  12209. (setq tags "")
  12210. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12211. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12212. ;; Insert new tags at the correct column
  12213. (beginning-of-line 1)
  12214. (setq level (or (and (looking-at org-outline-regexp)
  12215. (- (match-end 0) (point) 1))
  12216. 1))
  12217. (cond
  12218. ((and (equal current "") (equal tags "")))
  12219. ((re-search-forward
  12220. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12221. (point-at-eol) t)
  12222. (if (equal tags "")
  12223. (setq rpl "")
  12224. (goto-char (match-beginning 0))
  12225. (setq c0 (current-column)
  12226. ;; compute offset for the case of org-indent-mode active
  12227. di (if org-indent-mode
  12228. (* (1- org-indent-indentation-per-level) (1- level))
  12229. 0)
  12230. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12231. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12232. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12233. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12234. (replace-match rpl t t)
  12235. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12236. tags)
  12237. (t (error "Tags alignment failed")))
  12238. (org-move-to-column col)
  12239. (unless just-align
  12240. (run-hooks 'org-after-tags-change-hook)))))
  12241. (defun org-change-tag-in-region (beg end tag off)
  12242. "Add or remove TAG for each entry in the region.
  12243. This works in the agenda, and also in an org-mode buffer."
  12244. (interactive
  12245. (list (region-beginning) (region-end)
  12246. (let ((org-last-tags-completion-table
  12247. (if (eq major-mode 'org-mode)
  12248. (org-get-buffer-tags)
  12249. (org-global-tags-completion-table))))
  12250. (org-icompleting-read
  12251. "Tag: " 'org-tags-completion-function nil nil nil
  12252. 'org-tags-history))
  12253. (progn
  12254. (message "[s]et or [r]emove? ")
  12255. (equal (read-char-exclusive) ?r))))
  12256. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12257. (let ((agendap (equal major-mode 'org-agenda-mode))
  12258. l1 l2 m buf pos newhead (cnt 0))
  12259. (goto-char end)
  12260. (setq l2 (1- (org-current-line)))
  12261. (goto-char beg)
  12262. (setq l1 (org-current-line))
  12263. (loop for l from l1 to l2 do
  12264. (org-goto-line l)
  12265. (setq m (get-text-property (point) 'org-hd-marker))
  12266. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12267. (and agendap m))
  12268. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12269. pos (if agendap m (point)))
  12270. (with-current-buffer buf
  12271. (save-excursion
  12272. (save-restriction
  12273. (goto-char pos)
  12274. (setq cnt (1+ cnt))
  12275. (org-toggle-tag tag (if off 'off 'on))
  12276. (setq newhead (org-get-heading)))))
  12277. (and agendap (org-agenda-change-all-lines newhead m))))
  12278. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12279. (defun org-tags-completion-function (string predicate &optional flag)
  12280. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12281. (confirm (lambda (x) (stringp (car x)))))
  12282. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12283. (setq s1 (match-string 1 string)
  12284. s2 (match-string 2 string))
  12285. (setq s1 "" s2 string))
  12286. (cond
  12287. ((eq flag nil)
  12288. ;; try completion
  12289. (setq rtn (try-completion s2 ctable confirm))
  12290. (if (stringp rtn)
  12291. (setq rtn
  12292. (concat s1 s2 (substring rtn (length s2))
  12293. (if (and org-add-colon-after-tag-completion
  12294. (assoc rtn ctable))
  12295. ":" ""))))
  12296. rtn)
  12297. ((eq flag t)
  12298. ;; all-completions
  12299. (all-completions s2 ctable confirm)
  12300. )
  12301. ((eq flag 'lambda)
  12302. ;; exact match?
  12303. (assoc s2 ctable)))
  12304. ))
  12305. (defun org-fast-tag-insert (kwd tags face &optional end)
  12306. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12307. (insert (format "%-12s" (concat kwd ":"))
  12308. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12309. (or end "")))
  12310. (defun org-fast-tag-show-exit (flag)
  12311. (save-excursion
  12312. (org-goto-line 3)
  12313. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12314. (replace-match ""))
  12315. (when flag
  12316. (end-of-line 1)
  12317. (org-move-to-column (- (window-width) 19) t)
  12318. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12319. (defun org-set-current-tags-overlay (current prefix)
  12320. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12321. (if (featurep 'xemacs)
  12322. (org-overlay-display org-tags-overlay (concat prefix s)
  12323. 'secondary-selection)
  12324. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12325. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12326. (defvar org-last-tag-selection-key nil)
  12327. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12328. "Fast tag selection with single keys.
  12329. CURRENT is the current list of tags in the headline, INHERITED is the
  12330. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12331. possibly with grouping information. TODO-TABLE is a similar table with
  12332. TODO keywords, should these have keys assigned to them.
  12333. If the keys are nil, a-z are automatically assigned.
  12334. Returns the new tags string, or nil to not change the current settings."
  12335. (let* ((fulltable (append table todo-table))
  12336. (maxlen (apply 'max (mapcar
  12337. (lambda (x)
  12338. (if (stringp (car x)) (string-width (car x)) 0))
  12339. fulltable)))
  12340. (buf (current-buffer))
  12341. (expert (eq org-fast-tag-selection-single-key 'expert))
  12342. (buffer-tags nil)
  12343. (fwidth (+ maxlen 3 1 3))
  12344. (ncol (/ (- (window-width) 4) fwidth))
  12345. (i-face 'org-done)
  12346. (c-face 'org-todo)
  12347. tg cnt e c char c1 c2 ntable tbl rtn
  12348. ov-start ov-end ov-prefix
  12349. (exit-after-next org-fast-tag-selection-single-key)
  12350. (done-keywords org-done-keywords)
  12351. groups ingroup)
  12352. (save-excursion
  12353. (beginning-of-line 1)
  12354. (if (looking-at
  12355. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12356. (setq ov-start (match-beginning 1)
  12357. ov-end (match-end 1)
  12358. ov-prefix "")
  12359. (setq ov-start (1- (point-at-eol))
  12360. ov-end (1+ ov-start))
  12361. (skip-chars-forward "^\n\r")
  12362. (setq ov-prefix
  12363. (concat
  12364. (buffer-substring (1- (point)) (point))
  12365. (if (> (current-column) org-tags-column)
  12366. " "
  12367. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12368. (move-overlay org-tags-overlay ov-start ov-end)
  12369. (save-window-excursion
  12370. (if expert
  12371. (set-buffer (get-buffer-create " *Org tags*"))
  12372. (delete-other-windows)
  12373. (split-window-vertically)
  12374. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12375. (erase-buffer)
  12376. (org-set-local 'org-done-keywords done-keywords)
  12377. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12378. (org-fast-tag-insert "Current" current c-face "\n\n")
  12379. (org-fast-tag-show-exit exit-after-next)
  12380. (org-set-current-tags-overlay current ov-prefix)
  12381. (setq tbl fulltable char ?a cnt 0)
  12382. (while (setq e (pop tbl))
  12383. (cond
  12384. ((equal (car e) :startgroup)
  12385. (push '() groups) (setq ingroup t)
  12386. (when (not (= cnt 0))
  12387. (setq cnt 0)
  12388. (insert "\n"))
  12389. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12390. ((equal (car e) :endgroup)
  12391. (setq ingroup nil cnt 0)
  12392. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12393. ((equal e '(:newline))
  12394. (when (not (= cnt 0))
  12395. (setq cnt 0)
  12396. (insert "\n")
  12397. (setq e (car tbl))
  12398. (while (equal (car tbl) '(:newline))
  12399. (insert "\n")
  12400. (setq tbl (cdr tbl)))))
  12401. (t
  12402. (setq tg (copy-sequence (car e)) c2 nil)
  12403. (if (cdr e)
  12404. (setq c (cdr e))
  12405. ;; automatically assign a character.
  12406. (setq c1 (string-to-char
  12407. (downcase (substring
  12408. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12409. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12410. (while (or (rassoc char ntable) (rassoc char table))
  12411. (setq char (1+ char)))
  12412. (setq c2 c1))
  12413. (setq c (or c2 char)))
  12414. (if ingroup (push tg (car groups)))
  12415. (setq tg (org-add-props tg nil 'face
  12416. (cond
  12417. ((not (assoc tg table))
  12418. (org-get-todo-face tg))
  12419. ((member tg current) c-face)
  12420. ((member tg inherited) i-face)
  12421. (t nil))))
  12422. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12423. (insert "[" c "] " tg (make-string
  12424. (- fwidth 4 (length tg)) ?\ ))
  12425. (push (cons tg c) ntable)
  12426. (when (= (setq cnt (1+ cnt)) ncol)
  12427. (insert "\n")
  12428. (if ingroup (insert " "))
  12429. (setq cnt 0)))))
  12430. (setq ntable (nreverse ntable))
  12431. (insert "\n")
  12432. (goto-char (point-min))
  12433. (if (not expert) (org-fit-window-to-buffer))
  12434. (setq rtn
  12435. (catch 'exit
  12436. (while t
  12437. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12438. (if (not groups) "no " "")
  12439. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12440. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12441. (setq org-last-tag-selection-key c)
  12442. (cond
  12443. ((= c ?\r) (throw 'exit t))
  12444. ((= c ?!)
  12445. (setq groups (not groups))
  12446. (goto-char (point-min))
  12447. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12448. ((= c ?\C-c)
  12449. (if (not expert)
  12450. (org-fast-tag-show-exit
  12451. (setq exit-after-next (not exit-after-next)))
  12452. (setq expert nil)
  12453. (delete-other-windows)
  12454. (set-window-buffer (split-window-vertically) " *Org tags*")
  12455. (org-switch-to-buffer-other-window " *Org tags*")
  12456. (org-fit-window-to-buffer)))
  12457. ((or (= c ?\C-g)
  12458. (and (= c ?q) (not (rassoc c ntable))))
  12459. (org-detach-overlay org-tags-overlay)
  12460. (setq quit-flag t))
  12461. ((= c ?\ )
  12462. (setq current nil)
  12463. (if exit-after-next (setq exit-after-next 'now)))
  12464. ((= c ?\t)
  12465. (condition-case nil
  12466. (setq tg (org-icompleting-read
  12467. "Tag: "
  12468. (or buffer-tags
  12469. (with-current-buffer buf
  12470. (org-get-buffer-tags)))))
  12471. (quit (setq tg "")))
  12472. (when (string-match "\\S-" tg)
  12473. (add-to-list 'buffer-tags (list tg))
  12474. (if (member tg current)
  12475. (setq current (delete tg current))
  12476. (push tg current)))
  12477. (if exit-after-next (setq exit-after-next 'now)))
  12478. ((setq e (rassoc c todo-table) tg (car e))
  12479. (with-current-buffer buf
  12480. (save-excursion (org-todo tg)))
  12481. (if exit-after-next (setq exit-after-next 'now)))
  12482. ((setq e (rassoc c ntable) tg (car e))
  12483. (if (member tg current)
  12484. (setq current (delete tg current))
  12485. (loop for g in groups do
  12486. (if (member tg g)
  12487. (mapc (lambda (x)
  12488. (setq current (delete x current)))
  12489. g)))
  12490. (push tg current))
  12491. (if exit-after-next (setq exit-after-next 'now))))
  12492. ;; Create a sorted list
  12493. (setq current
  12494. (sort current
  12495. (lambda (a b)
  12496. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12497. (if (eq exit-after-next 'now) (throw 'exit t))
  12498. (goto-char (point-min))
  12499. (beginning-of-line 2)
  12500. (delete-region (point) (point-at-eol))
  12501. (org-fast-tag-insert "Current" current c-face)
  12502. (org-set-current-tags-overlay current ov-prefix)
  12503. (while (re-search-forward
  12504. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12505. (setq tg (match-string 1))
  12506. (add-text-properties
  12507. (match-beginning 1) (match-end 1)
  12508. (list 'face
  12509. (cond
  12510. ((member tg current) c-face)
  12511. ((member tg inherited) i-face)
  12512. (t (get-text-property (match-beginning 1) 'face))))))
  12513. (goto-char (point-min)))))
  12514. (org-detach-overlay org-tags-overlay)
  12515. (if rtn
  12516. (mapconcat 'identity current ":")
  12517. nil))))
  12518. (defun org-get-tags-string ()
  12519. "Get the TAGS string in the current headline."
  12520. (unless (org-at-heading-p t)
  12521. (error "Not on a heading"))
  12522. (save-excursion
  12523. (beginning-of-line 1)
  12524. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12525. (org-match-string-no-properties 1)
  12526. "")))
  12527. (defun org-get-tags ()
  12528. "Get the list of tags specified in the current headline."
  12529. (org-split-string (org-get-tags-string) ":"))
  12530. (defun org-get-buffer-tags ()
  12531. "Get a table of all tags used in the buffer, for completion."
  12532. (let (tags)
  12533. (save-excursion
  12534. (goto-char (point-min))
  12535. (while (re-search-forward
  12536. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12537. (when (equal (char-after (point-at-bol 0)) ?*)
  12538. (mapc (lambda (x) (add-to-list 'tags x))
  12539. (org-split-string (org-match-string-no-properties 1) ":")))))
  12540. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12541. (mapcar 'list tags)))
  12542. ;;;; The mapping API
  12543. ;;;###autoload
  12544. (defun org-map-entries (func &optional match scope &rest skip)
  12545. "Call FUNC at each headline selected by MATCH in SCOPE.
  12546. FUNC is a function or a lisp form. The function will be called without
  12547. arguments, with the cursor positioned at the beginning of the headline.
  12548. The return values of all calls to the function will be collected and
  12549. returned as a list.
  12550. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12551. does not need to preserve point. After evaluation, the cursor will be
  12552. moved to the end of the line (presumably of the headline of the
  12553. processed entry) and search continues from there. Under some
  12554. circumstances, this may not produce the wanted results. For example,
  12555. if you have removed (e.g. archived) the current (sub)tree it could
  12556. mean that the next entry will be skipped entirely. In such cases, you
  12557. can specify the position from where search should continue by making
  12558. FUNC set the variable `org-map-continue-from' to the desired buffer
  12559. position.
  12560. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12561. Only headlines that are matched by this query will be considered during
  12562. the iteration. When MATCH is nil or t, all headlines will be
  12563. visited by the iteration.
  12564. SCOPE determines the scope of this command. It can be any of:
  12565. nil The current buffer, respecting the restriction if any
  12566. tree The subtree started with the entry at point
  12567. region The entries within the active region, if any
  12568. region-start-level
  12569. The entries within the active region, but only those at
  12570. the same level than the first one.
  12571. file The current buffer, without restriction
  12572. file-with-archives
  12573. The current buffer, and any archives associated with it
  12574. agenda All agenda files
  12575. agenda-with-archives
  12576. All agenda files with any archive files associated with them
  12577. \(file1 file2 ...)
  12578. If this is a list, all files in the list will be scanned
  12579. The remaining args are treated as settings for the skipping facilities of
  12580. the scanner. The following items can be given here:
  12581. archive skip trees with the archive tag.
  12582. comment skip trees with the COMMENT keyword
  12583. function or Emacs Lisp form:
  12584. will be used as value for `org-agenda-skip-function', so whenever
  12585. the function returns t, FUNC will not be called for that
  12586. entry and search will continue from the point where the
  12587. function leaves it.
  12588. If your function needs to retrieve the tags including inherited tags
  12589. at the *current* entry, you can use the value of the variable
  12590. `org-scanner-tags' which will be much faster than getting the value
  12591. with `org-get-tags-at'. If your function gets properties with
  12592. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12593. to t around the call to `org-entry-properties' to get the same speedup.
  12594. Note that if your function moves around to retrieve tags and properties at
  12595. a *different* entry, you cannot use these techniques."
  12596. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12597. (not (org-region-active-p)))
  12598. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12599. (org-agenda-skip-archived-trees (memq 'archive skip))
  12600. (org-agenda-skip-comment-trees (memq 'comment skip))
  12601. (org-agenda-skip-function
  12602. (car (org-delete-all '(comment archive) skip)))
  12603. (org-tags-match-list-sublevels t)
  12604. (start-level (eq scope 'region-start-level))
  12605. matcher file res
  12606. org-todo-keywords-for-agenda
  12607. org-done-keywords-for-agenda
  12608. org-todo-keyword-alist-for-agenda
  12609. org-drawers-for-agenda
  12610. org-tag-alist-for-agenda)
  12611. (cond
  12612. ((eq match t) (setq matcher t))
  12613. ((eq match nil) (setq matcher t))
  12614. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12615. (save-excursion
  12616. (save-restriction
  12617. (cond ((eq scope 'tree)
  12618. (org-back-to-heading t)
  12619. (org-narrow-to-subtree)
  12620. (setq scope nil))
  12621. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12622. (org-region-active-p))
  12623. ;; If needed, set start-level to a string like "2"
  12624. (when start-level
  12625. (save-excursion
  12626. (goto-char (region-beginning))
  12627. (unless (org-at-heading-p) (outline-next-heading))
  12628. (setq start-level (org-current-level))))
  12629. (narrow-to-region (region-beginning)
  12630. (save-excursion
  12631. (goto-char (region-end))
  12632. (unless (and (bolp) (org-at-heading-p))
  12633. (outline-next-heading))
  12634. (point)))
  12635. (setq scope nil)))
  12636. (if (not scope)
  12637. (progn
  12638. (org-prepare-agenda-buffers
  12639. (list (buffer-file-name (current-buffer))))
  12640. (setq res (org-scan-tags func matcher nil start-level)))
  12641. ;; Get the right scope
  12642. (cond
  12643. ((and scope (listp scope) (symbolp (car scope)))
  12644. (setq scope (eval scope)))
  12645. ((eq scope 'agenda)
  12646. (setq scope (org-agenda-files t)))
  12647. ((eq scope 'agenda-with-archives)
  12648. (setq scope (org-agenda-files t))
  12649. (setq scope (org-add-archive-files scope)))
  12650. ((eq scope 'file)
  12651. (setq scope (list (buffer-file-name))))
  12652. ((eq scope 'file-with-archives)
  12653. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12654. (org-prepare-agenda-buffers scope)
  12655. (while (setq file (pop scope))
  12656. (with-current-buffer (org-find-base-buffer-visiting file)
  12657. (save-excursion
  12658. (save-restriction
  12659. (widen)
  12660. (goto-char (point-min))
  12661. (setq res (append res (org-scan-tags func matcher))))))))))
  12662. res)))
  12663. ;;;; Properties
  12664. ;;; Setting and retrieving properties
  12665. (defconst org-special-properties
  12666. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12667. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12668. "The special properties valid in Org-mode.
  12669. These are properties that are not defined in the property drawer,
  12670. but in some other way.")
  12671. (defconst org-default-properties
  12672. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12673. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12674. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12675. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12676. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12677. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12678. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12679. "Some properties that are used by Org-mode for various purposes.
  12680. Being in this list makes sure that they are offered for completion.")
  12681. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12682. "Regular expression matching the first line of a property drawer.")
  12683. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12684. "Regular expression matching the last line of a property drawer.")
  12685. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12686. "Regular expression matching the first line of a property drawer.")
  12687. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12688. "Regular expression matching the first line of a property drawer.")
  12689. (defconst org-property-drawer-re
  12690. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12691. org-property-end-re "\\)\n?")
  12692. "Matches an entire property drawer.")
  12693. (defconst org-clock-drawer-re
  12694. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12695. org-property-end-re "\\)\n?")
  12696. "Matches an entire clock drawer.")
  12697. (defsubst org-re-property (property)
  12698. "Return a regexp matching PROPERTY.
  12699. Match group 1 will be set to the value "
  12700. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12701. (defun org-property-action ()
  12702. "Do an action on properties."
  12703. (interactive)
  12704. (let (c)
  12705. (org-at-property-p)
  12706. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12707. (setq c (read-char-exclusive))
  12708. (cond
  12709. ((equal c ?s)
  12710. (call-interactively 'org-set-property))
  12711. ((equal c ?d)
  12712. (call-interactively 'org-delete-property))
  12713. ((equal c ?D)
  12714. (call-interactively 'org-delete-property-globally))
  12715. ((equal c ?c)
  12716. (call-interactively 'org-compute-property-at-point))
  12717. (t (error "No such property action %c" c)))))
  12718. (defun org-set-effort (&optional value)
  12719. "Set the effort property of the current entry.
  12720. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12721. allowed value."
  12722. (interactive "P")
  12723. (if (equal value 0) (setq value 10))
  12724. (let* ((completion-ignore-case t)
  12725. (prop org-effort-property)
  12726. (cur (org-entry-get nil prop))
  12727. (allowed (org-property-get-allowed-values nil prop 'table))
  12728. (existing (mapcar 'list (org-property-values prop)))
  12729. rpl
  12730. (val (cond
  12731. ((stringp value) value)
  12732. ((and allowed (integerp value))
  12733. (or (car (nth (1- value) allowed))
  12734. (car (org-last allowed))))
  12735. (allowed
  12736. (message "Select 1-9,0, [RET%s]: %s"
  12737. (if cur (concat "=" cur) "")
  12738. (mapconcat 'car allowed " "))
  12739. (setq rpl (read-char-exclusive))
  12740. (if (equal rpl ?\r)
  12741. cur
  12742. (setq rpl (- rpl ?0))
  12743. (if (equal rpl 0) (setq rpl 10))
  12744. (if (and (> rpl 0) (<= rpl (length allowed)))
  12745. (car (nth (1- rpl) allowed))
  12746. (org-completing-read "Effort: " allowed nil))))
  12747. (t
  12748. (let (org-completion-use-ido org-completion-use-iswitchb)
  12749. (org-completing-read
  12750. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12751. (concat "[" cur "]") "")
  12752. ": ")
  12753. existing nil nil "" nil cur))))))
  12754. (unless (equal (org-entry-get nil prop) val)
  12755. (org-entry-put nil prop val))
  12756. (message "%s is now %s" prop val)))
  12757. (defun org-at-property-p ()
  12758. "Is cursor inside a property drawer?"
  12759. (save-excursion
  12760. (beginning-of-line 1)
  12761. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12762. (save-match-data ;; Used by calling procedures
  12763. (let ((p (point))
  12764. (range (unless (org-before-first-heading-p)
  12765. (org-get-property-block))))
  12766. (and range (<= (car range) p) (< p (cdr range))))))))
  12767. (defun org-get-property-block (&optional beg end force)
  12768. "Return the (beg . end) range of the body of the property drawer.
  12769. BEG and END can be beginning and end of subtree, if not given
  12770. they will be found.
  12771. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12772. (catch 'exit
  12773. (save-excursion
  12774. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12775. (end (or end (progn (outline-next-heading) (point)))))
  12776. (goto-char beg)
  12777. (if (re-search-forward org-property-start-re end t)
  12778. (setq beg (1+ (match-end 0)))
  12779. (if force
  12780. (save-excursion
  12781. (org-insert-property-drawer)
  12782. (setq end (progn (outline-next-heading) (point))))
  12783. (throw 'exit nil))
  12784. (goto-char beg)
  12785. (if (re-search-forward org-property-start-re end t)
  12786. (setq beg (1+ (match-end 0)))))
  12787. (if (re-search-forward org-property-end-re end t)
  12788. (setq end (match-beginning 0))
  12789. (or force (throw 'exit nil))
  12790. (goto-char beg)
  12791. (setq end beg)
  12792. (org-indent-line-function)
  12793. (insert ":END:\n"))
  12794. (cons beg end)))))
  12795. (defun org-entry-properties (&optional pom which specific)
  12796. "Get all properties of the entry at point-or-marker POM.
  12797. This includes the TODO keyword, the tags, time strings for deadline,
  12798. scheduled, and clocking, and any additional properties defined in the
  12799. entry. The return value is an alist, keys may occur multiple times
  12800. if the property key was used several times.
  12801. POM may also be nil, in which case the current entry is used.
  12802. If WHICH is nil or `all', get all properties. If WHICH is
  12803. `special' or `standard', only get that subclass. If WHICH
  12804. is a string only get exactly this property. SPECIFIC can be a string, the
  12805. specific property we are interested in. Specifying it can speed
  12806. things up because then unnecessary parsing is avoided."
  12807. (setq which (or which 'all))
  12808. (org-with-point-at pom
  12809. (let ((clockstr (substring org-clock-string 0 -1))
  12810. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12811. (case-fold-search nil)
  12812. beg end range props sum-props key key1 value string clocksum)
  12813. (save-excursion
  12814. (when (condition-case nil
  12815. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12816. (error nil))
  12817. (setq beg (point))
  12818. (setq sum-props (get-text-property (point) 'org-summaries))
  12819. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12820. (outline-next-heading)
  12821. (setq end (point))
  12822. (when (memq which '(all special))
  12823. ;; Get the special properties, like TODO and tags
  12824. (goto-char beg)
  12825. (when (and (or (not specific) (string= specific "TODO"))
  12826. (looking-at org-todo-line-regexp) (match-end 2))
  12827. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12828. (when (and (or (not specific) (string= specific "PRIORITY"))
  12829. (looking-at org-priority-regexp))
  12830. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12831. (when (or (not specific) (string= specific "FILE"))
  12832. (push (cons "FILE" buffer-file-name) props))
  12833. (when (and (or (not specific) (string= specific "TAGS"))
  12834. (setq value (org-get-tags-string))
  12835. (string-match "\\S-" value))
  12836. (push (cons "TAGS" value) props))
  12837. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12838. (setq value (org-get-tags-at)))
  12839. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12840. ":"))
  12841. props))
  12842. (when (or (not specific) (string= specific "BLOCKED"))
  12843. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12844. (when (or (not specific)
  12845. (member specific
  12846. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12847. "TIMESTAMP" "TIMESTAMP_IA")))
  12848. (catch 'match
  12849. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12850. (setq key (if (match-end 1)
  12851. (substring (org-match-string-no-properties 1)
  12852. 0 -1))
  12853. string (if (equal key clockstr)
  12854. (org-no-properties
  12855. (org-trim
  12856. (buffer-substring
  12857. (match-beginning 3) (goto-char
  12858. (point-at-eol)))))
  12859. (substring (org-match-string-no-properties 3)
  12860. 1 -1)))
  12861. ;; Get the correct property name from the key. This is
  12862. ;; necessary if the user has configured time keywords.
  12863. (setq key1 (concat key ":"))
  12864. (cond
  12865. ((not key)
  12866. (setq key
  12867. (if (= (char-after (match-beginning 3)) ?\[)
  12868. "TIMESTAMP_IA" "TIMESTAMP")))
  12869. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12870. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12871. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12872. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12873. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12874. (progn
  12875. (push (cons key string) props)
  12876. ;; no need to search further if match is found
  12877. (throw 'match t))
  12878. (when (or (equal key "CLOCK") (not (assoc key props)))
  12879. (push (cons key string) props))))))
  12880. )
  12881. (when (memq which '(all standard))
  12882. ;; Get the standard properties, like :PROP: ...
  12883. (setq range (org-get-property-block beg end))
  12884. (when range
  12885. (goto-char (car range))
  12886. (while (re-search-forward
  12887. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12888. (cdr range) t)
  12889. (setq key (org-match-string-no-properties 1)
  12890. value (org-trim (or (org-match-string-no-properties 2) "")))
  12891. (unless (member key excluded)
  12892. (push (cons key (or value "")) props)))))
  12893. (if clocksum
  12894. (push (cons "CLOCKSUM"
  12895. (org-columns-number-to-string (/ (float clocksum) 60.)
  12896. 'add_times))
  12897. props))
  12898. (unless (assoc "CATEGORY" props)
  12899. (push (cons "CATEGORY" (org-get-category)) props))
  12900. (append sum-props (nreverse props)))))))
  12901. (defun org-entry-get (pom property &optional inherit literal-nil)
  12902. "Get value of PROPERTY for entry at point-or-marker POM.
  12903. If INHERIT is non-nil and the entry does not have the property,
  12904. then also check higher levels of the hierarchy.
  12905. If INHERIT is the symbol `selective', use inheritance only if the setting
  12906. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12907. If the property is present but empty, the return value is the empty string.
  12908. If the property is not present at all, nil is returned.
  12909. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12910. do not interpret it as the list atom nil. This is used for inheritance
  12911. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12912. (org-with-point-at pom
  12913. (if (and inherit (if (eq inherit 'selective)
  12914. (org-property-inherit-p property)
  12915. t))
  12916. (org-entry-get-with-inheritance property literal-nil)
  12917. (if (member property org-special-properties)
  12918. ;; We need a special property. Use `org-entry-properties' to
  12919. ;; retrieve it, but specify the wanted property
  12920. (cdr (assoc property (org-entry-properties nil 'special property)))
  12921. (let ((range (unless (org-before-first-heading-p)
  12922. (org-get-property-block)))
  12923. (props (list (or (assoc property org-file-properties)
  12924. (assoc property org-global-properties)
  12925. (assoc property org-global-properties-fixed))))
  12926. val)
  12927. (flet ((ap (key)
  12928. (when (re-search-forward
  12929. (org-re-property key) (cdr range) t)
  12930. (setq props
  12931. (org-update-property-plist
  12932. key
  12933. (if (match-end 1)
  12934. (org-match-string-no-properties 1) "")
  12935. props)))))
  12936. (when (and range (goto-char (car range)))
  12937. (ap property)
  12938. (goto-char (car range))
  12939. (while (ap (concat property "+")))
  12940. (setq val (cdr (assoc property props)))
  12941. (when val (if literal-nil val (org-not-nil val))))))))))
  12942. (defun org-property-or-variable-value (var &optional inherit)
  12943. "Check if there is a property fixing the value of VAR.
  12944. If yes, return this value. If not, return the current value of the variable."
  12945. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12946. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12947. (read prop)
  12948. (symbol-value var))))
  12949. (defun org-entry-delete (pom property)
  12950. "Delete the property PROPERTY from entry at point-or-marker POM."
  12951. (org-with-point-at pom
  12952. (if (member property org-special-properties)
  12953. nil ; cannot delete these properties.
  12954. (let ((range (org-get-property-block)))
  12955. (if (and range
  12956. (goto-char (car range))
  12957. (re-search-forward
  12958. (org-re-property property)
  12959. (cdr range) t))
  12960. (progn
  12961. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12962. t)
  12963. nil)))))
  12964. ;; Multi-values properties are properties that contain multiple values
  12965. ;; These values are assumed to be single words, separated by whitespace.
  12966. (defun org-entry-add-to-multivalued-property (pom property value)
  12967. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12968. (let* ((old (org-entry-get pom property))
  12969. (values (and old (org-split-string old "[ \t]"))))
  12970. (setq value (org-entry-protect-space value))
  12971. (unless (member value values)
  12972. (setq values (cons value values))
  12973. (org-entry-put pom property
  12974. (mapconcat 'identity values " ")))))
  12975. (defun org-entry-remove-from-multivalued-property (pom property value)
  12976. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12977. (let* ((old (org-entry-get pom property))
  12978. (values (and old (org-split-string old "[ \t]"))))
  12979. (setq value (org-entry-protect-space value))
  12980. (when (member value values)
  12981. (setq values (delete value values))
  12982. (org-entry-put pom property
  12983. (mapconcat 'identity values " ")))))
  12984. (defun org-entry-member-in-multivalued-property (pom property value)
  12985. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12986. (let* ((old (org-entry-get pom property))
  12987. (values (and old (org-split-string old "[ \t]"))))
  12988. (setq value (org-entry-protect-space value))
  12989. (member value values)))
  12990. (defun org-entry-get-multivalued-property (pom property)
  12991. "Return a list of values in a multivalued property."
  12992. (let* ((value (org-entry-get pom property))
  12993. (values (and value (org-split-string value "[ \t]"))))
  12994. (mapcar 'org-entry-restore-space values)))
  12995. (defun org-entry-put-multivalued-property (pom property &rest values)
  12996. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12997. VALUES should be a list of strings. Spaces will be protected."
  12998. (org-entry-put pom property
  12999. (mapconcat 'org-entry-protect-space values " "))
  13000. (let* ((value (org-entry-get pom property))
  13001. (values (and value (org-split-string value "[ \t]"))))
  13002. (mapcar 'org-entry-restore-space values)))
  13003. (defun org-entry-protect-space (s)
  13004. "Protect spaces and newline in string S."
  13005. (while (string-match " " s)
  13006. (setq s (replace-match "%20" t t s)))
  13007. (while (string-match "\n" s)
  13008. (setq s (replace-match "%0A" t t s)))
  13009. s)
  13010. (defun org-entry-restore-space (s)
  13011. "Restore spaces and newline in string S."
  13012. (while (string-match "%20" s)
  13013. (setq s (replace-match " " t t s)))
  13014. (while (string-match "%0A" s)
  13015. (setq s (replace-match "\n" t t s)))
  13016. s)
  13017. (defvar org-entry-property-inherited-from (make-marker)
  13018. "Marker pointing to the entry from where a property was inherited.
  13019. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13020. location of the entry where the inheritance search matched. If there was
  13021. no match, the marker will point nowhere.
  13022. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13023. is set.")
  13024. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13025. "Get entry property, and search higher levels if not present.
  13026. The search will stop at the first ancestor which has the property defined.
  13027. If the value found is \"nil\", return nil to show that the property
  13028. should be considered as undefined (this is the meaning of nil here).
  13029. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13030. (move-marker org-entry-property-inherited-from nil)
  13031. (let (tmp)
  13032. (unless (org-before-first-heading-p)
  13033. (save-excursion
  13034. (save-restriction
  13035. (widen)
  13036. (catch 'ex
  13037. (while t
  13038. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13039. (org-back-to-heading t)
  13040. (move-marker org-entry-property-inherited-from (point))
  13041. (throw 'ex tmp))
  13042. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13043. (setq tmp (or tmp
  13044. (cdr (assoc property org-file-properties))
  13045. (cdr (assoc property org-global-properties))
  13046. (cdr (assoc property org-global-properties-fixed))))
  13047. (if literal-nil tmp (org-not-nil tmp))))
  13048. (defvar org-property-changed-functions nil
  13049. "Hook called when the value of a property has changed.
  13050. Each hook function should accept two arguments, the name of the property
  13051. and the new value.")
  13052. (defun org-entry-put (pom property value)
  13053. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13054. (org-with-point-at pom
  13055. (org-back-to-heading t)
  13056. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13057. range)
  13058. (cond
  13059. ((equal property "TODO")
  13060. (when (and (stringp value) (string-match "\\S-" value)
  13061. (not (member value org-todo-keywords-1)))
  13062. (error "\"%s\" is not a valid TODO state" value))
  13063. (if (or (not value)
  13064. (not (string-match "\\S-" value)))
  13065. (setq value 'none))
  13066. (org-todo value)
  13067. (org-set-tags nil 'align))
  13068. ((equal property "PRIORITY")
  13069. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13070. (string-to-char value) ?\ ))
  13071. (org-set-tags nil 'align))
  13072. ((equal property "SCHEDULED")
  13073. (if (re-search-forward org-scheduled-time-regexp end t)
  13074. (cond
  13075. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13076. ((eq value 'later) (org-timestamp-change 1 'day))
  13077. (t (call-interactively 'org-schedule)))
  13078. (call-interactively 'org-schedule)))
  13079. ((equal property "DEADLINE")
  13080. (if (re-search-forward org-deadline-time-regexp end t)
  13081. (cond
  13082. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13083. ((eq value 'later) (org-timestamp-change 1 'day))
  13084. (t (call-interactively 'org-deadline)))
  13085. (call-interactively 'org-deadline)))
  13086. ((member property org-special-properties)
  13087. (error "The %s property can not yet be set with `org-entry-put'"
  13088. property))
  13089. (t ; a non-special property
  13090. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13091. (setq range (org-get-property-block beg end 'force))
  13092. (goto-char (car range))
  13093. (if (re-search-forward
  13094. (org-re-property property) (cdr range) t)
  13095. (progn
  13096. (delete-region (match-beginning 0) (match-end 0))
  13097. (goto-char (match-beginning 0)))
  13098. (goto-char (cdr range))
  13099. (insert "\n")
  13100. (backward-char 1)
  13101. (org-indent-line-function))
  13102. (insert ":" property ":")
  13103. (and value (insert " " value))
  13104. (org-indent-line-function)))))
  13105. (run-hook-with-args 'org-property-changed-functions property value)))
  13106. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13107. "Get all property keys in the current buffer.
  13108. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13109. like tags and TODO state.
  13110. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13111. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13112. and others.
  13113. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13114. formats in the current buffer."
  13115. (let (rtn range cfmt s p)
  13116. (save-excursion
  13117. (save-restriction
  13118. (widen)
  13119. (goto-char (point-min))
  13120. (while (re-search-forward org-property-start-re nil t)
  13121. (setq range (org-get-property-block))
  13122. (goto-char (car range))
  13123. (while (re-search-forward
  13124. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13125. (cdr range) t)
  13126. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13127. (outline-next-heading))))
  13128. (when include-specials
  13129. (setq rtn (append org-special-properties rtn)))
  13130. (when include-defaults
  13131. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13132. (add-to-list 'rtn org-effort-property))
  13133. (when include-columns
  13134. (save-excursion
  13135. (save-restriction
  13136. (widen)
  13137. (goto-char (point-min))
  13138. (while (re-search-forward
  13139. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13140. nil t)
  13141. (setq cfmt (match-string 2) s 0)
  13142. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13143. cfmt s)
  13144. (setq s (match-end 0)
  13145. p (match-string 1 cfmt))
  13146. (unless (or (equal p "ITEM")
  13147. (member p org-special-properties))
  13148. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13149. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13150. (defun org-property-values (key)
  13151. "Return a list of all values of property KEY in the current buffer."
  13152. (save-excursion
  13153. (save-restriction
  13154. (widen)
  13155. (goto-char (point-min))
  13156. (let ((re (org-re-property key))
  13157. values)
  13158. (while (re-search-forward re nil t)
  13159. (add-to-list 'values (org-trim (match-string 1))))
  13160. (delete "" values)))))
  13161. (defun org-insert-property-drawer ()
  13162. "Insert a property drawer into the current entry."
  13163. (interactive)
  13164. (org-back-to-heading t)
  13165. (looking-at org-outline-regexp)
  13166. (let ((indent (if org-adapt-indentation
  13167. (- (match-end 0)(match-beginning 0))
  13168. 0))
  13169. (beg (point))
  13170. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13171. end hiddenp)
  13172. (outline-next-heading)
  13173. (setq end (point))
  13174. (goto-char beg)
  13175. (while (re-search-forward re end t))
  13176. (setq hiddenp (outline-invisible-p))
  13177. (end-of-line 1)
  13178. (and (equal (char-after) ?\n) (forward-char 1))
  13179. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13180. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13181. ;; just skip this line
  13182. (beginning-of-line 2)
  13183. ;; Drawer start, find the end
  13184. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13185. (beginning-of-line 1)))
  13186. (org-skip-over-state-notes)
  13187. (skip-chars-backward " \t\n\r")
  13188. (if (eq (char-before) ?*) (forward-char 1))
  13189. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13190. (beginning-of-line 0)
  13191. (org-indent-to-column indent)
  13192. (beginning-of-line 2)
  13193. (org-indent-to-column indent)
  13194. (beginning-of-line 0)
  13195. (if hiddenp
  13196. (save-excursion
  13197. (org-back-to-heading t)
  13198. (hide-entry))
  13199. (org-flag-drawer t))))
  13200. (defvar org-property-set-functions-alist nil
  13201. "Property set function alist.
  13202. Each entry should have the following format:
  13203. (PROPERTY . READ-FUNCTION)
  13204. The read function will be called with the same argument as
  13205. `org-completing-read'.")
  13206. (defun org-set-property-function (property)
  13207. "Get the function that should be used to set PROPERTY.
  13208. This is computed according to `org-property-set-functions-alist'."
  13209. (or (cdr (assoc property org-property-set-functions-alist))
  13210. 'org-completing-read))
  13211. (defun org-read-property-value (property)
  13212. "Read PROPERTY value from user."
  13213. (let* ((completion-ignore-case t)
  13214. (allowed (org-property-get-allowed-values nil property 'table))
  13215. (cur (org-entry-get nil property))
  13216. (prompt (concat property " value"
  13217. (if (and cur (string-match "\\S-" cur))
  13218. (concat " [" cur "]") "") ": "))
  13219. (set-function (org-set-property-function property))
  13220. (val (if allowed
  13221. (funcall set-function prompt allowed nil
  13222. (not (get-text-property 0 'org-unrestricted
  13223. (caar allowed))))
  13224. (let (org-completion-use-ido org-completion-use-iswitchb)
  13225. (funcall set-function prompt
  13226. (mapcar 'list (org-property-values property))
  13227. nil nil "" nil cur)))))
  13228. (if (equal val "")
  13229. cur
  13230. val)))
  13231. (defvar org-last-set-property nil)
  13232. (defun org-read-property-name ()
  13233. "Read a property name."
  13234. (let* ((completion-ignore-case t)
  13235. (keys (org-buffer-property-keys nil t t))
  13236. (default-prop (or (save-excursion
  13237. (save-match-data
  13238. (beginning-of-line)
  13239. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13240. (null (string= (match-string 1) "END"))
  13241. (match-string 1))))
  13242. org-last-set-property))
  13243. (property (org-icompleting-read
  13244. (concat "Property"
  13245. (if default-prop (concat " [" default-prop "]") "")
  13246. ": ")
  13247. (mapcar 'list keys)
  13248. nil nil nil nil
  13249. default-prop
  13250. )))
  13251. (if (member property keys)
  13252. property
  13253. (or (cdr (assoc (downcase property)
  13254. (mapcar (lambda (x) (cons (downcase x) x))
  13255. keys)))
  13256. property))))
  13257. (defun org-set-property (property value)
  13258. "In the current entry, set PROPERTY to VALUE.
  13259. When called interactively, this will prompt for a property name, offering
  13260. completion on existing and default properties. And then it will prompt
  13261. for a value, offering completion either on allowed values (via an inherited
  13262. xxx_ALL property) or on existing values in other instances of this property
  13263. in the current file."
  13264. (interactive (list nil nil))
  13265. (let* ((property (or property (org-read-property-name)))
  13266. (value (or value (org-read-property-value property)))
  13267. (fn (assoc property org-properties-postprocess-alist)))
  13268. (setq org-last-set-property property)
  13269. ;; Possibly postprocess the inserted value:
  13270. (when fn (setq value (funcall (cadr fn) value)))
  13271. (unless (equal (org-entry-get nil property) value)
  13272. (org-entry-put nil property value))))
  13273. (defun org-delete-property (property)
  13274. "In the current entry, delete PROPERTY."
  13275. (interactive
  13276. (let* ((completion-ignore-case t)
  13277. (prop (org-icompleting-read "Property: "
  13278. (org-entry-properties nil 'standard))))
  13279. (list prop)))
  13280. (message "Property %s %s" property
  13281. (if (org-entry-delete nil property)
  13282. "deleted"
  13283. "was not present in the entry")))
  13284. (defun org-delete-property-globally (property)
  13285. "Remove PROPERTY globally, from all entries."
  13286. (interactive
  13287. (let* ((completion-ignore-case t)
  13288. (prop (org-icompleting-read
  13289. "Globally remove property: "
  13290. (mapcar 'list (org-buffer-property-keys)))))
  13291. (list prop)))
  13292. (save-excursion
  13293. (save-restriction
  13294. (widen)
  13295. (goto-char (point-min))
  13296. (let ((cnt 0))
  13297. (while (re-search-forward
  13298. (org-re-property property)
  13299. nil t)
  13300. (setq cnt (1+ cnt))
  13301. (replace-match ""))
  13302. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13303. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13304. (defun org-compute-property-at-point ()
  13305. "Compute the property at point.
  13306. This looks for an enclosing column format, extracts the operator and
  13307. then applies it to the property in the column format's scope."
  13308. (interactive)
  13309. (unless (org-at-property-p)
  13310. (error "Not at a property"))
  13311. (let ((prop (org-match-string-no-properties 2)))
  13312. (org-columns-get-format-and-top-level)
  13313. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13314. (error "No operator defined for property %s" prop))
  13315. (org-columns-compute prop)))
  13316. (defvar org-property-allowed-value-functions nil
  13317. "Hook for functions supplying allowed values for a specific property.
  13318. The functions must take a single argument, the name of the property, and
  13319. return a flat list of allowed values. If \":ETC\" is one of
  13320. the values, this means that these values are intended as defaults for
  13321. completion, but that other values should be allowed too.
  13322. The functions must return nil if they are not responsible for this
  13323. property.")
  13324. (defun org-property-get-allowed-values (pom property &optional table)
  13325. "Get allowed values for the property PROPERTY.
  13326. When TABLE is non-nil, return an alist that can directly be used for
  13327. completion."
  13328. (let (vals)
  13329. (cond
  13330. ((equal property "TODO")
  13331. (setq vals (org-with-point-at pom
  13332. (append org-todo-keywords-1 '("")))))
  13333. ((equal property "PRIORITY")
  13334. (let ((n org-lowest-priority))
  13335. (while (>= n org-highest-priority)
  13336. (push (char-to-string n) vals)
  13337. (setq n (1- n)))))
  13338. ((member property org-special-properties))
  13339. ((setq vals (run-hook-with-args-until-success
  13340. 'org-property-allowed-value-functions property)))
  13341. (t
  13342. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13343. (when (and vals (string-match "\\S-" vals))
  13344. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13345. (setq vals (mapcar (lambda (x)
  13346. (cond ((stringp x) x)
  13347. ((numberp x) (number-to-string x))
  13348. ((symbolp x) (symbol-name x))
  13349. (t "???")))
  13350. vals)))))
  13351. (when (member ":ETC" vals)
  13352. (setq vals (remove ":ETC" vals))
  13353. (org-add-props (car vals) '(org-unrestricted t)))
  13354. (if table (mapcar 'list vals) vals)))
  13355. (defun org-property-previous-allowed-value (&optional previous)
  13356. "Switch to the next allowed value for this property."
  13357. (interactive)
  13358. (org-property-next-allowed-value t))
  13359. (defun org-property-next-allowed-value (&optional previous)
  13360. "Switch to the next allowed value for this property."
  13361. (interactive)
  13362. (unless (org-at-property-p)
  13363. (error "Not at a property"))
  13364. (let* ((key (match-string 2))
  13365. (value (match-string 3))
  13366. (allowed (or (org-property-get-allowed-values (point) key)
  13367. (and (member value '("[ ]" "[-]" "[X]"))
  13368. '("[ ]" "[X]"))))
  13369. nval)
  13370. (unless allowed
  13371. (error "Allowed values for this property have not been defined"))
  13372. (if previous (setq allowed (reverse allowed)))
  13373. (if (member value allowed)
  13374. (setq nval (car (cdr (member value allowed)))))
  13375. (setq nval (or nval (car allowed)))
  13376. (if (equal nval value)
  13377. (error "Only one allowed value for this property"))
  13378. (org-at-property-p)
  13379. (replace-match (concat " :" key ": " nval) t t)
  13380. (org-indent-line-function)
  13381. (beginning-of-line 1)
  13382. (skip-chars-forward " \t")
  13383. (run-hook-with-args 'org-property-changed-functions key nval)))
  13384. (defun org-find-olp (path &optional this-buffer)
  13385. "Return a marker pointing to the entry at outline path OLP.
  13386. If anything goes wrong, throw an error.
  13387. You can wrap this call to catch the error like this:
  13388. (condition-case msg
  13389. (org-mobile-locate-entry (match-string 4))
  13390. (error (nth 1 msg)))
  13391. The return value will then be either a string with the error message,
  13392. or a marker if everything is OK.
  13393. If THIS-BUFFER is set, the outline path does not contain a file,
  13394. only headings."
  13395. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13396. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13397. (level 1)
  13398. (lmin 1)
  13399. (lmax 1)
  13400. limit re end found pos heading cnt flevel)
  13401. (unless buffer (error "File not found :%s" file))
  13402. (with-current-buffer buffer
  13403. (save-excursion
  13404. (save-restriction
  13405. (widen)
  13406. (setq limit (point-max))
  13407. (goto-char (point-min))
  13408. (while (setq heading (pop path))
  13409. (setq re (format org-complex-heading-regexp-format
  13410. (regexp-quote heading)))
  13411. (setq cnt 0 pos (point))
  13412. (while (re-search-forward re end t)
  13413. (setq level (- (match-end 1) (match-beginning 1)))
  13414. (if (and (>= level lmin) (<= level lmax))
  13415. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13416. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13417. lmax heading))
  13418. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13419. lmax heading))
  13420. (goto-char found)
  13421. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13422. (setq end (save-excursion (org-end-of-subtree t t))))
  13423. (when (org-at-heading-p)
  13424. (move-marker (make-marker) (point))))))))
  13425. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13426. "Find node HEADING in BUFFER.
  13427. Return a marker to the heading if it was found, or nil if not.
  13428. If POS-ONLY is set, return just the position instead of a marker.
  13429. The heading text must match exact, but it may have a TODO keyword,
  13430. a priority cookie and tags in the standard locations."
  13431. (with-current-buffer (or buffer (current-buffer))
  13432. (save-excursion
  13433. (save-restriction
  13434. (widen)
  13435. (goto-char (point-min))
  13436. (let (case-fold-search)
  13437. (if (re-search-forward
  13438. (format org-complex-heading-regexp-format
  13439. (regexp-quote heading)) nil t)
  13440. (if pos-only
  13441. (match-beginning 0)
  13442. (move-marker (make-marker) (match-beginning 0)))))))))
  13443. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13444. "Find Org node headline HEADING in all .org files in directory DIR.
  13445. When the target headline is found, return a marker to this location."
  13446. (let ((files (directory-files (or dir default-directory)
  13447. nil "\\`[^.#].*\\.org\\'"))
  13448. file visiting m buffer)
  13449. (catch 'found
  13450. (while (setq file (pop files))
  13451. (message "trying %s" file)
  13452. (setq visiting (org-find-base-buffer-visiting file))
  13453. (setq buffer (or visiting (find-file-noselect file)))
  13454. (setq m (org-find-exact-headline-in-buffer
  13455. heading buffer))
  13456. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13457. (and m (throw 'found m))))))
  13458. (defun org-find-entry-with-id (ident)
  13459. "Locate the entry that contains the ID property with exact value IDENT.
  13460. IDENT can be a string, a symbol or a number, this function will search for
  13461. the string representation of it.
  13462. Return the position where this entry starts, or nil if there is no such entry."
  13463. (interactive "sID: ")
  13464. (let ((id (cond
  13465. ((stringp ident) ident)
  13466. ((symbol-name ident) (symbol-name ident))
  13467. ((numberp ident) (number-to-string ident))
  13468. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13469. (case-fold-search nil))
  13470. (save-excursion
  13471. (save-restriction
  13472. (widen)
  13473. (goto-char (point-min))
  13474. (when (re-search-forward
  13475. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13476. nil t)
  13477. (org-back-to-heading t)
  13478. (point))))))
  13479. ;;;; Timestamps
  13480. (defvar org-last-changed-timestamp nil)
  13481. (defvar org-last-inserted-timestamp nil
  13482. "The last time stamp inserted with `org-insert-time-stamp'.")
  13483. (defvar org-time-was-given) ; dynamically scoped parameter
  13484. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13485. (defvar org-ts-what) ; dynamically scoped parameter
  13486. (defun org-time-stamp (arg &optional inactive)
  13487. "Prompt for a date/time and insert a time stamp.
  13488. If the user specifies a time like HH:MM, or if this command is called
  13489. with a prefix argument, the time stamp will contain date and time.
  13490. Otherwise, only the date will be included. All parts of a date not
  13491. specified by the user will be filled in from the current date/time.
  13492. So if you press just return without typing anything, the time stamp
  13493. will represent the current date/time. If there is already a timestamp
  13494. at the cursor, it will be modified."
  13495. (interactive "P")
  13496. (let* ((ts nil)
  13497. (default-time
  13498. ;; Default time is either today, or, when entering a range,
  13499. ;; the range start.
  13500. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13501. (save-excursion
  13502. (re-search-backward
  13503. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13504. (- (point) 20) t)))
  13505. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13506. (current-time)))
  13507. (default-input (and ts (org-get-compact-tod ts)))
  13508. (repeater (save-excursion
  13509. (save-match-data
  13510. (beginning-of-line)
  13511. (when (re-search-forward
  13512. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13513. (save-excursion (progn (end-of-line) (point))) t)
  13514. (match-string 0)))))
  13515. org-time-was-given org-end-time-was-given time)
  13516. (cond
  13517. ((and (org-at-timestamp-p t)
  13518. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13519. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13520. (insert "--")
  13521. (setq time (let ((this-command this-command))
  13522. (org-read-date arg 'totime nil nil
  13523. default-time default-input)))
  13524. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13525. ((org-at-timestamp-p t)
  13526. (setq time (let ((this-command this-command))
  13527. (org-read-date arg 'totime nil nil default-time default-input)))
  13528. (when (org-at-timestamp-p t) ; just to get the match data
  13529. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13530. (replace-match "")
  13531. (setq org-last-changed-timestamp
  13532. (org-insert-time-stamp
  13533. time (or org-time-was-given arg)
  13534. inactive nil nil (list org-end-time-was-given)))
  13535. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13536. (setq org-last-changed-timestamp
  13537. (concat (substring org-last-inserted-timestamp 0 -1)
  13538. " " repeater ">"))))
  13539. (message "Timestamp updated"))
  13540. (t
  13541. (setq time (let ((this-command this-command))
  13542. (org-read-date arg 'totime nil nil default-time default-input)))
  13543. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13544. nil nil (list org-end-time-was-given))))))
  13545. ;; FIXME: can we use this for something else, like computing time differences?
  13546. (defun org-get-compact-tod (s)
  13547. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13548. (let* ((t1 (match-string 1 s))
  13549. (h1 (string-to-number (match-string 2 s)))
  13550. (m1 (string-to-number (match-string 3 s)))
  13551. (t2 (and (match-end 4) (match-string 5 s)))
  13552. (h2 (and t2 (string-to-number (match-string 6 s))))
  13553. (m2 (and t2 (string-to-number (match-string 7 s))))
  13554. dh dm)
  13555. (if (not t2)
  13556. t1
  13557. (setq dh (- h2 h1) dm (- m2 m1))
  13558. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13559. (concat t1 "+" (number-to-string dh)
  13560. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13561. (defun org-time-stamp-inactive (&optional arg)
  13562. "Insert an inactive time stamp.
  13563. An inactive time stamp is enclosed in square brackets instead of angle
  13564. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13565. does not link to the calendar and cannot be changed with the S-cursor keys.
  13566. So these are more for recording a certain time/date."
  13567. (interactive "P")
  13568. (org-time-stamp arg 'inactive))
  13569. (defvar org-date-ovl (make-overlay 1 1))
  13570. (overlay-put org-date-ovl 'face 'org-warning)
  13571. (org-detach-overlay org-date-ovl)
  13572. (defvar org-ans1) ; dynamically scoped parameter
  13573. (defvar org-ans2) ; dynamically scoped parameter
  13574. (defvar org-plain-time-of-day-regexp) ; defined below
  13575. (defvar org-overriding-default-time nil) ; dynamically scoped
  13576. (defvar org-read-date-overlay nil)
  13577. (defvar org-dcst nil) ; dynamically scoped
  13578. (defvar org-read-date-history nil)
  13579. (defvar org-read-date-final-answer nil)
  13580. (defvar org-read-date-analyze-futurep nil)
  13581. (defvar org-read-date-analyze-forced-year nil)
  13582. (defun org-read-date (&optional with-time to-time from-string prompt
  13583. default-time default-input)
  13584. "Read a date, possibly a time, and make things smooth for the user.
  13585. The prompt will suggest to enter an ISO date, but you can also enter anything
  13586. which will at least partially be understood by `parse-time-string'.
  13587. Unrecognized parts of the date will default to the current day, month, year,
  13588. hour and minute. If this command is called to replace a timestamp at point,
  13589. or to enter the second timestamp of a range, the default time is taken
  13590. from the existing stamp. Furthermore, the command prefers the future,
  13591. so if you are giving a date where the year is not given, and the day-month
  13592. combination is already past in the current year, it will assume you
  13593. mean next year. For details, see the manual. A few examples:
  13594. 3-2-5 --> 2003-02-05
  13595. feb 15 --> currentyear-02-15
  13596. 2/15 --> currentyear-02-15
  13597. sep 12 9 --> 2009-09-12
  13598. 12:45 --> today 12:45
  13599. 22 sept 0:34 --> currentyear-09-22 0:34
  13600. 12 --> currentyear-currentmonth-12
  13601. Fri --> nearest Friday (today or later)
  13602. etc.
  13603. Furthermore you can specify a relative date by giving, as the *first* thing
  13604. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13605. change in days weeks, months, years.
  13606. With a single plus or minus, the date is relative to today. With a double
  13607. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13608. +4d --> four days from today
  13609. +4 --> same as above
  13610. +2w --> two weeks from today
  13611. ++5 --> five days from default date
  13612. The function understands only English month and weekday abbreviations,
  13613. but this can be configured with the variables `parse-time-months' and
  13614. `parse-time-weekdays'.
  13615. While prompting, a calendar is popped up - you can also select the
  13616. date with the mouse (button 1). The calendar shows a period of three
  13617. months. To scroll it to other months, use the keys `>' and `<'.
  13618. If you don't like the calendar, turn it off with
  13619. \(setq org-read-date-popup-calendar nil)
  13620. With optional argument TO-TIME, the date will immediately be converted
  13621. to an internal time.
  13622. With an optional argument WITH-TIME, the prompt will suggest to also
  13623. insert a time. Note that when WITH-TIME is not set, you can still
  13624. enter a time, and this function will inform the calling routine about
  13625. this change. The calling routine may then choose to change the format
  13626. used to insert the time stamp into the buffer to include the time.
  13627. With optional argument FROM-STRING, read from this string instead from
  13628. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13629. the time/date that is used for everything that is not specified by the
  13630. user."
  13631. (require 'parse-time)
  13632. (let* ((org-time-stamp-rounding-minutes
  13633. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13634. (org-dcst org-display-custom-times)
  13635. (ct (org-current-time))
  13636. (def (or org-overriding-default-time default-time ct))
  13637. (defdecode (decode-time def))
  13638. (dummy (progn
  13639. (when (< (nth 2 defdecode) org-extend-today-until)
  13640. (setcar (nthcdr 2 defdecode) -1)
  13641. (setcar (nthcdr 1 defdecode) 59)
  13642. (setq def (apply 'encode-time defdecode)
  13643. defdecode (decode-time def)))))
  13644. (calendar-frame-setup nil)
  13645. (calendar-setup nil)
  13646. (calendar-move-hook nil)
  13647. (calendar-view-diary-initially-flag nil)
  13648. (calendar-view-holidays-initially-flag nil)
  13649. (timestr (format-time-string
  13650. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13651. (prompt (concat (if prompt (concat prompt " ") "")
  13652. (format "Date+time [%s]: " timestr)))
  13653. ans (org-ans0 "") org-ans1 org-ans2 final)
  13654. (cond
  13655. (from-string (setq ans from-string))
  13656. (org-read-date-popup-calendar
  13657. (save-excursion
  13658. (save-window-excursion
  13659. (calendar)
  13660. (unwind-protect
  13661. (progn
  13662. (calendar-forward-day (- (time-to-days def)
  13663. (calendar-absolute-from-gregorian
  13664. (calendar-current-date))))
  13665. (org-eval-in-calendar nil t)
  13666. (let* ((old-map (current-local-map))
  13667. (map (copy-keymap calendar-mode-map))
  13668. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13669. (org-defkey map (kbd "RET") 'org-calendar-select)
  13670. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13671. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13672. (org-defkey minibuffer-local-map [(meta shift left)]
  13673. (lambda () (interactive)
  13674. (org-eval-in-calendar '(calendar-backward-month 1))))
  13675. (org-defkey minibuffer-local-map [(meta shift right)]
  13676. (lambda () (interactive)
  13677. (org-eval-in-calendar '(calendar-forward-month 1))))
  13678. (org-defkey minibuffer-local-map [(meta shift up)]
  13679. (lambda () (interactive)
  13680. (org-eval-in-calendar '(calendar-backward-year 1))))
  13681. (org-defkey minibuffer-local-map [(meta shift down)]
  13682. (lambda () (interactive)
  13683. (org-eval-in-calendar '(calendar-forward-year 1))))
  13684. (org-defkey minibuffer-local-map [?\e (shift left)]
  13685. (lambda () (interactive)
  13686. (org-eval-in-calendar '(calendar-backward-month 1))))
  13687. (org-defkey minibuffer-local-map [?\e (shift right)]
  13688. (lambda () (interactive)
  13689. (org-eval-in-calendar '(calendar-forward-month 1))))
  13690. (org-defkey minibuffer-local-map [?\e (shift up)]
  13691. (lambda () (interactive)
  13692. (org-eval-in-calendar '(calendar-backward-year 1))))
  13693. (org-defkey minibuffer-local-map [?\e (shift down)]
  13694. (lambda () (interactive)
  13695. (org-eval-in-calendar '(calendar-forward-year 1))))
  13696. (org-defkey minibuffer-local-map [(shift up)]
  13697. (lambda () (interactive)
  13698. (org-eval-in-calendar '(calendar-backward-week 1))))
  13699. (org-defkey minibuffer-local-map [(shift down)]
  13700. (lambda () (interactive)
  13701. (org-eval-in-calendar '(calendar-forward-week 1))))
  13702. (org-defkey minibuffer-local-map [(shift left)]
  13703. (lambda () (interactive)
  13704. (org-eval-in-calendar '(calendar-backward-day 1))))
  13705. (org-defkey minibuffer-local-map [(shift right)]
  13706. (lambda () (interactive)
  13707. (org-eval-in-calendar '(calendar-forward-day 1))))
  13708. (org-defkey minibuffer-local-map ">"
  13709. (lambda () (interactive)
  13710. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13711. (org-defkey minibuffer-local-map "<"
  13712. (lambda () (interactive)
  13713. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13714. (org-defkey minibuffer-local-map "\C-v"
  13715. (lambda () (interactive)
  13716. (org-eval-in-calendar
  13717. '(calendar-scroll-left-three-months 1))))
  13718. (org-defkey minibuffer-local-map "\M-v"
  13719. (lambda () (interactive)
  13720. (org-eval-in-calendar
  13721. '(calendar-scroll-right-three-months 1))))
  13722. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13723. (unwind-protect
  13724. (progn
  13725. (use-local-map map)
  13726. (add-hook 'post-command-hook 'org-read-date-display)
  13727. (setq org-ans0 (read-string prompt default-input
  13728. 'org-read-date-history nil))
  13729. ;; org-ans0: from prompt
  13730. ;; org-ans1: from mouse click
  13731. ;; org-ans2: from calendar motion
  13732. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13733. (remove-hook 'post-command-hook 'org-read-date-display)
  13734. (use-local-map old-map)
  13735. (when org-read-date-overlay
  13736. (delete-overlay org-read-date-overlay)
  13737. (setq org-read-date-overlay nil)))))
  13738. (bury-buffer "*Calendar*")))))
  13739. (t ; Naked prompt only
  13740. (unwind-protect
  13741. (setq ans (read-string prompt default-input
  13742. 'org-read-date-history timestr))
  13743. (when org-read-date-overlay
  13744. (delete-overlay org-read-date-overlay)
  13745. (setq org-read-date-overlay nil)))))
  13746. (setq final (org-read-date-analyze ans def defdecode))
  13747. (when org-read-date-analyze-forced-year
  13748. (message "Year was forced into %s"
  13749. (if org-read-date-force-compatible-dates
  13750. "compatible range (1970-2037)"
  13751. "range representable on this machine"))
  13752. (ding))
  13753. ;; One round trip to get rid of 34th of August and stuff like that....
  13754. (setq final (decode-time (apply 'encode-time final)))
  13755. (setq org-read-date-final-answer ans)
  13756. (if to-time
  13757. (apply 'encode-time final)
  13758. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13759. (format "%04d-%02d-%02d %02d:%02d"
  13760. (nth 5 final) (nth 4 final) (nth 3 final)
  13761. (nth 2 final) (nth 1 final))
  13762. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13763. (defvar def)
  13764. (defvar defdecode)
  13765. (defvar with-time)
  13766. (defun org-read-date-display ()
  13767. "Display the current date prompt interpretation in the minibuffer."
  13768. (when org-read-date-display-live
  13769. (when org-read-date-overlay
  13770. (delete-overlay org-read-date-overlay))
  13771. (let ((p (point)))
  13772. (end-of-line 1)
  13773. (while (not (equal (buffer-substring
  13774. (max (point-min) (- (point) 4)) (point))
  13775. " "))
  13776. (insert " "))
  13777. (goto-char p))
  13778. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13779. " " (or org-ans1 org-ans2)))
  13780. (org-end-time-was-given nil)
  13781. (f (org-read-date-analyze ans def defdecode))
  13782. (fmts (if org-dcst
  13783. org-time-stamp-custom-formats
  13784. org-time-stamp-formats))
  13785. (fmt (if (or with-time
  13786. (and (boundp 'org-time-was-given) org-time-was-given))
  13787. (cdr fmts)
  13788. (car fmts)))
  13789. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13790. (when (and org-end-time-was-given
  13791. (string-match org-plain-time-of-day-regexp txt))
  13792. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13793. org-end-time-was-given
  13794. (substring txt (match-end 0)))))
  13795. (when org-read-date-analyze-futurep
  13796. (setq txt (concat txt " (=>F)")))
  13797. (setq org-read-date-overlay
  13798. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13799. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13800. (defun org-read-date-analyze (ans def defdecode)
  13801. "Analyze the combined answer of the date prompt."
  13802. ;; FIXME: cleanup and comment
  13803. (let ((nowdecode (decode-time (current-time)))
  13804. delta deltan deltaw deltadef year month day
  13805. hour minute second wday pm h2 m2 tl wday1
  13806. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13807. (setq org-read-date-analyze-futurep nil
  13808. org-read-date-analyze-forced-year nil)
  13809. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13810. (setq ans "+0"))
  13811. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13812. (setq ans (replace-match "" t t ans)
  13813. deltan (car delta)
  13814. deltaw (nth 1 delta)
  13815. deltadef (nth 2 delta)))
  13816. ;; Check if there is an iso week date in there
  13817. ;; If yes, store the info and postpone interpreting it until the rest
  13818. ;; of the parsing is done
  13819. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13820. (setq iso-year (if (match-end 1)
  13821. (org-small-year-to-year
  13822. (string-to-number (match-string 1 ans))))
  13823. iso-weekday (if (match-end 3)
  13824. (string-to-number (match-string 3 ans)))
  13825. iso-week (string-to-number (match-string 2 ans)))
  13826. (setq ans (replace-match "" t t ans)))
  13827. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13828. (when (string-match
  13829. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13830. (setq year (if (match-end 2)
  13831. (string-to-number (match-string 2 ans))
  13832. (progn (setq kill-year t)
  13833. (string-to-number (format-time-string "%Y"))))
  13834. month (string-to-number (match-string 3 ans))
  13835. day (string-to-number (match-string 4 ans)))
  13836. (if (< year 100) (setq year (+ 2000 year)))
  13837. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13838. t nil ans)))
  13839. ;; Help matching dotted european dates
  13840. (when (string-match
  13841. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13842. (setq year (if (match-end 3)
  13843. (string-to-number (match-string 3 ans))
  13844. (progn (setq kill-year t)
  13845. (string-to-number (format-time-string "%Y"))))
  13846. day (string-to-number (match-string 1 ans))
  13847. month (string-to-number (match-string 2 ans))
  13848. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13849. t nil ans)))
  13850. ;; Help matching american dates, like 5/30 or 5/30/7
  13851. (when (string-match
  13852. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13853. (setq year (if (match-end 4)
  13854. (string-to-number (match-string 4 ans))
  13855. (progn (setq kill-year t)
  13856. (string-to-number (format-time-string "%Y"))))
  13857. month (string-to-number (match-string 1 ans))
  13858. day (string-to-number (match-string 2 ans)))
  13859. (if (< year 100) (setq year (+ 2000 year)))
  13860. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13861. t nil ans)))
  13862. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13863. ;; If there is a time with am/pm, and *no* time without it, we convert
  13864. ;; so that matching will be successful.
  13865. (loop for i from 1 to 2 do ; twice, for end time as well
  13866. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13867. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13868. (setq hour (string-to-number (match-string 1 ans))
  13869. minute (if (match-end 3)
  13870. (string-to-number (match-string 3 ans))
  13871. 0)
  13872. pm (equal ?p
  13873. (string-to-char (downcase (match-string 4 ans)))))
  13874. (if (and (= hour 12) (not pm))
  13875. (setq hour 0)
  13876. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13877. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13878. t t ans))))
  13879. ;; Check if a time range is given as a duration
  13880. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13881. (setq hour (string-to-number (match-string 1 ans))
  13882. h2 (+ hour (string-to-number (match-string 3 ans)))
  13883. minute (string-to-number (match-string 2 ans))
  13884. m2 (+ minute (if (match-end 5) (string-to-number
  13885. (match-string 5 ans))0)))
  13886. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13887. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13888. t t ans)))
  13889. ;; Check if there is a time range
  13890. (when (boundp 'org-end-time-was-given)
  13891. (setq org-time-was-given nil)
  13892. (when (and (string-match org-plain-time-of-day-regexp ans)
  13893. (match-end 8))
  13894. (setq org-end-time-was-given (match-string 8 ans))
  13895. (setq ans (concat (substring ans 0 (match-beginning 7))
  13896. (substring ans (match-end 7))))))
  13897. (setq tl (parse-time-string ans)
  13898. day (or (nth 3 tl) (nth 3 defdecode))
  13899. month (or (nth 4 tl)
  13900. (if (and org-read-date-prefer-future
  13901. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13902. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13903. (nth 4 defdecode)))
  13904. year (or (and (not kill-year) (nth 5 tl))
  13905. (if (and org-read-date-prefer-future
  13906. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13907. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13908. (nth 5 defdecode)))
  13909. hour (or (nth 2 tl) (nth 2 defdecode))
  13910. minute (or (nth 1 tl) (nth 1 defdecode))
  13911. second (or (nth 0 tl) 0)
  13912. wday (nth 6 tl))
  13913. (when (and (eq org-read-date-prefer-future 'time)
  13914. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13915. (equal day (nth 3 nowdecode))
  13916. (equal month (nth 4 nowdecode))
  13917. (equal year (nth 5 nowdecode))
  13918. (nth 2 tl)
  13919. (or (< (nth 2 tl) (nth 2 nowdecode))
  13920. (and (= (nth 2 tl) (nth 2 nowdecode))
  13921. (nth 1 tl)
  13922. (< (nth 1 tl) (nth 1 nowdecode)))))
  13923. (setq day (1+ day)
  13924. futurep t))
  13925. ;; Special date definitions below
  13926. (cond
  13927. (iso-week
  13928. ;; There was an iso week
  13929. (require 'cal-iso)
  13930. (setq futurep nil)
  13931. (setq year (or iso-year year)
  13932. day (or iso-weekday wday 1)
  13933. wday nil ; to make sure that the trigger below does not match
  13934. iso-date (calendar-gregorian-from-absolute
  13935. (calendar-absolute-from-iso
  13936. (list iso-week day year))))
  13937. ; FIXME: Should we also push ISO weeks into the future?
  13938. ; (when (and org-read-date-prefer-future
  13939. ; (not iso-year)
  13940. ; (< (calendar-absolute-from-gregorian iso-date)
  13941. ; (time-to-days (current-time))))
  13942. ; (setq year (1+ year)
  13943. ; iso-date (calendar-gregorian-from-absolute
  13944. ; (calendar-absolute-from-iso
  13945. ; (list iso-week day year)))))
  13946. (setq month (car iso-date)
  13947. year (nth 2 iso-date)
  13948. day (nth 1 iso-date)))
  13949. (deltan
  13950. (setq futurep nil)
  13951. (unless deltadef
  13952. (let ((now (decode-time (current-time))))
  13953. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13954. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13955. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13956. ((equal deltaw "m") (setq month (+ month deltan)))
  13957. ((equal deltaw "y") (setq year (+ year deltan)))))
  13958. ((and wday (not (nth 3 tl)))
  13959. (setq futurep nil)
  13960. ;; Weekday was given, but no day, so pick that day in the week
  13961. ;; on or after the derived date.
  13962. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13963. (unless (equal wday wday1)
  13964. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13965. (if (and (boundp 'org-time-was-given)
  13966. (nth 2 tl))
  13967. (setq org-time-was-given t))
  13968. (if (< year 100) (setq year (+ 2000 year)))
  13969. ;; Check of the date is representable
  13970. (if org-read-date-force-compatible-dates
  13971. (progn
  13972. (if (< year 1970)
  13973. (setq year 1970 org-read-date-analyze-forced-year t))
  13974. (if (> year 2037)
  13975. (setq year 2037 org-read-date-analyze-forced-year t)))
  13976. (condition-case nil
  13977. (ignore (encode-time second minute hour day month year))
  13978. (error
  13979. (setq year (nth 5 defdecode))
  13980. (setq org-read-date-analyze-forced-year t))))
  13981. (setq org-read-date-analyze-futurep futurep)
  13982. (list second minute hour day month year)))
  13983. (defvar parse-time-weekdays)
  13984. (defun org-read-date-get-relative (s today default)
  13985. "Check string S for special relative date string.
  13986. TODAY and DEFAULT are internal times, for today and for a default.
  13987. Return shift list (N what def-flag)
  13988. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13989. N is the number of WHATs to shift.
  13990. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13991. the DEFAULT date rather than TODAY."
  13992. (require 'parse-time)
  13993. (when (and
  13994. (string-match
  13995. (concat
  13996. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13997. "\\([0-9]+\\)?"
  13998. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13999. "\\([ \t]\\|$\\)") s)
  14000. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14001. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14002. (string-to-char (substring (match-string 1 s) -1))
  14003. ?+))
  14004. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14005. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14006. (what (if (match-end 3) (match-string 3 s) "d"))
  14007. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14008. (date (if rel default today))
  14009. (wday (nth 6 (decode-time date)))
  14010. delta)
  14011. (if wday1
  14012. (progn
  14013. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14014. (if (= dir ?-) (setq delta (- delta 7)))
  14015. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14016. (list delta "d" rel))
  14017. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14018. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14019. "Turn a user-specified date into the internal representation.
  14020. The internal representation needed by the calendar is (month day year).
  14021. This is a wrapper to handle the brain-dead convention in calendar that
  14022. user function argument order change dependent on argument order."
  14023. (if (boundp 'calendar-date-style)
  14024. (cond
  14025. ((eq calendar-date-style 'american)
  14026. (list arg1 arg2 arg3))
  14027. ((eq calendar-date-style 'european)
  14028. (list arg2 arg1 arg3))
  14029. ((eq calendar-date-style 'iso)
  14030. (list arg2 arg3 arg1)))
  14031. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14032. (if (org-bound-and-true-p european-calendar-style)
  14033. (list arg2 arg1 arg3)
  14034. (list arg1 arg2 arg3)))))
  14035. (defun org-eval-in-calendar (form &optional keepdate)
  14036. "Eval FORM in the calendar window and return to current window.
  14037. Also, store the cursor date in variable org-ans2."
  14038. (let ((sf (selected-frame))
  14039. (sw (selected-window)))
  14040. (select-window (get-buffer-window "*Calendar*" t))
  14041. (eval form)
  14042. (when (and (not keepdate) (calendar-cursor-to-date))
  14043. (let* ((date (calendar-cursor-to-date))
  14044. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14045. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14046. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14047. (select-window sw)
  14048. (org-select-frame-set-input-focus sf)))
  14049. (defun org-calendar-select ()
  14050. "Return to `org-read-date' with the date currently selected.
  14051. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14052. (interactive)
  14053. (when (calendar-cursor-to-date)
  14054. (let* ((date (calendar-cursor-to-date))
  14055. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14056. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14057. (if (active-minibuffer-window) (exit-minibuffer))))
  14058. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14059. "Insert a date stamp for the date given by the internal TIME.
  14060. WITH-HM means use the stamp format that includes the time of the day.
  14061. INACTIVE means use square brackets instead of angular ones, so that the
  14062. stamp will not contribute to the agenda.
  14063. PRE and POST are optional strings to be inserted before and after the
  14064. stamp.
  14065. The command returns the inserted time stamp."
  14066. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14067. stamp)
  14068. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14069. (insert-before-markers (or pre ""))
  14070. (when (listp extra)
  14071. (setq extra (car extra))
  14072. (if (and (stringp extra)
  14073. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14074. (setq extra (format "-%02d:%02d"
  14075. (string-to-number (match-string 1 extra))
  14076. (string-to-number (match-string 2 extra))))
  14077. (setq extra nil)))
  14078. (when extra
  14079. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14080. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14081. (insert-before-markers (or post ""))
  14082. (setq org-last-inserted-timestamp stamp)))
  14083. (defun org-toggle-time-stamp-overlays ()
  14084. "Toggle the use of custom time stamp formats."
  14085. (interactive)
  14086. (setq org-display-custom-times (not org-display-custom-times))
  14087. (unless org-display-custom-times
  14088. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14089. (while (setq p (next-single-property-change p 'display))
  14090. (if (and (get-text-property p 'display)
  14091. (eq (get-text-property p 'face) 'org-date))
  14092. (remove-text-properties
  14093. p (setq p (next-single-property-change p 'display))
  14094. '(display t))))
  14095. (set-buffer-modified-p bmp)))
  14096. (if (featurep 'xemacs)
  14097. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14098. (org-restart-font-lock)
  14099. (setq org-table-may-need-update t)
  14100. (if org-display-custom-times
  14101. (message "Time stamps are overlaid with custom format")
  14102. (message "Time stamp overlays removed")))
  14103. (defun org-display-custom-time (beg end)
  14104. "Overlay modified time stamp format over timestamp between BEG and END."
  14105. (let* ((ts (buffer-substring beg end))
  14106. t1 w1 with-hm tf time str w2 (off 0))
  14107. (save-match-data
  14108. (setq t1 (org-parse-time-string ts t))
  14109. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14110. (setq off (- (match-end 0) (match-beginning 0)))))
  14111. (setq end (- end off))
  14112. (setq w1 (- end beg)
  14113. with-hm (and (nth 1 t1) (nth 2 t1))
  14114. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14115. time (org-fix-decoded-time t1)
  14116. str (org-add-props
  14117. (format-time-string
  14118. (substring tf 1 -1) (apply 'encode-time time))
  14119. nil 'mouse-face 'highlight)
  14120. w2 (length str))
  14121. (if (not (= w2 w1))
  14122. (add-text-properties (1+ beg) (+ 2 beg)
  14123. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14124. (if (featurep 'xemacs)
  14125. (progn
  14126. (put-text-property beg end 'invisible t)
  14127. (put-text-property beg end 'end-glyph (make-glyph str)))
  14128. (put-text-property beg end 'display str))))
  14129. (defun org-translate-time (string)
  14130. "Translate all timestamps in STRING to custom format.
  14131. But do this only if the variable `org-display-custom-times' is set."
  14132. (when org-display-custom-times
  14133. (save-match-data
  14134. (let* ((start 0)
  14135. (re org-ts-regexp-both)
  14136. t1 with-hm inactive tf time str beg end)
  14137. (while (setq start (string-match re string start))
  14138. (setq beg (match-beginning 0)
  14139. end (match-end 0)
  14140. t1 (save-match-data
  14141. (org-parse-time-string (substring string beg end) t))
  14142. with-hm (and (nth 1 t1) (nth 2 t1))
  14143. inactive (equal (substring string beg (1+ beg)) "[")
  14144. tf (funcall (if with-hm 'cdr 'car)
  14145. org-time-stamp-custom-formats)
  14146. time (org-fix-decoded-time t1)
  14147. str (format-time-string
  14148. (concat
  14149. (if inactive "[" "<") (substring tf 1 -1)
  14150. (if inactive "]" ">"))
  14151. (apply 'encode-time time))
  14152. string (replace-match str t t string)
  14153. start (+ start (length str)))))))
  14154. string)
  14155. (defun org-fix-decoded-time (time)
  14156. "Set 0 instead of nil for the first 6 elements of time.
  14157. Don't touch the rest."
  14158. (let ((n 0))
  14159. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14160. (defun org-days-to-time (timestamp-string)
  14161. "Difference between TIMESTAMP-STRING and now in days."
  14162. (- (time-to-days (org-time-string-to-time timestamp-string))
  14163. (time-to-days (current-time))))
  14164. (defun org-deadline-close (timestamp-string &optional ndays)
  14165. "Is the time in TIMESTAMP-STRING close to the current date?"
  14166. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14167. (and (< (org-days-to-time timestamp-string) ndays)
  14168. (not (org-entry-is-done-p))))
  14169. (defun org-get-wdays (ts)
  14170. "Get the deadline lead time appropriate for timestring TS."
  14171. (cond
  14172. ((<= org-deadline-warning-days 0)
  14173. ;; 0 or negative, enforce this value no matter what
  14174. (- org-deadline-warning-days))
  14175. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14176. ;; lead time is specified.
  14177. (floor (* (string-to-number (match-string 1 ts))
  14178. (cdr (assoc (match-string 2 ts)
  14179. '(("d" . 1) ("w" . 7)
  14180. ("m" . 30.4) ("y" . 365.25)))))))
  14181. ;; go for the default.
  14182. (t org-deadline-warning-days)))
  14183. (defun org-calendar-select-mouse (ev)
  14184. "Return to `org-read-date' with the date currently selected.
  14185. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14186. (interactive "e")
  14187. (mouse-set-point ev)
  14188. (when (calendar-cursor-to-date)
  14189. (let* ((date (calendar-cursor-to-date))
  14190. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14191. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14192. (if (active-minibuffer-window) (exit-minibuffer))))
  14193. (defun org-check-deadlines (ndays)
  14194. "Check if there are any deadlines due or past due.
  14195. A deadline is considered due if it happens within `org-deadline-warning-days'
  14196. days from today's date. If the deadline appears in an entry marked DONE,
  14197. it is not shown. The prefix arg NDAYS can be used to test that many
  14198. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14199. (interactive "P")
  14200. (let* ((org-warn-days
  14201. (cond
  14202. ((equal ndays '(4)) 100000)
  14203. (ndays (prefix-numeric-value ndays))
  14204. (t (abs org-deadline-warning-days))))
  14205. (case-fold-search nil)
  14206. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14207. (callback
  14208. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14209. (message "%d deadlines past-due or due within %d days"
  14210. (org-occur regexp nil callback)
  14211. org-warn-days)))
  14212. (defun org-check-before-date (date)
  14213. "Check if there are deadlines or scheduled entries before DATE."
  14214. (interactive (list (org-read-date)))
  14215. (let ((case-fold-search nil)
  14216. (regexp (concat "\\<\\(" org-deadline-string
  14217. "\\|" org-scheduled-string
  14218. "\\) *<\\([^>]+\\)>"))
  14219. (callback
  14220. (lambda () (time-less-p
  14221. (org-time-string-to-time (match-string 2))
  14222. (org-time-string-to-time date)))))
  14223. (message "%d entries before %s"
  14224. (org-occur regexp nil callback) date)))
  14225. (defun org-check-after-date (date)
  14226. "Check if there are deadlines or scheduled entries after DATE."
  14227. (interactive (list (org-read-date)))
  14228. (let ((case-fold-search nil)
  14229. (regexp (concat "\\<\\(" org-deadline-string
  14230. "\\|" org-scheduled-string
  14231. "\\) *<\\([^>]+\\)>"))
  14232. (callback
  14233. (lambda () (not
  14234. (time-less-p
  14235. (org-time-string-to-time (match-string 2))
  14236. (org-time-string-to-time date))))))
  14237. (message "%d entries after %s"
  14238. (org-occur regexp nil callback) date)))
  14239. (defun org-check-dates-range (start-date end-date)
  14240. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14241. (interactive (list (org-read-date nil nil nil "Range starts")
  14242. (org-read-date nil nil nil "Range end")))
  14243. (let ((case-fold-search nil)
  14244. (regexp (concat "\\<\\(" org-deadline-string
  14245. "\\|" org-scheduled-string
  14246. "\\) *<\\([^>]+\\)>"))
  14247. (callback
  14248. (lambda ()
  14249. (let ((match (match-string 2)))
  14250. (and
  14251. (not (time-less-p
  14252. (org-time-string-to-time match)
  14253. (org-time-string-to-time start-date)))
  14254. (time-less-p
  14255. (org-time-string-to-time match)
  14256. (org-time-string-to-time end-date)))))))
  14257. (message "%d entries between %s and %s"
  14258. (org-occur regexp nil callback) start-date end-date)))
  14259. (defun org-evaluate-time-range (&optional to-buffer)
  14260. "Evaluate a time range by computing the difference between start and end.
  14261. Normally the result is just printed in the echo area, but with prefix arg
  14262. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14263. If the time range is actually in a table, the result is inserted into the
  14264. next column.
  14265. For time difference computation, a year is assumed to be exactly 365
  14266. days in order to avoid rounding problems."
  14267. (interactive "P")
  14268. (or
  14269. (org-clock-update-time-maybe)
  14270. (save-excursion
  14271. (unless (org-at-date-range-p t)
  14272. (goto-char (point-at-bol))
  14273. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14274. (if (not (org-at-date-range-p t))
  14275. (error "Not at a time-stamp range, and none found in current line")))
  14276. (let* ((ts1 (match-string 1))
  14277. (ts2 (match-string 2))
  14278. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14279. (match-end (match-end 0))
  14280. (time1 (org-time-string-to-time ts1))
  14281. (time2 (org-time-string-to-time ts2))
  14282. (t1 (org-float-time time1))
  14283. (t2 (org-float-time time2))
  14284. (diff (abs (- t2 t1)))
  14285. (negative (< (- t2 t1) 0))
  14286. ;; (ys (floor (* 365 24 60 60)))
  14287. (ds (* 24 60 60))
  14288. (hs (* 60 60))
  14289. (fy "%dy %dd %02d:%02d")
  14290. (fy1 "%dy %dd")
  14291. (fd "%dd %02d:%02d")
  14292. (fd1 "%dd")
  14293. (fh "%02d:%02d")
  14294. y d h m align)
  14295. (if havetime
  14296. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14297. y 0
  14298. d (floor (/ diff ds)) diff (mod diff ds)
  14299. h (floor (/ diff hs)) diff (mod diff hs)
  14300. m (floor (/ diff 60)))
  14301. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14302. y 0
  14303. d (floor (+ (/ diff ds) 0.5))
  14304. h 0 m 0))
  14305. (if (not to-buffer)
  14306. (message "%s" (org-make-tdiff-string y d h m))
  14307. (if (org-at-table-p)
  14308. (progn
  14309. (goto-char match-end)
  14310. (setq align t)
  14311. (and (looking-at " *|") (goto-char (match-end 0))))
  14312. (goto-char match-end))
  14313. (if (looking-at
  14314. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14315. (replace-match ""))
  14316. (if negative (insert " -"))
  14317. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14318. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14319. (insert " " (format fh h m))))
  14320. (if align (org-table-align))
  14321. (message "Time difference inserted")))))
  14322. (defun org-make-tdiff-string (y d h m)
  14323. (let ((fmt "")
  14324. (l nil))
  14325. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14326. l (push y l)))
  14327. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14328. l (push d l)))
  14329. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14330. l (push h l)))
  14331. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14332. l (push m l)))
  14333. (apply 'format fmt (nreverse l))))
  14334. (defun org-time-string-to-time (s &optional buffer pos)
  14335. (condition-case errdata
  14336. (apply 'encode-time (org-parse-time-string s))
  14337. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14338. s (if (not (and buffer pos))
  14339. ""
  14340. (format " at %d in buffer `%s'" pos buffer))
  14341. (cdr errdata)))))
  14342. (defun org-time-string-to-seconds (s)
  14343. (org-float-time (org-time-string-to-time s)))
  14344. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14345. "Convert a time stamp to an absolute day number.
  14346. If there is a specifier for a cyclic time stamp, get the closest date to
  14347. DAYNR.
  14348. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14349. The variable date is bound by the calendar when this is called."
  14350. (cond
  14351. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14352. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14353. daynr
  14354. (+ daynr 1000)))
  14355. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14356. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14357. (time-to-days (current-time))) (match-string 0 s)
  14358. prefer show-all))
  14359. (t (time-to-days
  14360. (condition-case errdata
  14361. (apply 'encode-time (org-parse-time-string s))
  14362. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14363. s (if (not (and buffer pos))
  14364. ""
  14365. (format " at %d in buffer `%s'" pos buffer))
  14366. (cdr errdata))))))))
  14367. (defun org-days-to-iso-week (days)
  14368. "Return the iso week number."
  14369. (require 'cal-iso)
  14370. (car (calendar-iso-from-absolute days)))
  14371. (defun org-small-year-to-year (year)
  14372. "Convert 2-digit years into 4-digit years.
  14373. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14374. The year 2000 cannot be abbreviated. Any year larger than 99
  14375. is returned unchanged."
  14376. (if (< year 38)
  14377. (setq year (+ 2000 year))
  14378. (if (< year 100)
  14379. (setq year (+ 1900 year))))
  14380. year)
  14381. (defun org-time-from-absolute (d)
  14382. "Return the time corresponding to date D.
  14383. D may be an absolute day number, or a calendar-type list (month day year)."
  14384. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14385. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14386. (defun org-calendar-holiday ()
  14387. "List of holidays, for Diary display in Org-mode."
  14388. (require 'holidays)
  14389. (let ((hl (funcall
  14390. (if (fboundp 'calendar-check-holidays)
  14391. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14392. (if hl (mapconcat 'identity hl "; "))))
  14393. (defun org-diary-sexp-entry (sexp entry date)
  14394. "Process a SEXP diary ENTRY for DATE."
  14395. (require 'diary-lib)
  14396. (let ((result (if calendar-debug-sexp
  14397. (let ((stack-trace-on-error t))
  14398. (eval (car (read-from-string sexp))))
  14399. (condition-case nil
  14400. (eval (car (read-from-string sexp)))
  14401. (error
  14402. (beep)
  14403. (message "Bad sexp at line %d in %s: %s"
  14404. (org-current-line)
  14405. (buffer-file-name) sexp)
  14406. (sleep-for 2))))))
  14407. (cond ((stringp result) (split-string result "; "))
  14408. ((and (consp result)
  14409. (not (consp (cdr result)))
  14410. (stringp (cdr result))) (cdr result))
  14411. ((and (consp result)
  14412. (stringp (car result))) result)
  14413. (result entry)
  14414. (t nil))))
  14415. (defun org-diary-to-ical-string (frombuf)
  14416. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14417. This uses the icalendar.el library."
  14418. (let* ((tmpdir (if (featurep 'xemacs)
  14419. (temp-directory)
  14420. temporary-file-directory))
  14421. (tmpfile (make-temp-name
  14422. (expand-file-name "orgics" tmpdir)))
  14423. buf rtn b e)
  14424. (with-current-buffer frombuf
  14425. (icalendar-export-region (point-min) (point-max) tmpfile)
  14426. (setq buf (find-buffer-visiting tmpfile))
  14427. (set-buffer buf)
  14428. (goto-char (point-min))
  14429. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14430. (setq b (match-beginning 0)))
  14431. (goto-char (point-max))
  14432. (if (re-search-backward "^END:VEVENT" nil t)
  14433. (setq e (match-end 0)))
  14434. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14435. (kill-buffer buf)
  14436. (delete-file tmpfile)
  14437. rtn))
  14438. (defun org-closest-date (start current change prefer show-all)
  14439. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14440. When PREFER is `past', return a date that is either CURRENT or past.
  14441. When PREFER is `future', return a date that is either CURRENT or future.
  14442. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14443. ;; Make the proper lists from the dates
  14444. (catch 'exit
  14445. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14446. dn dw sday cday n1 n2 n0
  14447. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14448. (setq start (org-date-to-gregorian start)
  14449. current (org-date-to-gregorian
  14450. (if show-all
  14451. current
  14452. (time-to-days (current-time))))
  14453. sday (calendar-absolute-from-gregorian start)
  14454. cday (calendar-absolute-from-gregorian current))
  14455. (if (<= cday sday) (throw 'exit sday))
  14456. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14457. (setq dn (string-to-number (match-string 1 change))
  14458. dw (cdr (assoc (match-string 2 change) a1)))
  14459. (error "Invalid change specifier: %s" change))
  14460. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14461. (cond
  14462. ((eq dw 'day)
  14463. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14464. n2 (+ n1 dn)))
  14465. ((eq dw 'year)
  14466. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14467. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14468. (setq date1 (list m d y1)
  14469. n1 (calendar-absolute-from-gregorian date1)
  14470. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14471. n2 (calendar-absolute-from-gregorian date2)))
  14472. ((eq dw 'month)
  14473. ;; approx number of month between the two dates
  14474. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14475. ;; How often does dn fit in there?
  14476. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14477. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14478. m (+ m nm)
  14479. ny (floor (/ m 12))
  14480. y (+ y ny)
  14481. m (- m (* ny 12)))
  14482. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14483. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14484. (setq m2 (+ m dn) y2 y)
  14485. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14486. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14487. (while (<= n2 cday)
  14488. (setq n1 n2 m m2 y y2)
  14489. (setq m2 (+ m dn) y2 y)
  14490. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14491. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14492. ;; Make sure n1 is the earlier date
  14493. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14494. (if show-all
  14495. (cond
  14496. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14497. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14498. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14499. (cond
  14500. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14501. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14502. (t (if (= cday n1) n1 n2)))))))
  14503. (defun org-date-to-gregorian (date)
  14504. "Turn any specification of DATE into a Gregorian date for the calendar."
  14505. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14506. ((and (listp date) (= (length date) 3)) date)
  14507. ((stringp date)
  14508. (setq date (org-parse-time-string date))
  14509. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14510. ((listp date)
  14511. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14512. (defun org-parse-time-string (s &optional nodefault)
  14513. "Parse the standard Org-mode time string.
  14514. This should be a lot faster than the normal `parse-time-string'.
  14515. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14516. hour and minute fields will be nil if not given."
  14517. (if (string-match org-ts-regexp0 s)
  14518. (list 0
  14519. (if (or (match-beginning 8) (not nodefault))
  14520. (string-to-number (or (match-string 8 s) "0")))
  14521. (if (or (match-beginning 7) (not nodefault))
  14522. (string-to-number (or (match-string 7 s) "0")))
  14523. (string-to-number (match-string 4 s))
  14524. (string-to-number (match-string 3 s))
  14525. (string-to-number (match-string 2 s))
  14526. nil nil nil)
  14527. (error "Not a standard Org-mode time string: %s" s)))
  14528. (defun org-timestamp-up (&optional arg)
  14529. "Increase the date item at the cursor by one.
  14530. If the cursor is on the year, change the year. If it is on the month,
  14531. the day or the time, change that.
  14532. With prefix ARG, change by that many units."
  14533. (interactive "p")
  14534. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14535. (defun org-timestamp-down (&optional arg)
  14536. "Decrease the date item at the cursor by one.
  14537. If the cursor is on the year, change the year. If it is on the month,
  14538. the day or the time, change that.
  14539. With prefix ARG, change by that many units."
  14540. (interactive "p")
  14541. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14542. (defun org-timestamp-up-day (&optional arg)
  14543. "Increase the date in the time stamp by one day.
  14544. With prefix ARG, change that many days."
  14545. (interactive "p")
  14546. (if (and (not (org-at-timestamp-p t))
  14547. (org-at-heading-p))
  14548. (org-todo 'up)
  14549. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14550. (defun org-timestamp-down-day (&optional arg)
  14551. "Decrease the date in the time stamp by one day.
  14552. With prefix ARG, change that many days."
  14553. (interactive "p")
  14554. (if (and (not (org-at-timestamp-p t))
  14555. (org-at-heading-p))
  14556. (org-todo 'down)
  14557. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14558. (defun org-at-timestamp-p (&optional inactive-ok)
  14559. "Determine if the cursor is in or at a timestamp."
  14560. (interactive)
  14561. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14562. (pos (point))
  14563. (ans (or (looking-at tsr)
  14564. (save-excursion
  14565. (skip-chars-backward "^[<\n\r\t")
  14566. (if (> (point) (point-min)) (backward-char 1))
  14567. (and (looking-at tsr)
  14568. (> (- (match-end 0) pos) -1))))))
  14569. (and ans
  14570. (boundp 'org-ts-what)
  14571. (setq org-ts-what
  14572. (cond
  14573. ((= pos (match-beginning 0)) 'bracket)
  14574. ;; Point is considered to be "on the bracket" whether
  14575. ;; it's really on it or right after it.
  14576. ((or (= pos (1- (match-end 0)))
  14577. (= pos (match-end 0))) 'bracket)
  14578. ((org-pos-in-match-range pos 2) 'year)
  14579. ((org-pos-in-match-range pos 3) 'month)
  14580. ((org-pos-in-match-range pos 7) 'hour)
  14581. ((org-pos-in-match-range pos 8) 'minute)
  14582. ((or (org-pos-in-match-range pos 4)
  14583. (org-pos-in-match-range pos 5)) 'day)
  14584. ((and (> pos (or (match-end 8) (match-end 5)))
  14585. (< pos (match-end 0)))
  14586. (- pos (or (match-end 8) (match-end 5))))
  14587. (t 'day))))
  14588. ans))
  14589. (defun org-toggle-timestamp-type ()
  14590. "Toggle the type (<active> or [inactive]) of a time stamp."
  14591. (interactive)
  14592. (when (org-at-timestamp-p t)
  14593. (let ((beg (match-beginning 0)) (end (match-end 0))
  14594. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14595. (save-excursion
  14596. (goto-char beg)
  14597. (while (re-search-forward "[][<>]" end t)
  14598. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14599. t t)))
  14600. (message "Timestamp is now %sactive"
  14601. (if (equal (char-after beg) ?<) "" "in")))))
  14602. (defun org-timestamp-change (n &optional what updown)
  14603. "Change the date in the time stamp at point.
  14604. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14605. `year', `minute', `second'. If WHAT is not given, the cursor position
  14606. in the timestamp determines what will be changed."
  14607. (let ((origin (point)) origin-cat
  14608. with-hm inactive
  14609. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14610. org-ts-what
  14611. extra rem
  14612. ts time time0)
  14613. (if (not (org-at-timestamp-p t))
  14614. (error "Not at a timestamp"))
  14615. (if (and (not what) (eq org-ts-what 'bracket))
  14616. (org-toggle-timestamp-type)
  14617. ;; Point isn't on brackets. Remember the part of the time-stamp
  14618. ;; the point was in. Indeed, size of time-stamps may change,
  14619. ;; but point must be kept in the same category nonetheless.
  14620. (setq origin-cat org-ts-what)
  14621. (if (and (not what) (not (eq org-ts-what 'day))
  14622. org-display-custom-times
  14623. (get-text-property (point) 'display)
  14624. (not (get-text-property (1- (point)) 'display)))
  14625. (setq org-ts-what 'day))
  14626. (setq org-ts-what (or what org-ts-what)
  14627. inactive (= (char-after (match-beginning 0)) ?\[)
  14628. ts (match-string 0))
  14629. (replace-match "")
  14630. (if (string-match
  14631. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14632. ts)
  14633. (setq extra (match-string 1 ts)))
  14634. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14635. (setq with-hm t))
  14636. (setq time0 (org-parse-time-string ts))
  14637. (when (and updown
  14638. (eq org-ts-what 'minute)
  14639. (not current-prefix-arg))
  14640. ;; This looks like s-up and s-down. Change by one rounding step.
  14641. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14642. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14643. (setcar (cdr time0) (+ (nth 1 time0)
  14644. (if (> n 0) (- rem) (- dm rem))))))
  14645. (setq time
  14646. (encode-time (or (car time0) 0)
  14647. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14648. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14649. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14650. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14651. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14652. (nthcdr 6 time0)))
  14653. (when (and (member org-ts-what '(hour minute))
  14654. extra
  14655. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14656. (setq extra (org-modify-ts-extra
  14657. extra
  14658. (if (eq org-ts-what 'hour) 2 5)
  14659. n dm)))
  14660. (when (integerp org-ts-what)
  14661. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14662. (if (eq what 'calendar)
  14663. (let ((cal-date (org-get-date-from-calendar)))
  14664. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14665. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14666. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14667. (setcar time0 (or (car time0) 0))
  14668. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14669. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14670. (setq time (apply 'encode-time time0))))
  14671. ;; Insert the new time-stamp, and ensure point stays in the same
  14672. ;; category as before (i.e. not after the last position in that
  14673. ;; category).
  14674. (let ((pos (point)))
  14675. ;; Stay before inserted string. `save-excursion' is of no use.
  14676. (setq org-last-changed-timestamp
  14677. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14678. (goto-char pos))
  14679. (save-match-data
  14680. (looking-at org-ts-regexp3)
  14681. (goto-char (cond
  14682. ;; `day' category ends before `hour' if any, or at
  14683. ;; the end of the day name.
  14684. ((eq origin-cat 'day)
  14685. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14686. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14687. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14688. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14689. ;; `year' and `month' have both fixed size: point
  14690. ;; couldn't have moved into another part.
  14691. (t origin))))
  14692. ;; Update clock if on a CLOCK line.
  14693. (org-clock-update-time-maybe)
  14694. ;; Try to recenter the calendar window, if any.
  14695. (if (and org-calendar-follow-timestamp-change
  14696. (get-buffer-window "*Calendar*" t)
  14697. (memq org-ts-what '(day month year)))
  14698. (org-recenter-calendar (time-to-days time))))))
  14699. (defun org-modify-ts-extra (s pos n dm)
  14700. "Change the different parts of the lead-time and repeat fields in timestamp."
  14701. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14702. ng h m new rem)
  14703. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14704. (cond
  14705. ((or (org-pos-in-match-range pos 2)
  14706. (org-pos-in-match-range pos 3))
  14707. (setq m (string-to-number (match-string 3 s))
  14708. h (string-to-number (match-string 2 s)))
  14709. (if (org-pos-in-match-range pos 2)
  14710. (setq h (+ h n))
  14711. (setq n (* dm (org-no-warnings (signum n))))
  14712. (when (not (= 0 (setq rem (% m dm))))
  14713. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14714. (setq m (+ m n)))
  14715. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14716. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14717. (setq h (min 24 (max 0 h)))
  14718. (setq ng 1 new (format "-%02d:%02d" h m)))
  14719. ((org-pos-in-match-range pos 6)
  14720. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14721. ((org-pos-in-match-range pos 5)
  14722. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14723. ((org-pos-in-match-range pos 9)
  14724. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14725. ((org-pos-in-match-range pos 8)
  14726. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14727. (when ng
  14728. (setq s (concat
  14729. (substring s 0 (match-beginning ng))
  14730. new
  14731. (substring s (match-end ng))))))
  14732. s))
  14733. (defun org-recenter-calendar (date)
  14734. "If the calendar is visible, recenter it to DATE."
  14735. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14736. (when cwin
  14737. (let ((calendar-move-hook nil))
  14738. (with-selected-window cwin
  14739. (calendar-goto-date (if (listp date) date
  14740. (calendar-gregorian-from-absolute date))))))))
  14741. (defun org-goto-calendar (&optional arg)
  14742. "Go to the Emacs calendar at the current date.
  14743. If there is a time stamp in the current line, go to that date.
  14744. A prefix ARG can be used to force the current date."
  14745. (interactive "P")
  14746. (let ((tsr org-ts-regexp) diff
  14747. (calendar-move-hook nil)
  14748. (calendar-view-holidays-initially-flag nil)
  14749. (calendar-view-diary-initially-flag nil))
  14750. (if (or (org-at-timestamp-p)
  14751. (save-excursion
  14752. (beginning-of-line 1)
  14753. (looking-at (concat ".*" tsr))))
  14754. (let ((d1 (time-to-days (current-time)))
  14755. (d2 (time-to-days
  14756. (org-time-string-to-time (match-string 1)))))
  14757. (setq diff (- d2 d1))))
  14758. (calendar)
  14759. (calendar-goto-today)
  14760. (if (and diff (not arg)) (calendar-forward-day diff))))
  14761. (defun org-get-date-from-calendar ()
  14762. "Return a list (month day year) of date at point in calendar."
  14763. (with-current-buffer "*Calendar*"
  14764. (save-match-data
  14765. (calendar-cursor-to-date))))
  14766. (defun org-date-from-calendar ()
  14767. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14768. If there is already a time stamp at the cursor position, update it."
  14769. (interactive)
  14770. (if (org-at-timestamp-p t)
  14771. (org-timestamp-change 0 'calendar)
  14772. (let ((cal-date (org-get-date-from-calendar)))
  14773. (org-insert-time-stamp
  14774. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14775. (defun org-minutes-to-hh:mm-string (m)
  14776. "Compute H:MM from a number of minutes."
  14777. (let ((h (/ m 60)))
  14778. (setq m (- m (* 60 h)))
  14779. (format org-time-clocksum-format h m)))
  14780. (defun org-hh:mm-string-to-minutes (s)
  14781. "Convert a string H:MM to a number of minutes.
  14782. If the string is just a number, interpret it as minutes.
  14783. In fact, the first hh:mm or number in the string will be taken,
  14784. there can be extra stuff in the string.
  14785. If no number is found, the return value is 0."
  14786. (cond
  14787. ((integerp s) s)
  14788. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14789. (+ (* (string-to-number (match-string 1 s)) 60)
  14790. (string-to-number (match-string 2 s))))
  14791. ((string-match "\\([0-9]+\\)" s)
  14792. (string-to-number (match-string 1 s)))
  14793. (t 0)))
  14794. (defcustom org-effort-durations
  14795. `(("h" . 60)
  14796. ("d" . ,(* 60 8))
  14797. ("w" . ,(* 60 8 5))
  14798. ("m" . ,(* 60 8 5 4))
  14799. ("y" . ,(* 60 8 5 40)))
  14800. "Conversion factor to minutes for an effort modifier.
  14801. Each entry has the form (MODIFIER . MINUTES).
  14802. In an effort string, a number followed by MODIFIER is multiplied
  14803. by the specified number of MINUTES to obtain an effort in
  14804. minutes.
  14805. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14806. effort string \"2hours\" is equivalent to 120 minutes."
  14807. :group 'org-agenda
  14808. :type '(alist :key-type (string :tag "Modifier")
  14809. :value-type (number :tag "Minutes")))
  14810. (defun org-duration-string-to-minutes (s)
  14811. "Convert a duration string S to minutes.
  14812. A bare number is interpreted as minutes, modifiers can be set by
  14813. customizing `org-effort-durations' (which see).
  14814. Entries containing a colon are interpreted as H:MM by
  14815. `org-hh:mm-string-to-minutes'."
  14816. (let ((result 0)
  14817. (re (concat "\\([0-9]+\\) *\\("
  14818. (regexp-opt (mapcar 'car org-effort-durations))
  14819. "\\)")))
  14820. (while (string-match re s)
  14821. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14822. (string-to-number (match-string 1 s))))
  14823. (setq s (replace-match "" nil t s)))
  14824. (incf result (org-hh:mm-string-to-minutes s))
  14825. result))
  14826. ;;;; Files
  14827. (defun org-save-all-org-buffers ()
  14828. "Save all Org-mode buffers without user confirmation."
  14829. (interactive)
  14830. (message "Saving all Org-mode buffers...")
  14831. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14832. (when (featurep 'org-id) (org-id-locations-save))
  14833. (message "Saving all Org-mode buffers... done"))
  14834. (defun org-revert-all-org-buffers ()
  14835. "Revert all Org-mode buffers.
  14836. Prompt for confirmation when there are unsaved changes.
  14837. Be sure you know what you are doing before letting this function
  14838. overwrite your changes.
  14839. This function is useful in a setup where one tracks org files
  14840. with a version control system, to revert on one machine after pulling
  14841. changes from another. I believe the procedure must be like this:
  14842. 1. M-x org-save-all-org-buffers
  14843. 2. Pull changes from the other machine, resolve conflicts
  14844. 3. M-x org-revert-all-org-buffers"
  14845. (interactive)
  14846. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14847. (error "Abort"))
  14848. (save-excursion
  14849. (save-window-excursion
  14850. (mapc
  14851. (lambda (b)
  14852. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14853. (with-current-buffer b buffer-file-name))
  14854. (org-pop-to-buffer-same-window b)
  14855. (revert-buffer t 'no-confirm)))
  14856. (buffer-list))
  14857. (when (and (featurep 'org-id) org-id-track-globally)
  14858. (org-id-locations-load)))))
  14859. ;;;; Agenda files
  14860. ;;;###autoload
  14861. (defun org-switchb (&optional arg)
  14862. "Switch between Org buffers.
  14863. With one prefix argument, restrict available buffers to files.
  14864. With two prefix arguments, restrict available buffers to agenda files.
  14865. Defaults to `iswitchb' for buffer name completion.
  14866. Set `org-completion-use-ido' to make it use ido instead."
  14867. (interactive "P")
  14868. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14869. ((equal arg '(16)) (org-buffer-list 'agenda))
  14870. (t (org-buffer-list))))
  14871. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14872. (org-completion-use-ido org-completion-use-ido))
  14873. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14874. (setq org-completion-use-iswitchb t))
  14875. (org-pop-to-buffer-same-window
  14876. (org-icompleting-read "Org buffer: "
  14877. (mapcar 'list (mapcar 'buffer-name blist))
  14878. nil t))))
  14879. ;;; Define some older names previously used for this functionality
  14880. ;;;###autoload
  14881. (defalias 'org-ido-switchb 'org-switchb)
  14882. ;;;###autoload
  14883. (defalias 'org-iswitchb 'org-switchb)
  14884. (defun org-buffer-list (&optional predicate exclude-tmp)
  14885. "Return a list of Org buffers.
  14886. PREDICATE can be `export', `files' or `agenda'.
  14887. export restrict the list to Export buffers.
  14888. files restrict the list to buffers visiting Org files.
  14889. agenda restrict the list to buffers visiting agenda files.
  14890. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14891. (let* ((bfn nil)
  14892. (agenda-files (and (eq predicate 'agenda)
  14893. (mapcar 'file-truename (org-agenda-files t))))
  14894. (filter
  14895. (cond
  14896. ((eq predicate 'files)
  14897. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14898. ((eq predicate 'export)
  14899. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14900. ((eq predicate 'agenda)
  14901. (lambda (b)
  14902. (with-current-buffer b
  14903. (and (eq major-mode 'org-mode)
  14904. (setq bfn (buffer-file-name b))
  14905. (member (file-truename bfn) agenda-files)))))
  14906. (t (lambda (b) (with-current-buffer b
  14907. (or (eq major-mode 'org-mode)
  14908. (string-match "\*Org .*Export"
  14909. (buffer-name b)))))))))
  14910. (delq nil
  14911. (mapcar
  14912. (lambda(b)
  14913. (if (and (funcall filter b)
  14914. (or (not exclude-tmp)
  14915. (not (string-match "tmp" (buffer-name b)))))
  14916. b
  14917. nil))
  14918. (buffer-list)))))
  14919. (defun org-agenda-files (&optional unrestricted archives)
  14920. "Get the list of agenda files.
  14921. Optional UNRESTRICTED means return the full list even if a restriction
  14922. is currently in place.
  14923. When ARCHIVES is t, include all archive files that are really being
  14924. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14925. `org-agenda-archives-mode' is t."
  14926. (let ((files
  14927. (cond
  14928. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14929. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14930. ((listp org-agenda-files) org-agenda-files)
  14931. (t (error "Invalid value of `org-agenda-files'")))))
  14932. (setq files (apply 'append
  14933. (mapcar (lambda (f)
  14934. (if (file-directory-p f)
  14935. (directory-files
  14936. f t org-agenda-file-regexp)
  14937. (list f)))
  14938. files)))
  14939. (when org-agenda-skip-unavailable-files
  14940. (setq files (delq nil
  14941. (mapcar (function
  14942. (lambda (file)
  14943. (and (file-readable-p file) file)))
  14944. files))))
  14945. (when (or (eq archives t)
  14946. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14947. (setq files (org-add-archive-files files)))
  14948. files))
  14949. (defun org-agenda-file-p (&optional file)
  14950. "Return non-nil, if FILE is an agenda file.
  14951. If FILE is omitted, use the file associated with the current
  14952. buffer."
  14953. (member (or file (buffer-file-name))
  14954. (org-agenda-files t)))
  14955. (defun org-edit-agenda-file-list ()
  14956. "Edit the list of agenda files.
  14957. Depending on setup, this either uses customize to edit the variable
  14958. `org-agenda-files', or it visits the file that is holding the list. In the
  14959. latter case, the buffer is set up in a way that saving it automatically kills
  14960. the buffer and restores the previous window configuration."
  14961. (interactive)
  14962. (if (stringp org-agenda-files)
  14963. (let ((cw (current-window-configuration)))
  14964. (find-file org-agenda-files)
  14965. (org-set-local 'org-window-configuration cw)
  14966. (org-add-hook 'after-save-hook
  14967. (lambda ()
  14968. (set-window-configuration
  14969. (prog1 org-window-configuration
  14970. (kill-buffer (current-buffer))))
  14971. (org-install-agenda-files-menu)
  14972. (message "New agenda file list installed"))
  14973. nil 'local)
  14974. (message "%s" (substitute-command-keys
  14975. "Edit list and finish with \\[save-buffer]")))
  14976. (customize-variable 'org-agenda-files)))
  14977. (defun org-store-new-agenda-file-list (list)
  14978. "Set new value for the agenda file list and save it correctly."
  14979. (if (stringp org-agenda-files)
  14980. (let ((fe (org-read-agenda-file-list t)) b u)
  14981. (while (setq b (find-buffer-visiting org-agenda-files))
  14982. (kill-buffer b))
  14983. (with-temp-file org-agenda-files
  14984. (insert
  14985. (mapconcat
  14986. (lambda (f) ;; Keep un-expanded entries.
  14987. (if (setq u (assoc f fe))
  14988. (cdr u)
  14989. f))
  14990. list "\n")
  14991. "\n")))
  14992. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14993. (org-insert-mode-line-in-empty-file nil))
  14994. (setq org-agenda-files list)
  14995. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14996. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14997. "Read the list of agenda files from a file.
  14998. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14999. filenames, used by `org-store-new-agenda-file-list' to write back
  15000. un-expanded file names."
  15001. (when (file-directory-p org-agenda-files)
  15002. (error "`org-agenda-files' cannot be a single directory"))
  15003. (when (stringp org-agenda-files)
  15004. (with-temp-buffer
  15005. (insert-file-contents org-agenda-files)
  15006. (mapcar
  15007. (lambda (f)
  15008. (let ((e (expand-file-name (substitute-in-file-name f)
  15009. org-directory)))
  15010. (if pair-with-expansion
  15011. (cons e f)
  15012. e)))
  15013. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15014. ;;;###autoload
  15015. (defun org-cycle-agenda-files ()
  15016. "Cycle through the files in `org-agenda-files'.
  15017. If the current buffer visits an agenda file, find the next one in the list.
  15018. If the current buffer does not, find the first agenda file."
  15019. (interactive)
  15020. (let* ((fs (org-agenda-files t))
  15021. (files (append fs (list (car fs))))
  15022. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15023. file)
  15024. (unless files (error "No agenda files"))
  15025. (catch 'exit
  15026. (while (setq file (pop files))
  15027. (if (equal (file-truename file) tcf)
  15028. (when (car files)
  15029. (find-file (car files))
  15030. (throw 'exit t))))
  15031. (find-file (car fs)))
  15032. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15033. (defun org-agenda-file-to-front (&optional to-end)
  15034. "Move/add the current file to the top of the agenda file list.
  15035. If the file is not present in the list, it is added to the front. If it is
  15036. present, it is moved there. With optional argument TO-END, add/move to the
  15037. end of the list."
  15038. (interactive "P")
  15039. (let ((org-agenda-skip-unavailable-files nil)
  15040. (file-alist (mapcar (lambda (x)
  15041. (cons (file-truename x) x))
  15042. (org-agenda-files t)))
  15043. (ctf (file-truename buffer-file-name))
  15044. x had)
  15045. (setq x (assoc ctf file-alist) had x)
  15046. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15047. (if to-end
  15048. (setq file-alist (append (delq x file-alist) (list x)))
  15049. (setq file-alist (cons x (delq x file-alist))))
  15050. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15051. (org-install-agenda-files-menu)
  15052. (message "File %s to %s of agenda file list"
  15053. (if had "moved" "added") (if to-end "end" "front"))))
  15054. (defun org-remove-file (&optional file)
  15055. "Remove current file from the list of files in variable `org-agenda-files'.
  15056. These are the files which are being checked for agenda entries.
  15057. Optional argument FILE means use this file instead of the current."
  15058. (interactive)
  15059. (let* ((org-agenda-skip-unavailable-files nil)
  15060. (file (or file buffer-file-name))
  15061. (true-file (file-truename file))
  15062. (afile (abbreviate-file-name file))
  15063. (files (delq nil (mapcar
  15064. (lambda (x)
  15065. (if (equal true-file
  15066. (file-truename x))
  15067. nil x))
  15068. (org-agenda-files t)))))
  15069. (if (not (= (length files) (length (org-agenda-files t))))
  15070. (progn
  15071. (org-store-new-agenda-file-list files)
  15072. (org-install-agenda-files-menu)
  15073. (message "Removed file: %s" afile))
  15074. (message "File was not in list: %s (not removed)" afile))))
  15075. (defun org-file-menu-entry (file)
  15076. (vector file (list 'find-file file) t))
  15077. (defun org-check-agenda-file (file)
  15078. "Make sure FILE exists. If not, ask user what to do."
  15079. (when (not (file-exists-p file))
  15080. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15081. (abbreviate-file-name file))
  15082. (let ((r (downcase (read-char-exclusive))))
  15083. (cond
  15084. ((equal r ?r)
  15085. (org-remove-file file)
  15086. (throw 'nextfile t))
  15087. (t (error "Abort"))))))
  15088. (defun org-get-agenda-file-buffer (file)
  15089. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15090. it to the list of buffers which might be released later."
  15091. (let ((buf (org-find-base-buffer-visiting file)))
  15092. (if buf
  15093. buf ; just return it
  15094. ;; Make a new buffer and remember it
  15095. (setq buf (find-file-noselect file))
  15096. (if buf (push buf org-agenda-new-buffers))
  15097. buf)))
  15098. (defun org-release-buffers (blist)
  15099. "Release all buffers in list, asking the user for confirmation when needed.
  15100. When a buffer is unmodified, it is just killed. When modified, it is saved
  15101. \(if the user agrees) and then killed."
  15102. (let (buf file)
  15103. (while (setq buf (pop blist))
  15104. (setq file (buffer-file-name buf))
  15105. (when (and (buffer-modified-p buf)
  15106. file
  15107. (y-or-n-p (format "Save file %s? " file)))
  15108. (with-current-buffer buf (save-buffer)))
  15109. (kill-buffer buf))))
  15110. (defun org-prepare-agenda-buffers (files)
  15111. "Create buffers for all agenda files, protect archived trees and comments."
  15112. (interactive)
  15113. (let ((pa '(:org-archived t))
  15114. (pc '(:org-comment t))
  15115. (pall '(:org-archived t :org-comment t))
  15116. (inhibit-read-only t)
  15117. (rea (concat ":" org-archive-tag ":"))
  15118. bmp file re)
  15119. (save-excursion
  15120. (save-restriction
  15121. (while (setq file (pop files))
  15122. (catch 'nextfile
  15123. (if (bufferp file)
  15124. (set-buffer file)
  15125. (org-check-agenda-file file)
  15126. (set-buffer (org-get-agenda-file-buffer file)))
  15127. (widen)
  15128. (setq bmp (buffer-modified-p))
  15129. (org-refresh-category-properties)
  15130. (setq org-todo-keywords-for-agenda
  15131. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15132. (setq org-done-keywords-for-agenda
  15133. (append org-done-keywords-for-agenda org-done-keywords))
  15134. (setq org-todo-keyword-alist-for-agenda
  15135. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15136. (setq org-drawers-for-agenda
  15137. (append org-drawers-for-agenda org-drawers))
  15138. (setq org-tag-alist-for-agenda
  15139. (append org-tag-alist-for-agenda org-tag-alist))
  15140. (save-excursion
  15141. (remove-text-properties (point-min) (point-max) pall)
  15142. (when org-agenda-skip-archived-trees
  15143. (goto-char (point-min))
  15144. (while (re-search-forward rea nil t)
  15145. (if (org-at-heading-p t)
  15146. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15147. (goto-char (point-min))
  15148. (setq re (format org-heading-keyword-regexp-format
  15149. org-comment-string))
  15150. (while (re-search-forward re nil t)
  15151. (add-text-properties
  15152. (match-beginning 0) (org-end-of-subtree t) pc)))
  15153. (set-buffer-modified-p bmp)))))
  15154. (setq org-todo-keywords-for-agenda
  15155. (org-uniquify org-todo-keywords-for-agenda))
  15156. (setq org-todo-keyword-alist-for-agenda
  15157. (org-uniquify org-todo-keyword-alist-for-agenda)
  15158. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15159. ;;;; Embedded LaTeX
  15160. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15161. "Keymap for the minor `org-cdlatex-mode'.")
  15162. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15163. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15164. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15165. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15166. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15167. (defvar org-cdlatex-texmathp-advice-is-done nil
  15168. "Flag remembering if we have applied the advice to texmathp already.")
  15169. (define-minor-mode org-cdlatex-mode
  15170. "Toggle the minor `org-cdlatex-mode'.
  15171. This mode supports entering LaTeX environment and math in LaTeX fragments
  15172. in Org-mode.
  15173. \\{org-cdlatex-mode-map}"
  15174. nil " OCDL" nil
  15175. (when org-cdlatex-mode
  15176. (require 'cdlatex)
  15177. (run-hooks 'cdlatex-mode-hook)
  15178. (cdlatex-compute-tables))
  15179. (unless org-cdlatex-texmathp-advice-is-done
  15180. (setq org-cdlatex-texmathp-advice-is-done t)
  15181. (defadvice texmathp (around org-math-always-on activate)
  15182. "Always return t in org-mode buffers.
  15183. This is because we want to insert math symbols without dollars even outside
  15184. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15185. an embedded LaTeX fragment, let texmathp do its job.
  15186. \\[org-cdlatex-mode-map]"
  15187. (interactive)
  15188. (let (p)
  15189. (cond
  15190. ((not (eq major-mode 'org-mode)) ad-do-it)
  15191. ((eq this-command 'cdlatex-math-symbol)
  15192. (setq ad-return-value t
  15193. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15194. (t
  15195. (let ((p (org-inside-LaTeX-fragment-p)))
  15196. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15197. (setq ad-return-value t
  15198. texmathp-why '("Org-mode embedded math" . 0))
  15199. (if p ad-do-it)))))))))
  15200. (defun turn-on-org-cdlatex ()
  15201. "Unconditionally turn on `org-cdlatex-mode'."
  15202. (org-cdlatex-mode 1))
  15203. (defun org-inside-LaTeX-fragment-p ()
  15204. "Test if point is inside a LaTeX fragment.
  15205. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15206. sequence appearing also before point.
  15207. Even though the matchers for math are configurable, this function assumes
  15208. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15209. delimiters are skipped when they have been removed by customization.
  15210. The return value is nil, or a cons cell with the delimiter and the
  15211. position of this delimiter.
  15212. This function does a reasonably good job, but can locally be fooled by
  15213. for example currency specifications. For example it will assume being in
  15214. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15215. fragments that are properly closed, but during editing, we have to live
  15216. with the uncertainty caused by missing closing delimiters. This function
  15217. looks only before point, not after."
  15218. (catch 'exit
  15219. (let ((pos (point))
  15220. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15221. (lim (progn
  15222. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15223. (point)))
  15224. dd-on str (start 0) m re)
  15225. (goto-char pos)
  15226. (when dodollar
  15227. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15228. re (nth 1 (assoc "$" org-latex-regexps)))
  15229. (while (string-match re str start)
  15230. (cond
  15231. ((= (match-end 0) (length str))
  15232. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15233. ((= (match-end 0) (- (length str) 5))
  15234. (throw 'exit nil))
  15235. (t (setq start (match-end 0))))))
  15236. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15237. (goto-char pos)
  15238. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15239. (and (match-beginning 2) (throw 'exit nil))
  15240. ;; count $$
  15241. (while (re-search-backward "\\$\\$" lim t)
  15242. (setq dd-on (not dd-on)))
  15243. (goto-char pos)
  15244. (if dd-on (cons "$$" m))))))
  15245. (defun org-inside-latex-macro-p ()
  15246. "Is point inside a LaTeX macro or its arguments?"
  15247. (save-match-data
  15248. (org-in-regexp
  15249. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15250. (defun org-try-cdlatex-tab ()
  15251. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15252. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15253. - inside a LaTeX fragment, or
  15254. - after the first word in a line, where an abbreviation expansion could
  15255. insert a LaTeX environment."
  15256. (when org-cdlatex-mode
  15257. (cond
  15258. ;; Before any word on the line: No expansion possible.
  15259. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15260. ;; Just after first word on the line: Expand it. Make sure it
  15261. ;; cannot happen on headlines, though.
  15262. ((save-excursion
  15263. (skip-chars-backward "a-zA-Z0-9*")
  15264. (skip-chars-backward " \t")
  15265. (and (bolp) (not (org-at-heading-p))))
  15266. (cdlatex-tab) t)
  15267. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15268. (defun org-cdlatex-underscore-caret (&optional arg)
  15269. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15270. Revert to the normal definition outside of these fragments."
  15271. (interactive "P")
  15272. (if (org-inside-LaTeX-fragment-p)
  15273. (call-interactively 'cdlatex-sub-superscript)
  15274. (let (org-cdlatex-mode)
  15275. (call-interactively (key-binding (vector last-input-event))))))
  15276. (defun org-cdlatex-math-modify (&optional arg)
  15277. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15278. Revert to the normal definition outside of these fragments."
  15279. (interactive "P")
  15280. (if (org-inside-LaTeX-fragment-p)
  15281. (call-interactively 'cdlatex-math-modify)
  15282. (let (org-cdlatex-mode)
  15283. (call-interactively (key-binding (vector last-input-event))))))
  15284. (defvar org-latex-fragment-image-overlays nil
  15285. "List of overlays carrying the images of latex fragments.")
  15286. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15287. (defun org-remove-latex-fragment-image-overlays ()
  15288. "Remove all overlays with LaTeX fragment images in current buffer."
  15289. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15290. (setq org-latex-fragment-image-overlays nil))
  15291. (defun org-preview-latex-fragment (&optional subtree)
  15292. "Preview the LaTeX fragment at point, or all locally or globally.
  15293. If the cursor is in a LaTeX fragment, create the image and overlay
  15294. it over the source code. If there is no fragment at point, display
  15295. all fragments in the current text, from one headline to the next. With
  15296. prefix SUBTREE, display all fragments in the current subtree. With a
  15297. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15298. the cursor is before the first headline,
  15299. display all fragments in the buffer.
  15300. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15301. (interactive "P")
  15302. (unless buffer-file-name
  15303. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15304. (org-remove-latex-fragment-image-overlays)
  15305. (save-excursion
  15306. (save-restriction
  15307. (let (beg end at msg)
  15308. (cond
  15309. ((or (equal subtree '(16))
  15310. (not (save-excursion
  15311. (re-search-backward org-outline-regexp-bol nil t))))
  15312. (setq beg (point-min) end (point-max)
  15313. msg "Creating images for buffer...%s"))
  15314. ((equal subtree '(4))
  15315. (org-back-to-heading)
  15316. (setq beg (point) end (org-end-of-subtree t)
  15317. msg "Creating images for subtree...%s"))
  15318. (t
  15319. (if (setq at (org-inside-LaTeX-fragment-p))
  15320. (goto-char (max (point-min) (- (cdr at) 2)))
  15321. (org-back-to-heading))
  15322. (setq beg (point) end (progn (outline-next-heading) (point))
  15323. msg (if at "Creating image...%s"
  15324. "Creating images for entry...%s"))))
  15325. (message msg "")
  15326. (narrow-to-region beg end)
  15327. (goto-char beg)
  15328. (org-format-latex
  15329. (concat "ltxpng/" (file-name-sans-extension
  15330. (file-name-nondirectory
  15331. buffer-file-name)))
  15332. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15333. (message msg "done. Use `C-c C-c' to remove images.")))))
  15334. (defvar org-latex-regexps
  15335. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15336. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15337. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15338. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15339. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15340. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15341. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15342. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15343. "Regular expressions for matching embedded LaTeX.")
  15344. (defvar org-export-have-math nil) ;; dynamic scoping
  15345. (defun org-format-latex (prefix &optional dir overlays msg at
  15346. forbuffer processing-type)
  15347. "Replace LaTeX fragments with links to an image, and produce images.
  15348. Some of the options can be changed using the variable
  15349. `org-format-latex-options'."
  15350. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15351. (let* ((prefixnodir (file-name-nondirectory prefix))
  15352. (absprefix (expand-file-name prefix dir))
  15353. (todir (file-name-directory absprefix))
  15354. (opt org-format-latex-options)
  15355. (matchers (plist-get opt :matchers))
  15356. (re-list org-latex-regexps)
  15357. (org-format-latex-header-extra
  15358. (plist-get (org-infile-export-plist) :latex-header-extra))
  15359. (cnt 0) txt hash link beg end re e checkdir
  15360. executables-checked string
  15361. m n block-type block linkfile movefile ov)
  15362. ;; Check the different regular expressions
  15363. (while (setq e (pop re-list))
  15364. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15365. block (if block-type "\n\n" ""))
  15366. (when (member m matchers)
  15367. (goto-char (point-min))
  15368. (while (re-search-forward re nil t)
  15369. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15370. (not (get-text-property (match-beginning n)
  15371. 'org-protected))
  15372. (or (not overlays)
  15373. (not (eq (get-char-property (match-beginning n)
  15374. 'org-overlay-type)
  15375. 'org-latex-overlay))))
  15376. (setq org-export-have-math t)
  15377. (cond
  15378. ((eq processing-type 'verbatim)
  15379. ;; Leave the text verbatim, just protect it
  15380. (add-text-properties (match-beginning n) (match-end n)
  15381. '(org-protected t)))
  15382. ((eq processing-type 'mathjax)
  15383. ;; Prepare for MathJax processing
  15384. (setq string (match-string n))
  15385. (if (member m '("$" "$1"))
  15386. (save-excursion
  15387. (delete-region (match-beginning n) (match-end n))
  15388. (goto-char (match-beginning n))
  15389. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15390. "\\)")
  15391. '(org-protected t))))
  15392. (add-text-properties (match-beginning n) (match-end n)
  15393. '(org-protected t))))
  15394. ((eq processing-type 'dvipng)
  15395. ;; Process to an image
  15396. (setq txt (match-string n)
  15397. beg (match-beginning n) end (match-end n)
  15398. cnt (1+ cnt))
  15399. (let (print-length print-level) ; make sure full list is printed
  15400. (setq hash (sha1 (prin1-to-string
  15401. (list org-format-latex-header
  15402. org-format-latex-header-extra
  15403. org-export-latex-default-packages-alist
  15404. org-export-latex-packages-alist
  15405. org-format-latex-options
  15406. forbuffer txt)))
  15407. linkfile (format "%s_%s.png" prefix hash)
  15408. movefile (format "%s_%s.png" absprefix hash)))
  15409. (setq link (concat block "[[file:" linkfile "]]" block))
  15410. (if msg (message msg cnt))
  15411. (goto-char beg)
  15412. (unless checkdir ; make sure the directory exists
  15413. (setq checkdir t)
  15414. (or (file-directory-p todir) (make-directory todir t)))
  15415. (unless executables-checked
  15416. (org-check-external-command
  15417. "latex" "needed to convert LaTeX fragments to images")
  15418. (org-check-external-command
  15419. "dvipng" "needed to convert LaTeX fragments to images")
  15420. (setq executables-checked t))
  15421. (unless (file-exists-p movefile)
  15422. (org-create-formula-image
  15423. txt movefile opt forbuffer))
  15424. (if overlays
  15425. (progn
  15426. (mapc (lambda (o)
  15427. (if (eq (overlay-get o 'org-overlay-type)
  15428. 'org-latex-overlay)
  15429. (delete-overlay o)))
  15430. (overlays-in beg end))
  15431. (setq ov (make-overlay beg end))
  15432. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15433. (if (featurep 'xemacs)
  15434. (progn
  15435. (overlay-put ov 'invisible t)
  15436. (overlay-put
  15437. ov 'end-glyph
  15438. (make-glyph (vector 'png :file movefile))))
  15439. (overlay-put
  15440. ov 'display
  15441. (list 'image :type 'png :file movefile :ascent 'center)))
  15442. (push ov org-latex-fragment-image-overlays)
  15443. (goto-char end))
  15444. (delete-region beg end)
  15445. (insert (org-add-props link
  15446. (list 'org-latex-src
  15447. (replace-regexp-in-string
  15448. "\"" "" txt)
  15449. 'org-latex-src-embed-type
  15450. (if block-type 'paragraph 'character))))))
  15451. ((eq processing-type 'mathml)
  15452. ;; Process to MathML
  15453. (unless executables-checked
  15454. (unless (save-match-data (org-format-latex-mathml-available-p))
  15455. (error "LaTeX to MathML converter not configured"))
  15456. (setq executables-checked t))
  15457. (setq txt (match-string n)
  15458. beg (match-beginning n) end (match-end n)
  15459. cnt (1+ cnt))
  15460. (if msg (message msg cnt))
  15461. (goto-char beg)
  15462. (delete-region beg end)
  15463. (insert (org-format-latex-as-mathml
  15464. txt block-type prefix dir)))
  15465. (t
  15466. (error "Unknown conversion type %s for latex fragments"
  15467. processing-type)))))))))
  15468. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15469. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15470. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15471. sucessful, return the portion between \"<math...> </math>\"
  15472. elements otherwise return nil. When MATHML-FILE is specified,
  15473. write the results in to that file. When invoked as an
  15474. interactive command, prompt for LATEX-FRAG, with initial value
  15475. set to the current active region and echo the results for user
  15476. inspection."
  15477. (interactive (list (let ((frag (when (region-active-p)
  15478. (buffer-substring-no-properties
  15479. (region-beginning) (region-end)))))
  15480. (read-string "LaTeX Fragment: " frag nil frag))))
  15481. (unless latex-frag (error "Invalid latex-frag"))
  15482. (let* ((tmp-in-file (file-relative-name
  15483. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15484. (ignore (write-region latex-frag nil tmp-in-file))
  15485. (tmp-out-file (file-relative-name
  15486. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15487. (cmd (format-spec
  15488. org-latex-to-mathml-convert-command
  15489. `((?j . ,(shell-quote-argument
  15490. (expand-file-name org-latex-to-mathml-jar-file)))
  15491. (?I . ,(shell-quote-argument tmp-in-file))
  15492. (?o . ,(shell-quote-argument tmp-out-file)))))
  15493. mathml shell-command-output)
  15494. (when (org-called-interactively-p 'any)
  15495. (unless (org-format-latex-mathml-available-p)
  15496. (error "LaTeX to MathML converter not configured")))
  15497. (message "Running %s" cmd)
  15498. (setq shell-command-output (shell-command-to-string cmd))
  15499. (setq mathml
  15500. (when (file-readable-p tmp-out-file)
  15501. (with-current-buffer (find-file-noselect tmp-out-file t)
  15502. (goto-char (point-min))
  15503. (when (re-search-forward
  15504. (concat
  15505. (regexp-quote
  15506. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15507. "\\(.\\|\n\\)*"
  15508. (regexp-quote "</math>")) nil t)
  15509. (prog1 (match-string 0) (kill-buffer))))))
  15510. (cond
  15511. (mathml
  15512. (setq mathml
  15513. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15514. (when mathml-file
  15515. (write-region mathml nil mathml-file))
  15516. (when (org-called-interactively-p 'any)
  15517. (message mathml)))
  15518. ((message "LaTeX to MathML conversion failed")
  15519. (message shell-command-output)))
  15520. (delete-file tmp-in-file)
  15521. (when (file-exists-p tmp-out-file)
  15522. (delete-file tmp-out-file))
  15523. mathml))
  15524. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15525. prefix &optional dir)
  15526. "Use `org-create-math-formula' but check local cache first."
  15527. (let* ((absprefix (expand-file-name prefix dir))
  15528. (print-length nil) (print-level nil)
  15529. (formula-id (concat
  15530. "formula-"
  15531. (sha1
  15532. (prin1-to-string
  15533. (list latex-frag
  15534. org-latex-to-mathml-convert-command)))))
  15535. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15536. (formula-cache-dir (file-name-directory formula-cache)))
  15537. (unless (file-directory-p formula-cache-dir)
  15538. (make-directory formula-cache-dir t))
  15539. (unless (file-exists-p formula-cache)
  15540. (org-create-math-formula latex-frag formula-cache))
  15541. (if (file-exists-p formula-cache)
  15542. ;; Successful conversion. Return the link to MathML file.
  15543. (org-add-props
  15544. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15545. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15546. 'org-latex-src-embed-type (if latex-frag-type
  15547. 'paragraph 'character)))
  15548. ;; Failed conversion. Return the LaTeX fragment verbatim
  15549. (add-text-properties
  15550. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15551. latex-frag)))
  15552. ;; This function borrows from Ganesh Swami's latex2png.el
  15553. (defun org-create-formula-image (string tofile options buffer)
  15554. "This calls dvipng."
  15555. (require 'org-latex)
  15556. (let* ((tmpdir (if (featurep 'xemacs)
  15557. (temp-directory)
  15558. temporary-file-directory))
  15559. (texfilebase (make-temp-name
  15560. (expand-file-name "orgtex" tmpdir)))
  15561. (texfile (concat texfilebase ".tex"))
  15562. (dvifile (concat texfilebase ".dvi"))
  15563. (pngfile (concat texfilebase ".png"))
  15564. (fnh (if (featurep 'xemacs)
  15565. (font-height (face-font 'default))
  15566. (face-attribute 'default :height nil)))
  15567. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15568. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15569. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15570. "Black"))
  15571. (bg (or (plist-get options (if buffer :background :html-background))
  15572. "Transparent")))
  15573. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15574. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15575. (with-temp-file texfile
  15576. (insert (org-splice-latex-header
  15577. org-format-latex-header
  15578. org-export-latex-default-packages-alist
  15579. org-export-latex-packages-alist t
  15580. org-format-latex-header-extra))
  15581. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15582. (require 'org-latex)
  15583. (org-export-latex-fix-inputenc))
  15584. (let ((dir default-directory))
  15585. (condition-case nil
  15586. (progn
  15587. (cd tmpdir)
  15588. (call-process "latex" nil nil nil texfile))
  15589. (error nil))
  15590. (cd dir))
  15591. (if (not (file-exists-p dvifile))
  15592. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15593. (condition-case nil
  15594. (if (featurep 'xemacs)
  15595. (call-process "dvipng" nil nil nil
  15596. "-fg" fg "-bg" bg
  15597. "-T" "tight"
  15598. "-o" pngfile
  15599. dvifile)
  15600. (call-process "dvipng" nil nil nil
  15601. "-fg" fg "-bg" bg
  15602. "-D" dpi
  15603. ;;"-x" scale "-y" scale
  15604. "-T" "tight"
  15605. "-o" pngfile
  15606. dvifile))
  15607. (error nil))
  15608. (if (not (file-exists-p pngfile))
  15609. (if org-format-latex-signal-error
  15610. (error "Failed to create png file from %s" texfile)
  15611. (message "Failed to create png file from %s" texfile)
  15612. nil)
  15613. ;; Use the requested file name and clean up
  15614. (copy-file pngfile tofile 'replace)
  15615. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15616. (delete-file (concat texfilebase e)))
  15617. pngfile))))
  15618. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15619. "Fill a LaTeX header template TPL.
  15620. In the template, the following place holders will be recognized:
  15621. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15622. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15623. [PACKAGES] \\usepackage statements for PKG
  15624. [NO-PACKAGES] do not include PKG
  15625. [EXTRA] the string EXTRA
  15626. [NO-EXTRA] do not include EXTRA
  15627. For backward compatibility, if both the positive and the negative place
  15628. holder is missing, the positive one (without the \"NO-\") will be
  15629. assumed to be present at the end of the template.
  15630. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15631. EXTRA is a string.
  15632. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15633. (let (rpl (end ""))
  15634. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15635. (setq rpl (if (or (match-end 1) (not def-pkg))
  15636. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15637. tpl (replace-match rpl t t tpl))
  15638. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15639. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15640. (setq rpl (if (or (match-end 1) (not pkg))
  15641. "" (org-latex-packages-to-string pkg snippets-p t))
  15642. tpl (replace-match rpl t t tpl))
  15643. (if pkg (setq end
  15644. (concat end "\n"
  15645. (org-latex-packages-to-string pkg snippets-p)))))
  15646. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15647. (setq rpl (if (or (match-end 1) (not extra))
  15648. "" (concat extra "\n"))
  15649. tpl (replace-match rpl t t tpl))
  15650. (if (and extra (string-match "\\S-" extra))
  15651. (setq end (concat end "\n" extra))))
  15652. (if (string-match "\\S-" end)
  15653. (concat tpl "\n" end)
  15654. tpl)))
  15655. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15656. "Turn an alist of packages into a string with the \\usepackage macros."
  15657. (setq pkg (mapconcat (lambda(p)
  15658. (cond
  15659. ((stringp p) p)
  15660. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15661. (format "%% Package %s omitted" (cadr p)))
  15662. ((equal "" (car p))
  15663. (format "\\usepackage{%s}" (cadr p)))
  15664. (t
  15665. (format "\\usepackage[%s]{%s}"
  15666. (car p) (cadr p)))))
  15667. pkg
  15668. "\n"))
  15669. (if newline (concat pkg "\n") pkg))
  15670. (defun org-dvipng-color (attr)
  15671. "Return an rgb color specification for dvipng."
  15672. (apply 'format "rgb %s %s %s"
  15673. (mapcar 'org-normalize-color
  15674. (if (featurep 'xemacs)
  15675. (color-rgb-components
  15676. (face-property 'default
  15677. (cond ((eq attr :foreground) 'foreground)
  15678. ((eq attr :background) 'background))))
  15679. (color-values (face-attribute 'default attr nil))))))
  15680. (defun org-normalize-color (value)
  15681. "Return string to be used as color value for an RGB component."
  15682. (format "%g" (/ value 65535.0)))
  15683. ;; Image display
  15684. (defvar org-inline-image-overlays nil)
  15685. (make-variable-buffer-local 'org-inline-image-overlays)
  15686. (defun org-toggle-inline-images (&optional include-linked)
  15687. "Toggle the display of inline images.
  15688. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15689. (interactive "P")
  15690. (if org-inline-image-overlays
  15691. (progn
  15692. (org-remove-inline-images)
  15693. (message "Inline image display turned off"))
  15694. (org-display-inline-images include-linked)
  15695. (if org-inline-image-overlays
  15696. (message "%d images displayed inline"
  15697. (length org-inline-image-overlays))
  15698. (message "No images to display inline"))))
  15699. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15700. "Display inline images.
  15701. Normally only links without a description part are inlined, because this
  15702. is how it will work for export. When INCLUDE-LINKED is set, also links
  15703. with a description part will be inlined. This can be nice for a quick
  15704. look at those images, but it does not reflect what exported files will look
  15705. like.
  15706. When REFRESH is set, refresh existing images between BEG and END.
  15707. This will create new image displays only if necessary.
  15708. BEG and END default to the buffer boundaries."
  15709. (interactive "P")
  15710. (unless refresh
  15711. (org-remove-inline-images)
  15712. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15713. (save-excursion
  15714. (save-restriction
  15715. (widen)
  15716. (setq beg (or beg (point-min)) end (or end (point-max)))
  15717. (goto-char (point-min))
  15718. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15719. (substring (org-image-file-name-regexp) 0 -2)
  15720. "\\)\\]" (if include-linked "" "\\]")))
  15721. old file ov img)
  15722. (while (re-search-forward re end t)
  15723. (setq old (get-char-property-and-overlay (match-beginning 1)
  15724. 'org-image-overlay))
  15725. (setq file (expand-file-name
  15726. (concat (or (match-string 3) "") (match-string 4))))
  15727. (when (file-exists-p file)
  15728. (if (and (car-safe old) refresh)
  15729. (image-refresh (overlay-get (cdr old) 'display))
  15730. (setq img (save-match-data (create-image file)))
  15731. (when img
  15732. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15733. (overlay-put ov 'display img)
  15734. (overlay-put ov 'face 'default)
  15735. (overlay-put ov 'org-image-overlay t)
  15736. (overlay-put ov 'modification-hooks
  15737. (list 'org-display-inline-modification-hook))
  15738. (push ov org-inline-image-overlays)))))))))
  15739. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15740. "Remove inline-display overlay if a corresponding region is modified."
  15741. (let ((inhibit-modification-hooks t))
  15742. (when (and ov after)
  15743. (delete ov org-inline-image-overlays)
  15744. (delete-overlay ov))))
  15745. (defun org-remove-inline-images ()
  15746. "Remove inline display of images."
  15747. (interactive)
  15748. (mapc 'delete-overlay org-inline-image-overlays)
  15749. (setq org-inline-image-overlays nil))
  15750. ;;;; Key bindings
  15751. ;; Outline functions from `outline-mode-prefix-map'
  15752. ;; that can be remapped in Org:
  15753. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15754. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15755. (define-key org-mode-map [remap outline-forward-same-level]
  15756. 'org-forward-same-level)
  15757. (define-key org-mode-map [remap outline-backward-same-level]
  15758. 'org-backward-same-level)
  15759. (define-key org-mode-map [remap show-branches]
  15760. 'org-kill-note-or-show-branches)
  15761. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15762. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15763. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15764. ;; Outline functions from `outline-mode-prefix-map'
  15765. ;; that can not be remapped in Org:
  15766. ;; - the column "key binding" shows whether the Outline function is still
  15767. ;; available in Org mode on the same key that it has been bound to in
  15768. ;; Outline mode:
  15769. ;; - "overridden": key used for a different functionality in Org mode
  15770. ;; - else: key still bound to the same Outline function in Org mode
  15771. ;; | Outline function | key binding | Org replacement |
  15772. ;; |------------------------------------+-------------+-----------------------|
  15773. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15774. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15775. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15776. ;; | `hide-subtree' | overridden | visibility cycling |
  15777. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15778. ;; | `hide-body' | overridden | no replacement |
  15779. ;; | `show-all' | overridden | no replacement |
  15780. ;; | `hide-entry' | overridden | visibility cycling |
  15781. ;; | `show-entry' | overridden | no replacement |
  15782. ;; | `hide-leaves' | overridden | no replacement |
  15783. ;; | `hide-sublevels' | overridden | no replacement |
  15784. ;; | `hide-other' | overridden | no replacement |
  15785. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15786. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15787. ;; Make `C-c C-x' a prefix key
  15788. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15789. ;; TAB key with modifiers
  15790. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15791. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15792. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15793. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15794. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15795. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15796. ;; The following line is necessary under Suse GNU/Linux
  15797. (unless (featurep 'xemacs)
  15798. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15799. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15800. (define-key org-mode-map [backtab] 'org-shifttab)
  15801. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15802. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15803. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15804. ;; Cursor keys with modifiers
  15805. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15806. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15807. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15808. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15809. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15810. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15811. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15812. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15813. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15814. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15815. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15816. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15817. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15818. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15819. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15820. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15821. ;; Babel keys
  15822. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15823. (mapc (lambda (pair)
  15824. (define-key org-babel-map (car pair) (cdr pair)))
  15825. org-babel-key-bindings)
  15826. ;;; Extra keys for tty access.
  15827. ;; We only set them when really needed because otherwise the
  15828. ;; menus don't show the simple keys
  15829. (when (or org-use-extra-keys
  15830. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15831. (not window-system))
  15832. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15833. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15834. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15835. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15836. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15837. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15838. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15839. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15840. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15841. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15842. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15843. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15844. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15845. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15846. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15847. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15848. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15849. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15850. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15851. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15852. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15853. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15854. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15855. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15856. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15857. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15858. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15859. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15860. ;; All the other keys
  15861. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15862. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15863. (if (boundp 'narrow-map)
  15864. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15865. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15866. (if (boundp 'narrow-map)
  15867. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15868. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15869. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15870. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15871. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15872. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15873. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15874. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15875. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15876. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15877. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15878. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15879. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15880. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15881. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15882. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15883. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15884. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15885. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15886. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15887. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15888. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15889. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15890. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15891. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15892. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15893. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15894. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15895. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15896. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15897. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15898. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15899. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15900. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15901. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15902. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15903. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15904. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15905. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15906. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15907. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15908. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15909. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15910. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15911. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15912. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15913. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15914. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15915. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15916. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15917. (org-defkey org-mode-map "\C-m" 'org-return)
  15918. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15919. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15920. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15921. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15922. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15923. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15924. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15925. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15926. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15927. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15928. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15929. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15930. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15931. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15932. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15933. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15934. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15935. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15936. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15937. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15938. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15939. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15940. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15941. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15942. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15943. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15944. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15945. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15946. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15947. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15948. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15949. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15950. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15951. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15952. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15953. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15954. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15955. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15956. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15957. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15958. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15959. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15960. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15961. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15962. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15963. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15964. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15965. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15966. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15967. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15968. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15969. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15970. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15971. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15972. (when (featurep 'xemacs)
  15973. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15974. (defconst org-speed-commands-default
  15975. '(
  15976. ("Outline Navigation")
  15977. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15978. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15979. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15980. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15981. ("u" . (org-speed-move-safe 'outline-up-heading))
  15982. ("j" . org-goto)
  15983. ("g" . (org-refile t))
  15984. ("Outline Visibility")
  15985. ("c" . org-cycle)
  15986. ("C" . org-shifttab)
  15987. (" " . org-display-outline-path)
  15988. ("Outline Structure Editing")
  15989. ("U" . org-shiftmetaup)
  15990. ("D" . org-shiftmetadown)
  15991. ("r" . org-metaright)
  15992. ("l" . org-metaleft)
  15993. ("R" . org-shiftmetaright)
  15994. ("L" . org-shiftmetaleft)
  15995. ("i" . (progn (forward-char 1) (call-interactively
  15996. 'org-insert-heading-respect-content)))
  15997. ("^" . org-sort)
  15998. ("w" . org-refile)
  15999. ("a" . org-archive-subtree-default-with-confirmation)
  16000. ("." . org-mark-subtree)
  16001. ("Clock Commands")
  16002. ("I" . org-clock-in)
  16003. ("O" . org-clock-out)
  16004. ("Meta Data Editing")
  16005. ("t" . org-todo)
  16006. ("0" . (org-priority ?\ ))
  16007. ("1" . (org-priority ?A))
  16008. ("2" . (org-priority ?B))
  16009. ("3" . (org-priority ?C))
  16010. (";" . org-set-tags-command)
  16011. ("e" . org-set-effort)
  16012. ("Agenda Views etc")
  16013. ("v" . org-agenda)
  16014. ("/" . org-sparse-tree)
  16015. ("Misc")
  16016. ("o" . org-open-at-point)
  16017. ("?" . org-speed-command-help)
  16018. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16019. (">" . (org-agenda-remove-restriction-lock))
  16020. )
  16021. "The default speed commands.")
  16022. (defun org-print-speed-command (e)
  16023. (if (> (length (car e)) 1)
  16024. (progn
  16025. (princ "\n")
  16026. (princ (car e))
  16027. (princ "\n")
  16028. (princ (make-string (length (car e)) ?-))
  16029. (princ "\n"))
  16030. (princ (car e))
  16031. (princ " ")
  16032. (if (symbolp (cdr e))
  16033. (princ (symbol-name (cdr e)))
  16034. (prin1 (cdr e)))
  16035. (princ "\n")))
  16036. (defun org-speed-command-help ()
  16037. "Show the available speed commands."
  16038. (interactive)
  16039. (if (not org-use-speed-commands)
  16040. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16041. (with-output-to-temp-buffer "*Help*"
  16042. (princ "User-defined Speed commands\n===========================\n")
  16043. (mapc 'org-print-speed-command org-speed-commands-user)
  16044. (princ "\n")
  16045. (princ "Built-in Speed commands\n=======================\n")
  16046. (mapc 'org-print-speed-command org-speed-commands-default))
  16047. (with-current-buffer "*Help*"
  16048. (setq truncate-lines t))))
  16049. (defun org-speed-move-safe (cmd)
  16050. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16051. If not, return to the original position and throw an error."
  16052. (interactive)
  16053. (let ((pos (point)))
  16054. (call-interactively cmd)
  16055. (unless (and (bolp) (org-at-heading-p))
  16056. (goto-char pos)
  16057. (error "Boundary reached while executing %s" cmd))))
  16058. (defvar org-self-insert-command-undo-counter 0)
  16059. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16060. (defvar org-speed-command nil)
  16061. (defun org-speed-command-default-hook (keys)
  16062. "Hook for activating single-letter speed commands.
  16063. `org-speed-commands-default' specifies a minimal command set.
  16064. Use `org-speed-commands-user' for further customization."
  16065. (when (or (and (bolp) (looking-at org-outline-regexp))
  16066. (and (functionp org-use-speed-commands)
  16067. (funcall org-use-speed-commands)))
  16068. (cdr (assoc keys (append org-speed-commands-user
  16069. org-speed-commands-default)))))
  16070. (defun org-babel-speed-command-hook (keys)
  16071. "Hook for activating single-letter code block commands."
  16072. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16073. (cdr (assoc keys org-babel-key-bindings))))
  16074. (defcustom org-speed-command-hook
  16075. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16076. "Hook for activating speed commands at strategic locations.
  16077. Hook functions are called in sequence until a valid handler is
  16078. found.
  16079. Each hook takes a single argument, a user-pressed command key
  16080. which is also a `self-insert-command' from the global map.
  16081. Within the hook, examine the cursor position and the command key
  16082. and return nil or a valid handler as appropriate. Handler could
  16083. be one of an interactive command, a function, or a form.
  16084. Set `org-use-speed-commands' to non-nil value to enable this
  16085. hook. The default setting is `org-speed-command-default-hook'."
  16086. :group 'org-structure
  16087. :type 'hook)
  16088. (defun org-self-insert-command (N)
  16089. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16090. If the cursor is in a table looking at whitespace, the whitespace is
  16091. overwritten, and the table is not marked as requiring realignment."
  16092. (interactive "p")
  16093. (org-check-before-invisible-edit 'insert)
  16094. (cond
  16095. ((and org-use-speed-commands
  16096. (setq org-speed-command
  16097. (run-hook-with-args-until-success
  16098. 'org-speed-command-hook (this-command-keys))))
  16099. (cond
  16100. ((commandp org-speed-command)
  16101. (setq this-command org-speed-command)
  16102. (call-interactively org-speed-command))
  16103. ((functionp org-speed-command)
  16104. (funcall org-speed-command))
  16105. ((and org-speed-command (listp org-speed-command))
  16106. (eval org-speed-command))
  16107. (t (let (org-use-speed-commands)
  16108. (call-interactively 'org-self-insert-command)))))
  16109. ((and
  16110. (org-table-p)
  16111. (progn
  16112. ;; check if we blank the field, and if that triggers align
  16113. (and (featurep 'org-table) org-table-auto-blank-field
  16114. (member last-command
  16115. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16116. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16117. ;; got extra space, this field does not determine column width
  16118. (let (org-table-may-need-update) (org-table-blank-field))
  16119. ;; no extra space, this field may determine column width
  16120. (org-table-blank-field)))
  16121. t)
  16122. (eq N 1)
  16123. (looking-at "[^|\n]* |"))
  16124. (let (org-table-may-need-update)
  16125. (goto-char (1- (match-end 0)))
  16126. (delete-char -1)
  16127. (goto-char (match-beginning 0))
  16128. (self-insert-command N)))
  16129. (t
  16130. (setq org-table-may-need-update t)
  16131. (self-insert-command N)
  16132. (org-fix-tags-on-the-fly)
  16133. (if org-self-insert-cluster-for-undo
  16134. (if (not (eq last-command 'org-self-insert-command))
  16135. (setq org-self-insert-command-undo-counter 1)
  16136. (if (>= org-self-insert-command-undo-counter 20)
  16137. (setq org-self-insert-command-undo-counter 1)
  16138. (and (> org-self-insert-command-undo-counter 0)
  16139. buffer-undo-list (listp buffer-undo-list)
  16140. (not (cadr buffer-undo-list)) ; remove nil entry
  16141. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16142. (setq org-self-insert-command-undo-counter
  16143. (1+ org-self-insert-command-undo-counter))))))))
  16144. (defun org-check-before-invisible-edit (kind)
  16145. "Check is editing if kind KIND would be dangerous with invisible text around.
  16146. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16147. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16148. (if (and org-catch-invisible-edits
  16149. (or (not (boundp 'visible-mode)) (not visible-mode))
  16150. (or (get-char-property (point) 'invisible)
  16151. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16152. ;; OK, we need to take a closer look
  16153. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16154. (invisible-before-point (if (bobp) nil (get-char-property
  16155. (1- (point)) 'invisible)))
  16156. (border-and-ok-direction
  16157. (or
  16158. ;; Check if we are acting predictably before invisible text
  16159. (and invisible-at-point (not invisible-before-point)
  16160. (memq kind '(insert delete-backward)))
  16161. ;; Check if we are acting predictably after invisible text
  16162. ;; This works not well, and I have turned it off. It seems
  16163. ;; better to always show and stop after invisible text.
  16164. ;; (and (not invisible-at-point) invisible-before-point
  16165. ;; (memq kind '(insert delete)))
  16166. )))
  16167. (when (or (memq invisible-at-point '(outline org-hide-block))
  16168. (memq invisible-before-point '(outline org-hide-block)))
  16169. (if (eq org-catch-invisible-edits 'error)
  16170. (error "Editing in invisible areas is prohibited - make visible first"))
  16171. ;; Make the area visible
  16172. (save-excursion
  16173. (if invisible-before-point
  16174. (goto-char (previous-single-char-property-change
  16175. (point) 'invisible)))
  16176. (org-cycle))
  16177. (cond
  16178. ((eq org-catch-invisible-edits 'show)
  16179. ;; That's it, we do the edit after showing
  16180. (message
  16181. "Unfolding invisible region around point before editing")
  16182. (sit-for 1))
  16183. ((and (eq org-catch-invisible-edits 'smart)
  16184. border-and-ok-direction)
  16185. (message "Unfolding invisible region around point before editing"))
  16186. (t
  16187. ;; Don't do the edit, make the user repeat it in full visibility
  16188. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16189. (defun org-fix-tags-on-the-fly ()
  16190. (when (and (equal (char-after (point-at-bol)) ?*)
  16191. (org-at-heading-p))
  16192. (org-align-tags-here org-tags-column)))
  16193. (defun org-delete-backward-char (N)
  16194. "Like `delete-backward-char', insert whitespace at field end in tables.
  16195. When deleting backwards, in tables this function will insert whitespace in
  16196. front of the next \"|\" separator, to keep the table aligned. The table will
  16197. still be marked for re-alignment if the field did fill the entire column,
  16198. because, in this case the deletion might narrow the column."
  16199. (interactive "p")
  16200. (org-check-before-invisible-edit 'delete-backward)
  16201. (if (and (org-table-p)
  16202. (eq N 1)
  16203. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16204. (looking-at ".*?|"))
  16205. (let ((pos (point))
  16206. (noalign (looking-at "[^|\n\r]* |"))
  16207. (c org-table-may-need-update))
  16208. (backward-delete-char N)
  16209. (if (not overwrite-mode)
  16210. (progn
  16211. (skip-chars-forward "^|")
  16212. (insert " ")
  16213. (goto-char (1- pos))))
  16214. ;; noalign: if there were two spaces at the end, this field
  16215. ;; does not determine the width of the column.
  16216. (if noalign (setq org-table-may-need-update c)))
  16217. (backward-delete-char N)
  16218. (org-fix-tags-on-the-fly)))
  16219. (defun org-delete-char (N)
  16220. "Like `delete-char', but insert whitespace at field end in tables.
  16221. When deleting characters, in tables this function will insert whitespace in
  16222. front of the next \"|\" separator, to keep the table aligned. The table will
  16223. still be marked for re-alignment if the field did fill the entire column,
  16224. because, in this case the deletion might narrow the column."
  16225. (interactive "p")
  16226. (org-check-before-invisible-edit 'delete)
  16227. (if (and (org-table-p)
  16228. (not (bolp))
  16229. (not (= (char-after) ?|))
  16230. (eq N 1))
  16231. (if (looking-at ".*?|")
  16232. (let ((pos (point))
  16233. (noalign (looking-at "[^|\n\r]* |"))
  16234. (c org-table-may-need-update))
  16235. (replace-match (concat
  16236. (substring (match-string 0) 1 -1)
  16237. " |"))
  16238. (goto-char pos)
  16239. ;; noalign: if there were two spaces at the end, this field
  16240. ;; does not determine the width of the column.
  16241. (if noalign (setq org-table-may-need-update c)))
  16242. (delete-char N))
  16243. (delete-char N)
  16244. (org-fix-tags-on-the-fly)))
  16245. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16246. (put 'org-self-insert-command 'delete-selection t)
  16247. (put 'orgtbl-self-insert-command 'delete-selection t)
  16248. (put 'org-delete-char 'delete-selection 'supersede)
  16249. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16250. (put 'org-yank 'delete-selection 'yank)
  16251. ;; Make `flyspell-mode' delay after some commands
  16252. (put 'org-self-insert-command 'flyspell-delayed t)
  16253. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16254. (put 'org-delete-char 'flyspell-delayed t)
  16255. (put 'org-delete-backward-char 'flyspell-delayed t)
  16256. ;; Make pabbrev-mode expand after org-mode commands
  16257. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16258. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16259. ;; How to do this: Measure non-white length of current string
  16260. ;; If equal to column width, we should realign.
  16261. (defun org-remap (map &rest commands)
  16262. "In MAP, remap the functions given in COMMANDS.
  16263. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16264. (let (new old)
  16265. (while commands
  16266. (setq old (pop commands) new (pop commands))
  16267. (if (fboundp 'command-remapping)
  16268. (org-defkey map (vector 'remap old) new)
  16269. (substitute-key-definition old new map global-map)))))
  16270. (when (eq org-enable-table-editor 'optimized)
  16271. ;; If the user wants maximum table support, we need to hijack
  16272. ;; some standard editing functions
  16273. (org-remap org-mode-map
  16274. 'self-insert-command 'org-self-insert-command
  16275. 'delete-char 'org-delete-char
  16276. 'delete-backward-char 'org-delete-backward-char)
  16277. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16278. (defvar org-ctrl-c-ctrl-c-hook nil
  16279. "Hook for functions attaching themselves to `C-c C-c'.
  16280. This can be used to add additional functionality to the C-c C-c
  16281. key which executes context-dependent commands. This hook is run
  16282. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16283. run after the last test.
  16284. Each function will be called with no arguments. The function
  16285. must check if the context is appropriate for it to act. If yes,
  16286. it should do its thing and then return a non-nil value. If the
  16287. context is wrong, just do nothing and return nil.")
  16288. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16289. "Hook for functions attaching themselves to `C-c C-c'.
  16290. This can be used to add additional functionality to the C-c C-c
  16291. key which executes context-dependent commands. This hook is run
  16292. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16293. before the first test.
  16294. Each function will be called with no arguments. The function
  16295. must check if the context is appropriate for it to act. If yes,
  16296. it should do its thing and then return a non-nil value. If the
  16297. context is wrong, just do nothing and return nil.")
  16298. (defvar org-tab-first-hook nil
  16299. "Hook for functions to attach themselves to TAB.
  16300. See `org-ctrl-c-ctrl-c-hook' for more information.
  16301. This hook runs as the first action when TAB is pressed, even before
  16302. `org-cycle' messes around with the `outline-regexp' to cater for
  16303. inline tasks and plain list item folding.
  16304. If any function in this hook returns t, any other actions that
  16305. would have been caused by TAB (such as table field motion or visibility
  16306. cycling) will not occur.")
  16307. (defvar org-tab-after-check-for-table-hook nil
  16308. "Hook for functions to attach themselves to TAB.
  16309. See `org-ctrl-c-ctrl-c-hook' for more information.
  16310. This hook runs after it has been established that the cursor is not in a
  16311. table, but before checking if the cursor is in a headline or if global cycling
  16312. should be done.
  16313. If any function in this hook returns t, not other actions like visibility
  16314. cycling will be done.")
  16315. (defvar org-tab-after-check-for-cycling-hook nil
  16316. "Hook for functions to attach themselves to TAB.
  16317. See `org-ctrl-c-ctrl-c-hook' for more information.
  16318. This hook runs after it has been established that not table field motion and
  16319. not visibility should be done because of current context. This is probably
  16320. the place where a package like yasnippets can hook in.")
  16321. (defvar org-tab-before-tab-emulation-hook nil
  16322. "Hook for functions to attach themselves to TAB.
  16323. See `org-ctrl-c-ctrl-c-hook' for more information.
  16324. This hook runs after every other options for TAB have been exhausted, but
  16325. before indentation and \t insertion takes place.")
  16326. (defvar org-metaleft-hook nil
  16327. "Hook for functions attaching themselves to `M-left'.
  16328. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16329. (defvar org-metaright-hook nil
  16330. "Hook for functions attaching themselves to `M-right'.
  16331. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16332. (defvar org-metaup-hook nil
  16333. "Hook for functions attaching themselves to `M-up'.
  16334. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16335. (defvar org-metadown-hook nil
  16336. "Hook for functions attaching themselves to `M-down'.
  16337. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16338. (defvar org-shiftmetaleft-hook nil
  16339. "Hook for functions attaching themselves to `M-S-left'.
  16340. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16341. (defvar org-shiftmetaright-hook nil
  16342. "Hook for functions attaching themselves to `M-S-right'.
  16343. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16344. (defvar org-shiftmetaup-hook nil
  16345. "Hook for functions attaching themselves to `M-S-up'.
  16346. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16347. (defvar org-shiftmetadown-hook nil
  16348. "Hook for functions attaching themselves to `M-S-down'.
  16349. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16350. (defvar org-metareturn-hook nil
  16351. "Hook for functions attaching themselves to `M-RET'.
  16352. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16353. (defvar org-shiftup-hook nil
  16354. "Hook for functions attaching themselves to `S-up'.
  16355. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16356. (defvar org-shiftup-final-hook nil
  16357. "Hook for functions attaching themselves to `S-up'.
  16358. This one runs after all other options except shift-select have been excluded.
  16359. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16360. (defvar org-shiftdown-hook nil
  16361. "Hook for functions attaching themselves to `S-down'.
  16362. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16363. (defvar org-shiftdown-final-hook nil
  16364. "Hook for functions attaching themselves to `S-down'.
  16365. This one runs after all other options except shift-select have been excluded.
  16366. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16367. (defvar org-shiftleft-hook nil
  16368. "Hook for functions attaching themselves to `S-left'.
  16369. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16370. (defvar org-shiftleft-final-hook nil
  16371. "Hook for functions attaching themselves to `S-left'.
  16372. This one runs after all other options except shift-select have been excluded.
  16373. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16374. (defvar org-shiftright-hook nil
  16375. "Hook for functions attaching themselves to `S-right'.
  16376. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16377. (defvar org-shiftright-final-hook nil
  16378. "Hook for functions attaching themselves to `S-right'.
  16379. This one runs after all other options except shift-select have been excluded.
  16380. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16381. (defun org-modifier-cursor-error ()
  16382. "Throw an error, a modified cursor command was applied in wrong context."
  16383. (error "This command is active in special context like tables, headlines or items"))
  16384. (defun org-shiftselect-error ()
  16385. "Throw an error because Shift-Cursor command was applied in wrong context."
  16386. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16387. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16388. (error "This command works only in special context like headlines or timestamps")))
  16389. (defun org-call-for-shift-select (cmd)
  16390. (let ((this-command-keys-shift-translated t))
  16391. (call-interactively cmd)))
  16392. (defun org-shifttab (&optional arg)
  16393. "Global visibility cycling or move to previous table field.
  16394. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16395. on context.
  16396. See the individual commands for more information."
  16397. (interactive "P")
  16398. (cond
  16399. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16400. ((integerp arg)
  16401. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16402. (message "Content view to level: %d" arg)
  16403. (org-content (prefix-numeric-value arg2))
  16404. (setq org-cycle-global-status 'overview)))
  16405. (t (call-interactively 'org-global-cycle))))
  16406. (defun org-shiftmetaleft ()
  16407. "Promote subtree or delete table column.
  16408. Calls `org-promote-subtree', `org-outdent-item',
  16409. or `org-table-delete-column', depending on context.
  16410. See the individual commands for more information."
  16411. (interactive)
  16412. (cond
  16413. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16414. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16415. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16416. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16417. (t (org-modifier-cursor-error))))
  16418. (defun org-shiftmetaright ()
  16419. "Demote subtree or insert table column.
  16420. Calls `org-demote-subtree', `org-indent-item',
  16421. or `org-table-insert-column', depending on context.
  16422. See the individual commands for more information."
  16423. (interactive)
  16424. (cond
  16425. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16426. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16427. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16428. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16429. (t (org-modifier-cursor-error))))
  16430. (defun org-shiftmetaup (&optional arg)
  16431. "Move subtree up or kill table row.
  16432. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16433. `org-move-item-up' depending on context. See the individual commands
  16434. for more information."
  16435. (interactive "P")
  16436. (cond
  16437. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16438. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16439. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16440. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16441. (t (org-modifier-cursor-error))))
  16442. (defun org-shiftmetadown (&optional arg)
  16443. "Move subtree down or insert table row.
  16444. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16445. `org-move-item-down', depending on context. See the individual
  16446. commands for more information."
  16447. (interactive "P")
  16448. (cond
  16449. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16450. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16451. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16452. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16453. (t (org-modifier-cursor-error))))
  16454. (defsubst org-hidden-tree-error ()
  16455. (error
  16456. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16457. (defun org-metaleft (&optional arg)
  16458. "Promote heading or move table column to left.
  16459. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16460. With no specific context, calls the Emacs default `backward-word'.
  16461. See the individual commands for more information."
  16462. (interactive "P")
  16463. (cond
  16464. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16465. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16466. ((org-with-limited-levels
  16467. (or (org-at-heading-p)
  16468. (and (org-region-active-p)
  16469. (save-excursion
  16470. (goto-char (region-beginning))
  16471. (org-at-heading-p)))))
  16472. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16473. (call-interactively 'org-do-promote))
  16474. ;; At an inline task.
  16475. ((org-at-heading-p)
  16476. (call-interactively 'org-inlinetask-promote))
  16477. ((or (org-at-item-p)
  16478. (and (org-region-active-p)
  16479. (save-excursion
  16480. (goto-char (region-beginning))
  16481. (org-at-item-p))))
  16482. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16483. (call-interactively 'org-outdent-item))
  16484. (t (call-interactively 'backward-word))))
  16485. (defun org-metaright (&optional arg)
  16486. "Demote subtree or move table column to right.
  16487. Calls `org-do-demote' or `org-table-move-column', depending on context.
  16488. With no specific context, calls the Emacs default `forward-word'.
  16489. See the individual commands for more information."
  16490. (interactive "P")
  16491. (cond
  16492. ((run-hook-with-args-until-success 'org-metaright-hook))
  16493. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16494. ((org-with-limited-levels
  16495. (or (org-at-heading-p)
  16496. (and (org-region-active-p)
  16497. (save-excursion
  16498. (goto-char (region-beginning))
  16499. (org-at-heading-p)))))
  16500. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16501. (call-interactively 'org-do-demote))
  16502. ;; At an inline task.
  16503. ((org-at-heading-p)
  16504. (call-interactively 'org-inlinetask-demote))
  16505. ((or (org-at-item-p)
  16506. (and (org-region-active-p)
  16507. (save-excursion
  16508. (goto-char (region-beginning))
  16509. (org-at-item-p))))
  16510. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16511. (call-interactively 'org-indent-item))
  16512. (t (call-interactively 'forward-word))))
  16513. (defun org-check-for-hidden (what)
  16514. "Check if there are hidden headlines/items in the current visual line.
  16515. WHAT can be either `headlines' or `items'. If the current line is
  16516. an outline or item heading and it has a folded subtree below it,
  16517. this function returns t, nil otherwise."
  16518. (let ((re (cond
  16519. ((eq what 'headlines) org-outline-regexp-bol)
  16520. ((eq what 'items) (org-item-beginning-re))
  16521. (t (error "This should not happen"))))
  16522. beg end)
  16523. (save-excursion
  16524. (catch 'exit
  16525. (unless (org-region-active-p)
  16526. (setq beg (point-at-bol))
  16527. (beginning-of-line 2)
  16528. (while (and (not (eobp)) ;; this is like `next-line'
  16529. (get-char-property (1- (point)) 'invisible))
  16530. (beginning-of-line 2))
  16531. (setq end (point))
  16532. (goto-char beg)
  16533. (goto-char (point-at-eol))
  16534. (setq end (max end (point)))
  16535. (while (re-search-forward re end t)
  16536. (if (get-char-property (match-beginning 0) 'invisible)
  16537. (throw 'exit t))))
  16538. nil))))
  16539. (defun org-metaup (&optional arg)
  16540. "Move subtree up or move table row up.
  16541. Calls `org-move-subtree-up' or `org-table-move-row' or
  16542. `org-move-item-up', depending on context. See the individual commands
  16543. for more information."
  16544. (interactive "P")
  16545. (cond
  16546. ((run-hook-with-args-until-success 'org-metaup-hook))
  16547. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16548. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16549. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16550. (t (transpose-lines 1) (beginning-of-line -1))))
  16551. (defun org-metadown (&optional arg)
  16552. "Move subtree down or move table row down.
  16553. Calls `org-move-subtree-down' or `org-table-move-row' or
  16554. `org-move-item-down', depending on context. See the individual
  16555. commands for more information."
  16556. (interactive "P")
  16557. (cond
  16558. ((run-hook-with-args-until-success 'org-metadown-hook))
  16559. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16560. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16561. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16562. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16563. (defun org-shiftup (&optional arg)
  16564. "Increase item in timestamp or increase priority of current headline.
  16565. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16566. depending on context. See the individual commands for more information."
  16567. (interactive "P")
  16568. (cond
  16569. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16570. ((and org-support-shift-select (org-region-active-p))
  16571. (org-call-for-shift-select 'previous-line))
  16572. ((org-at-timestamp-p t)
  16573. (call-interactively (if org-edit-timestamp-down-means-later
  16574. 'org-timestamp-down 'org-timestamp-up)))
  16575. ((and (not (eq org-support-shift-select 'always))
  16576. org-enable-priority-commands
  16577. (org-at-heading-p))
  16578. (call-interactively 'org-priority-up))
  16579. ((and (not org-support-shift-select) (org-at-item-p))
  16580. (call-interactively 'org-previous-item))
  16581. ((org-clocktable-try-shift 'up arg))
  16582. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16583. (org-support-shift-select
  16584. (org-call-for-shift-select 'previous-line))
  16585. (t (org-shiftselect-error))))
  16586. (defun org-shiftdown (&optional arg)
  16587. "Decrease item in timestamp or decrease priority of current headline.
  16588. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16589. depending on context. See the individual commands for more information."
  16590. (interactive "P")
  16591. (cond
  16592. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16593. ((and org-support-shift-select (org-region-active-p))
  16594. (org-call-for-shift-select 'next-line))
  16595. ((org-at-timestamp-p t)
  16596. (call-interactively (if org-edit-timestamp-down-means-later
  16597. 'org-timestamp-up 'org-timestamp-down)))
  16598. ((and (not (eq org-support-shift-select 'always))
  16599. org-enable-priority-commands
  16600. (org-at-heading-p))
  16601. (call-interactively 'org-priority-down))
  16602. ((and (not org-support-shift-select) (org-at-item-p))
  16603. (call-interactively 'org-next-item))
  16604. ((org-clocktable-try-shift 'down arg))
  16605. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16606. (org-support-shift-select
  16607. (org-call-for-shift-select 'next-line))
  16608. (t (org-shiftselect-error))))
  16609. (defun org-shiftright (&optional arg)
  16610. "Cycle the thing at point or in the current line, depending on context.
  16611. Depending on context, this does one of the following:
  16612. - switch a timestamp at point one day into the future
  16613. - on a headline, switch to the next TODO keyword.
  16614. - on an item, switch entire list to the next bullet type
  16615. - on a property line, switch to the next allowed value
  16616. - on a clocktable definition line, move time block into the future"
  16617. (interactive "P")
  16618. (cond
  16619. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16620. ((and org-support-shift-select (org-region-active-p))
  16621. (org-call-for-shift-select 'forward-char))
  16622. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16623. ((and (not (eq org-support-shift-select 'always))
  16624. (org-at-heading-p))
  16625. (let ((org-inhibit-logging
  16626. (not org-treat-S-cursor-todo-selection-as-state-change))
  16627. (org-inhibit-blocking
  16628. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16629. (org-call-with-arg 'org-todo 'right)))
  16630. ((or (and org-support-shift-select
  16631. (not (eq org-support-shift-select 'always))
  16632. (org-at-item-bullet-p))
  16633. (and (not org-support-shift-select) (org-at-item-p)))
  16634. (org-call-with-arg 'org-cycle-list-bullet nil))
  16635. ((and (not (eq org-support-shift-select 'always))
  16636. (org-at-property-p))
  16637. (call-interactively 'org-property-next-allowed-value))
  16638. ((org-clocktable-try-shift 'right arg))
  16639. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16640. (org-support-shift-select
  16641. (org-call-for-shift-select 'forward-char))
  16642. (t (org-shiftselect-error))))
  16643. (defun org-shiftleft (&optional arg)
  16644. "Cycle the thing at point or in the current line, depending on context.
  16645. Depending on context, this does one of the following:
  16646. - switch a timestamp at point one day into the past
  16647. - on a headline, switch to the previous TODO keyword.
  16648. - on an item, switch entire list to the previous bullet type
  16649. - on a property line, switch to the previous allowed value
  16650. - on a clocktable definition line, move time block into the past"
  16651. (interactive "P")
  16652. (cond
  16653. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16654. ((and org-support-shift-select (org-region-active-p))
  16655. (org-call-for-shift-select 'backward-char))
  16656. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16657. ((and (not (eq org-support-shift-select 'always))
  16658. (org-at-heading-p))
  16659. (let ((org-inhibit-logging
  16660. (not org-treat-S-cursor-todo-selection-as-state-change))
  16661. (org-inhibit-blocking
  16662. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16663. (org-call-with-arg 'org-todo 'left)))
  16664. ((or (and org-support-shift-select
  16665. (not (eq org-support-shift-select 'always))
  16666. (org-at-item-bullet-p))
  16667. (and (not org-support-shift-select) (org-at-item-p)))
  16668. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16669. ((and (not (eq org-support-shift-select 'always))
  16670. (org-at-property-p))
  16671. (call-interactively 'org-property-previous-allowed-value))
  16672. ((org-clocktable-try-shift 'left arg))
  16673. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16674. (org-support-shift-select
  16675. (org-call-for-shift-select 'backward-char))
  16676. (t (org-shiftselect-error))))
  16677. (defun org-shiftcontrolright ()
  16678. "Switch to next TODO set."
  16679. (interactive)
  16680. (cond
  16681. ((and org-support-shift-select (org-region-active-p))
  16682. (org-call-for-shift-select 'forward-word))
  16683. ((and (not (eq org-support-shift-select 'always))
  16684. (org-at-heading-p))
  16685. (org-call-with-arg 'org-todo 'nextset))
  16686. (org-support-shift-select
  16687. (org-call-for-shift-select 'forward-word))
  16688. (t (org-shiftselect-error))))
  16689. (defun org-shiftcontrolleft ()
  16690. "Switch to previous TODO set."
  16691. (interactive)
  16692. (cond
  16693. ((and org-support-shift-select (org-region-active-p))
  16694. (org-call-for-shift-select 'backward-word))
  16695. ((and (not (eq org-support-shift-select 'always))
  16696. (org-at-heading-p))
  16697. (org-call-with-arg 'org-todo 'previousset))
  16698. (org-support-shift-select
  16699. (org-call-for-shift-select 'backward-word))
  16700. (t (org-shiftselect-error))))
  16701. (defun org-shiftcontrolup ()
  16702. "Change timestamps synchronously up in CLOCK log lines."
  16703. (interactive)
  16704. (cond ((and (not org-support-shift-select)
  16705. (org-at-clock-log-p)
  16706. (org-at-timestamp-p t))
  16707. (org-clock-timestamps-up))
  16708. (t (org-shiftselect-error))))
  16709. (defun org-shiftcontroldown ()
  16710. "Change timestamps synchronously down in CLOCK log lines."
  16711. (interactive)
  16712. (cond ((and (not org-support-shift-select)
  16713. (org-at-clock-log-p)
  16714. (org-at-timestamp-p t))
  16715. (org-clock-timestamps-down))
  16716. (t (org-shiftselect-error))))
  16717. (defun org-ctrl-c-ret ()
  16718. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16719. (interactive)
  16720. (cond
  16721. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16722. (t (call-interactively 'org-insert-heading))))
  16723. (defun org-find-visible ()
  16724. (let ((s (point)))
  16725. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16726. (get-char-property s 'invisible)))
  16727. s))
  16728. (defun org-find-invisible ()
  16729. (let ((s (point)))
  16730. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16731. (not (get-char-property s 'invisible))))
  16732. s))
  16733. (defun org-copy-visible (beg end)
  16734. "Copy the visible parts of the region."
  16735. (interactive "r")
  16736. (let (snippets s)
  16737. (save-excursion
  16738. (save-restriction
  16739. (narrow-to-region beg end)
  16740. (setq s (goto-char (point-min)))
  16741. (while (not (= (point) (point-max)))
  16742. (goto-char (org-find-invisible))
  16743. (push (buffer-substring s (point)) snippets)
  16744. (setq s (goto-char (org-find-visible))))))
  16745. (kill-new (apply 'concat (nreverse snippets)))))
  16746. (defun org-copy-special ()
  16747. "Copy region in table or copy current subtree.
  16748. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16749. See the individual commands for more information."
  16750. (interactive)
  16751. (call-interactively
  16752. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16753. (defun org-cut-special ()
  16754. "Cut region in table or cut current subtree.
  16755. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16756. See the individual commands for more information."
  16757. (interactive)
  16758. (call-interactively
  16759. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16760. (defun org-paste-special (arg)
  16761. "Paste rectangular region into table, or past subtree relative to level.
  16762. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16763. See the individual commands for more information."
  16764. (interactive "P")
  16765. (if (org-at-table-p)
  16766. (org-table-paste-rectangle)
  16767. (org-paste-subtree arg)))
  16768. (defun org-edit-special (&optional arg)
  16769. "Call a special editor for the stuff at point.
  16770. When at a table, call the formula editor with `org-table-edit-formulas'.
  16771. When at the first line of an src example, call `org-edit-src-code'.
  16772. When in an #+include line, visit the include file. Otherwise call
  16773. `ffap' to visit the file at point."
  16774. (interactive)
  16775. ;; possibly prep session before editing source
  16776. (when arg
  16777. (let* ((info (org-babel-get-src-block-info))
  16778. (lang (nth 0 info))
  16779. (params (nth 2 info))
  16780. (session (cdr (assoc :session params))))
  16781. (when (and info session) ;; we are in a source-code block with a session
  16782. (funcall
  16783. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16784. (cond ;; proceed with `org-edit-special'
  16785. ((save-excursion
  16786. (beginning-of-line 1)
  16787. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16788. (find-file (org-trim (match-string 1))))
  16789. ((org-edit-src-code))
  16790. ((org-edit-fixed-width-region))
  16791. ((org-at-table.el-p)
  16792. (org-edit-src-code))
  16793. ((or (org-at-table-p)
  16794. (save-excursion
  16795. (beginning-of-line 1)
  16796. (looking-at "[ \t]*#\\+TBLFM:")))
  16797. (call-interactively 'org-table-edit-formulas))
  16798. (t (call-interactively 'ffap))))
  16799. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16800. (defun org-ctrl-c-ctrl-c (&optional arg)
  16801. "Set tags in headline, or update according to changed information at point.
  16802. This command does many different things, depending on context:
  16803. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16804. this is what we do.
  16805. - If the cursor is on a statistics cookie, update it.
  16806. - If the cursor is in a headline, prompt for tags and insert them
  16807. into the current line, aligned to `org-tags-column'. When called
  16808. with prefix arg, realign all tags in the current buffer.
  16809. - If the cursor is in one of the special #+KEYWORD lines, this
  16810. triggers scanning the buffer for these lines and updating the
  16811. information.
  16812. - If the cursor is inside a table, realign the table. This command
  16813. works even if the automatic table editor has been turned off.
  16814. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16815. the entire table.
  16816. - If the cursor is at a footnote reference or definition, jump to
  16817. the corresponding definition or references, respectively.
  16818. - If the cursor is a the beginning of a dynamic block, update it.
  16819. - If the current buffer is a capture buffer, close note and file it.
  16820. - If the cursor is on a <<<target>>>, update radio targets and
  16821. corresponding links in this buffer.
  16822. - If the cursor is on a numbered item in a plain list, renumber the
  16823. ordered list.
  16824. - If the cursor is on a checkbox, toggle it.
  16825. - If the cursor is on a code block, evaluate it. The variable
  16826. `org-confirm-babel-evaluate' can be used to control prompting
  16827. before code block evaluation, by default every code block
  16828. evaluation requires confirmation. Code block evaluation can be
  16829. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16830. (interactive "P")
  16831. (let ((org-enable-table-editor t))
  16832. (cond
  16833. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16834. org-occur-highlights
  16835. org-latex-fragment-image-overlays)
  16836. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16837. (org-remove-occur-highlights)
  16838. (org-remove-latex-fragment-image-overlays)
  16839. (message "Temporary highlights/overlays removed from current buffer"))
  16840. ((and (local-variable-p 'org-finish-function (current-buffer))
  16841. (fboundp org-finish-function))
  16842. (funcall org-finish-function))
  16843. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16844. ((org-in-regexp org-ts-regexp-both)
  16845. (org-timestamp-change 0 'day))
  16846. ((or (looking-at org-property-start-re)
  16847. (org-at-property-p))
  16848. (call-interactively 'org-property-action))
  16849. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  16850. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16851. (or (org-at-heading-p) (org-at-item-p)))
  16852. (call-interactively 'org-update-statistics-cookies))
  16853. ((org-at-heading-p) (call-interactively 'org-set-tags))
  16854. ((org-at-table.el-p)
  16855. (message "Use C-c ' to edit table.el tables"))
  16856. ((org-at-table-p)
  16857. (org-table-maybe-eval-formula)
  16858. (if arg
  16859. (call-interactively 'org-table-recalculate)
  16860. (org-table-maybe-recalculate-line))
  16861. (call-interactively 'org-table-align)
  16862. (orgtbl-send-table 'maybe))
  16863. ((or (org-footnote-at-reference-p)
  16864. (org-footnote-at-definition-p))
  16865. (call-interactively 'org-footnote-action))
  16866. ((org-at-item-checkbox-p)
  16867. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16868. ;; list only if at top item.
  16869. (let* ((cbox (match-string 1))
  16870. (struct (org-list-struct))
  16871. (old-struct (copy-tree struct))
  16872. (parents (org-list-parents-alist struct))
  16873. (orderedp (org-entry-get nil "ORDERED"))
  16874. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16875. block-item)
  16876. ;; Use a light version of `org-toggle-checkbox' to avoid
  16877. ;; computing list structure twice.
  16878. (let ((new-box (cond
  16879. ((equal arg '(16)) "[-]")
  16880. ((equal arg '(4)) nil)
  16881. ((equal "[X]" cbox) "[ ]")
  16882. (t "[X]"))))
  16883. (if (and firstp arg)
  16884. ;; If at first item of sub-list, remove check-box from
  16885. ;; every item at the same level.
  16886. (mapc
  16887. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  16888. (org-list-get-all-items
  16889. (point-at-bol) struct (org-list-prevs-alist struct)))
  16890. (org-list-set-checkbox (point-at-bol) struct new-box)))
  16891. ;; Replicate `org-list-write-struct', while grabbing a return
  16892. ;; value from `org-list-struct-fix-box'.
  16893. (org-list-struct-fix-ind struct parents 2)
  16894. (org-list-struct-fix-item-end struct)
  16895. (let ((prevs (org-list-prevs-alist struct)))
  16896. (org-list-struct-fix-bul struct prevs)
  16897. (org-list-struct-fix-ind struct parents)
  16898. (setq block-item
  16899. (org-list-struct-fix-box struct parents prevs orderedp)))
  16900. (org-list-struct-apply-struct struct old-struct)
  16901. (org-update-checkbox-count-maybe)
  16902. (when block-item
  16903. (message
  16904. "Checkboxes were removed due to unchecked box at line %d"
  16905. (org-current-line block-item)))
  16906. (when firstp (org-list-send-list 'maybe))))
  16907. ((org-at-item-p)
  16908. ;; Cursor at an item: repair list. Do checkbox related actions
  16909. ;; only if function was called with an argument. Send list only
  16910. ;; if at top item.
  16911. (let* ((struct (org-list-struct))
  16912. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16913. old-struct)
  16914. (when arg
  16915. (setq old-struct (copy-tree struct))
  16916. (if firstp
  16917. ;; If at first item of sub-list, add check-box to every
  16918. ;; item at the same level.
  16919. (mapc
  16920. (lambda (pos)
  16921. (unless (org-list-get-checkbox pos struct)
  16922. (org-list-set-checkbox pos struct "[ ]")))
  16923. (org-list-get-all-items
  16924. (point-at-bol) struct (org-list-prevs-alist struct)))
  16925. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  16926. (org-list-write-struct
  16927. struct (org-list-parents-alist struct) old-struct)
  16928. (when arg (org-update-checkbox-count-maybe))
  16929. (when firstp (org-list-send-list 'maybe))))
  16930. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16931. ;; Dynamic block
  16932. (beginning-of-line 1)
  16933. (save-excursion (org-update-dblock)))
  16934. ((save-excursion
  16935. (beginning-of-line 1)
  16936. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16937. (cond
  16938. ((equal (match-string 1) "TBLFM")
  16939. ;; Recalculate the table before this line
  16940. (save-excursion
  16941. (beginning-of-line 1)
  16942. (skip-chars-backward " \r\n\t")
  16943. (if (org-at-table-p)
  16944. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16945. (t
  16946. (let ((org-inhibit-startup-visibility-stuff t)
  16947. (org-startup-align-all-tables nil))
  16948. (when (boundp 'org-table-coordinate-overlays)
  16949. (mapc 'delete-overlay org-table-coordinate-overlays)
  16950. (setq org-table-coordinate-overlays nil))
  16951. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16952. (message "Local setup has been refreshed"))))
  16953. ((org-clock-update-time-maybe))
  16954. (t
  16955. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  16956. (error "C-c C-c can do nothing useful at this location"))))))
  16957. (defun org-mode-restart ()
  16958. "Restart Org-mode, to scan again for special lines.
  16959. Also updates the keyword regular expressions."
  16960. (interactive)
  16961. (org-mode)
  16962. (message "Org-mode restarted"))
  16963. (defun org-kill-note-or-show-branches ()
  16964. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16965. (interactive)
  16966. (if (not org-finish-function)
  16967. (progn
  16968. (hide-subtree)
  16969. (call-interactively 'show-branches))
  16970. (let ((org-note-abort t))
  16971. (funcall org-finish-function))))
  16972. (defun org-return (&optional indent)
  16973. "Goto next table row or insert a newline.
  16974. Calls `org-table-next-row' or `newline', depending on context.
  16975. See the individual commands for more information."
  16976. (interactive)
  16977. (cond
  16978. ((bobp) (if indent (newline-and-indent) (newline)))
  16979. ((org-at-table-p)
  16980. (org-table-justify-field-maybe)
  16981. (call-interactively 'org-table-next-row))
  16982. ;; when `newline-and-indent' is called within a list, make sure
  16983. ;; text moved stays inside the item.
  16984. ((and (org-in-item-p) indent)
  16985. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16986. (progn
  16987. (save-match-data (newline))
  16988. (org-indent-line-to (length (match-string 0))))
  16989. (let ((ind (org-get-indentation)))
  16990. (newline)
  16991. (if (org-looking-back org-list-end-re)
  16992. (org-indent-line-function)
  16993. (org-indent-line-to ind)))))
  16994. ((and org-return-follows-link
  16995. (let ((tprop (get-text-property (point) 'face)))
  16996. (or (eq tprop 'org-link)
  16997. (and (listp tprop) (memq 'org-link tprop)))))
  16998. (call-interactively 'org-open-at-point))
  16999. ((and (org-at-heading-p)
  17000. (looking-at
  17001. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17002. (org-show-entry)
  17003. (end-of-line 1)
  17004. (newline))
  17005. (t (if indent (newline-and-indent) (newline)))))
  17006. (defun org-return-indent ()
  17007. "Goto next table row or insert a newline and indent.
  17008. Calls `org-table-next-row' or `newline-and-indent', depending on
  17009. context. See the individual commands for more information."
  17010. (interactive)
  17011. (org-return t))
  17012. (defun org-ctrl-c-star ()
  17013. "Compute table, or change heading status of lines.
  17014. Calls `org-table-recalculate' or `org-toggle-heading',
  17015. depending on context."
  17016. (interactive)
  17017. (cond
  17018. ((org-at-table-p)
  17019. (call-interactively 'org-table-recalculate))
  17020. (t
  17021. ;; Convert all lines in region to list items
  17022. (call-interactively 'org-toggle-heading))))
  17023. (defun org-ctrl-c-minus ()
  17024. "Insert separator line in table or modify bullet status of line.
  17025. Also turns a plain line or a region of lines into list items.
  17026. Calls `org-table-insert-hline', `org-toggle-item', or
  17027. `org-cycle-list-bullet', depending on context."
  17028. (interactive)
  17029. (cond
  17030. ((org-at-table-p)
  17031. (call-interactively 'org-table-insert-hline))
  17032. ((org-region-active-p)
  17033. (call-interactively 'org-toggle-item))
  17034. ((org-in-item-p)
  17035. (call-interactively 'org-cycle-list-bullet))
  17036. (t
  17037. (call-interactively 'org-toggle-item))))
  17038. (defun org-toggle-item (arg)
  17039. "Convert headings or normal lines to items, items to normal lines.
  17040. If there is no active region, only the current line is considered.
  17041. If the first non blank line in the region is an headline, convert
  17042. all headlines to items, shifting text accordingly.
  17043. If it is an item, convert all items to normal lines.
  17044. If it is normal text, change region into an item. With a prefix
  17045. argument ARG, change each line in region into an item."
  17046. (interactive "P")
  17047. (let ((shift-text
  17048. (function
  17049. ;; Shift text in current section to IND, from point to END.
  17050. ;; The function leaves point to END line.
  17051. (lambda (ind end)
  17052. (let ((min-i 1000) (end (copy-marker end)))
  17053. ;; First determine the minimum indentation (MIN-I) of
  17054. ;; the text.
  17055. (save-excursion
  17056. (catch 'exit
  17057. (while (< (point) end)
  17058. (let ((i (org-get-indentation)))
  17059. (cond
  17060. ;; Skip blank lines and inline tasks.
  17061. ((looking-at "^[ \t]*$"))
  17062. ((looking-at org-outline-regexp-bol))
  17063. ;; We can't find less than 0 indentation.
  17064. ((zerop i) (throw 'exit (setq min-i 0)))
  17065. ((< i min-i) (setq min-i i))))
  17066. (forward-line))))
  17067. ;; Then indent each line so that a line indented to
  17068. ;; MIN-I becomes indented to IND. Ignore blank lines
  17069. ;; and inline tasks in the process.
  17070. (let ((delta (- ind min-i)))
  17071. (while (< (point) end)
  17072. (unless (or (looking-at "^[ \t]*$")
  17073. (looking-at org-outline-regexp-bol))
  17074. (org-indent-line-to (+ (org-get-indentation) delta)))
  17075. (forward-line)))))))
  17076. (skip-blanks
  17077. (function
  17078. ;; Return beginning of first non-blank line, starting from
  17079. ;; line at POS.
  17080. (lambda (pos)
  17081. (save-excursion
  17082. (goto-char pos)
  17083. (skip-chars-forward " \r\t\n")
  17084. (point-at-bol)))))
  17085. beg end)
  17086. ;; Determine boundaries of changes.
  17087. (if (org-region-active-p)
  17088. (setq beg (funcall skip-blanks (region-beginning))
  17089. end (copy-marker (region-end)))
  17090. (setq beg (funcall skip-blanks (point-at-bol))
  17091. end (copy-marker (point-at-eol))))
  17092. ;; Depending on the starting line, choose an action on the text
  17093. ;; between BEG and END.
  17094. (org-with-limited-levels
  17095. (save-excursion
  17096. (goto-char beg)
  17097. (cond
  17098. ;; Case 1. Start at an item: de-itemize. Note that it only
  17099. ;; happens when a region is active: `org-ctrl-c-minus'
  17100. ;; would call `org-cycle-list-bullet' otherwise.
  17101. ((org-at-item-p)
  17102. (while (< (point) end)
  17103. (when (org-at-item-p)
  17104. (skip-chars-forward " \t")
  17105. (delete-region (point) (match-end 0)))
  17106. (forward-line)))
  17107. ;; Case 2. Start at an heading: convert to items.
  17108. ((org-at-heading-p)
  17109. (let* ((bul (org-list-bullet-string "-"))
  17110. (bul-len (length bul))
  17111. ;; Indentation of the first heading. It should be
  17112. ;; relative to the indentation of its parent, if any.
  17113. (start-ind (save-excursion
  17114. (cond
  17115. ((not org-adapt-indentation) 0)
  17116. ((not (outline-previous-heading)) 0)
  17117. (t (length (match-string 0))))))
  17118. ;; Level of first heading. Further headings will be
  17119. ;; compared to it to determine hierarchy in the list.
  17120. (ref-level (org-reduced-level (org-outline-level))))
  17121. (while (< (point) end)
  17122. (let* ((level (org-reduced-level (org-outline-level)))
  17123. (delta (max 0 (- level ref-level))))
  17124. ;; If current headline is less indented than the first
  17125. ;; one, set it as reference, in order to preserve
  17126. ;; subtrees.
  17127. (when (< level ref-level) (setq ref-level level))
  17128. (replace-match bul t t)
  17129. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17130. ;; Ensure all text down to END (or SECTION-END) belongs
  17131. ;; to the newly created item.
  17132. (let ((section-end (save-excursion
  17133. (or (outline-next-heading) (point)))))
  17134. (forward-line)
  17135. (funcall shift-text
  17136. (+ start-ind (* (1+ delta) bul-len))
  17137. (min end section-end)))))))
  17138. ;; Case 3. Normal line with ARG: turn each non-item line into
  17139. ;; an item.
  17140. (arg
  17141. (while (< (point) end)
  17142. (unless (or (org-at-heading-p) (org-at-item-p))
  17143. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17144. (replace-match
  17145. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17146. (forward-line)))
  17147. ;; Case 4. Normal line without ARG: make the first line of
  17148. ;; region an item, and shift indentation of others
  17149. ;; lines to set them as item's body.
  17150. (t (let* ((bul (org-list-bullet-string "-"))
  17151. (bul-len (length bul))
  17152. (ref-ind (org-get-indentation)))
  17153. (skip-chars-forward " \t")
  17154. (insert bul)
  17155. (forward-line)
  17156. (while (< (point) end)
  17157. ;; Ensure that lines less indented than first one
  17158. ;; still get included in item body.
  17159. (funcall shift-text
  17160. (+ ref-ind bul-len)
  17161. (min end (save-excursion (or (outline-next-heading)
  17162. (point)))))
  17163. (forward-line)))))))))
  17164. (defun org-toggle-heading (&optional nstars)
  17165. "Convert headings to normal text, or items or text to headings.
  17166. If there is no active region, only the current line is considered.
  17167. If the first non blank line is an headline, remove the stars from
  17168. all headlines in the region.
  17169. If it is a plain list item, turn all plain list items into headings.
  17170. If it is a normal line, turn each and every normal line (i.e. not
  17171. an heading or an item) in the region into a heading.
  17172. When converting a line into a heading, the number of stars is chosen
  17173. such that the lines become children of the current entry. However,
  17174. when a prefix argument is given, its value determines the number of
  17175. stars to add."
  17176. (interactive "P")
  17177. (let ((skip-blanks
  17178. (function
  17179. ;; Return beginning of first non-blank line, starting from
  17180. ;; line at POS.
  17181. (lambda (pos)
  17182. (save-excursion
  17183. (goto-char pos)
  17184. (skip-chars-forward " \r\t\n")
  17185. (point-at-bol)))))
  17186. beg end)
  17187. ;; Determine boundaries of changes. If region ends at a bol, do
  17188. ;; not consider the last line to be in the region.
  17189. (if (org-region-active-p)
  17190. (setq beg (funcall skip-blanks (region-beginning))
  17191. end (copy-marker (save-excursion
  17192. (goto-char (region-end))
  17193. (if (bolp) (point) (point-at-eol)))))
  17194. (setq beg (funcall skip-blanks (point-at-bol))
  17195. end (copy-marker (point-at-eol))))
  17196. ;; Ensure inline tasks don't count as headings.
  17197. (org-with-limited-levels
  17198. (save-excursion
  17199. (goto-char beg)
  17200. (cond
  17201. ;; Case 1. Started at an heading: de-star headings.
  17202. ((org-at-heading-p)
  17203. (while (< (point) end)
  17204. (when (org-at-heading-p t)
  17205. (looking-at org-outline-regexp) (replace-match ""))
  17206. (forward-line)))
  17207. ;; Case 2. Started at an item: change items into headlines.
  17208. ;; One star will be added by `org-list-to-subtree'.
  17209. ((org-at-item-p)
  17210. (let* ((stars (make-string
  17211. (if nstars
  17212. ;; subtract the star that will be added again by
  17213. ;; `org-list-to-subtree'
  17214. (1- (prefix-numeric-value current-prefix-arg))
  17215. (or (org-current-level) 0))
  17216. ?*))
  17217. (add-stars
  17218. (cond (nstars "") ; stars from prefix only
  17219. ((equal stars "") "") ; before first heading
  17220. (org-odd-levels-only "*") ; inside heading, odd
  17221. (t "")))) ; inside heading, oddeven
  17222. (while (< (point) end)
  17223. (when (org-at-item-p)
  17224. ;; Pay attention to cases when region ends before list.
  17225. (let* ((struct (org-list-struct))
  17226. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17227. (save-restriction
  17228. (narrow-to-region (point) list-end)
  17229. (insert
  17230. (org-list-to-subtree
  17231. (org-list-parse-list t)
  17232. '(:istart (concat stars add-stars (funcall get-stars depth))
  17233. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17234. (forward-line))))
  17235. ;; Case 3. Started at normal text: make every line an heading,
  17236. ;; skipping headlines and items.
  17237. (t (let* ((stars (make-string
  17238. (if nstars
  17239. (prefix-numeric-value current-prefix-arg)
  17240. (or (org-current-level) 0))
  17241. ?*))
  17242. (add-stars
  17243. (cond (nstars "") ; stars from prefix only
  17244. ((equal stars "") "*") ; before first heading
  17245. (org-odd-levels-only "**") ; inside heading, odd
  17246. (t "*"))) ; inside heading, oddeven
  17247. (rpl (concat stars add-stars " ")))
  17248. (while (< (point) end)
  17249. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17250. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17251. (replace-match (concat rpl (match-string 2))))
  17252. (forward-line)))))))))
  17253. (defun org-meta-return (&optional arg)
  17254. "Insert a new heading or wrap a region in a table.
  17255. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17256. See the individual commands for more information."
  17257. (interactive "P")
  17258. (cond
  17259. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17260. ((org-at-table-p)
  17261. (call-interactively 'org-table-wrap-region))
  17262. (t (call-interactively 'org-insert-heading))))
  17263. ;;; Menu entries
  17264. ;; Define the Org-mode menus
  17265. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17266. '("Tbl"
  17267. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17268. ["Next Field" org-cycle (org-at-table-p)]
  17269. ["Previous Field" org-shifttab (org-at-table-p)]
  17270. ["Next Row" org-return (org-at-table-p)]
  17271. "--"
  17272. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17273. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17274. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17275. "--"
  17276. ("Column"
  17277. ["Move Column Left" org-metaleft (org-at-table-p)]
  17278. ["Move Column Right" org-metaright (org-at-table-p)]
  17279. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17280. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17281. ("Row"
  17282. ["Move Row Up" org-metaup (org-at-table-p)]
  17283. ["Move Row Down" org-metadown (org-at-table-p)]
  17284. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17285. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17286. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17287. "--"
  17288. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17289. ("Rectangle"
  17290. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17291. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17292. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17293. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17294. "--"
  17295. ("Calculate"
  17296. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17297. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17298. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17299. "--"
  17300. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17301. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17302. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17303. "--"
  17304. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17305. "--"
  17306. ["Sum Column/Rectangle" org-table-sum
  17307. (or (org-at-table-p) (org-region-active-p))]
  17308. ["Which Column?" org-table-current-column (org-at-table-p)])
  17309. ["Debug Formulas"
  17310. org-table-toggle-formula-debugger
  17311. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17312. ["Show Col/Row Numbers"
  17313. org-table-toggle-coordinate-overlays
  17314. :style toggle
  17315. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17316. "--"
  17317. ["Create" org-table-create (and (not (org-at-table-p))
  17318. org-enable-table-editor)]
  17319. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17320. ["Import from File" org-table-import (not (org-at-table-p))]
  17321. ["Export to File" org-table-export (org-at-table-p)]
  17322. "--"
  17323. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17324. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17325. '("Org"
  17326. ("Show/Hide"
  17327. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17328. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17329. ["Sparse Tree..." org-sparse-tree t]
  17330. ["Reveal Context" org-reveal t]
  17331. ["Show All" show-all t]
  17332. "--"
  17333. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17334. "--"
  17335. ["New Heading" org-insert-heading t]
  17336. ("Navigate Headings"
  17337. ["Up" outline-up-heading t]
  17338. ["Next" outline-next-visible-heading t]
  17339. ["Previous" outline-previous-visible-heading t]
  17340. ["Next Same Level" outline-forward-same-level t]
  17341. ["Previous Same Level" outline-backward-same-level t]
  17342. "--"
  17343. ["Jump" org-goto t])
  17344. ("Edit Structure"
  17345. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17346. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17347. "--"
  17348. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17349. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17350. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17351. "--"
  17352. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17353. "--"
  17354. ["Copy visible text" org-copy-visible t]
  17355. "--"
  17356. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17357. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17358. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17359. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17360. "--"
  17361. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17362. "--"
  17363. ["Convert to odd levels" org-convert-to-odd-levels t]
  17364. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17365. ("Editing"
  17366. ["Emphasis..." org-emphasize t]
  17367. ["Edit Source Example" org-edit-special t]
  17368. "--"
  17369. ["Footnote new/jump" org-footnote-action t]
  17370. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17371. ("Archive"
  17372. ["Archive (default method)" org-archive-subtree-default t]
  17373. "--"
  17374. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17375. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17376. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17377. )
  17378. "--"
  17379. ("Hyperlinks"
  17380. ["Store Link (Global)" org-store-link t]
  17381. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17382. ["Insert Link" org-insert-link t]
  17383. ["Follow Link" org-open-at-point t]
  17384. "--"
  17385. ["Next link" org-next-link t]
  17386. ["Previous link" org-previous-link t]
  17387. "--"
  17388. ["Descriptive Links"
  17389. org-toggle-link-display
  17390. :style radio
  17391. :selected org-descriptive-links
  17392. ]
  17393. ["Literal Links"
  17394. org-toggle-link-display
  17395. :style radio
  17396. :selected (not org-descriptive-links)])
  17397. "--"
  17398. ("TODO Lists"
  17399. ["TODO/DONE/-" org-todo t]
  17400. ("Select keyword"
  17401. ["Next keyword" org-shiftright (org-at-heading-p)]
  17402. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17403. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17404. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17405. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17406. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17407. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17408. "--"
  17409. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17410. :selected org-enforce-todo-dependencies :style toggle :active t]
  17411. "Settings for tree at point"
  17412. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17413. :selected (org-entry-get nil "ORDERED")
  17414. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17415. ["Do Children parallel" org-toggle-ordered-property :style radio
  17416. :selected (not (org-entry-get nil "ORDERED"))
  17417. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17418. "--"
  17419. ["Set Priority" org-priority t]
  17420. ["Priority Up" org-shiftup t]
  17421. ["Priority Down" org-shiftdown t]
  17422. "--"
  17423. ["Get news from all feeds" org-feed-update-all t]
  17424. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17425. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17426. ("TAGS and Properties"
  17427. ["Set Tags" org-set-tags-command t]
  17428. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17429. "--"
  17430. ["Set property" org-set-property t]
  17431. ["Column view of properties" org-columns t]
  17432. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17433. ("Dates and Scheduling"
  17434. ["Timestamp" org-time-stamp t]
  17435. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17436. ("Change Date"
  17437. ["1 Day Later" org-shiftright t]
  17438. ["1 Day Earlier" org-shiftleft t]
  17439. ["1 ... Later" org-shiftup t]
  17440. ["1 ... Earlier" org-shiftdown t])
  17441. ["Compute Time Range" org-evaluate-time-range t]
  17442. ["Schedule Item" org-schedule t]
  17443. ["Deadline" org-deadline t]
  17444. "--"
  17445. ["Custom time format" org-toggle-time-stamp-overlays
  17446. :style radio :selected org-display-custom-times]
  17447. "--"
  17448. ["Goto Calendar" org-goto-calendar t]
  17449. ["Date from Calendar" org-date-from-calendar t]
  17450. "--"
  17451. ["Start/Restart Timer" org-timer-start t]
  17452. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17453. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17454. ["Insert Timer String" org-timer t]
  17455. ["Insert Timer Item" org-timer-item t])
  17456. ("Logging work"
  17457. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17458. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17459. ["Clock out" org-clock-out t]
  17460. ["Clock cancel" org-clock-cancel t]
  17461. "--"
  17462. ["Mark as default task" org-clock-mark-default-task t]
  17463. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17464. ["Goto running clock" org-clock-goto t]
  17465. "--"
  17466. ["Display times" org-clock-display t]
  17467. ["Create clock table" org-clock-report t]
  17468. "--"
  17469. ["Record DONE time"
  17470. (progn (setq org-log-done (not org-log-done))
  17471. (message "Switching to %s will %s record a timestamp"
  17472. (car org-done-keywords)
  17473. (if org-log-done "automatically" "not")))
  17474. :style toggle :selected org-log-done])
  17475. "--"
  17476. ["Agenda Command..." org-agenda t]
  17477. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17478. ("File List for Agenda")
  17479. ("Special views current file"
  17480. ["TODO Tree" org-show-todo-tree t]
  17481. ["Check Deadlines" org-check-deadlines t]
  17482. ["Timeline" org-timeline t]
  17483. ["Tags/Property tree" org-match-sparse-tree t])
  17484. "--"
  17485. ["Export/Publish..." org-export t]
  17486. ("LaTeX"
  17487. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17488. :selected org-cdlatex-mode]
  17489. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17490. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17491. ["Modify math symbol" org-cdlatex-math-modify
  17492. (org-inside-LaTeX-fragment-p)]
  17493. ["Insert citation" org-reftex-citation t]
  17494. "--"
  17495. ["Template for BEAMER" org-insert-beamer-options-template t])
  17496. "--"
  17497. ("MobileOrg"
  17498. ["Push Files and Views" org-mobile-push t]
  17499. ["Get Captured and Flagged" org-mobile-pull t]
  17500. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17501. "--"
  17502. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17503. "--"
  17504. ("Documentation"
  17505. ["Show Version" org-version t]
  17506. ["Info Documentation" org-info t])
  17507. ("Customize"
  17508. ["Browse Org Group" org-customize t]
  17509. "--"
  17510. ["Expand This Menu" org-create-customize-menu
  17511. (fboundp 'customize-menu-create)])
  17512. ["Send bug report" org-submit-bug-report t]
  17513. "--"
  17514. ("Refresh/Reload"
  17515. ["Refresh setup current buffer" org-mode-restart t]
  17516. ["Reload Org (after update)" org-reload t]
  17517. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17518. ))
  17519. (defun org-info (&optional node)
  17520. "Read documentation for Org-mode in the info system.
  17521. With optional NODE, go directly to that node."
  17522. (interactive)
  17523. (info (format "(org)%s" (or node ""))))
  17524. ;;;###autoload
  17525. (defun org-submit-bug-report ()
  17526. "Submit a bug report on Org-mode via mail.
  17527. Don't hesitate to report any problems or inaccurate documentation.
  17528. If you don't have setup sending mail from (X)Emacs, please copy the
  17529. output buffer into your mail program, as it gives us important
  17530. information about your Org-mode version and configuration."
  17531. (interactive)
  17532. (require 'reporter)
  17533. (org-load-modules-maybe)
  17534. (org-require-autoloaded-modules)
  17535. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17536. (reporter-submit-bug-report
  17537. "emacs-orgmode@gnu.org"
  17538. (org-version)
  17539. (let (list)
  17540. (save-window-excursion
  17541. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17542. (delete-other-windows)
  17543. (erase-buffer)
  17544. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17545. We would like to add your full Org-mode and Outline configuration to the
  17546. bug report. This greatly simplifies the work of the maintainer and
  17547. other experts on the mailing list.
  17548. HOWEVER, some variables you have customized may contain private
  17549. information. The names of customers, colleagues, or friends, might
  17550. appear in the form of file names, tags, todo states, or search strings.
  17551. If you answer yes to the prompt, you might want to check and remove
  17552. such private information before sending the email.")
  17553. (add-text-properties (point-min) (point-max) '(face org-warning))
  17554. (when (yes-or-no-p "Include your Org-mode configuration ")
  17555. (mapatoms
  17556. (lambda (v)
  17557. (and (boundp v)
  17558. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17559. (or (and (symbol-value v)
  17560. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17561. (and
  17562. (get v 'custom-type) (get v 'standard-value)
  17563. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17564. (push v list)))))
  17565. (kill-buffer (get-buffer "*Warn about privacy*"))
  17566. list))
  17567. nil nil
  17568. "Remember to cover the basics, that is, what you expected to happen and
  17569. what in fact did happen. You don't know how to make a good report? See
  17570. http://orgmode.org/manual/Feedback.html#Feedback
  17571. Your bug report will be posted to the Org-mode mailing list.
  17572. ------------------------------------------------------------------------")
  17573. (save-excursion
  17574. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17575. (replace-match "\\1Bug: \\3 [\\2]")))))
  17576. (defun org-install-agenda-files-menu ()
  17577. (let ((bl (buffer-list)))
  17578. (save-excursion
  17579. (while bl
  17580. (set-buffer (pop bl))
  17581. (if (eq major-mode 'org-mode) (setq bl nil)))
  17582. (when (eq major-mode 'org-mode)
  17583. (easy-menu-change
  17584. '("Org") "File List for Agenda"
  17585. (append
  17586. (list
  17587. ["Edit File List" (org-edit-agenda-file-list) t]
  17588. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17589. ["Remove Current File from List" org-remove-file t]
  17590. ["Cycle through agenda files" org-cycle-agenda-files t]
  17591. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17592. "--")
  17593. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17594. ;;;; Documentation
  17595. ;;;###autoload
  17596. (defun org-require-autoloaded-modules ()
  17597. (interactive)
  17598. (mapc 'require
  17599. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17600. org-docbook org-exp org-html org-icalendar
  17601. org-id org-latex
  17602. org-publish org-remember org-table
  17603. org-timer org-xoxo)))
  17604. ;;;###autoload
  17605. (defun org-reload (&optional uncompiled)
  17606. "Reload all org lisp files.
  17607. With prefix arg UNCOMPILED, load the uncompiled versions."
  17608. (interactive "P")
  17609. (require 'find-func)
  17610. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17611. (dir-org (file-name-directory (org-find-library-name "org")))
  17612. (dir-org-contrib (ignore-errors
  17613. (file-name-directory
  17614. (org-find-library-name "org-contribdir"))))
  17615. (babel-files
  17616. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17617. (append (list nil "comint" "eval" "exp" "keys"
  17618. "lob" "ref" "table" "tangle")
  17619. (delq nil
  17620. (mapcar
  17621. (lambda (lang)
  17622. (when (cdr lang) (symbol-name (car lang))))
  17623. org-babel-load-languages)))))
  17624. (files
  17625. (append (directory-files dir-org t file-re)
  17626. babel-files
  17627. (and dir-org-contrib
  17628. (directory-files dir-org-contrib t file-re))))
  17629. (remove-re (concat (if (featurep 'xemacs)
  17630. "org-colview" "org-colview-xemacs")
  17631. "\\'")))
  17632. (setq files (mapcar 'file-name-sans-extension files))
  17633. (setq files (mapcar
  17634. (lambda (x) (if (string-match remove-re x) nil x))
  17635. files))
  17636. (setq files (delq nil files))
  17637. (mapc
  17638. (lambda (f)
  17639. (when (featurep (intern (file-name-nondirectory f)))
  17640. (if (and (not uncompiled)
  17641. (file-exists-p (concat f ".elc")))
  17642. (load (concat f ".elc") nil nil t)
  17643. (load (concat f ".el") nil nil t))))
  17644. files))
  17645. (org-version))
  17646. ;;;###autoload
  17647. (defun org-customize ()
  17648. "Call the customize function with org as argument."
  17649. (interactive)
  17650. (org-load-modules-maybe)
  17651. (org-require-autoloaded-modules)
  17652. (customize-browse 'org))
  17653. (defun org-create-customize-menu ()
  17654. "Create a full customization menu for Org-mode, insert it into the menu."
  17655. (interactive)
  17656. (org-load-modules-maybe)
  17657. (org-require-autoloaded-modules)
  17658. (if (fboundp 'customize-menu-create)
  17659. (progn
  17660. (easy-menu-change
  17661. '("Org") "Customize"
  17662. `(["Browse Org group" org-customize t]
  17663. "--"
  17664. ,(customize-menu-create 'org)
  17665. ["Set" Custom-set t]
  17666. ["Save" Custom-save t]
  17667. ["Reset to Current" Custom-reset-current t]
  17668. ["Reset to Saved" Custom-reset-saved t]
  17669. ["Reset to Standard Settings" Custom-reset-standard t]))
  17670. (message "\"Org\"-menu now contains full customization menu"))
  17671. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17672. ;;;; Miscellaneous stuff
  17673. ;;; Generally useful functions
  17674. (defun org-get-at-bol (property)
  17675. "Get text property PROPERTY at beginning of line."
  17676. (get-text-property (point-at-bol) property))
  17677. (defun org-find-text-property-in-string (prop s)
  17678. "Return the first non-nil value of property PROP in string S."
  17679. (or (get-text-property 0 prop s)
  17680. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17681. prop s)))
  17682. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17683. "Display the given MESSAGE as a warning."
  17684. (if (fboundp 'display-warning)
  17685. (display-warning 'org message
  17686. (if (featurep 'xemacs) 'warning :warning))
  17687. (let ((buf (get-buffer-create "*Org warnings*")))
  17688. (with-current-buffer buf
  17689. (goto-char (point-max))
  17690. (insert "Warning (Org): " message)
  17691. (unless (bolp)
  17692. (newline)))
  17693. (display-buffer buf)
  17694. (sit-for 0))))
  17695. (defun org-eval (form)
  17696. "Eval FORM and return result."
  17697. (condition-case error
  17698. (eval form)
  17699. (error (format "%%![Error: %s]" error))))
  17700. (defun org-in-clocktable-p ()
  17701. "Check if the cursor is in a clocktable."
  17702. (let ((pos (point)) start)
  17703. (save-excursion
  17704. (end-of-line 1)
  17705. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  17706. (setq start (match-beginning 0))
  17707. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  17708. (>= (match-end 0) pos)
  17709. start))))
  17710. (defun org-in-commented-line ()
  17711. "Is point in a line starting with `#'?"
  17712. (equal (char-after (point-at-bol)) ?#))
  17713. (defun org-in-indented-comment-line ()
  17714. "Is point in a line starting with `#' after some white space?"
  17715. (save-excursion
  17716. (save-match-data
  17717. (goto-char (point-at-bol))
  17718. (looking-at "[ \t]*#"))))
  17719. (defun org-in-verbatim-emphasis ()
  17720. (save-match-data
  17721. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17722. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17723. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17724. (if (and marker (marker-buffer marker)
  17725. (buffer-live-p (marker-buffer marker)))
  17726. (progn
  17727. (org-pop-to-buffer-same-window (marker-buffer marker))
  17728. (if (or (> marker (point-max)) (< marker (point-min)))
  17729. (widen))
  17730. (goto-char marker)
  17731. (org-show-context 'org-goto))
  17732. (if bookmark
  17733. (bookmark-jump bookmark)
  17734. (error "Cannot find location"))))
  17735. (defun org-quote-csv-field (s)
  17736. "Quote field for inclusion in CSV material."
  17737. (if (string-match "[\",]" s)
  17738. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17739. s))
  17740. (defun org-force-self-insert (N)
  17741. "Needed to enforce self-insert under remapping."
  17742. (interactive "p")
  17743. (self-insert-command N))
  17744. (defun org-string-width (s)
  17745. "Compute width of string, ignoring invisible characters.
  17746. This ignores character with invisibility property `org-link', and also
  17747. characters with property `org-cwidth', because these will become invisible
  17748. upon the next fontification round."
  17749. (let (b l)
  17750. (when (or (eq t buffer-invisibility-spec)
  17751. (assq 'org-link buffer-invisibility-spec))
  17752. (while (setq b (text-property-any 0 (length s)
  17753. 'invisible 'org-link s))
  17754. (setq s (concat (substring s 0 b)
  17755. (substring s (or (next-single-property-change
  17756. b 'invisible s) (length s)))))))
  17757. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17758. (setq s (concat (substring s 0 b)
  17759. (substring s (or (next-single-property-change
  17760. b 'org-cwidth s) (length s))))))
  17761. (setq l (string-width s) b -1)
  17762. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17763. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17764. l))
  17765. (defun org-shorten-string (s maxlength)
  17766. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17767. If the string is shorter or has length MAXLENGTH, just return the
  17768. original string. If it is longer, the functions finds a space in the
  17769. string, breaks this string off at that locations and adds three dots
  17770. as ellipsis. Including the ellipsis, the string will not be longer
  17771. than MAXLENGTH. If finding a good breaking point in the string does
  17772. not work, the string is just chopped off in the middle of a word
  17773. if necessary."
  17774. (if (<= (length s) maxlength)
  17775. s
  17776. (let* ((n (max (- maxlength 4) 1))
  17777. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17778. (if (string-match re s)
  17779. (concat (match-string 1 s) "...")
  17780. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17781. (defun org-get-indentation (&optional line)
  17782. "Get the indentation of the current line, interpreting tabs.
  17783. When LINE is given, assume it represents a line and compute its indentation."
  17784. (if line
  17785. (if (string-match "^ *" (org-remove-tabs line))
  17786. (match-end 0))
  17787. (save-excursion
  17788. (beginning-of-line 1)
  17789. (skip-chars-forward " \t")
  17790. (current-column))))
  17791. (defun org-get-string-indentation (s)
  17792. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17793. (let ((n -1) (i 0) (w tab-width) c)
  17794. (catch 'exit
  17795. (while (< (setq n (1+ n)) (length s))
  17796. (setq c (aref s n))
  17797. (cond ((= c ?\ ) (setq i (1+ i)))
  17798. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17799. (t (throw 'exit t)))))
  17800. i))
  17801. (defun org-remove-tabs (s &optional width)
  17802. "Replace tabulators in S with spaces.
  17803. Assumes that s is a single line, starting in column 0."
  17804. (setq width (or width tab-width))
  17805. (while (string-match "\t" s)
  17806. (setq s (replace-match
  17807. (make-string
  17808. (- (* width (/ (+ (match-beginning 0) width) width))
  17809. (match-beginning 0)) ?\ )
  17810. t t s)))
  17811. s)
  17812. (defun org-fix-indentation (line ind)
  17813. "Fix indentation in LINE.
  17814. IND is a cons cell with target and minimum indentation.
  17815. If the current indentation in LINE is smaller than the minimum,
  17816. leave it alone. If it is larger than ind, set it to the target."
  17817. (let* ((l (org-remove-tabs line))
  17818. (i (org-get-indentation l))
  17819. (i1 (car ind)) (i2 (cdr ind)))
  17820. (if (>= i i2) (setq l (substring line i2)))
  17821. (if (> i1 0)
  17822. (concat (make-string i1 ?\ ) l)
  17823. l)))
  17824. (defun org-remove-indentation (code &optional n)
  17825. "Remove the maximum common indentation from the lines in CODE.
  17826. N may optionally be the number of spaces to remove."
  17827. (with-temp-buffer
  17828. (insert code)
  17829. (org-do-remove-indentation n)
  17830. (buffer-string)))
  17831. (defun org-do-remove-indentation (&optional n)
  17832. "Remove the maximum common indentation from the buffer."
  17833. (untabify (point-min) (point-max))
  17834. (let ((min 10000) re)
  17835. (if n
  17836. (setq min n)
  17837. (goto-char (point-min))
  17838. (while (re-search-forward "^ *[^ \n]" nil t)
  17839. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17840. (unless (or (= min 0) (= min 10000))
  17841. (setq re (format "^ \\{%d\\}" min))
  17842. (goto-char (point-min))
  17843. (while (re-search-forward re nil t)
  17844. (replace-match "")
  17845. (end-of-line 1))
  17846. min)))
  17847. (defun org-fill-template (template alist)
  17848. "Find each %key of ALIST in TEMPLATE and replace it."
  17849. (let ((case-fold-search nil)
  17850. entry key value)
  17851. (setq alist (sort (copy-sequence alist)
  17852. (lambda (a b) (< (length (car a)) (length (car b))))))
  17853. (while (setq entry (pop alist))
  17854. (setq template
  17855. (replace-regexp-in-string
  17856. (concat "%" (regexp-quote (car entry)))
  17857. (cdr entry) template t t)))
  17858. template))
  17859. (defun org-base-buffer (buffer)
  17860. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17861. (if (not buffer)
  17862. buffer
  17863. (or (buffer-base-buffer buffer)
  17864. buffer)))
  17865. (defun org-trim (s)
  17866. "Remove whitespace at beginning and end of string."
  17867. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17868. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17869. s)
  17870. (defun org-wrap (string &optional width lines)
  17871. "Wrap string to either a number of lines, or a width in characters.
  17872. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17873. that costs. If there is a word longer than WIDTH, the text is actually
  17874. wrapped to the length of that word.
  17875. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17876. many lines, whatever width that takes.
  17877. The return value is a list of lines, without newlines at the end."
  17878. (let* ((words (org-split-string string "[ \t\n]+"))
  17879. (maxword (apply 'max (mapcar 'org-string-width words)))
  17880. w ll)
  17881. (cond (width
  17882. (org-do-wrap words (max maxword width)))
  17883. (lines
  17884. (setq w maxword)
  17885. (setq ll (org-do-wrap words maxword))
  17886. (if (<= (length ll) lines)
  17887. ll
  17888. (setq ll words)
  17889. (while (> (length ll) lines)
  17890. (setq w (1+ w))
  17891. (setq ll (org-do-wrap words w)))
  17892. ll))
  17893. (t (error "Cannot wrap this")))))
  17894. (defun org-do-wrap (words width)
  17895. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17896. (let (lines line)
  17897. (while words
  17898. (setq line (pop words))
  17899. (while (and words (< (+ (length line) (length (car words))) width))
  17900. (setq line (concat line " " (pop words))))
  17901. (setq lines (push line lines)))
  17902. (nreverse lines)))
  17903. (defun org-split-string (string &optional separators)
  17904. "Splits STRING into substrings at SEPARATORS.
  17905. No empty strings are returned if there are matches at the beginning
  17906. and end of string."
  17907. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17908. (start 0)
  17909. notfirst
  17910. (list nil))
  17911. (while (and (string-match rexp string
  17912. (if (and notfirst
  17913. (= start (match-beginning 0))
  17914. (< start (length string)))
  17915. (1+ start) start))
  17916. (< (match-beginning 0) (length string)))
  17917. (setq notfirst t)
  17918. (or (eq (match-beginning 0) 0)
  17919. (and (eq (match-beginning 0) (match-end 0))
  17920. (eq (match-beginning 0) start))
  17921. (setq list
  17922. (cons (substring string start (match-beginning 0))
  17923. list)))
  17924. (setq start (match-end 0)))
  17925. (or (eq start (length string))
  17926. (setq list
  17927. (cons (substring string start)
  17928. list)))
  17929. (nreverse list)))
  17930. (defun org-quote-vert (s)
  17931. "Replace \"|\" with \"\\vert\"."
  17932. (while (string-match "|" s)
  17933. (setq s (replace-match "\\vert" t t s)))
  17934. s)
  17935. (defun org-uuidgen-p (s)
  17936. "Is S an ID created by UUIDGEN?"
  17937. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17938. (defun org-context ()
  17939. "Return a list of contexts of the current cursor position.
  17940. If several contexts apply, all are returned.
  17941. Each context entry is a list with a symbol naming the context, and
  17942. two positions indicating start and end of the context. Possible
  17943. contexts are:
  17944. :headline anywhere in a headline
  17945. :headline-stars on the leading stars in a headline
  17946. :todo-keyword on a TODO keyword (including DONE) in a headline
  17947. :tags on the TAGS in a headline
  17948. :priority on the priority cookie in a headline
  17949. :item on the first line of a plain list item
  17950. :item-bullet on the bullet/number of a plain list item
  17951. :checkbox on the checkbox in a plain list item
  17952. :table in an org-mode table
  17953. :table-special on a special filed in a table
  17954. :table-table in a table.el table
  17955. :link on a hyperlink
  17956. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17957. :target on a <<target>>
  17958. :radio-target on a <<<radio-target>>>
  17959. :latex-fragment on a LaTeX fragment
  17960. :latex-preview on a LaTeX fragment with overlaid preview image
  17961. This function expects the position to be visible because it uses font-lock
  17962. faces as a help to recognize the following contexts: :table-special, :link,
  17963. and :keyword."
  17964. (let* ((f (get-text-property (point) 'face))
  17965. (faces (if (listp f) f (list f)))
  17966. (p (point)) clist o)
  17967. ;; First the large context
  17968. (cond
  17969. ((org-at-heading-p t)
  17970. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17971. (when (progn
  17972. (beginning-of-line 1)
  17973. (looking-at org-todo-line-tags-regexp))
  17974. (push (org-point-in-group p 1 :headline-stars) clist)
  17975. (push (org-point-in-group p 2 :todo-keyword) clist)
  17976. (push (org-point-in-group p 4 :tags) clist))
  17977. (goto-char p)
  17978. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17979. (if (looking-at "\\[#[A-Z0-9]\\]")
  17980. (push (org-point-in-group p 0 :priority) clist)))
  17981. ((org-at-item-p)
  17982. (push (org-point-in-group p 2 :item-bullet) clist)
  17983. (push (list :item (point-at-bol)
  17984. (save-excursion (org-end-of-item) (point)))
  17985. clist)
  17986. (and (org-at-item-checkbox-p)
  17987. (push (org-point-in-group p 0 :checkbox) clist)))
  17988. ((org-at-table-p)
  17989. (push (list :table (org-table-begin) (org-table-end)) clist)
  17990. (if (memq 'org-formula faces)
  17991. (push (list :table-special
  17992. (previous-single-property-change p 'face)
  17993. (next-single-property-change p 'face)) clist)))
  17994. ((org-at-table-p 'any)
  17995. (push (list :table-table) clist)))
  17996. (goto-char p)
  17997. ;; Now the small context
  17998. (cond
  17999. ((org-at-timestamp-p)
  18000. (push (org-point-in-group p 0 :timestamp) clist))
  18001. ((memq 'org-link faces)
  18002. (push (list :link
  18003. (previous-single-property-change p 'face)
  18004. (next-single-property-change p 'face)) clist))
  18005. ((memq 'org-special-keyword faces)
  18006. (push (list :keyword
  18007. (previous-single-property-change p 'face)
  18008. (next-single-property-change p 'face)) clist))
  18009. ((org-at-target-p)
  18010. (push (org-point-in-group p 0 :target) clist)
  18011. (goto-char (1- (match-beginning 0)))
  18012. (if (looking-at org-radio-target-regexp)
  18013. (push (org-point-in-group p 0 :radio-target) clist))
  18014. (goto-char p))
  18015. ((setq o (car (delq nil
  18016. (mapcar
  18017. (lambda (x)
  18018. (if (memq x org-latex-fragment-image-overlays) x))
  18019. (overlays-at (point))))))
  18020. (push (list :latex-fragment
  18021. (overlay-start o) (overlay-end o)) clist)
  18022. (push (list :latex-preview
  18023. (overlay-start o) (overlay-end o)) clist))
  18024. ((org-inside-LaTeX-fragment-p)
  18025. ;; FIXME: positions wrong.
  18026. (push (list :latex-fragment (point) (point)) clist)))
  18027. (setq clist (nreverse (delq nil clist)))
  18028. clist))
  18029. ;; FIXME: Compare with at-regexp-p Do we need both?
  18030. (defun org-in-regexp (re &optional nlines visually)
  18031. "Check if point is inside a match of regexp.
  18032. Normally only the current line is checked, but you can include NLINES extra
  18033. lines both before and after point into the search.
  18034. If VISUALLY is set, require that the cursor is not after the match but
  18035. really on, so that the block visually is on the match."
  18036. (catch 'exit
  18037. (let ((pos (point))
  18038. (eol (point-at-eol (+ 1 (or nlines 0))))
  18039. (inc (if visually 1 0)))
  18040. (save-excursion
  18041. (beginning-of-line (- 1 (or nlines 0)))
  18042. (while (re-search-forward re eol t)
  18043. (if (and (<= (match-beginning 0) pos)
  18044. (>= (+ inc (match-end 0)) pos))
  18045. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18046. (defun org-at-regexp-p (regexp)
  18047. "Is point inside a match of REGEXP in the current line?"
  18048. (catch 'exit
  18049. (save-excursion
  18050. (let ((pos (point)) (end (point-at-eol)))
  18051. (beginning-of-line 1)
  18052. (while (re-search-forward regexp end t)
  18053. (if (and (<= (match-beginning 0) pos)
  18054. (>= (match-end 0) pos))
  18055. (throw 'exit t)))
  18056. nil))))
  18057. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18058. "Non-nil when point is between matches of START-RE and END-RE.
  18059. Also return a non-nil value when point is on one of the matches.
  18060. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18061. buffer positions. Default values are the positions of headlines
  18062. surrounding the point.
  18063. The functions returns a cons cell whose car (resp. cdr) is the
  18064. position before START-RE (resp. after END-RE)."
  18065. (save-match-data
  18066. (let ((pos (point))
  18067. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18068. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18069. beg end)
  18070. (save-excursion
  18071. ;; Point is on a block when on START-RE or if START-RE can be
  18072. ;; found before it...
  18073. (and (or (org-at-regexp-p start-re)
  18074. (re-search-backward start-re limit-up t))
  18075. (setq beg (match-beginning 0))
  18076. ;; ... and END-RE after it...
  18077. (goto-char (match-end 0))
  18078. (re-search-forward end-re limit-down t)
  18079. (> (setq end (match-end 0)) pos)
  18080. ;; ... without another START-RE in-between.
  18081. (goto-char (match-beginning 0))
  18082. (not (re-search-backward start-re (1+ beg) t))
  18083. ;; Return value.
  18084. (cons beg end))))))
  18085. (defun org-in-block-p (names)
  18086. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18087. NAMES is a list of strings containing names of blocks.
  18088. Return first block name matched, or nil. Beware that in case of
  18089. nested blocks, the returned name may not belong to the closest
  18090. block from point."
  18091. (save-match-data
  18092. (catch 'exit
  18093. (let ((case-fold-search t)
  18094. (lim-up (save-excursion (outline-previous-heading)))
  18095. (lim-down (save-excursion (outline-next-heading))))
  18096. (mapc (lambda (name)
  18097. (let ((n (regexp-quote name)))
  18098. (when (org-between-regexps-p
  18099. (concat "^[ \t]*#\\+begin_" n)
  18100. (concat "^[ \t]*#\\+end_" n)
  18101. lim-up lim-down)
  18102. (throw 'exit n))))
  18103. names))
  18104. nil)))
  18105. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18106. "Call `multi-occur' with buffers for all agenda files."
  18107. (interactive "sOrg-files matching: \np")
  18108. (let* ((files (org-agenda-files))
  18109. (tnames (mapcar 'file-truename files))
  18110. (extra org-agenda-text-search-extra-files)
  18111. f)
  18112. (when (eq (car extra) 'agenda-archives)
  18113. (setq extra (cdr extra))
  18114. (setq files (org-add-archive-files files)))
  18115. (while (setq f (pop extra))
  18116. (unless (member (file-truename f) tnames)
  18117. (add-to-list 'files f 'append)
  18118. (add-to-list 'tnames (file-truename f) 'append)))
  18119. (multi-occur
  18120. (mapcar (lambda (x)
  18121. (with-current-buffer
  18122. (or (get-file-buffer x) (find-file-noselect x))
  18123. (widen)
  18124. (current-buffer)))
  18125. files)
  18126. regexp)))
  18127. (if (boundp 'occur-mode-find-occurrence-hook)
  18128. ;; Emacs 23
  18129. (add-hook 'occur-mode-find-occurrence-hook
  18130. (lambda ()
  18131. (when (eq major-mode 'org-mode)
  18132. (org-reveal))))
  18133. ;; Emacs 22
  18134. (defadvice occur-mode-goto-occurrence
  18135. (after org-occur-reveal activate)
  18136. (and (eq major-mode 'org-mode) (org-reveal)))
  18137. (defadvice occur-mode-goto-occurrence-other-window
  18138. (after org-occur-reveal activate)
  18139. (and (eq major-mode 'org-mode) (org-reveal)))
  18140. (defadvice occur-mode-display-occurrence
  18141. (after org-occur-reveal activate)
  18142. (when (eq major-mode 'org-mode)
  18143. (let ((pos (occur-mode-find-occurrence)))
  18144. (with-current-buffer (marker-buffer pos)
  18145. (save-excursion
  18146. (goto-char pos)
  18147. (org-reveal)))))))
  18148. (defun org-occur-link-in-agenda-files ()
  18149. "Create a link and search for it in the agendas.
  18150. The link is not stored in `org-stored-links', it is just created
  18151. for the search purpose."
  18152. (interactive)
  18153. (let ((link (condition-case nil
  18154. (org-store-link nil)
  18155. (error "Unable to create a link to here"))))
  18156. (org-occur-in-agenda-files (regexp-quote link))))
  18157. (defun org-uniquify (list)
  18158. "Remove duplicate elements from LIST."
  18159. (let (res)
  18160. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18161. res))
  18162. (defun org-delete-all (elts list)
  18163. "Remove all elements in ELTS from LIST."
  18164. (while elts
  18165. (setq list (delete (pop elts) list)))
  18166. list)
  18167. (defun org-count (cl-item cl-seq)
  18168. "Count the number of occurrences of ITEM in SEQ.
  18169. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18170. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18171. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18172. (while (< cl-start cl-end)
  18173. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18174. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18175. (setq cl-start (1+ cl-start)))
  18176. cl-count))
  18177. (defun org-remove-if (predicate seq)
  18178. "Remove everything from SEQ that fulfills PREDICATE."
  18179. (let (res e)
  18180. (while seq
  18181. (setq e (pop seq))
  18182. (if (not (funcall predicate e)) (push e res)))
  18183. (nreverse res)))
  18184. (defun org-remove-if-not (predicate seq)
  18185. "Remove everything from SEQ that does not fulfill PREDICATE."
  18186. (let (res e)
  18187. (while seq
  18188. (setq e (pop seq))
  18189. (if (funcall predicate e) (push e res)))
  18190. (nreverse res)))
  18191. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18192. "Reduce two-argument FUNCTION across SEQ.
  18193. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18194. \":initial-value\" removed."
  18195. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18196. (cadr (memq :initial-value cl-keys)))
  18197. (cl-seq (pop cl-seq))
  18198. (t (funcall cl-func)))))
  18199. (while cl-seq
  18200. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18201. cl-accum))
  18202. (defun org-back-over-empty-lines ()
  18203. "Move backwards over whitespace, to the beginning of the first empty line.
  18204. Returns the number of empty lines passed."
  18205. (let ((pos (point)))
  18206. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18207. (skip-chars-backward " \t\n\r")
  18208. (unless (eobp)
  18209. (forward-line -1)))
  18210. (beginning-of-line 2)
  18211. (goto-char (min (point) pos))
  18212. (count-lines (point) pos)))
  18213. (defun org-skip-whitespace ()
  18214. (skip-chars-forward " \t\n\r"))
  18215. (defun org-point-in-group (point group &optional context)
  18216. "Check if POINT is in match-group GROUP.
  18217. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18218. match. If the match group does not exist or point is not inside it,
  18219. return nil."
  18220. (and (match-beginning group)
  18221. (>= point (match-beginning group))
  18222. (<= point (match-end group))
  18223. (if context
  18224. (list context (match-beginning group) (match-end group))
  18225. t)))
  18226. (defun org-switch-to-buffer-other-window (&rest args)
  18227. "Switch to buffer in a second window on the current frame.
  18228. In particular, do not allow pop-up frames.
  18229. Returns the newly created buffer."
  18230. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18231. special-display-function)
  18232. (apply 'switch-to-buffer-other-window args)))
  18233. (defun org-combine-plists (&rest plists)
  18234. "Create a single property list from all plists in PLISTS.
  18235. The process starts by copying the first list, and then setting properties
  18236. from the other lists. Settings in the last list are the most significant
  18237. ones and overrule settings in the other lists."
  18238. (let ((rtn (copy-sequence (pop plists)))
  18239. p v ls)
  18240. (while plists
  18241. (setq ls (pop plists))
  18242. (while ls
  18243. (setq p (pop ls) v (pop ls))
  18244. (setq rtn (plist-put rtn p v))))
  18245. rtn))
  18246. (defun org-move-line-down (arg)
  18247. "Move the current line down. With prefix argument, move it past ARG lines."
  18248. (interactive "p")
  18249. (let ((col (current-column))
  18250. beg end pos)
  18251. (beginning-of-line 1) (setq beg (point))
  18252. (beginning-of-line 2) (setq end (point))
  18253. (beginning-of-line (+ 1 arg))
  18254. (setq pos (move-marker (make-marker) (point)))
  18255. (insert (delete-and-extract-region beg end))
  18256. (goto-char pos)
  18257. (org-move-to-column col)))
  18258. (defun org-move-line-up (arg)
  18259. "Move the current line up. With prefix argument, move it past ARG lines."
  18260. (interactive "p")
  18261. (let ((col (current-column))
  18262. beg end pos)
  18263. (beginning-of-line 1) (setq beg (point))
  18264. (beginning-of-line 2) (setq end (point))
  18265. (beginning-of-line (- arg))
  18266. (setq pos (move-marker (make-marker) (point)))
  18267. (insert (delete-and-extract-region beg end))
  18268. (goto-char pos)
  18269. (org-move-to-column col)))
  18270. (defun org-replace-escapes (string table)
  18271. "Replace %-escapes in STRING with values in TABLE.
  18272. TABLE is an association list with keys like \"%a\" and string values.
  18273. The sequences in STRING may contain normal field width and padding information,
  18274. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18275. so values can contain further %-escapes if they are define later in TABLE."
  18276. (let ((tbl (copy-alist table))
  18277. (case-fold-search nil)
  18278. (pchg 0)
  18279. e re rpl)
  18280. (while (setq e (pop tbl))
  18281. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18282. (when (and (cdr e) (string-match re (cdr e)))
  18283. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18284. (safe "SREF"))
  18285. (add-text-properties 0 3 (list 'sref sref) safe)
  18286. (setcdr e (replace-match safe t t (cdr e)))))
  18287. (while (string-match re string)
  18288. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18289. (cdr e)))
  18290. (setq string (replace-match rpl t t string))))
  18291. (while (setq pchg (next-property-change pchg string))
  18292. (let ((sref (get-text-property pchg 'sref string)))
  18293. (when (and sref (string-match "SREF" string pchg))
  18294. (setq string (replace-match sref t t string)))))
  18295. string))
  18296. (defun org-sublist (list start end)
  18297. "Return a section of LIST, from START to END.
  18298. Counting starts at 1."
  18299. (let (rtn (c start))
  18300. (setq list (nthcdr (1- start) list))
  18301. (while (and list (<= c end))
  18302. (push (pop list) rtn)
  18303. (setq c (1+ c)))
  18304. (nreverse rtn)))
  18305. (defun org-find-base-buffer-visiting (file)
  18306. "Like `find-buffer-visiting' but always return the base buffer and
  18307. not an indirect buffer."
  18308. (let ((buf (or (get-file-buffer file)
  18309. (find-buffer-visiting file))))
  18310. (if buf
  18311. (or (buffer-base-buffer buf) buf)
  18312. nil)))
  18313. (defun org-image-file-name-regexp (&optional extensions)
  18314. "Return regexp matching the file names of images.
  18315. If EXTENSIONS is given, only match these."
  18316. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18317. (image-file-name-regexp)
  18318. (let ((image-file-name-extensions
  18319. (or extensions
  18320. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18321. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18322. (concat "\\."
  18323. (regexp-opt (nconc (mapcar 'upcase
  18324. image-file-name-extensions)
  18325. image-file-name-extensions)
  18326. t)
  18327. "\\'"))))
  18328. (defun org-file-image-p (file &optional extensions)
  18329. "Return non-nil if FILE is an image."
  18330. (save-match-data
  18331. (string-match (org-image-file-name-regexp extensions) file)))
  18332. (defun org-get-cursor-date ()
  18333. "Return the date at cursor in as a time.
  18334. This works in the calendar and in the agenda, anywhere else it just
  18335. returns the current time."
  18336. (let (date day defd)
  18337. (cond
  18338. ((eq major-mode 'calendar-mode)
  18339. (setq date (calendar-cursor-to-date)
  18340. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18341. ((eq major-mode 'org-agenda-mode)
  18342. (setq day (get-text-property (point) 'day))
  18343. (if day
  18344. (setq date (calendar-gregorian-from-absolute day)
  18345. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18346. (nth 2 date))))))
  18347. (or defd (current-time))))
  18348. (defvar org-agenda-action-marker (make-marker)
  18349. "Marker pointing to the entry for the next agenda action.")
  18350. (defun org-mark-entry-for-agenda-action ()
  18351. "Mark the current entry as target of an agenda action.
  18352. Agenda actions are actions executed from the agenda with the key `k',
  18353. which make use of the date at the cursor."
  18354. (interactive)
  18355. (move-marker org-agenda-action-marker
  18356. (save-excursion (org-back-to-heading t) (point))
  18357. (current-buffer))
  18358. (message
  18359. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18360. (defun org-mark-subtree ()
  18361. "Mark the current subtree.
  18362. This puts point at the start of the current subtree, and mark at the end.
  18363. If point is in an inline task, mark that task instead."
  18364. (interactive)
  18365. (let ((inline-task-p
  18366. (and (featurep 'org-inlinetask)
  18367. (org-inlinetask-in-task-p)))
  18368. (beg))
  18369. ;; Get beginning of subtree
  18370. (cond
  18371. (inline-task-p (org-inlinetask-goto-beginning))
  18372. ((org-at-heading-p) (beginning-of-line))
  18373. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18374. (setq beg (point))
  18375. ;; Get end of it
  18376. (if inline-task-p
  18377. (org-inlinetask-goto-end)
  18378. (org-end-of-subtree))
  18379. ;; Mark zone
  18380. (push-mark (point) nil t)
  18381. (goto-char beg)))
  18382. ;;; Paragraph filling stuff.
  18383. ;; We want this to be just right, so use the full arsenal.
  18384. (defun org-indent-line-function ()
  18385. "Indent line depending on context."
  18386. (interactive)
  18387. (let* ((pos (point))
  18388. (itemp (org-at-item-p))
  18389. (case-fold-search t)
  18390. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18391. (inline-task-p (and (featurep 'org-inlinetask)
  18392. (org-inlinetask-in-task-p)))
  18393. (inline-re (and inline-task-p
  18394. (org-inlinetask-outline-regexp)))
  18395. column)
  18396. (beginning-of-line 1)
  18397. (cond
  18398. ;; Comments
  18399. ((looking-at "# ") (setq column 0))
  18400. ;; Headings
  18401. ((looking-at org-outline-regexp) (setq column 0))
  18402. ;; Included files
  18403. ((looking-at "#\\+include:") (setq column 0))
  18404. ;; Footnote definition
  18405. ((looking-at org-footnote-definition-re) (setq column 0))
  18406. ;; Literal examples
  18407. ((looking-at "[ \t]*:\\( \\|$\\)")
  18408. (setq column (org-get-indentation))) ; do nothing
  18409. ;; Lists
  18410. ((ignore-errors (goto-char (org-in-item-p)))
  18411. (setq column (if itemp
  18412. (org-get-indentation)
  18413. (org-list-item-body-column (point))))
  18414. (goto-char pos))
  18415. ;; Drawers
  18416. ((and (looking-at "[ \t]*:END:")
  18417. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18418. (save-excursion
  18419. (goto-char (1- (match-beginning 1)))
  18420. (setq column (current-column))))
  18421. ;; Special blocks
  18422. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18423. (save-excursion
  18424. (re-search-backward
  18425. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18426. (setq column (org-get-indentation (match-string 0))))
  18427. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18428. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18429. (save-excursion
  18430. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18431. (setq column
  18432. (cond ((equal (downcase (match-string 1)) "src")
  18433. ;; src blocks: let `org-edit-src-exit' handle them
  18434. (org-get-indentation))
  18435. ((equal (downcase (match-string 1)) "example")
  18436. (max (org-get-indentation)
  18437. (org-get-indentation (match-string 0))))
  18438. (t
  18439. (org-get-indentation (match-string 0))))))
  18440. ;; This line has nothing special, look at the previous relevant
  18441. ;; line to compute indentation
  18442. (t
  18443. (beginning-of-line 0)
  18444. (while (and (not (bobp))
  18445. (not (looking-at org-drawer-regexp))
  18446. ;; When point started in an inline task, do not move
  18447. ;; above task starting line.
  18448. (not (and inline-task-p (looking-at inline-re)))
  18449. ;; Skip drawers, blocks, empty lines, verbatim,
  18450. ;; comments, tables, footnotes definitions, lists,
  18451. ;; inline tasks.
  18452. (or (and (looking-at "[ \t]*:END:")
  18453. (re-search-backward org-drawer-regexp nil t))
  18454. (and (looking-at "[ \t]*#\\+end_")
  18455. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18456. (looking-at "[ \t]*[\n:#|]")
  18457. (looking-at org-footnote-definition-re)
  18458. (and (ignore-errors (goto-char (org-in-item-p)))
  18459. (goto-char
  18460. (org-list-get-top-point (org-list-struct))))
  18461. (and (not inline-task-p)
  18462. (featurep 'org-inlinetask)
  18463. (org-inlinetask-in-task-p)
  18464. (or (org-inlinetask-goto-beginning) t))))
  18465. (beginning-of-line 0))
  18466. (cond
  18467. ;; There was an heading above.
  18468. ((looking-at "\\*+[ \t]+")
  18469. (if (not org-adapt-indentation)
  18470. (setq column 0)
  18471. (goto-char (match-end 0))
  18472. (setq column (current-column))))
  18473. ;; A drawer had started and is unfinished
  18474. ((looking-at org-drawer-regexp)
  18475. (goto-char (1- (match-beginning 1)))
  18476. (setq column (current-column)))
  18477. ;; Else, nothing noticeable found: get indentation and go on.
  18478. (t (setq column (org-get-indentation))))))
  18479. ;; Now apply indentation and move cursor accordingly
  18480. (goto-char pos)
  18481. (if (<= (current-column) (current-indentation))
  18482. (org-indent-line-to column)
  18483. (save-excursion (org-indent-line-to column)))
  18484. ;; Special polishing for properties, see `org-property-format'
  18485. (setq column (current-column))
  18486. (beginning-of-line 1)
  18487. (if (looking-at
  18488. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18489. (replace-match (concat (match-string 1)
  18490. (format org-property-format
  18491. (match-string 2) (match-string 3)))
  18492. t t))
  18493. (org-move-to-column column)))
  18494. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18495. "Variable to store copy of `adaptive-fill-regexp'.
  18496. Since `adaptive-fill-regexp' is set to never match, we need to
  18497. store a backup of its value before entering `org-mode' so that
  18498. the functionality can be provided as a fall-back.")
  18499. (defun org-set-autofill-regexps ()
  18500. (interactive)
  18501. ;; In the paragraph separator we include headlines, because filling
  18502. ;; text in a line directly attached to a headline would otherwise
  18503. ;; fill the headline as well.
  18504. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18505. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18506. ;; The paragraph starter includes hand-formatted lists.
  18507. (org-set-local
  18508. 'paragraph-start
  18509. (concat
  18510. "\f" "\\|"
  18511. "[ ]*$" "\\|"
  18512. org-outline-regexp "\\|"
  18513. "[ \t]*#" "\\|"
  18514. (org-item-re) "\\|"
  18515. "[ \t]*[:|]" "\\|"
  18516. "\\$\\$" "\\|"
  18517. "\\\\\\(begin\\|end\\|[][]\\)"))
  18518. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18519. ;; But only if the user has not turned off tables or fixed-width regions
  18520. (org-set-local
  18521. 'auto-fill-inhibit-regexp
  18522. (concat org-outline-regexp
  18523. "\\|#\\+"
  18524. "\\|[ \t]*" org-keyword-time-regexp
  18525. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18526. (concat
  18527. "\\|[ \t]*["
  18528. (if org-enable-table-editor "|" "")
  18529. (if org-enable-fixed-width-editor ":" "")
  18530. "]"))))
  18531. ;; We use our own fill-paragraph function, to make sure that tables
  18532. ;; and fixed-width regions are not wrapped. That function will pass
  18533. ;; through to `fill-paragraph' when appropriate.
  18534. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18535. ;; Prevent auto-fill from inserting unwanted new items.
  18536. (if (boundp 'fill-nobreak-predicate)
  18537. (org-set-local 'fill-nobreak-predicate
  18538. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  18539. fill-nobreak-predicate
  18540. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate))))
  18541. ;; Adaptive filling: To get full control, first make sure that
  18542. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18543. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18544. (org-set-local 'org-adaptive-fill-regexp-backup
  18545. adaptive-fill-regexp))
  18546. (org-set-local 'adaptive-fill-regexp "\000")
  18547. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18548. (org-set-local 'adaptive-fill-function
  18549. 'org-adaptive-fill-function)
  18550. (org-set-local
  18551. 'align-mode-rules-list
  18552. '((org-in-buffer-settings
  18553. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18554. (modes . '(org-mode))))))
  18555. (defun org-fill-item-nobreak-p ()
  18556. "Non-nil when a line break at point would insert a new item."
  18557. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18558. (defun org-fill-paragraph (&optional justify)
  18559. "Re-align a table, pass through to fill-paragraph if no table."
  18560. (let ((table-p (org-at-table-p))
  18561. (table.el-p (org-at-table.el-p))
  18562. (itemp (org-in-item-p)))
  18563. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18564. (save-excursion (goto-char (point-at-bol))
  18565. (looking-at org-outline-regexp)))
  18566. t) ; skip headlines
  18567. (table.el-p t) ; skip table.el tables
  18568. (table-p (org-table-align) t) ; align Org tables
  18569. (itemp ; align text in items
  18570. (let* ((struct (save-excursion (goto-char itemp)
  18571. (org-list-struct)))
  18572. (parents (org-list-parents-alist struct))
  18573. (children (org-list-get-children itemp struct parents))
  18574. beg end prev next prefix)
  18575. ;; Determine in which part of item point is: before
  18576. ;; first child, after last child, between two
  18577. ;; sub-lists, or simply in item if there's no child.
  18578. (cond
  18579. ((not children)
  18580. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18581. beg itemp
  18582. end (org-list-get-item-end itemp struct)))
  18583. ((< (point) (setq next (car children)))
  18584. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18585. beg itemp
  18586. end next))
  18587. ((> (point) (setq prev (car (last children))))
  18588. (setq beg (org-list-get-item-end prev struct)
  18589. end (org-list-get-item-end itemp struct)
  18590. prefix (save-excursion
  18591. (goto-char beg)
  18592. (skip-chars-forward " \t")
  18593. (make-string (current-column) ?\ ))))
  18594. (t (catch 'exit
  18595. (while (setq next (pop children))
  18596. (if (> (point) next)
  18597. (setq prev next)
  18598. (setq beg (org-list-get-item-end prev struct)
  18599. end next
  18600. prefix (save-excursion
  18601. (goto-char beg)
  18602. (skip-chars-forward " \t")
  18603. (make-string (current-column) ?\ )))
  18604. (throw 'exit nil))))))
  18605. ;; Use `fill-paragraph' with buffer narrowed to item
  18606. ;; without any child, and with our computed PREFIX.
  18607. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18608. (save-restriction
  18609. (narrow-to-region beg end)
  18610. (save-excursion (fill-paragraph justify)))) t))
  18611. ;; Special case where point is not in a list but is on
  18612. ;; a paragraph adjacent to a list: make sure this paragraph
  18613. ;; doesn't get merged with the end of the list by narrowing
  18614. ;; buffer first.
  18615. ((save-excursion (forward-paragraph -1)
  18616. (setq itemp (org-in-item-p)))
  18617. (let ((struct (save-excursion (goto-char itemp)
  18618. (org-list-struct))))
  18619. (save-restriction
  18620. (narrow-to-region (org-list-get-bottom-point struct)
  18621. (save-excursion (forward-paragraph 1)
  18622. (point)))
  18623. (fill-paragraph justify) t)))
  18624. ;; Else simply call `fill-paragraph'.
  18625. (t nil))))
  18626. ;; For reference, this is the default value of adaptive-fill-regexp
  18627. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18628. (defun org-adaptive-fill-function ()
  18629. "Return a fill prefix for org-mode files."
  18630. (let (itemp)
  18631. (save-excursion
  18632. (cond
  18633. ;; Comment line
  18634. ((looking-at "#[ \t]+")
  18635. (match-string-no-properties 0))
  18636. ;; Plain list item
  18637. ((org-at-item-p)
  18638. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18639. ;; Point is in a list after `backward-paragraph': original
  18640. ;; point wasn't in the list, or filling would have been taken
  18641. ;; care of by `org-auto-fill-function', but the list and the
  18642. ;; real paragraph are not separated by a blank line. Thus, move
  18643. ;; point after the list to go back to real paragraph and
  18644. ;; determine fill-prefix.
  18645. ((setq itemp (org-in-item-p))
  18646. (goto-char itemp)
  18647. (let* ((struct (org-list-struct))
  18648. (bottom (org-list-get-bottom-point struct)))
  18649. (goto-char bottom)
  18650. (make-string (org-get-indentation) ?\ )))
  18651. ;; Other text
  18652. ((looking-at org-adaptive-fill-regexp-backup)
  18653. (match-string-no-properties 0))))))
  18654. (defun org-auto-fill-function ()
  18655. "Auto-fill function."
  18656. (let (itemp prefix)
  18657. ;; When in a list, compute an appropriate fill-prefix and make
  18658. ;; sure it will be used by `do-auto-fill'.
  18659. (if (setq itemp (org-in-item-p))
  18660. (progn
  18661. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18662. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18663. (do-auto-fill)))
  18664. ;; Else just use `do-auto-fill'.
  18665. (do-auto-fill))))
  18666. ;;; Other stuff.
  18667. (defun org-toggle-fixed-width-section (arg)
  18668. "Toggle the fixed-width export.
  18669. If there is no active region, the QUOTE keyword at the current headline is
  18670. inserted or removed. When present, it causes the text between this headline
  18671. and the next to be exported as fixed-width text, and unmodified.
  18672. If there is an active region, this command adds or removes a colon as the
  18673. first character of this line. If the first character of a line is a colon,
  18674. this line is also exported in fixed-width font."
  18675. (interactive "P")
  18676. (let* ((cc 0)
  18677. (regionp (org-region-active-p))
  18678. (beg (if regionp (region-beginning) (point)))
  18679. (end (if regionp (region-end)))
  18680. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18681. (case-fold-search nil)
  18682. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18683. off)
  18684. (if regionp
  18685. (save-excursion
  18686. (goto-char beg)
  18687. (setq cc (current-column))
  18688. (beginning-of-line 1)
  18689. (setq off (looking-at re))
  18690. (while (> nlines 0)
  18691. (setq nlines (1- nlines))
  18692. (beginning-of-line 1)
  18693. (cond
  18694. (arg
  18695. (org-move-to-column cc t)
  18696. (insert ": \n")
  18697. (forward-line -1))
  18698. ((and off (looking-at re))
  18699. (replace-match "" t t nil 1))
  18700. ((not off) (org-move-to-column cc t) (insert ": ")))
  18701. (forward-line 1)))
  18702. (save-excursion
  18703. (org-back-to-heading)
  18704. (cond
  18705. ((looking-at (format org-heading-keyword-regexp-format
  18706. org-quote-string))
  18707. (goto-char (match-end 1))
  18708. (looking-at (concat " +" org-quote-string))
  18709. (replace-match "" t t)
  18710. (when (eolp) (insert " ")))
  18711. ((looking-at org-outline-regexp)
  18712. (goto-char (match-end 0))
  18713. (insert org-quote-string " ")))))))
  18714. (defun org-reftex-citation ()
  18715. "Use reftex-citation to insert a citation into the buffer.
  18716. This looks for a line like
  18717. #+BIBLIOGRAPHY: foo plain option:-d
  18718. and derives from it that foo.bib is the bibliography file relevant
  18719. for this document. It then installs the necessary environment for RefTeX
  18720. to work in this buffer and calls `reftex-citation' to insert a citation
  18721. into the buffer.
  18722. Export of such citations to both LaTeX and HTML is handled by the contributed
  18723. package org-exp-bibtex by Taru Karttunen."
  18724. (interactive)
  18725. (let ((reftex-docstruct-symbol 'rds)
  18726. (reftex-cite-format "\\cite{%l}")
  18727. rds bib)
  18728. (save-excursion
  18729. (save-restriction
  18730. (widen)
  18731. (let ((case-fold-search t)
  18732. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18733. (if (not (save-excursion
  18734. (or (re-search-forward re nil t)
  18735. (re-search-backward re nil t))))
  18736. (error "No bibliography defined in file")
  18737. (setq bib (concat (match-string 1) ".bib")
  18738. rds (list (list 'bib bib)))))))
  18739. (call-interactively 'reftex-citation)))
  18740. ;;;; Functions extending outline functionality
  18741. (defun org-beginning-of-line (&optional arg)
  18742. "Go to the beginning of the current line. If that is invisible, continue
  18743. to a visible line beginning. This makes the function of C-a more intuitive.
  18744. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18745. first attempt, and only move to after the tags when the cursor is already
  18746. beyond the end of the headline."
  18747. (interactive "P")
  18748. (let ((pos (point))
  18749. (special (if (consp org-special-ctrl-a/e)
  18750. (car org-special-ctrl-a/e)
  18751. org-special-ctrl-a/e))
  18752. refpos)
  18753. (if (org-bound-and-true-p line-move-visual)
  18754. (beginning-of-visual-line 1)
  18755. (beginning-of-line 1))
  18756. (if (and arg (fboundp 'move-beginning-of-line))
  18757. (call-interactively 'move-beginning-of-line)
  18758. (if (bobp)
  18759. nil
  18760. (backward-char 1)
  18761. (if (org-truely-invisible-p)
  18762. (while (and (not (bobp)) (org-truely-invisible-p))
  18763. (backward-char 1)
  18764. (beginning-of-line 1))
  18765. (forward-char 1))))
  18766. (when special
  18767. (cond
  18768. ((and (looking-at org-complex-heading-regexp)
  18769. (= (char-after (match-end 1)) ?\ ))
  18770. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18771. (point-at-eol)))
  18772. (goto-char
  18773. (if (eq special t)
  18774. (cond ((> pos refpos) refpos)
  18775. ((= pos (point)) refpos)
  18776. (t (point)))
  18777. (cond ((> pos (point)) (point))
  18778. ((not (eq last-command this-command)) (point))
  18779. (t refpos)))))
  18780. ((org-at-item-p)
  18781. ;; Being at an item and not looking at an the item means point
  18782. ;; was previously moved to beginning of a visual line, whiche
  18783. ;; doesn't contain the item. Therefore, do nothing special,
  18784. ;; just stay here.
  18785. (when (looking-at org-list-full-item-re)
  18786. ;; Set special position at first white space character after
  18787. ;; bullet, and check-box, if any.
  18788. (let ((after-bullet
  18789. (let ((box (match-end 3)))
  18790. (if (not box) (match-end 1)
  18791. (let ((after (char-after box)))
  18792. (if (and after (= after ? )) (1+ box) box))))))
  18793. ;; Special case: Move point to special position when
  18794. ;; currently after it or at beginning of line.
  18795. (if (eq special t)
  18796. (when (or (> pos after-bullet) (= (point) pos))
  18797. (goto-char after-bullet))
  18798. ;; Reversed case: Move point to special position when
  18799. ;; point was already at beginning of line and command is
  18800. ;; repeated.
  18801. (when (and (= (point) pos) (eq last-command this-command))
  18802. (goto-char after-bullet))))))))
  18803. (org-no-warnings
  18804. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18805. (defun org-end-of-line (&optional arg)
  18806. "Go to the end of the line.
  18807. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18808. first attempt, and only move to after the tags when the cursor is already
  18809. beyond the end of the headline."
  18810. (interactive "P")
  18811. (let ((special (if (consp org-special-ctrl-a/e)
  18812. (cdr org-special-ctrl-a/e)
  18813. org-special-ctrl-a/e)))
  18814. (cond
  18815. ((or (not special) arg
  18816. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  18817. (call-interactively
  18818. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18819. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18820. (t 'end-of-line))))
  18821. ((org-at-heading-p)
  18822. (let ((pos (point)))
  18823. (beginning-of-line 1)
  18824. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18825. (if (eq special t)
  18826. (if (or (< pos (match-beginning 1))
  18827. (= pos (match-end 0)))
  18828. (goto-char (match-beginning 1))
  18829. (goto-char (match-end 0)))
  18830. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18831. (goto-char (match-end 0))
  18832. (goto-char (match-beginning 1))))
  18833. (call-interactively (if (fboundp 'move-end-of-line)
  18834. 'move-end-of-line
  18835. 'end-of-line)))))
  18836. ((org-at-drawer-p)
  18837. (move-end-of-line 1)
  18838. (when (overlays-at (1- (point))) (backward-char 1)))
  18839. ;; At an item: Move before any hidden text.
  18840. (t (call-interactively
  18841. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18842. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18843. (t 'end-of-line)))))
  18844. (org-no-warnings
  18845. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18846. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18847. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18848. (defun org-backward-sentence (&optional arg)
  18849. "Go to beginning of sentence, or beginning of table field.
  18850. This will call `backward-sentence' or `org-table-beginning-of-field',
  18851. depending on context."
  18852. (interactive "P")
  18853. (cond
  18854. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18855. (t (call-interactively 'backward-sentence))))
  18856. (defun org-forward-sentence (&optional arg)
  18857. "Go to end of sentence, or end of table field.
  18858. This will call `forward-sentence' or `org-table-end-of-field',
  18859. depending on context."
  18860. (interactive "P")
  18861. (cond
  18862. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18863. (t (call-interactively 'forward-sentence))))
  18864. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18865. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18866. (defun org-kill-line (&optional arg)
  18867. "Kill line, to tags or end of line."
  18868. (interactive "P")
  18869. (cond
  18870. ((or (not org-special-ctrl-k)
  18871. (bolp)
  18872. (not (org-at-heading-p)))
  18873. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18874. org-ctrl-k-protect-subtree)
  18875. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18876. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18877. (error "C-k aborted - would kill hidden subtree")))
  18878. (call-interactively 'kill-line))
  18879. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18880. (kill-region (point) (match-beginning 1))
  18881. (org-set-tags nil t))
  18882. (t (kill-region (point) (point-at-eol)))))
  18883. (define-key org-mode-map "\C-k" 'org-kill-line)
  18884. (defun org-yank (&optional arg)
  18885. "Yank. If the kill is a subtree, treat it specially.
  18886. This command will look at the current kill and check if is a single
  18887. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18888. cursor is at the beginning of a line or after the stars of a currently
  18889. empty headline, then the yank is handled specially. How exactly depends
  18890. on the value of the following variables, both set by default.
  18891. org-yank-folded-subtrees
  18892. When set, the subtree(s) will be folded after insertion, but only
  18893. if doing so would now swallow text after the yanked text.
  18894. org-yank-adjusted-subtrees
  18895. When set, the subtree will be promoted or demoted in order to
  18896. fit into the local outline tree structure, which means that the level
  18897. will be adjusted so that it becomes the smaller one of the two
  18898. *visible* surrounding headings.
  18899. Any prefix to this command will cause `yank' to be called directly with
  18900. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18901. will just
  18902. plainly yank the text as it is.
  18903. \[1] The test checks if the first non-white line is a heading
  18904. and if there are no other headings with fewer stars."
  18905. (interactive "P")
  18906. (org-yank-generic 'yank arg))
  18907. (defun org-yank-generic (command arg)
  18908. "Perform some yank-like command.
  18909. This function implements the behavior described in the `org-yank'
  18910. documentation. However, it has been generalized to work for any
  18911. interactive command with similar behavior."
  18912. ;; pretend to be command COMMAND
  18913. (setq this-command command)
  18914. (if arg
  18915. (call-interactively command)
  18916. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18917. (and (org-kill-is-subtree-p)
  18918. (or (bolp)
  18919. (and (looking-at "[ \t]*$")
  18920. (string-match
  18921. "\\`\\*+\\'"
  18922. (buffer-substring (point-at-bol) (point)))))))
  18923. swallowp)
  18924. (cond
  18925. ((and subtreep org-yank-folded-subtrees)
  18926. (let ((beg (point))
  18927. end)
  18928. (if (and subtreep org-yank-adjusted-subtrees)
  18929. (org-paste-subtree nil nil 'for-yank)
  18930. (call-interactively command))
  18931. (setq end (point))
  18932. (goto-char beg)
  18933. (when (and (bolp) subtreep
  18934. (not (setq swallowp
  18935. (org-yank-folding-would-swallow-text beg end))))
  18936. (org-with-limited-levels
  18937. (or (looking-at org-outline-regexp)
  18938. (re-search-forward org-outline-regexp-bol end t))
  18939. (while (and (< (point) end) (looking-at org-outline-regexp))
  18940. (hide-subtree)
  18941. (org-cycle-show-empty-lines 'folded)
  18942. (condition-case nil
  18943. (outline-forward-same-level 1)
  18944. (error (goto-char end))))))
  18945. (when swallowp
  18946. (message
  18947. "Inserted text not folded because that would swallow text"))
  18948. (goto-char end)
  18949. (skip-chars-forward " \t\n\r")
  18950. (beginning-of-line 1)
  18951. (push-mark beg 'nomsg)))
  18952. ((and subtreep org-yank-adjusted-subtrees)
  18953. (let ((beg (point-at-bol)))
  18954. (org-paste-subtree nil nil 'for-yank)
  18955. (push-mark beg 'nomsg)))
  18956. (t
  18957. (call-interactively command))))))
  18958. (defun org-yank-folding-would-swallow-text (beg end)
  18959. "Would hide-subtree at BEG swallow any text after END?"
  18960. (let (level)
  18961. (org-with-limited-levels
  18962. (save-excursion
  18963. (goto-char beg)
  18964. (when (or (looking-at org-outline-regexp)
  18965. (re-search-forward org-outline-regexp-bol end t))
  18966. (setq level (org-outline-level)))
  18967. (goto-char end)
  18968. (skip-chars-forward " \t\r\n\v\f")
  18969. (if (or (eobp)
  18970. (and (bolp) (looking-at org-outline-regexp)
  18971. (<= (org-outline-level) level)))
  18972. nil ; Nothing would be swallowed
  18973. t))))) ; something would swallow
  18974. (define-key org-mode-map "\C-y" 'org-yank)
  18975. (defun org-truely-invisible-p ()
  18976. "Check if point is at a character currently not visible.
  18977. This version does not only check the character property, but also
  18978. `visible-mode'."
  18979. ;; Early versions of noutline don't have `outline-invisible-p'.
  18980. (if (org-bound-and-true-p visible-mode)
  18981. nil
  18982. (outline-invisible-p)))
  18983. (defun org-invisible-p2 ()
  18984. "Check if point is at a character currently not visible."
  18985. (save-excursion
  18986. (if (and (eolp) (not (bobp))) (backward-char 1))
  18987. ;; Early versions of noutline don't have `outline-invisible-p'.
  18988. (outline-invisible-p)))
  18989. (defun org-back-to-heading (&optional invisible-ok)
  18990. "Call `outline-back-to-heading', but provide a better error message."
  18991. (condition-case nil
  18992. (outline-back-to-heading invisible-ok)
  18993. (error (error "Before first headline at position %d in buffer %s"
  18994. (point) (current-buffer)))))
  18995. (defun org-beginning-of-defun ()
  18996. "Go to the beginning of the subtree, i.e. back to the heading."
  18997. (org-back-to-heading))
  18998. (defun org-end-of-defun ()
  18999. "Go to the end of the subtree."
  19000. (org-end-of-subtree nil t))
  19001. (defun org-before-first-heading-p ()
  19002. "Before first heading?"
  19003. (save-excursion
  19004. (end-of-line)
  19005. (null (re-search-backward org-outline-regexp-bol nil t))))
  19006. (defun org-at-heading-p (&optional ignored)
  19007. (outline-on-heading-p t))
  19008. ;; Compatibility alias with Org versions < 7.8.03
  19009. (defalias 'org-on-heading-p 'org-at-heading-p)
  19010. (defun org-at-drawer-p nil
  19011. "Whether point is at a drawer."
  19012. (save-excursion
  19013. (move-beginning-of-line 1)
  19014. (looking-at org-drawer-regexp)))
  19015. (defun org-point-at-end-of-empty-headline ()
  19016. "If point is at the end of an empty headline, return t, else nil.
  19017. If the heading only contains a TODO keyword, it is still still considered
  19018. empty."
  19019. (and (looking-at "[ \t]*$")
  19020. (save-excursion
  19021. (beginning-of-line 1)
  19022. (let ((case-fold-search nil))
  19023. (looking-at org-todo-line-regexp)))
  19024. (string= (match-string 3) "")))
  19025. (defun org-at-heading-or-item-p ()
  19026. (or (org-at-heading-p) (org-at-item-p)))
  19027. (defun org-at-target-p ()
  19028. (or (org-in-regexp org-radio-target-regexp)
  19029. (org-in-regexp org-target-regexp)))
  19030. ;; Compatibility alias with Org versions < 7.8.03
  19031. (defalias 'org-on-target-p 'org-at-target-p)
  19032. (defun org-up-heading-all (arg)
  19033. "Move to the heading line of which the present line is a subheading.
  19034. This function considers both visible and invisible heading lines.
  19035. With argument, move up ARG levels."
  19036. (if (fboundp 'outline-up-heading-all)
  19037. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19038. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19039. (defun org-up-heading-safe ()
  19040. "Move to the heading line of which the present line is a subheading.
  19041. This version will not throw an error. It will return the level of the
  19042. headline found, or nil if no higher level is found.
  19043. Also, this function will be a lot faster than `outline-up-heading',
  19044. because it relies on stars being the outline starters. This can really
  19045. make a significant difference in outlines with very many siblings."
  19046. (let (start-level re)
  19047. (org-back-to-heading t)
  19048. (setq start-level (funcall outline-level))
  19049. (if (equal start-level 1)
  19050. nil
  19051. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19052. (if (re-search-backward re nil t)
  19053. (funcall outline-level)))))
  19054. (defun org-first-sibling-p ()
  19055. "Is this heading the first child of its parents?"
  19056. (interactive)
  19057. (let ((re org-outline-regexp-bol)
  19058. level l)
  19059. (unless (org-at-heading-p t)
  19060. (error "Not at a heading"))
  19061. (setq level (funcall outline-level))
  19062. (save-excursion
  19063. (if (not (re-search-backward re nil t))
  19064. t
  19065. (setq l (funcall outline-level))
  19066. (< l level)))))
  19067. (defun org-goto-sibling (&optional previous)
  19068. "Goto the next sibling, even if it is invisible.
  19069. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19070. when a sibling was found. When none is found, return nil and don't
  19071. move point."
  19072. (let ((fun (if previous 're-search-backward 're-search-forward))
  19073. (pos (point))
  19074. (re org-outline-regexp-bol)
  19075. level l)
  19076. (when (condition-case nil (org-back-to-heading t) (error nil))
  19077. (setq level (funcall outline-level))
  19078. (catch 'exit
  19079. (or previous (forward-char 1))
  19080. (while (funcall fun re nil t)
  19081. (setq l (funcall outline-level))
  19082. (when (< l level) (goto-char pos) (throw 'exit nil))
  19083. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19084. (goto-char pos)
  19085. nil))))
  19086. (defun org-show-siblings ()
  19087. "Show all siblings of the current headline."
  19088. (save-excursion
  19089. (while (org-goto-sibling) (org-flag-heading nil)))
  19090. (save-excursion
  19091. (while (org-goto-sibling 'previous)
  19092. (org-flag-heading nil))))
  19093. (defun org-goto-first-child ()
  19094. "Goto the first child, even if it is invisible.
  19095. Return t when a child was found. Otherwise don't move point and
  19096. return nil."
  19097. (let (level (pos (point)) (re org-outline-regexp-bol))
  19098. (when (condition-case nil (org-back-to-heading t) (error nil))
  19099. (setq level (outline-level))
  19100. (forward-char 1)
  19101. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19102. (progn (goto-char (match-beginning 0)) t)
  19103. (goto-char pos) nil))))
  19104. (defun org-show-hidden-entry ()
  19105. "Show an entry where even the heading is hidden."
  19106. (save-excursion
  19107. (org-show-entry)))
  19108. (defun org-flag-heading (flag &optional entry)
  19109. "Flag the current heading. FLAG non-nil means make invisible.
  19110. When ENTRY is non-nil, show the entire entry."
  19111. (save-excursion
  19112. (org-back-to-heading t)
  19113. ;; Check if we should show the entire entry
  19114. (if entry
  19115. (progn
  19116. (org-show-entry)
  19117. (save-excursion
  19118. (and (outline-next-heading)
  19119. (org-flag-heading nil))))
  19120. (outline-flag-region (max (point-min) (1- (point)))
  19121. (save-excursion (outline-end-of-heading) (point))
  19122. flag))))
  19123. (defun org-get-next-sibling ()
  19124. "Move to next heading of the same level, and return point.
  19125. If there is no such heading, return nil.
  19126. This is like outline-next-sibling, but invisible headings are ok."
  19127. (let ((level (funcall outline-level)))
  19128. (outline-next-heading)
  19129. (while (and (not (eobp)) (> (funcall outline-level) level))
  19130. (outline-next-heading))
  19131. (if (or (eobp) (< (funcall outline-level) level))
  19132. nil
  19133. (point))))
  19134. (defun org-get-last-sibling ()
  19135. "Move to previous heading of the same level, and return point.
  19136. If there is no such heading, return nil."
  19137. (let ((opoint (point))
  19138. (level (funcall outline-level)))
  19139. (outline-previous-heading)
  19140. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19141. (while (and (> (funcall outline-level) level)
  19142. (not (bobp)))
  19143. (outline-previous-heading))
  19144. (if (< (funcall outline-level) level)
  19145. nil
  19146. (point)))))
  19147. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19148. ;; This contains an exact copy of the original function, but it uses
  19149. ;; `org-back-to-heading', to make it work also in invisible
  19150. ;; trees. And is uses an invisible-OK argument.
  19151. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19152. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19153. ;; with many children and grandchildren etc, this can be much faster
  19154. ;; than the outline version.
  19155. (org-back-to-heading invisible-OK)
  19156. (let ((first t)
  19157. (level (funcall outline-level)))
  19158. (if (and (eq major-mode 'org-mode) (< level 1000))
  19159. ;; A true heading (not a plain list item), in Org-mode
  19160. ;; This means we can easily find the end by looking
  19161. ;; only for the right number of stars. Using a regexp to do
  19162. ;; this is so much faster than using a Lisp loop.
  19163. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19164. (forward-char 1)
  19165. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19166. ;; something else, do it the slow way
  19167. (while (and (not (eobp))
  19168. (or first (> (funcall outline-level) level)))
  19169. (setq first nil)
  19170. (outline-next-heading)))
  19171. (unless to-heading
  19172. (if (memq (preceding-char) '(?\n ?\^M))
  19173. (progn
  19174. ;; Go to end of line before heading
  19175. (forward-char -1)
  19176. (if (memq (preceding-char) '(?\n ?\^M))
  19177. ;; leave blank line before heading
  19178. (forward-char -1))))))
  19179. (point))
  19180. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19181. "Use Org version in org-mode, for dramatic speed-up."
  19182. (if (eq major-mode 'org-mode)
  19183. (progn
  19184. (org-end-of-subtree nil t)
  19185. (unless (eobp) (backward-char 1)))
  19186. ad-do-it))
  19187. (defun org-end-of-meta-data-and-drawers ()
  19188. "Jump to the first text after meta data and drawers in the current entry.
  19189. This will move over empty lines, lines with planning time stamps,
  19190. clocking lines, and drawers."
  19191. (org-back-to-heading t)
  19192. (let ((end (save-excursion (outline-next-heading) (point)))
  19193. (re (concat "\\(" org-drawer-regexp "\\)"
  19194. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19195. (forward-line 1)
  19196. (while (re-search-forward re end t)
  19197. (if (not (match-end 1))
  19198. ;; empty or planning line
  19199. (forward-line 1)
  19200. ;; a drawer, find the end
  19201. (re-search-forward "^[ \t]*:END:" end 'move)
  19202. (forward-line 1)))
  19203. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19204. (point)))
  19205. (defun org-forward-same-level (arg &optional invisible-ok)
  19206. "Move forward to the arg'th subheading at same level as this one.
  19207. Stop at the first and last subheadings of a superior heading.
  19208. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19209. it wil also look at invisible ones."
  19210. (interactive "p")
  19211. (org-back-to-heading invisible-ok)
  19212. (org-at-heading-p)
  19213. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19214. (re (format "^\\*\\{1,%d\\} " level))
  19215. l)
  19216. (forward-char 1)
  19217. (while (> arg 0)
  19218. (while (and (re-search-forward re nil 'move)
  19219. (setq l (- (match-end 0) (match-beginning 0) 1))
  19220. (= l level)
  19221. (not invisible-ok)
  19222. (progn (backward-char 1) (outline-invisible-p)))
  19223. (if (< l level) (setq arg 1)))
  19224. (setq arg (1- arg)))
  19225. (beginning-of-line 1)))
  19226. (defun org-backward-same-level (arg &optional invisible-ok)
  19227. "Move backward to the arg'th subheading at same level as this one.
  19228. Stop at the first and last subheadings of a superior heading."
  19229. (interactive "p")
  19230. (org-back-to-heading)
  19231. (org-at-heading-p)
  19232. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19233. (re (format "^\\*\\{1,%d\\} " level))
  19234. l)
  19235. (while (> arg 0)
  19236. (while (and (re-search-backward re nil 'move)
  19237. (setq l (- (match-end 0) (match-beginning 0) 1))
  19238. (= l level)
  19239. (not invisible-ok)
  19240. (outline-invisible-p))
  19241. (if (< l level) (setq arg 1)))
  19242. (setq arg (1- arg)))))
  19243. (defun org-show-subtree ()
  19244. "Show everything after this heading at deeper levels."
  19245. (outline-flag-region
  19246. (point)
  19247. (save-excursion
  19248. (org-end-of-subtree t t))
  19249. nil))
  19250. (defun org-show-entry ()
  19251. "Show the body directly following this heading.
  19252. Show the heading too, if it is currently invisible."
  19253. (interactive)
  19254. (save-excursion
  19255. (condition-case nil
  19256. (progn
  19257. (org-back-to-heading t)
  19258. (outline-flag-region
  19259. (max (point-min) (1- (point)))
  19260. (save-excursion
  19261. (if (re-search-forward
  19262. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19263. (match-beginning 1)
  19264. (point-max)))
  19265. nil)
  19266. (org-cycle-hide-drawers 'children))
  19267. (error nil))))
  19268. (defun org-make-options-regexp (kwds &optional extra)
  19269. "Make a regular expression for keyword lines."
  19270. (concat
  19271. "^"
  19272. "#?[ \t]*\\+\\("
  19273. (mapconcat 'regexp-quote kwds "\\|")
  19274. (if extra (concat "\\|" extra))
  19275. "\\):[ \t]*"
  19276. "\\(.*\\)"))
  19277. ;; Make isearch reveal the necessary context
  19278. (defun org-isearch-end ()
  19279. "Reveal context after isearch exits."
  19280. (when isearch-success ; only if search was successful
  19281. (if (featurep 'xemacs)
  19282. ;; Under XEmacs, the hook is run in the correct place,
  19283. ;; we directly show the context.
  19284. (org-show-context 'isearch)
  19285. ;; In Emacs the hook runs *before* restoring the overlays.
  19286. ;; So we have to use a one-time post-command-hook to do this.
  19287. ;; (Emacs 22 has a special variable, see function `org-mode')
  19288. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19289. isearch-mode-end-hook-quit)
  19290. ;; Only when the isearch was not quitted.
  19291. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19292. 'append 'local)))))
  19293. (defun org-isearch-post-command ()
  19294. "Remove self from hook, and show context."
  19295. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19296. (org-show-context 'isearch))
  19297. ;;;; Integration with and fixes for other packages
  19298. ;;; Imenu support
  19299. (defvar org-imenu-markers nil
  19300. "All markers currently used by Imenu.")
  19301. (make-variable-buffer-local 'org-imenu-markers)
  19302. (defun org-imenu-new-marker (&optional pos)
  19303. "Return a new marker for use by Imenu, and remember the marker."
  19304. (let ((m (make-marker)))
  19305. (move-marker m (or pos (point)))
  19306. (push m org-imenu-markers)
  19307. m))
  19308. (defun org-imenu-get-tree ()
  19309. "Produce the index for Imenu."
  19310. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19311. (setq org-imenu-markers nil)
  19312. (let* ((n org-imenu-depth)
  19313. (re (concat "^" (org-get-limited-outline-regexp)))
  19314. (subs (make-vector (1+ n) nil))
  19315. (last-level 0)
  19316. m level head)
  19317. (save-excursion
  19318. (save-restriction
  19319. (widen)
  19320. (goto-char (point-max))
  19321. (while (re-search-backward re nil t)
  19322. (setq level (org-reduced-level (funcall outline-level)))
  19323. (when (<= level n)
  19324. (looking-at org-complex-heading-regexp)
  19325. (setq head (org-link-display-format
  19326. (org-match-string-no-properties 4))
  19327. m (org-imenu-new-marker))
  19328. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19329. (if (>= level last-level)
  19330. (push (cons head m) (aref subs level))
  19331. (push (cons head (aref subs (1+ level))) (aref subs level))
  19332. (loop for i from (1+ level) to n do (aset subs i nil)))
  19333. (setq last-level level)))))
  19334. (aref subs 1)))
  19335. (eval-after-load "imenu"
  19336. '(progn
  19337. (add-hook 'imenu-after-jump-hook
  19338. (lambda ()
  19339. (if (eq major-mode 'org-mode)
  19340. (org-show-context 'org-goto))))))
  19341. (defun org-link-display-format (link)
  19342. "Replace a link with either the description, or the link target
  19343. if no description is present"
  19344. (save-match-data
  19345. (if (string-match org-bracket-link-analytic-regexp link)
  19346. (replace-match (if (match-end 5)
  19347. (match-string 5 link)
  19348. (concat (match-string 1 link)
  19349. (match-string 3 link)))
  19350. nil t link)
  19351. link)))
  19352. (defun org-toggle-link-display ()
  19353. "Toggle the literal or descriptive display of links."
  19354. (interactive)
  19355. (if org-descriptive-links
  19356. (progn (org-remove-from-invisibility-spec '(org-link))
  19357. (org-restart-font-lock)
  19358. (setq org-descriptive-links nil))
  19359. (progn (add-to-invisibility-spec '(org-link))
  19360. (org-restart-font-lock)
  19361. (setq org-descriptive-links t))))
  19362. ;; Speedbar support
  19363. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19364. "Overlay marking the agenda restriction line in speedbar.")
  19365. (overlay-put org-speedbar-restriction-lock-overlay
  19366. 'face 'org-agenda-restriction-lock)
  19367. (overlay-put org-speedbar-restriction-lock-overlay
  19368. 'help-echo "Agendas are currently limited to this item.")
  19369. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19370. (defun org-speedbar-set-agenda-restriction ()
  19371. "Restrict future agenda commands to the location at point in speedbar.
  19372. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19373. (interactive)
  19374. (require 'org-agenda)
  19375. (let (p m tp np dir txt)
  19376. (cond
  19377. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19378. 'org-imenu t))
  19379. (setq m (get-text-property p 'org-imenu-marker))
  19380. (with-current-buffer (marker-buffer m)
  19381. (goto-char m)
  19382. (org-agenda-set-restriction-lock 'subtree)))
  19383. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19384. 'speedbar-function 'speedbar-find-file))
  19385. (setq tp (previous-single-property-change
  19386. (1+ p) 'speedbar-function)
  19387. np (next-single-property-change
  19388. tp 'speedbar-function)
  19389. dir (speedbar-line-directory)
  19390. txt (buffer-substring-no-properties (or tp (point-min))
  19391. (or np (point-max))))
  19392. (with-current-buffer (find-file-noselect
  19393. (let ((default-directory dir))
  19394. (expand-file-name txt)))
  19395. (unless (eq major-mode 'org-mode)
  19396. (error "Cannot restrict to non-Org-mode file"))
  19397. (org-agenda-set-restriction-lock 'file)))
  19398. (t (error "Don't know how to restrict Org-mode's agenda")))
  19399. (move-overlay org-speedbar-restriction-lock-overlay
  19400. (point-at-bol) (point-at-eol))
  19401. (setq current-prefix-arg nil)
  19402. (org-agenda-maybe-redo)))
  19403. (eval-after-load "speedbar"
  19404. '(progn
  19405. (speedbar-add-supported-extension ".org")
  19406. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19407. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19408. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19409. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19410. (add-hook 'speedbar-visiting-tag-hook
  19411. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19412. ;;; Fixes and Hacks for problems with other packages
  19413. ;; Make flyspell not check words in links, to not mess up our keymap
  19414. (defun org-mode-flyspell-verify ()
  19415. "Don't let flyspell put overlays at active buttons, or on
  19416. {todo,all-time,additional-option-like}-keywords."
  19417. (let ((pos (max (1- (point)) (point-min)))
  19418. (word (thing-at-point 'word)))
  19419. (and (not (get-text-property pos 'keymap))
  19420. (not (get-text-property pos 'org-no-flyspell))
  19421. (not (member word org-todo-keywords-1))
  19422. (not (member word org-all-time-keywords))
  19423. (not (member word org-additional-option-like-keywords)))))
  19424. (defun org-remove-flyspell-overlays-in (beg end)
  19425. "Remove flyspell overlays in region."
  19426. (and (org-bound-and-true-p flyspell-mode)
  19427. (fboundp 'flyspell-delete-region-overlays)
  19428. (flyspell-delete-region-overlays beg end))
  19429. (add-text-properties beg end '(org-no-flyspell t)))
  19430. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19431. (eval-after-load "bookmark"
  19432. '(if (boundp 'bookmark-after-jump-hook)
  19433. ;; We can use the hook
  19434. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19435. ;; Hook not available, use advice
  19436. (defadvice bookmark-jump (after org-make-visible activate)
  19437. "Make the position visible."
  19438. (org-bookmark-jump-unhide))))
  19439. ;; Make sure saveplace shows the location if it was hidden
  19440. (eval-after-load "saveplace"
  19441. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19442. "Make the position visible."
  19443. (org-bookmark-jump-unhide)))
  19444. ;; Make sure ecb shows the location if it was hidden
  19445. (eval-after-load "ecb"
  19446. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19447. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19448. (if (eq major-mode 'org-mode)
  19449. (org-show-context))))
  19450. (defun org-bookmark-jump-unhide ()
  19451. "Unhide the current position, to show the bookmark location."
  19452. (and (eq major-mode 'org-mode)
  19453. (or (outline-invisible-p)
  19454. (save-excursion (goto-char (max (point-min) (1- (point))))
  19455. (outline-invisible-p)))
  19456. (org-show-context 'bookmark-jump)))
  19457. ;; Make session.el ignore our circular variable
  19458. (eval-after-load "session"
  19459. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19460. ;;;; Experimental code
  19461. (defun org-closed-in-range ()
  19462. "Sparse tree of items closed in a certain time range.
  19463. Still experimental, may disappear in the future."
  19464. (interactive)
  19465. ;; Get the time interval from the user.
  19466. (let* ((time1 (org-float-time
  19467. (org-read-date nil 'to-time nil "Starting date: ")))
  19468. (time2 (org-float-time
  19469. (org-read-date nil 'to-time nil "End date:")))
  19470. ;; callback function
  19471. (callback (lambda ()
  19472. (let ((time
  19473. (org-float-time
  19474. (apply 'encode-time
  19475. (org-parse-time-string
  19476. (match-string 1))))))
  19477. ;; check if time in interval
  19478. (and (>= time time1) (<= time time2))))))
  19479. ;; make tree, check each match with the callback
  19480. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19481. ;;;; Finish up
  19482. (provide 'org)
  19483. (run-hooks 'org-load-hook)
  19484. ;;; org.el ends here