org.el 806 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.8.06
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. (require 'format-spec)
  73. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  74. (when (fboundp 'defvaralias)
  75. (unless (boundp 'calendar-view-holidays-initially-flag)
  76. (defvaralias 'calendar-view-holidays-initially-flag
  77. 'view-calendar-holidays-initially))
  78. (unless (boundp 'calendar-view-diary-initially-flag)
  79. (defvaralias 'calendar-view-diary-initially-flag
  80. 'view-diary-entries-initially))
  81. (unless (boundp 'diary-fancy-buffer)
  82. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  83. (require 'outline) (require 'noutline)
  84. ;; Other stuff we need.
  85. (require 'time-date)
  86. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  87. (require 'easymenu)
  88. (require 'overlay)
  89. (require 'org-macs)
  90. (require 'org-entities)
  91. (require 'org-compat)
  92. (require 'org-faces)
  93. (require 'org-list)
  94. (require 'org-pcomplete)
  95. (require 'org-src)
  96. (require 'org-footnote)
  97. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  98. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  99. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  100. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  101. (declare-function org-at-clock-log-p "org-clock" ())
  102. (declare-function org-clock-timestamps-up "org-clock" ())
  103. (declare-function org-clock-timestamps-down "org-clock" ())
  104. ;; babel
  105. (require 'ob)
  106. (require 'ob-table)
  107. (require 'ob-lob)
  108. (require 'ob-ref)
  109. (require 'ob-tangle)
  110. (require 'ob-comint)
  111. (require 'ob-keys)
  112. ;; load languages based on value of `org-babel-load-languages'
  113. (defvar org-babel-load-languages)
  114. ;;;###autoload
  115. (defun org-babel-do-load-languages (sym value)
  116. "Load the languages defined in `org-babel-load-languages'."
  117. (set-default sym value)
  118. (mapc (lambda (pair)
  119. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  120. (if active
  121. (progn
  122. (require (intern (concat "ob-" lang))))
  123. (progn
  124. (funcall 'fmakunbound
  125. (intern (concat "org-babel-execute:" lang)))
  126. (funcall 'fmakunbound
  127. (intern (concat "org-babel-expand-body:" lang)))))))
  128. org-babel-load-languages))
  129. (defcustom org-babel-load-languages '((emacs-lisp . t))
  130. "Languages which can be evaluated in Org-mode buffers.
  131. This list can be used to load support for any of the languages
  132. below, note that each language will depend on a different set of
  133. system executables and/or Emacs modes. When a language is
  134. \"loaded\", then code blocks in that language can be evaluated
  135. with `org-babel-execute-src-block' bound by default to C-c
  136. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  137. be set to remove code block evaluation from the C-c C-c
  138. keybinding. By default only Emacs Lisp (which has no
  139. requirements) is loaded."
  140. :group 'org-babel
  141. :set 'org-babel-do-load-languages
  142. :version "24.1"
  143. :type '(alist :tag "Babel Languages"
  144. :key-type
  145. (choice
  146. (const :tag "Awk" awk)
  147. (const :tag "C" C)
  148. (const :tag "R" R)
  149. (const :tag "Asymptote" asymptote)
  150. (const :tag "Calc" calc)
  151. (const :tag "Clojure" clojure)
  152. (const :tag "CSS" css)
  153. (const :tag "Ditaa" ditaa)
  154. (const :tag "Dot" dot)
  155. (const :tag "Emacs Lisp" emacs-lisp)
  156. (const :tag "Fortran" fortran)
  157. (const :tag "Gnuplot" gnuplot)
  158. (const :tag "Haskell" haskell)
  159. (const :tag "IO" io)
  160. (const :tag "Java" java)
  161. (const :tag "Javascript" js)
  162. (const :tag "LaTeX" latex)
  163. (const :tag "Ledger" ledger)
  164. (const :tag "Lilypond" lilypond)
  165. (const :tag "Maxima" maxima)
  166. (const :tag "Matlab" matlab)
  167. (const :tag "Mscgen" mscgen)
  168. (const :tag "Ocaml" ocaml)
  169. (const :tag "Octave" octave)
  170. (const :tag "Org" org)
  171. (const :tag "Perl" perl)
  172. (const :tag "Pico Lisp" picolisp)
  173. (const :tag "PlantUML" plantuml)
  174. (const :tag "Python" python)
  175. (const :tag "Ruby" ruby)
  176. (const :tag "Sass" sass)
  177. (const :tag "Scala" scala)
  178. (const :tag "Scheme" scheme)
  179. (const :tag "Screen" screen)
  180. (const :tag "Shell Script" sh)
  181. (const :tag "Shen" shen)
  182. (const :tag "Sql" sql)
  183. (const :tag "Sqlite" sqlite))
  184. :value-type (boolean :tag "Activate" :value t)))
  185. ;;;; Customization variables
  186. (defcustom org-clone-delete-id nil
  187. "Remove ID property of clones of a subtree.
  188. When non-nil, clones of a subtree don't inherit the ID property.
  189. Otherwise they inherit the ID property with a new unique
  190. identifier."
  191. :type 'boolean
  192. :version "24.1"
  193. :group 'org-id)
  194. ;;; Version
  195. (defconst org-version "7.8.06"
  196. "The version number of the file org.el.")
  197. ;;;###autoload
  198. (defun org-version (&optional here)
  199. "Show the org-mode version in the echo area.
  200. With prefix arg HERE, insert it at point."
  201. (interactive "P")
  202. (let* ((origin default-directory)
  203. (version org-version)
  204. (git-version)
  205. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  206. (when (and (file-exists-p (expand-file-name ".git" dir))
  207. (executable-find "git"))
  208. (unwind-protect
  209. (progn
  210. (cd dir)
  211. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  212. (with-current-buffer "*Shell Command Output*"
  213. (goto-char (point-min))
  214. (setq git-version (buffer-substring (point) (point-at-eol))))
  215. (subst-char-in-string ?- ?. git-version t)
  216. (when (string-match "\\S-"
  217. (shell-command-to-string
  218. "git diff-index --name-only HEAD --"))
  219. (setq git-version (concat git-version ".dirty")))
  220. (setq version (concat version " (" git-version ")"))))
  221. (cd origin)))
  222. (setq version (format "Org-mode version %s" version))
  223. (if here (insert version))
  224. (message version)))
  225. ;;; Compatibility constants
  226. ;;; The custom variables
  227. (defgroup org nil
  228. "Outline-based notes management and organizer."
  229. :tag "Org"
  230. :group 'outlines
  231. :group 'calendar)
  232. (defcustom org-mode-hook nil
  233. "Mode hook for Org-mode, run after the mode was turned on."
  234. :group 'org
  235. :type 'hook)
  236. (defcustom org-load-hook nil
  237. "Hook that is run after org.el has been loaded."
  238. :group 'org
  239. :type 'hook)
  240. (defcustom org-log-buffer-setup-hook nil
  241. "Hook that is run after an Org log buffer is created."
  242. :group 'org
  243. :version "24.1"
  244. :type 'hook)
  245. (defvar org-modules) ; defined below
  246. (defvar org-modules-loaded nil
  247. "Have the modules been loaded already?")
  248. (defun org-load-modules-maybe (&optional force)
  249. "Load all extensions listed in `org-modules'."
  250. (when (or force (not org-modules-loaded))
  251. (mapc (lambda (ext)
  252. (condition-case nil (require ext)
  253. (error (message "Problems while trying to load feature `%s'" ext))))
  254. org-modules)
  255. (setq org-modules-loaded t)))
  256. (defun org-set-modules (var value)
  257. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  258. (set var value)
  259. (when (featurep 'org)
  260. (org-load-modules-maybe 'force)))
  261. (when (org-bound-and-true-p org-modules)
  262. (let ((a (member 'org-infojs org-modules)))
  263. (and a (setcar a 'org-jsinfo))))
  264. (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)
  265. "Modules that should always be loaded together with org.el.
  266. If a description starts with <C>, the file is not part of Emacs
  267. and loading it will require that you have downloaded and properly installed
  268. the org-mode distribution.
  269. You can also use this system to load external packages (i.e. neither Org
  270. core modules, nor modules from the CONTRIB directory). Just add symbols
  271. to the end of the list. If the package is called org-xyz.el, then you need
  272. to add the symbol `xyz', and the package must have a call to
  273. (provide 'org-xyz)"
  274. :group 'org
  275. :set 'org-set-modules
  276. :type
  277. '(set :greedy t
  278. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  279. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  280. (const :tag " crypt: Encryption of subtrees" org-crypt)
  281. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  282. (const :tag " docview: Links to doc-view buffers" org-docview)
  283. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  284. (const :tag " id: Global IDs for identifying entries" org-id)
  285. (const :tag " info: Links to Info nodes" org-info)
  286. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  287. (const :tag " habit: Track your consistency with habits" org-habit)
  288. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  289. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  290. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  291. (const :tag " mew Links to Mew folders/messages" org-mew)
  292. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  293. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  294. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  295. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  296. (const :tag " vm: Links to VM folders/messages" org-vm)
  297. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  298. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  299. (const :tag " mouse: Additional mouse support" org-mouse)
  300. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  301. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  302. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  303. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  304. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  305. (const :tag "C collector: Collect properties into tables" org-collector)
  306. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  307. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  308. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  309. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  310. (const :tag "C eval: Include command output as text" org-eval)
  311. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  312. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  313. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  314. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  315. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  316. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  317. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  318. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  319. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  320. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  321. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  322. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  323. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  324. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  325. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  326. (const :tag "C registry: A registry for Org-mode links" org-registry)
  327. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  328. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  329. (const :tag "C secretary: Team management with org-mode" org-secretary)
  330. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  331. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  332. (const :tag "C track: Keep up with Org-mode development" org-track)
  333. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  334. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  335. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  336. (defcustom org-support-shift-select nil
  337. "Non-nil means make shift-cursor commands select text when possible.
  338. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  339. start selecting a region, or enlarge regions started in this way.
  340. In Org-mode, in special contexts, these same keys are used for
  341. other purposes, important enough to compete with shift selection.
  342. Org tries to balance these needs by supporting `shift-select-mode'
  343. outside these special contexts, under control of this variable.
  344. The default of this variable is nil, to avoid confusing behavior. Shifted
  345. cursor keys will then execute Org commands in the following contexts:
  346. - on a headline, changing TODO state (left/right) and priority (up/down)
  347. - on a time stamp, changing the time
  348. - in a plain list item, changing the bullet type
  349. - in a property definition line, switching between allowed values
  350. - in the BEGIN line of a clock table (changing the time block).
  351. Outside these contexts, the commands will throw an error.
  352. When this variable is t and the cursor is not in a special
  353. context, Org-mode will support shift-selection for making and
  354. enlarging regions. To make this more effective, the bullet
  355. cycling will no longer happen anywhere in an item line, but only
  356. if the cursor is exactly on the bullet.
  357. If you set this variable to the symbol `always', then the keys
  358. will not be special in headlines, property lines, and item lines,
  359. to make shift selection work there as well. If this is what you
  360. want, you can use the following alternative commands: `C-c C-t'
  361. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  362. can be used to switch TODO sets, `C-c -' to cycle item bullet
  363. types, and properties can be edited by hand or in column view.
  364. However, when the cursor is on a timestamp, shift-cursor commands
  365. will still edit the time stamp - this is just too good to give up.
  366. XEmacs user should have this variable set to nil, because
  367. `shift-select-mode' is in Emacs 23 or later only."
  368. :group 'org
  369. :type '(choice
  370. (const :tag "Never" nil)
  371. (const :tag "When outside special context" t)
  372. (const :tag "Everywhere except timestamps" always)))
  373. (defcustom org-loop-over-headlines-in-active-region nil
  374. "Shall some commands act upon headlines in the active region?
  375. When set to `t', some commands will be performed in all headlines
  376. within the active region.
  377. When set to `start-level', some commands will be performed in all
  378. headlines within the active region, provided that these headlines
  379. are of the same level than the first one.
  380. When set to a string, those commands will be performed on the
  381. matching headlines within the active region. Such string must be
  382. a tags/property/todo match as it is used in the agenda tags view.
  383. The list of commands is: `org-schedule', `org-deadline',
  384. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  385. `org-archive-to-archive-sibling'. The archiving commands skip
  386. already archived entries."
  387. :type '(choice (const :tag "Don't loop" nil)
  388. (const :tag "All headlines in active region" t)
  389. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  390. (string :tag "Tags/Property/Todo matcher"))
  391. :version "24.1"
  392. :group 'org-todo
  393. :group 'org-archive)
  394. (defgroup org-startup nil
  395. "Options concerning startup of Org-mode."
  396. :tag "Org Startup"
  397. :group 'org)
  398. (defcustom org-startup-folded t
  399. "Non-nil means entering Org-mode will switch to OVERVIEW.
  400. This can also be configured on a per-file basis by adding one of
  401. the following lines anywhere in the buffer:
  402. #+STARTUP: fold (or `overview', this is equivalent)
  403. #+STARTUP: nofold (or `showall', this is equivalent)
  404. #+STARTUP: content
  405. #+STARTUP: showeverything"
  406. :group 'org-startup
  407. :type '(choice
  408. (const :tag "nofold: show all" nil)
  409. (const :tag "fold: overview" t)
  410. (const :tag "content: all headlines" content)
  411. (const :tag "show everything, even drawers" showeverything)))
  412. (defcustom org-startup-truncated t
  413. "Non-nil means entering Org-mode will set `truncate-lines'.
  414. This is useful since some lines containing links can be very long and
  415. uninteresting. Also tables look terrible when wrapped."
  416. :group 'org-startup
  417. :type 'boolean)
  418. (defcustom org-startup-indented nil
  419. "Non-nil means turn on `org-indent-mode' on startup.
  420. This can also be configured on a per-file basis by adding one of
  421. the following lines anywhere in the buffer:
  422. #+STARTUP: indent
  423. #+STARTUP: noindent"
  424. :group 'org-structure
  425. :type '(choice
  426. (const :tag "Not" nil)
  427. (const :tag "Globally (slow on startup in large files)" t)))
  428. (defcustom org-use-sub-superscripts t
  429. "Non-nil means interpret \"_\" and \"^\" for export.
  430. When this option is turned on, you can use TeX-like syntax for sub- and
  431. superscripts. Several characters after \"_\" or \"^\" will be
  432. considered as a single item - so grouping with {} is normally not
  433. needed. For example, the following things will be parsed as single
  434. sub- or superscripts.
  435. 10^24 or 10^tau several digits will be considered 1 item.
  436. 10^-12 or 10^-tau a leading sign with digits or a word
  437. x^2-y^3 will be read as x^2 - y^3, because items are
  438. terminated by almost any nonword/nondigit char.
  439. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  440. Still, ambiguity is possible - so when in doubt use {} to enclose the
  441. sub/superscript. If you set this variable to the symbol `{}',
  442. the braces are *required* in order to trigger interpretations as
  443. sub/superscript. This can be helpful in documents that need \"_\"
  444. frequently in plain text.
  445. Not all export backends support this, but HTML does.
  446. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  447. :group 'org-startup
  448. :group 'org-export-translation
  449. :version "24.1"
  450. :type '(choice
  451. (const :tag "Always interpret" t)
  452. (const :tag "Only with braces" {})
  453. (const :tag "Never interpret" nil)))
  454. (if (fboundp 'defvaralias)
  455. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  456. (defcustom org-startup-with-beamer-mode nil
  457. "Non-nil means turn on `org-beamer-mode' on startup.
  458. This can also be configured on a per-file basis by adding one of
  459. the following lines anywhere in the buffer:
  460. #+STARTUP: beamer"
  461. :group 'org-startup
  462. :version "24.1"
  463. :type 'boolean)
  464. (defcustom org-startup-align-all-tables nil
  465. "Non-nil means align all tables when visiting a file.
  466. This is useful when the column width in tables is forced with <N> cookies
  467. in table fields. Such tables will look correct only after the first re-align.
  468. This can also be configured on a per-file basis by adding one of
  469. the following lines anywhere in the buffer:
  470. #+STARTUP: align
  471. #+STARTUP: noalign"
  472. :group 'org-startup
  473. :type 'boolean)
  474. (defcustom org-startup-with-inline-images nil
  475. "Non-nil means show inline images when loading a new Org file.
  476. This can also be configured on a per-file basis by adding one of
  477. the following lines anywhere in the buffer:
  478. #+STARTUP: inlineimages
  479. #+STARTUP: noinlineimages"
  480. :group 'org-startup
  481. :version "24.1"
  482. :type 'boolean)
  483. (defcustom org-insert-mode-line-in-empty-file nil
  484. "Non-nil means insert the first line setting Org-mode in empty files.
  485. When the function `org-mode' is called interactively in an empty file, this
  486. normally means that the file name does not automatically trigger Org-mode.
  487. To ensure that the file will always be in Org-mode in the future, a
  488. line enforcing Org-mode will be inserted into the buffer, if this option
  489. has been set."
  490. :group 'org-startup
  491. :type 'boolean)
  492. (defcustom org-replace-disputed-keys nil
  493. "Non-nil means use alternative key bindings for some keys.
  494. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  495. These keys are also used by other packages like shift-selection-mode'
  496. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  497. If you want to use Org-mode together with one of these other modes,
  498. or more generally if you would like to move some Org-mode commands to
  499. other keys, set this variable and configure the keys with the variable
  500. `org-disputed-keys'.
  501. This option is only relevant at load-time of Org-mode, and must be set
  502. *before* org.el is loaded. Changing it requires a restart of Emacs to
  503. become effective."
  504. :group 'org-startup
  505. :type 'boolean)
  506. (defcustom org-use-extra-keys nil
  507. "Non-nil means use extra key sequence definitions for certain commands.
  508. This happens automatically if you run XEmacs or if `window-system'
  509. is nil. This variable lets you do the same manually. You must
  510. set it before loading org.
  511. Example: on Carbon Emacs 22 running graphically, with an external
  512. keyboard on a Powerbook, the default way of setting M-left might
  513. not work for either Alt or ESC. Setting this variable will make
  514. it work for ESC."
  515. :group 'org-startup
  516. :type 'boolean)
  517. (if (fboundp 'defvaralias)
  518. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  519. (defcustom org-disputed-keys
  520. '(([(shift up)] . [(meta p)])
  521. ([(shift down)] . [(meta n)])
  522. ([(shift left)] . [(meta -)])
  523. ([(shift right)] . [(meta +)])
  524. ([(control shift right)] . [(meta shift +)])
  525. ([(control shift left)] . [(meta shift -)]))
  526. "Keys for which Org-mode and other modes compete.
  527. This is an alist, cars are the default keys, second element specifies
  528. the alternative to use when `org-replace-disputed-keys' is t.
  529. Keys can be specified in any syntax supported by `define-key'.
  530. The value of this option takes effect only at Org-mode's startup,
  531. therefore you'll have to restart Emacs to apply it after changing."
  532. :group 'org-startup
  533. :type 'alist)
  534. (defun org-key (key)
  535. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  536. Or return the original if not disputed.
  537. Also apply the translations defined in `org-xemacs-key-equivalents'."
  538. (when org-replace-disputed-keys
  539. (let* ((nkey (key-description key))
  540. (x (org-find-if (lambda (x)
  541. (equal (key-description (car x)) nkey))
  542. org-disputed-keys)))
  543. (setq key (if x (cdr x) key))))
  544. (when (featurep 'xemacs)
  545. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  546. key)
  547. (defun org-find-if (predicate seq)
  548. (catch 'exit
  549. (while seq
  550. (if (funcall predicate (car seq))
  551. (throw 'exit (car seq))
  552. (pop seq)))))
  553. (defun org-defkey (keymap key def)
  554. "Define a key, possibly translated, as returned by `org-key'."
  555. (define-key keymap (org-key key) def))
  556. (defcustom org-ellipsis nil
  557. "The ellipsis to use in the Org-mode outline.
  558. When nil, just use the standard three dots. When a string, use that instead,
  559. When a face, use the standard 3 dots, but with the specified face.
  560. The change affects only Org-mode (which will then use its own display table).
  561. Changing this requires executing `M-x org-mode' in a buffer to become
  562. effective."
  563. :group 'org-startup
  564. :type '(choice (const :tag "Default" nil)
  565. (face :tag "Face" :value org-warning)
  566. (string :tag "String" :value "...#")))
  567. (defvar org-display-table nil
  568. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  569. (defgroup org-keywords nil
  570. "Keywords in Org-mode."
  571. :tag "Org Keywords"
  572. :group 'org)
  573. (defcustom org-deadline-string "DEADLINE:"
  574. "String to mark deadline entries.
  575. A deadline is this string, followed by a time stamp. Should be a word,
  576. terminated by a colon. You can insert a schedule keyword and
  577. a timestamp with \\[org-deadline].
  578. Changes become only effective after restarting Emacs."
  579. :group 'org-keywords
  580. :type 'string)
  581. (defcustom org-scheduled-string "SCHEDULED:"
  582. "String to mark scheduled TODO entries.
  583. A schedule is this string, followed by a time stamp. Should be a word,
  584. terminated by a colon. You can insert a schedule keyword and
  585. a timestamp with \\[org-schedule].
  586. Changes become only effective after restarting Emacs."
  587. :group 'org-keywords
  588. :type 'string)
  589. (defcustom org-closed-string "CLOSED:"
  590. "String used as the prefix for timestamps logging closing a TODO entry."
  591. :group 'org-keywords
  592. :type 'string)
  593. (defcustom org-clock-string "CLOCK:"
  594. "String used as prefix for timestamps clocking work hours on an item."
  595. :group 'org-keywords
  596. :type 'string)
  597. (defcustom org-comment-string "COMMENT"
  598. "Entries starting with this keyword will never be exported.
  599. An entry can be toggled between COMMENT and normal with
  600. \\[org-toggle-comment].
  601. Changes become only effective after restarting Emacs."
  602. :group 'org-keywords
  603. :type 'string)
  604. (defcustom org-quote-string "QUOTE"
  605. "Entries starting with this keyword will be exported in fixed-width font.
  606. Quoting applies only to the text in the entry following the headline, and does
  607. not extend beyond the next headline, even if that is lower level.
  608. An entry can be toggled between QUOTE and normal with
  609. \\[org-toggle-fixed-width-section]."
  610. :group 'org-keywords
  611. :type 'string)
  612. (defconst org-repeat-re
  613. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  614. "Regular expression for specifying repeated events.
  615. After a match, group 1 contains the repeat expression.")
  616. (defgroup org-structure nil
  617. "Options concerning the general structure of Org-mode files."
  618. :tag "Org Structure"
  619. :group 'org)
  620. (defgroup org-reveal-location nil
  621. "Options about how to make context of a location visible."
  622. :tag "Org Reveal Location"
  623. :group 'org-structure)
  624. (defconst org-context-choice
  625. '(choice
  626. (const :tag "Always" t)
  627. (const :tag "Never" nil)
  628. (repeat :greedy t :tag "Individual contexts"
  629. (cons
  630. (choice :tag "Context"
  631. (const agenda)
  632. (const org-goto)
  633. (const occur-tree)
  634. (const tags-tree)
  635. (const link-search)
  636. (const mark-goto)
  637. (const bookmark-jump)
  638. (const isearch)
  639. (const default))
  640. (boolean))))
  641. "Contexts for the reveal options.")
  642. (defcustom org-show-hierarchy-above '((default . t))
  643. "Non-nil means show full hierarchy when revealing a location.
  644. Org-mode often shows locations in an org-mode file which might have
  645. been invisible before. When this is set, the hierarchy of headings
  646. above the exposed location is shown.
  647. Turning this off for example for sparse trees makes them very compact.
  648. Instead of t, this can also be an alist specifying this option for different
  649. contexts. Valid contexts are
  650. agenda when exposing an entry from the agenda
  651. org-goto when using the command `org-goto' on key C-c C-j
  652. occur-tree when using the command `org-occur' on key C-c /
  653. tags-tree when constructing a sparse tree based on tags matches
  654. link-search when exposing search matches associated with a link
  655. mark-goto when exposing the jump goal of a mark
  656. bookmark-jump when exposing a bookmark location
  657. isearch when exiting from an incremental search
  658. default default for all contexts not set explicitly"
  659. :group 'org-reveal-location
  660. :type org-context-choice)
  661. (defcustom org-show-following-heading '((default . nil))
  662. "Non-nil means show following heading when revealing a location.
  663. Org-mode often shows locations in an org-mode file which might have
  664. been invisible before. When this is set, the heading following the
  665. match is shown.
  666. Turning this off for example for sparse trees makes them very compact,
  667. but makes it harder to edit the location of the match. In such a case,
  668. use the command \\[org-reveal] to show more context.
  669. Instead of t, this can also be an alist specifying this option for different
  670. contexts. See `org-show-hierarchy-above' for valid contexts."
  671. :group 'org-reveal-location
  672. :type org-context-choice)
  673. (defcustom org-show-siblings '((default . nil) (isearch t))
  674. "Non-nil means show all sibling heading when revealing a location.
  675. Org-mode often shows locations in an org-mode file which might have
  676. been invisible before. When this is set, the sibling of the current entry
  677. heading are all made visible. If `org-show-hierarchy-above' is t,
  678. the same happens on each level of the hierarchy above the current entry.
  679. By default this is on for the isearch context, off for all other contexts.
  680. Turning this off for example for sparse trees makes them very compact,
  681. but makes it harder to edit the location of the match. In such a case,
  682. use the command \\[org-reveal] to show more context.
  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-show-entry-below '((default . nil))
  688. "Non-nil means show the entry below a headline when revealing a location.
  689. Org-mode often shows locations in an org-mode file which might have
  690. been invisible before. When this is set, the text below the headline that is
  691. exposed is also shown.
  692. By default this is off for all contexts.
  693. Instead of t, this can also be an alist specifying this option for different
  694. contexts. See `org-show-hierarchy-above' for valid contexts."
  695. :group 'org-reveal-location
  696. :type org-context-choice)
  697. (defcustom org-indirect-buffer-display 'other-window
  698. "How should indirect tree buffers be displayed?
  699. This applies to indirect buffers created with the commands
  700. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  701. Valid values are:
  702. current-window Display in the current window
  703. other-window Just display in another window.
  704. dedicated-frame Create one new frame, and re-use it each time.
  705. new-frame Make a new frame each time. Note that in this case
  706. previously-made indirect buffers are kept, and you need to
  707. kill these buffers yourself."
  708. :group 'org-structure
  709. :group 'org-agenda-windows
  710. :type '(choice
  711. (const :tag "In current window" current-window)
  712. (const :tag "In current frame, other window" other-window)
  713. (const :tag "Each time a new frame" new-frame)
  714. (const :tag "One dedicated frame" dedicated-frame)))
  715. (defcustom org-use-speed-commands nil
  716. "Non-nil means activate single letter commands at beginning of a headline.
  717. This may also be a function to test for appropriate locations where speed
  718. commands should be active."
  719. :group 'org-structure
  720. :type '(choice
  721. (const :tag "Never" nil)
  722. (const :tag "At beginning of headline stars" t)
  723. (function)))
  724. (defcustom org-speed-commands-user nil
  725. "Alist of additional speed commands.
  726. This list will be checked before `org-speed-commands-default'
  727. when the variable `org-use-speed-commands' is non-nil
  728. and when the cursor is at the beginning of a headline.
  729. The car if each entry is a string with a single letter, which must
  730. be assigned to `self-insert-command' in the global map.
  731. The cdr is either a command to be called interactively, a function
  732. to be called, or a form to be evaluated.
  733. An entry that is just a list with a single string will be interpreted
  734. as a descriptive headline that will be added when listing the speed
  735. commands in the Help buffer using the `?' speed command."
  736. :group 'org-structure
  737. :type '(repeat :value ("k" . ignore)
  738. (choice :value ("k" . ignore)
  739. (list :tag "Descriptive Headline" (string :tag "Headline"))
  740. (cons :tag "Letter and Command"
  741. (string :tag "Command letter")
  742. (choice
  743. (function)
  744. (sexp))))))
  745. (defgroup org-cycle nil
  746. "Options concerning visibility cycling in Org-mode."
  747. :tag "Org Cycle"
  748. :group 'org-structure)
  749. (defcustom org-cycle-skip-children-state-if-no-children t
  750. "Non-nil means skip CHILDREN state in entries that don't have any."
  751. :group 'org-cycle
  752. :type 'boolean)
  753. (defcustom org-cycle-max-level nil
  754. "Maximum level which should still be subject to visibility cycling.
  755. Levels higher than this will, for cycling, be treated as text, not a headline.
  756. When `org-odd-levels-only' is set, a value of N in this variable actually
  757. means 2N-1 stars as the limiting headline.
  758. When nil, cycle all levels.
  759. Note that the limiting level of cycling is also influenced by
  760. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  761. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  762. than its value."
  763. :group 'org-cycle
  764. :type '(choice
  765. (const :tag "No limit" nil)
  766. (integer :tag "Maximum level")))
  767. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  768. "Names of drawers. Drawers are not opened by cycling on the headline above.
  769. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  770. this:
  771. :DRAWERNAME:
  772. .....
  773. :END:
  774. The drawer \"PROPERTIES\" is special for capturing properties through
  775. the property API.
  776. Drawers can be defined on the per-file basis with a line like:
  777. #+DRAWERS: HIDDEN STATE PROPERTIES"
  778. :group 'org-structure
  779. :group 'org-cycle
  780. :type '(repeat (string :tag "Drawer Name")))
  781. (defcustom org-hide-block-startup nil
  782. "Non-nil means entering Org-mode will fold all blocks.
  783. This can also be set in on a per-file basis with
  784. #+STARTUP: hideblocks
  785. #+STARTUP: showblocks"
  786. :group 'org-startup
  787. :group 'org-cycle
  788. :type 'boolean)
  789. (defcustom org-cycle-global-at-bob nil
  790. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  791. This makes it possible to do global cycling without having to use S-TAB or
  792. \\[universal-argument] TAB. For this special case to work, the first line \
  793. of the buffer
  794. must not be a headline - it may be empty or some other text. When used in
  795. this way, `org-cycle-hook' is disables temporarily, to make sure the
  796. cursor stays at the beginning of the buffer.
  797. When this option is nil, don't do anything special at the beginning
  798. of the buffer."
  799. :group 'org-cycle
  800. :type 'boolean)
  801. (defcustom org-cycle-level-after-item/entry-creation t
  802. "Non-nil means cycle entry level or item indentation in new empty entries.
  803. When the cursor is at the end of an empty headline, i.e with only stars
  804. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  805. and then all possible ancestor states, before returning to the original state.
  806. This makes data entry extremely fast: M-RET to create a new headline,
  807. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  808. When the cursor is at the end of an empty plain list item, one TAB will
  809. make it a subitem, two or more tabs will back up to make this an item
  810. higher up in the item hierarchy."
  811. :group 'org-cycle
  812. :type 'boolean)
  813. (defcustom org-cycle-emulate-tab t
  814. "Where should `org-cycle' emulate TAB.
  815. nil Never
  816. white Only in completely white lines
  817. whitestart Only at the beginning of lines, before the first non-white char
  818. t Everywhere except in headlines
  819. exc-hl-bol Everywhere except at the start of a headline
  820. If TAB is used in a place where it does not emulate TAB, the current subtree
  821. visibility is cycled."
  822. :group 'org-cycle
  823. :type '(choice (const :tag "Never" nil)
  824. (const :tag "Only in completely white lines" white)
  825. (const :tag "Before first char in a line" whitestart)
  826. (const :tag "Everywhere except in headlines" t)
  827. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  828. ))
  829. (defcustom org-cycle-separator-lines 2
  830. "Number of empty lines needed to keep an empty line between collapsed trees.
  831. If you leave an empty line between the end of a subtree and the following
  832. headline, this empty line is hidden when the subtree is folded.
  833. Org-mode will leave (exactly) one empty line visible if the number of
  834. empty lines is equal or larger to the number given in this variable.
  835. So the default 2 means at least 2 empty lines after the end of a subtree
  836. are needed to produce free space between a collapsed subtree and the
  837. following headline.
  838. If the number is negative, and the number of empty lines is at least -N,
  839. all empty lines are shown.
  840. Special case: when 0, never leave empty lines in collapsed view."
  841. :group 'org-cycle
  842. :type 'integer)
  843. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  844. (defcustom org-pre-cycle-hook nil
  845. "Hook that is run before visibility cycling is happening.
  846. The function(s) in this hook must accept a single argument which indicates
  847. the new state that will be set right after running this hook. The
  848. argument is a symbol. Before a global state change, it can have the values
  849. `overview', `content', or `all'. Before a local state change, it can have
  850. the values `folded', `children', or `subtree'."
  851. :group 'org-cycle
  852. :type 'hook)
  853. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  854. org-cycle-hide-drawers
  855. org-cycle-show-empty-lines
  856. org-optimize-window-after-visibility-change)
  857. "Hook that is run after `org-cycle' has changed the buffer visibility.
  858. The function(s) in this hook must accept a single argument which indicates
  859. the new state that was set by the most recent `org-cycle' command. The
  860. argument is a symbol. After a global state change, it can have the values
  861. `overview', `content', or `all'. After a local state change, it can have
  862. the values `folded', `children', or `subtree'."
  863. :group 'org-cycle
  864. :type 'hook)
  865. (defgroup org-edit-structure nil
  866. "Options concerning structure editing in Org-mode."
  867. :tag "Org Edit Structure"
  868. :group 'org-structure)
  869. (defcustom org-odd-levels-only nil
  870. "Non-nil means skip even levels and only use odd levels for the outline.
  871. This has the effect that two stars are being added/taken away in
  872. promotion/demotion commands. It also influences how levels are
  873. handled by the exporters.
  874. Changing it requires restart of `font-lock-mode' to become effective
  875. for fontification also in regions already fontified.
  876. You may also set this on a per-file basis by adding one of the following
  877. lines to the buffer:
  878. #+STARTUP: odd
  879. #+STARTUP: oddeven"
  880. :group 'org-edit-structure
  881. :group 'org-appearance
  882. :type 'boolean)
  883. (defcustom org-adapt-indentation t
  884. "Non-nil means adapt indentation to outline node level.
  885. When this variable is set, Org assumes that you write outlines by
  886. indenting text in each node to align with the headline (after the stars).
  887. The following issues are influenced by this variable:
  888. - When this is set and the *entire* text in an entry is indented, the
  889. indentation is increased by one space in a demotion command, and
  890. decreased by one in a promotion command. If any line in the entry
  891. body starts with text at column 0, indentation is not changed at all.
  892. - Property drawers and planning information is inserted indented when
  893. this variable s set. When nil, they will not be indented.
  894. - TAB indents a line relative to context. The lines below a headline
  895. will be indented when this variable is set.
  896. Note that this is all about true indentation, by adding and removing
  897. space characters. See also `org-indent.el' which does level-dependent
  898. indentation in a virtual way, i.e. at display time in Emacs."
  899. :group 'org-edit-structure
  900. :type 'boolean)
  901. (defcustom org-special-ctrl-a/e nil
  902. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  903. When t, `C-a' will bring back the cursor to the beginning of the
  904. headline text, i.e. after the stars and after a possible TODO
  905. keyword. In an item, this will be the position after bullet and
  906. check-box, if any. When the cursor is already at that position,
  907. another `C-a' will bring it to the beginning of the line.
  908. `C-e' will jump to the end of the headline, ignoring the presence
  909. of tags in the headline. A second `C-e' will then jump to the
  910. true end of the line, after any tags. This also means that, when
  911. this variable is non-nil, `C-e' also will never jump beyond the
  912. end of the heading of a folded section, i.e. not after the
  913. ellipses.
  914. When set to the symbol `reversed', the first `C-a' or `C-e' works
  915. normally, going to the true line boundary first. Only a directly
  916. following, identical keypress will bring the cursor to the
  917. special positions.
  918. This may also be a cons cell where the behavior for `C-a' and
  919. `C-e' is set separately."
  920. :group 'org-edit-structure
  921. :type '(choice
  922. (const :tag "off" nil)
  923. (const :tag "on: after stars/bullet and before tags first" t)
  924. (const :tag "reversed: true line boundary first" reversed)
  925. (cons :tag "Set C-a and C-e separately"
  926. (choice :tag "Special C-a"
  927. (const :tag "off" nil)
  928. (const :tag "on: after stars/bullet first" t)
  929. (const :tag "reversed: before stars/bullet first" reversed))
  930. (choice :tag "Special C-e"
  931. (const :tag "off" nil)
  932. (const :tag "on: before tags first" t)
  933. (const :tag "reversed: after tags first" reversed)))))
  934. (if (fboundp 'defvaralias)
  935. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  936. (defcustom org-special-ctrl-k nil
  937. "Non-nil means `C-k' will behave specially in headlines.
  938. When nil, `C-k' will call the default `kill-line' command.
  939. When t, the following will happen while the cursor is in the headline:
  940. - When the cursor is at the beginning of a headline, kill the entire
  941. line and possible the folded subtree below the line.
  942. - When in the middle of the headline text, kill the headline up to the tags.
  943. - When after the headline text, kill the tags."
  944. :group 'org-edit-structure
  945. :type 'boolean)
  946. (defcustom org-ctrl-k-protect-subtree nil
  947. "Non-nil means, do not delete a hidden subtree with C-k.
  948. When set to the symbol `error', simply throw an error when C-k is
  949. used to kill (part-of) a headline that has hidden text behind it.
  950. Any other non-nil value will result in a query to the user, if it is
  951. OK to kill that hidden subtree. When nil, kill without remorse."
  952. :group 'org-edit-structure
  953. :version "24.1"
  954. :type '(choice
  955. (const :tag "Do not protect hidden subtrees" nil)
  956. (const :tag "Protect hidden subtrees with a security query" t)
  957. (const :tag "Never kill a hidden subtree with C-k" error)))
  958. (defcustom org-catch-invisible-edits nil
  959. "Check if in invisible region before inserting or deleting a character.
  960. Valid values are:
  961. nil Do not check, so just do invisible edits.
  962. error Throw an error and do nothing.
  963. show Make point visible, and do the requested edit.
  964. show-and-error Make point visible, then throw an error and abort the edit.
  965. smart Make point visible, and do insertion/deletion if it is
  966. adjacent to visible text and the change feels predictable.
  967. Never delete a previously invisible character or add in the
  968. middle or right after an invisible region. Basically, this
  969. allows insertion and backward-delete right before ellipses.
  970. FIXME: maybe in this case we should not even show?"
  971. :group 'org-edit-structure
  972. :version "24.1"
  973. :type '(choice
  974. (const :tag "Do not check" nil)
  975. (const :tag "Throw error when trying to edit" error)
  976. (const :tag "Unhide, but do not do the edit" show-and-error)
  977. (const :tag "Show invisible part and do the edit" show)
  978. (const :tag "Be smart and do the right thing" smart)))
  979. (defcustom org-yank-folded-subtrees t
  980. "Non-nil means when yanking subtrees, fold them.
  981. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  982. it starts with a heading and all other headings in it are either children
  983. or siblings, then fold all the subtrees. However, do this only if no
  984. text after the yank would be swallowed into a folded tree by this action."
  985. :group 'org-edit-structure
  986. :type 'boolean)
  987. (defcustom org-yank-adjusted-subtrees nil
  988. "Non-nil means when yanking subtrees, adjust the level.
  989. With this setting, `org-paste-subtree' is used to insert the subtree, see
  990. this function for details."
  991. :group 'org-edit-structure
  992. :type 'boolean)
  993. (defcustom org-M-RET-may-split-line '((default . t))
  994. "Non-nil means M-RET will split the line at the cursor position.
  995. When nil, it will go to the end of the line before making a
  996. new line.
  997. You may also set this option in a different way for different
  998. contexts. Valid contexts are:
  999. headline when creating a new headline
  1000. item when creating a new item
  1001. table in a table field
  1002. default the value to be used for all contexts not explicitly
  1003. customized"
  1004. :group 'org-structure
  1005. :group 'org-table
  1006. :type '(choice
  1007. (const :tag "Always" t)
  1008. (const :tag "Never" nil)
  1009. (repeat :greedy t :tag "Individual contexts"
  1010. (cons
  1011. (choice :tag "Context"
  1012. (const headline)
  1013. (const item)
  1014. (const table)
  1015. (const default))
  1016. (boolean)))))
  1017. (defcustom org-insert-heading-respect-content nil
  1018. "Non-nil means insert new headings after the current subtree.
  1019. When nil, the new heading is created directly after the current line.
  1020. The commands \\[org-insert-heading-respect-content] and
  1021. \\[org-insert-todo-heading-respect-content] turn this variable on
  1022. for the duration of the command."
  1023. :group 'org-structure
  1024. :type 'boolean)
  1025. (defcustom org-blank-before-new-entry '((heading . auto)
  1026. (plain-list-item . auto))
  1027. "Should `org-insert-heading' leave a blank line before new heading/item?
  1028. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1029. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1030. which case Org will look at the surrounding headings/items and try to
  1031. make an intelligent decision whether to insert a blank line or not.
  1032. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1033. set, the setting here is ignored and no empty line is inserted, to avoid
  1034. breaking the list structure."
  1035. :group 'org-edit-structure
  1036. :type '(list
  1037. (cons (const heading)
  1038. (choice (const :tag "Never" nil)
  1039. (const :tag "Always" t)
  1040. (const :tag "Auto" auto)))
  1041. (cons (const plain-list-item)
  1042. (choice (const :tag "Never" nil)
  1043. (const :tag "Always" t)
  1044. (const :tag "Auto" auto)))))
  1045. (defcustom org-insert-heading-hook nil
  1046. "Hook being run after inserting a new heading."
  1047. :group 'org-edit-structure
  1048. :type 'hook)
  1049. (defcustom org-enable-fixed-width-editor t
  1050. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1051. This currently only means they are never auto-wrapped.
  1052. When nil, such lines will be treated like ordinary lines.
  1053. See also the QUOTE keyword."
  1054. :group 'org-edit-structure
  1055. :type 'boolean)
  1056. (defcustom org-goto-auto-isearch t
  1057. "Non-nil means typing characters in `org-goto' starts incremental search."
  1058. :group 'org-edit-structure
  1059. :type 'boolean)
  1060. (defgroup org-sparse-trees nil
  1061. "Options concerning sparse trees in Org-mode."
  1062. :tag "Org Sparse Trees"
  1063. :group 'org-structure)
  1064. (defcustom org-highlight-sparse-tree-matches t
  1065. "Non-nil means highlight all matches that define a sparse tree.
  1066. The highlights will automatically disappear the next time the buffer is
  1067. changed by an edit command."
  1068. :group 'org-sparse-trees
  1069. :type 'boolean)
  1070. (defcustom org-remove-highlights-with-change t
  1071. "Non-nil means any change to the buffer will remove temporary highlights.
  1072. Such highlights are created by `org-occur' and `org-clock-display'.
  1073. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1074. The highlights created by `org-preview-latex-fragment' always need
  1075. `C-c C-c' to be removed."
  1076. :group 'org-sparse-trees
  1077. :group 'org-time
  1078. :type 'boolean)
  1079. (defcustom org-occur-hook '(org-first-headline-recenter)
  1080. "Hook that is run after `org-occur' has constructed a sparse tree.
  1081. This can be used to recenter the window to show as much of the structure
  1082. as possible."
  1083. :group 'org-sparse-trees
  1084. :type 'hook)
  1085. (defgroup org-imenu-and-speedbar nil
  1086. "Options concerning imenu and speedbar in Org-mode."
  1087. :tag "Org Imenu and Speedbar"
  1088. :group 'org-structure)
  1089. (defcustom org-imenu-depth 2
  1090. "The maximum level for Imenu access to Org-mode headlines.
  1091. This also applied for speedbar access."
  1092. :group 'org-imenu-and-speedbar
  1093. :type 'integer)
  1094. (defgroup org-table nil
  1095. "Options concerning tables in Org-mode."
  1096. :tag "Org Table"
  1097. :group 'org)
  1098. (defcustom org-enable-table-editor 'optimized
  1099. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1100. When nil, such lines will be treated like ordinary lines.
  1101. When equal to the symbol `optimized', the table editor will be optimized to
  1102. do the following:
  1103. - Automatic overwrite mode in front of whitespace in table fields.
  1104. This makes the structure of the table stay in tact as long as the edited
  1105. field does not exceed the column width.
  1106. - Minimize the number of realigns. Normally, the table is aligned each time
  1107. TAB or RET are pressed to move to another field. With optimization this
  1108. happens only if changes to a field might have changed the column width.
  1109. Optimization requires replacing the functions `self-insert-command',
  1110. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1111. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1112. very good at guessing when a re-align will be necessary, but you can always
  1113. force one with \\[org-ctrl-c-ctrl-c].
  1114. If you would like to use the optimized version in Org-mode, but the
  1115. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1116. This variable can be used to turn on and off the table editor during a session,
  1117. but in order to toggle optimization, a restart is required.
  1118. See also the variable `org-table-auto-blank-field'."
  1119. :group 'org-table
  1120. :type '(choice
  1121. (const :tag "off" nil)
  1122. (const :tag "on" t)
  1123. (const :tag "on, optimized" optimized)))
  1124. (defcustom org-self-insert-cluster-for-undo t
  1125. "Non-nil means cluster self-insert commands for undo when possible.
  1126. If this is set, then, like in the Emacs command loop, 20 consecutive
  1127. characters will be undone together.
  1128. This is configurable, because there is some impact on typing performance."
  1129. :group 'org-table
  1130. :type 'boolean)
  1131. (defcustom org-table-tab-recognizes-table.el t
  1132. "Non-nil means TAB will automatically notice a table.el table.
  1133. When it sees such a table, it moves point into it and - if necessary -
  1134. calls `table-recognize-table'."
  1135. :group 'org-table-editing
  1136. :type 'boolean)
  1137. (defgroup org-link nil
  1138. "Options concerning links in Org-mode."
  1139. :tag "Org Link"
  1140. :group 'org)
  1141. (defvar org-link-abbrev-alist-local nil
  1142. "Buffer-local version of `org-link-abbrev-alist', which see.
  1143. The value of this is taken from the #+LINK lines.")
  1144. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1145. (defcustom org-link-abbrev-alist nil
  1146. "Alist of link abbreviations.
  1147. The car of each element is a string, to be replaced at the start of a link.
  1148. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1149. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1150. [[linkkey:tag][description]]
  1151. The 'linkkey' must be a word word, starting with a letter, followed
  1152. by letters, numbers, '-' or '_'.
  1153. If REPLACE is a string, the tag will simply be appended to create the link.
  1154. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1155. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1156. be inserted at that point (see the function `url-hexify-string').
  1157. REPLACE may also be a function that will be called with the tag as the
  1158. only argument to create the link, which should be returned as a string.
  1159. See the manual for examples."
  1160. :group 'org-link
  1161. :type '(repeat
  1162. (cons
  1163. (string :tag "Protocol")
  1164. (choice
  1165. (string :tag "Format")
  1166. (function)))))
  1167. (defcustom org-descriptive-links t
  1168. "Non-nil means Org will display descriptive links.
  1169. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1170. \"Org Website\", hiding the link itself and just displaying its
  1171. description. When set to `nil', Org will display the full links
  1172. literally.
  1173. You can interactively set the value of this variable by calling
  1174. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1175. :group 'org-link
  1176. :type 'boolean)
  1177. (defcustom org-link-file-path-type 'adaptive
  1178. "How the path name in file links should be stored.
  1179. Valid values are:
  1180. relative Relative to the current directory, i.e. the directory of the file
  1181. into which the link is being inserted.
  1182. absolute Absolute path, if possible with ~ for home directory.
  1183. noabbrev Absolute path, no abbreviation of home directory.
  1184. adaptive Use relative path for files in the current directory and sub-
  1185. directories of it. For other files, use an absolute path."
  1186. :group 'org-link
  1187. :type '(choice
  1188. (const relative)
  1189. (const absolute)
  1190. (const noabbrev)
  1191. (const adaptive)))
  1192. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1193. "Types of links that should be activated in Org-mode files.
  1194. This is a list of symbols, each leading to the activation of a certain link
  1195. type. In principle, it does not hurt to turn on most link types - there may
  1196. be a small gain when turning off unused link types. The types are:
  1197. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1198. angle Links in angular brackets that may contain whitespace like
  1199. <bbdb:Carsten Dominik>.
  1200. plain Plain links in normal text, no whitespace, like http://google.com.
  1201. radio Text that is matched by a radio target, see manual for details.
  1202. tag Tag settings in a headline (link to tag search).
  1203. date Time stamps (link to calendar).
  1204. footnote Footnote labels.
  1205. Changing this variable requires a restart of Emacs to become effective."
  1206. :group 'org-link
  1207. :type '(set :greedy t
  1208. (const :tag "Double bracket links" bracket)
  1209. (const :tag "Angular bracket links" angle)
  1210. (const :tag "Plain text links" plain)
  1211. (const :tag "Radio target matches" radio)
  1212. (const :tag "Tags" tag)
  1213. (const :tag "Timestamps" date)
  1214. (const :tag "Footnotes" footnote)))
  1215. (defcustom org-make-link-description-function nil
  1216. "Function to use to generate link descriptions from links.
  1217. If nil the link location will be used. This function must take
  1218. two parameters; the first is the link and the second the
  1219. description `org-insert-link' has generated, and should return the
  1220. description to use."
  1221. :group 'org-link
  1222. :type 'function)
  1223. (defgroup org-link-store nil
  1224. "Options concerning storing links in Org-mode."
  1225. :tag "Org Store Link"
  1226. :group 'org-link)
  1227. (defcustom org-email-link-description-format "Email %c: %.30s"
  1228. "Format of the description part of a link to an email or usenet message.
  1229. The following %-escapes will be replaced by corresponding information:
  1230. %F full \"From\" field
  1231. %f name, taken from \"From\" field, address if no name
  1232. %T full \"To\" field
  1233. %t first name in \"To\" field, address if no name
  1234. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1235. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1236. %s subject
  1237. %d date
  1238. %m message-id.
  1239. You may use normal field width specification between the % and the letter.
  1240. This is for example useful to limit the length of the subject.
  1241. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1242. :group 'org-link-store
  1243. :type 'string)
  1244. (defcustom org-from-is-user-regexp
  1245. (let (r1 r2)
  1246. (when (and user-mail-address (not (string= user-mail-address "")))
  1247. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1248. (when (and user-full-name (not (string= user-full-name "")))
  1249. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1250. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1251. "Regexp matched against the \"From:\" header of an email or usenet message.
  1252. It should match if the message is from the user him/herself."
  1253. :group 'org-link-store
  1254. :type 'regexp)
  1255. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1256. "Non-nil means storing a link to an Org file will use entry IDs.
  1257. Note that before this variable is even considered, org-id must be loaded,
  1258. so please customize `org-modules' and turn it on.
  1259. The variable can have the following values:
  1260. t Create an ID if needed to make a link to the current entry.
  1261. create-if-interactive
  1262. If `org-store-link' is called directly (interactively, as a user
  1263. command), do create an ID to support the link. But when doing the
  1264. job for remember, only use the ID if it already exists. The
  1265. purpose of this setting is to avoid proliferation of unwanted
  1266. IDs, just because you happen to be in an Org file when you
  1267. call `org-remember' that automatically and preemptively
  1268. creates a link. If you do want to get an ID link in a remember
  1269. template to an entry not having an ID, create it first by
  1270. explicitly creating a link to it, using `C-c C-l' first.
  1271. create-if-interactive-and-no-custom-id
  1272. Like create-if-interactive, but do not create an ID if there is
  1273. a CUSTOM_ID property defined in the entry. This is the default.
  1274. use-existing
  1275. Use existing ID, do not create one.
  1276. nil Never use an ID to make a link, instead link using a text search for
  1277. the headline text."
  1278. :group 'org-link-store
  1279. :type '(choice
  1280. (const :tag "Create ID to make link" t)
  1281. (const :tag "Create if storing link interactively"
  1282. create-if-interactive)
  1283. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1284. create-if-interactive-and-no-custom-id)
  1285. (const :tag "Only use existing" use-existing)
  1286. (const :tag "Do not use ID to create link" nil)))
  1287. (defcustom org-context-in-file-links t
  1288. "Non-nil means file links from `org-store-link' contain context.
  1289. A search string will be added to the file name with :: as separator and
  1290. used to find the context when the link is activated by the command
  1291. `org-open-at-point'. When this option is t, the entire active region
  1292. will be placed in the search string of the file link. If set to a
  1293. positive integer, only the first n lines of context will be stored.
  1294. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1295. negates this setting for the duration of the command."
  1296. :group 'org-link-store
  1297. :type '(choice boolean integer))
  1298. (defcustom org-keep-stored-link-after-insertion nil
  1299. "Non-nil means keep link in list for entire session.
  1300. The command `org-store-link' adds a link pointing to the current
  1301. location to an internal list. These links accumulate during a session.
  1302. The command `org-insert-link' can be used to insert links into any
  1303. Org-mode file (offering completion for all stored links). When this
  1304. option is nil, every link which has been inserted once using \\[org-insert-link]
  1305. will be removed from the list, to make completing the unused links
  1306. more efficient."
  1307. :group 'org-link-store
  1308. :type 'boolean)
  1309. (defgroup org-link-follow nil
  1310. "Options concerning following links in Org-mode."
  1311. :tag "Org Follow Link"
  1312. :group 'org-link)
  1313. (defcustom org-link-translation-function nil
  1314. "Function to translate links with different syntax to Org syntax.
  1315. This can be used to translate links created for example by the Planner
  1316. or emacs-wiki packages to Org syntax.
  1317. The function must accept two parameters, a TYPE containing the link
  1318. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1319. which is everything after the link protocol. It should return a cons
  1320. with possibly modified values of type and path.
  1321. Org contains a function for this, so if you set this variable to
  1322. `org-translate-link-from-planner', you should be able follow many
  1323. links created by planner."
  1324. :group 'org-link-follow
  1325. :type 'function)
  1326. (defcustom org-follow-link-hook nil
  1327. "Hook that is run after a link has been followed."
  1328. :group 'org-link-follow
  1329. :type 'hook)
  1330. (defcustom org-tab-follows-link nil
  1331. "Non-nil means on links TAB will follow the link.
  1332. Needs to be set before org.el is loaded.
  1333. This really should not be used, it does not make sense, and the
  1334. implementation is bad."
  1335. :group 'org-link-follow
  1336. :type 'boolean)
  1337. (defcustom org-return-follows-link nil
  1338. "Non-nil means on links RET will follow the link."
  1339. :group 'org-link-follow
  1340. :type 'boolean)
  1341. (defcustom org-mouse-1-follows-link
  1342. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1343. "Non-nil means mouse-1 on a link will follow the link.
  1344. A longer mouse click will still set point. Does not work on XEmacs.
  1345. Needs to be set before org.el is loaded."
  1346. :group 'org-link-follow
  1347. :type 'boolean)
  1348. (defcustom org-mark-ring-length 4
  1349. "Number of different positions to be recorded in the ring.
  1350. Changing this requires a restart of Emacs to work correctly."
  1351. :group 'org-link-follow
  1352. :type 'integer)
  1353. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1354. "Non-nil means internal links in Org files must exactly match a headline.
  1355. When nil, the link search tries to match a phrase with all words
  1356. in the search text."
  1357. :group 'org-link-follow
  1358. :version "24.1"
  1359. :type '(choice
  1360. (const :tag "Use fuzzy text search" nil)
  1361. (const :tag "Match only exact headline" t)
  1362. (const :tag "Match exact headline or query to create it"
  1363. query-to-create)))
  1364. (defcustom org-link-frame-setup
  1365. '((vm . vm-visit-folder-other-frame)
  1366. (vm-imap . vm-visit-imap-folder-other-frame)
  1367. (gnus . org-gnus-no-new-news)
  1368. (file . find-file-other-window)
  1369. (wl . wl-other-frame))
  1370. "Setup the frame configuration for following links.
  1371. When following a link with Emacs, it may often be useful to display
  1372. this link in another window or frame. This variable can be used to
  1373. set this up for the different types of links.
  1374. For VM, use any of
  1375. `vm-visit-folder'
  1376. `vm-visit-folder-other-window'
  1377. `vm-visit-folder-other-frame'
  1378. For Gnus, use any of
  1379. `gnus'
  1380. `gnus-other-frame'
  1381. `org-gnus-no-new-news'
  1382. For FILE, use any of
  1383. `find-file'
  1384. `find-file-other-window'
  1385. `find-file-other-frame'
  1386. For Wanderlust use any of
  1387. `wl'
  1388. `wl-other-frame'
  1389. For the calendar, use the variable `calendar-setup'.
  1390. For BBDB, it is currently only possible to display the matches in
  1391. another window."
  1392. :group 'org-link-follow
  1393. :type '(list
  1394. (cons (const vm)
  1395. (choice
  1396. (const vm-visit-folder)
  1397. (const vm-visit-folder-other-window)
  1398. (const vm-visit-folder-other-frame)))
  1399. (cons (const gnus)
  1400. (choice
  1401. (const gnus)
  1402. (const gnus-other-frame)
  1403. (const org-gnus-no-new-news)))
  1404. (cons (const file)
  1405. (choice
  1406. (const find-file)
  1407. (const find-file-other-window)
  1408. (const find-file-other-frame)))
  1409. (cons (const wl)
  1410. (choice
  1411. (const wl)
  1412. (const wl-other-frame)))))
  1413. (defcustom org-display-internal-link-with-indirect-buffer nil
  1414. "Non-nil means use indirect buffer to display infile links.
  1415. Activating internal links (from one location in a file to another location
  1416. in the same file) normally just jumps to the location. When the link is
  1417. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1418. is displayed in
  1419. another window. When this option is set, the other window actually displays
  1420. an indirect buffer clone of the current buffer, to avoid any visibility
  1421. changes to the current buffer."
  1422. :group 'org-link-follow
  1423. :type 'boolean)
  1424. (defcustom org-open-non-existing-files nil
  1425. "Non-nil means `org-open-file' will open non-existing files.
  1426. When nil, an error will be generated.
  1427. This variable applies only to external applications because they
  1428. might choke on non-existing files. If the link is to a file that
  1429. will be opened in Emacs, the variable is ignored."
  1430. :group 'org-link-follow
  1431. :type 'boolean)
  1432. (defcustom org-open-directory-means-index-dot-org nil
  1433. "Non-nil means a link to a directory really means to index.org.
  1434. When nil, following a directory link will run dired or open a finder/explorer
  1435. window on that directory."
  1436. :group 'org-link-follow
  1437. :type 'boolean)
  1438. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1439. "Function and arguments to call for following mailto links.
  1440. This is a list with the first element being a Lisp function, and the
  1441. remaining elements being arguments to the function. In string arguments,
  1442. %a will be replaced by the address, and %s will be replaced by the subject
  1443. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1444. :group 'org-link-follow
  1445. :type '(choice
  1446. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1447. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1448. (const :tag "message-mail" (message-mail "%a" "%s"))
  1449. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1450. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1451. "Non-nil means ask for confirmation before executing shell links.
  1452. Shell links can be dangerous: just think about a link
  1453. [[shell:rm -rf ~/*][Google Search]]
  1454. This link would show up in your Org-mode document as \"Google Search\",
  1455. but really it would remove your entire home directory.
  1456. Therefore we advise against setting this variable to nil.
  1457. Just change it to `y-or-n-p' if you want to confirm with a
  1458. single keystroke rather than having to type \"yes\"."
  1459. :group 'org-link-follow
  1460. :type '(choice
  1461. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1462. (const :tag "with y-or-n (faster)" y-or-n-p)
  1463. (const :tag "no confirmation (dangerous)" nil)))
  1464. (put 'org-confirm-shell-link-function
  1465. 'safe-local-variable
  1466. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1467. (defcustom org-confirm-shell-link-not-regexp ""
  1468. "A regexp to skip confirmation for shell links."
  1469. :group 'org-link-follow
  1470. :version "24.1"
  1471. :type 'regexp)
  1472. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1473. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1474. Elisp links can be dangerous: just think about a link
  1475. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1476. This link would show up in your Org-mode document as \"Google Search\",
  1477. but really it would remove your entire home directory.
  1478. Therefore we advise against setting this variable to nil.
  1479. Just change it to `y-or-n-p' if you want to confirm with a
  1480. single keystroke rather than having to type \"yes\"."
  1481. :group 'org-link-follow
  1482. :type '(choice
  1483. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1484. (const :tag "with y-or-n (faster)" y-or-n-p)
  1485. (const :tag "no confirmation (dangerous)" nil)))
  1486. (put 'org-confirm-shell-link-function
  1487. 'safe-local-variable
  1488. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1489. (defcustom org-confirm-elisp-link-not-regexp ""
  1490. "A regexp to skip confirmation for Elisp links."
  1491. :group 'org-link-follow
  1492. :version "24.1"
  1493. :type 'regexp)
  1494. (defconst org-file-apps-defaults-gnu
  1495. '((remote . emacs)
  1496. (system . mailcap)
  1497. (t . mailcap))
  1498. "Default file applications on a UNIX or GNU/Linux system.
  1499. See `org-file-apps'.")
  1500. (defconst org-file-apps-defaults-macosx
  1501. '((remote . emacs)
  1502. (t . "open %s")
  1503. (system . "open %s")
  1504. ("ps.gz" . "gv %s")
  1505. ("eps.gz" . "gv %s")
  1506. ("dvi" . "xdvi %s")
  1507. ("fig" . "xfig %s"))
  1508. "Default file applications on a MacOS X system.
  1509. The system \"open\" is known as a default, but we use X11 applications
  1510. for some files for which the OS does not have a good default.
  1511. See `org-file-apps'.")
  1512. (defconst org-file-apps-defaults-windowsnt
  1513. (list
  1514. '(remote . emacs)
  1515. (cons t
  1516. (list (if (featurep 'xemacs)
  1517. 'mswindows-shell-execute
  1518. 'w32-shell-execute)
  1519. "open" 'file))
  1520. (cons 'system
  1521. (list (if (featurep 'xemacs)
  1522. 'mswindows-shell-execute
  1523. 'w32-shell-execute)
  1524. "open" 'file)))
  1525. "Default file applications on a Windows NT system.
  1526. The system \"open\" is used for most files.
  1527. See `org-file-apps'.")
  1528. (defcustom org-file-apps
  1529. '(
  1530. (auto-mode . emacs)
  1531. ("\\.mm\\'" . default)
  1532. ("\\.x?html?\\'" . default)
  1533. ("\\.pdf\\'" . default)
  1534. )
  1535. "External applications for opening `file:path' items in a document.
  1536. Org-mode uses system defaults for different file types, but
  1537. you can use this variable to set the application for a given file
  1538. extension. The entries in this list are cons cells where the car identifies
  1539. files and the cdr the corresponding command. Possible values for the
  1540. file identifier are
  1541. \"string\" A string as a file identifier can be interpreted in different
  1542. ways, depending on its contents:
  1543. - Alphanumeric characters only:
  1544. Match links with this file extension.
  1545. Example: (\"pdf\" . \"evince %s\")
  1546. to open PDFs with evince.
  1547. - Regular expression: Match links where the
  1548. filename matches the regexp. If you want to
  1549. use groups here, use shy groups.
  1550. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1551. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1552. to open *.html and *.xhtml with firefox.
  1553. - Regular expression which contains (non-shy) groups:
  1554. Match links where the whole link, including \"::\", and
  1555. anything after that, matches the regexp.
  1556. In a custom command string, %1, %2, etc. are replaced with
  1557. the parts of the link that were matched by the groups.
  1558. For backwards compatibility, if a command string is given
  1559. that does not use any of the group matches, this case is
  1560. handled identically to the second one (i.e. match against
  1561. file name only).
  1562. In a custom lisp form, you can access the group matches with
  1563. (match-string n link).
  1564. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1565. to open [[file:document.pdf::5]] with evince at page 5.
  1566. `directory' Matches a directory
  1567. `remote' Matches a remote file, accessible through tramp or efs.
  1568. Remote files most likely should be visited through Emacs
  1569. because external applications cannot handle such paths.
  1570. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1571. so all files Emacs knows how to handle. Using this with
  1572. command `emacs' will open most files in Emacs. Beware that this
  1573. will also open html files inside Emacs, unless you add
  1574. (\"html\" . default) to the list as well.
  1575. t Default for files not matched by any of the other options.
  1576. `system' The system command to open files, like `open' on Windows
  1577. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1578. that will be selected if you call `C-c C-o' with a double
  1579. \\[universal-argument] \\[universal-argument] prefix.
  1580. Possible values for the command are:
  1581. `emacs' The file will be visited by the current Emacs process.
  1582. `default' Use the default application for this file type, which is the
  1583. association for t in the list, most likely in the system-specific
  1584. part.
  1585. This can be used to overrule an unwanted setting in the
  1586. system-specific variable.
  1587. `system' Use the system command for opening files, like \"open\".
  1588. This command is specified by the entry whose car is `system'.
  1589. Most likely, the system-specific version of this variable
  1590. does define this command, but you can overrule/replace it
  1591. here.
  1592. string A command to be executed by a shell; %s will be replaced
  1593. by the path to the file.
  1594. sexp A Lisp form which will be evaluated. The file path will
  1595. be available in the Lisp variable `file'.
  1596. For more examples, see the system specific constants
  1597. `org-file-apps-defaults-macosx'
  1598. `org-file-apps-defaults-windowsnt'
  1599. `org-file-apps-defaults-gnu'."
  1600. :group 'org-link-follow
  1601. :type '(repeat
  1602. (cons (choice :value ""
  1603. (string :tag "Extension")
  1604. (const :tag "System command to open files" system)
  1605. (const :tag "Default for unrecognized files" t)
  1606. (const :tag "Remote file" remote)
  1607. (const :tag "Links to a directory" directory)
  1608. (const :tag "Any files that have Emacs modes"
  1609. auto-mode))
  1610. (choice :value ""
  1611. (const :tag "Visit with Emacs" emacs)
  1612. (const :tag "Use default" default)
  1613. (const :tag "Use the system command" system)
  1614. (string :tag "Command")
  1615. (sexp :tag "Lisp form")))))
  1616. (defgroup org-refile nil
  1617. "Options concerning refiling entries in Org-mode."
  1618. :tag "Org Refile"
  1619. :group 'org)
  1620. (defcustom org-directory "~/org"
  1621. "Directory with org files.
  1622. This is just a default location to look for Org files. There is no need
  1623. at all to put your files into this directory. It is only used in the
  1624. following situations:
  1625. 1. When a remember template specifies a target file that is not an
  1626. absolute path. The path will then be interpreted relative to
  1627. `org-directory'
  1628. 2. When a remember note is filed away in an interactive way (when exiting the
  1629. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1630. with `org-directory' as the default path."
  1631. :group 'org-refile
  1632. :group 'org-remember
  1633. :type 'directory)
  1634. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1635. "Default target for storing notes.
  1636. Used as a fall back file for org-remember.el and org-capture.el, for
  1637. templates that do not specify a target file."
  1638. :group 'org-refile
  1639. :group 'org-remember
  1640. :type '(choice
  1641. (const :tag "Default from remember-data-file" nil)
  1642. file))
  1643. (defcustom org-goto-interface 'outline
  1644. "The default interface to be used for `org-goto'.
  1645. Allowed values are:
  1646. outline The interface shows an outline of the relevant file
  1647. and the correct heading is found by moving through
  1648. the outline or by searching with incremental search.
  1649. outline-path-completion Headlines in the current buffer are offered via
  1650. completion. This is the interface also used by
  1651. the refile command."
  1652. :group 'org-refile
  1653. :type '(choice
  1654. (const :tag "Outline" outline)
  1655. (const :tag "Outline-path-completion" outline-path-completion)))
  1656. (defcustom org-goto-max-level 5
  1657. "Maximum target level when running `org-goto' with refile interface."
  1658. :group 'org-refile
  1659. :type 'integer)
  1660. (defcustom org-reverse-note-order nil
  1661. "Non-nil means store new notes at the beginning of a file or entry.
  1662. When nil, new notes will be filed to the end of a file or entry.
  1663. This can also be a list with cons cells of regular expressions that
  1664. are matched against file names, and values."
  1665. :group 'org-remember
  1666. :group 'org-refile
  1667. :type '(choice
  1668. (const :tag "Reverse always" t)
  1669. (const :tag "Reverse never" nil)
  1670. (repeat :tag "By file name regexp"
  1671. (cons regexp boolean))))
  1672. (defcustom org-log-refile nil
  1673. "Information to record when a task is refiled.
  1674. Possible values are:
  1675. nil Don't add anything
  1676. time Add a time stamp to the task
  1677. note Prompt for a note and add it with template `org-log-note-headings'
  1678. This option can also be set with on a per-file-basis with
  1679. #+STARTUP: nologrefile
  1680. #+STARTUP: logrefile
  1681. #+STARTUP: lognoterefile
  1682. You can have local logging settings for a subtree by setting the LOGGING
  1683. property to one or more of these keywords.
  1684. When bulk-refiling from the agenda, the value `note' is forbidden and
  1685. will temporarily be changed to `time'."
  1686. :group 'org-refile
  1687. :group 'org-progress
  1688. :version "24.1"
  1689. :type '(choice
  1690. (const :tag "No logging" nil)
  1691. (const :tag "Record timestamp" time)
  1692. (const :tag "Record timestamp with note." note)))
  1693. (defcustom org-refile-targets nil
  1694. "Targets for refiling entries with \\[org-refile].
  1695. This is a list of cons cells. Each cell contains:
  1696. - a specification of the files to be considered, either a list of files,
  1697. or a symbol whose function or variable value will be used to retrieve
  1698. a file name or a list of file names. If you use `org-agenda-files' for
  1699. that, all agenda files will be scanned for targets. Nil means consider
  1700. headings in the current buffer.
  1701. - A specification of how to find candidate refile targets. This may be
  1702. any of:
  1703. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1704. This tag has to be present in all target headlines, inheritance will
  1705. not be considered.
  1706. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1707. todo keyword.
  1708. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1709. headlines that are refiling targets.
  1710. - a cons cell (:level . N). Any headline of level N is considered a target.
  1711. Note that, when `org-odd-levels-only' is set, level corresponds to
  1712. order in hierarchy, not to the number of stars.
  1713. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1714. Note that, when `org-odd-levels-only' is set, level corresponds to
  1715. order in hierarchy, not to the number of stars.
  1716. Each element of this list generates a set of possible targets.
  1717. The union of these sets is presented (with completion) to
  1718. the user by `org-refile'.
  1719. You can set the variable `org-refile-target-verify-function' to a function
  1720. to verify each headline found by the simple criteria above.
  1721. When this variable is nil, all top-level headlines in the current buffer
  1722. are used, equivalent to the value `((nil . (:level . 1))'."
  1723. :group 'org-refile
  1724. :type '(repeat
  1725. (cons
  1726. (choice :value org-agenda-files
  1727. (const :tag "All agenda files" org-agenda-files)
  1728. (const :tag "Current buffer" nil)
  1729. (function) (variable) (file))
  1730. (choice :tag "Identify target headline by"
  1731. (cons :tag "Specific tag" (const :value :tag) (string))
  1732. (cons :tag "TODO keyword" (const :value :todo) (string))
  1733. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1734. (cons :tag "Level number" (const :value :level) (integer))
  1735. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1736. (defcustom org-refile-target-verify-function nil
  1737. "Function to verify if the headline at point should be a refile target.
  1738. The function will be called without arguments, with point at the
  1739. beginning of the headline. It should return t and leave point
  1740. where it is if the headline is a valid target for refiling.
  1741. If the target should not be selected, the function must return nil.
  1742. In addition to this, it may move point to a place from where the search
  1743. should be continued. For example, the function may decide that the entire
  1744. subtree of the current entry should be excluded and move point to the end
  1745. of the subtree."
  1746. :group 'org-refile
  1747. :type 'function)
  1748. (defcustom org-refile-use-cache nil
  1749. "Non-nil means cache refile targets to speed up the process.
  1750. The cache for a particular file will be updated automatically when
  1751. the buffer has been killed, or when any of the marker used for flagging
  1752. refile targets no longer points at a live buffer.
  1753. If you have added new entries to a buffer that might themselves be targets,
  1754. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1755. find that easier, `C-u C-u C-u C-c C-w'."
  1756. :group 'org-refile
  1757. :version "24.1"
  1758. :type 'boolean)
  1759. (defcustom org-refile-use-outline-path nil
  1760. "Non-nil means provide refile targets as paths.
  1761. So a level 3 headline will be available as level1/level2/level3.
  1762. When the value is `file', also include the file name (without directory)
  1763. into the path. In this case, you can also stop the completion after
  1764. the file name, to get entries inserted as top level in the file.
  1765. When `full-file-path', include the full file path."
  1766. :group 'org-refile
  1767. :type '(choice
  1768. (const :tag "Not" nil)
  1769. (const :tag "Yes" t)
  1770. (const :tag "Start with file name" file)
  1771. (const :tag "Start with full file path" full-file-path)))
  1772. (defcustom org-outline-path-complete-in-steps t
  1773. "Non-nil means complete the outline path in hierarchical steps.
  1774. When Org-mode uses the refile interface to select an outline path
  1775. \(see variable `org-refile-use-outline-path'), the completion of
  1776. the path can be done is a single go, or if can be done in steps down
  1777. the headline hierarchy. Going in steps is probably the best if you
  1778. do not use a special completion package like `ido' or `icicles'.
  1779. However, when using these packages, going in one step can be very
  1780. fast, while still showing the whole path to the entry."
  1781. :group 'org-refile
  1782. :type 'boolean)
  1783. (defcustom org-refile-allow-creating-parent-nodes nil
  1784. "Non-nil means allow to create new nodes as refile targets.
  1785. New nodes are then created by adding \"/new node name\" to the completion
  1786. of an existing node. When the value of this variable is `confirm',
  1787. new node creation must be confirmed by the user (recommended)
  1788. When nil, the completion must match an existing entry.
  1789. Note that, if the new heading is not seen by the criteria
  1790. listed in `org-refile-targets', multiple instances of the same
  1791. heading would be created by trying again to file under the new
  1792. heading."
  1793. :group 'org-refile
  1794. :type '(choice
  1795. (const :tag "Never" nil)
  1796. (const :tag "Always" t)
  1797. (const :tag "Prompt for confirmation" confirm)))
  1798. (defcustom org-refile-active-region-within-subtree nil
  1799. "Non-nil means also refile active region within a subtree.
  1800. By default `org-refile' doesn't allow refiling regions if they
  1801. don't contain a set of subtrees, but it might be convenient to
  1802. do so sometimes: in that case, the first line of the region is
  1803. converted to a headline before refiling."
  1804. :group 'org-refile
  1805. :version "24.1"
  1806. :type 'boolean)
  1807. (defgroup org-todo nil
  1808. "Options concerning TODO items in Org-mode."
  1809. :tag "Org TODO"
  1810. :group 'org)
  1811. (defgroup org-progress nil
  1812. "Options concerning Progress logging in Org-mode."
  1813. :tag "Org Progress"
  1814. :group 'org-time)
  1815. (defvar org-todo-interpretation-widgets
  1816. '((:tag "Sequence (cycling hits every state)" sequence)
  1817. (:tag "Type (cycling directly to DONE)" type))
  1818. "The available interpretation symbols for customizing `org-todo-keywords'.
  1819. Interested libraries should add to this list.")
  1820. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1821. "List of TODO entry keyword sequences and their interpretation.
  1822. \\<org-mode-map>This is a list of sequences.
  1823. Each sequence starts with a symbol, either `sequence' or `type',
  1824. indicating if the keywords should be interpreted as a sequence of
  1825. action steps, or as different types of TODO items. The first
  1826. keywords are states requiring action - these states will select a headline
  1827. for inclusion into the global TODO list Org-mode produces. If one of
  1828. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1829. signify that no further action is necessary. If \"|\" is not found,
  1830. the last keyword is treated as the only DONE state of the sequence.
  1831. The command \\[org-todo] cycles an entry through these states, and one
  1832. additional state where no keyword is present. For details about this
  1833. cycling, see the manual.
  1834. TODO keywords and interpretation can also be set on a per-file basis with
  1835. the special #+SEQ_TODO and #+TYP_TODO lines.
  1836. Each keyword can optionally specify a character for fast state selection
  1837. \(in combination with the variable `org-use-fast-todo-selection')
  1838. and specifiers for state change logging, using the same syntax
  1839. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1840. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1841. indicates to record a time stamp each time this state is selected.
  1842. Each keyword may also specify if a timestamp or a note should be
  1843. recorded when entering or leaving the state, by adding additional
  1844. characters in the parenthesis after the keyword. This looks like this:
  1845. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1846. record only the time of the state change. With X and Y being either
  1847. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1848. Y when leaving the state if and only if the *target* state does not
  1849. define X. You may omit any of the fast-selection key or X or /Y,
  1850. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1851. For backward compatibility, this variable may also be just a list
  1852. of keywords - in this case the interpretation (sequence or type) will be
  1853. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1854. :group 'org-todo
  1855. :group 'org-keywords
  1856. :type '(choice
  1857. (repeat :tag "Old syntax, just keywords"
  1858. (string :tag "Keyword"))
  1859. (repeat :tag "New syntax"
  1860. (cons
  1861. (choice
  1862. :tag "Interpretation"
  1863. ;;Quick and dirty way to see
  1864. ;;`org-todo-interpretations'. This takes the
  1865. ;;place of item arguments
  1866. :convert-widget
  1867. (lambda (widget)
  1868. (widget-put widget
  1869. :args (mapcar
  1870. #'(lambda (x)
  1871. (widget-convert
  1872. (cons 'const x)))
  1873. org-todo-interpretation-widgets))
  1874. widget))
  1875. (repeat
  1876. (string :tag "Keyword"))))))
  1877. (defvar org-todo-keywords-1 nil
  1878. "All TODO and DONE keywords active in a buffer.")
  1879. (make-variable-buffer-local 'org-todo-keywords-1)
  1880. (defvar org-todo-keywords-for-agenda nil)
  1881. (defvar org-done-keywords-for-agenda nil)
  1882. (defvar org-drawers-for-agenda nil)
  1883. (defvar org-todo-keyword-alist-for-agenda nil)
  1884. (defvar org-tag-alist-for-agenda nil)
  1885. (defvar org-agenda-contributing-files nil)
  1886. (defvar org-not-done-keywords nil)
  1887. (make-variable-buffer-local 'org-not-done-keywords)
  1888. (defvar org-done-keywords nil)
  1889. (make-variable-buffer-local 'org-done-keywords)
  1890. (defvar org-todo-heads nil)
  1891. (make-variable-buffer-local 'org-todo-heads)
  1892. (defvar org-todo-sets nil)
  1893. (make-variable-buffer-local 'org-todo-sets)
  1894. (defvar org-todo-log-states nil)
  1895. (make-variable-buffer-local 'org-todo-log-states)
  1896. (defvar org-todo-kwd-alist nil)
  1897. (make-variable-buffer-local 'org-todo-kwd-alist)
  1898. (defvar org-todo-key-alist nil)
  1899. (make-variable-buffer-local 'org-todo-key-alist)
  1900. (defvar org-todo-key-trigger nil)
  1901. (make-variable-buffer-local 'org-todo-key-trigger)
  1902. (defcustom org-todo-interpretation 'sequence
  1903. "Controls how TODO keywords are interpreted.
  1904. This variable is in principle obsolete and is only used for
  1905. backward compatibility, if the interpretation of todo keywords is
  1906. not given already in `org-todo-keywords'. See that variable for
  1907. more information."
  1908. :group 'org-todo
  1909. :group 'org-keywords
  1910. :type '(choice (const sequence)
  1911. (const type)))
  1912. (defcustom org-use-fast-todo-selection t
  1913. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1914. This variable describes if and under what circumstances the cycling
  1915. mechanism for TODO keywords will be replaced by a single-key, direct
  1916. selection scheme.
  1917. When nil, fast selection is never used.
  1918. When the symbol `prefix', it will be used when `org-todo' is called with
  1919. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1920. in an agenda buffer.
  1921. When t, fast selection is used by default. In this case, the prefix
  1922. argument forces cycling instead.
  1923. In all cases, the special interface is only used if access keys have actually
  1924. been assigned by the user, i.e. if keywords in the configuration are followed
  1925. by a letter in parenthesis, like TODO(t)."
  1926. :group 'org-todo
  1927. :type '(choice
  1928. (const :tag "Never" nil)
  1929. (const :tag "By default" t)
  1930. (const :tag "Only with C-u C-c C-t" prefix)))
  1931. (defcustom org-provide-todo-statistics t
  1932. "Non-nil means update todo statistics after insert and toggle.
  1933. ALL-HEADLINES means update todo statistics by including headlines
  1934. with no TODO keyword as well, counting them as not done.
  1935. A list of TODO keywords means the same, but skip keywords that are
  1936. not in this list.
  1937. When this is set, todo statistics is updated in the parent of the
  1938. current entry each time a todo state is changed."
  1939. :group 'org-todo
  1940. :type '(choice
  1941. (const :tag "Yes, only for TODO entries" t)
  1942. (const :tag "Yes, including all entries" 'all-headlines)
  1943. (repeat :tag "Yes, for TODOs in this list"
  1944. (string :tag "TODO keyword"))
  1945. (other :tag "No TODO statistics" nil)))
  1946. (defcustom org-hierarchical-todo-statistics t
  1947. "Non-nil means TODO statistics covers just direct children.
  1948. When nil, all entries in the subtree are considered.
  1949. This has only an effect if `org-provide-todo-statistics' is set.
  1950. To set this to nil for only a single subtree, use a COOKIE_DATA
  1951. property and include the word \"recursive\" into the value."
  1952. :group 'org-todo
  1953. :type 'boolean)
  1954. (defcustom org-after-todo-state-change-hook nil
  1955. "Hook which is run after the state of a TODO item was changed.
  1956. The new state (a string with a TODO keyword, or nil) is available in the
  1957. Lisp variable `state'."
  1958. :group 'org-todo
  1959. :type 'hook)
  1960. (defvar org-blocker-hook nil
  1961. "Hook for functions that are allowed to block a state change.
  1962. Each function gets as its single argument a property list, see
  1963. `org-trigger-hook' for more information about this list.
  1964. If any of the functions in this hook returns nil, the state change
  1965. is blocked.")
  1966. (defvar org-trigger-hook nil
  1967. "Hook for functions that are triggered by a state change.
  1968. Each function gets as its single argument a property list with at least
  1969. the following elements:
  1970. (:type type-of-change :position pos-at-entry-start
  1971. :from old-state :to new-state)
  1972. Depending on the type, more properties may be present.
  1973. This mechanism is currently implemented for:
  1974. TODO state changes
  1975. ------------------
  1976. :type todo-state-change
  1977. :from previous state (keyword as a string), or nil, or a symbol
  1978. 'todo' or 'done', to indicate the general type of state.
  1979. :to new state, like in :from")
  1980. (defcustom org-enforce-todo-dependencies nil
  1981. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1982. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1983. be blocked if any prior sibling is not yet done.
  1984. Finally, if the parent is blocked because of ordered siblings of its own,
  1985. the child will also be blocked.
  1986. This variable needs to be set before org.el is loaded, and you need to
  1987. restart Emacs after a change to make the change effective. The only way
  1988. to change is while Emacs is running is through the customize interface."
  1989. :set (lambda (var val)
  1990. (set var val)
  1991. (if val
  1992. (add-hook 'org-blocker-hook
  1993. 'org-block-todo-from-children-or-siblings-or-parent)
  1994. (remove-hook 'org-blocker-hook
  1995. 'org-block-todo-from-children-or-siblings-or-parent)))
  1996. :group 'org-todo
  1997. :type 'boolean)
  1998. (defcustom org-enforce-todo-checkbox-dependencies nil
  1999. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2000. When this is nil, checkboxes have no influence on switching TODO states.
  2001. When non-nil, you first need to check off all check boxes before the TODO
  2002. entry can be switched to DONE.
  2003. This variable needs to be set before org.el is loaded, and you need to
  2004. restart Emacs after a change to make the change effective. The only way
  2005. to change is while Emacs is running is through the customize interface."
  2006. :set (lambda (var val)
  2007. (set var val)
  2008. (if val
  2009. (add-hook 'org-blocker-hook
  2010. 'org-block-todo-from-checkboxes)
  2011. (remove-hook 'org-blocker-hook
  2012. 'org-block-todo-from-checkboxes)))
  2013. :group 'org-todo
  2014. :type 'boolean)
  2015. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2016. "Non-nil means inserting a TODO heading is treated as state change.
  2017. So when the command \\[org-insert-todo-heading] is used, state change
  2018. logging will apply if appropriate. When nil, the new TODO item will
  2019. be inserted directly, and no logging will take place."
  2020. :group 'org-todo
  2021. :type 'boolean)
  2022. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2023. "Non-nil means switching TODO states with S-cursor counts as state change.
  2024. This is the default behavior. However, setting this to nil allows a
  2025. convenient way to select a TODO state and bypass any logging associated
  2026. with that."
  2027. :group 'org-todo
  2028. :type 'boolean)
  2029. (defcustom org-todo-state-tags-triggers nil
  2030. "Tag changes that should be triggered by TODO state changes.
  2031. This is a list. Each entry is
  2032. (state-change (tag . flag) .......)
  2033. State-change can be a string with a state, and empty string to indicate the
  2034. state that has no TODO keyword, or it can be one of the symbols `todo'
  2035. or `done', meaning any not-done or done state, respectively."
  2036. :group 'org-todo
  2037. :group 'org-tags
  2038. :type '(repeat
  2039. (cons (choice :tag "When changing to"
  2040. (const :tag "Not-done state" todo)
  2041. (const :tag "Done state" done)
  2042. (string :tag "State"))
  2043. (repeat
  2044. (cons :tag "Tag action"
  2045. (string :tag "Tag")
  2046. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2047. (defcustom org-log-done nil
  2048. "Information to record when a task moves to the DONE state.
  2049. Possible values are:
  2050. nil Don't add anything, just change the keyword
  2051. time Add a time stamp to the task
  2052. note Prompt for a note and add it with template `org-log-note-headings'
  2053. This option can also be set with on a per-file-basis with
  2054. #+STARTUP: nologdone
  2055. #+STARTUP: logdone
  2056. #+STARTUP: lognotedone
  2057. You can have local logging settings for a subtree by setting the LOGGING
  2058. property to one or more of these keywords."
  2059. :group 'org-todo
  2060. :group 'org-progress
  2061. :type '(choice
  2062. (const :tag "No logging" nil)
  2063. (const :tag "Record CLOSED timestamp" time)
  2064. (const :tag "Record CLOSED timestamp with note." note)))
  2065. ;; Normalize old uses of org-log-done.
  2066. (cond
  2067. ((eq org-log-done t) (setq org-log-done 'time))
  2068. ((and (listp org-log-done) (memq 'done org-log-done))
  2069. (setq org-log-done 'note)))
  2070. (defcustom org-log-reschedule nil
  2071. "Information to record when the scheduling date of a tasks is modified.
  2072. Possible values are:
  2073. nil Don't add anything, just change the date
  2074. time Add a time stamp to the task
  2075. note Prompt for a note and add it with template `org-log-note-headings'
  2076. This option can also be set with on a per-file-basis with
  2077. #+STARTUP: nologreschedule
  2078. #+STARTUP: logreschedule
  2079. #+STARTUP: lognotereschedule"
  2080. :group 'org-todo
  2081. :group 'org-progress
  2082. :type '(choice
  2083. (const :tag "No logging" nil)
  2084. (const :tag "Record timestamp" time)
  2085. (const :tag "Record timestamp with note." note)))
  2086. (defcustom org-log-redeadline nil
  2087. "Information to record when the deadline date of a tasks is modified.
  2088. Possible values are:
  2089. nil Don't add anything, just change the date
  2090. time Add a time stamp to the task
  2091. note Prompt for a note and add it with template `org-log-note-headings'
  2092. This option can also be set with on a per-file-basis with
  2093. #+STARTUP: nologredeadline
  2094. #+STARTUP: logredeadline
  2095. #+STARTUP: lognoteredeadline
  2096. You can have local logging settings for a subtree by setting the LOGGING
  2097. property to one or more of these keywords."
  2098. :group 'org-todo
  2099. :group 'org-progress
  2100. :type '(choice
  2101. (const :tag "No logging" nil)
  2102. (const :tag "Record timestamp" time)
  2103. (const :tag "Record timestamp with note." note)))
  2104. (defcustom org-log-note-clock-out nil
  2105. "Non-nil means record a note when clocking out of an item.
  2106. This can also be configured on a per-file basis by adding one of
  2107. the following lines anywhere in the buffer:
  2108. #+STARTUP: lognoteclock-out
  2109. #+STARTUP: nolognoteclock-out"
  2110. :group 'org-todo
  2111. :group 'org-progress
  2112. :type 'boolean)
  2113. (defcustom org-log-done-with-time t
  2114. "Non-nil means the CLOSED time stamp will contain date and time.
  2115. When nil, only the date will be recorded."
  2116. :group 'org-progress
  2117. :type 'boolean)
  2118. (defcustom org-log-note-headings
  2119. '((done . "CLOSING NOTE %t")
  2120. (state . "State %-12s from %-12S %t")
  2121. (note . "Note taken on %t")
  2122. (reschedule . "Rescheduled from %S on %t")
  2123. (delschedule . "Not scheduled, was %S on %t")
  2124. (redeadline . "New deadline from %S on %t")
  2125. (deldeadline . "Removed deadline, was %S on %t")
  2126. (refile . "Refiled on %t")
  2127. (clock-out . ""))
  2128. "Headings for notes added to entries.
  2129. The value is an alist, with the car being a symbol indicating the note
  2130. context, and the cdr is the heading to be used. The heading may also be the
  2131. empty string.
  2132. %t in the heading will be replaced by a time stamp.
  2133. %T will be an active time stamp instead the default inactive one
  2134. %d will be replaced by a short-format time stamp.
  2135. %D will be replaced by an active short-format time stamp.
  2136. %s will be replaced by the new TODO state, in double quotes.
  2137. %S will be replaced by the old TODO state, in double quotes.
  2138. %u will be replaced by the user name.
  2139. %U will be replaced by the full user name.
  2140. In fact, it is not a good idea to change the `state' entry, because
  2141. agenda log mode depends on the format of these entries."
  2142. :group 'org-todo
  2143. :group 'org-progress
  2144. :type '(list :greedy t
  2145. (cons (const :tag "Heading when closing an item" done) string)
  2146. (cons (const :tag
  2147. "Heading when changing todo state (todo sequence only)"
  2148. state) string)
  2149. (cons (const :tag "Heading when just taking a note" note) string)
  2150. (cons (const :tag "Heading when clocking out" clock-out) string)
  2151. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2152. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2153. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2154. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2155. (cons (const :tag "Heading when refiling" refile) string)))
  2156. (unless (assq 'note org-log-note-headings)
  2157. (push '(note . "%t") org-log-note-headings))
  2158. (defcustom org-log-into-drawer nil
  2159. "Non-nil means insert state change notes and time stamps into a drawer.
  2160. When nil, state changes notes will be inserted after the headline and
  2161. any scheduling and clock lines, but not inside a drawer.
  2162. The value of this variable should be the name of the drawer to use.
  2163. LOGBOOK is proposed as the default drawer for this purpose, you can
  2164. also set this to a string to define the drawer of your choice.
  2165. A value of t is also allowed, representing \"LOGBOOK\".
  2166. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2167. will be ignored.
  2168. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2169. a subtree."
  2170. :group 'org-todo
  2171. :group 'org-progress
  2172. :type '(choice
  2173. (const :tag "Not into a drawer" nil)
  2174. (const :tag "LOGBOOK" t)
  2175. (string :tag "Other")))
  2176. (if (fboundp 'defvaralias)
  2177. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2178. (defun org-log-into-drawer ()
  2179. "Return the value of `org-log-into-drawer', but let properties overrule.
  2180. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2181. used instead of the default value."
  2182. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2183. (cond
  2184. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2185. ((equal p "t") "LOGBOOK")
  2186. (t p))))
  2187. (defcustom org-log-state-notes-insert-after-drawers nil
  2188. "Non-nil means insert state change notes after any drawers in entry.
  2189. Only the drawers that *immediately* follow the headline and the
  2190. deadline/scheduled line are skipped.
  2191. When nil, insert notes right after the heading and perhaps the line
  2192. with deadline/scheduling if present.
  2193. This variable will have no effect if `org-log-into-drawer' is
  2194. set."
  2195. :group 'org-todo
  2196. :group 'org-progress
  2197. :type 'boolean)
  2198. (defcustom org-log-states-order-reversed t
  2199. "Non-nil means the latest state note will be directly after heading.
  2200. When nil, the state change notes will be ordered according to time."
  2201. :group 'org-todo
  2202. :group 'org-progress
  2203. :type 'boolean)
  2204. (defcustom org-todo-repeat-to-state nil
  2205. "The TODO state to which a repeater should return the repeating task.
  2206. By default this is the first task in a TODO sequence, or the previous state
  2207. in a TODO_TYP set. But you can specify another task here.
  2208. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2209. :group 'org-todo
  2210. :version "24.1"
  2211. :type '(choice (const :tag "Head of sequence" nil)
  2212. (string :tag "Specific state")))
  2213. (defcustom org-log-repeat 'time
  2214. "Non-nil means record moving through the DONE state when triggering repeat.
  2215. An auto-repeating task is immediately switched back to TODO when
  2216. marked DONE. If you are not logging state changes (by adding \"@\"
  2217. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2218. record a closing note, there will be no record of the task moving
  2219. through DONE. This variable forces taking a note anyway.
  2220. nil Don't force a record
  2221. time Record a time stamp
  2222. note Record a note
  2223. This option can also be set with on a per-file-basis with
  2224. #+STARTUP: logrepeat
  2225. #+STARTUP: lognoterepeat
  2226. #+STARTUP: nologrepeat
  2227. You can have local logging settings for a subtree by setting the LOGGING
  2228. property to one or more of these keywords."
  2229. :group 'org-todo
  2230. :group 'org-progress
  2231. :type '(choice
  2232. (const :tag "Don't force a record" nil)
  2233. (const :tag "Force recording the DONE state" time)
  2234. (const :tag "Force recording a note with the DONE state" note)))
  2235. (defgroup org-priorities nil
  2236. "Priorities in Org-mode."
  2237. :tag "Org Priorities"
  2238. :group 'org-todo)
  2239. (defcustom org-enable-priority-commands t
  2240. "Non-nil means priority commands are active.
  2241. When nil, these commands will be disabled, so that you never accidentally
  2242. set a priority."
  2243. :group 'org-priorities
  2244. :type 'boolean)
  2245. (defcustom org-highest-priority ?A
  2246. "The highest priority of TODO items. A character like ?A, ?B etc.
  2247. Must have a smaller ASCII number than `org-lowest-priority'."
  2248. :group 'org-priorities
  2249. :type 'character)
  2250. (defcustom org-lowest-priority ?C
  2251. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2252. Must have a larger ASCII number than `org-highest-priority'."
  2253. :group 'org-priorities
  2254. :type 'character)
  2255. (defcustom org-default-priority ?B
  2256. "The default priority of TODO items.
  2257. This is the priority an item gets if no explicit priority is given.
  2258. When starting to cycle on an empty priority the first step in the cycle
  2259. depends on `org-priority-start-cycle-with-default'. The resulting first
  2260. step priority must not exceed the range from `org-highest-priority' to
  2261. `org-lowest-priority' which means that `org-default-priority' has to be
  2262. in this range exclusive or inclusive the range boundaries. Else the
  2263. first step refuses to set the default and the second will fall back
  2264. to (depending on the command used) the highest or lowest priority."
  2265. :group 'org-priorities
  2266. :type 'character)
  2267. (defcustom org-priority-start-cycle-with-default t
  2268. "Non-nil means start with default priority when starting to cycle.
  2269. When this is nil, the first step in the cycle will be (depending on the
  2270. command used) one higher or lower than the default priority.
  2271. See also `org-default-priority'."
  2272. :group 'org-priorities
  2273. :type 'boolean)
  2274. (defcustom org-get-priority-function nil
  2275. "Function to extract the priority from a string.
  2276. The string is normally the headline. If this is nil Org computes the
  2277. priority from the priority cookie like [#A] in the headline. It returns
  2278. an integer, increasing by 1000 for each priority level.
  2279. The user can set a different function here, which should take a string
  2280. as an argument and return the numeric priority."
  2281. :group 'org-priorities
  2282. :version "24.1"
  2283. :type 'function)
  2284. (defgroup org-time nil
  2285. "Options concerning time stamps and deadlines in Org-mode."
  2286. :tag "Org Time"
  2287. :group 'org)
  2288. (defcustom org-insert-labeled-timestamps-at-point nil
  2289. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2290. When nil, these labeled time stamps are forces into the second line of an
  2291. entry, just after the headline. When scheduling from the global TODO list,
  2292. the time stamp will always be forced into the second line."
  2293. :group 'org-time
  2294. :type 'boolean)
  2295. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2296. "Formats for `format-time-string' which are used for time stamps.
  2297. It is not recommended to change this constant.")
  2298. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2299. "Number of minutes to round time stamps to.
  2300. These are two values, the first applies when first creating a time stamp.
  2301. The second applies when changing it with the commands `S-up' and `S-down'.
  2302. When changing the time stamp, this means that it will change in steps
  2303. of N minutes, as given by the second value.
  2304. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2305. numbers should be factors of 60, so for example 5, 10, 15.
  2306. When this is larger than 1, you can still force an exact time stamp by using
  2307. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2308. and by using a prefix arg to `S-up/down' to specify the exact number
  2309. of minutes to shift."
  2310. :group 'org-time
  2311. :get #'(lambda (var) ; Make sure both elements are there
  2312. (if (integerp (default-value var))
  2313. (list (default-value var) 5)
  2314. (default-value var)))
  2315. :type '(list
  2316. (integer :tag "when inserting times")
  2317. (integer :tag "when modifying times")))
  2318. ;; Normalize old customizations of this variable.
  2319. (when (integerp org-time-stamp-rounding-minutes)
  2320. (setq org-time-stamp-rounding-minutes
  2321. (list org-time-stamp-rounding-minutes
  2322. org-time-stamp-rounding-minutes)))
  2323. (defcustom org-display-custom-times nil
  2324. "Non-nil means overlay custom formats over all time stamps.
  2325. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2326. To turn this on on a per-file basis, insert anywhere in the file:
  2327. #+STARTUP: customtime"
  2328. :group 'org-time
  2329. :set 'set-default
  2330. :type 'sexp)
  2331. (make-variable-buffer-local 'org-display-custom-times)
  2332. (defcustom org-time-stamp-custom-formats
  2333. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2334. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2335. These are overlaid over the default ISO format if the variable
  2336. `org-display-custom-times' is set. Time like %H:%M should be at the
  2337. end of the second format. The custom formats are also honored by export
  2338. commands, if custom time display is turned on at the time of export."
  2339. :group 'org-time
  2340. :type 'sexp)
  2341. (defun org-time-stamp-format (&optional long inactive)
  2342. "Get the right format for a time string."
  2343. (let ((f (if long (cdr org-time-stamp-formats)
  2344. (car org-time-stamp-formats))))
  2345. (if inactive
  2346. (concat "[" (substring f 1 -1) "]")
  2347. f)))
  2348. (defcustom org-time-clocksum-format "%d:%02d"
  2349. "The format string used when creating CLOCKSUM lines.
  2350. This is also used when org-mode generates a time duration."
  2351. :group 'org-time
  2352. :type 'string)
  2353. (defcustom org-time-clocksum-use-fractional nil
  2354. "If non-nil, \\[org-clock-display] uses fractional times.
  2355. org-mode generates a time duration."
  2356. :group 'org-time
  2357. :type 'boolean)
  2358. (defcustom org-time-clocksum-fractional-format "%.2f"
  2359. "The format string used when creating CLOCKSUM lines, or when
  2360. org-mode generates a time duration."
  2361. :group 'org-time
  2362. :type 'string)
  2363. (defcustom org-deadline-warning-days 14
  2364. "No. of days before expiration during which a deadline becomes active.
  2365. This variable governs the display in sparse trees and in the agenda.
  2366. When 0 or negative, it means use this number (the absolute value of it)
  2367. even if a deadline has a different individual lead time specified.
  2368. Custom commands can set this variable in the options section."
  2369. :group 'org-time
  2370. :group 'org-agenda-daily/weekly
  2371. :type 'integer)
  2372. (defcustom org-read-date-prefer-future t
  2373. "Non-nil means assume future for incomplete date input from user.
  2374. This affects the following situations:
  2375. 1. The user gives a month but not a year.
  2376. For example, if it is April and you enter \"feb 2\", this will be read
  2377. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2378. 2. The user gives a day, but no month.
  2379. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2380. read this as the third of *next* month. However, if you enter \"17\",
  2381. it will be considered as *this* month.
  2382. If you set this variable to the symbol `time', then also the following
  2383. will work:
  2384. 3. If the user gives a time, but no day. If the time is before now,
  2385. to will be interpreted as tomorrow.
  2386. Currently none of this works for ISO week specifications.
  2387. When this option is nil, the current day, month and year will always be
  2388. used as defaults.
  2389. See also `org-agenda-jump-prefer-future'."
  2390. :group 'org-time
  2391. :type '(choice
  2392. (const :tag "Never" nil)
  2393. (const :tag "Check month and day" t)
  2394. (const :tag "Check month, day, and time" time)))
  2395. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2396. "Should the agenda jump command prefer the future for incomplete dates?
  2397. The default is to do the same as configured in `org-read-date-prefer-future'.
  2398. But you can also set a deviating value here.
  2399. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2400. :group 'org-agenda
  2401. :group 'org-time
  2402. :version "24.1"
  2403. :type '(choice
  2404. (const :tag "Use org-read-date-prefer-future"
  2405. org-read-date-prefer-future)
  2406. (const :tag "Never" nil)
  2407. (const :tag "Always" t)))
  2408. (defcustom org-read-date-force-compatible-dates t
  2409. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2410. Depending on the system Emacs is running on, certain dates cannot
  2411. be represented with the type used internally to represent time.
  2412. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2413. correctly. Some systems allow for earlier dates, some for later,
  2414. some for both. One way to find out it to insert any date into an
  2415. Org buffer, putting the cursor on the year and hitting S-up and
  2416. S-down to test the range.
  2417. When this variable is set to t, the date/time prompt will not let
  2418. you specify dates outside the 1970-2037 range, so it is certain that
  2419. these dates will work in whatever version of Emacs you are
  2420. running, and also that you can move a file from one Emacs implementation
  2421. to another. WHenever Org is forcing the year for you, it will display
  2422. a message and beep.
  2423. When this variable is nil, Org will check if the date is
  2424. representable in the specific Emacs implementation you are using.
  2425. If not, it will force a year, usually the current year, and beep
  2426. to remind you. Currently this setting is not recommended because
  2427. the likelihood that you will open your Org files in an Emacs that
  2428. has limited date range is not negligible.
  2429. A workaround for this problem is to use diary sexp dates for time
  2430. stamps outside of this range."
  2431. :group 'org-time
  2432. :version "24.1"
  2433. :type 'boolean)
  2434. (defcustom org-read-date-display-live t
  2435. "Non-nil means display current interpretation of date prompt live.
  2436. This display will be in an overlay, in the minibuffer."
  2437. :group 'org-time
  2438. :type 'boolean)
  2439. (defcustom org-read-date-popup-calendar t
  2440. "Non-nil means pop up a calendar when prompting for a date.
  2441. In the calendar, the date can be selected with mouse-1. However, the
  2442. minibuffer will also be active, and you can simply enter the date as well.
  2443. When nil, only the minibuffer will be available."
  2444. :group 'org-time
  2445. :type 'boolean)
  2446. (if (fboundp 'defvaralias)
  2447. (defvaralias 'org-popup-calendar-for-date-prompt
  2448. 'org-read-date-popup-calendar))
  2449. (defcustom org-read-date-minibuffer-setup-hook nil
  2450. "Hook to be used to set up keys for the date/time interface.
  2451. Add key definitions to `minibuffer-local-map', which will be a temporary
  2452. copy."
  2453. :group 'org-time
  2454. :type 'hook)
  2455. (defcustom org-extend-today-until 0
  2456. "The hour when your day really ends. Must be an integer.
  2457. This has influence for the following applications:
  2458. - When switching the agenda to \"today\". It it is still earlier than
  2459. the time given here, the day recognized as TODAY is actually yesterday.
  2460. - When a date is read from the user and it is still before the time given
  2461. here, the current date and time will be assumed to be yesterday, 23:59.
  2462. Also, timestamps inserted in remember templates follow this rule.
  2463. IMPORTANT: This is a feature whose implementation is and likely will
  2464. remain incomplete. Really, it is only here because past midnight seems to
  2465. be the favorite working time of John Wiegley :-)"
  2466. :group 'org-time
  2467. :type 'integer)
  2468. (defcustom org-use-effective-time nil
  2469. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2470. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2471. \"effective time\" of any timestamps between midnight and 8am will be
  2472. 23:59 of the previous day."
  2473. :group 'boolean
  2474. :version "24.1"
  2475. :type 'integer)
  2476. (defcustom org-edit-timestamp-down-means-later nil
  2477. "Non-nil means S-down will increase the time in a time stamp.
  2478. When nil, S-up will increase."
  2479. :group 'org-time
  2480. :type 'boolean)
  2481. (defcustom org-calendar-follow-timestamp-change t
  2482. "Non-nil means make the calendar window follow timestamp changes.
  2483. When a timestamp is modified and the calendar window is visible, it will be
  2484. moved to the new date."
  2485. :group 'org-time
  2486. :type 'boolean)
  2487. (defgroup org-tags nil
  2488. "Options concerning tags in Org-mode."
  2489. :tag "Org Tags"
  2490. :group 'org)
  2491. (defcustom org-tag-alist nil
  2492. "List of tags allowed in Org-mode files.
  2493. When this list is nil, Org-mode will base TAG input on what is already in the
  2494. buffer.
  2495. The value of this variable is an alist, the car of each entry must be a
  2496. keyword as a string, the cdr may be a character that is used to select
  2497. that tag through the fast-tag-selection interface.
  2498. See the manual for details."
  2499. :group 'org-tags
  2500. :type '(repeat
  2501. (choice
  2502. (cons (string :tag "Tag name")
  2503. (character :tag "Access char"))
  2504. (list :tag "Start radio group"
  2505. (const :startgroup)
  2506. (option (string :tag "Group description")))
  2507. (list :tag "End radio group"
  2508. (const :endgroup)
  2509. (option (string :tag "Group description")))
  2510. (const :tag "New line" (:newline)))))
  2511. (defcustom org-tag-persistent-alist nil
  2512. "List of tags that will always appear in all Org-mode files.
  2513. This is in addition to any in buffer settings or customizations
  2514. of `org-tag-alist'.
  2515. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2516. The value of this variable is an alist, the car of each entry must be a
  2517. keyword as a string, the cdr may be a character that is used to select
  2518. that tag through the fast-tag-selection interface.
  2519. See the manual for details.
  2520. To disable these tags on a per-file basis, insert anywhere in the file:
  2521. #+STARTUP: noptag"
  2522. :group 'org-tags
  2523. :type '(repeat
  2524. (choice
  2525. (cons (string :tag "Tag name")
  2526. (character :tag "Access char"))
  2527. (const :tag "Start radio group" (:startgroup))
  2528. (const :tag "End radio group" (:endgroup))
  2529. (const :tag "New line" (:newline)))))
  2530. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2531. "If non-nil, always offer completion for all tags of all agenda files.
  2532. Instead of customizing this variable directly, you might want to
  2533. set it locally for capture buffers, because there no list of
  2534. tags in that file can be created dynamically (there are none).
  2535. (add-hook 'org-capture-mode-hook
  2536. (lambda ()
  2537. (set (make-local-variable
  2538. 'org-complete-tags-always-offer-all-agenda-tags)
  2539. t)))"
  2540. :group 'org-tags
  2541. :version "24.1"
  2542. :type 'boolean)
  2543. (defvar org-file-tags nil
  2544. "List of tags that can be inherited by all entries in the file.
  2545. The tags will be inherited if the variable `org-use-tag-inheritance'
  2546. says they should be.
  2547. This variable is populated from #+FILETAGS lines.")
  2548. (defcustom org-use-fast-tag-selection 'auto
  2549. "Non-nil means use fast tag selection scheme.
  2550. This is a special interface to select and deselect tags with single keys.
  2551. When nil, fast selection is never used.
  2552. When the symbol `auto', fast selection is used if and only if selection
  2553. characters for tags have been configured, either through the variable
  2554. `org-tag-alist' or through a #+TAGS line in the buffer.
  2555. When t, fast selection is always used and selection keys are assigned
  2556. automatically if necessary."
  2557. :group 'org-tags
  2558. :type '(choice
  2559. (const :tag "Always" t)
  2560. (const :tag "Never" nil)
  2561. (const :tag "When selection characters are configured" 'auto)))
  2562. (defcustom org-fast-tag-selection-single-key nil
  2563. "Non-nil means fast tag selection exits after first change.
  2564. When nil, you have to press RET to exit it.
  2565. During fast tag selection, you can toggle this flag with `C-c'.
  2566. This variable can also have the value `expert'. In this case, the window
  2567. displaying the tags menu is not even shown, until you press C-c again."
  2568. :group 'org-tags
  2569. :type '(choice
  2570. (const :tag "No" nil)
  2571. (const :tag "Yes" t)
  2572. (const :tag "Expert" expert)))
  2573. (defvar org-fast-tag-selection-include-todo nil
  2574. "Non-nil means fast tags selection interface will also offer TODO states.
  2575. This is an undocumented feature, you should not rely on it.")
  2576. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2577. "The column to which tags should be indented in a headline.
  2578. If this number is positive, it specifies the column. If it is negative,
  2579. it means that the tags should be flushright to that column. For example,
  2580. -80 works well for a normal 80 character screen.
  2581. When 0, place tags directly after headline text, with only one space in
  2582. between."
  2583. :group 'org-tags
  2584. :type 'integer)
  2585. (defcustom org-auto-align-tags t
  2586. "Non-nil keeps tags aligned when modifying headlines.
  2587. Some operations (i.e. demoting) change the length of a headline and
  2588. therefore shift the tags around. With this option turned on, after
  2589. each such operation the tags are again aligned to `org-tags-column'."
  2590. :group 'org-tags
  2591. :type 'boolean)
  2592. (defcustom org-use-tag-inheritance t
  2593. "Non-nil means tags in levels apply also for sublevels.
  2594. When nil, only the tags directly given in a specific line apply there.
  2595. This may also be a list of tags that should be inherited, or a regexp that
  2596. matches tags that should be inherited. Additional control is possible
  2597. with the variable `org-tags-exclude-from-inheritance' which gives an
  2598. explicit list of tags to be excluded from inheritance., even if the value of
  2599. `org-use-tag-inheritance' would select it for inheritance.
  2600. If this option is t, a match early-on in a tree can lead to a large
  2601. number of matches in the subtree when constructing the agenda or creating
  2602. a sparse tree. If you only want to see the first match in a tree during
  2603. a search, check out the variable `org-tags-match-list-sublevels'."
  2604. :group 'org-tags
  2605. :type '(choice
  2606. (const :tag "Not" nil)
  2607. (const :tag "Always" t)
  2608. (repeat :tag "Specific tags" (string :tag "Tag"))
  2609. (regexp :tag "Tags matched by regexp")))
  2610. (defcustom org-tags-exclude-from-inheritance nil
  2611. "List of tags that should never be inherited.
  2612. This is a way to exclude a few tags from inheritance. For way to do
  2613. the opposite, to actively allow inheritance for selected tags,
  2614. see the variable `org-use-tag-inheritance'."
  2615. :group 'org-tags
  2616. :type '(repeat (string :tag "Tag")))
  2617. (defun org-tag-inherit-p (tag)
  2618. "Check if TAG is one that should be inherited."
  2619. (cond
  2620. ((member tag org-tags-exclude-from-inheritance) nil)
  2621. ((eq org-use-tag-inheritance t) t)
  2622. ((not org-use-tag-inheritance) nil)
  2623. ((stringp org-use-tag-inheritance)
  2624. (string-match org-use-tag-inheritance tag))
  2625. ((listp org-use-tag-inheritance)
  2626. (member tag org-use-tag-inheritance))
  2627. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2628. (defcustom org-tags-match-list-sublevels t
  2629. "Non-nil means list also sublevels of headlines matching a search.
  2630. This variable applies to tags/property searches, and also to stuck
  2631. projects because this search is based on a tags match as well.
  2632. When set to the symbol `indented', sublevels are indented with
  2633. leading dots.
  2634. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2635. the sublevels of a headline matching a tag search often also match
  2636. the same search. Listing all of them can create very long lists.
  2637. Setting this variable to nil causes subtrees of a match to be skipped.
  2638. This variable is semi-obsolete and probably should always be true. It
  2639. is better to limit inheritance to certain tags using the variables
  2640. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2641. :group 'org-tags
  2642. :type '(choice
  2643. (const :tag "No, don't list them" nil)
  2644. (const :tag "Yes, do list them" t)
  2645. (const :tag "List them, indented with leading dots" indented)))
  2646. (defcustom org-tags-sort-function nil
  2647. "When set, tags are sorted using this function as a comparator."
  2648. :group 'org-tags
  2649. :type '(choice
  2650. (const :tag "No sorting" nil)
  2651. (const :tag "Alphabetical" string<)
  2652. (const :tag "Reverse alphabetical" string>)
  2653. (function :tag "Custom function" nil)))
  2654. (defvar org-tags-history nil
  2655. "History of minibuffer reads for tags.")
  2656. (defvar org-last-tags-completion-table nil
  2657. "The last used completion table for tags.")
  2658. (defvar org-after-tags-change-hook nil
  2659. "Hook that is run after the tags in a line have changed.")
  2660. (defgroup org-properties nil
  2661. "Options concerning properties in Org-mode."
  2662. :tag "Org Properties"
  2663. :group 'org)
  2664. (defcustom org-property-format "%-10s %s"
  2665. "How property key/value pairs should be formatted by `indent-line'.
  2666. When `indent-line' hits a property definition, it will format the line
  2667. according to this format, mainly to make sure that the values are
  2668. lined-up with respect to each other."
  2669. :group 'org-properties
  2670. :type 'string)
  2671. (defcustom org-properties-postprocess-alist nil
  2672. "Alist of properties and functions to adjust inserted values.
  2673. Elements of this alist must be of the form
  2674. ([string] [function])
  2675. where [string] must be a property name and [function] must be a
  2676. lambda expression: this lambda expression must take one argument,
  2677. the value to adjust, and return the new value as a string.
  2678. For example, this element will allow the property \"Remaining\"
  2679. to be updated wrt the relation between the \"Effort\" property
  2680. and the clock summary:
  2681. ((\"Remaining\" (lambda(value)
  2682. (let ((clocksum (org-clock-sum-current-item))
  2683. (effort (org-duration-string-to-minutes
  2684. (org-entry-get (point) \"Effort\"))))
  2685. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2686. :group 'org-properties
  2687. :version "24.1"
  2688. :type 'alist)
  2689. (defcustom org-use-property-inheritance nil
  2690. "Non-nil means properties apply also for sublevels.
  2691. This setting is chiefly used during property searches. Turning it on can
  2692. cause significant overhead when doing a search, which is why it is not
  2693. on by default.
  2694. When nil, only the properties directly given in the current entry count.
  2695. When t, every property is inherited. The value may also be a list of
  2696. properties that should have inheritance, or a regular expression matching
  2697. properties that should be inherited.
  2698. However, note that some special properties use inheritance under special
  2699. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2700. and the properties ending in \"_ALL\" when they are used as descriptor
  2701. for valid values of a property.
  2702. Note for programmers:
  2703. When querying an entry with `org-entry-get', you can control if inheritance
  2704. should be used. By default, `org-entry-get' looks only at the local
  2705. properties. You can request inheritance by setting the inherit argument
  2706. to t (to force inheritance) or to `selective' (to respect the setting
  2707. in this variable)."
  2708. :group 'org-properties
  2709. :type '(choice
  2710. (const :tag "Not" nil)
  2711. (const :tag "Always" t)
  2712. (repeat :tag "Specific properties" (string :tag "Property"))
  2713. (regexp :tag "Properties matched by regexp")))
  2714. (defun org-property-inherit-p (property)
  2715. "Check if PROPERTY is one that should be inherited."
  2716. (cond
  2717. ((eq org-use-property-inheritance t) t)
  2718. ((not org-use-property-inheritance) nil)
  2719. ((stringp org-use-property-inheritance)
  2720. (string-match org-use-property-inheritance property))
  2721. ((listp org-use-property-inheritance)
  2722. (member property org-use-property-inheritance))
  2723. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2724. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2725. "The default column format, if no other format has been defined.
  2726. This variable can be set on the per-file basis by inserting a line
  2727. #+COLUMNS: %25ITEM ....."
  2728. :group 'org-properties
  2729. :type 'string)
  2730. (defcustom org-columns-ellipses ".."
  2731. "The ellipses to be used when a field in column view is truncated.
  2732. When this is the empty string, as many characters as possible are shown,
  2733. but then there will be no visual indication that the field has been truncated.
  2734. When this is a string of length N, the last N characters of a truncated
  2735. field are replaced by this string. If the column is narrower than the
  2736. ellipses string, only part of the ellipses string will be shown."
  2737. :group 'org-properties
  2738. :type 'string)
  2739. (defcustom org-columns-modify-value-for-display-function nil
  2740. "Function that modifies values for display in column view.
  2741. For example, it can be used to cut out a certain part from a time stamp.
  2742. The function must take 2 arguments:
  2743. column-title The title of the column (*not* the property name)
  2744. value The value that should be modified.
  2745. The function should return the value that should be displayed,
  2746. or nil if the normal value should be used."
  2747. :group 'org-properties
  2748. :type 'function)
  2749. (defcustom org-effort-property "Effort"
  2750. "The property that is being used to keep track of effort estimates.
  2751. Effort estimates given in this property need to have the format H:MM."
  2752. :group 'org-properties
  2753. :group 'org-progress
  2754. :type '(string :tag "Property"))
  2755. (defconst org-global-properties-fixed
  2756. '(("VISIBILITY_ALL" . "folded children content all")
  2757. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2758. "List of property/value pairs that can be inherited by any entry.
  2759. These are fixed values, for the preset properties. The user variable
  2760. that can be used to add to this list is `org-global-properties'.
  2761. The entries in this list are cons cells where the car is a property
  2762. name and cdr is a string with the value. If the value represents
  2763. multiple items like an \"_ALL\" property, separate the items by
  2764. spaces.")
  2765. (defcustom org-global-properties nil
  2766. "List of property/value pairs that can be inherited by any entry.
  2767. This list will be combined with the constant `org-global-properties-fixed'.
  2768. The entries in this list are cons cells where the car is a property
  2769. name and cdr is a string with the value.
  2770. You can set buffer-local values for the same purpose in the variable
  2771. `org-file-properties' this by adding lines like
  2772. #+PROPERTY: NAME VALUE"
  2773. :group 'org-properties
  2774. :type '(repeat
  2775. (cons (string :tag "Property")
  2776. (string :tag "Value"))))
  2777. (defvar org-file-properties nil
  2778. "List of property/value pairs that can be inherited by any entry.
  2779. Valid for the current buffer.
  2780. This variable is populated from #+PROPERTY lines.")
  2781. (make-variable-buffer-local 'org-file-properties)
  2782. (defgroup org-agenda nil
  2783. "Options concerning agenda views in Org-mode."
  2784. :tag "Org Agenda"
  2785. :group 'org)
  2786. (defvar org-category nil
  2787. "Variable used by org files to set a category for agenda display.
  2788. Such files should use a file variable to set it, for example
  2789. # -*- mode: org; org-category: \"ELisp\"
  2790. or contain a special line
  2791. #+CATEGORY: ELisp
  2792. If the file does not specify a category, then file's base name
  2793. is used instead.")
  2794. (make-variable-buffer-local 'org-category)
  2795. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2796. (defcustom org-agenda-files nil
  2797. "The files to be used for agenda display.
  2798. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2799. \\[org-remove-file]. You can also use customize to edit the list.
  2800. If an entry is a directory, all files in that directory that are matched by
  2801. `org-agenda-file-regexp' will be part of the file list.
  2802. If the value of the variable is not a list but a single file name, then
  2803. the list of agenda files is actually stored and maintained in that file, one
  2804. agenda file per line. In this file paths can be given relative to
  2805. `org-directory'. Tilde expansion and environment variable substitution
  2806. are also made."
  2807. :group 'org-agenda
  2808. :type '(choice
  2809. (repeat :tag "List of files and directories" file)
  2810. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2811. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2812. "Regular expression to match files for `org-agenda-files'.
  2813. If any element in the list in that variable contains a directory instead
  2814. of a normal file, all files in that directory that are matched by this
  2815. regular expression will be included."
  2816. :group 'org-agenda
  2817. :type 'regexp)
  2818. (defcustom org-agenda-text-search-extra-files nil
  2819. "List of extra files to be searched by text search commands.
  2820. These files will be search in addition to the agenda files by the
  2821. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2822. Note that these files will only be searched for text search commands,
  2823. not for the other agenda views like todo lists, tag searches or the weekly
  2824. agenda. This variable is intended to list notes and possibly archive files
  2825. that should also be searched by these two commands.
  2826. In fact, if the first element in the list is the symbol `agenda-archives',
  2827. than all archive files of all agenda files will be added to the search
  2828. scope."
  2829. :group 'org-agenda
  2830. :type '(set :greedy t
  2831. (const :tag "Agenda Archives" agenda-archives)
  2832. (repeat :inline t (file))))
  2833. (if (fboundp 'defvaralias)
  2834. (defvaralias 'org-agenda-multi-occur-extra-files
  2835. 'org-agenda-text-search-extra-files))
  2836. (defcustom org-agenda-skip-unavailable-files nil
  2837. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2838. A nil value means to remove them, after a query, from the list."
  2839. :group 'org-agenda
  2840. :type 'boolean)
  2841. (defcustom org-calendar-to-agenda-key [?c]
  2842. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2843. The command `org-calendar-goto-agenda' will be bound to this key. The
  2844. default is the character `c' because then `c' can be used to switch back and
  2845. forth between agenda and calendar."
  2846. :group 'org-agenda
  2847. :type 'sexp)
  2848. (defcustom org-calendar-agenda-action-key [?k]
  2849. "The key to be installed in `calendar-mode-map' for agenda-action.
  2850. The command `org-agenda-action' will be bound to this key. The
  2851. default is the character `k' because we use the same key in the agenda."
  2852. :group 'org-agenda
  2853. :type 'sexp)
  2854. (defcustom org-calendar-insert-diary-entry-key [?i]
  2855. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2856. This option is irrelevant until `org-agenda-diary-file' has been configured
  2857. to point to an Org-mode file. When that is the case, the command
  2858. `org-agenda-diary-entry' will be bound to the key given here, by default
  2859. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2860. if you want to continue doing this, you need to change this to a different
  2861. key."
  2862. :group 'org-agenda
  2863. :type 'sexp)
  2864. (defcustom org-agenda-diary-file 'diary-file
  2865. "File to which to add new entries with the `i' key in agenda and calendar.
  2866. When this is the symbol `diary-file', the functionality in the Emacs
  2867. calendar will be used to add entries to the `diary-file'. But when this
  2868. points to a file, `org-agenda-diary-entry' will be used instead."
  2869. :group 'org-agenda
  2870. :type '(choice
  2871. (const :tag "The standard Emacs diary file" diary-file)
  2872. (file :tag "Special Org file diary entries")))
  2873. (eval-after-load "calendar"
  2874. '(progn
  2875. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2876. 'org-calendar-goto-agenda)
  2877. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2878. 'org-agenda-action)
  2879. (add-hook 'calendar-mode-hook
  2880. (lambda ()
  2881. (unless (eq org-agenda-diary-file 'diary-file)
  2882. (define-key calendar-mode-map
  2883. org-calendar-insert-diary-entry-key
  2884. 'org-agenda-diary-entry))))))
  2885. (defgroup org-latex nil
  2886. "Options for embedding LaTeX code into Org-mode."
  2887. :tag "Org LaTeX"
  2888. :group 'org)
  2889. (defcustom org-format-latex-options
  2890. '(:foreground default :background default :scale 1.0
  2891. :html-foreground "Black" :html-background "Transparent"
  2892. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2893. "Options for creating images from LaTeX fragments.
  2894. This is a property list with the following properties:
  2895. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2896. `default' means use the foreground of the default face.
  2897. :background the background color, or \"Transparent\".
  2898. `default' means use the background of the default face.
  2899. :scale a scaling factor for the size of the images, to get more pixels
  2900. :html-foreground, :html-background, :html-scale
  2901. the same numbers for HTML export.
  2902. :matchers a list indicating which matchers should be used to
  2903. find LaTeX fragments. Valid members of this list are:
  2904. \"begin\" find environments
  2905. \"$1\" find single characters surrounded by $.$
  2906. \"$\" find math expressions surrounded by $...$
  2907. \"$$\" find math expressions surrounded by $$....$$
  2908. \"\\(\" find math expressions surrounded by \\(...\\)
  2909. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2910. :group 'org-latex
  2911. :type 'plist)
  2912. (defcustom org-format-latex-signal-error t
  2913. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2914. When nil, just push out a message."
  2915. :group 'org-latex
  2916. :version "24.1"
  2917. :type 'boolean)
  2918. (defcustom org-latex-to-mathml-jar-file nil
  2919. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2920. Use this to specify additional executable file say a jar file.
  2921. When using MathToWeb as the converter, specify the full-path to
  2922. your mathtoweb.jar file."
  2923. :group 'org-latex
  2924. :version "24.1"
  2925. :type '(choice
  2926. (const :tag "None" nil)
  2927. (file :tag "JAR file" :must-match t)))
  2928. (defcustom org-latex-to-mathml-convert-command nil
  2929. "Command to convert LaTeX fragments to MathML.
  2930. Replace format-specifiers in the command as noted below and use
  2931. `shell-command' to convert LaTeX to MathML.
  2932. %j: Executable file in fully expanded form as specified by
  2933. `org-latex-to-mathml-jar-file'.
  2934. %I: Input LaTeX file in fully expanded form
  2935. %o: Output MathML file
  2936. This command is used by `org-create-math-formula'.
  2937. When using MathToWeb as the converter, set this to
  2938. \"java -jar %j -unicode -force -df %o %I\"."
  2939. :group 'org-latex
  2940. :version "24.1"
  2941. :type '(choice
  2942. (const :tag "None" nil)
  2943. (string :tag "\nShell command")))
  2944. (defun org-format-latex-mathml-available-p ()
  2945. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2946. (save-match-data
  2947. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2948. org-latex-to-mathml-convert-command)
  2949. (let ((executable (car (split-string
  2950. org-latex-to-mathml-convert-command))))
  2951. (when (executable-find executable)
  2952. (if (string-match
  2953. "%j" org-latex-to-mathml-convert-command)
  2954. (file-readable-p org-latex-to-mathml-jar-file)
  2955. t))))))
  2956. (defcustom org-format-latex-header "\\documentclass{article}
  2957. \\usepackage[usenames]{color}
  2958. \\usepackage{amsmath}
  2959. \\usepackage[mathscr]{eucal}
  2960. \\pagestyle{empty} % do not remove
  2961. \[PACKAGES]
  2962. \[DEFAULT-PACKAGES]
  2963. % The settings below are copied from fullpage.sty
  2964. \\setlength{\\textwidth}{\\paperwidth}
  2965. \\addtolength{\\textwidth}{-3cm}
  2966. \\setlength{\\oddsidemargin}{1.5cm}
  2967. \\addtolength{\\oddsidemargin}{-2.54cm}
  2968. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2969. \\setlength{\\textheight}{\\paperheight}
  2970. \\addtolength{\\textheight}{-\\headheight}
  2971. \\addtolength{\\textheight}{-\\headsep}
  2972. \\addtolength{\\textheight}{-\\footskip}
  2973. \\addtolength{\\textheight}{-3cm}
  2974. \\setlength{\\topmargin}{1.5cm}
  2975. \\addtolength{\\topmargin}{-2.54cm}"
  2976. "The document header used for processing LaTeX fragments.
  2977. It is imperative that this header make sure that no page number
  2978. appears on the page. The package defined in the variables
  2979. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2980. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2981. will be appended."
  2982. :group 'org-latex
  2983. :type 'string)
  2984. (defvar org-format-latex-header-extra nil)
  2985. (defun org-set-packages-alist (var val)
  2986. "Set the packages alist and make sure it has 3 elements per entry."
  2987. (set var (mapcar (lambda (x)
  2988. (if (and (consp x) (= (length x) 2))
  2989. (list (car x) (nth 1 x) t)
  2990. x))
  2991. val)))
  2992. (defun org-get-packages-alist (var)
  2993. "Get the packages alist and make sure it has 3 elements per entry."
  2994. (mapcar (lambda (x)
  2995. (if (and (consp x) (= (length x) 2))
  2996. (list (car x) (nth 1 x) t)
  2997. x))
  2998. (default-value var)))
  2999. ;; The following variables are defined here because is it also used
  3000. ;; when formatting latex fragments. Originally it was part of the
  3001. ;; LaTeX exporter, which is why the name includes "export".
  3002. (defcustom org-export-latex-default-packages-alist
  3003. '(("AUTO" "inputenc" t)
  3004. ("T1" "fontenc" t)
  3005. ("" "fixltx2e" nil)
  3006. ("" "graphicx" t)
  3007. ("" "longtable" nil)
  3008. ("" "float" nil)
  3009. ("" "wrapfig" nil)
  3010. ("" "soul" t)
  3011. ("" "textcomp" t)
  3012. ("" "marvosym" t)
  3013. ("" "wasysym" t)
  3014. ("" "latexsym" t)
  3015. ("" "amssymb" t)
  3016. ("" "hyperref" nil)
  3017. "\\tolerance=1000"
  3018. )
  3019. "Alist of default packages to be inserted in the header.
  3020. Change this only if one of the packages here causes an incompatibility
  3021. with another package you are using.
  3022. The packages in this list are needed by one part or another of Org-mode
  3023. to function properly.
  3024. - inputenc, fontenc: for basic font and character selection
  3025. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3026. for interpreting the entities in `org-entities'. You can skip some of these
  3027. packages if you don't use any of the symbols in it.
  3028. - graphicx: for including images
  3029. - float, wrapfig: for figure placement
  3030. - longtable: for long tables
  3031. - hyperref: for cross references
  3032. Therefore you should not modify this variable unless you know what you
  3033. are doing. The one reason to change it anyway is that you might be loading
  3034. some other package that conflicts with one of the default packages.
  3035. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3036. If SNIPPET-FLAG is t, the package also needs to be included when
  3037. compiling LaTeX snippets into images for inclusion into HTML."
  3038. :group 'org-export-latex
  3039. :set 'org-set-packages-alist
  3040. :get 'org-get-packages-alist
  3041. :version "24.1"
  3042. :type '(repeat
  3043. (choice
  3044. (list :tag "options/package pair"
  3045. (string :tag "options")
  3046. (string :tag "package")
  3047. (boolean :tag "Snippet"))
  3048. (string :tag "A line of LaTeX"))))
  3049. (defcustom org-export-latex-packages-alist nil
  3050. "Alist of packages to be inserted in every LaTeX header.
  3051. These will be inserted after `org-export-latex-default-packages-alist'.
  3052. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3053. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3054. turning LaTeX snippets into images for inclusion into HTML.
  3055. Make sure that you only list packages here which:
  3056. - you want in every file
  3057. - do not conflict with the default packages in
  3058. `org-export-latex-default-packages-alist'
  3059. - do not conflict with the setup in `org-format-latex-header'."
  3060. :group 'org-export-latex
  3061. :set 'org-set-packages-alist
  3062. :get 'org-get-packages-alist
  3063. :type '(repeat
  3064. (choice
  3065. (list :tag "options/package pair"
  3066. (string :tag "options")
  3067. (string :tag "package")
  3068. (boolean :tag "Snippet"))
  3069. (string :tag "A line of LaTeX"))))
  3070. (defgroup org-appearance nil
  3071. "Settings for Org-mode appearance."
  3072. :tag "Org Appearance"
  3073. :group 'org)
  3074. (defcustom org-level-color-stars-only nil
  3075. "Non-nil means fontify only the stars in each headline.
  3076. When nil, the entire headline is fontified.
  3077. Changing it requires restart of `font-lock-mode' to become effective
  3078. also in regions already fontified."
  3079. :group 'org-appearance
  3080. :type 'boolean)
  3081. (defcustom org-hide-leading-stars nil
  3082. "Non-nil means hide the first N-1 stars in a headline.
  3083. This works by using the face `org-hide' for these stars. This
  3084. face is white for a light background, and black for a dark
  3085. background. You may have to customize the face `org-hide' to
  3086. make this work.
  3087. Changing it requires restart of `font-lock-mode' to become effective
  3088. also in regions already fontified.
  3089. You may also set this on a per-file basis by adding one of the following
  3090. lines to the buffer:
  3091. #+STARTUP: hidestars
  3092. #+STARTUP: showstars"
  3093. :group 'org-appearance
  3094. :type 'boolean)
  3095. (defcustom org-hidden-keywords nil
  3096. "List of symbols corresponding to keywords to be hidden the org buffer.
  3097. For example, a value '(title) for this list will make the document's title
  3098. appear in the buffer without the initial #+TITLE: keyword."
  3099. :group 'org-appearance
  3100. :version "24.1"
  3101. :type '(set (const :tag "#+AUTHOR" author)
  3102. (const :tag "#+DATE" date)
  3103. (const :tag "#+EMAIL" email)
  3104. (const :tag "#+TITLE" title)))
  3105. (defcustom org-fontify-done-headline nil
  3106. "Non-nil means change the face of a headline if it is marked DONE.
  3107. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3108. When this is non-nil, the headline after the keyword is set to the
  3109. `org-headline-done' as an additional indication."
  3110. :group 'org-appearance
  3111. :type 'boolean)
  3112. (defcustom org-fontify-emphasized-text t
  3113. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3114. Changing this variable requires a restart of Emacs to take effect."
  3115. :group 'org-appearance
  3116. :type 'boolean)
  3117. (defcustom org-fontify-whole-heading-line nil
  3118. "Non-nil means fontify the whole line for headings.
  3119. This is useful when setting a background color for the
  3120. org-level-* faces."
  3121. :group 'org-appearance
  3122. :type 'boolean)
  3123. (defcustom org-highlight-latex-fragments-and-specials nil
  3124. "Non-nil means fontify what is treated specially by the exporters."
  3125. :group 'org-appearance
  3126. :type 'boolean)
  3127. (defcustom org-hide-emphasis-markers nil
  3128. "Non-nil mean font-lock should hide the emphasis marker characters."
  3129. :group 'org-appearance
  3130. :type 'boolean)
  3131. (defcustom org-pretty-entities nil
  3132. "Non-nil means show entities as UTF8 characters.
  3133. When nil, the \\name form remains in the buffer."
  3134. :group 'org-appearance
  3135. :version "24.1"
  3136. :type 'boolean)
  3137. (defcustom org-pretty-entities-include-sub-superscripts t
  3138. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3139. :group 'org-appearance
  3140. :version "24.1"
  3141. :type 'boolean)
  3142. (defvar org-emph-re nil
  3143. "Regular expression for matching emphasis.
  3144. After a match, the match groups contain these elements:
  3145. 0 The match of the full regular expression, including the characters
  3146. before and after the proper match
  3147. 1 The character before the proper match, or empty at beginning of line
  3148. 2 The proper match, including the leading and trailing markers
  3149. 3 The leading marker like * or /, indicating the type of highlighting
  3150. 4 The text between the emphasis markers, not including the markers
  3151. 5 The character after the match, empty at the end of a line")
  3152. (defvar org-verbatim-re nil
  3153. "Regular expression for matching verbatim text.")
  3154. (defvar org-emphasis-regexp-components) ; defined just below
  3155. (defvar org-emphasis-alist) ; defined just below
  3156. (defun org-set-emph-re (var val)
  3157. "Set variable and compute the emphasis regular expression."
  3158. (set var val)
  3159. (when (and (boundp 'org-emphasis-alist)
  3160. (boundp 'org-emphasis-regexp-components)
  3161. org-emphasis-alist org-emphasis-regexp-components)
  3162. (let* ((e org-emphasis-regexp-components)
  3163. (pre (car e))
  3164. (post (nth 1 e))
  3165. (border (nth 2 e))
  3166. (body (nth 3 e))
  3167. (nl (nth 4 e))
  3168. (body1 (concat body "*?"))
  3169. (markers (mapconcat 'car org-emphasis-alist ""))
  3170. (vmarkers (mapconcat
  3171. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3172. org-emphasis-alist "")))
  3173. ;; make sure special characters appear at the right position in the class
  3174. (if (string-match "\\^" markers)
  3175. (setq markers (concat (replace-match "" t t markers) "^")))
  3176. (if (string-match "-" markers)
  3177. (setq markers (concat (replace-match "" t t markers) "-")))
  3178. (if (string-match "\\^" vmarkers)
  3179. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3180. (if (string-match "-" vmarkers)
  3181. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3182. (if (> nl 0)
  3183. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3184. (int-to-string nl) "\\}")))
  3185. ;; Make the regexp
  3186. (setq org-emph-re
  3187. (concat "\\([" pre "]\\|^\\)"
  3188. "\\("
  3189. "\\([" markers "]\\)"
  3190. "\\("
  3191. "[^" border "]\\|"
  3192. "[^" border "]"
  3193. body1
  3194. "[^" border "]"
  3195. "\\)"
  3196. "\\3\\)"
  3197. "\\([" post "]\\|$\\)"))
  3198. (setq org-verbatim-re
  3199. (concat "\\([" pre "]\\|^\\)"
  3200. "\\("
  3201. "\\([" vmarkers "]\\)"
  3202. "\\("
  3203. "[^" border "]\\|"
  3204. "[^" border "]"
  3205. body1
  3206. "[^" border "]"
  3207. "\\)"
  3208. "\\3\\)"
  3209. "\\([" post "]\\|$\\)")))))
  3210. (defcustom org-emphasis-regexp-components
  3211. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3212. "Components used to build the regular expression for emphasis.
  3213. This is a list with five entries. Terminology: In an emphasis string
  3214. like \" *strong word* \", we call the initial space PREMATCH, the final
  3215. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3216. and \"trong wor\" is the body. The different components in this variable
  3217. specify what is allowed/forbidden in each part:
  3218. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3219. post Chars allowed as postmatch. End of line will be allowed too.
  3220. border The chars *forbidden* as border characters.
  3221. body-regexp A regexp like \".\" to match a body character. Don't use
  3222. non-shy groups here, and don't allow newline here.
  3223. newline The maximum number of newlines allowed in an emphasis exp.
  3224. Use customize to modify this, or restart Emacs after changing it."
  3225. :group 'org-appearance
  3226. :set 'org-set-emph-re
  3227. :type '(list
  3228. (sexp :tag "Allowed chars in pre ")
  3229. (sexp :tag "Allowed chars in post ")
  3230. (sexp :tag "Forbidden chars in border ")
  3231. (sexp :tag "Regexp for body ")
  3232. (integer :tag "number of newlines allowed")
  3233. (option (boolean :tag "Please ignore this button"))))
  3234. (defcustom org-emphasis-alist
  3235. `(("*" bold "<b>" "</b>")
  3236. ("/" italic "<i>" "</i>")
  3237. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3238. ("=" org-code "<code>" "</code>" verbatim)
  3239. ("~" org-verbatim "<code>" "</code>" verbatim)
  3240. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3241. "<del>" "</del>")
  3242. )
  3243. "Special syntax for emphasized text.
  3244. Text starting and ending with a special character will be emphasized, for
  3245. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3246. characters, the face to be used by font-lock for highlighting in Org-mode
  3247. Emacs buffers, and the HTML tags to be used for this.
  3248. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3249. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3250. Use customize to modify this, or restart Emacs after changing it."
  3251. :group 'org-appearance
  3252. :set 'org-set-emph-re
  3253. :type '(repeat
  3254. (list
  3255. (string :tag "Marker character")
  3256. (choice
  3257. (face :tag "Font-lock-face")
  3258. (plist :tag "Face property list"))
  3259. (string :tag "HTML start tag")
  3260. (string :tag "HTML end tag")
  3261. (option (const verbatim)))))
  3262. (defvar org-protecting-blocks
  3263. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3264. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3265. This is needed for font-lock setup.")
  3266. ;;; Miscellaneous options
  3267. (defgroup org-completion nil
  3268. "Completion in Org-mode."
  3269. :tag "Org Completion"
  3270. :group 'org)
  3271. (defcustom org-completion-use-ido nil
  3272. "Non-nil means use ido completion wherever possible.
  3273. Note that `ido-mode' must be active for this variable to be relevant.
  3274. If you decide to turn this variable on, you might well want to turn off
  3275. `org-outline-path-complete-in-steps'.
  3276. See also `org-completion-use-iswitchb'."
  3277. :group 'org-completion
  3278. :type 'boolean)
  3279. (defcustom org-completion-use-iswitchb nil
  3280. "Non-nil means use iswitchb completion wherever possible.
  3281. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3282. If you decide to turn this variable on, you might well want to turn off
  3283. `org-outline-path-complete-in-steps'.
  3284. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3285. :group 'org-completion
  3286. :type 'boolean)
  3287. (defcustom org-completion-fallback-command 'hippie-expand
  3288. "The expansion command called by \\[pcomplete] in normal context.
  3289. Normal means, no org-mode-specific context."
  3290. :group 'org-completion
  3291. :type 'function)
  3292. ;;; Functions and variables from their packages
  3293. ;; Declared here to avoid compiler warnings
  3294. ;; XEmacs only
  3295. (defvar outline-mode-menu-heading)
  3296. (defvar outline-mode-menu-show)
  3297. (defvar outline-mode-menu-hide)
  3298. (defvar zmacs-regions) ; XEmacs regions
  3299. ;; Emacs only
  3300. (defvar mark-active)
  3301. ;; Various packages
  3302. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3303. (declare-function calendar-forward-day "cal-move" (arg))
  3304. (declare-function calendar-goto-date "cal-move" (date))
  3305. (declare-function calendar-goto-today "cal-move" ())
  3306. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3307. (defvar calc-embedded-close-formula)
  3308. (defvar calc-embedded-open-formula)
  3309. (declare-function cdlatex-tab "ext:cdlatex" ())
  3310. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3311. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3312. (defvar font-lock-unfontify-region-function)
  3313. (declare-function iswitchb-read-buffer "iswitchb"
  3314. (prompt &optional default require-match start matches-set))
  3315. (defvar iswitchb-temp-buflist)
  3316. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3317. (defvar org-agenda-tags-todo-honor-ignore-options)
  3318. (declare-function org-agenda-skip "org-agenda" ())
  3319. (declare-function
  3320. org-agenda-format-item "org-agenda"
  3321. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3322. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3323. (declare-function org-agenda-change-all-lines "org-agenda"
  3324. (newhead hdmarker &optional fixface just-this))
  3325. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3326. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3327. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3328. (beg end))
  3329. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3330. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3331. "org-agenda" (&optional end))
  3332. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3333. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3334. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3335. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3336. (declare-function org-indent-mode "org-indent" (&optional arg))
  3337. (declare-function parse-time-string "parse-time" (string))
  3338. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3339. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3340. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3341. (defvar remember-data-file)
  3342. (defvar texmathp-why)
  3343. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3344. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3345. (defvar w3m-current-url)
  3346. (defvar w3m-current-title)
  3347. (defvar org-latex-regexps)
  3348. ;;; Autoload and prepare some org modules
  3349. ;; Some table stuff that needs to be defined here, because it is used
  3350. ;; by the functions setting up org-mode or checking for table context.
  3351. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3352. "Detect an org-type or table-type table.")
  3353. (defconst org-table-line-regexp "^[ \t]*|"
  3354. "Detect an org-type table line.")
  3355. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3356. "Detect an org-type table line.")
  3357. (defconst org-table-hline-regexp "^[ \t]*|-"
  3358. "Detect an org-type table hline.")
  3359. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3360. "Detect a table-type table hline.")
  3361. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3362. "Detect the first line outside a table when searching from within it.
  3363. This works for both table types.")
  3364. ;; Autoload the functions in org-table.el that are needed by functions here.
  3365. (eval-and-compile
  3366. (org-autoload "org-table"
  3367. '(org-table-align org-table-begin org-table-blank-field
  3368. org-table-convert org-table-convert-region org-table-copy-down
  3369. org-table-copy-region org-table-create
  3370. org-table-create-or-convert-from-region
  3371. org-table-create-with-table.el org-table-current-dline
  3372. org-table-cut-region org-table-delete-column org-table-edit-field
  3373. org-table-edit-formulas org-table-end org-table-eval-formula
  3374. org-table-export org-table-field-info
  3375. org-table-get-stored-formulas org-table-goto-column
  3376. org-table-hline-and-move org-table-import org-table-insert-column
  3377. org-table-insert-hline org-table-insert-row org-table-iterate
  3378. org-table-justify-field-maybe org-table-kill-row
  3379. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3380. org-table-move-column org-table-move-column-left
  3381. org-table-move-column-right org-table-move-row
  3382. org-table-move-row-down org-table-move-row-up
  3383. org-table-next-field org-table-next-row org-table-paste-rectangle
  3384. org-table-previous-field org-table-recalculate
  3385. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3386. org-table-toggle-coordinate-overlays
  3387. org-table-toggle-formula-debugger org-table-wrap-region
  3388. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3389. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3390. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3391. (defun org-at-table-p (&optional table-type)
  3392. "Return t if the cursor is inside an org-type table.
  3393. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3394. (if org-enable-table-editor
  3395. (save-excursion
  3396. (beginning-of-line 1)
  3397. (looking-at (if table-type org-table-any-line-regexp
  3398. org-table-line-regexp)))
  3399. nil))
  3400. (defsubst org-table-p () (org-at-table-p))
  3401. (defun org-at-table.el-p ()
  3402. "Return t if and only if we are at a table.el table."
  3403. (and (org-at-table-p 'any)
  3404. (save-excursion
  3405. (goto-char (org-table-begin 'any))
  3406. (looking-at org-table1-hline-regexp))))
  3407. (defun org-table-recognize-table.el ()
  3408. "If there is a table.el table nearby, recognize it and move into it."
  3409. (if org-table-tab-recognizes-table.el
  3410. (if (org-at-table.el-p)
  3411. (progn
  3412. (beginning-of-line 1)
  3413. (if (looking-at org-table-dataline-regexp)
  3414. nil
  3415. (if (looking-at org-table1-hline-regexp)
  3416. (progn
  3417. (beginning-of-line 2)
  3418. (if (looking-at org-table-any-border-regexp)
  3419. (beginning-of-line -1)))))
  3420. (if (re-search-forward "|" (org-table-end t) t)
  3421. (progn
  3422. (require 'table)
  3423. (if (table--at-cell-p (point))
  3424. t
  3425. (message "recognizing table.el table...")
  3426. (table-recognize-table)
  3427. (message "recognizing table.el table...done")))
  3428. (error "This should not happen"))
  3429. t)
  3430. nil)
  3431. nil))
  3432. (defun org-at-table-hline-p ()
  3433. "Return t if the cursor is inside a hline in a table."
  3434. (if org-enable-table-editor
  3435. (save-excursion
  3436. (beginning-of-line 1)
  3437. (looking-at org-table-hline-regexp))
  3438. nil))
  3439. (defvar org-table-clean-did-remove-column nil)
  3440. (defun org-table-map-tables (function &optional quietly)
  3441. "Apply FUNCTION to the start of all tables in the buffer."
  3442. (save-excursion
  3443. (save-restriction
  3444. (widen)
  3445. (goto-char (point-min))
  3446. (while (re-search-forward org-table-any-line-regexp nil t)
  3447. (unless quietly
  3448. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3449. (beginning-of-line 1)
  3450. (when (looking-at org-table-line-regexp)
  3451. (save-excursion (funcall function))
  3452. (or (looking-at org-table-line-regexp)
  3453. (forward-char 1)))
  3454. (re-search-forward org-table-any-border-regexp nil 1))))
  3455. (unless quietly (message "Mapping tables: done")))
  3456. ;; Declare and autoload functions from org-exp.el & Co
  3457. (declare-function org-default-export-plist "org-exp")
  3458. (declare-function org-infile-export-plist "org-exp")
  3459. (declare-function org-get-current-options "org-exp")
  3460. (eval-and-compile
  3461. (org-autoload "org-exp"
  3462. '(org-export org-export-visible
  3463. org-insert-export-options-template
  3464. org-table-clean-before-export))
  3465. (org-autoload "org-ascii"
  3466. '(org-export-as-ascii org-export-ascii-preprocess
  3467. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3468. org-export-region-as-ascii))
  3469. (org-autoload "org-latex"
  3470. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3471. org-replace-region-by-latex org-export-region-as-latex
  3472. org-export-as-latex org-export-as-pdf
  3473. org-export-as-pdf-and-open))
  3474. (org-autoload "org-html"
  3475. '(org-export-as-html-and-open
  3476. org-export-as-html-batch org-export-as-html-to-buffer
  3477. org-replace-region-by-html org-export-region-as-html
  3478. org-export-as-html))
  3479. (org-autoload "org-docbook"
  3480. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3481. org-replace-region-by-docbook org-export-region-as-docbook
  3482. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3483. org-export-as-docbook))
  3484. (org-autoload "org-icalendar"
  3485. '(org-export-icalendar-this-file
  3486. org-export-icalendar-all-agenda-files
  3487. org-export-icalendar-combine-agenda-files))
  3488. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3489. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3490. ;; Declare and autoload functions from org-agenda.el
  3491. (eval-and-compile
  3492. (org-autoload "org-agenda"
  3493. '(org-agenda org-agenda-list org-search-view
  3494. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3495. org-diary org-agenda-to-appt
  3496. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3497. ;; Autoload org-remember
  3498. (eval-and-compile
  3499. (org-autoload "org-remember"
  3500. '(org-remember-insinuate org-remember-annotation
  3501. org-remember-apply-template org-remember org-remember-handler)))
  3502. (eval-and-compile
  3503. (org-autoload "org-capture"
  3504. '(org-capture org-capture-insert-template-here
  3505. org-capture-import-remember-templates)))
  3506. ;; Autoload org-clock.el
  3507. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3508. (beg end))
  3509. (declare-function org-clock-update-mode-line "org-clock" ())
  3510. (declare-function org-resolve-clocks "org-clock"
  3511. (&optional also-non-dangling-p prompt last-valid))
  3512. (defvar org-clock-start-time)
  3513. (defvar org-clock-marker (make-marker)
  3514. "Marker recording the last clock-in.")
  3515. (defvar org-clock-hd-marker (make-marker)
  3516. "Marker recording the last clock-in, but the headline position.")
  3517. (defvar org-clock-heading ""
  3518. "The heading of the current clock entry.")
  3519. (defun org-clock-is-active ()
  3520. "Return non-nil if clock is currently running.
  3521. The return value is actually the clock marker."
  3522. (marker-buffer org-clock-marker))
  3523. (eval-and-compile
  3524. (org-autoload
  3525. "org-clock"
  3526. '(org-clock-in org-clock-out org-clock-cancel
  3527. org-clock-goto org-clock-sum org-clock-display
  3528. org-clock-remove-overlays org-clock-report
  3529. org-clocktable-shift org-dblock-write:clocktable
  3530. org-get-clocktable org-resolve-clocks)))
  3531. (defun org-clock-update-time-maybe ()
  3532. "If this is a CLOCK line, update it and return t.
  3533. Otherwise, return nil."
  3534. (interactive)
  3535. (save-excursion
  3536. (beginning-of-line 1)
  3537. (skip-chars-forward " \t")
  3538. (when (looking-at org-clock-string)
  3539. (let ((re (concat "[ \t]*" org-clock-string
  3540. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3541. "\\([ \t]*=>.*\\)?\\)?"))
  3542. ts te h m s neg)
  3543. (cond
  3544. ((not (looking-at re))
  3545. nil)
  3546. ((not (match-end 2))
  3547. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3548. (> org-clock-marker (point))
  3549. (<= org-clock-marker (point-at-eol)))
  3550. ;; The clock is running here
  3551. (setq org-clock-start-time
  3552. (apply 'encode-time
  3553. (org-parse-time-string (match-string 1))))
  3554. (org-clock-update-mode-line)))
  3555. (t
  3556. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3557. (end-of-line 1)
  3558. (setq ts (match-string 1)
  3559. te (match-string 3))
  3560. (setq s (- (org-float-time
  3561. (apply 'encode-time (org-parse-time-string te)))
  3562. (org-float-time
  3563. (apply 'encode-time (org-parse-time-string ts))))
  3564. neg (< s 0)
  3565. s (abs s)
  3566. h (floor (/ s 3600))
  3567. s (- s (* 3600 h))
  3568. m (floor (/ s 60))
  3569. s (- s (* 60 s)))
  3570. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3571. t))))))
  3572. (defun org-check-running-clock ()
  3573. "Check if the current buffer contains the running clock.
  3574. If yes, offer to stop it and to save the buffer with the changes."
  3575. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3576. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3577. (buffer-name))))
  3578. (org-clock-out)
  3579. (when (y-or-n-p "Save changed buffer?")
  3580. (save-buffer))))
  3581. (defun org-clocktable-try-shift (dir n)
  3582. "Check if this line starts a clock table, if yes, shift the time block."
  3583. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3584. (org-clocktable-shift dir n)))
  3585. ;; Autoload org-timer.el
  3586. (eval-and-compile
  3587. (org-autoload
  3588. "org-timer"
  3589. '(org-timer-start org-timer org-timer-item
  3590. org-timer-change-times-in-region
  3591. org-timer-set-timer
  3592. org-timer-reset-timers
  3593. org-timer-show-remaining-time)))
  3594. ;; Autoload org-feed.el
  3595. (eval-and-compile
  3596. (org-autoload
  3597. "org-feed"
  3598. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3599. ;; Autoload org-indent.el
  3600. ;; Define the variable already here, to make sure we have it.
  3601. (defvar org-indent-mode nil
  3602. "Non-nil if Org-Indent mode is enabled.
  3603. Use the command `org-indent-mode' to change this variable.")
  3604. (eval-and-compile
  3605. (org-autoload
  3606. "org-indent"
  3607. '(org-indent-mode)))
  3608. ;; Autoload org-mobile.el
  3609. (eval-and-compile
  3610. (org-autoload
  3611. "org-mobile"
  3612. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3613. ;; Autoload archiving code
  3614. ;; The stuff that is needed for cycling and tags has to be defined here.
  3615. (defgroup org-archive nil
  3616. "Options concerning archiving in Org-mode."
  3617. :tag "Org Archive"
  3618. :group 'org-structure)
  3619. (defcustom org-archive-location "%s_archive::"
  3620. "The location where subtrees should be archived.
  3621. The value of this variable is a string, consisting of two parts,
  3622. separated by a double-colon. The first part is a filename and
  3623. the second part is a headline.
  3624. When the filename is omitted, archiving happens in the same file.
  3625. %s in the filename will be replaced by the current file
  3626. name (without the directory part). Archiving to a different file
  3627. is useful to keep archived entries from contributing to the
  3628. Org-mode Agenda.
  3629. The archived entries will be filed as subtrees of the specified
  3630. headline. When the headline is omitted, the subtrees are simply
  3631. filed away at the end of the file, as top-level entries. Also in
  3632. the heading you can use %s to represent the file name, this can be
  3633. useful when using the same archive for a number of different files.
  3634. Here are a few examples:
  3635. \"%s_archive::\"
  3636. If the current file is Projects.org, archive in file
  3637. Projects.org_archive, as top-level trees. This is the default.
  3638. \"::* Archived Tasks\"
  3639. Archive in the current file, under the top-level headline
  3640. \"* Archived Tasks\".
  3641. \"~/org/archive.org::\"
  3642. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3643. \"~/org/archive.org::* From %s\"
  3644. Archive in file ~/org/archive.org (absolute path), under headlines
  3645. \"From FILENAME\" where file name is the current file name.
  3646. \"basement::** Finished Tasks\"
  3647. Archive in file ./basement (relative path), as level 3 trees
  3648. below the level 2 heading \"** Finished Tasks\".
  3649. You may set this option on a per-file basis by adding to the buffer a
  3650. line like
  3651. #+ARCHIVE: basement::** Finished Tasks
  3652. You may also define it locally for a subtree by setting an ARCHIVE property
  3653. in the entry. If such a property is found in an entry, or anywhere up
  3654. the hierarchy, it will be used."
  3655. :group 'org-archive
  3656. :type 'string)
  3657. (defcustom org-archive-tag "ARCHIVE"
  3658. "The tag that marks a subtree as archived.
  3659. An archived subtree does not open during visibility cycling, and does
  3660. not contribute to the agenda listings.
  3661. After changing this, font-lock must be restarted in the relevant buffers to
  3662. get the proper fontification."
  3663. :group 'org-archive
  3664. :group 'org-keywords
  3665. :type 'string)
  3666. (defcustom org-agenda-skip-archived-trees t
  3667. "Non-nil means the agenda will skip any items located in archived trees.
  3668. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3669. variable is no longer recommended, you should leave it at the value t.
  3670. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3671. :group 'org-archive
  3672. :group 'org-agenda-skip
  3673. :type 'boolean)
  3674. (defcustom org-columns-skip-archived-trees t
  3675. "Non-nil means ignore archived trees when creating column view."
  3676. :group 'org-archive
  3677. :group 'org-properties
  3678. :type 'boolean)
  3679. (defcustom org-cycle-open-archived-trees nil
  3680. "Non-nil means `org-cycle' will open archived trees.
  3681. An archived tree is a tree marked with the tag ARCHIVE.
  3682. When nil, archived trees will stay folded. You can still open them with
  3683. normal outline commands like `show-all', but not with the cycling commands."
  3684. :group 'org-archive
  3685. :group 'org-cycle
  3686. :type 'boolean)
  3687. (defcustom org-sparse-tree-open-archived-trees nil
  3688. "Non-nil means sparse tree construction shows matches in archived trees.
  3689. When nil, matches in these trees are highlighted, but the trees are kept in
  3690. collapsed state."
  3691. :group 'org-archive
  3692. :group 'org-sparse-trees
  3693. :type 'boolean)
  3694. (defun org-cycle-hide-archived-subtrees (state)
  3695. "Re-hide all archived subtrees after a visibility state change."
  3696. (when (and (not org-cycle-open-archived-trees)
  3697. (not (memq state '(overview folded))))
  3698. (save-excursion
  3699. (let* ((globalp (memq state '(contents all)))
  3700. (beg (if globalp (point-min) (point)))
  3701. (end (if globalp (point-max) (org-end-of-subtree t))))
  3702. (org-hide-archived-subtrees beg end)
  3703. (goto-char beg)
  3704. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3705. (message "%s" (substitute-command-keys
  3706. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3707. (defun org-force-cycle-archived ()
  3708. "Cycle subtree even if it is archived."
  3709. (interactive)
  3710. (setq this-command 'org-cycle)
  3711. (let ((org-cycle-open-archived-trees t))
  3712. (call-interactively 'org-cycle)))
  3713. (defun org-hide-archived-subtrees (beg end)
  3714. "Re-hide all archived subtrees after a visibility state change."
  3715. (save-excursion
  3716. (let* ((re (concat ":" org-archive-tag ":")))
  3717. (goto-char beg)
  3718. (while (re-search-forward re end t)
  3719. (when (org-at-heading-p)
  3720. (org-flag-subtree t)
  3721. (org-end-of-subtree t))))))
  3722. (defun org-flag-subtree (flag)
  3723. (save-excursion
  3724. (org-back-to-heading t)
  3725. (outline-end-of-heading)
  3726. (outline-flag-region (point)
  3727. (progn (org-end-of-subtree t) (point))
  3728. flag)))
  3729. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3730. (eval-and-compile
  3731. (org-autoload "org-archive"
  3732. '(org-add-archive-files org-archive-subtree
  3733. org-archive-to-archive-sibling org-toggle-archive-tag
  3734. org-archive-subtree-default
  3735. org-archive-subtree-default-with-confirmation)))
  3736. ;; Autoload Column View Code
  3737. (declare-function org-columns-number-to-string "org-colview")
  3738. (declare-function org-columns-get-format-and-top-level "org-colview")
  3739. (declare-function org-columns-compute "org-colview")
  3740. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3741. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3742. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3743. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3744. ;; Autoload ID code
  3745. (declare-function org-id-store-link "org-id")
  3746. (declare-function org-id-locations-load "org-id")
  3747. (declare-function org-id-locations-save "org-id")
  3748. (defvar org-id-track-globally)
  3749. (org-autoload "org-id"
  3750. '(org-id-get-create org-id-new org-id-copy org-id-get
  3751. org-id-get-with-outline-path-completion
  3752. org-id-get-with-outline-drilling org-id-store-link
  3753. org-id-goto org-id-find org-id-store-link))
  3754. ;; Autoload Plotting Code
  3755. (org-autoload "org-plot"
  3756. '(org-plot/gnuplot))
  3757. ;;; Variables for pre-computed regular expressions, all buffer local
  3758. (defvar org-drawer-regexp nil
  3759. "Matches first line of a hidden block.")
  3760. (make-variable-buffer-local 'org-drawer-regexp)
  3761. (defvar org-todo-regexp nil
  3762. "Matches any of the TODO state keywords.")
  3763. (make-variable-buffer-local 'org-todo-regexp)
  3764. (defvar org-not-done-regexp nil
  3765. "Matches any of the TODO state keywords except the last one.")
  3766. (make-variable-buffer-local 'org-not-done-regexp)
  3767. (defvar org-not-done-heading-regexp nil
  3768. "Matches a TODO headline that is not done.")
  3769. (make-variable-buffer-local 'org-not-done-regexp)
  3770. (defvar org-todo-line-regexp nil
  3771. "Matches a headline and puts TODO state into group 2 if present.")
  3772. (make-variable-buffer-local 'org-todo-line-regexp)
  3773. (defvar org-complex-heading-regexp nil
  3774. "Matches a headline and puts everything into groups:
  3775. group 1: the stars
  3776. group 2: The todo keyword, maybe
  3777. group 3: Priority cookie
  3778. group 4: True headline
  3779. group 5: Tags")
  3780. (make-variable-buffer-local 'org-complex-heading-regexp)
  3781. (defvar org-complex-heading-regexp-format nil
  3782. "Printf format to make regexp to match an exact headline.
  3783. This regexp will match the headline of any node which has the
  3784. exact headline text that is put into the format, but may have any
  3785. TODO state, priority and tags.")
  3786. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3787. (defvar org-todo-line-tags-regexp nil
  3788. "Matches a headline and puts TODO state into group 2 if present.
  3789. Also put tags into group 4 if tags are present.")
  3790. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3791. (defvar org-ds-keyword-length 12
  3792. "Maximum length of the Deadline and SCHEDULED keywords.")
  3793. (make-variable-buffer-local 'org-ds-keyword-length)
  3794. (defvar org-deadline-regexp nil
  3795. "Matches the DEADLINE keyword.")
  3796. (make-variable-buffer-local 'org-deadline-regexp)
  3797. (defvar org-deadline-time-regexp nil
  3798. "Matches the DEADLINE keyword together with a time stamp.")
  3799. (make-variable-buffer-local 'org-deadline-time-regexp)
  3800. (defvar org-deadline-line-regexp nil
  3801. "Matches the DEADLINE keyword and the rest of the line.")
  3802. (make-variable-buffer-local 'org-deadline-line-regexp)
  3803. (defvar org-scheduled-regexp nil
  3804. "Matches the SCHEDULED keyword.")
  3805. (make-variable-buffer-local 'org-scheduled-regexp)
  3806. (defvar org-scheduled-time-regexp nil
  3807. "Matches the SCHEDULED keyword together with a time stamp.")
  3808. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3809. (defvar org-closed-time-regexp nil
  3810. "Matches the CLOSED keyword together with a time stamp.")
  3811. (make-variable-buffer-local 'org-closed-time-regexp)
  3812. (defvar org-keyword-time-regexp nil
  3813. "Matches any of the 4 keywords, together with the time stamp.")
  3814. (make-variable-buffer-local 'org-keyword-time-regexp)
  3815. (defvar org-keyword-time-not-clock-regexp nil
  3816. "Matches any of the 3 keywords, together with the time stamp.")
  3817. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3818. (defvar org-maybe-keyword-time-regexp nil
  3819. "Matches a timestamp, possibly preceded by a keyword.")
  3820. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3821. (defvar org-planning-or-clock-line-re nil
  3822. "Matches a line with planning or clock info.")
  3823. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3824. (defvar org-all-time-keywords nil
  3825. "List of time keywords.")
  3826. (make-variable-buffer-local 'org-all-time-keywords)
  3827. (defconst org-plain-time-of-day-regexp
  3828. (concat
  3829. "\\(\\<[012]?[0-9]"
  3830. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3831. "\\(--?"
  3832. "\\(\\<[012]?[0-9]"
  3833. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3834. "\\)?")
  3835. "Regular expression to match a plain time or time range.
  3836. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3837. groups carry important information:
  3838. 0 the full match
  3839. 1 the first time, range or not
  3840. 8 the second time, if it is a range.")
  3841. (defconst org-plain-time-extension-regexp
  3842. (concat
  3843. "\\(\\<[012]?[0-9]"
  3844. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3845. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3846. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3847. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3848. groups carry important information:
  3849. 0 the full match
  3850. 7 hours of duration
  3851. 9 minutes of duration")
  3852. (defconst org-stamp-time-of-day-regexp
  3853. (concat
  3854. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3855. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3856. "\\(--?"
  3857. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3858. "Regular expression to match a timestamp time or time range.
  3859. After a match, the following groups carry important information:
  3860. 0 the full match
  3861. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3862. 2 the first time, range or not
  3863. 4 the second time, if it is a range.")
  3864. (defconst org-startup-options
  3865. '(("fold" org-startup-folded t)
  3866. ("overview" org-startup-folded t)
  3867. ("nofold" org-startup-folded nil)
  3868. ("showall" org-startup-folded nil)
  3869. ("showeverything" org-startup-folded showeverything)
  3870. ("content" org-startup-folded content)
  3871. ("indent" org-startup-indented t)
  3872. ("noindent" org-startup-indented nil)
  3873. ("hidestars" org-hide-leading-stars t)
  3874. ("showstars" org-hide-leading-stars nil)
  3875. ("odd" org-odd-levels-only t)
  3876. ("oddeven" org-odd-levels-only nil)
  3877. ("align" org-startup-align-all-tables t)
  3878. ("noalign" org-startup-align-all-tables nil)
  3879. ("inlineimages" org-startup-with-inline-images t)
  3880. ("noinlineimages" org-startup-with-inline-images nil)
  3881. ("customtime" org-display-custom-times t)
  3882. ("logdone" org-log-done time)
  3883. ("lognotedone" org-log-done note)
  3884. ("nologdone" org-log-done nil)
  3885. ("lognoteclock-out" org-log-note-clock-out t)
  3886. ("nolognoteclock-out" org-log-note-clock-out nil)
  3887. ("logrepeat" org-log-repeat state)
  3888. ("lognoterepeat" org-log-repeat note)
  3889. ("nologrepeat" org-log-repeat nil)
  3890. ("logreschedule" org-log-reschedule time)
  3891. ("lognotereschedule" org-log-reschedule note)
  3892. ("nologreschedule" org-log-reschedule nil)
  3893. ("logredeadline" org-log-redeadline time)
  3894. ("lognoteredeadline" org-log-redeadline note)
  3895. ("nologredeadline" org-log-redeadline nil)
  3896. ("logrefile" org-log-refile time)
  3897. ("lognoterefile" org-log-refile note)
  3898. ("nologrefile" org-log-refile nil)
  3899. ("fninline" org-footnote-define-inline t)
  3900. ("nofninline" org-footnote-define-inline nil)
  3901. ("fnlocal" org-footnote-section nil)
  3902. ("fnauto" org-footnote-auto-label t)
  3903. ("fnprompt" org-footnote-auto-label nil)
  3904. ("fnconfirm" org-footnote-auto-label confirm)
  3905. ("fnplain" org-footnote-auto-label plain)
  3906. ("fnadjust" org-footnote-auto-adjust t)
  3907. ("nofnadjust" org-footnote-auto-adjust nil)
  3908. ("constcgs" constants-unit-system cgs)
  3909. ("constSI" constants-unit-system SI)
  3910. ("noptag" org-tag-persistent-alist nil)
  3911. ("hideblocks" org-hide-block-startup t)
  3912. ("nohideblocks" org-hide-block-startup nil)
  3913. ("beamer" org-startup-with-beamer-mode t)
  3914. ("entitiespretty" org-pretty-entities t)
  3915. ("entitiesplain" org-pretty-entities nil))
  3916. "Variable associated with STARTUP options for org-mode.
  3917. Each element is a list of three items: The startup options as written
  3918. in the #+STARTUP line, the corresponding variable, and the value to
  3919. set this variable to if the option is found. An optional forth element PUSH
  3920. means to push this value onto the list in the variable.")
  3921. (defun org-update-property-plist (key val props)
  3922. "Update PROPS with KEY and VAL."
  3923. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3924. (key (if appending (substring key 0 (- (length key) 1)) key))
  3925. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3926. (previous (cdr (assoc key props))))
  3927. (if appending
  3928. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3929. (cons (cons key val) remainder))))
  3930. (defconst org-block-regexp
  3931. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3932. "Regular expression for hiding blocks.")
  3933. (defconst org-heading-keyword-regexp-format
  3934. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3935. "Printf format for a regexp matching an headline with some keyword.
  3936. This regexp will match the headline of any node which has the
  3937. exact keyword that is put into the format. The keyword isn't in
  3938. any group by default, but the stars and the body are.")
  3939. (defconst org-heading-keyword-maybe-regexp-format
  3940. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3941. "Printf format for a regexp matching an headline, possibly with some keyword.
  3942. This regexp can match any headline with the specified keyword, or
  3943. without a keyword. The keyword isn't in any group by default,
  3944. but the stars and the body are.")
  3945. (defun org-set-regexps-and-options ()
  3946. "Precompute regular expressions for current buffer."
  3947. (when (eq major-mode 'org-mode)
  3948. (org-set-local 'org-todo-kwd-alist nil)
  3949. (org-set-local 'org-todo-key-alist nil)
  3950. (org-set-local 'org-todo-key-trigger nil)
  3951. (org-set-local 'org-todo-keywords-1 nil)
  3952. (org-set-local 'org-done-keywords nil)
  3953. (org-set-local 'org-todo-heads nil)
  3954. (org-set-local 'org-todo-sets nil)
  3955. (org-set-local 'org-todo-log-states nil)
  3956. (org-set-local 'org-file-properties nil)
  3957. (org-set-local 'org-file-tags nil)
  3958. (let ((re (org-make-options-regexp
  3959. '("CATEGORY" "TODO" "COLUMNS"
  3960. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3961. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3962. "OPTIONS")
  3963. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3964. (splitre "[ \t]+")
  3965. (scripts org-use-sub-superscripts)
  3966. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3967. tail sep kws1 prio props ftags drawers beamer-p
  3968. ext-setup-or-nil setup-contents (start 0))
  3969. (save-excursion
  3970. (save-restriction
  3971. (widen)
  3972. (goto-char (point-min))
  3973. (while (or (and ext-setup-or-nil
  3974. (string-match re ext-setup-or-nil start)
  3975. (setq start (match-end 0)))
  3976. (and (setq ext-setup-or-nil nil start 0)
  3977. (re-search-forward re nil t)))
  3978. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3979. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3980. (if (stringp value) (setq value (org-trim value)))
  3981. (cond
  3982. ((equal key "CATEGORY")
  3983. (setq cat value))
  3984. ((member key '("SEQ_TODO" "TODO"))
  3985. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3986. ((equal key "TYP_TODO")
  3987. (push (cons 'type (org-split-string value splitre)) kwds))
  3988. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3989. ;; general TODO-like setup
  3990. (push (cons (intern (downcase (match-string 1 key)))
  3991. (org-split-string value splitre)) kwds))
  3992. ((equal key "TAGS")
  3993. (setq tags (append tags (if tags '("\\n") nil)
  3994. (org-split-string value splitre))))
  3995. ((equal key "COLUMNS")
  3996. (org-set-local 'org-columns-default-format value))
  3997. ((equal key "LINK")
  3998. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3999. (push (cons (match-string 1 value)
  4000. (org-trim (match-string 2 value)))
  4001. links)))
  4002. ((equal key "PRIORITIES")
  4003. (setq prio (org-split-string value " +")))
  4004. ((equal key "PROPERTY")
  4005. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4006. (setq props (org-update-property-plist (match-string 1 value)
  4007. (match-string 2 value)
  4008. props))))
  4009. ((equal key "FILETAGS")
  4010. (when (string-match "\\S-" value)
  4011. (setq ftags
  4012. (append
  4013. ftags
  4014. (apply 'append
  4015. (mapcar (lambda (x) (org-split-string x ":"))
  4016. (org-split-string value)))))))
  4017. ((equal key "DRAWERS")
  4018. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4019. ((equal key "CONSTANTS")
  4020. (setq const (append const (org-split-string value splitre))))
  4021. ((equal key "STARTUP")
  4022. (let ((opts (org-split-string value splitre))
  4023. l var val)
  4024. (while (setq l (pop opts))
  4025. (when (setq l (assoc l org-startup-options))
  4026. (setq var (nth 1 l) val (nth 2 l))
  4027. (if (not (nth 3 l))
  4028. (set (make-local-variable var) val)
  4029. (if (not (listp (symbol-value var)))
  4030. (set (make-local-variable var) nil))
  4031. (set (make-local-variable var) (symbol-value var))
  4032. (add-to-list var val))))))
  4033. ((equal key "ARCHIVE")
  4034. (setq arch value)
  4035. (remove-text-properties 0 (length arch)
  4036. '(face t fontified t) arch))
  4037. ((equal key "LATEX_CLASS")
  4038. (setq beamer-p (equal value "beamer")))
  4039. ((equal key "OPTIONS")
  4040. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4041. (setq scripts (read (match-string 2 value)))))
  4042. ((equal key "SETUPFILE")
  4043. (setq setup-contents (org-file-contents
  4044. (expand-file-name
  4045. (org-remove-double-quotes value))
  4046. 'noerror))
  4047. (if (not ext-setup-or-nil)
  4048. (setq ext-setup-or-nil setup-contents start 0)
  4049. (setq ext-setup-or-nil
  4050. (concat (substring ext-setup-or-nil 0 start)
  4051. "\n" setup-contents "\n"
  4052. (substring ext-setup-or-nil start)))))))
  4053. ;; search for property blocks
  4054. (goto-char (point-min))
  4055. (while (re-search-forward org-block-regexp nil t)
  4056. (when (equal "PROPERTY" (upcase (match-string 1)))
  4057. (setq value (replace-regexp-in-string
  4058. "[\n\r]" " " (match-string 4)))
  4059. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4060. (setq props (org-update-property-plist (match-string 1 value)
  4061. (match-string 2 value)
  4062. props)))))))
  4063. (org-set-local 'org-use-sub-superscripts scripts)
  4064. (when cat
  4065. (org-set-local 'org-category (intern cat))
  4066. (push (cons "CATEGORY" cat) props))
  4067. (when prio
  4068. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4069. (setq prio (mapcar 'string-to-char prio))
  4070. (org-set-local 'org-highest-priority (nth 0 prio))
  4071. (org-set-local 'org-lowest-priority (nth 1 prio))
  4072. (org-set-local 'org-default-priority (nth 2 prio)))
  4073. (and props (org-set-local 'org-file-properties (nreverse props)))
  4074. (and ftags (org-set-local 'org-file-tags
  4075. (mapcar 'org-add-prop-inherited ftags)))
  4076. (and drawers (org-set-local 'org-drawers drawers))
  4077. (and arch (org-set-local 'org-archive-location arch))
  4078. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4079. ;; Process the TODO keywords
  4080. (unless kwds
  4081. ;; Use the global values as if they had been given locally.
  4082. (setq kwds (default-value 'org-todo-keywords))
  4083. (if (stringp (car kwds))
  4084. (setq kwds (list (cons org-todo-interpretation
  4085. (default-value 'org-todo-keywords)))))
  4086. (setq kwds (reverse kwds)))
  4087. (setq kwds (nreverse kwds))
  4088. (let (inter kws kw)
  4089. (while (setq kws (pop kwds))
  4090. (let ((kws (or
  4091. (run-hook-with-args-until-success
  4092. 'org-todo-setup-filter-hook kws)
  4093. kws)))
  4094. (setq inter (pop kws) sep (member "|" kws)
  4095. kws0 (delete "|" (copy-sequence kws))
  4096. kwsa nil
  4097. kws1 (mapcar
  4098. (lambda (x)
  4099. ;; 1 2
  4100. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4101. (progn
  4102. (setq kw (match-string 1 x)
  4103. key (and (match-end 2) (match-string 2 x))
  4104. log (org-extract-log-state-settings x))
  4105. (push (cons kw (and key (string-to-char key))) kwsa)
  4106. (and log (push log org-todo-log-states))
  4107. kw)
  4108. (error "Invalid TODO keyword %s" x)))
  4109. kws0)
  4110. kwsa (if kwsa (append '((:startgroup))
  4111. (nreverse kwsa)
  4112. '((:endgroup))))
  4113. hw (car kws1)
  4114. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4115. tail (list inter hw (car dws) (org-last dws))))
  4116. (add-to-list 'org-todo-heads hw 'append)
  4117. (push kws1 org-todo-sets)
  4118. (setq org-done-keywords (append org-done-keywords dws nil))
  4119. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4120. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4121. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4122. (setq org-todo-sets (nreverse org-todo-sets)
  4123. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4124. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4125. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4126. ;; Process the constants
  4127. (when const
  4128. (let (e cst)
  4129. (while (setq e (pop const))
  4130. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4131. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4132. (setq org-table-formula-constants-local cst)))
  4133. ;; Process the tags.
  4134. (when tags
  4135. (let (e tgs)
  4136. (while (setq e (pop tags))
  4137. (cond
  4138. ((equal e "{") (push '(:startgroup) tgs))
  4139. ((equal e "}") (push '(:endgroup) tgs))
  4140. ((equal e "\\n") (push '(:newline) tgs))
  4141. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4142. (push (cons (match-string 1 e)
  4143. (string-to-char (match-string 2 e)))
  4144. tgs))
  4145. (t (push (list e) tgs))))
  4146. (org-set-local 'org-tag-alist nil)
  4147. (while (setq e (pop tgs))
  4148. (or (and (stringp (car e))
  4149. (assoc (car e) org-tag-alist))
  4150. (push e org-tag-alist)))))
  4151. ;; Compute the regular expressions and other local variables.
  4152. ;; Using `org-outline-regexp-bol' would complicate them much,
  4153. ;; because of the fixed white space at the end of that string.
  4154. (if (not org-done-keywords)
  4155. (setq org-done-keywords (and org-todo-keywords-1
  4156. (list (org-last org-todo-keywords-1)))))
  4157. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4158. (length org-scheduled-string)
  4159. (length org-clock-string)
  4160. (length org-closed-string)))
  4161. org-drawer-regexp
  4162. (concat "^[ \t]*:\\("
  4163. (mapconcat 'regexp-quote org-drawers "\\|")
  4164. "\\):[ \t]*$")
  4165. org-not-done-keywords
  4166. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4167. org-todo-regexp
  4168. (concat "\\("
  4169. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4170. "\\)")
  4171. org-not-done-regexp
  4172. (concat "\\("
  4173. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4174. "\\)")
  4175. org-not-done-heading-regexp
  4176. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4177. org-todo-line-regexp
  4178. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4179. org-complex-heading-regexp
  4180. (concat "^\\(\\*+\\)"
  4181. "\\(?: +" org-todo-regexp "\\)?"
  4182. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4183. "\\(?: +\\(.*?\\)\\)?"
  4184. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4185. "[ \t]*$")
  4186. org-complex-heading-regexp-format
  4187. (concat "^\\(\\*+\\)"
  4188. "\\(?: +" org-todo-regexp "\\)?"
  4189. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4190. "\\(?: +"
  4191. ;; Stats cookies can be stuck to body.
  4192. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4193. "\\(%s\\)"
  4194. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4195. "\\)"
  4196. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4197. "[ \t]*$")
  4198. org-todo-line-tags-regexp
  4199. (concat "^\\(\\*+\\)"
  4200. "\\(?: +" org-todo-regexp "\\)?"
  4201. "\\(?: +\\(.*?\\)\\)?"
  4202. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4203. "[ \t]*$")
  4204. org-deadline-regexp (concat "\\<" org-deadline-string)
  4205. org-deadline-time-regexp
  4206. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4207. org-deadline-line-regexp
  4208. (concat "\\<\\(" org-deadline-string "\\).*")
  4209. org-scheduled-regexp
  4210. (concat "\\<" org-scheduled-string)
  4211. org-scheduled-time-regexp
  4212. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4213. org-closed-time-regexp
  4214. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4215. org-keyword-time-regexp
  4216. (concat "\\<\\(" org-scheduled-string
  4217. "\\|" org-deadline-string
  4218. "\\|" org-closed-string
  4219. "\\|" org-clock-string "\\)"
  4220. " *[[<]\\([^]>]+\\)[]>]")
  4221. org-keyword-time-not-clock-regexp
  4222. (concat "\\<\\(" org-scheduled-string
  4223. "\\|" org-deadline-string
  4224. "\\|" org-closed-string
  4225. "\\)"
  4226. " *[[<]\\([^]>]+\\)[]>]")
  4227. org-maybe-keyword-time-regexp
  4228. (concat "\\(\\<\\(" org-scheduled-string
  4229. "\\|" org-deadline-string
  4230. "\\|" org-closed-string
  4231. "\\|" org-clock-string "\\)\\)?"
  4232. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4233. org-planning-or-clock-line-re
  4234. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4235. "\\|" org-deadline-string
  4236. "\\|" org-closed-string "\\|" org-clock-string
  4237. "\\)\\>\\)")
  4238. org-all-time-keywords
  4239. (mapcar (lambda (w) (substring w 0 -1))
  4240. (list org-scheduled-string org-deadline-string
  4241. org-clock-string org-closed-string))
  4242. )
  4243. (org-compute-latex-and-specials-regexp)
  4244. (org-set-font-lock-defaults))))
  4245. (defun org-file-contents (file &optional noerror)
  4246. "Return the contents of FILE, as a string."
  4247. (if (or (not file)
  4248. (not (file-readable-p file)))
  4249. (if noerror
  4250. (progn
  4251. (message "Cannot read file \"%s\"" file)
  4252. (ding) (sit-for 2)
  4253. "")
  4254. (error "Cannot read file \"%s\"" file))
  4255. (with-temp-buffer
  4256. (insert-file-contents file)
  4257. (buffer-string))))
  4258. (defun org-extract-log-state-settings (x)
  4259. "Extract the log state setting from a TODO keyword string.
  4260. This will extract info from a string like \"WAIT(w@/!)\"."
  4261. (let (kw key log1 log2)
  4262. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4263. (setq kw (match-string 1 x)
  4264. key (and (match-end 2) (match-string 2 x))
  4265. log1 (and (match-end 3) (match-string 3 x))
  4266. log2 (and (match-end 4) (match-string 4 x)))
  4267. (and (or log1 log2)
  4268. (list kw
  4269. (and log1 (if (equal log1 "!") 'time 'note))
  4270. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4271. (defun org-remove-keyword-keys (list)
  4272. "Remove a pair of parenthesis at the end of each string in LIST."
  4273. (mapcar (lambda (x)
  4274. (if (string-match "(.*)$" x)
  4275. (substring x 0 (match-beginning 0))
  4276. x))
  4277. list))
  4278. (defun org-assign-fast-keys (alist)
  4279. "Assign fast keys to a keyword-key alist.
  4280. Respect keys that are already there."
  4281. (let (new e (alt ?0))
  4282. (while (setq e (pop alist))
  4283. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4284. (cdr e)) ;; Key already assigned.
  4285. (push e new)
  4286. (let ((clist (string-to-list (downcase (car e))))
  4287. (used (append new alist)))
  4288. (when (= (car clist) ?@)
  4289. (pop clist))
  4290. (while (and clist (rassoc (car clist) used))
  4291. (pop clist))
  4292. (unless clist
  4293. (while (rassoc alt used)
  4294. (incf alt)))
  4295. (push (cons (car e) (or (car clist) alt)) new))))
  4296. (nreverse new)))
  4297. ;;; Some variables used in various places
  4298. (defvar org-window-configuration nil
  4299. "Used in various places to store a window configuration.")
  4300. (defvar org-selected-window nil
  4301. "Used in various places to store a window configuration.")
  4302. (defvar org-finish-function nil
  4303. "Function to be called when `C-c C-c' is used.
  4304. This is for getting out of special buffers like remember.")
  4305. ;; FIXME: Occasionally check by commenting these, to make sure
  4306. ;; no other functions uses these, forgetting to let-bind them.
  4307. (defvar entry)
  4308. (defvar last-state)
  4309. (defvar date)
  4310. ;; Defined somewhere in this file, but used before definition.
  4311. (defvar org-entities) ;; defined in org-entities.el
  4312. (defvar org-struct-menu)
  4313. (defvar org-org-menu)
  4314. (defvar org-tbl-menu)
  4315. ;;;; Define the Org-mode
  4316. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4317. (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"))
  4318. ;; We use a before-change function to check if a table might need
  4319. ;; an update.
  4320. (defvar org-table-may-need-update t
  4321. "Indicates that a table might need an update.
  4322. This variable is set by `org-before-change-function'.
  4323. `org-table-align' sets it back to nil.")
  4324. (defun org-before-change-function (beg end)
  4325. "Every change indicates that a table might need an update."
  4326. (setq org-table-may-need-update t))
  4327. (defvar org-mode-map)
  4328. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4329. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4330. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4331. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4332. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4333. (defvar org-table-buffer-is-an nil)
  4334. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4335. ;; some places -- e.g. see the macro org-with-limited-levels.
  4336. ;;
  4337. ;; In Org buffers, the value of `outline-regexp' is that of
  4338. ;; `org-outline-regexp'. The only function still directly relying on
  4339. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4340. ;; job when `orgstruct-mode' is active.
  4341. (defvar org-outline-regexp "\\*+ "
  4342. "Regexp to match Org headlines.")
  4343. (defconst org-outline-regexp-bol "^\\*+ "
  4344. "Regexp to match Org headlines.
  4345. This is similar to `org-outline-regexp' but additionally makes
  4346. sure that we are at the beginning of the line.")
  4347. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4348. "Matches an headline, putting stars and text into groups.
  4349. Stars are put in group 1 and the trimmed body in group 2.")
  4350. (defvar bidi-paragraph-direction)
  4351. ;;;###autoload
  4352. (define-derived-mode org-mode outline-mode "Org"
  4353. "Outline-based notes management and organizer, alias
  4354. \"Carsten's outline-mode for keeping track of everything.\"
  4355. Org-mode develops organizational tasks around a NOTES file which
  4356. contains information about projects as plain text. Org-mode is
  4357. implemented on top of outline-mode, which is ideal to keep the content
  4358. of large files well structured. It supports ToDo items, deadlines and
  4359. time stamps, which magically appear in the diary listing of the Emacs
  4360. calendar. Tables are easily created with a built-in table editor.
  4361. Plain text URL-like links connect to websites, emails (VM), Usenet
  4362. messages (Gnus), BBDB entries, and any files related to the project.
  4363. For printing and sharing of notes, an Org-mode file (or a part of it)
  4364. can be exported as a structured ASCII or HTML file.
  4365. The following commands are available:
  4366. \\{org-mode-map}"
  4367. ;; Get rid of Outline menus, they are not needed
  4368. ;; Need to do this here because define-derived-mode sets up
  4369. ;; the keymap so late. Still, it is a waste to call this each time
  4370. ;; we switch another buffer into org-mode.
  4371. (if (featurep 'xemacs)
  4372. (when (boundp 'outline-mode-menu-heading)
  4373. ;; Assume this is Greg's port, it uses easymenu
  4374. (easy-menu-remove outline-mode-menu-heading)
  4375. (easy-menu-remove outline-mode-menu-show)
  4376. (easy-menu-remove outline-mode-menu-hide))
  4377. (define-key org-mode-map [menu-bar headings] 'undefined)
  4378. (define-key org-mode-map [menu-bar hide] 'undefined)
  4379. (define-key org-mode-map [menu-bar show] 'undefined))
  4380. (org-load-modules-maybe)
  4381. (easy-menu-add org-org-menu)
  4382. (easy-menu-add org-tbl-menu)
  4383. (org-install-agenda-files-menu)
  4384. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4385. (add-to-invisibility-spec '(org-cwidth))
  4386. (add-to-invisibility-spec '(org-hide-block . t))
  4387. (when (featurep 'xemacs)
  4388. (org-set-local 'line-move-ignore-invisible t))
  4389. (org-set-local 'outline-regexp org-outline-regexp)
  4390. (org-set-local 'outline-level 'org-outline-level)
  4391. (setq bidi-paragraph-direction 'left-to-right)
  4392. (when (and org-ellipsis
  4393. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4394. (fboundp 'make-glyph-code))
  4395. (unless org-display-table
  4396. (setq org-display-table (make-display-table)))
  4397. (set-display-table-slot
  4398. org-display-table 4
  4399. (vconcat (mapcar
  4400. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4401. org-ellipsis)))
  4402. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4403. (setq buffer-display-table org-display-table))
  4404. (org-set-regexps-and-options)
  4405. (when (and org-tag-faces (not org-tags-special-faces-re))
  4406. ;; tag faces set outside customize.... force initialization.
  4407. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4408. ;; Calc embedded
  4409. (org-set-local 'calc-embedded-open-mode "# ")
  4410. (modify-syntax-entry ?@ "w")
  4411. (if org-startup-truncated (setq truncate-lines t))
  4412. (org-set-local 'font-lock-unfontify-region-function
  4413. 'org-unfontify-region)
  4414. ;; Activate before-change-function
  4415. (org-set-local 'org-table-may-need-update t)
  4416. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4417. 'local)
  4418. ;; Check for running clock before killing a buffer
  4419. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4420. ;; Paragraphs and auto-filling
  4421. (org-set-autofill-regexps)
  4422. (setq indent-line-function 'org-indent-line-function)
  4423. (org-update-radio-target-regexp)
  4424. ;; Beginning/end of defun
  4425. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4426. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4427. ;; Next error for sparse trees
  4428. (org-set-local 'next-error-function 'org-occur-next-match)
  4429. ;; Make sure dependence stuff works reliably, even for users who set it
  4430. ;; too late :-(
  4431. (if org-enforce-todo-dependencies
  4432. (add-hook 'org-blocker-hook
  4433. 'org-block-todo-from-children-or-siblings-or-parent)
  4434. (remove-hook 'org-blocker-hook
  4435. 'org-block-todo-from-children-or-siblings-or-parent))
  4436. (if org-enforce-todo-checkbox-dependencies
  4437. (add-hook 'org-blocker-hook
  4438. 'org-block-todo-from-checkboxes)
  4439. (remove-hook 'org-blocker-hook
  4440. 'org-block-todo-from-checkboxes))
  4441. ;; Comment characters
  4442. (org-set-local 'comment-start "#")
  4443. (org-set-local 'comment-padding " ")
  4444. ;; Align options lines
  4445. (org-set-local
  4446. 'align-mode-rules-list
  4447. '((org-in-buffer-settings
  4448. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4449. (modes . '(org-mode)))))
  4450. ;; Imenu
  4451. (org-set-local 'imenu-create-index-function
  4452. 'org-imenu-get-tree)
  4453. ;; Make isearch reveal context
  4454. (if (or (featurep 'xemacs)
  4455. (not (boundp 'outline-isearch-open-invisible-function)))
  4456. ;; Emacs 21 and XEmacs make use of the hook
  4457. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4458. ;; Emacs 22 deals with this through a special variable
  4459. (org-set-local 'outline-isearch-open-invisible-function
  4460. (lambda (&rest ignore) (org-show-context 'isearch))))
  4461. ;; Turn on org-beamer-mode?
  4462. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4463. ;; Setup the pcomplete hooks
  4464. (set (make-local-variable 'pcomplete-command-completion-function)
  4465. 'org-pcomplete-initial)
  4466. (set (make-local-variable 'pcomplete-command-name-function)
  4467. 'org-command-at-point)
  4468. (set (make-local-variable 'pcomplete-default-completion-function)
  4469. 'ignore)
  4470. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4471. 'org-parse-arguments)
  4472. (set (make-local-variable 'pcomplete-termination-string) "")
  4473. (set (make-local-variable 'face-remapping-alist)
  4474. '((default org-default)))
  4475. ;; If empty file that did not turn on org-mode automatically, make it to.
  4476. (if (and org-insert-mode-line-in-empty-file
  4477. (org-called-interactively-p 'any)
  4478. (= (point-min) (point-max)))
  4479. (insert "# -*- mode: org -*-\n\n"))
  4480. (unless org-inhibit-startup
  4481. (when org-startup-align-all-tables
  4482. (let ((bmp (buffer-modified-p)))
  4483. (org-table-map-tables 'org-table-align 'quietly)
  4484. (set-buffer-modified-p bmp)))
  4485. (when org-startup-with-inline-images
  4486. (org-display-inline-images))
  4487. (when org-startup-indented
  4488. (require 'org-indent)
  4489. (org-indent-mode 1))
  4490. (unless org-inhibit-startup-visibility-stuff
  4491. (org-set-startup-visibility))))
  4492. (when (fboundp 'abbrev-table-put)
  4493. (abbrev-table-put org-mode-abbrev-table
  4494. :parents (list text-mode-abbrev-table)))
  4495. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4496. (defun org-current-time ()
  4497. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4498. (if (> (car org-time-stamp-rounding-minutes) 1)
  4499. (let ((r (car org-time-stamp-rounding-minutes))
  4500. (time (decode-time)))
  4501. (apply 'encode-time
  4502. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4503. (nthcdr 2 time))))
  4504. (current-time)))
  4505. (defun org-today ()
  4506. "Return today date, considering `org-extend-today-until'."
  4507. (time-to-days
  4508. (time-subtract (current-time)
  4509. (list 0 (* 3600 org-extend-today-until) 0))))
  4510. ;;;; Font-Lock stuff, including the activators
  4511. (defvar org-mouse-map (make-sparse-keymap))
  4512. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4513. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4514. (when org-mouse-1-follows-link
  4515. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4516. (when org-tab-follows-link
  4517. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4518. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4519. (require 'font-lock)
  4520. (defconst org-non-link-chars "]\t\n\r<>")
  4521. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4522. "shell" "elisp" "doi" "message"))
  4523. (defvar org-link-types-re nil
  4524. "Matches a link that has a url-like prefix like \"http:\"")
  4525. (defvar org-link-re-with-space nil
  4526. "Matches a link with spaces, optional angular brackets around it.")
  4527. (defvar org-link-re-with-space2 nil
  4528. "Matches a link with spaces, optional angular brackets around it.")
  4529. (defvar org-link-re-with-space3 nil
  4530. "Matches a link with spaces, only for internal part in bracket links.")
  4531. (defvar org-angle-link-re nil
  4532. "Matches link with angular brackets, spaces are allowed.")
  4533. (defvar org-plain-link-re nil
  4534. "Matches plain link, without spaces.")
  4535. (defvar org-bracket-link-regexp nil
  4536. "Matches a link in double brackets.")
  4537. (defvar org-bracket-link-analytic-regexp nil
  4538. "Regular expression used to analyze links.
  4539. Here is what the match groups contain after a match:
  4540. 1: http:
  4541. 2: http
  4542. 3: path
  4543. 4: [desc]
  4544. 5: desc")
  4545. (defvar org-bracket-link-analytic-regexp++ nil
  4546. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4547. (defvar org-any-link-re nil
  4548. "Regular expression matching any link.")
  4549. (defcustom org-match-sexp-depth 3
  4550. "Number of stacked braces for sub/superscript matching.
  4551. This has to be set before loading org.el to be effective."
  4552. :group 'org-export-translation ; ??????????????????????????/
  4553. :type 'integer)
  4554. (defun org-create-multibrace-regexp (left right n)
  4555. "Create a regular expression which will match a balanced sexp.
  4556. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4557. as single character strings.
  4558. The regexp returned will match the entire expression including the
  4559. delimiters. It will also define a single group which contains the
  4560. match except for the outermost delimiters. The maximum depth of
  4561. stacked delimiters is N. Escaping delimiters is not possible."
  4562. (let* ((nothing (concat "[^" left right "]*?"))
  4563. (or "\\|")
  4564. (re nothing)
  4565. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4566. (while (> n 1)
  4567. (setq n (1- n)
  4568. re (concat re or next)
  4569. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4570. (concat left "\\(" re "\\)" right)))
  4571. (defvar org-match-substring-regexp
  4572. (concat
  4573. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4574. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4575. "\\|"
  4576. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4577. "\\|"
  4578. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4579. "The regular expression matching a sub- or superscript.")
  4580. (defvar org-match-substring-with-braces-regexp
  4581. (concat
  4582. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4583. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4584. "\\)")
  4585. "The regular expression matching a sub- or superscript, forcing braces.")
  4586. (defun org-make-link-regexps ()
  4587. "Update the link regular expressions.
  4588. This should be called after the variable `org-link-types' has changed."
  4589. (setq org-link-types-re
  4590. (concat
  4591. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4592. org-link-re-with-space
  4593. (concat
  4594. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4595. "\\([^" org-non-link-chars " ]"
  4596. "[^" org-non-link-chars "]*"
  4597. "[^" org-non-link-chars " ]\\)>?")
  4598. org-link-re-with-space2
  4599. (concat
  4600. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4601. "\\([^" org-non-link-chars " ]"
  4602. "[^\t\n\r]*"
  4603. "[^" org-non-link-chars " ]\\)>?")
  4604. org-link-re-with-space3
  4605. (concat
  4606. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4607. "\\([^" org-non-link-chars " ]"
  4608. "[^\t\n\r]*\\)")
  4609. org-angle-link-re
  4610. (concat
  4611. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4612. "\\([^" org-non-link-chars " ]"
  4613. "[^" org-non-link-chars "]*"
  4614. "\\)>")
  4615. org-plain-link-re
  4616. (concat
  4617. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4618. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4619. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4620. org-bracket-link-regexp
  4621. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4622. org-bracket-link-analytic-regexp
  4623. (concat
  4624. "\\[\\["
  4625. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4626. "\\([^]]+\\)"
  4627. "\\]"
  4628. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4629. "\\]")
  4630. org-bracket-link-analytic-regexp++
  4631. (concat
  4632. "\\[\\["
  4633. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4634. "\\([^]]+\\)"
  4635. "\\]"
  4636. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4637. "\\]")
  4638. org-any-link-re
  4639. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4640. org-angle-link-re "\\)\\|\\("
  4641. org-plain-link-re "\\)")))
  4642. (org-make-link-regexps)
  4643. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4644. "Regular expression for fast time stamp matching.")
  4645. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4646. "Regular expression for fast time stamp matching.")
  4647. (defconst org-ts-regexp0
  4648. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4649. "Regular expression matching time strings for analysis.
  4650. This one does not require the space after the date, so it can be used
  4651. on a string that terminates immediately after the date.")
  4652. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4653. "Regular expression matching time strings for analysis.")
  4654. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4655. "Regular expression matching time stamps, with groups.")
  4656. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4657. "Regular expression matching time stamps (also [..]), with groups.")
  4658. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4659. "Regular expression matching a time stamp range.")
  4660. (defconst org-tr-regexp-both
  4661. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4662. "Regular expression matching a time stamp range.")
  4663. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4664. org-ts-regexp "\\)?")
  4665. "Regular expression matching a time stamp or time stamp range.")
  4666. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4667. org-ts-regexp-both "\\)?")
  4668. "Regular expression matching a time stamp or time stamp range.
  4669. The time stamps may be either active or inactive.")
  4670. (defvar org-emph-face nil)
  4671. (defun org-do-emphasis-faces (limit)
  4672. "Run through the buffer and add overlays to emphasized strings."
  4673. (let (rtn a)
  4674. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4675. (if (not (= (char-after (match-beginning 3))
  4676. (char-after (match-beginning 4))))
  4677. (progn
  4678. (setq rtn t)
  4679. (setq a (assoc (match-string 3) org-emphasis-alist))
  4680. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4681. 'face
  4682. (nth 1 a))
  4683. (and (nth 4 a)
  4684. (org-remove-flyspell-overlays-in
  4685. (match-beginning 0) (match-end 0)))
  4686. (add-text-properties (match-beginning 2) (match-end 2)
  4687. '(font-lock-multiline t org-emphasis t))
  4688. (when org-hide-emphasis-markers
  4689. (add-text-properties (match-end 4) (match-beginning 5)
  4690. '(invisible org-link))
  4691. (add-text-properties (match-beginning 3) (match-end 3)
  4692. '(invisible org-link)))))
  4693. (backward-char 1))
  4694. rtn))
  4695. (defun org-emphasize (&optional char)
  4696. "Insert or change an emphasis, i.e. a font like bold or italic.
  4697. If there is an active region, change that region to a new emphasis.
  4698. If there is no region, just insert the marker characters and position
  4699. the cursor between them.
  4700. CHAR should be either the marker character, or the first character of the
  4701. HTML tag associated with that emphasis. If CHAR is a space, the means
  4702. to remove the emphasis of the selected region.
  4703. If char is not given (for example in an interactive call) it
  4704. will be prompted for."
  4705. (interactive)
  4706. (let ((eal org-emphasis-alist) e det
  4707. (erc org-emphasis-regexp-components)
  4708. (prompt "")
  4709. (string "") beg end move tag c s)
  4710. (if (org-region-active-p)
  4711. (setq beg (region-beginning) end (region-end)
  4712. string (buffer-substring beg end))
  4713. (setq move t))
  4714. (while (setq e (pop eal))
  4715. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4716. c (aref tag 0))
  4717. (push (cons c (string-to-char (car e))) det)
  4718. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4719. (substring tag 1)))))
  4720. (setq det (nreverse det))
  4721. (unless char
  4722. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4723. (setq char (read-char-exclusive)))
  4724. (setq char (or (cdr (assoc char det)) char))
  4725. (if (equal char ?\ )
  4726. (setq s "" move nil)
  4727. (unless (assoc (char-to-string char) org-emphasis-alist)
  4728. (error "No such emphasis marker: \"%c\"" char))
  4729. (setq s (char-to-string char)))
  4730. (while (and (> (length string) 1)
  4731. (equal (substring string 0 1) (substring string -1))
  4732. (assoc (substring string 0 1) org-emphasis-alist))
  4733. (setq string (substring string 1 -1)))
  4734. (setq string (concat s string s))
  4735. (if beg (delete-region beg end))
  4736. (unless (or (bolp)
  4737. (string-match (concat "[" (nth 0 erc) "\n]")
  4738. (char-to-string (char-before (point)))))
  4739. (insert " "))
  4740. (unless (or (eobp)
  4741. (string-match (concat "[" (nth 1 erc) "\n]")
  4742. (char-to-string (char-after (point)))))
  4743. (insert " ") (backward-char 1))
  4744. (insert string)
  4745. (and move (backward-char 1))))
  4746. (defconst org-nonsticky-props
  4747. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4748. (defsubst org-rear-nonsticky-at (pos)
  4749. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4750. (defun org-activate-plain-links (limit)
  4751. "Run through the buffer and add overlays to links."
  4752. (catch 'exit
  4753. (let (f)
  4754. (if (re-search-forward org-plain-link-re limit t)
  4755. (progn
  4756. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4757. (setq f (get-text-property (match-beginning 0) 'face))
  4758. (if (or (eq f 'org-tag)
  4759. (and (listp f) (memq 'org-tag f)))
  4760. nil
  4761. (add-text-properties (match-beginning 0) (match-end 0)
  4762. (list 'mouse-face 'highlight
  4763. 'face 'org-link
  4764. 'keymap org-mouse-map))
  4765. (org-rear-nonsticky-at (match-end 0)))
  4766. t)))))
  4767. (defun org-activate-code (limit)
  4768. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4769. (progn
  4770. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4771. (remove-text-properties (match-beginning 0) (match-end 0)
  4772. '(display t invisible t intangible t))
  4773. t)))
  4774. (defcustom org-src-fontify-natively nil
  4775. "When non-nil, fontify code in code blocks."
  4776. :type 'boolean
  4777. :version "24.1"
  4778. :group 'org-appearance
  4779. :group 'org-babel)
  4780. (defun org-fontify-meta-lines-and-blocks (limit)
  4781. (condition-case nil
  4782. (org-fontify-meta-lines-and-blocks-1 limit)
  4783. (error (message "org-mode fontification error"))))
  4784. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4785. "Fontify #+ lines and blocks, in the correct ways."
  4786. (let ((case-fold-search t))
  4787. (if (re-search-forward
  4788. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4789. limit t)
  4790. (let ((beg (match-beginning 0))
  4791. (block-start (match-end 0))
  4792. (block-end nil)
  4793. (lang (match-string 7))
  4794. (beg1 (line-beginning-position 2))
  4795. (dc1 (downcase (match-string 2)))
  4796. (dc3 (downcase (match-string 3)))
  4797. end end1 quoting block-type ovl)
  4798. (cond
  4799. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4800. ;; a single line of backend-specific content
  4801. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4802. (remove-text-properties (match-beginning 0) (match-end 0)
  4803. '(display t invisible t intangible t))
  4804. (add-text-properties (match-beginning 1) (match-end 3)
  4805. '(font-lock-fontified t face org-meta-line))
  4806. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4807. '(font-lock-fontified t face org-block))
  4808. ; for backend-specific code
  4809. t)
  4810. ((and (match-end 4) (equal dc3 "begin"))
  4811. ;; Truly a block
  4812. (setq block-type (downcase (match-string 5))
  4813. quoting (member block-type org-protecting-blocks))
  4814. (when (re-search-forward
  4815. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4816. nil t) ;; on purpose, we look further than LIMIT
  4817. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4818. (setq block-end (match-beginning 0))
  4819. (when quoting
  4820. (remove-text-properties beg end
  4821. '(display t invisible t intangible t)))
  4822. (add-text-properties
  4823. beg end
  4824. '(font-lock-fontified t font-lock-multiline t))
  4825. (add-text-properties beg beg1 '(face org-meta-line))
  4826. (add-text-properties end1 (min (point-max) (1+ end))
  4827. '(face org-meta-line)) ; for end_src
  4828. (cond
  4829. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4830. (org-src-font-lock-fontify-block lang block-start block-end)
  4831. ;; remove old background overlays
  4832. (mapc (lambda (ov)
  4833. (if (eq (overlay-get ov 'face) 'org-block-background)
  4834. (delete-overlay ov)))
  4835. (overlays-at (/ (+ beg1 block-end) 2)))
  4836. ;; add a background overlay
  4837. (setq ovl (make-overlay beg1 block-end))
  4838. (overlay-put ovl 'face 'org-block-background)
  4839. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4840. (quoting
  4841. (add-text-properties beg1 (min (point-max) (1+ end1))
  4842. '(face org-block))) ; end of source block
  4843. ((not org-fontify-quote-and-verse-blocks))
  4844. ((string= block-type "quote")
  4845. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4846. ((string= block-type "verse")
  4847. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4848. (add-text-properties beg beg1 '(face org-block-begin-line))
  4849. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4850. t))
  4851. ((member dc1 '("title:" "author:" "email:" "date:"))
  4852. (add-text-properties
  4853. beg (match-end 3)
  4854. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4855. '(font-lock-fontified t invisible t)
  4856. '(font-lock-fontified t face org-document-info-keyword)))
  4857. (add-text-properties
  4858. (match-beginning 6) (match-end 6)
  4859. (if (string-equal dc1 "title:")
  4860. '(font-lock-fontified t face org-document-title)
  4861. '(font-lock-fontified t face org-document-info))))
  4862. ((not (member (char-after beg) '(?\ ?\t)))
  4863. ;; just any other in-buffer setting, but not indented
  4864. (add-text-properties
  4865. beg (1+ (match-end 0))
  4866. '(font-lock-fontified t face org-meta-line))
  4867. t)
  4868. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4869. "orgtbl:" "tblfm:" "tblname:" "results:"
  4870. "call:" "header:" "headers:" "name:"))
  4871. (and (match-end 4) (equal dc3 "attr")))
  4872. (add-text-properties
  4873. beg (match-end 0)
  4874. '(font-lock-fontified t face org-meta-line))
  4875. t)
  4876. ((member dc3 '(" " ""))
  4877. (add-text-properties
  4878. beg (match-end 0)
  4879. '(font-lock-fontified t face font-lock-comment-face)))
  4880. (t nil))))))
  4881. (defun org-strip-protective-commas (beg end)
  4882. "Strip protective commas between BEG and END in the current buffer."
  4883. (interactive "r")
  4884. (save-excursion
  4885. (save-match-data
  4886. (goto-char beg)
  4887. (let ((front-line (save-excursion
  4888. (re-search-forward
  4889. "[^[:space:]]" end t)
  4890. (goto-char (match-beginning 0))
  4891. (current-column))))
  4892. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4893. (goto-char (match-beginning 1))
  4894. (when (= (current-column) front-line)
  4895. (replace-match "" nil nil nil 1)))))))
  4896. (defun org-activate-angle-links (limit)
  4897. "Run through the buffer and add overlays to links."
  4898. (if (re-search-forward org-angle-link-re limit t)
  4899. (progn
  4900. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4901. (add-text-properties (match-beginning 0) (match-end 0)
  4902. (list 'mouse-face 'highlight
  4903. 'keymap org-mouse-map))
  4904. (org-rear-nonsticky-at (match-end 0))
  4905. t)))
  4906. (defun org-activate-footnote-links (limit)
  4907. "Run through the buffer and add overlays to footnotes."
  4908. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4909. (when fn
  4910. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4911. (org-remove-flyspell-overlays-in beg end)
  4912. (add-text-properties beg end
  4913. (list 'mouse-face 'highlight
  4914. 'keymap org-mouse-map
  4915. 'help-echo
  4916. (if (= (point-at-bol) beg)
  4917. "Footnote definition"
  4918. "Footnote reference")
  4919. 'font-lock-fontified t
  4920. 'font-lock-multiline t
  4921. 'face 'org-footnote))))))
  4922. (defun org-activate-bracket-links (limit)
  4923. "Run through the buffer and add overlays to bracketed links."
  4924. (if (re-search-forward org-bracket-link-regexp limit t)
  4925. (let* ((help (concat "LINK: "
  4926. (org-match-string-no-properties 1)))
  4927. ;; FIXME: above we should remove the escapes.
  4928. ;; but that requires another match, protecting match data,
  4929. ;; a lot of overhead for font-lock.
  4930. (ip (org-maybe-intangible
  4931. (list 'invisible 'org-link
  4932. 'keymap org-mouse-map 'mouse-face 'highlight
  4933. 'font-lock-multiline t 'help-echo help)))
  4934. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4935. 'font-lock-multiline t 'help-echo help)))
  4936. ;; We need to remove the invisible property here. Table narrowing
  4937. ;; may have made some of this invisible.
  4938. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4939. (remove-text-properties (match-beginning 0) (match-end 0)
  4940. '(invisible nil))
  4941. (if (match-end 3)
  4942. (progn
  4943. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4944. (org-rear-nonsticky-at (match-beginning 3))
  4945. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4946. (org-rear-nonsticky-at (match-end 3))
  4947. (add-text-properties (match-end 3) (match-end 0) ip)
  4948. (org-rear-nonsticky-at (match-end 0)))
  4949. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4950. (org-rear-nonsticky-at (match-beginning 1))
  4951. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4952. (org-rear-nonsticky-at (match-end 1))
  4953. (add-text-properties (match-end 1) (match-end 0) ip)
  4954. (org-rear-nonsticky-at (match-end 0)))
  4955. t)))
  4956. (defun org-activate-dates (limit)
  4957. "Run through the buffer and add overlays to dates."
  4958. (if (re-search-forward org-tsr-regexp-both limit t)
  4959. (progn
  4960. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4961. (add-text-properties (match-beginning 0) (match-end 0)
  4962. (list 'mouse-face 'highlight
  4963. 'keymap org-mouse-map))
  4964. (org-rear-nonsticky-at (match-end 0))
  4965. (when org-display-custom-times
  4966. (if (match-end 3)
  4967. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4968. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4969. t)))
  4970. (defvar org-target-link-regexp nil
  4971. "Regular expression matching radio targets in plain text.")
  4972. (make-variable-buffer-local 'org-target-link-regexp)
  4973. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4974. "Regular expression matching a link target.")
  4975. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4976. "Regular expression matching a radio target.")
  4977. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4978. "Regular expression matching any target.")
  4979. (defun org-activate-target-links (limit)
  4980. "Run through the buffer and add overlays to target matches."
  4981. (when org-target-link-regexp
  4982. (let ((case-fold-search t))
  4983. (if (re-search-forward org-target-link-regexp limit t)
  4984. (progn
  4985. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4986. (add-text-properties (match-beginning 0) (match-end 0)
  4987. (list 'mouse-face 'highlight
  4988. 'keymap org-mouse-map
  4989. 'help-echo "Radio target link"
  4990. 'org-linked-text t))
  4991. (org-rear-nonsticky-at (match-end 0))
  4992. t)))))
  4993. (defun org-update-radio-target-regexp ()
  4994. "Find all radio targets in this file and update the regular expression."
  4995. (interactive)
  4996. (when (memq 'radio org-activate-links)
  4997. (setq org-target-link-regexp
  4998. (org-make-target-link-regexp (org-all-targets 'radio)))
  4999. (org-restart-font-lock)))
  5000. (defun org-hide-wide-columns (limit)
  5001. (let (s e)
  5002. (setq s (text-property-any (point) (or limit (point-max))
  5003. 'org-cwidth t))
  5004. (when s
  5005. (setq e (next-single-property-change s 'org-cwidth))
  5006. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5007. (goto-char e)
  5008. t)))
  5009. (defvar org-latex-and-specials-regexp nil
  5010. "Regular expression for highlighting export special stuff.")
  5011. (defvar org-match-substring-regexp)
  5012. (defvar org-match-substring-with-braces-regexp)
  5013. ;; This should be with the exporter code, but we also use if for font-locking
  5014. (defconst org-export-html-special-string-regexps
  5015. '(("\\\\-" . "&shy;")
  5016. ("---\\([^-]\\)" . "&mdash;\\1")
  5017. ("--\\([^-]\\)" . "&ndash;\\1")
  5018. ("\\.\\.\\." . "&hellip;"))
  5019. "Regular expressions for special string conversion.")
  5020. (defun org-compute-latex-and-specials-regexp ()
  5021. "Compute regular expression for stuff treated specially by exporters."
  5022. (if (not org-highlight-latex-fragments-and-specials)
  5023. (org-set-local 'org-latex-and-specials-regexp nil)
  5024. (require 'org-exp)
  5025. (let*
  5026. ((matchers (plist-get org-format-latex-options :matchers))
  5027. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5028. org-latex-regexps)))
  5029. (org-export-allow-BIND nil)
  5030. (options (org-combine-plists (org-default-export-plist)
  5031. (org-infile-export-plist)))
  5032. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5033. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5034. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5035. (org-export-html-expand (plist-get options :expand-quoted-html))
  5036. (org-export-with-special-strings (plist-get options :special-strings))
  5037. (re-sub
  5038. (cond
  5039. ((equal org-export-with-sub-superscripts '{})
  5040. (list org-match-substring-with-braces-regexp))
  5041. (org-export-with-sub-superscripts
  5042. (list org-match-substring-regexp))
  5043. (t nil)))
  5044. (re-latex
  5045. (if org-export-with-LaTeX-fragments
  5046. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5047. (re-macros
  5048. (if org-export-with-TeX-macros
  5049. (list (concat "\\\\"
  5050. (regexp-opt
  5051. (append
  5052. (delq nil
  5053. (mapcar 'car-safe
  5054. (append org-entities-user
  5055. org-entities)))
  5056. (if (boundp 'org-latex-entities)
  5057. (mapcar (lambda (x)
  5058. (or (car-safe x) x))
  5059. org-latex-entities)
  5060. nil))
  5061. 'words))) ; FIXME
  5062. ))
  5063. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5064. (re-special (if org-export-with-special-strings
  5065. (mapcar (lambda (x) (car x))
  5066. org-export-html-special-string-regexps)))
  5067. (re-rest
  5068. (delq nil
  5069. (list
  5070. (if org-export-html-expand "@<[^>\n]+>")
  5071. ))))
  5072. (org-set-local
  5073. 'org-latex-and-specials-regexp
  5074. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5075. re-rest) "\\|")))))
  5076. (defun org-do-latex-and-special-faces (limit)
  5077. "Run through the buffer and add overlays to links."
  5078. (when org-latex-and-specials-regexp
  5079. (let (rtn d)
  5080. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5081. limit t))
  5082. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5083. 'face))
  5084. '(org-code org-verbatim underline)))
  5085. (progn
  5086. (setq rtn t
  5087. d (cond ((member (char-after (1+ (match-beginning 0)))
  5088. '(?_ ?^)) 1)
  5089. (t 0)))
  5090. (font-lock-prepend-text-property
  5091. (+ d (match-beginning 0)) (match-end 0)
  5092. 'face 'org-latex-and-export-specials)
  5093. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5094. '(font-lock-multiline t)))))
  5095. rtn)))
  5096. (defun org-restart-font-lock ()
  5097. "Restart `font-lock-mode', to force refontification."
  5098. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5099. (font-lock-mode -1)
  5100. (font-lock-mode 1)))
  5101. (defun org-all-targets (&optional radio)
  5102. "Return a list of all targets in this file.
  5103. With optional argument RADIO, only find radio targets."
  5104. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5105. rtn)
  5106. (save-excursion
  5107. (goto-char (point-min))
  5108. (while (re-search-forward re nil t)
  5109. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5110. rtn)))
  5111. (defun org-make-target-link-regexp (targets)
  5112. "Make regular expression matching all strings in TARGETS.
  5113. The regular expression finds the targets also if there is a line break
  5114. between words."
  5115. (and targets
  5116. (concat
  5117. "\\<\\("
  5118. (mapconcat
  5119. (lambda (x)
  5120. (setq x (regexp-quote x))
  5121. (while (string-match " +" x)
  5122. (setq x (replace-match "\\s-+" t t x)))
  5123. x)
  5124. targets
  5125. "\\|")
  5126. "\\)\\>")))
  5127. (defun org-activate-tags (limit)
  5128. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5129. (progn
  5130. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5131. (add-text-properties (match-beginning 1) (match-end 1)
  5132. (list 'mouse-face 'highlight
  5133. 'keymap org-mouse-map))
  5134. (org-rear-nonsticky-at (match-end 1))
  5135. t)))
  5136. (defun org-outline-level ()
  5137. "Compute the outline level of the heading at point.
  5138. This function assumes that the cursor is at the beginning of a line matched
  5139. by `outline-regexp'. Otherwise it returns garbage.
  5140. If this is called at a normal headline, the level is the number of stars.
  5141. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5142. (save-excursion
  5143. (looking-at org-outline-regexp)
  5144. (1- (- (match-end 0) (match-beginning 0)))))
  5145. (defvar org-font-lock-keywords nil)
  5146. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5147. "Regular expression matching a property line.")
  5148. (defvar org-font-lock-hook nil
  5149. "Functions to be called for special font lock stuff.")
  5150. (defvar org-font-lock-set-keywords-hook nil
  5151. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5152. This is called after `org-font-lock-extra-keywords' is defined, but before
  5153. it is installed to be used by font lock. This can be useful if something
  5154. needs to be inserted at a specific position in the font-lock sequence.")
  5155. (defun org-font-lock-hook (limit)
  5156. (run-hook-with-args 'org-font-lock-hook limit))
  5157. (defun org-set-font-lock-defaults ()
  5158. (let* ((em org-fontify-emphasized-text)
  5159. (lk org-activate-links)
  5160. (org-font-lock-extra-keywords
  5161. (list
  5162. ;; Call the hook
  5163. '(org-font-lock-hook)
  5164. ;; Headlines
  5165. `(,(if org-fontify-whole-heading-line
  5166. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5167. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5168. (1 (org-get-level-face 1))
  5169. (2 (org-get-level-face 2))
  5170. (3 (org-get-level-face 3)))
  5171. ;; Table lines
  5172. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5173. (1 'org-table t))
  5174. ;; Table internals
  5175. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5176. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5177. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5178. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5179. ;; Drawers
  5180. (list org-drawer-regexp '(0 'org-special-keyword t))
  5181. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5182. ;; Properties
  5183. (list org-property-re
  5184. '(1 'org-special-keyword t)
  5185. '(3 'org-property-value t))
  5186. ;; Links
  5187. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5188. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5189. (if (memq 'plain lk) '(org-activate-plain-links))
  5190. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5191. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5192. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5193. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5194. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5195. '(org-hide-wide-columns (0 nil append))
  5196. ;; TODO keyword
  5197. (list (format org-heading-keyword-regexp-format
  5198. org-todo-regexp)
  5199. '(2 (org-get-todo-face 2) t))
  5200. ;; DONE
  5201. (if org-fontify-done-headline
  5202. (list (format org-heading-keyword-regexp-format
  5203. (concat
  5204. "\\(?:"
  5205. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5206. "\\)"))
  5207. '(2 'org-headline-done t))
  5208. nil)
  5209. ;; Priorities
  5210. '(org-font-lock-add-priority-faces)
  5211. ;; Tags
  5212. '(org-font-lock-add-tag-faces)
  5213. ;; Special keywords
  5214. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5215. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5216. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5217. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5218. ;; Emphasis
  5219. (if em
  5220. (if (featurep 'xemacs)
  5221. '(org-do-emphasis-faces (0 nil append))
  5222. '(org-do-emphasis-faces)))
  5223. ;; Checkboxes
  5224. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5225. 1 'org-checkbox prepend)
  5226. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5227. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5228. (0 (org-get-checkbox-statistics-face) t)))
  5229. ;; Description list items
  5230. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5231. 1 'bold prepend)
  5232. ;; ARCHIVEd headings
  5233. (list (concat
  5234. org-outline-regexp-bol
  5235. "\\(.*:" org-archive-tag ":.*\\)")
  5236. '(1 'org-archived prepend))
  5237. ;; Specials
  5238. '(org-do-latex-and-special-faces)
  5239. '(org-fontify-entities)
  5240. '(org-raise-scripts)
  5241. ;; Code
  5242. '(org-activate-code (1 'org-code t))
  5243. ;; COMMENT
  5244. (list (format org-heading-keyword-regexp-format
  5245. (concat "\\("
  5246. org-comment-string "\\|" org-quote-string
  5247. "\\)"))
  5248. '(2 'org-special-keyword t))
  5249. '("^#.*" (0 'font-lock-comment-face t))
  5250. ;; Blocks and meta lines
  5251. '(org-fontify-meta-lines-and-blocks)
  5252. )))
  5253. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5254. (run-hooks 'org-font-lock-set-keywords-hook)
  5255. ;; Now set the full font-lock-keywords
  5256. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5257. (org-set-local 'font-lock-defaults
  5258. '(org-font-lock-keywords t nil nil backward-paragraph))
  5259. (kill-local-variable 'font-lock-keywords) nil))
  5260. (defun org-toggle-pretty-entities ()
  5261. "Toggle the composition display of entities as UTF8 characters."
  5262. (interactive)
  5263. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5264. (org-restart-font-lock)
  5265. (if org-pretty-entities
  5266. (message "Entities are displayed as UTF8 characters")
  5267. (save-restriction
  5268. (widen)
  5269. (org-decompose-region (point-min) (point-max))
  5270. (message "Entities are displayed plain"))))
  5271. (defun org-fontify-entities (limit)
  5272. "Find an entity to fontify."
  5273. (let (ee)
  5274. (when org-pretty-entities
  5275. (catch 'match
  5276. (while (re-search-forward
  5277. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5278. limit t)
  5279. (if (and (not (org-in-indented-comment-line))
  5280. (setq ee (org-entity-get (match-string 1)))
  5281. (= (length (nth 6 ee)) 1))
  5282. (progn
  5283. (add-text-properties
  5284. (match-beginning 0) (match-end 1)
  5285. (list 'font-lock-fontified t))
  5286. (compose-region (match-beginning 0) (match-end 1)
  5287. (nth 6 ee) nil)
  5288. (backward-char 1)
  5289. (throw 'match t))))
  5290. nil))))
  5291. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5292. "Fontify string S like in Org-mode."
  5293. (with-temp-buffer
  5294. (insert s)
  5295. (let ((org-odd-levels-only odd-levels))
  5296. (org-mode)
  5297. (font-lock-fontify-buffer)
  5298. (buffer-string))))
  5299. (defvar org-m nil)
  5300. (defvar org-l nil)
  5301. (defvar org-f nil)
  5302. (defun org-get-level-face (n)
  5303. "Get the right face for match N in font-lock matching of headlines."
  5304. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5305. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5306. (if org-cycle-level-faces
  5307. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5308. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5309. (cond
  5310. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5311. ((eq n 2) org-f)
  5312. (t (if org-level-color-stars-only nil org-f))))
  5313. (defun org-get-todo-face (kwd)
  5314. "Get the right face for a TODO keyword KWD.
  5315. If KWD is a number, get the corresponding match group."
  5316. (if (numberp kwd) (setq kwd (match-string kwd)))
  5317. (or (org-face-from-face-or-color
  5318. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5319. (and (member kwd org-done-keywords) 'org-done)
  5320. 'org-todo))
  5321. (defun org-face-from-face-or-color (context inherit face-or-color)
  5322. "Create a face list that inherits INHERIT, but sets the foreground color.
  5323. When FACE-OR-COLOR is not a string, just return it."
  5324. (if (stringp face-or-color)
  5325. (list :inherit inherit
  5326. (cdr (assoc context org-faces-easy-properties))
  5327. face-or-color)
  5328. face-or-color))
  5329. (defun org-font-lock-add-tag-faces (limit)
  5330. "Add the special tag faces."
  5331. (when (and org-tag-faces org-tags-special-faces-re)
  5332. (while (re-search-forward org-tags-special-faces-re limit t)
  5333. (add-text-properties (match-beginning 1) (match-end 1)
  5334. (list 'face (org-get-tag-face 1)
  5335. 'font-lock-fontified t))
  5336. (backward-char 1))))
  5337. (defun org-font-lock-add-priority-faces (limit)
  5338. "Add the special priority faces."
  5339. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5340. (add-text-properties
  5341. (match-beginning 0) (match-end 0)
  5342. (list 'face (or (org-face-from-face-or-color
  5343. 'priority 'org-special-keyword
  5344. (cdr (assoc (char-after (match-beginning 1))
  5345. org-priority-faces)))
  5346. 'org-special-keyword)
  5347. 'font-lock-fontified t))))
  5348. (defun org-get-tag-face (kwd)
  5349. "Get the right face for a TODO keyword KWD.
  5350. If KWD is a number, get the corresponding match group."
  5351. (if (numberp kwd) (setq kwd (match-string kwd)))
  5352. (or (org-face-from-face-or-color
  5353. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5354. 'org-tag))
  5355. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5356. "Remove fontification and activation overlays from links."
  5357. (font-lock-default-unfontify-region beg end)
  5358. (let* ((buffer-undo-list t)
  5359. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5360. (inhibit-modification-hooks t)
  5361. deactivate-mark buffer-file-name buffer-file-truename)
  5362. (org-decompose-region beg end)
  5363. (remove-text-properties beg end
  5364. '(mouse-face t keymap t org-linked-text t
  5365. invisible t intangible t
  5366. org-no-flyspell t org-emphasis t))
  5367. (org-remove-font-lock-display-properties beg end)))
  5368. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5369. ((raise 0.3) (height 0.7))
  5370. ((raise -0.5))
  5371. ((raise 0.5)))
  5372. "Display properties for showing superscripts and subscripts.")
  5373. (defun org-remove-font-lock-display-properties (beg end)
  5374. "Remove specific display properties that have been added by font lock.
  5375. The will remove the raise properties that are used to show superscripts
  5376. and subscripts."
  5377. (let (next prop)
  5378. (while (< beg end)
  5379. (setq next (next-single-property-change beg 'display nil end)
  5380. prop (get-text-property beg 'display))
  5381. (if (member prop org-script-display)
  5382. (put-text-property beg next 'display nil))
  5383. (setq beg next))))
  5384. (defun org-raise-scripts (limit)
  5385. "Add raise properties to sub/superscripts."
  5386. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5387. (if (re-search-forward
  5388. (if (eq org-use-sub-superscripts t)
  5389. org-match-substring-regexp
  5390. org-match-substring-with-braces-regexp)
  5391. limit t)
  5392. (let* ((pos (point)) table-p comment-p
  5393. (mpos (match-beginning 3))
  5394. (emph-p (get-text-property mpos 'org-emphasis))
  5395. (link-p (get-text-property mpos 'mouse-face))
  5396. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5397. (goto-char (point-at-bol))
  5398. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5399. comment-p (org-looking-at-p "[ \t]*#"))
  5400. (goto-char pos)
  5401. ;; FIXME: Should we go back one character here, for a_b^c
  5402. ;; (goto-char (1- pos)) ;????????????????????
  5403. (if (or comment-p emph-p link-p keyw-p)
  5404. t
  5405. (put-text-property (match-beginning 3) (match-end 0)
  5406. 'display
  5407. (if (equal (char-after (match-beginning 2)) ?^)
  5408. (nth (if table-p 3 1) org-script-display)
  5409. (nth (if table-p 2 0) org-script-display)))
  5410. (add-text-properties (match-beginning 2) (match-end 2)
  5411. (list 'invisible t
  5412. 'org-dwidth t 'org-dwidth-n 1))
  5413. (if (and (eq (char-after (match-beginning 3)) ?{)
  5414. (eq (char-before (match-end 3)) ?}))
  5415. (progn
  5416. (add-text-properties
  5417. (match-beginning 3) (1+ (match-beginning 3))
  5418. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5419. (add-text-properties
  5420. (1- (match-end 3)) (match-end 3)
  5421. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5422. t)))))
  5423. ;;;; Visibility cycling, including org-goto and indirect buffer
  5424. ;;; Cycling
  5425. (defvar org-cycle-global-status nil)
  5426. (make-variable-buffer-local 'org-cycle-global-status)
  5427. (defvar org-cycle-subtree-status nil)
  5428. (make-variable-buffer-local 'org-cycle-subtree-status)
  5429. ;;;###autoload
  5430. (defvar org-inlinetask-min-level)
  5431. (defun org-cycle (&optional arg)
  5432. "TAB-action and visibility cycling for Org-mode.
  5433. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5434. is outline visibility cycling, but it also invokes other actions
  5435. in special contexts.
  5436. - When this function is called with a prefix argument, rotate the entire
  5437. buffer through 3 states (global cycling)
  5438. 1. OVERVIEW: Show only top-level headlines.
  5439. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5440. 3. SHOW ALL: Show everything.
  5441. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5442. determined by the variable `org-startup-folded', and by any VISIBILITY
  5443. properties in the buffer.
  5444. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5445. including any drawers.
  5446. - When inside a table, re-align the table and move to the next field.
  5447. - When point is at the beginning of a headline, rotate the subtree started
  5448. by this line through 3 different states (local cycling)
  5449. 1. FOLDED: Only the main headline is shown.
  5450. 2. CHILDREN: The main headline and the direct children are shown.
  5451. From this state, you can move to one of the children
  5452. and zoom in further.
  5453. 3. SUBTREE: Show the entire subtree, including body text.
  5454. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5455. - When point is at the beginning of an empty headline and the variable
  5456. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5457. of the headline by demoting and promoting it to likely levels. This
  5458. speeds up creation document structure by pressing TAB once or several
  5459. times right after creating a new headline.
  5460. - When there is a numeric prefix, go up to a heading with level ARG, do
  5461. a `show-subtree' and return to the previous cursor position. If ARG
  5462. is negative, go up that many levels.
  5463. - When point is not at the beginning of a headline, execute the global
  5464. binding for TAB, which is re-indenting the line. See the option
  5465. `org-cycle-emulate-tab' for details.
  5466. - Special case: if point is at the beginning of the buffer and there is
  5467. no headline in line 1, this function will act as if called with prefix arg
  5468. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5469. But only if also the variable `org-cycle-global-at-bob' is t."
  5470. (interactive "P")
  5471. (org-load-modules-maybe)
  5472. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5473. (and org-cycle-level-after-item/entry-creation
  5474. (or (org-cycle-level)
  5475. (org-cycle-item-indentation))))
  5476. (let* ((limit-level
  5477. (or org-cycle-max-level
  5478. (and (boundp 'org-inlinetask-min-level)
  5479. org-inlinetask-min-level
  5480. (1- org-inlinetask-min-level))))
  5481. (nstars (and limit-level
  5482. (if org-odd-levels-only
  5483. (and limit-level (1- (* limit-level 2)))
  5484. limit-level)))
  5485. (org-outline-regexp
  5486. (if (not (eq major-mode 'org-mode))
  5487. outline-regexp
  5488. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5489. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5490. (not (looking-at org-outline-regexp))))
  5491. (org-cycle-hook
  5492. (if bob-special
  5493. (delq 'org-optimize-window-after-visibility-change
  5494. (copy-sequence org-cycle-hook))
  5495. org-cycle-hook))
  5496. (pos (point)))
  5497. (if (or bob-special (equal arg '(4)))
  5498. ;; special case: use global cycling
  5499. (setq arg t))
  5500. (cond
  5501. ((equal arg '(16))
  5502. (setq last-command 'dummy)
  5503. (org-set-startup-visibility)
  5504. (message "Startup visibility, plus VISIBILITY properties"))
  5505. ((equal arg '(64))
  5506. (show-all)
  5507. (message "Entire buffer visible, including drawers"))
  5508. ;; Table: enter it or move to the next field.
  5509. ((org-at-table-p 'any)
  5510. (if (org-at-table.el-p)
  5511. (message "Use C-c ' to edit table.el tables")
  5512. (if arg (org-table-edit-field t)
  5513. (org-table-justify-field-maybe)
  5514. (call-interactively 'org-table-next-field))))
  5515. ((run-hook-with-args-until-success
  5516. 'org-tab-after-check-for-table-hook))
  5517. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5518. ((eq arg t) (org-cycle-internal-global))
  5519. ;; Drawers: delegate to `org-flag-drawer'.
  5520. ((and org-drawers org-drawer-regexp
  5521. (save-excursion
  5522. (beginning-of-line 1)
  5523. (looking-at org-drawer-regexp)))
  5524. (org-flag-drawer ; toggle block visibility
  5525. (not (get-char-property (match-end 0) 'invisible))))
  5526. ;; Show-subtree, ARG levels up from here.
  5527. ((integerp arg)
  5528. (save-excursion
  5529. (org-back-to-heading)
  5530. (outline-up-heading (if (< arg 0) (- arg)
  5531. (- (funcall outline-level) arg)))
  5532. (org-show-subtree)))
  5533. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5534. ((and (featurep 'org-inlinetask)
  5535. (org-inlinetask-at-task-p)
  5536. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5537. (org-inlinetask-toggle-visibility))
  5538. ((org-try-cdlatex-tab))
  5539. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5540. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5541. (save-excursion (beginning-of-line 1)
  5542. (looking-at org-outline-regexp)))
  5543. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5544. (org-cycle-internal-local))
  5545. ;; From there: TAB emulation and template completion.
  5546. (buffer-read-only (org-back-to-heading))
  5547. ((run-hook-with-args-until-success
  5548. 'org-tab-after-check-for-cycling-hook))
  5549. ((org-try-structure-completion))
  5550. ((run-hook-with-args-until-success
  5551. 'org-tab-before-tab-emulation-hook))
  5552. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5553. (or (not (bolp))
  5554. (not (looking-at org-outline-regexp))))
  5555. (call-interactively (global-key-binding "\t")))
  5556. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5557. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5558. (or (and (eq org-cycle-emulate-tab 'white)
  5559. (= (match-end 0) (point-at-eol)))
  5560. (and (eq org-cycle-emulate-tab 'whitestart)
  5561. (>= (match-end 0) pos))))
  5562. t
  5563. (eq org-cycle-emulate-tab t))
  5564. (call-interactively (global-key-binding "\t")))
  5565. (t (save-excursion
  5566. (org-back-to-heading)
  5567. (org-cycle)))))))
  5568. (defun org-cycle-internal-global ()
  5569. "Do the global cycling action."
  5570. ;; Hack to avoid display of messages for .org attachments in Gnus
  5571. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5572. (cond
  5573. ((and (eq last-command this-command)
  5574. (eq org-cycle-global-status 'overview))
  5575. ;; We just created the overview - now do table of contents
  5576. ;; This can be slow in very large buffers, so indicate action
  5577. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5578. (unless ga (message "CONTENTS..."))
  5579. (org-content)
  5580. (unless ga (message "CONTENTS...done"))
  5581. (setq org-cycle-global-status 'contents)
  5582. (run-hook-with-args 'org-cycle-hook 'contents))
  5583. ((and (eq last-command this-command)
  5584. (eq org-cycle-global-status 'contents))
  5585. ;; We just showed the table of contents - now show everything
  5586. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5587. (show-all)
  5588. (unless ga (message "SHOW ALL"))
  5589. (setq org-cycle-global-status 'all)
  5590. (run-hook-with-args 'org-cycle-hook 'all))
  5591. (t
  5592. ;; Default action: go to overview
  5593. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5594. (org-overview)
  5595. (unless ga (message "OVERVIEW"))
  5596. (setq org-cycle-global-status 'overview)
  5597. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5598. (defun org-cycle-internal-local ()
  5599. "Do the local cycling action."
  5600. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5601. ;; First, determine end of headline (EOH), end of subtree or item
  5602. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5603. (save-excursion
  5604. (if (org-at-item-p)
  5605. (progn
  5606. (beginning-of-line)
  5607. (setq struct (org-list-struct))
  5608. (setq eoh (point-at-eol))
  5609. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5610. (setq has-children (org-list-has-child-p (point) struct)))
  5611. (org-back-to-heading)
  5612. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5613. (setq eos (save-excursion
  5614. (org-end-of-subtree t)
  5615. (unless (eobp)
  5616. (skip-chars-forward " \t\n"))
  5617. (if (eobp) (point) (1- (point)))))
  5618. (setq has-children
  5619. (or (save-excursion
  5620. (let ((level (funcall outline-level)))
  5621. (outline-next-heading)
  5622. (and (org-at-heading-p t)
  5623. (> (funcall outline-level) level))))
  5624. (save-excursion
  5625. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5626. ;; Determine end invisible part of buffer (EOL)
  5627. (beginning-of-line 2)
  5628. ;; XEmacs doesn't have `next-single-char-property-change'
  5629. (if (featurep 'xemacs)
  5630. (while (and (not (eobp)) ;; this is like `next-line'
  5631. (get-char-property (1- (point)) 'invisible))
  5632. (beginning-of-line 2))
  5633. (while (and (not (eobp)) ;; this is like `next-line'
  5634. (get-char-property (1- (point)) 'invisible))
  5635. (goto-char (next-single-char-property-change (point) 'invisible))
  5636. (and (eolp) (beginning-of-line 2))))
  5637. (setq eol (point)))
  5638. ;; Find out what to do next and set `this-command'
  5639. (cond
  5640. ((= eos eoh)
  5641. ;; Nothing is hidden behind this heading
  5642. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5643. (message "EMPTY ENTRY")
  5644. (setq org-cycle-subtree-status nil)
  5645. (save-excursion
  5646. (goto-char eos)
  5647. (outline-next-heading)
  5648. (if (outline-invisible-p) (org-flag-heading nil))))
  5649. ((and (or (>= eol eos)
  5650. (not (string-match "\\S-" (buffer-substring eol eos))))
  5651. (or has-children
  5652. (not (setq children-skipped
  5653. org-cycle-skip-children-state-if-no-children))))
  5654. ;; Entire subtree is hidden in one line: children view
  5655. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5656. (if (org-at-item-p)
  5657. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5658. (org-show-entry)
  5659. (org-with-limited-levels (show-children))
  5660. (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5661. (org-cycle-hide-drawers 'subtree))
  5662. ;; Fold every list in subtree to top-level items.
  5663. (when (eq org-cycle-include-plain-lists 'integrate)
  5664. (save-excursion
  5665. (org-back-to-heading)
  5666. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5667. (beginning-of-line 1)
  5668. (let* ((struct (org-list-struct))
  5669. (prevs (org-list-prevs-alist struct))
  5670. (end (org-list-get-bottom-point struct)))
  5671. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5672. (org-list-get-all-items (point) struct prevs))
  5673. (goto-char end))))))
  5674. (message "CHILDREN")
  5675. (save-excursion
  5676. (goto-char eos)
  5677. (outline-next-heading)
  5678. (if (outline-invisible-p) (org-flag-heading nil)))
  5679. (setq org-cycle-subtree-status 'children)
  5680. (run-hook-with-args 'org-cycle-hook 'children))
  5681. ((or children-skipped
  5682. (and (eq last-command this-command)
  5683. (eq org-cycle-subtree-status 'children)))
  5684. ;; We just showed the children, or no children are there,
  5685. ;; now show everything.
  5686. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5687. (outline-flag-region eoh eos nil)
  5688. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5689. (setq org-cycle-subtree-status 'subtree)
  5690. (run-hook-with-args 'org-cycle-hook 'subtree))
  5691. (t
  5692. ;; Default action: hide the subtree.
  5693. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5694. (outline-flag-region eoh eos t)
  5695. (message "FOLDED")
  5696. (setq org-cycle-subtree-status 'folded)
  5697. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5698. ;;;###autoload
  5699. (defun org-global-cycle (&optional arg)
  5700. "Cycle the global visibility. For details see `org-cycle'.
  5701. With \\[universal-argument] prefix arg, switch to startup visibility.
  5702. With a numeric prefix, show all headlines up to that level."
  5703. (interactive "P")
  5704. (let ((org-cycle-include-plain-lists
  5705. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5706. (cond
  5707. ((integerp arg)
  5708. (show-all)
  5709. (hide-sublevels arg)
  5710. (setq org-cycle-global-status 'contents))
  5711. ((equal arg '(4))
  5712. (org-set-startup-visibility)
  5713. (message "Startup visibility, plus VISIBILITY properties."))
  5714. (t
  5715. (org-cycle '(4))))))
  5716. (defun org-set-startup-visibility ()
  5717. "Set the visibility required by startup options and properties."
  5718. (cond
  5719. ((eq org-startup-folded t)
  5720. (org-cycle '(4)))
  5721. ((eq org-startup-folded 'content)
  5722. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5723. (org-cycle '(4)) (org-cycle '(4)))))
  5724. (unless (eq org-startup-folded 'showeverything)
  5725. (if org-hide-block-startup (org-hide-block-all))
  5726. (org-set-visibility-according-to-property 'no-cleanup)
  5727. (org-cycle-hide-archived-subtrees 'all)
  5728. (org-cycle-hide-drawers 'all)
  5729. (org-cycle-show-empty-lines t)))
  5730. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5731. "Switch subtree visibilities according to :VISIBILITY: property."
  5732. (interactive)
  5733. (let (org-show-entry-below state)
  5734. (save-excursion
  5735. (goto-char (point-min))
  5736. (while (re-search-forward
  5737. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5738. nil t)
  5739. (setq state (match-string 1))
  5740. (save-excursion
  5741. (org-back-to-heading t)
  5742. (hide-subtree)
  5743. (org-reveal)
  5744. (cond
  5745. ((equal state '("fold" "folded"))
  5746. (hide-subtree))
  5747. ((equal state "children")
  5748. (org-show-hidden-entry)
  5749. (show-children))
  5750. ((equal state "content")
  5751. (save-excursion
  5752. (save-restriction
  5753. (org-narrow-to-subtree)
  5754. (org-content))))
  5755. ((member state '("all" "showall"))
  5756. (show-subtree)))))
  5757. (unless no-cleanup
  5758. (org-cycle-hide-archived-subtrees 'all)
  5759. (org-cycle-hide-drawers 'all)
  5760. (org-cycle-show-empty-lines 'all)))))
  5761. ;; This function uses outline-regexp instead of the more fundamental
  5762. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5763. ;; buffers, where outline-regexp is needed.
  5764. (defun org-overview ()
  5765. "Switch to overview mode, showing only top-level headlines.
  5766. Really, this shows all headlines with level equal or greater than the level
  5767. of the first headline in the buffer. This is important, because if the
  5768. first headline is not level one, then (hide-sublevels 1) gives confusing
  5769. results."
  5770. (interactive)
  5771. (let ((level (save-excursion
  5772. (goto-char (point-min))
  5773. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5774. (progn
  5775. (goto-char (match-beginning 0))
  5776. (funcall outline-level))))))
  5777. (and level (hide-sublevels level))))
  5778. (defun org-content (&optional arg)
  5779. "Show all headlines in the buffer, like a table of contents.
  5780. With numerical argument N, show content up to level N."
  5781. (interactive "P")
  5782. (save-excursion
  5783. ;; Visit all headings and show their offspring
  5784. (and (integerp arg) (org-overview))
  5785. (goto-char (point-max))
  5786. (catch 'exit
  5787. (while (and (progn (condition-case nil
  5788. (outline-previous-visible-heading 1)
  5789. (error (goto-char (point-min))))
  5790. t)
  5791. (looking-at org-outline-regexp))
  5792. (if (integerp arg)
  5793. (show-children (1- arg))
  5794. (show-branches))
  5795. (if (bobp) (throw 'exit nil))))))
  5796. (defun org-optimize-window-after-visibility-change (state)
  5797. "Adjust the window after a change in outline visibility.
  5798. This function is the default value of the hook `org-cycle-hook'."
  5799. (when (get-buffer-window (current-buffer))
  5800. (cond
  5801. ((eq state 'content) nil)
  5802. ((eq state 'all) nil)
  5803. ((eq state 'folded) nil)
  5804. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5805. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5806. (defun org-remove-empty-overlays-at (pos)
  5807. "Remove outline overlays that do not contain non-white stuff."
  5808. (mapc
  5809. (lambda (o)
  5810. (and (eq 'outline (overlay-get o 'invisible))
  5811. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5812. (overlay-end o))))
  5813. (delete-overlay o)))
  5814. (overlays-at pos)))
  5815. (defun org-clean-visibility-after-subtree-move ()
  5816. "Fix visibility issues after moving a subtree."
  5817. ;; First, find a reasonable region to look at:
  5818. ;; Start two siblings above, end three below
  5819. (let* ((beg (save-excursion
  5820. (and (org-get-last-sibling)
  5821. (org-get-last-sibling))
  5822. (point)))
  5823. (end (save-excursion
  5824. (and (org-get-next-sibling)
  5825. (org-get-next-sibling)
  5826. (org-get-next-sibling))
  5827. (if (org-at-heading-p)
  5828. (point-at-eol)
  5829. (point))))
  5830. (level (looking-at "\\*+"))
  5831. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5832. (save-excursion
  5833. (save-restriction
  5834. (narrow-to-region beg end)
  5835. (when re
  5836. ;; Properly fold already folded siblings
  5837. (goto-char (point-min))
  5838. (while (re-search-forward re nil t)
  5839. (if (and (not (outline-invisible-p))
  5840. (save-excursion
  5841. (goto-char (point-at-eol)) (outline-invisible-p)))
  5842. (hide-entry))))
  5843. (org-cycle-show-empty-lines 'overview)
  5844. (org-cycle-hide-drawers 'overview)))))
  5845. (defun org-cycle-show-empty-lines (state)
  5846. "Show empty lines above all visible headlines.
  5847. The region to be covered depends on STATE when called through
  5848. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5849. entire buffer covered. Note that an empty line is only shown if there
  5850. are at least `org-cycle-separator-lines' empty lines before the headline."
  5851. (when (not (= org-cycle-separator-lines 0))
  5852. (save-excursion
  5853. (let* ((n (abs org-cycle-separator-lines))
  5854. (re (cond
  5855. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5856. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5857. (t (let ((ns (number-to-string (- n 2))))
  5858. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5859. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5860. beg end b e)
  5861. (cond
  5862. ((memq state '(overview contents t))
  5863. (setq beg (point-min) end (point-max)))
  5864. ((memq state '(children folded))
  5865. (setq beg (point) end (progn (org-end-of-subtree t t)
  5866. (beginning-of-line 2)
  5867. (point)))))
  5868. (when beg
  5869. (goto-char beg)
  5870. (while (re-search-forward re end t)
  5871. (unless (get-char-property (match-end 1) 'invisible)
  5872. (setq e (match-end 1))
  5873. (if (< org-cycle-separator-lines 0)
  5874. (setq b (save-excursion
  5875. (goto-char (match-beginning 0))
  5876. (org-back-over-empty-lines)
  5877. (if (save-excursion
  5878. (goto-char (max (point-min) (1- (point))))
  5879. (org-at-heading-p))
  5880. (1- (point))
  5881. (point))))
  5882. (setq b (match-beginning 1)))
  5883. (outline-flag-region b e nil)))))))
  5884. ;; Never hide empty lines at the end of the file.
  5885. (save-excursion
  5886. (goto-char (point-max))
  5887. (outline-previous-heading)
  5888. (outline-end-of-heading)
  5889. (if (and (looking-at "[ \t\n]+")
  5890. (= (match-end 0) (point-max)))
  5891. (outline-flag-region (point) (match-end 0) nil))))
  5892. (defun org-show-empty-lines-in-parent ()
  5893. "Move to the parent and re-show empty lines before visible headlines."
  5894. (save-excursion
  5895. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5896. (org-cycle-show-empty-lines context))))
  5897. (defun org-files-list ()
  5898. "Return `org-agenda-files' list, plus all open org-mode files.
  5899. This is useful for operations that need to scan all of a user's
  5900. open and agenda-wise Org files."
  5901. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5902. (dolist (buf (buffer-list))
  5903. (with-current-buffer buf
  5904. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5905. (let ((file (expand-file-name (buffer-file-name))))
  5906. (unless (member file files)
  5907. (push file files))))))
  5908. files))
  5909. (defsubst org-entry-beginning-position ()
  5910. "Return the beginning position of the current entry."
  5911. (save-excursion (outline-back-to-heading t) (point)))
  5912. (defsubst org-entry-end-position ()
  5913. "Return the end position of the current entry."
  5914. (save-excursion (outline-next-heading) (point)))
  5915. (defun org-cycle-hide-drawers (state)
  5916. "Re-hide all drawers after a visibility state change."
  5917. (when (and (eq major-mode 'org-mode)
  5918. (not (memq state '(overview folded contents))))
  5919. (save-excursion
  5920. (let* ((globalp (memq state '(contents all)))
  5921. (beg (if globalp (point-min) (point)))
  5922. (end (if globalp (point-max)
  5923. (if (eq state 'children)
  5924. (save-excursion (outline-next-heading) (point))
  5925. (org-end-of-subtree t)))))
  5926. (goto-char beg)
  5927. (while (re-search-forward org-drawer-regexp end t)
  5928. (org-flag-drawer t))))))
  5929. (defun org-flag-drawer (flag)
  5930. (save-excursion
  5931. (beginning-of-line 1)
  5932. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5933. (let ((b (match-end 0)))
  5934. (if (re-search-forward
  5935. "^[ \t]*:END:"
  5936. (save-excursion (outline-next-heading) (point)) t)
  5937. (outline-flag-region b (point-at-eol) flag)
  5938. (error ":END: line missing at position %s" b))))))
  5939. (defun org-subtree-end-visible-p ()
  5940. "Is the end of the current subtree visible?"
  5941. (pos-visible-in-window-p
  5942. (save-excursion (org-end-of-subtree t) (point))))
  5943. (defun org-first-headline-recenter (&optional N)
  5944. "Move cursor to the first headline and recenter the headline.
  5945. Optional argument N means put the headline into the Nth line of the window."
  5946. (goto-char (point-min))
  5947. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5948. (beginning-of-line)
  5949. (recenter (prefix-numeric-value N))))
  5950. ;;; Saving and restoring visibility
  5951. (defun org-outline-overlay-data (&optional use-markers)
  5952. "Return a list of the locations of all outline overlays.
  5953. These are overlays with the `invisible' property value `outline'.
  5954. The return value is a list of cons cells, with start and stop
  5955. positions for each overlay.
  5956. If USE-MARKERS is set, return the positions as markers."
  5957. (let (beg end)
  5958. (save-excursion
  5959. (save-restriction
  5960. (widen)
  5961. (delq nil
  5962. (mapcar (lambda (o)
  5963. (when (eq (overlay-get o 'invisible) 'outline)
  5964. (setq beg (overlay-start o)
  5965. end (overlay-end o))
  5966. (and beg end (> end beg)
  5967. (if use-markers
  5968. (cons (move-marker (make-marker) beg)
  5969. (move-marker (make-marker) end))
  5970. (cons beg end)))))
  5971. (overlays-in (point-min) (point-max))))))))
  5972. (defun org-set-outline-overlay-data (data)
  5973. "Create visibility overlays for all positions in DATA.
  5974. DATA should have been made by `org-outline-overlay-data'."
  5975. (let (o)
  5976. (save-excursion
  5977. (save-restriction
  5978. (widen)
  5979. (show-all)
  5980. (mapc (lambda (c)
  5981. (outline-flag-region (car c) (cdr c) t))
  5982. data)))))
  5983. ;;; Folding of blocks
  5984. (defvar org-hide-block-overlays nil
  5985. "Overlays hiding blocks.")
  5986. (make-variable-buffer-local 'org-hide-block-overlays)
  5987. (defun org-block-map (function &optional start end)
  5988. "Call FUNCTION at the head of all source blocks in the current buffer.
  5989. Optional arguments START and END can be used to limit the range."
  5990. (let ((start (or start (point-min)))
  5991. (end (or end (point-max))))
  5992. (save-excursion
  5993. (goto-char start)
  5994. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5995. (save-excursion
  5996. (save-match-data
  5997. (goto-char (match-beginning 0))
  5998. (funcall function)))))))
  5999. (defun org-hide-block-toggle-all ()
  6000. "Toggle the visibility of all blocks in the current buffer."
  6001. (org-block-map #'org-hide-block-toggle))
  6002. (defun org-hide-block-all ()
  6003. "Fold all blocks in the current buffer."
  6004. (interactive)
  6005. (org-show-block-all)
  6006. (org-block-map #'org-hide-block-toggle-maybe))
  6007. (defun org-show-block-all ()
  6008. "Unfold all blocks in the current buffer."
  6009. (interactive)
  6010. (mapc 'delete-overlay org-hide-block-overlays)
  6011. (setq org-hide-block-overlays nil))
  6012. (defun org-hide-block-toggle-maybe ()
  6013. "Toggle visibility of block at point."
  6014. (interactive)
  6015. (let ((case-fold-search t))
  6016. (if (save-excursion
  6017. (beginning-of-line 1)
  6018. (looking-at org-block-regexp))
  6019. (progn (org-hide-block-toggle)
  6020. t) ;; to signal that we took action
  6021. nil))) ;; to signal that we did not
  6022. (defun org-hide-block-toggle (&optional force)
  6023. "Toggle the visibility of the current block."
  6024. (interactive)
  6025. (save-excursion
  6026. (beginning-of-line)
  6027. (if (re-search-forward org-block-regexp nil t)
  6028. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6029. (end (match-end 0)) ;; end of entire body
  6030. ov)
  6031. (if (memq t (mapcar (lambda (overlay)
  6032. (eq (overlay-get overlay 'invisible)
  6033. 'org-hide-block))
  6034. (overlays-at start)))
  6035. (if (or (not force) (eq force 'off))
  6036. (mapc (lambda (ov)
  6037. (when (member ov org-hide-block-overlays)
  6038. (setq org-hide-block-overlays
  6039. (delq ov org-hide-block-overlays)))
  6040. (when (eq (overlay-get ov 'invisible)
  6041. 'org-hide-block)
  6042. (delete-overlay ov)))
  6043. (overlays-at start)))
  6044. (setq ov (make-overlay start end))
  6045. (overlay-put ov 'invisible 'org-hide-block)
  6046. ;; make the block accessible to isearch
  6047. (overlay-put
  6048. ov 'isearch-open-invisible
  6049. (lambda (ov)
  6050. (when (member ov org-hide-block-overlays)
  6051. (setq org-hide-block-overlays
  6052. (delq ov org-hide-block-overlays)))
  6053. (when (eq (overlay-get ov 'invisible)
  6054. 'org-hide-block)
  6055. (delete-overlay ov))))
  6056. (push ov org-hide-block-overlays)))
  6057. (error "Not looking at a source block"))))
  6058. ;; org-tab-after-check-for-cycling-hook
  6059. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6060. ;; Remove overlays when changing major mode
  6061. (add-hook 'org-mode-hook
  6062. (lambda () (org-add-hook 'change-major-mode-hook
  6063. 'org-show-block-all 'append 'local)))
  6064. ;;; Org-goto
  6065. (defvar org-goto-window-configuration nil)
  6066. (defvar org-goto-marker nil)
  6067. (defvar org-goto-map
  6068. (let ((map (make-sparse-keymap)))
  6069. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6070. (while (setq cmd (pop cmds))
  6071. (substitute-key-definition cmd cmd map global-map)))
  6072. (suppress-keymap map)
  6073. (org-defkey map "\C-m" 'org-goto-ret)
  6074. (org-defkey map [(return)] 'org-goto-ret)
  6075. (org-defkey map [(left)] 'org-goto-left)
  6076. (org-defkey map [(right)] 'org-goto-right)
  6077. (org-defkey map [(control ?g)] 'org-goto-quit)
  6078. (org-defkey map "\C-i" 'org-cycle)
  6079. (org-defkey map [(tab)] 'org-cycle)
  6080. (org-defkey map [(down)] 'outline-next-visible-heading)
  6081. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6082. (if org-goto-auto-isearch
  6083. (if (fboundp 'define-key-after)
  6084. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6085. nil)
  6086. (org-defkey map "q" 'org-goto-quit)
  6087. (org-defkey map "n" 'outline-next-visible-heading)
  6088. (org-defkey map "p" 'outline-previous-visible-heading)
  6089. (org-defkey map "f" 'outline-forward-same-level)
  6090. (org-defkey map "b" 'outline-backward-same-level)
  6091. (org-defkey map "u" 'outline-up-heading))
  6092. (org-defkey map "/" 'org-occur)
  6093. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6094. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6095. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6096. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6097. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6098. map))
  6099. (defconst org-goto-help
  6100. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6101. RET=jump to location [Q]uit and return to previous location
  6102. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6103. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6104. ;; FIXME: Docstring does not mention both interfaces
  6105. (defun org-goto (&optional alternative-interface)
  6106. "Look up a different location in the current file, keeping current visibility.
  6107. When you want look-up or go to a different location in a document, the
  6108. fastest way is often to fold the entire buffer and then dive into the tree.
  6109. This method has the disadvantage, that the previous location will be folded,
  6110. which may not be what you want.
  6111. This command works around this by showing a copy of the current buffer
  6112. in an indirect buffer, in overview mode. You can dive into the tree in
  6113. that copy, use org-occur and incremental search to find a location.
  6114. When pressing RET or `Q', the command returns to the original buffer in
  6115. which the visibility is still unchanged. After RET it will also jump to
  6116. the location selected in the indirect buffer and expose the headline
  6117. hierarchy above."
  6118. (interactive "P")
  6119. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6120. (org-refile-use-outline-path t)
  6121. (org-refile-target-verify-function nil)
  6122. (interface
  6123. (if (not alternative-interface)
  6124. org-goto-interface
  6125. (if (eq org-goto-interface 'outline)
  6126. 'outline-path-completion
  6127. 'outline)))
  6128. (org-goto-start-pos (point))
  6129. (selected-point
  6130. (if (eq interface 'outline)
  6131. (car (org-get-location (current-buffer) org-goto-help))
  6132. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6133. (org-refile-check-position pa)
  6134. (nth 3 pa)))))
  6135. (if selected-point
  6136. (progn
  6137. (org-mark-ring-push org-goto-start-pos)
  6138. (goto-char selected-point)
  6139. (if (or (outline-invisible-p) (org-invisible-p2))
  6140. (org-show-context 'org-goto)))
  6141. (message "Quit"))))
  6142. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6143. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6144. (defvar org-goto-local-auto-isearch-map) ; defined below
  6145. (defun org-get-location (buf help)
  6146. "Let the user select a location in the Org-mode buffer BUF.
  6147. This function uses a recursive edit. It returns the selected position
  6148. or nil."
  6149. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6150. (isearch-hide-immediately nil)
  6151. (isearch-search-fun-function
  6152. (lambda () 'org-goto-local-search-headings))
  6153. (org-goto-selected-point org-goto-exit-command)
  6154. (pop-up-frames nil)
  6155. (special-display-buffer-names nil)
  6156. (special-display-regexps nil)
  6157. (special-display-function nil))
  6158. (save-excursion
  6159. (save-window-excursion
  6160. (delete-other-windows)
  6161. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6162. (org-pop-to-buffer-same-window
  6163. (condition-case nil
  6164. (make-indirect-buffer (current-buffer) "*org-goto*")
  6165. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6166. (with-output-to-temp-buffer "*Help*"
  6167. (princ help))
  6168. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6169. (setq buffer-read-only nil)
  6170. (let ((org-startup-truncated t)
  6171. (org-startup-folded nil)
  6172. (org-startup-align-all-tables nil))
  6173. (org-mode)
  6174. (org-overview))
  6175. (setq buffer-read-only t)
  6176. (if (and (boundp 'org-goto-start-pos)
  6177. (integer-or-marker-p org-goto-start-pos))
  6178. (let ((org-show-hierarchy-above t)
  6179. (org-show-siblings t)
  6180. (org-show-following-heading t))
  6181. (goto-char org-goto-start-pos)
  6182. (and (outline-invisible-p) (org-show-context)))
  6183. (goto-char (point-min)))
  6184. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6185. (message "Select location and press RET")
  6186. (use-local-map org-goto-map)
  6187. (recursive-edit)
  6188. ))
  6189. (kill-buffer "*org-goto*")
  6190. (cons org-goto-selected-point org-goto-exit-command)))
  6191. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6192. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6193. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6194. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6195. (defun org-goto-local-search-headings (string bound noerror)
  6196. "Search and make sure that any matches are in headlines."
  6197. (catch 'return
  6198. (while (if isearch-forward
  6199. (search-forward string bound noerror)
  6200. (search-backward string bound noerror))
  6201. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6202. (and (member :headline context)
  6203. (not (member :tags context))))
  6204. (throw 'return (point))))))
  6205. (defun org-goto-local-auto-isearch ()
  6206. "Start isearch."
  6207. (interactive)
  6208. (goto-char (point-min))
  6209. (let ((keys (this-command-keys)))
  6210. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6211. (isearch-mode t)
  6212. (isearch-process-search-char (string-to-char keys)))))
  6213. (defun org-goto-ret (&optional arg)
  6214. "Finish `org-goto' by going to the new location."
  6215. (interactive "P")
  6216. (setq org-goto-selected-point (point)
  6217. org-goto-exit-command 'return)
  6218. (throw 'exit nil))
  6219. (defun org-goto-left ()
  6220. "Finish `org-goto' by going to the new location."
  6221. (interactive)
  6222. (if (org-at-heading-p)
  6223. (progn
  6224. (beginning-of-line 1)
  6225. (setq org-goto-selected-point (point)
  6226. org-goto-exit-command 'left)
  6227. (throw 'exit nil))
  6228. (error "Not on a heading")))
  6229. (defun org-goto-right ()
  6230. "Finish `org-goto' by going to the new location."
  6231. (interactive)
  6232. (if (org-at-heading-p)
  6233. (progn
  6234. (setq org-goto-selected-point (point)
  6235. org-goto-exit-command 'right)
  6236. (throw 'exit nil))
  6237. (error "Not on a heading")))
  6238. (defun org-goto-quit ()
  6239. "Finish `org-goto' without cursor motion."
  6240. (interactive)
  6241. (setq org-goto-selected-point nil)
  6242. (setq org-goto-exit-command 'quit)
  6243. (throw 'exit nil))
  6244. ;;; Indirect buffer display of subtrees
  6245. (defvar org-indirect-dedicated-frame nil
  6246. "This is the frame being used for indirect tree display.")
  6247. (defvar org-last-indirect-buffer nil)
  6248. (defun org-tree-to-indirect-buffer (&optional arg)
  6249. "Create indirect buffer and narrow it to current subtree.
  6250. With numerical prefix ARG, go up to this level and then take that tree.
  6251. If ARG is negative, go up that many levels.
  6252. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6253. indirect buffer previously made with this command, to avoid proliferation of
  6254. indirect buffers. However, when you call the command with a \
  6255. \\[universal-argument] prefix, or
  6256. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6257. is kept so that you can work with several indirect buffers at the same time.
  6258. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6259. \\[universal-argument] prefix also
  6260. requests that a new frame be made for the new buffer, so that the dedicated
  6261. frame is not changed."
  6262. (interactive "P")
  6263. (let ((cbuf (current-buffer))
  6264. (cwin (selected-window))
  6265. (pos (point))
  6266. beg end level heading ibuf)
  6267. (save-excursion
  6268. (org-back-to-heading t)
  6269. (when (numberp arg)
  6270. (setq level (org-outline-level))
  6271. (if (< arg 0) (setq arg (+ level arg)))
  6272. (while (> (setq level (org-outline-level)) arg)
  6273. (outline-up-heading 1 t)))
  6274. (setq beg (point)
  6275. heading (org-get-heading))
  6276. (org-end-of-subtree t t)
  6277. (if (org-at-heading-p) (backward-char 1))
  6278. (setq end (point)))
  6279. (if (and (buffer-live-p org-last-indirect-buffer)
  6280. (not (eq org-indirect-buffer-display 'new-frame))
  6281. (not arg))
  6282. (kill-buffer org-last-indirect-buffer))
  6283. (setq ibuf (org-get-indirect-buffer cbuf)
  6284. org-last-indirect-buffer ibuf)
  6285. (cond
  6286. ((or (eq org-indirect-buffer-display 'new-frame)
  6287. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6288. (select-frame (make-frame))
  6289. (delete-other-windows)
  6290. (org-pop-to-buffer-same-window ibuf)
  6291. (org-set-frame-title heading))
  6292. ((eq org-indirect-buffer-display 'dedicated-frame)
  6293. (raise-frame
  6294. (select-frame (or (and org-indirect-dedicated-frame
  6295. (frame-live-p org-indirect-dedicated-frame)
  6296. org-indirect-dedicated-frame)
  6297. (setq org-indirect-dedicated-frame (make-frame)))))
  6298. (delete-other-windows)
  6299. (org-pop-to-buffer-same-window ibuf)
  6300. (org-set-frame-title (concat "Indirect: " heading)))
  6301. ((eq org-indirect-buffer-display 'current-window)
  6302. (org-pop-to-buffer-same-window ibuf))
  6303. ((eq org-indirect-buffer-display 'other-window)
  6304. (pop-to-buffer ibuf))
  6305. (t (error "Invalid value")))
  6306. (if (featurep 'xemacs)
  6307. (save-excursion (org-mode) (turn-on-font-lock)))
  6308. (narrow-to-region beg end)
  6309. (show-all)
  6310. (goto-char pos)
  6311. (run-hook-with-args 'org-cycle-hook 'all)
  6312. (and (window-live-p cwin) (select-window cwin))))
  6313. (defun org-get-indirect-buffer (&optional buffer)
  6314. (setq buffer (or buffer (current-buffer)))
  6315. (let ((n 1) (base (buffer-name buffer)) bname)
  6316. (while (buffer-live-p
  6317. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6318. (setq n (1+ n)))
  6319. (condition-case nil
  6320. (make-indirect-buffer buffer bname 'clone)
  6321. (error (make-indirect-buffer buffer bname)))))
  6322. (defun org-set-frame-title (title)
  6323. "Set the title of the current frame to the string TITLE."
  6324. ;; FIXME: how to name a single frame in XEmacs???
  6325. (unless (featurep 'xemacs)
  6326. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6327. ;;;; Structure editing
  6328. ;;; Inserting headlines
  6329. (defun org-previous-line-empty-p ()
  6330. (save-excursion
  6331. (and (not (bobp))
  6332. (or (beginning-of-line 0) t)
  6333. (save-match-data
  6334. (looking-at "[ \t]*$")))))
  6335. (defun org-insert-heading (&optional force-heading invisible-ok)
  6336. "Insert a new heading or item with same depth at point.
  6337. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6338. If point is at the beginning of a headline, insert a sibling before the
  6339. current headline. If point is not at the beginning, split the line,
  6340. create the new headline with the text in the current line after point
  6341. \(but see also the variable `org-M-RET-may-split-line').
  6342. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6343. This is important for non-interactive uses of the command."
  6344. (interactive "P")
  6345. (if (or (= (buffer-size) 0)
  6346. (and (not (save-excursion
  6347. (and (ignore-errors (org-back-to-heading invisible-ok))
  6348. (org-at-heading-p))))
  6349. (or force-heading (not (org-in-item-p)))))
  6350. (progn
  6351. (insert "\n* ")
  6352. (run-hooks 'org-insert-heading-hook))
  6353. (when (or force-heading (not (org-insert-item)))
  6354. (let* ((empty-line-p nil)
  6355. (level nil)
  6356. (on-heading (org-at-heading-p))
  6357. (head (save-excursion
  6358. (condition-case nil
  6359. (progn
  6360. (org-back-to-heading invisible-ok)
  6361. (when (and (not on-heading)
  6362. (featurep 'org-inlinetask)
  6363. (integerp org-inlinetask-min-level)
  6364. (>= (length (match-string 0))
  6365. org-inlinetask-min-level))
  6366. ;; Find a heading level before the inline task
  6367. (while (and (setq level (org-up-heading-safe))
  6368. (>= level org-inlinetask-min-level)))
  6369. (if (org-at-heading-p)
  6370. (org-back-to-heading invisible-ok)
  6371. (error "This should not happen")))
  6372. (setq empty-line-p (org-previous-line-empty-p))
  6373. (match-string 0))
  6374. (error "*"))))
  6375. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6376. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6377. pos hide-previous previous-pos)
  6378. (cond
  6379. ((and (org-at-heading-p) (bolp)
  6380. (or (bobp)
  6381. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6382. ;; insert before the current line
  6383. (open-line (if blank 2 1)))
  6384. ((and (bolp)
  6385. (not org-insert-heading-respect-content)
  6386. (or (bobp)
  6387. (save-excursion
  6388. (backward-char 1) (not (outline-invisible-p)))))
  6389. ;; insert right here
  6390. nil)
  6391. (t
  6392. ;; somewhere in the line
  6393. (save-excursion
  6394. (setq previous-pos (point-at-bol))
  6395. (end-of-line)
  6396. (setq hide-previous (outline-invisible-p)))
  6397. (and org-insert-heading-respect-content (org-show-subtree))
  6398. (let ((split
  6399. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6400. (save-excursion
  6401. (let ((p (point)))
  6402. (goto-char (point-at-bol))
  6403. (and (looking-at org-complex-heading-regexp)
  6404. (match-beginning 4)
  6405. (> p (match-beginning 4)))))))
  6406. tags pos)
  6407. (cond
  6408. (org-insert-heading-respect-content
  6409. (org-end-of-subtree nil t)
  6410. (when (featurep 'org-inlinetask)
  6411. (while (and (not (eobp))
  6412. (looking-at "\\(\\*+\\)[ \t]+")
  6413. (>= (length (match-string 1))
  6414. org-inlinetask-min-level))
  6415. (org-end-of-subtree nil t)))
  6416. (or (bolp) (newline))
  6417. (or (org-previous-line-empty-p)
  6418. (and blank (newline)))
  6419. (open-line 1))
  6420. ((org-at-heading-p)
  6421. (when hide-previous
  6422. (show-children)
  6423. (org-show-entry))
  6424. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6425. (setq tags (and (match-end 2) (match-string 2)))
  6426. (and (match-end 1)
  6427. (delete-region (match-beginning 1) (match-end 1)))
  6428. (setq pos (point-at-bol))
  6429. (or split (end-of-line 1))
  6430. (delete-horizontal-space)
  6431. (if (string-match "\\`\\*+\\'"
  6432. (buffer-substring (point-at-bol) (point)))
  6433. (insert " "))
  6434. (newline (if blank 2 1))
  6435. (when tags
  6436. (save-excursion
  6437. (goto-char pos)
  6438. (end-of-line 1)
  6439. (insert " " tags)
  6440. (org-set-tags nil 'align))))
  6441. (t
  6442. (or split (end-of-line 1))
  6443. (newline (if blank 2 1)))))))
  6444. (insert head) (just-one-space)
  6445. (setq pos (point))
  6446. (end-of-line 1)
  6447. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6448. (when (and org-insert-heading-respect-content hide-previous)
  6449. (save-excursion
  6450. (goto-char previous-pos)
  6451. (hide-subtree)))
  6452. (run-hooks 'org-insert-heading-hook)))))
  6453. (defun org-get-heading (&optional no-tags no-todo)
  6454. "Return the heading of the current entry, without the stars.
  6455. When NO-TAGS is non-nil, don't include tags.
  6456. When NO-TODO is non-nil, don't include TODO keywords."
  6457. (save-excursion
  6458. (org-back-to-heading t)
  6459. (cond
  6460. ((and no-tags no-todo)
  6461. (looking-at org-complex-heading-regexp)
  6462. (match-string 4))
  6463. (no-tags
  6464. (looking-at (concat org-outline-regexp
  6465. "\\(.*?\\)"
  6466. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6467. (match-string 1))
  6468. (no-todo
  6469. (looking-at org-todo-line-regexp)
  6470. (match-string 3))
  6471. (t (looking-at org-heading-regexp)
  6472. (match-string 2)))))
  6473. (defun org-heading-components ()
  6474. "Return the components of the current heading.
  6475. This is a list with the following elements:
  6476. - the level as an integer
  6477. - the reduced level, different if `org-odd-levels-only' is set.
  6478. - the TODO keyword, or nil
  6479. - the priority character, like ?A, or nil if no priority is given
  6480. - the headline text itself, or the tags string if no headline text
  6481. - the tags string, or nil."
  6482. (save-excursion
  6483. (org-back-to-heading t)
  6484. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6485. (list (length (match-string 1))
  6486. (org-reduced-level (length (match-string 1)))
  6487. (org-match-string-no-properties 2)
  6488. (and (match-end 3) (aref (match-string 3) 2))
  6489. (org-match-string-no-properties 4)
  6490. (org-match-string-no-properties 5)))))
  6491. (defun org-get-entry ()
  6492. "Get the entry text, after heading, entire subtree."
  6493. (save-excursion
  6494. (org-back-to-heading t)
  6495. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6496. (defun org-insert-heading-after-current ()
  6497. "Insert a new heading with same level as current, after current subtree."
  6498. (interactive)
  6499. (org-back-to-heading)
  6500. (org-insert-heading)
  6501. (org-move-subtree-down)
  6502. (end-of-line 1))
  6503. (defun org-insert-heading-respect-content ()
  6504. (interactive)
  6505. (let ((org-insert-heading-respect-content t))
  6506. (org-insert-heading t)))
  6507. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6508. (interactive "P")
  6509. (let ((org-insert-heading-respect-content t))
  6510. (org-insert-todo-heading force-state t)))
  6511. (defun org-insert-todo-heading (arg &optional force-heading)
  6512. "Insert a new heading with the same level and TODO state as current heading.
  6513. If the heading has no TODO state, or if the state is DONE, use the first
  6514. state (TODO by default). Also with prefix arg, force first state."
  6515. (interactive "P")
  6516. (when (or force-heading (not (org-insert-item 'checkbox)))
  6517. (org-insert-heading force-heading)
  6518. (save-excursion
  6519. (org-back-to-heading)
  6520. (outline-previous-heading)
  6521. (looking-at org-todo-line-regexp))
  6522. (let*
  6523. ((new-mark-x
  6524. (if (or arg
  6525. (not (match-beginning 2))
  6526. (member (match-string 2) org-done-keywords))
  6527. (car org-todo-keywords-1)
  6528. (match-string 2)))
  6529. (new-mark
  6530. (or
  6531. (run-hook-with-args-until-success
  6532. 'org-todo-get-default-hook new-mark-x nil)
  6533. new-mark-x)))
  6534. (beginning-of-line 1)
  6535. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6536. (if org-treat-insert-todo-heading-as-state-change
  6537. (org-todo new-mark)
  6538. (insert new-mark " "))))
  6539. (when org-provide-todo-statistics
  6540. (org-update-parent-todo-statistics))))
  6541. (defun org-insert-subheading (arg)
  6542. "Insert a new subheading and demote it.
  6543. Works for outline headings and for plain lists alike."
  6544. (interactive "P")
  6545. (org-insert-heading arg)
  6546. (cond
  6547. ((org-at-heading-p) (org-do-demote))
  6548. ((org-at-item-p) (org-indent-item))))
  6549. (defun org-insert-todo-subheading (arg)
  6550. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6551. Works for outline headings and for plain lists alike."
  6552. (interactive "P")
  6553. (org-insert-todo-heading arg)
  6554. (cond
  6555. ((org-at-heading-p) (org-do-demote))
  6556. ((org-at-item-p) (org-indent-item))))
  6557. ;;; Promotion and Demotion
  6558. (defvar org-after-demote-entry-hook nil
  6559. "Hook run after an entry has been demoted.
  6560. The cursor will be at the beginning of the entry.
  6561. When a subtree is being demoted, the hook will be called for each node.")
  6562. (defvar org-after-promote-entry-hook nil
  6563. "Hook run after an entry has been promoted.
  6564. The cursor will be at the beginning of the entry.
  6565. When a subtree is being promoted, the hook will be called for each node.")
  6566. (defun org-promote-subtree ()
  6567. "Promote the entire subtree.
  6568. See also `org-promote'."
  6569. (interactive)
  6570. (save-excursion
  6571. (org-with-limited-levels (org-map-tree 'org-promote)))
  6572. (org-fix-position-after-promote))
  6573. (defun org-demote-subtree ()
  6574. "Demote the entire subtree. See `org-demote'.
  6575. See also `org-promote'."
  6576. (interactive)
  6577. (save-excursion
  6578. (org-with-limited-levels (org-map-tree 'org-demote)))
  6579. (org-fix-position-after-promote))
  6580. (defun org-do-promote ()
  6581. "Promote the current heading higher up the tree.
  6582. If the region is active in `transient-mark-mode', promote all headings
  6583. in the region."
  6584. (interactive)
  6585. (save-excursion
  6586. (if (org-region-active-p)
  6587. (org-map-region 'org-promote (region-beginning) (region-end))
  6588. (org-promote)))
  6589. (org-fix-position-after-promote))
  6590. (defun org-do-demote ()
  6591. "Demote the current heading lower down the tree.
  6592. If the region is active in `transient-mark-mode', demote all headings
  6593. in the region."
  6594. (interactive)
  6595. (save-excursion
  6596. (if (org-region-active-p)
  6597. (org-map-region 'org-demote (region-beginning) (region-end))
  6598. (org-demote)))
  6599. (org-fix-position-after-promote))
  6600. (defun org-fix-position-after-promote ()
  6601. "Make sure that after pro/demotion cursor position is right."
  6602. (let ((pos (point)))
  6603. (when (save-excursion
  6604. (beginning-of-line 1)
  6605. (looking-at org-todo-line-regexp)
  6606. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6607. (cond ((eobp) (insert " "))
  6608. ((eolp) (insert " "))
  6609. ((equal (char-after) ?\ ) (forward-char 1))))))
  6610. (defun org-current-level ()
  6611. "Return the level of the current entry, or nil if before the first headline.
  6612. The level is the number of stars at the beginning of the headline."
  6613. (save-excursion
  6614. (org-with-limited-levels
  6615. (if (ignore-errors (org-back-to-heading t))
  6616. (funcall outline-level)))))
  6617. (defun org-get-previous-line-level ()
  6618. "Return the outline depth of the last headline before the current line.
  6619. Returns 0 for the first headline in the buffer, and nil if before the
  6620. first headline."
  6621. (let ((current-level (org-current-level))
  6622. (prev-level (when (> (line-number-at-pos) 1)
  6623. (save-excursion
  6624. (beginning-of-line 0)
  6625. (org-current-level)))))
  6626. (cond ((null current-level) nil) ; Before first headline
  6627. ((null prev-level) 0) ; At first headline
  6628. (prev-level))))
  6629. (defun org-reduced-level (l)
  6630. "Compute the effective level of a heading.
  6631. This takes into account the setting of `org-odd-levels-only'."
  6632. (cond
  6633. ((zerop l) 0)
  6634. (org-odd-levels-only (1+ (floor (/ l 2))))
  6635. (t l)))
  6636. (defun org-level-increment ()
  6637. "Return the number of stars that will be added or removed at a
  6638. time to headlines when structure editing, based on the value of
  6639. `org-odd-levels-only'."
  6640. (if org-odd-levels-only 2 1))
  6641. (defun org-get-valid-level (level &optional change)
  6642. "Rectify a level change under the influence of `org-odd-levels-only'
  6643. LEVEL is a current level, CHANGE is by how much the level should be
  6644. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6645. even level numbers will become the next higher odd number."
  6646. (if org-odd-levels-only
  6647. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6648. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6649. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6650. (max 1 (+ level (or change 0)))))
  6651. (if (boundp 'define-obsolete-function-alias)
  6652. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6653. (define-obsolete-function-alias 'org-get-legal-level
  6654. 'org-get-valid-level)
  6655. (define-obsolete-function-alias 'org-get-legal-level
  6656. 'org-get-valid-level "23.1")))
  6657. (defun org-promote ()
  6658. "Promote the current heading higher up the tree.
  6659. If the region is active in `transient-mark-mode', promote all headings
  6660. in the region."
  6661. (org-back-to-heading t)
  6662. (let* ((level (save-match-data (funcall outline-level)))
  6663. (after-change-functions (remove 'flyspell-after-change-function
  6664. after-change-functions))
  6665. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6666. (diff (abs (- level (length up-head) -1))))
  6667. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6668. (replace-match up-head nil t)
  6669. ;; Fixup tag positioning
  6670. (and org-auto-align-tags (org-set-tags nil t))
  6671. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6672. (run-hooks 'org-after-promote-entry-hook)))
  6673. (defun org-demote ()
  6674. "Demote the current heading lower down the tree.
  6675. If the region is active in `transient-mark-mode', demote all headings
  6676. in the region."
  6677. (org-back-to-heading t)
  6678. (let* ((level (save-match-data (funcall outline-level)))
  6679. (after-change-functions (remove 'flyspell-after-change-function
  6680. after-change-functions))
  6681. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6682. (diff (abs (- level (length down-head) -1))))
  6683. (replace-match down-head nil t)
  6684. ;; Fixup tag positioning
  6685. (and org-auto-align-tags (org-set-tags nil t))
  6686. (if org-adapt-indentation (org-fixup-indentation diff))
  6687. (run-hooks 'org-after-demote-entry-hook)))
  6688. (defun org-cycle-level ()
  6689. "Cycle the level of an empty headline through possible states.
  6690. This goes first to child, then to parent, level, then up the hierarchy.
  6691. After top level, it switches back to sibling level."
  6692. (interactive)
  6693. (let ((org-adapt-indentation nil))
  6694. (when (org-point-at-end-of-empty-headline)
  6695. (setq this-command 'org-cycle-level) ; Only needed for caching
  6696. (let ((cur-level (org-current-level))
  6697. (prev-level (org-get-previous-line-level)))
  6698. (cond
  6699. ;; If first headline in file, promote to top-level.
  6700. ((= prev-level 0)
  6701. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6702. do (org-do-promote)))
  6703. ;; If same level as prev, demote one.
  6704. ((= prev-level cur-level)
  6705. (org-do-demote))
  6706. ;; If parent is top-level, promote to top level if not already.
  6707. ((= prev-level 1)
  6708. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6709. do (org-do-promote)))
  6710. ;; If top-level, return to prev-level.
  6711. ((= cur-level 1)
  6712. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6713. do (org-do-demote)))
  6714. ;; If less than prev-level, promote one.
  6715. ((< cur-level prev-level)
  6716. (org-do-promote))
  6717. ;; If deeper than prev-level, promote until higher than
  6718. ;; prev-level.
  6719. ((> cur-level prev-level)
  6720. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6721. do (org-do-promote))))
  6722. t))))
  6723. (defun org-map-tree (fun)
  6724. "Call FUN for every heading underneath the current one."
  6725. (org-back-to-heading)
  6726. (let ((level (funcall outline-level)))
  6727. (save-excursion
  6728. (funcall fun)
  6729. (while (and (progn
  6730. (outline-next-heading)
  6731. (> (funcall outline-level) level))
  6732. (not (eobp)))
  6733. (funcall fun)))))
  6734. (defun org-map-region (fun beg end)
  6735. "Call FUN for every heading between BEG and END."
  6736. (let ((org-ignore-region t))
  6737. (save-excursion
  6738. (setq end (copy-marker end))
  6739. (goto-char beg)
  6740. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6741. (< (point) end))
  6742. (funcall fun))
  6743. (while (and (progn
  6744. (outline-next-heading)
  6745. (< (point) end))
  6746. (not (eobp)))
  6747. (funcall fun)))))
  6748. (defvar org-property-end-re) ; silence byte-compiler
  6749. (defun org-fixup-indentation (diff)
  6750. "Change the indentation in the current entry by DIFF.
  6751. However, if any line in the current entry has no indentation, or if it
  6752. would end up with no indentation after the change, nothing at all is done."
  6753. (save-excursion
  6754. (let ((end (save-excursion (outline-next-heading)
  6755. (point-marker)))
  6756. (prohibit (if (> diff 0)
  6757. "^\\S-"
  6758. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6759. col)
  6760. (unless (save-excursion (end-of-line 1)
  6761. (re-search-forward prohibit end t))
  6762. (while (and (< (point) end)
  6763. (re-search-forward "^[ \t]+" end t))
  6764. (goto-char (match-end 0))
  6765. (setq col (current-column))
  6766. (if (< diff 0) (replace-match ""))
  6767. (org-indent-to-column (+ diff col))))
  6768. (move-marker end nil))))
  6769. (defun org-convert-to-odd-levels ()
  6770. "Convert an org-mode file with all levels allowed to one with odd levels.
  6771. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6772. level 5 etc."
  6773. (interactive)
  6774. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6775. (let ((outline-level 'org-outline-level)
  6776. (org-odd-levels-only nil) n)
  6777. (save-excursion
  6778. (goto-char (point-min))
  6779. (while (re-search-forward "^\\*\\*+ " nil t)
  6780. (setq n (- (length (match-string 0)) 2))
  6781. (while (>= (setq n (1- n)) 0)
  6782. (org-demote))
  6783. (end-of-line 1))))))
  6784. (defun org-convert-to-oddeven-levels ()
  6785. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6786. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6787. file contains a section with an even level, conversion would
  6788. destroy the structure of the file. An error is signaled in this
  6789. case."
  6790. (interactive)
  6791. (goto-char (point-min))
  6792. ;; First check if there are no even levels
  6793. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6794. (org-show-context t)
  6795. (error "Not all levels are odd in this file. Conversion not possible"))
  6796. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6797. (let ((outline-regexp org-outline-regexp)
  6798. (outline-level 'org-outline-level)
  6799. (org-odd-levels-only nil) n)
  6800. (save-excursion
  6801. (goto-char (point-min))
  6802. (while (re-search-forward "^\\*\\*+ " nil t)
  6803. (setq n (/ (1- (length (match-string 0))) 2))
  6804. (while (>= (setq n (1- n)) 0)
  6805. (org-promote))
  6806. (end-of-line 1))))))
  6807. (defun org-tr-level (n)
  6808. "Make N odd if required."
  6809. (if org-odd-levels-only (1+ (/ n 2)) n))
  6810. ;;; Vertical tree motion, cutting and pasting of subtrees
  6811. (defun org-move-subtree-up (&optional arg)
  6812. "Move the current subtree up past ARG headlines of the same level."
  6813. (interactive "p")
  6814. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6815. (defun org-move-subtree-down (&optional arg)
  6816. "Move the current subtree down past ARG headlines of the same level."
  6817. (interactive "p")
  6818. (setq arg (prefix-numeric-value arg))
  6819. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6820. 'org-get-last-sibling))
  6821. (ins-point (make-marker))
  6822. (cnt (abs arg))
  6823. (col (current-column))
  6824. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6825. ;; Select the tree
  6826. (org-back-to-heading)
  6827. (setq beg0 (point))
  6828. (save-excursion
  6829. (setq ne-beg (org-back-over-empty-lines))
  6830. (setq beg (point)))
  6831. (save-match-data
  6832. (save-excursion (outline-end-of-heading)
  6833. (setq folded (outline-invisible-p)))
  6834. (outline-end-of-subtree))
  6835. (outline-next-heading)
  6836. (setq ne-end (org-back-over-empty-lines))
  6837. (setq end (point))
  6838. (goto-char beg0)
  6839. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6840. ;; include less whitespace
  6841. (save-excursion
  6842. (goto-char beg)
  6843. (forward-line (- ne-beg ne-end))
  6844. (setq beg (point))))
  6845. ;; Find insertion point, with error handling
  6846. (while (> cnt 0)
  6847. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6848. (progn (goto-char beg0)
  6849. (error "Cannot move past superior level or buffer limit")))
  6850. (setq cnt (1- cnt)))
  6851. (if (> arg 0)
  6852. ;; Moving forward - still need to move over subtree
  6853. (progn (org-end-of-subtree t t)
  6854. (save-excursion
  6855. (org-back-over-empty-lines)
  6856. (or (bolp) (newline)))))
  6857. (setq ne-ins (org-back-over-empty-lines))
  6858. (move-marker ins-point (point))
  6859. (setq txt (buffer-substring beg end))
  6860. (org-save-markers-in-region beg end)
  6861. (delete-region beg end)
  6862. (org-remove-empty-overlays-at beg)
  6863. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6864. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6865. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6866. (let ((bbb (point)))
  6867. (insert-before-markers txt)
  6868. (org-reinstall-markers-in-region bbb)
  6869. (move-marker ins-point bbb))
  6870. (or (bolp) (insert "\n"))
  6871. (setq ins-end (point))
  6872. (goto-char ins-point)
  6873. (org-skip-whitespace)
  6874. (when (and (< arg 0)
  6875. (org-first-sibling-p)
  6876. (> ne-ins ne-beg))
  6877. ;; Move whitespace back to beginning
  6878. (save-excursion
  6879. (goto-char ins-end)
  6880. (let ((kill-whole-line t))
  6881. (kill-line (- ne-ins ne-beg)) (point)))
  6882. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6883. (move-marker ins-point nil)
  6884. (if folded
  6885. (hide-subtree)
  6886. (org-show-entry)
  6887. (show-children)
  6888. (org-cycle-hide-drawers 'children))
  6889. (org-clean-visibility-after-subtree-move)
  6890. ;; move back to the initial column we were at
  6891. (move-to-column col)))
  6892. (defvar org-subtree-clip ""
  6893. "Clipboard for cut and paste of subtrees.
  6894. This is actually only a copy of the kill, because we use the normal kill
  6895. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6896. (defvar org-subtree-clip-folded nil
  6897. "Was the last copied subtree folded?
  6898. This is used to fold the tree back after pasting.")
  6899. (defun org-cut-subtree (&optional n)
  6900. "Cut the current subtree into the clipboard.
  6901. With prefix arg N, cut this many sequential subtrees.
  6902. This is a short-hand for marking the subtree and then cutting it."
  6903. (interactive "p")
  6904. (org-copy-subtree n 'cut))
  6905. (defun org-copy-subtree (&optional n cut force-store-markers)
  6906. "Cut the current subtree into the clipboard.
  6907. With prefix arg N, cut this many sequential subtrees.
  6908. This is a short-hand for marking the subtree and then copying it.
  6909. If CUT is non-nil, actually cut the subtree.
  6910. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6911. of some markers in the region, even if CUT is non-nil. This is
  6912. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6913. (interactive "p")
  6914. (let (beg end folded (beg0 (point)))
  6915. (if (org-called-interactively-p 'any)
  6916. (org-back-to-heading nil) ; take what looks like a subtree
  6917. (org-back-to-heading t)) ; take what is really there
  6918. (org-back-over-empty-lines)
  6919. (setq beg (point))
  6920. (skip-chars-forward " \t\r\n")
  6921. (save-match-data
  6922. (save-excursion (outline-end-of-heading)
  6923. (setq folded (outline-invisible-p)))
  6924. (condition-case nil
  6925. (org-forward-same-level (1- n) t)
  6926. (error nil))
  6927. (org-end-of-subtree t t))
  6928. (org-back-over-empty-lines)
  6929. (setq end (point))
  6930. (goto-char beg0)
  6931. (when (> end beg)
  6932. (setq org-subtree-clip-folded folded)
  6933. (when (or cut force-store-markers)
  6934. (org-save-markers-in-region beg end))
  6935. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6936. (setq org-subtree-clip (current-kill 0))
  6937. (message "%s: Subtree(s) with %d characters"
  6938. (if cut "Cut" "Copied")
  6939. (length org-subtree-clip)))))
  6940. (defun org-paste-subtree (&optional level tree for-yank)
  6941. "Paste the clipboard as a subtree, with modification of headline level.
  6942. The entire subtree is promoted or demoted in order to match a new headline
  6943. level.
  6944. If the cursor is at the beginning of a headline, the same level as
  6945. that headline is used to paste the tree
  6946. If not, the new level is derived from the *visible* headings
  6947. before and after the insertion point, and taken to be the inferior headline
  6948. level of the two. So if the previous visible heading is level 3 and the
  6949. next is level 4 (or vice versa), level 4 will be used for insertion.
  6950. This makes sure that the subtree remains an independent subtree and does
  6951. not swallow low level entries.
  6952. You can also force a different level, either by using a numeric prefix
  6953. argument, or by inserting the heading marker by hand. For example, if the
  6954. cursor is after \"*****\", then the tree will be shifted to level 5.
  6955. If optional TREE is given, use this text instead of the kill ring.
  6956. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6957. move back over whitespace before inserting, and move point to the end of
  6958. the inserted text when done."
  6959. (interactive "P")
  6960. (setq tree (or tree (and kill-ring (current-kill 0))))
  6961. (unless (org-kill-is-subtree-p tree)
  6962. (error "%s"
  6963. (substitute-command-keys
  6964. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6965. (org-with-limited-levels
  6966. (let* ((visp (not (outline-invisible-p)))
  6967. (txt tree)
  6968. (^re_ "\\(\\*+\\)[ \t]*")
  6969. (old-level (if (string-match org-outline-regexp-bol txt)
  6970. (- (match-end 0) (match-beginning 0) 1)
  6971. -1))
  6972. (force-level (cond (level (prefix-numeric-value level))
  6973. ((and (looking-at "[ \t]*$")
  6974. (string-match
  6975. "^\\*+$" (buffer-substring
  6976. (point-at-bol) (point))))
  6977. (- (match-end 1) (match-beginning 1)))
  6978. ((and (bolp)
  6979. (looking-at org-outline-regexp))
  6980. (- (match-end 0) (point) 1))
  6981. (t nil)))
  6982. (previous-level (save-excursion
  6983. (condition-case nil
  6984. (progn
  6985. (outline-previous-visible-heading 1)
  6986. (if (looking-at ^re_)
  6987. (- (match-end 0) (match-beginning 0) 1)
  6988. 1))
  6989. (error 1))))
  6990. (next-level (save-excursion
  6991. (condition-case nil
  6992. (progn
  6993. (or (looking-at org-outline-regexp)
  6994. (outline-next-visible-heading 1))
  6995. (if (looking-at ^re_)
  6996. (- (match-end 0) (match-beginning 0) 1)
  6997. 1))
  6998. (error 1))))
  6999. (new-level (or force-level (max previous-level next-level)))
  7000. (shift (if (or (= old-level -1)
  7001. (= new-level -1)
  7002. (= old-level new-level))
  7003. 0
  7004. (- new-level old-level)))
  7005. (delta (if (> shift 0) -1 1))
  7006. (func (if (> shift 0) 'org-demote 'org-promote))
  7007. (org-odd-levels-only nil)
  7008. beg end newend)
  7009. ;; Remove the forced level indicator
  7010. (if force-level
  7011. (delete-region (point-at-bol) (point)))
  7012. ;; Paste
  7013. (beginning-of-line (if (bolp) 1 2))
  7014. (unless for-yank (org-back-over-empty-lines))
  7015. (setq beg (point))
  7016. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7017. (insert-before-markers txt)
  7018. (unless (string-match "\n\\'" txt) (insert "\n"))
  7019. (setq newend (point))
  7020. (org-reinstall-markers-in-region beg)
  7021. (setq end (point))
  7022. (goto-char beg)
  7023. (skip-chars-forward " \t\n\r")
  7024. (setq beg (point))
  7025. (if (and (outline-invisible-p) visp)
  7026. (save-excursion (outline-show-heading)))
  7027. ;; Shift if necessary
  7028. (unless (= shift 0)
  7029. (save-restriction
  7030. (narrow-to-region beg end)
  7031. (while (not (= shift 0))
  7032. (org-map-region func (point-min) (point-max))
  7033. (setq shift (+ delta shift)))
  7034. (goto-char (point-min))
  7035. (setq newend (point-max))))
  7036. (when (or (org-called-interactively-p 'interactive) for-yank)
  7037. (message "Clipboard pasted as level %d subtree" new-level))
  7038. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7039. kill-ring
  7040. (eq org-subtree-clip (current-kill 0))
  7041. org-subtree-clip-folded)
  7042. ;; The tree was folded before it was killed/copied
  7043. (hide-subtree))
  7044. (and for-yank (goto-char newend)))))
  7045. (defun org-kill-is-subtree-p (&optional txt)
  7046. "Check if the current kill is an outline subtree, or a set of trees.
  7047. Returns nil if kill does not start with a headline, or if the first
  7048. headline level is not the largest headline level in the tree.
  7049. So this will actually accept several entries of equal levels as well,
  7050. which is OK for `org-paste-subtree'.
  7051. If optional TXT is given, check this string instead of the current kill."
  7052. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7053. (re (org-get-limited-outline-regexp))
  7054. (^re (concat "^" re))
  7055. (start-level (and kill
  7056. (string-match
  7057. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7058. kill)
  7059. (- (match-end 2) (match-beginning 2) 1)))
  7060. (start (1+ (or (match-beginning 2) -1))))
  7061. (if (not start-level)
  7062. (progn
  7063. nil) ;; does not even start with a heading
  7064. (catch 'exit
  7065. (while (setq start (string-match ^re kill (1+ start)))
  7066. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7067. (throw 'exit nil)))
  7068. t))))
  7069. (defvar org-markers-to-move nil
  7070. "Markers that should be moved with a cut-and-paste operation.
  7071. Those markers are stored together with their positions relative to
  7072. the start of the region.")
  7073. (defun org-save-markers-in-region (beg end)
  7074. "Check markers in region.
  7075. If these markers are between BEG and END, record their position relative
  7076. to BEG, so that after moving the block of text, we can put the markers back
  7077. into place.
  7078. This function gets called just before an entry or tree gets cut from the
  7079. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7080. called immediately, to move the markers with the entries."
  7081. (setq org-markers-to-move nil)
  7082. (when (featurep 'org-clock)
  7083. (org-clock-save-markers-for-cut-and-paste beg end))
  7084. (when (featurep 'org-agenda)
  7085. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7086. (defun org-check-and-save-marker (marker beg end)
  7087. "Check if MARKER is between BEG and END.
  7088. If yes, remember the marker and the distance to BEG."
  7089. (when (and (marker-buffer marker)
  7090. (equal (marker-buffer marker) (current-buffer)))
  7091. (if (and (>= marker beg) (< marker end))
  7092. (push (cons marker (- marker beg)) org-markers-to-move))))
  7093. (defun org-reinstall-markers-in-region (beg)
  7094. "Move all remembered markers to their position relative to BEG."
  7095. (mapc (lambda (x)
  7096. (move-marker (car x) (+ beg (cdr x))))
  7097. org-markers-to-move)
  7098. (setq org-markers-to-move nil))
  7099. (defun org-narrow-to-subtree ()
  7100. "Narrow buffer to the current subtree."
  7101. (interactive)
  7102. (save-excursion
  7103. (save-match-data
  7104. (org-with-limited-levels
  7105. (narrow-to-region
  7106. (progn (org-back-to-heading t) (point))
  7107. (progn (org-end-of-subtree t t)
  7108. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7109. (point)))))))
  7110. (defun org-narrow-to-block ()
  7111. "Narrow buffer to the current block."
  7112. (interactive)
  7113. (let* ((case-fold-search t)
  7114. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7115. "^[ \t]*#\\+end_.*")))
  7116. (if blockp
  7117. (narrow-to-region (car blockp) (cdr blockp))
  7118. (error "Not in a block"))))
  7119. (eval-when-compile
  7120. (defvar org-property-drawer-re))
  7121. (defvar org-property-start-re) ;; defined below
  7122. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7123. "Clone the task (subtree) at point N times.
  7124. The clones will be inserted as siblings.
  7125. In interactive use, the user will be prompted for the number of
  7126. clones to be produced, and for a time SHIFT, which may be a
  7127. repeater as used in time stamps, for example `+3d'.
  7128. When a valid repeater is given and the entry contains any time
  7129. stamps, the clones will become a sequence in time, with time
  7130. stamps in the subtree shifted for each clone produced. If SHIFT
  7131. is nil or the empty string, time stamps will be left alone. The
  7132. ID property of the original subtree is removed.
  7133. If the original subtree did contain time stamps with a repeater,
  7134. the following will happen:
  7135. - the repeater will be removed in each clone
  7136. - an additional clone will be produced, with the current, unshifted
  7137. date(s) in the entry.
  7138. - the original entry will be placed *after* all the clones, with
  7139. repeater intact.
  7140. - the start days in the repeater in the original entry will be shifted
  7141. to past the last clone.
  7142. In this way you can spell out a number of instances of a repeating task,
  7143. and still retain the repeater to cover future instances of the task."
  7144. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7145. (let (beg end template task idprop
  7146. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7147. (drawer-re org-drawer-regexp))
  7148. (if (not (and (integerp n) (> n 0)))
  7149. (error "Invalid number of replications %s" n))
  7150. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7151. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7152. shift)))
  7153. (error "Invalid shift specification %s" shift))
  7154. (when doshift
  7155. (setq shift-n (string-to-number (match-string 1 shift))
  7156. shift-what (cdr (assoc (match-string 2 shift)
  7157. '(("d" . day) ("w" . week)
  7158. ("m" . month) ("y" . year))))))
  7159. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7160. (setq nmin 1 nmax n)
  7161. (org-back-to-heading t)
  7162. (setq beg (point))
  7163. (setq idprop (org-entry-get nil "ID"))
  7164. (org-end-of-subtree t t)
  7165. (or (bolp) (insert "\n"))
  7166. (setq end (point))
  7167. (setq template (buffer-substring beg end))
  7168. (when (and doshift
  7169. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[dwmy][^<>\n]*>" template))
  7170. (delete-region beg end)
  7171. (setq end beg)
  7172. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7173. (goto-char end)
  7174. (loop for n from nmin to nmax do
  7175. ;; prepare clone
  7176. (with-temp-buffer
  7177. (insert template)
  7178. (org-mode)
  7179. (goto-char (point-min))
  7180. (org-show-subtree)
  7181. (and idprop (if org-clone-delete-id
  7182. (org-entry-delete nil "ID")
  7183. (org-id-get-create t)))
  7184. (unless (= n 0)
  7185. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7186. (kill-whole-line))
  7187. (goto-char (point-min))
  7188. (while (re-search-forward drawer-re nil t)
  7189. (mapc (lambda (d)
  7190. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7191. (goto-char (point-min))
  7192. (when doshift
  7193. (while (re-search-forward org-ts-regexp-both nil t)
  7194. (org-timestamp-change (* n shift-n) shift-what))
  7195. (unless (= n n-no-remove)
  7196. (goto-char (point-min))
  7197. (while (re-search-forward org-ts-regexp nil t)
  7198. (save-excursion
  7199. (goto-char (match-beginning 0))
  7200. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[dwmy]\\)")
  7201. (delete-region (match-beginning 1) (match-end 1)))))))
  7202. (setq task (buffer-string)))
  7203. (insert task))
  7204. (goto-char beg)))
  7205. ;;; Outline Sorting
  7206. (defun org-sort (with-case)
  7207. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7208. Optional argument WITH-CASE means sort case-sensitively."
  7209. (interactive "P")
  7210. (cond
  7211. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7212. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7213. (t
  7214. (org-call-with-arg 'org-sort-entries with-case))))
  7215. (defun org-sort-remove-invisible (s)
  7216. (remove-text-properties 0 (length s) org-rm-props s)
  7217. (while (string-match org-bracket-link-regexp s)
  7218. (setq s (replace-match (if (match-end 2)
  7219. (match-string 3 s)
  7220. (match-string 1 s)) t t s)))
  7221. s)
  7222. (defvar org-priority-regexp) ; defined later in the file
  7223. (defvar org-after-sorting-entries-or-items-hook nil
  7224. "Hook that is run after a bunch of entries or items have been sorted.
  7225. When children are sorted, the cursor is in the parent line when this
  7226. hook gets called. When a region or a plain list is sorted, the cursor
  7227. will be in the first entry of the sorted region/list.")
  7228. (defun org-sort-entries
  7229. (&optional with-case sorting-type getkey-func compare-func property)
  7230. "Sort entries on a certain level of an outline tree.
  7231. If there is an active region, the entries in the region are sorted.
  7232. Else, if the cursor is before the first entry, sort the top-level items.
  7233. Else, the children of the entry at point are sorted.
  7234. Sorting can be alphabetically, numerically, by date/time as given by
  7235. a time stamp, by a property or by priority.
  7236. The command prompts for the sorting type unless it has been given to the
  7237. function through the SORTING-TYPE argument, which needs to be a character,
  7238. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7239. precise meaning of each character:
  7240. n Numerically, by converting the beginning of the entry/item to a number.
  7241. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7242. t By date/time, either the first active time stamp in the entry, or, if
  7243. none exist, by the first inactive one.
  7244. s By the scheduled date/time.
  7245. d By deadline date/time.
  7246. c By creation time, which is assumed to be the first inactive time stamp
  7247. at the beginning of a line.
  7248. p By priority according to the cookie.
  7249. r By the value of a property.
  7250. Capital letters will reverse the sort order.
  7251. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7252. called with point at the beginning of the record. It must return either
  7253. a string or a number that should serve as the sorting key for that record.
  7254. Comparing entries ignores case by default. However, with an optional argument
  7255. WITH-CASE, the sorting considers case as well."
  7256. (interactive "P")
  7257. (let ((case-func (if with-case 'identity 'downcase))
  7258. start beg end stars re re2
  7259. txt what tmp)
  7260. ;; Find beginning and end of region to sort
  7261. (cond
  7262. ((org-region-active-p)
  7263. ;; we will sort the region
  7264. (setq end (region-end)
  7265. what "region")
  7266. (goto-char (region-beginning))
  7267. (if (not (org-at-heading-p)) (outline-next-heading))
  7268. (setq start (point)))
  7269. ((or (org-at-heading-p)
  7270. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7271. ;; we will sort the children of the current headline
  7272. (org-back-to-heading)
  7273. (setq start (point)
  7274. end (progn (org-end-of-subtree t t)
  7275. (or (bolp) (insert "\n"))
  7276. (org-back-over-empty-lines)
  7277. (point))
  7278. what "children")
  7279. (goto-char start)
  7280. (show-subtree)
  7281. (outline-next-heading))
  7282. (t
  7283. ;; we will sort the top-level entries in this file
  7284. (goto-char (point-min))
  7285. (or (org-at-heading-p) (outline-next-heading))
  7286. (setq start (point))
  7287. (goto-char (point-max))
  7288. (beginning-of-line 1)
  7289. (when (looking-at ".*?\\S-")
  7290. ;; File ends in a non-white line
  7291. (end-of-line 1)
  7292. (insert "\n"))
  7293. (setq end (point-max))
  7294. (setq what "top-level")
  7295. (goto-char start)
  7296. (show-all)))
  7297. (setq beg (point))
  7298. (if (>= beg end) (error "Nothing to sort"))
  7299. (looking-at "\\(\\*+\\)")
  7300. (setq stars (match-string 1)
  7301. re (concat "^" (regexp-quote stars) " +")
  7302. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7303. txt (buffer-substring beg end))
  7304. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7305. (if (and (not (equal stars "*")) (string-match re2 txt))
  7306. (error "Region to sort contains a level above the first entry"))
  7307. (unless sorting-type
  7308. (message
  7309. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7310. [t]ime [s]cheduled [d]eadline [c]reated
  7311. A/N/T/S/D/C/P/O/F means reversed:"
  7312. what)
  7313. (setq sorting-type (read-char-exclusive))
  7314. (and (= (downcase sorting-type) ?f)
  7315. (setq getkey-func
  7316. (org-icompleting-read "Sort using function: "
  7317. obarray 'fboundp t nil nil))
  7318. (setq getkey-func (intern getkey-func)))
  7319. (and (= (downcase sorting-type) ?r)
  7320. (setq property
  7321. (org-icompleting-read "Property: "
  7322. (mapcar 'list (org-buffer-property-keys t))
  7323. nil t))))
  7324. (message "Sorting entries...")
  7325. (save-restriction
  7326. (narrow-to-region start end)
  7327. (let ((dcst (downcase sorting-type))
  7328. (case-fold-search nil)
  7329. (now (current-time)))
  7330. (sort-subr
  7331. (/= dcst sorting-type)
  7332. ;; This function moves to the beginning character of the "record" to
  7333. ;; be sorted.
  7334. (lambda nil
  7335. (if (re-search-forward re nil t)
  7336. (goto-char (match-beginning 0))
  7337. (goto-char (point-max))))
  7338. ;; This function moves to the last character of the "record" being
  7339. ;; sorted.
  7340. (lambda nil
  7341. (save-match-data
  7342. (condition-case nil
  7343. (outline-forward-same-level 1)
  7344. (error
  7345. (goto-char (point-max))))))
  7346. ;; This function returns the value that gets sorted against.
  7347. (lambda nil
  7348. (cond
  7349. ((= dcst ?n)
  7350. (if (looking-at org-complex-heading-regexp)
  7351. (string-to-number (match-string 4))
  7352. nil))
  7353. ((= dcst ?a)
  7354. (if (looking-at org-complex-heading-regexp)
  7355. (funcall case-func (match-string 4))
  7356. nil))
  7357. ((= dcst ?t)
  7358. (let ((end (save-excursion (outline-next-heading) (point))))
  7359. (if (or (re-search-forward org-ts-regexp end t)
  7360. (re-search-forward org-ts-regexp-both end t))
  7361. (org-time-string-to-seconds (match-string 0))
  7362. (org-float-time now))))
  7363. ((= dcst ?c)
  7364. (let ((end (save-excursion (outline-next-heading) (point))))
  7365. (if (re-search-forward
  7366. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7367. end t)
  7368. (org-time-string-to-seconds (match-string 0))
  7369. (org-float-time now))))
  7370. ((= dcst ?s)
  7371. (let ((end (save-excursion (outline-next-heading) (point))))
  7372. (if (re-search-forward org-scheduled-time-regexp end t)
  7373. (org-time-string-to-seconds (match-string 1))
  7374. (org-float-time now))))
  7375. ((= dcst ?d)
  7376. (let ((end (save-excursion (outline-next-heading) (point))))
  7377. (if (re-search-forward org-deadline-time-regexp end t)
  7378. (org-time-string-to-seconds (match-string 1))
  7379. (org-float-time now))))
  7380. ((= dcst ?p)
  7381. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7382. (string-to-char (match-string 2))
  7383. org-default-priority))
  7384. ((= dcst ?r)
  7385. (or (org-entry-get nil property) ""))
  7386. ((= dcst ?o)
  7387. (if (looking-at org-complex-heading-regexp)
  7388. (- 9999 (length (member (match-string 2)
  7389. org-todo-keywords-1)))))
  7390. ((= dcst ?f)
  7391. (if getkey-func
  7392. (progn
  7393. (setq tmp (funcall getkey-func))
  7394. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7395. tmp)
  7396. (error "Invalid key function `%s'" getkey-func)))
  7397. (t (error "Invalid sorting type `%c'" sorting-type))))
  7398. nil
  7399. (cond
  7400. ((= dcst ?a) 'string<)
  7401. ((= dcst ?f) compare-func)
  7402. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7403. (t nil)))))
  7404. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7405. (message "Sorting entries...done")))
  7406. (defun org-do-sort (table what &optional with-case sorting-type)
  7407. "Sort TABLE of WHAT according to SORTING-TYPE.
  7408. The user will be prompted for the SORTING-TYPE if the call to this
  7409. function does not specify it. WHAT is only for the prompt, to indicate
  7410. what is being sorted. The sorting key will be extracted from
  7411. the car of the elements of the table.
  7412. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7413. (unless sorting-type
  7414. (message
  7415. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7416. what)
  7417. (setq sorting-type (read-char-exclusive)))
  7418. (let ((dcst (downcase sorting-type))
  7419. extractfun comparefun)
  7420. ;; Define the appropriate functions
  7421. (cond
  7422. ((= dcst ?n)
  7423. (setq extractfun 'string-to-number
  7424. comparefun (if (= dcst sorting-type) '< '>)))
  7425. ((= dcst ?a)
  7426. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7427. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7428. comparefun (if (= dcst sorting-type)
  7429. 'string<
  7430. (lambda (a b) (and (not (string< a b))
  7431. (not (string= a b)))))))
  7432. ((= dcst ?t)
  7433. (setq extractfun
  7434. (lambda (x)
  7435. (if (or (string-match org-ts-regexp x)
  7436. (string-match org-ts-regexp-both x))
  7437. (org-float-time
  7438. (org-time-string-to-time (match-string 0 x)))
  7439. 0))
  7440. comparefun (if (= dcst sorting-type) '< '>)))
  7441. (t (error "Invalid sorting type `%c'" sorting-type)))
  7442. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7443. table)
  7444. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7445. ;;; The orgstruct minor mode
  7446. ;; Define a minor mode which can be used in other modes in order to
  7447. ;; integrate the org-mode structure editing commands.
  7448. ;; This is really a hack, because the org-mode structure commands use
  7449. ;; keys which normally belong to the major mode. Here is how it
  7450. ;; works: The minor mode defines all the keys necessary to operate the
  7451. ;; structure commands, but wraps the commands into a function which
  7452. ;; tests if the cursor is currently at a headline or a plain list
  7453. ;; item. If that is the case, the structure command is used,
  7454. ;; temporarily setting many Org-mode variables like regular
  7455. ;; expressions for filling etc. However, when any of those keys is
  7456. ;; used at a different location, function uses `key-binding' to look
  7457. ;; up if the key has an associated command in another currently active
  7458. ;; keymap (minor modes, major mode, global), and executes that
  7459. ;; command. There might be problems if any of the keys is otherwise
  7460. ;; used as a prefix key.
  7461. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7462. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7463. ;; addresses this by checking explicitly for both bindings.
  7464. (defvar orgstruct-mode-map (make-sparse-keymap)
  7465. "Keymap for the minor `orgstruct-mode'.")
  7466. (defvar org-local-vars nil
  7467. "List of local variables, for use by `orgstruct-mode'.")
  7468. ;;;###autoload
  7469. (define-minor-mode orgstruct-mode
  7470. "Toggle the minor mode `orgstruct-mode'.
  7471. This mode is for using Org-mode structure commands in other
  7472. modes. The following keys behave as if Org-mode were active, if
  7473. the cursor is on a headline, or on a plain list item (both as
  7474. defined by Org-mode).
  7475. M-up Move entry/item up
  7476. M-down Move entry/item down
  7477. M-left Promote
  7478. M-right Demote
  7479. M-S-up Move entry/item up
  7480. M-S-down Move entry/item down
  7481. M-S-left Promote subtree
  7482. M-S-right Demote subtree
  7483. M-q Fill paragraph and items like in Org-mode
  7484. C-c ^ Sort entries
  7485. C-c - Cycle list bullet
  7486. TAB Cycle item visibility
  7487. M-RET Insert new heading/item
  7488. S-M-RET Insert new TODO heading / Checkbox item
  7489. C-c C-c Set tags / toggle checkbox"
  7490. nil " OrgStruct" nil
  7491. (org-load-modules-maybe)
  7492. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7493. ;;;###autoload
  7494. (defun turn-on-orgstruct ()
  7495. "Unconditionally turn on `orgstruct-mode'."
  7496. (orgstruct-mode 1))
  7497. (defun orgstruct++-mode (&optional arg)
  7498. "Toggle `orgstruct-mode', the enhanced version of it.
  7499. In addition to setting orgstruct-mode, this also exports all indentation
  7500. and autofilling variables from org-mode into the buffer. It will also
  7501. recognize item context in multiline items.
  7502. Note that turning off orgstruct-mode will *not* remove the
  7503. indentation/paragraph settings. This can only be done by refreshing the
  7504. major mode, for example with \\[normal-mode]."
  7505. (interactive "P")
  7506. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7507. (if (< arg 1)
  7508. (orgstruct-mode -1)
  7509. (orgstruct-mode 1)
  7510. (let (var val)
  7511. (mapc
  7512. (lambda (x)
  7513. (when (string-match
  7514. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7515. (symbol-name (car x)))
  7516. (setq var (car x) val (nth 1 x))
  7517. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7518. org-local-vars)
  7519. (org-set-local 'orgstruct-is-++ t))))
  7520. (defvar orgstruct-is-++ nil
  7521. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7522. (make-variable-buffer-local 'orgstruct-is-++)
  7523. ;;;###autoload
  7524. (defun turn-on-orgstruct++ ()
  7525. "Unconditionally turn on `orgstruct++-mode'."
  7526. (orgstruct++-mode 1))
  7527. (defun orgstruct-error ()
  7528. "Error when there is no default binding for a structure key."
  7529. (interactive)
  7530. (error "This key has no function outside structure elements"))
  7531. (defun orgstruct-setup ()
  7532. "Setup orgstruct keymaps."
  7533. (let ((nfunc 0)
  7534. (bindings
  7535. (list
  7536. '([(meta up)] org-metaup)
  7537. '([(meta down)] org-metadown)
  7538. '([(meta left)] org-metaleft)
  7539. '([(meta right)] org-metaright)
  7540. '([(meta shift up)] org-shiftmetaup)
  7541. '([(meta shift down)] org-shiftmetadown)
  7542. '([(meta shift left)] org-shiftmetaleft)
  7543. '([(meta shift right)] org-shiftmetaright)
  7544. '([?\e (up)] org-metaup)
  7545. '([?\e (down)] org-metadown)
  7546. '([?\e (left)] org-metaleft)
  7547. '([?\e (right)] org-metaright)
  7548. '([?\e (shift up)] org-shiftmetaup)
  7549. '([?\e (shift down)] org-shiftmetadown)
  7550. '([?\e (shift left)] org-shiftmetaleft)
  7551. '([?\e (shift right)] org-shiftmetaright)
  7552. '([(shift up)] org-shiftup)
  7553. '([(shift down)] org-shiftdown)
  7554. '([(shift left)] org-shiftleft)
  7555. '([(shift right)] org-shiftright)
  7556. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7557. '("\M-q" fill-paragraph)
  7558. '("\C-c^" org-sort)
  7559. '("\C-c-" org-cycle-list-bullet)))
  7560. elt key fun cmd)
  7561. (while (setq elt (pop bindings))
  7562. (setq nfunc (1+ nfunc))
  7563. (setq key (org-key (car elt))
  7564. fun (nth 1 elt)
  7565. cmd (orgstruct-make-binding fun nfunc key))
  7566. (org-defkey orgstruct-mode-map key cmd))
  7567. ;; Special treatment needed for TAB and RET
  7568. (org-defkey orgstruct-mode-map [(tab)]
  7569. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7570. (org-defkey orgstruct-mode-map "\C-i"
  7571. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7572. (org-defkey orgstruct-mode-map "\M-\C-m"
  7573. (orgstruct-make-binding 'org-insert-heading 105
  7574. "\M-\C-m" [(meta return)]))
  7575. (org-defkey orgstruct-mode-map [(meta return)]
  7576. (orgstruct-make-binding 'org-insert-heading 106
  7577. [(meta return)] "\M-\C-m"))
  7578. (org-defkey orgstruct-mode-map [(shift meta return)]
  7579. (orgstruct-make-binding 'org-insert-todo-heading 107
  7580. [(meta return)] "\M-\C-m"))
  7581. (org-defkey orgstruct-mode-map "\e\C-m"
  7582. (orgstruct-make-binding 'org-insert-heading 108
  7583. "\e\C-m" [?\e (return)]))
  7584. (org-defkey orgstruct-mode-map [?\e (return)]
  7585. (orgstruct-make-binding 'org-insert-heading 109
  7586. [?\e (return)] "\e\C-m"))
  7587. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7588. (orgstruct-make-binding 'org-insert-todo-heading 110
  7589. [?\e (return)] "\e\C-m"))
  7590. (unless org-local-vars
  7591. (setq org-local-vars (org-get-local-variables)))
  7592. t))
  7593. (defun orgstruct-make-binding (fun n &rest keys)
  7594. "Create a function for binding in the structure minor mode.
  7595. FUN is the command to call inside a table. N is used to create a unique
  7596. command name. KEYS are keys that should be checked in for a command
  7597. to execute outside of tables."
  7598. (eval
  7599. (list 'defun
  7600. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7601. '(arg)
  7602. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7603. "Outside of structure, run the binding of `"
  7604. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7605. "'.")
  7606. '(interactive "p")
  7607. (list 'if
  7608. `(org-context-p 'headline 'item
  7609. (and orgstruct-is-++
  7610. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7611. 'item-body))
  7612. (list 'org-run-like-in-org-mode (list 'quote fun))
  7613. (list 'let '(orgstruct-mode)
  7614. (list 'call-interactively
  7615. (append '(or)
  7616. (mapcar (lambda (k)
  7617. (list 'key-binding k))
  7618. keys)
  7619. '('orgstruct-error))))))))
  7620. (defun org-context-p (&rest contexts)
  7621. "Check if local context is any of CONTEXTS.
  7622. Possible values in the list of contexts are `table', `headline', and `item'."
  7623. (let ((pos (point)))
  7624. (goto-char (point-at-bol))
  7625. (prog1 (or (and (memq 'table contexts)
  7626. (looking-at "[ \t]*|"))
  7627. (and (memq 'headline contexts)
  7628. (looking-at org-outline-regexp))
  7629. (and (memq 'item contexts)
  7630. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7631. (and (memq 'item-body contexts)
  7632. (org-in-item-p)))
  7633. (goto-char pos))))
  7634. (defun org-get-local-variables ()
  7635. "Return a list of all local variables in an org-mode buffer."
  7636. (let (varlist)
  7637. (with-current-buffer (get-buffer-create "*Org tmp*")
  7638. (erase-buffer)
  7639. (org-mode)
  7640. (setq varlist (buffer-local-variables)))
  7641. (kill-buffer "*Org tmp*")
  7642. (delq nil
  7643. (mapcar
  7644. (lambda (x)
  7645. (setq x
  7646. (if (symbolp x)
  7647. (list x)
  7648. (list (car x) (list 'quote (cdr x)))))
  7649. (if (string-match
  7650. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7651. (symbol-name (car x)))
  7652. x nil))
  7653. varlist))))
  7654. (defun org-clone-local-variables (from-buffer &optional regexp)
  7655. "Clone local variables from FROM-BUFFER.
  7656. Optional argument REGEXP selects variables to clone."
  7657. (mapc
  7658. (lambda (pair)
  7659. (and (symbolp (car pair))
  7660. (or (null regexp)
  7661. (string-match regexp (symbol-name (car pair))))
  7662. (set (make-local-variable (car pair))
  7663. (cdr pair))))
  7664. (buffer-local-variables from-buffer)))
  7665. ;;;###autoload
  7666. (defun org-run-like-in-org-mode (cmd)
  7667. "Run a command, pretending that the current buffer is in Org-mode.
  7668. This will temporarily bind local variables that are typically bound in
  7669. Org-mode to the values they have in Org-mode, and then interactively
  7670. call CMD."
  7671. (org-load-modules-maybe)
  7672. (unless org-local-vars
  7673. (setq org-local-vars (org-get-local-variables)))
  7674. (eval (list 'let org-local-vars
  7675. (list 'call-interactively (list 'quote cmd)))))
  7676. ;;;; Archiving
  7677. (defun org-get-category (&optional pos force-refresh)
  7678. "Get the category applying to position POS."
  7679. (save-match-data
  7680. (if force-refresh (org-refresh-category-properties))
  7681. (let ((pos (or pos (point))))
  7682. (or (get-text-property pos 'org-category)
  7683. (progn (org-refresh-category-properties)
  7684. (get-text-property pos 'org-category))))))
  7685. (defun org-refresh-category-properties ()
  7686. "Refresh category text properties in the buffer."
  7687. (let ((def-cat (cond
  7688. ((null org-category)
  7689. (if buffer-file-name
  7690. (file-name-sans-extension
  7691. (file-name-nondirectory buffer-file-name))
  7692. "???"))
  7693. ((symbolp org-category) (symbol-name org-category))
  7694. (t org-category)))
  7695. beg end cat pos optionp)
  7696. (org-unmodified
  7697. (save-excursion
  7698. (save-restriction
  7699. (widen)
  7700. (goto-char (point-min))
  7701. (put-text-property (point) (point-max) 'org-category def-cat)
  7702. (while (re-search-forward
  7703. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7704. (setq pos (match-end 0)
  7705. optionp (equal (char-after (match-beginning 0)) ?#)
  7706. cat (org-trim (match-string 2)))
  7707. (if optionp
  7708. (setq beg (point-at-bol) end (point-max))
  7709. (org-back-to-heading t)
  7710. (setq beg (point) end (org-end-of-subtree t t)))
  7711. (put-text-property beg end 'org-category cat)
  7712. (put-text-property beg end 'org-category-position beg)
  7713. (goto-char pos)))))))
  7714. ;;;; Link Stuff
  7715. ;;; Link abbreviations
  7716. (defun org-link-expand-abbrev (link)
  7717. "Apply replacements as defined in `org-link-abbrev-alist."
  7718. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7719. (let* ((key (match-string 1 link))
  7720. (as (or (assoc key org-link-abbrev-alist-local)
  7721. (assoc key org-link-abbrev-alist)))
  7722. (tag (and (match-end 2) (match-string 3 link)))
  7723. rpl)
  7724. (if (not as)
  7725. link
  7726. (setq rpl (cdr as))
  7727. (cond
  7728. ((symbolp rpl) (funcall rpl tag))
  7729. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7730. ((string-match "%h" rpl)
  7731. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7732. (t (concat rpl tag)))))
  7733. link))
  7734. ;;; Storing and inserting links
  7735. (defvar org-insert-link-history nil
  7736. "Minibuffer history for links inserted with `org-insert-link'.")
  7737. (defvar org-stored-links nil
  7738. "Contains the links stored with `org-store-link'.")
  7739. (defvar org-store-link-plist nil
  7740. "Plist with info about the most recently link created with `org-store-link'.")
  7741. (defvar org-link-protocols nil
  7742. "Link protocols added to Org-mode using `org-add-link-type'.")
  7743. (defvar org-store-link-functions nil
  7744. "List of functions that are called to create and store a link.
  7745. Each function will be called in turn until one returns a non-nil
  7746. value. Each function should check if it is responsible for creating
  7747. this link (for example by looking at the major mode).
  7748. If not, it must exit and return nil.
  7749. If yes, it should return a non-nil value after a calling
  7750. `org-store-link-props' with a list of properties and values.
  7751. Special properties are:
  7752. :type The link prefix, like \"http\". This must be given.
  7753. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7754. This is obligatory as well.
  7755. :description Optional default description for the second pair
  7756. of brackets in an Org-mode link. The user can still change
  7757. this when inserting this link into an Org-mode buffer.
  7758. In addition to these, any additional properties can be specified
  7759. and then used in remember templates.")
  7760. (defun org-add-link-type (type &optional follow export)
  7761. "Add TYPE to the list of `org-link-types'.
  7762. Re-compute all regular expressions depending on `org-link-types'
  7763. FOLLOW and EXPORT are two functions.
  7764. FOLLOW should take the link path as the single argument and do whatever
  7765. is necessary to follow the link, for example find a file or display
  7766. a mail message.
  7767. EXPORT should format the link path for export to one of the export formats.
  7768. It should be a function accepting three arguments:
  7769. path the path of the link, the text after the prefix (like \"http:\")
  7770. desc the description of the link, if any, or a description added by
  7771. org-export-normalize-links if there is none
  7772. format the export format, a symbol like `html' or `latex' or `ascii'..
  7773. The function may use the FORMAT information to return different values
  7774. depending on the format. The return value will be put literally into
  7775. the exported file. If the return value is nil, this means Org should
  7776. do what it normally does with links which do not have EXPORT defined.
  7777. Org-mode has a built-in default for exporting links. If you are happy with
  7778. this default, there is no need to define an export function for the link
  7779. type. For a simple example of an export function, see `org-bbdb.el'."
  7780. (add-to-list 'org-link-types type t)
  7781. (org-make-link-regexps)
  7782. (if (assoc type org-link-protocols)
  7783. (setcdr (assoc type org-link-protocols) (list follow export))
  7784. (push (list type follow export) org-link-protocols)))
  7785. (defvar org-agenda-buffer-name)
  7786. ;;;###autoload
  7787. (defun org-store-link (arg)
  7788. "\\<org-mode-map>Store an org-link to the current location.
  7789. This link is added to `org-stored-links' and can later be inserted
  7790. into an org-buffer with \\[org-insert-link].
  7791. For some link types, a prefix arg is interpreted:
  7792. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7793. For file links, arg negates `org-context-in-file-links'."
  7794. (interactive "P")
  7795. (org-load-modules-maybe)
  7796. (setq org-store-link-plist nil) ; reset
  7797. (org-with-limited-levels
  7798. (let (link cpltxt desc description search txt custom-id agenda-link)
  7799. (cond
  7800. ((run-hook-with-args-until-success 'org-store-link-functions)
  7801. (setq link (plist-get org-store-link-plist :link)
  7802. desc (or (plist-get org-store-link-plist :description) link)))
  7803. ((org-src-edit-buffer-p)
  7804. (let (label gc)
  7805. (while (or (not label)
  7806. (save-excursion
  7807. (save-restriction
  7808. (widen)
  7809. (goto-char (point-min))
  7810. (re-search-forward
  7811. (regexp-quote (format org-coderef-label-format label))
  7812. nil t))))
  7813. (when label (message "Label exists already") (sit-for 2))
  7814. (setq label (read-string "Code line label: " label)))
  7815. (end-of-line 1)
  7816. (setq link (format org-coderef-label-format label))
  7817. (setq gc (- 79 (length link)))
  7818. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7819. (insert link)
  7820. (setq link (concat "(" label ")") desc nil)))
  7821. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7822. ;; We are in the agenda, link to referenced location
  7823. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7824. (get-text-property (point) 'org-marker))))
  7825. (when m
  7826. (org-with-point-at m
  7827. (setq agenda-link
  7828. (if (org-called-interactively-p 'any)
  7829. (call-interactively 'org-store-link)
  7830. (org-store-link nil)))))))
  7831. ((eq major-mode 'calendar-mode)
  7832. (let ((cd (calendar-cursor-to-date)))
  7833. (setq link
  7834. (format-time-string
  7835. (car org-time-stamp-formats)
  7836. (apply 'encode-time
  7837. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7838. nil nil nil))))
  7839. (org-store-link-props :type "calendar" :date cd)))
  7840. ((eq major-mode 'w3-mode)
  7841. (setq cpltxt (if (and (buffer-name)
  7842. (not (string-match "Untitled" (buffer-name))))
  7843. (buffer-name)
  7844. (url-view-url t))
  7845. link (org-make-link (url-view-url t)))
  7846. (org-store-link-props :type "w3" :url (url-view-url t)))
  7847. ((eq major-mode 'w3m-mode)
  7848. (setq cpltxt (or w3m-current-title w3m-current-url)
  7849. link (org-make-link w3m-current-url))
  7850. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7851. ((setq search (run-hook-with-args-until-success
  7852. 'org-create-file-search-functions))
  7853. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7854. "::" search))
  7855. (setq cpltxt (or description link)))
  7856. ((eq major-mode 'image-mode)
  7857. (setq cpltxt (concat "file:"
  7858. (abbreviate-file-name buffer-file-name))
  7859. link (org-make-link cpltxt))
  7860. (org-store-link-props :type "image" :file buffer-file-name))
  7861. ((eq major-mode 'dired-mode)
  7862. ;; link to the file in the current line
  7863. (let ((file (dired-get-filename nil t)))
  7864. (setq file (if file
  7865. (abbreviate-file-name
  7866. (expand-file-name (dired-get-filename nil t)))
  7867. ;; otherwise, no file so use current directory.
  7868. default-directory))
  7869. (setq cpltxt (concat "file:" file)
  7870. link (org-make-link cpltxt))))
  7871. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7872. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7873. (cond
  7874. ((org-in-regexp "<<\\(.*?\\)>>")
  7875. (setq cpltxt
  7876. (concat "file:"
  7877. (abbreviate-file-name
  7878. (buffer-file-name (buffer-base-buffer)))
  7879. "::" (match-string 1))
  7880. link (org-make-link cpltxt)))
  7881. ((and (featurep 'org-id)
  7882. (or (eq org-link-to-org-use-id t)
  7883. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7884. (org-called-interactively-p 'any))
  7885. (and (eq org-link-to-org-use-id
  7886. 'create-if-interactive-and-no-custom-id)
  7887. (org-called-interactively-p 'any)
  7888. (not custom-id))
  7889. (and org-link-to-org-use-id
  7890. (org-entry-get nil "ID"))))
  7891. ;; We can make a link using the ID.
  7892. (setq link (condition-case nil
  7893. (prog1 (org-id-store-link)
  7894. (setq desc (plist-get org-store-link-plist
  7895. :description)))
  7896. (error
  7897. ;; probably before first headline, link to file only
  7898. (concat "file:"
  7899. (abbreviate-file-name
  7900. (buffer-file-name (buffer-base-buffer))))))))
  7901. (t
  7902. ;; Just link to current headline
  7903. (setq cpltxt (concat "file:"
  7904. (abbreviate-file-name
  7905. (buffer-file-name (buffer-base-buffer)))))
  7906. ;; Add a context search string
  7907. (when (org-xor org-context-in-file-links arg)
  7908. (setq txt (cond
  7909. ((org-at-heading-p) nil)
  7910. ((org-region-active-p)
  7911. (buffer-substring (region-beginning) (region-end)))
  7912. (t nil)))
  7913. (when (or (null txt) (string-match "\\S-" txt))
  7914. (setq cpltxt
  7915. (concat cpltxt "::"
  7916. (condition-case nil
  7917. (org-make-org-heading-search-string txt)
  7918. (error "")))
  7919. desc (or (nth 4 (ignore-errors
  7920. (org-heading-components))) "NONE"))))
  7921. (if (string-match "::\\'" cpltxt)
  7922. (setq cpltxt (substring cpltxt 0 -2)))
  7923. (setq link (org-make-link cpltxt)))))
  7924. ((buffer-file-name (buffer-base-buffer))
  7925. ;; Just link to this file here.
  7926. (setq cpltxt (concat "file:"
  7927. (abbreviate-file-name
  7928. (buffer-file-name (buffer-base-buffer)))))
  7929. ;; Add a context string
  7930. (when (org-xor org-context-in-file-links arg)
  7931. (setq txt (if (org-region-active-p)
  7932. (buffer-substring (region-beginning) (region-end))
  7933. (buffer-substring (point-at-bol) (point-at-eol))))
  7934. ;; Only use search option if there is some text.
  7935. (when (string-match "\\S-" txt)
  7936. (setq cpltxt
  7937. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7938. desc "NONE")))
  7939. (setq link (org-make-link cpltxt)))
  7940. ((org-called-interactively-p 'interactive)
  7941. (error "Cannot link to a buffer which is not visiting a file"))
  7942. (t (setq link nil)))
  7943. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7944. (setq link (or link cpltxt)
  7945. desc (or desc cpltxt))
  7946. (if (equal desc "NONE") (setq desc nil))
  7947. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7948. (progn
  7949. (setq org-stored-links
  7950. (cons (list link desc) org-stored-links))
  7951. (message "Stored: %s" (or desc link))
  7952. (when custom-id
  7953. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7954. "::#" custom-id))
  7955. (setq org-stored-links
  7956. (cons (list link desc) org-stored-links))))
  7957. (or agenda-link (and link (org-make-link-string link desc)))))))
  7958. (defun org-store-link-props (&rest plist)
  7959. "Store link properties, extract names and addresses."
  7960. (let (x adr)
  7961. (when (setq x (plist-get plist :from))
  7962. (setq adr (mail-extract-address-components x))
  7963. (setq plist (plist-put plist :fromname (car adr)))
  7964. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7965. (when (setq x (plist-get plist :to))
  7966. (setq adr (mail-extract-address-components x))
  7967. (setq plist (plist-put plist :toname (car adr)))
  7968. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7969. (let ((from (plist-get plist :from))
  7970. (to (plist-get plist :to)))
  7971. (when (and from to org-from-is-user-regexp)
  7972. (setq plist
  7973. (plist-put plist :fromto
  7974. (if (string-match org-from-is-user-regexp from)
  7975. (concat "to %t")
  7976. (concat "from %f"))))))
  7977. (setq org-store-link-plist plist))
  7978. (defun org-add-link-props (&rest plist)
  7979. "Add these properties to the link property list."
  7980. (let (key value)
  7981. (while plist
  7982. (setq key (pop plist) value (pop plist))
  7983. (setq org-store-link-plist
  7984. (plist-put org-store-link-plist key value)))))
  7985. (defun org-email-link-description (&optional fmt)
  7986. "Return the description part of an email link.
  7987. This takes information from `org-store-link-plist' and formats it
  7988. according to FMT (default from `org-email-link-description-format')."
  7989. (setq fmt (or fmt org-email-link-description-format))
  7990. (let* ((p org-store-link-plist)
  7991. (to (plist-get p :toaddress))
  7992. (from (plist-get p :fromaddress))
  7993. (table
  7994. (list
  7995. (cons "%c" (plist-get p :fromto))
  7996. (cons "%F" (plist-get p :from))
  7997. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7998. (cons "%T" (plist-get p :to))
  7999. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8000. (cons "%s" (plist-get p :subject))
  8001. (cons "%d" (plist-get p :date))
  8002. (cons "%m" (plist-get p :message-id)))))
  8003. (when (string-match "%c" fmt)
  8004. ;; Check if the user wrote this message
  8005. (if (and org-from-is-user-regexp from to
  8006. (save-match-data (string-match org-from-is-user-regexp from)))
  8007. (setq fmt (replace-match "to %t" t t fmt))
  8008. (setq fmt (replace-match "from %f" t t fmt))))
  8009. (org-replace-escapes fmt table)))
  8010. (defun org-make-org-heading-search-string (&optional string heading)
  8011. "Make search string for STRING or current headline."
  8012. (interactive)
  8013. (let ((s (or string (org-get-heading)))
  8014. (lines org-context-in-file-links))
  8015. (unless (and string (not heading))
  8016. ;; We are using a headline, clean up garbage in there.
  8017. (if (string-match org-todo-regexp s)
  8018. (setq s (replace-match "" t t s)))
  8019. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8020. (setq s (replace-match "" t t s)))
  8021. (setq s (org-trim s))
  8022. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8023. org-comment-string "\\)") s)
  8024. (setq s (replace-match "" t t s)))
  8025. (while (string-match org-ts-regexp s)
  8026. (setq s (replace-match "" t t s))))
  8027. (or string (setq s (concat "*" s))) ; Add * for headlines
  8028. (when (and string (integerp lines) (> lines 0))
  8029. (let ((slines (org-split-string s "\n")))
  8030. (when (< lines (length slines))
  8031. (setq s (mapconcat
  8032. 'identity
  8033. (reverse (nthcdr (- (length slines) lines)
  8034. (reverse slines))) "\n")))))
  8035. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8036. (defun org-make-link (&rest strings)
  8037. "Concatenate STRINGS."
  8038. (apply 'concat strings))
  8039. (defun org-make-link-string (link &optional description)
  8040. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8041. (unless (string-match "\\S-" link)
  8042. (error "Empty link"))
  8043. (when (and description
  8044. (stringp description)
  8045. (not (string-match "\\S-" description)))
  8046. (setq description nil))
  8047. (when (stringp description)
  8048. ;; Remove brackets from the description, they are fatal.
  8049. (while (string-match "\\[" description)
  8050. (setq description (replace-match "{" t t description)))
  8051. (while (string-match "\\]" description)
  8052. (setq description (replace-match "}" t t description))))
  8053. (when (equal link description)
  8054. ;; No description needed, it is identical
  8055. (setq description nil))
  8056. (when (and (not description)
  8057. (not (string-match (org-image-file-name-regexp) link))
  8058. (not (equal link (org-link-escape link))))
  8059. (setq description (org-extract-attributes link)))
  8060. (setq link
  8061. (cond ((string-match (org-image-file-name-regexp) link) link)
  8062. ((string-match org-link-types-re link)
  8063. (concat (match-string 1 link)
  8064. (org-link-escape (substring link (match-end 1)))))
  8065. (t (org-link-escape link))))
  8066. (concat "[[" link "]"
  8067. (if description (concat "[" description "]") "")
  8068. "]"))
  8069. (defconst org-link-escape-chars
  8070. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8071. "List of characters that should be escaped in link.
  8072. This is the list that is used for internal purposes.")
  8073. (defvar org-url-encoding-use-url-hexify nil)
  8074. (defconst org-link-escape-chars-browser
  8075. '(?\ )
  8076. "List of escapes for characters that are problematic in links.
  8077. This is the list that is used before handing over to the browser.")
  8078. (defun org-link-escape (text &optional table merge)
  8079. "Return percent escaped representation of TEXT.
  8080. TEXT is a string with the text to escape.
  8081. Optional argument TABLE is a list with characters that should be
  8082. escaped. When nil, `org-link-escape-chars' is used.
  8083. If optional argument MERGE is set, merge TABLE into
  8084. `org-link-escape-chars'."
  8085. (if (and org-url-encoding-use-url-hexify (not table))
  8086. (url-hexify-string text)
  8087. (cond
  8088. ((and table merge)
  8089. (mapc (lambda (defchr)
  8090. (unless (member defchr table)
  8091. (setq table (cons defchr table)))) org-link-escape-chars))
  8092. ((null table)
  8093. (setq table org-link-escape-chars)))
  8094. (mapconcat
  8095. (lambda (char)
  8096. (if (or (member char table)
  8097. (< char 32) (= char 37) (> char 126))
  8098. (mapconcat (lambda (sequence-element)
  8099. (format "%%%.2X" sequence-element))
  8100. (or (encode-coding-char char 'utf-8)
  8101. (error "Unable to percent escape character: %s"
  8102. (char-to-string char))) "")
  8103. (char-to-string char))) text "")))
  8104. (defun org-link-unescape (str)
  8105. "Unhex hexified Unicode strings as returned from the JavaScript function
  8106. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8107. (unless (and (null str) (string= "" str))
  8108. (let ((pos 0) (case-fold-search t) unhexed)
  8109. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8110. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8111. (setq str (replace-match unhexed t t str))
  8112. (setq pos (+ pos (length unhexed))))))
  8113. str)
  8114. (defun org-link-unescape-compound (hex)
  8115. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8116. Note: this function also decodes single byte encodings like
  8117. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8118. (save-match-data
  8119. (let* ((bytes (cdr (split-string hex "%")))
  8120. (ret "")
  8121. (eat 0)
  8122. (sum 0))
  8123. (while bytes
  8124. (let* ((val (string-to-number (pop bytes) 16))
  8125. (shift-xor
  8126. (if (= 0 eat)
  8127. (cond
  8128. ((>= val 252) (cons 6 252))
  8129. ((>= val 248) (cons 5 248))
  8130. ((>= val 240) (cons 4 240))
  8131. ((>= val 224) (cons 3 224))
  8132. ((>= val 192) (cons 2 192))
  8133. (t (cons 0 0)))
  8134. (cons 6 128))))
  8135. (if (>= val 192) (setq eat (car shift-xor)))
  8136. (setq val (logxor val (cdr shift-xor)))
  8137. (setq sum (+ (lsh sum (car shift-xor)) val))
  8138. (if (> eat 0) (setq eat (- eat 1)))
  8139. (cond
  8140. ((= 0 eat) ;multi byte
  8141. (setq ret (concat ret (org-char-to-string sum)))
  8142. (setq sum 0))
  8143. ((not bytes) ; single byte(s)
  8144. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8145. )) ;; end (while bytes
  8146. ret )))
  8147. (defun org-link-unescape-single-byte-sequence (hex)
  8148. "Unhexify hex-encoded single byte character sequences."
  8149. (mapconcat (lambda (byte)
  8150. (char-to-string (string-to-number byte 16)))
  8151. (cdr (split-string hex "%")) ""))
  8152. (defun org-xor (a b)
  8153. "Exclusive or."
  8154. (if a (not b) b))
  8155. (defun org-fixup-message-id-for-http (s)
  8156. "Replace special characters in a message id, so it can be used in an http query."
  8157. (when (string-match "%" s)
  8158. (setq s (mapconcat (lambda (c)
  8159. (if (eq c ?%)
  8160. "%25"
  8161. (char-to-string c)))
  8162. s "")))
  8163. (while (string-match "<" s)
  8164. (setq s (replace-match "%3C" t t s)))
  8165. (while (string-match ">" s)
  8166. (setq s (replace-match "%3E" t t s)))
  8167. (while (string-match "@" s)
  8168. (setq s (replace-match "%40" t t s)))
  8169. s)
  8170. ;;;###autoload
  8171. (defun org-insert-link-global ()
  8172. "Insert a link like Org-mode does.
  8173. This command can be called in any mode to insert a link in Org-mode syntax."
  8174. (interactive)
  8175. (org-load-modules-maybe)
  8176. (org-run-like-in-org-mode 'org-insert-link))
  8177. (defun org-insert-link (&optional complete-file link-location default-description)
  8178. "Insert a link. At the prompt, enter the link.
  8179. Completion can be used to insert any of the link protocol prefixes like
  8180. http or ftp in use.
  8181. The history can be used to select a link previously stored with
  8182. `org-store-link'. When the empty string is entered (i.e. if you just
  8183. press RET at the prompt), the link defaults to the most recently
  8184. stored link. As SPC triggers completion in the minibuffer, you need to
  8185. use M-SPC or C-q SPC to force the insertion of a space character.
  8186. You will also be prompted for a description, and if one is given, it will
  8187. be displayed in the buffer instead of the link.
  8188. If there is already a link at point, this command will allow you to edit link
  8189. and description parts.
  8190. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8191. be selected using completion. The path to the file will be relative to the
  8192. current directory if the file is in the current directory or a subdirectory.
  8193. Otherwise, the link will be the absolute path as completed in the minibuffer
  8194. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8195. option `org-link-file-path-type'.
  8196. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8197. the current directory or below.
  8198. With three \\[universal-argument] prefixes, negate the meaning of
  8199. `org-keep-stored-link-after-insertion'.
  8200. If `org-make-link-description-function' is non-nil, this function will be
  8201. called with the link target, and the result will be the default
  8202. link description.
  8203. If the LINK-LOCATION parameter is non-nil, this value will be
  8204. used as the link location instead of reading one interactively.
  8205. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8206. be used as the default description."
  8207. (interactive "P")
  8208. (let* ((wcf (current-window-configuration))
  8209. (region (if (org-region-active-p)
  8210. (buffer-substring (region-beginning) (region-end))))
  8211. (remove (and region (list (region-beginning) (region-end))))
  8212. (desc region)
  8213. tmphist ; byte-compile incorrectly complains about this
  8214. (link link-location)
  8215. entry file all-prefixes)
  8216. (cond
  8217. (link-location) ; specified by arg, just use it.
  8218. ((org-in-regexp org-bracket-link-regexp 1)
  8219. ;; We do have a link at point, and we are going to edit it.
  8220. (setq remove (list (match-beginning 0) (match-end 0)))
  8221. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8222. (setq link (read-string "Link: "
  8223. (org-link-unescape
  8224. (org-match-string-no-properties 1)))))
  8225. ((or (org-in-regexp org-angle-link-re)
  8226. (org-in-regexp org-plain-link-re))
  8227. ;; Convert to bracket link
  8228. (setq remove (list (match-beginning 0) (match-end 0))
  8229. link (read-string "Link: "
  8230. (org-remove-angle-brackets (match-string 0)))))
  8231. ((member complete-file '((4) (16)))
  8232. ;; Completing read for file names.
  8233. (setq link (org-file-complete-link complete-file)))
  8234. (t
  8235. ;; Read link, with completion for stored links.
  8236. (with-output-to-temp-buffer "*Org Links*"
  8237. (princ "Insert a link.
  8238. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8239. (when org-stored-links
  8240. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8241. (princ (mapconcat
  8242. (lambda (x)
  8243. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8244. (reverse org-stored-links) "\n"))))
  8245. (let ((cw (selected-window)))
  8246. (select-window (get-buffer-window "*Org Links*" 'visible))
  8247. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8248. (unless (pos-visible-in-window-p (point-max))
  8249. (org-fit-window-to-buffer))
  8250. (and (window-live-p cw) (select-window cw)))
  8251. ;; Fake a link history, containing the stored links.
  8252. (setq tmphist (append (mapcar 'car org-stored-links)
  8253. org-insert-link-history))
  8254. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8255. (mapcar 'car org-link-abbrev-alist)
  8256. org-link-types))
  8257. (unwind-protect
  8258. (progn
  8259. (setq link
  8260. (let ((org-completion-use-ido nil)
  8261. (org-completion-use-iswitchb nil))
  8262. (org-completing-read
  8263. "Link: "
  8264. (append
  8265. (mapcar (lambda (x) (list (concat x ":")))
  8266. all-prefixes)
  8267. (mapcar 'car org-stored-links))
  8268. nil nil nil
  8269. 'tmphist
  8270. (car (car org-stored-links)))))
  8271. (if (not (string-match "\\S-" link))
  8272. (error "No link selected"))
  8273. (if (or (member link all-prefixes)
  8274. (and (equal ":" (substring link -1))
  8275. (member (substring link 0 -1) all-prefixes)
  8276. (setq link (substring link 0 -1))))
  8277. (setq link (org-link-try-special-completion link))))
  8278. (set-window-configuration wcf)
  8279. (kill-buffer "*Org Links*"))
  8280. (setq entry (assoc link org-stored-links))
  8281. (or entry (push link org-insert-link-history))
  8282. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8283. (not org-keep-stored-link-after-insertion))
  8284. (setq org-stored-links (delq (assoc link org-stored-links)
  8285. org-stored-links)))
  8286. (setq desc (or desc (nth 1 entry)))))
  8287. (if (string-match org-plain-link-re link)
  8288. ;; URL-like link, normalize the use of angular brackets.
  8289. (setq link (org-make-link (org-remove-angle-brackets link))))
  8290. ;; Check if we are linking to the current file with a search option
  8291. ;; If yes, simplify the link by using only the search option.
  8292. (when (and buffer-file-name
  8293. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8294. (let* ((path (match-string 1 link))
  8295. (case-fold-search nil)
  8296. (search (match-string 2 link)))
  8297. (save-match-data
  8298. (if (equal (file-truename buffer-file-name) (file-truename path))
  8299. ;; We are linking to this same file, with a search option
  8300. (setq link search)))))
  8301. ;; Check if we can/should use a relative path. If yes, simplify the link
  8302. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8303. (let* ((type (match-string 1 link))
  8304. (path (match-string 2 link))
  8305. (origpath path)
  8306. (case-fold-search nil))
  8307. (cond
  8308. ((or (eq org-link-file-path-type 'absolute)
  8309. (equal complete-file '(16)))
  8310. (setq path (abbreviate-file-name (expand-file-name path))))
  8311. ((eq org-link-file-path-type 'noabbrev)
  8312. (setq path (expand-file-name path)))
  8313. ((eq org-link-file-path-type 'relative)
  8314. (setq path (file-relative-name path)))
  8315. (t
  8316. (save-match-data
  8317. (if (string-match (concat "^" (regexp-quote
  8318. (expand-file-name
  8319. (file-name-as-directory
  8320. default-directory))))
  8321. (expand-file-name path))
  8322. ;; We are linking a file with relative path name.
  8323. (setq path (substring (expand-file-name path)
  8324. (match-end 0)))
  8325. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8326. (setq link (concat type path))
  8327. (if (equal desc origpath)
  8328. (setq desc path))))
  8329. (if org-make-link-description-function
  8330. (setq desc (funcall org-make-link-description-function link desc))
  8331. (if default-description (setq desc default-description)))
  8332. (setq desc (read-string "Description: " desc))
  8333. (unless (string-match "\\S-" desc) (setq desc nil))
  8334. (if remove (apply 'delete-region remove))
  8335. (insert (org-make-link-string link desc))))
  8336. (defun org-link-try-special-completion (type)
  8337. "If there is completion support for link type TYPE, offer it."
  8338. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8339. (if (functionp fun)
  8340. (funcall fun)
  8341. (read-string "Link (no completion support): " (concat type ":")))))
  8342. (defun org-file-complete-link (&optional arg)
  8343. "Create a file link using completion."
  8344. (let (file link)
  8345. (setq file (read-file-name "File: "))
  8346. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8347. (pwd1 (file-name-as-directory (abbreviate-file-name
  8348. (expand-file-name ".")))))
  8349. (cond
  8350. ((equal arg '(16))
  8351. (setq link (org-make-link
  8352. "file:"
  8353. (abbreviate-file-name (expand-file-name file)))))
  8354. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8355. (setq link (org-make-link "file:" (match-string 1 file))))
  8356. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8357. (expand-file-name file))
  8358. (setq link (org-make-link
  8359. "file:" (match-string 1 (expand-file-name file)))))
  8360. (t (setq link (org-make-link "file:" file)))))
  8361. link))
  8362. (defun org-completing-read (&rest args)
  8363. "Completing-read with SPACE being a normal character."
  8364. (let ((enable-recursive-minibuffers t)
  8365. (minibuffer-local-completion-map
  8366. (copy-keymap minibuffer-local-completion-map)))
  8367. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8368. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8369. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8370. (apply 'org-icompleting-read args)))
  8371. (defun org-completing-read-no-i (&rest args)
  8372. (let (org-completion-use-ido org-completion-use-iswitchb)
  8373. (apply 'org-completing-read args)))
  8374. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8375. "Use iswitch as a completing-read replacement to choose from choices.
  8376. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8377. from."
  8378. (let* ((iswitchb-use-virtual-buffers nil)
  8379. (iswitchb-make-buflist-hook
  8380. (lambda ()
  8381. (setq iswitchb-temp-buflist choices))))
  8382. (iswitchb-read-buffer prompt)))
  8383. (defun org-icompleting-read (&rest args)
  8384. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8385. (org-without-partial-completion
  8386. (if (and org-completion-use-ido
  8387. (fboundp 'ido-completing-read)
  8388. (boundp 'ido-mode) ido-mode
  8389. (listp (second args)))
  8390. (let ((ido-enter-matching-directory nil))
  8391. (apply 'ido-completing-read (concat (car args))
  8392. (if (consp (car (nth 1 args)))
  8393. (mapcar 'car (nth 1 args))
  8394. (nth 1 args))
  8395. (cddr args)))
  8396. (if (and org-completion-use-iswitchb
  8397. (boundp 'iswitchb-mode) iswitchb-mode
  8398. (listp (second args)))
  8399. (apply 'org-iswitchb-completing-read (concat (car args))
  8400. (if (consp (car (nth 1 args)))
  8401. (mapcar 'car (nth 1 args))
  8402. (nth 1 args))
  8403. (cddr args))
  8404. (apply 'completing-read args)))))
  8405. (defun org-extract-attributes (s)
  8406. "Extract the attributes cookie from a string and set as text property."
  8407. (let (a attr (start 0) key value)
  8408. (save-match-data
  8409. (when (string-match "{{\\([^}]+\\)}}$" s)
  8410. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8411. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8412. (setq key (match-string 1 a) value (match-string 2 a)
  8413. start (match-end 0)
  8414. attr (plist-put attr (intern key) value))))
  8415. (org-add-props s nil 'org-attr attr))
  8416. s))
  8417. (defun org-extract-attributes-from-string (tag)
  8418. (let (key value attr)
  8419. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8420. (setq key (match-string 1 tag) value (match-string 2 tag)
  8421. tag (replace-match "" t t tag)
  8422. attr (plist-put attr (intern key) value)))
  8423. (cons tag attr)))
  8424. (defun org-attributes-to-string (plist)
  8425. "Format a property list into an HTML attribute list."
  8426. (let ((s "") key value)
  8427. (while plist
  8428. (setq key (pop plist) value (pop plist))
  8429. (and value
  8430. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8431. s))
  8432. ;;; Opening/following a link
  8433. (defvar org-link-search-failed nil)
  8434. (defvar org-open-link-functions nil
  8435. "Hook for functions finding a plain text link.
  8436. These functions must take a single argument, the link content.
  8437. They will be called for links that look like [[link text][description]]
  8438. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8439. like a filename (e.g. \"./blue.png\").
  8440. These functions will be called *before* Org attempts to resolve the
  8441. link by doing text searches in the current buffer - so if you want a
  8442. link \"[[target]]\" to still find \"<<target>>\", your function should
  8443. handle this as a special case.
  8444. When the function does handle the link, it must return a non-nil value.
  8445. If it decides that it is not responsible for this link, it must return
  8446. nil to indicate that that Org-mode can continue with other options
  8447. like exact and fuzzy text search.")
  8448. (defun org-next-link ()
  8449. "Move forward to the next link.
  8450. If the link is in hidden text, expose it."
  8451. (interactive)
  8452. (when (and org-link-search-failed (eq this-command last-command))
  8453. (goto-char (point-min))
  8454. (message "Link search wrapped back to beginning of buffer"))
  8455. (setq org-link-search-failed nil)
  8456. (let* ((pos (point))
  8457. (ct (org-context))
  8458. (a (assoc :link ct)))
  8459. (if a (goto-char (nth 2 a)))
  8460. (if (re-search-forward org-any-link-re nil t)
  8461. (progn
  8462. (goto-char (match-beginning 0))
  8463. (if (outline-invisible-p) (org-show-context)))
  8464. (goto-char pos)
  8465. (setq org-link-search-failed t)
  8466. (error "No further link found"))))
  8467. (defun org-previous-link ()
  8468. "Move backward to the previous link.
  8469. If the link is in hidden text, expose it."
  8470. (interactive)
  8471. (when (and org-link-search-failed (eq this-command last-command))
  8472. (goto-char (point-max))
  8473. (message "Link search wrapped back to end of buffer"))
  8474. (setq org-link-search-failed nil)
  8475. (let* ((pos (point))
  8476. (ct (org-context))
  8477. (a (assoc :link ct)))
  8478. (if a (goto-char (nth 1 a)))
  8479. (if (re-search-backward org-any-link-re nil t)
  8480. (progn
  8481. (goto-char (match-beginning 0))
  8482. (if (outline-invisible-p) (org-show-context)))
  8483. (goto-char pos)
  8484. (setq org-link-search-failed t)
  8485. (error "No further link found"))))
  8486. (defun org-translate-link (s)
  8487. "Translate a link string if a translation function has been defined."
  8488. (if (and org-link-translation-function
  8489. (fboundp org-link-translation-function)
  8490. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8491. (progn
  8492. (setq s (funcall org-link-translation-function
  8493. (match-string 1) (match-string 2)))
  8494. (concat (car s) ":" (cdr s)))
  8495. s))
  8496. (defun org-translate-link-from-planner (type path)
  8497. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8498. This is still an experimental function, your mileage may vary."
  8499. (cond
  8500. ((member type '("http" "https" "news" "ftp"))
  8501. ;; standard Internet links are the same.
  8502. nil)
  8503. ((and (equal type "irc") (string-match "^//" path))
  8504. ;; Planner has two / at the beginning of an irc link, we have 1.
  8505. ;; We should have zero, actually....
  8506. (setq path (substring path 1)))
  8507. ((and (equal type "lisp") (string-match "^/" path))
  8508. ;; Planner has a slash, we do not.
  8509. (setq type "elisp" path (substring path 1)))
  8510. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8511. ;; A typical message link. Planner has the id after the final slash,
  8512. ;; we separate it with a hash mark
  8513. (setq path (concat (match-string 1 path) "#"
  8514. (org-remove-angle-brackets (match-string 2 path)))))
  8515. )
  8516. (cons type path))
  8517. (defun org-find-file-at-mouse (ev)
  8518. "Open file link or URL at mouse."
  8519. (interactive "e")
  8520. (mouse-set-point ev)
  8521. (org-open-at-point 'in-emacs))
  8522. (defun org-open-at-mouse (ev)
  8523. "Open file link or URL at mouse.
  8524. See the docstring of `org-open-file' for details."
  8525. (interactive "e")
  8526. (mouse-set-point ev)
  8527. (if (eq major-mode 'org-agenda-mode)
  8528. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8529. (org-open-at-point))
  8530. (defvar org-window-config-before-follow-link nil
  8531. "The window configuration before following a link.
  8532. This is saved in case the need arises to restore it.")
  8533. (defvar org-open-link-marker (make-marker)
  8534. "Marker pointing to the location where `org-open-at-point; was called.")
  8535. ;;;###autoload
  8536. (defun org-open-at-point-global ()
  8537. "Follow a link like Org-mode does.
  8538. This command can be called in any mode to follow a link that has
  8539. Org-mode syntax."
  8540. (interactive)
  8541. (org-run-like-in-org-mode 'org-open-at-point))
  8542. ;;;###autoload
  8543. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8544. "Open a link in the string S, as if it was in Org-mode."
  8545. (interactive "sLink: \nP")
  8546. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8547. (with-temp-buffer
  8548. (let ((org-inhibit-startup (not reference-buffer)))
  8549. (org-mode)
  8550. (insert s)
  8551. (goto-char (point-min))
  8552. (when reference-buffer
  8553. (setq org-link-abbrev-alist-local
  8554. (with-current-buffer reference-buffer
  8555. org-link-abbrev-alist-local)))
  8556. (org-open-at-point arg reference-buffer)))))
  8557. (defvar org-open-at-point-functions nil
  8558. "Hook that is run when following a link at point.
  8559. Functions in this hook must return t if they identify and follow
  8560. a link at point. If they don't find anything interesting at point,
  8561. they must return nil.")
  8562. (defun org-open-at-point (&optional arg reference-buffer)
  8563. "Open link at or after point.
  8564. If there is no link at point, this function will search forward up to
  8565. the end of the current line.
  8566. Normally, files will be opened by an appropriate application. If the
  8567. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8568. With a double prefix argument, try to open outside of Emacs, in the
  8569. application the system uses for this file type."
  8570. (interactive "P")
  8571. ;; if in a code block, then open the block's results
  8572. (unless (call-interactively #'org-babel-open-src-block-result)
  8573. (org-load-modules-maybe)
  8574. (move-marker org-open-link-marker (point))
  8575. (setq org-window-config-before-follow-link (current-window-configuration))
  8576. (org-remove-occur-highlights nil nil t)
  8577. (cond
  8578. ((and (org-at-heading-p)
  8579. (not (org-in-regexp
  8580. (concat org-plain-link-re "\\|"
  8581. org-bracket-link-regexp "\\|"
  8582. org-angle-link-re "\\|"
  8583. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8584. (not (get-text-property (point) 'org-linked-text)))
  8585. (or (org-offer-links-in-entry arg)
  8586. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8587. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8588. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8589. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8590. (not (org-in-regexp org-bracket-link-regexp)))
  8591. (org-footnote-action))
  8592. (t
  8593. (let (type path link line search (pos (point)))
  8594. (catch 'match
  8595. (save-excursion
  8596. (skip-chars-forward "^]\n\r")
  8597. (when (org-in-regexp org-bracket-link-regexp 1)
  8598. (setq link (org-extract-attributes
  8599. (org-link-unescape (org-match-string-no-properties 1))))
  8600. (while (string-match " *\n *" link)
  8601. (setq link (replace-match " " t t link)))
  8602. (setq link (org-link-expand-abbrev link))
  8603. (cond
  8604. ((or (file-name-absolute-p link)
  8605. (string-match "^\\.\\.?/" link))
  8606. (setq type "file" path link))
  8607. ((string-match org-link-re-with-space3 link)
  8608. (setq type (match-string 1 link) path (match-string 2 link)))
  8609. (t (setq type "thisfile" path link)))
  8610. (throw 'match t)))
  8611. (when (get-text-property (point) 'org-linked-text)
  8612. (setq type "thisfile"
  8613. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8614. (1+ (point)) (point))
  8615. path (buffer-substring
  8616. (or (previous-single-property-change pos 'org-linked-text)
  8617. (point-min))
  8618. (or (next-single-property-change pos 'org-linked-text)
  8619. (point-max))))
  8620. (throw 'match t))
  8621. (save-excursion
  8622. (when (or (org-in-regexp org-angle-link-re)
  8623. (org-in-regexp org-plain-link-re))
  8624. (setq type (match-string 1)
  8625. path (org-link-unescape (match-string 2)))
  8626. (throw 'match t)))
  8627. (save-excursion
  8628. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8629. (setq type "tags"
  8630. path (match-string 1))
  8631. (while (string-match ":" path)
  8632. (setq path (replace-match "+" t t path)))
  8633. (throw 'match t)))
  8634. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8635. (setq type "tree-match"
  8636. path (match-string 1))
  8637. (throw 'match t)))
  8638. (unless path
  8639. (error "No link found"))
  8640. ;; switch back to reference buffer
  8641. ;; needed when if called in a temporary buffer through
  8642. ;; org-open-link-from-string
  8643. (with-current-buffer (or reference-buffer (current-buffer))
  8644. ;; Remove any trailing spaces in path
  8645. (if (string-match " +\\'" path)
  8646. (setq path (replace-match "" t t path)))
  8647. (if (and org-link-translation-function
  8648. (fboundp org-link-translation-function))
  8649. ;; Check if we need to translate the link
  8650. (let ((tmp (funcall org-link-translation-function type path)))
  8651. (setq type (car tmp) path (cdr tmp))))
  8652. (cond
  8653. ((assoc type org-link-protocols)
  8654. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8655. ((equal type "mailto")
  8656. (let ((cmd (car org-link-mailto-program))
  8657. (args (cdr org-link-mailto-program)) args1
  8658. (address path) (subject "") a)
  8659. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8660. (setq address (match-string 1 path)
  8661. subject (org-link-escape (match-string 2 path))))
  8662. (while args
  8663. (cond
  8664. ((not (stringp (car args))) (push (pop args) args1))
  8665. (t (setq a (pop args))
  8666. (if (string-match "%a" a)
  8667. (setq a (replace-match address t t a)))
  8668. (if (string-match "%s" a)
  8669. (setq a (replace-match subject t t a)))
  8670. (push a args1))))
  8671. (apply cmd (nreverse args1))))
  8672. ((member type '("http" "https" "ftp" "news"))
  8673. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8674. (org-link-escape
  8675. path org-link-escape-chars-browser)
  8676. path))))
  8677. ((string= type "doi")
  8678. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8679. (org-link-escape
  8680. path org-link-escape-chars-browser)
  8681. path))))
  8682. ((member type '("message"))
  8683. (browse-url (concat type ":" path)))
  8684. ((string= type "tags")
  8685. (org-tags-view arg path))
  8686. ((string= type "tree-match")
  8687. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8688. ((string= type "file")
  8689. (if (string-match "::\\([0-9]+\\)\\'" path)
  8690. (setq line (string-to-number (match-string 1 path))
  8691. path (substring path 0 (match-beginning 0)))
  8692. (if (string-match "::\\(.+\\)\\'" path)
  8693. (setq search (match-string 1 path)
  8694. path (substring path 0 (match-beginning 0)))))
  8695. (if (string-match "[*?{]" (file-name-nondirectory path))
  8696. (dired path)
  8697. (org-open-file path arg line search)))
  8698. ((string= type "shell")
  8699. (let ((cmd path))
  8700. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8701. (string-match org-confirm-shell-link-not-regexp cmd))
  8702. (not org-confirm-shell-link-function)
  8703. (funcall org-confirm-shell-link-function
  8704. (format "Execute \"%s\" in shell? "
  8705. (org-add-props cmd nil
  8706. 'face 'org-warning))))
  8707. (progn
  8708. (message "Executing %s" cmd)
  8709. (shell-command cmd))
  8710. (error "Abort"))))
  8711. ((string= type "elisp")
  8712. (let ((cmd path))
  8713. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8714. (string-match org-confirm-elisp-link-not-regexp cmd))
  8715. (not org-confirm-elisp-link-function)
  8716. (funcall org-confirm-elisp-link-function
  8717. (format "Execute \"%s\" as elisp? "
  8718. (org-add-props cmd nil
  8719. 'face 'org-warning))))
  8720. (message "%s => %s" cmd
  8721. (if (equal (string-to-char cmd) ?\()
  8722. (eval (read cmd))
  8723. (call-interactively (read cmd))))
  8724. (error "Abort"))))
  8725. ((and (string= type "thisfile")
  8726. (run-hook-with-args-until-success
  8727. 'org-open-link-functions path)))
  8728. ((string= type "thisfile")
  8729. (if arg
  8730. (switch-to-buffer-other-window
  8731. (org-get-buffer-for-internal-link (current-buffer)))
  8732. (org-mark-ring-push))
  8733. (let ((cmd `(org-link-search
  8734. ,path
  8735. ,(cond ((equal arg '(4)) ''occur)
  8736. ((equal arg '(16)) ''org-occur)
  8737. (t nil))
  8738. ,pos)))
  8739. (condition-case nil (let ((org-link-search-inhibit-query t))
  8740. (eval cmd))
  8741. (error (progn (widen) (eval cmd))))))
  8742. (t
  8743. (browse-url-at-point)))))))
  8744. (move-marker org-open-link-marker nil)
  8745. (run-hook-with-args 'org-follow-link-hook)))
  8746. (defun org-offer-links-in-entry (&optional nth zero)
  8747. "Offer links in the current entry and follow the selected link.
  8748. If there is only one link, follow it immediately as well.
  8749. If NTH is an integer, immediately pick the NTH link found.
  8750. If ZERO is a string, check also this string for a link, and if
  8751. there is one, offer it as link number zero."
  8752. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8753. "\\(" org-angle-link-re "\\)\\|"
  8754. "\\(" org-plain-link-re "\\)"))
  8755. (cnt ?0)
  8756. (in-emacs (if (integerp nth) nil nth))
  8757. have-zero end links link c)
  8758. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8759. (push (match-string 0 zero) links)
  8760. (setq cnt (1- cnt) have-zero t))
  8761. (save-excursion
  8762. (org-back-to-heading t)
  8763. (setq end (save-excursion (outline-next-heading) (point)))
  8764. (while (re-search-forward re end t)
  8765. (push (match-string 0) links))
  8766. (setq links (org-uniquify (reverse links))))
  8767. (cond
  8768. ((null links)
  8769. (message "No links"))
  8770. ((equal (length links) 1)
  8771. (setq link (list (car links))))
  8772. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8773. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8774. (t ; we have to select a link
  8775. (save-excursion
  8776. (save-window-excursion
  8777. (delete-other-windows)
  8778. (with-output-to-temp-buffer "*Select Link*"
  8779. (mapc (lambda (l)
  8780. (if (not (string-match org-bracket-link-regexp l))
  8781. (princ (format "[%c] %s\n" (incf cnt)
  8782. (org-remove-angle-brackets l)))
  8783. (if (match-end 3)
  8784. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8785. (match-string 3 l) (match-string 1 l)))
  8786. (princ (format "[%c] %s\n" (incf cnt)
  8787. (match-string 1 l))))))
  8788. links))
  8789. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8790. (message "Select link to open, RET to open all:")
  8791. (setq c (read-char-exclusive))
  8792. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8793. (when (equal c ?q) (error "Abort"))
  8794. (if (equal c ?\C-m)
  8795. (setq link links)
  8796. (setq nth (- c ?0))
  8797. (if have-zero (setq nth (1+ nth)))
  8798. (unless (and (integerp nth) (>= (length links) nth))
  8799. (error "Invalid link selection"))
  8800. (setq link (list (nth (1- nth) links))))))
  8801. (if link
  8802. (let ((buf (current-buffer)))
  8803. (dolist (l link)
  8804. (org-open-link-from-string l in-emacs buf))
  8805. t)
  8806. nil)))
  8807. ;; Add special file links that specify the way of opening
  8808. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8809. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8810. (defun org-open-file-with-system (path)
  8811. "Open file at PATH using the system way of opening it."
  8812. (org-open-file path 'system))
  8813. (defun org-open-file-with-emacs (path)
  8814. "Open file at PATH in Emacs."
  8815. (org-open-file path 'emacs))
  8816. (defun org-remove-file-link-modifiers ()
  8817. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8818. (goto-char (point-min))
  8819. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8820. (org-if-unprotected
  8821. (replace-match "file:" t t))))
  8822. (eval-after-load "org-exp"
  8823. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8824. 'org-remove-file-link-modifiers))
  8825. ;;;; Time estimates
  8826. (defun org-get-effort (&optional pom)
  8827. "Get the effort estimate for the current entry."
  8828. (org-entry-get pom org-effort-property))
  8829. ;;; File search
  8830. (defvar org-create-file-search-functions nil
  8831. "List of functions to construct the right search string for a file link.
  8832. These functions are called in turn with point at the location to
  8833. which the link should point.
  8834. A function in the hook should first test if it would like to
  8835. handle this file type, for example by checking the `major-mode'
  8836. or the file extension. If it decides not to handle this file, it
  8837. should just return nil to give other functions a chance. If it
  8838. does handle the file, it must return the search string to be used
  8839. when following the link. The search string will be part of the
  8840. file link, given after a double colon, and `org-open-at-point'
  8841. will automatically search for it. If special measures must be
  8842. taken to make the search successful, another function should be
  8843. added to the companion hook `org-execute-file-search-functions',
  8844. which see.
  8845. A function in this hook may also use `setq' to set the variable
  8846. `description' to provide a suggestion for the descriptive text to
  8847. be used for this link when it gets inserted into an Org-mode
  8848. buffer with \\[org-insert-link].")
  8849. (defvar org-execute-file-search-functions nil
  8850. "List of functions to execute a file search triggered by a link.
  8851. Functions added to this hook must accept a single argument, the
  8852. search string that was part of the file link, the part after the
  8853. double colon. The function must first check if it would like to
  8854. handle this search, for example by checking the `major-mode' or
  8855. the file extension. If it decides not to handle this search, it
  8856. should just return nil to give other functions a chance. If it
  8857. does handle the search, it must return a non-nil value to keep
  8858. other functions from trying.
  8859. Each function can access the current prefix argument through the
  8860. variable `current-prefix-argument'. Note that a single prefix is
  8861. used to force opening a link in Emacs, so it may be good to only
  8862. use a numeric or double prefix to guide the search function.
  8863. In case this is needed, a function in this hook can also restore
  8864. the window configuration before `org-open-at-point' was called using:
  8865. (set-window-configuration org-window-config-before-follow-link)")
  8866. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8867. (defun org-link-search (s &optional type avoid-pos stealth)
  8868. "Search for a link search option.
  8869. If S is surrounded by forward slashes, it is interpreted as a
  8870. regular expression. In org-mode files, this will create an `org-occur'
  8871. sparse tree. In ordinary files, `occur' will be used to list matches.
  8872. If the current buffer is in `dired-mode', grep will be used to search
  8873. in all files. If AVOID-POS is given, ignore matches near that position.
  8874. When optional argument STEALTH is non-nil, do not modify
  8875. visibility around point, thus ignoring
  8876. `org-show-hierarchy-above', `org-show-following-heading' and
  8877. `org-show-siblings' variables."
  8878. (let ((case-fold-search t)
  8879. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8880. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8881. (append '(("") (" ") ("\t") ("\n"))
  8882. org-emphasis-alist)
  8883. "\\|") "\\)"))
  8884. (pos (point))
  8885. (pre nil) (post nil)
  8886. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8887. (cond
  8888. ;; First check if there are any special search functions
  8889. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8890. ;; Now try the builtin stuff
  8891. ((and (equal (string-to-char s0) ?#)
  8892. (> (length s0) 1)
  8893. (save-excursion
  8894. (goto-char (point-min))
  8895. (and
  8896. (re-search-forward
  8897. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8898. (setq type 'dedicated
  8899. pos (match-beginning 0))))
  8900. ;; There is an exact target for this
  8901. (goto-char pos)
  8902. (org-back-to-heading t)))
  8903. ((save-excursion
  8904. (goto-char (point-min))
  8905. (and
  8906. (re-search-forward
  8907. (concat "<<" (regexp-quote s0) ">>") nil t)
  8908. (setq type 'dedicated
  8909. pos (match-beginning 0))))
  8910. ;; There is an exact target for this
  8911. (goto-char pos))
  8912. ((save-excursion
  8913. (goto-char (point-min))
  8914. (and
  8915. (re-search-forward
  8916. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  8917. (setq type 'dedicated pos (match-beginning 0))))
  8918. ;; Found an invisible target.
  8919. (goto-char pos))
  8920. ((save-excursion
  8921. (goto-char (point-min))
  8922. (and
  8923. (re-search-forward
  8924. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  8925. (setq type 'dedicated pos (match-beginning 0))))
  8926. ;; Found an element with a matching #+name affiliated keyword.
  8927. (goto-char pos))
  8928. ((and (string-match "^(\\(.*\\))$" s0)
  8929. (save-excursion
  8930. (goto-char (point-min))
  8931. (and
  8932. (re-search-forward
  8933. (concat "[^[]" (regexp-quote
  8934. (format org-coderef-label-format
  8935. (match-string 1 s0))))
  8936. nil t)
  8937. (setq type 'dedicated
  8938. pos (1+ (match-beginning 0))))))
  8939. ;; There is a coderef target for this
  8940. (goto-char pos))
  8941. ((string-match "^/\\(.*\\)/$" s)
  8942. ;; A regular expression
  8943. (cond
  8944. ((eq major-mode 'org-mode)
  8945. (org-occur (match-string 1 s)))
  8946. ;;((eq major-mode 'dired-mode)
  8947. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8948. (t (org-do-occur (match-string 1 s)))))
  8949. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8950. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8951. (goto-char (point-min))
  8952. (cond
  8953. ((let (case-fold-search)
  8954. (re-search-forward (format org-complex-heading-regexp-format
  8955. (regexp-quote s))
  8956. nil t))
  8957. ;; OK, found a match
  8958. (setq type 'dedicated)
  8959. (goto-char (match-beginning 0)))
  8960. ((and (not org-link-search-inhibit-query)
  8961. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8962. (y-or-n-p "No match - create this as a new heading? "))
  8963. (goto-char (point-max))
  8964. (or (bolp) (newline))
  8965. (insert "* " s "\n")
  8966. (beginning-of-line 0))
  8967. (t
  8968. (goto-char pos)
  8969. (error "No match"))))
  8970. (t
  8971. ;; A normal search string
  8972. (when (equal (string-to-char s) ?*)
  8973. ;; Anchor on headlines, post may include tags.
  8974. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8975. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8976. s (substring s 1)))
  8977. (remove-text-properties
  8978. 0 (length s)
  8979. '(face nil mouse-face nil keymap nil fontified nil) s)
  8980. ;; Make a series of regular expressions to find a match
  8981. (setq words (org-split-string s "[ \n\r\t]+")
  8982. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8983. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8984. "\\)" markers)
  8985. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8986. re2a (concat "[ \t\r\n]" re2a_)
  8987. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8988. re4 (concat "[^a-zA-Z_]" re4_)
  8989. re1 (concat pre re2 post)
  8990. re3 (concat pre (if pre re4_ re4) post)
  8991. re5 (concat pre ".*" re4)
  8992. re2 (concat pre re2)
  8993. re2a (concat pre (if pre re2a_ re2a))
  8994. re4 (concat pre (if pre re4_ re4))
  8995. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8996. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8997. re5 "\\)"
  8998. ))
  8999. (cond
  9000. ((eq type 'org-occur) (org-occur reall))
  9001. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9002. (t (goto-char (point-min))
  9003. (setq type 'fuzzy)
  9004. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9005. (org-search-not-self 1 re1 nil t)
  9006. (org-search-not-self 1 re2 nil t)
  9007. (org-search-not-self 1 re2a nil t)
  9008. (org-search-not-self 1 re3 nil t)
  9009. (org-search-not-self 1 re4 nil t)
  9010. (org-search-not-self 1 re5 nil t)
  9011. )
  9012. (goto-char (match-beginning 1))
  9013. (goto-char pos)
  9014. (error "No match"))))))
  9015. (and (eq major-mode 'org-mode)
  9016. (not stealth)
  9017. (org-show-context 'link-search))
  9018. type))
  9019. (defun org-search-not-self (group &rest args)
  9020. "Execute `re-search-forward', but only accept matches that do not
  9021. enclose the position of `org-open-link-marker'."
  9022. (let ((m org-open-link-marker))
  9023. (catch 'exit
  9024. (while (apply 're-search-forward args)
  9025. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9026. (goto-char (match-end group))
  9027. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9028. (> (match-beginning 0) (marker-position m))
  9029. (< (match-end 0) (marker-position m)))
  9030. (save-match-data
  9031. (or (not (org-in-regexp
  9032. org-bracket-link-analytic-regexp 1))
  9033. (not (match-end 4)) ; no description
  9034. (and (<= (match-beginning 4) (point))
  9035. (>= (match-end 4) (point))))))
  9036. (throw 'exit (point))))))))
  9037. (defun org-get-buffer-for-internal-link (buffer)
  9038. "Return a buffer to be used for displaying the link target of internal links."
  9039. (cond
  9040. ((not org-display-internal-link-with-indirect-buffer)
  9041. buffer)
  9042. ((string-match "(Clone)$" (buffer-name buffer))
  9043. (message "Buffer is already a clone, not making another one")
  9044. ;; we also do not modify visibility in this case
  9045. buffer)
  9046. (t ; make a new indirect buffer for displaying the link
  9047. (let* ((bn (buffer-name buffer))
  9048. (ibn (concat bn "(Clone)"))
  9049. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9050. (with-current-buffer ib (org-overview))
  9051. ib))))
  9052. (defun org-do-occur (regexp &optional cleanup)
  9053. "Call the Emacs command `occur'.
  9054. If CLEANUP is non-nil, remove the printout of the regular expression
  9055. in the *Occur* buffer. This is useful if the regex is long and not useful
  9056. to read."
  9057. (occur regexp)
  9058. (when cleanup
  9059. (let ((cwin (selected-window)) win beg end)
  9060. (when (setq win (get-buffer-window "*Occur*"))
  9061. (select-window win))
  9062. (goto-char (point-min))
  9063. (when (re-search-forward "match[a-z]+" nil t)
  9064. (setq beg (match-end 0))
  9065. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9066. (setq end (1- (match-beginning 0)))))
  9067. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9068. (goto-char (point-min))
  9069. (select-window cwin))))
  9070. ;;; The mark ring for links jumps
  9071. (defvar org-mark-ring nil
  9072. "Mark ring for positions before jumps in Org-mode.")
  9073. (defvar org-mark-ring-last-goto nil
  9074. "Last position in the mark ring used to go back.")
  9075. ;; Fill and close the ring
  9076. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9077. (loop for i from 1 to org-mark-ring-length do
  9078. (push (make-marker) org-mark-ring))
  9079. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9080. org-mark-ring)
  9081. (defun org-mark-ring-push (&optional pos buffer)
  9082. "Put the current position or POS into the mark ring and rotate it."
  9083. (interactive)
  9084. (setq pos (or pos (point)))
  9085. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9086. (move-marker (car org-mark-ring)
  9087. (or pos (point))
  9088. (or buffer (current-buffer)))
  9089. (message "%s"
  9090. (substitute-command-keys
  9091. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9092. (defun org-mark-ring-goto (&optional n)
  9093. "Jump to the previous position in the mark ring.
  9094. With prefix arg N, jump back that many stored positions. When
  9095. called several times in succession, walk through the entire ring.
  9096. Org-mode commands jumping to a different position in the current file,
  9097. or to another Org-mode file, automatically push the old position
  9098. onto the ring."
  9099. (interactive "p")
  9100. (let (p m)
  9101. (if (eq last-command this-command)
  9102. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9103. (setq p org-mark-ring))
  9104. (setq org-mark-ring-last-goto p)
  9105. (setq m (car p))
  9106. (org-pop-to-buffer-same-window (marker-buffer m))
  9107. (goto-char m)
  9108. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9109. (defun org-remove-angle-brackets (s)
  9110. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9111. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9112. s)
  9113. (defun org-add-angle-brackets (s)
  9114. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9115. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9116. s)
  9117. (defun org-remove-double-quotes (s)
  9118. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9119. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9120. s)
  9121. ;;; Following specific links
  9122. (defun org-follow-timestamp-link ()
  9123. (cond
  9124. ((org-at-date-range-p t)
  9125. (let ((org-agenda-start-on-weekday)
  9126. (t1 (match-string 1))
  9127. (t2 (match-string 2)))
  9128. (setq t1 (time-to-days (org-time-string-to-time t1))
  9129. t2 (time-to-days (org-time-string-to-time t2)))
  9130. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9131. ((org-at-timestamp-p t)
  9132. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9133. (substring (match-string 1) 0 10)))
  9134. 1))
  9135. (t (error "This should not happen"))))
  9136. ;;; Following file links
  9137. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9138. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9139. (declare-function mailcap-mime-info
  9140. "mailcap" (string &optional request no-decode))
  9141. (defvar org-wait nil)
  9142. (defun org-open-file (path &optional in-emacs line search)
  9143. "Open the file at PATH.
  9144. First, this expands any special file name abbreviations. Then the
  9145. configuration variable `org-file-apps' is checked if it contains an
  9146. entry for this file type, and if yes, the corresponding command is launched.
  9147. If no application is found, Emacs simply visits the file.
  9148. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9149. With a double \\[universal-argument] \\[universal-argument] \
  9150. prefix arg, Org tries to avoid opening in Emacs
  9151. and to use an external application to visit the file.
  9152. Optional LINE specifies a line to go to, optional SEARCH a string
  9153. to search for. If LINE or SEARCH is given, the file will be
  9154. opened in Emacs, unless an entry from org-file-apps that makes
  9155. use of groups in a regexp matches.
  9156. If you want to change the way frames are used when following a
  9157. link, please customize `org-link-frame-setup'.
  9158. If the file does not exist, an error is thrown."
  9159. (let* ((file (if (equal path "")
  9160. buffer-file-name
  9161. (substitute-in-file-name (expand-file-name path))))
  9162. (file-apps (append org-file-apps (org-default-apps)))
  9163. (apps (org-remove-if
  9164. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9165. (apps-dlink (org-remove-if-not
  9166. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9167. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9168. (dirp (if remp nil (file-directory-p file)))
  9169. (file (if (and dirp org-open-directory-means-index-dot-org)
  9170. (concat (file-name-as-directory file) "index.org")
  9171. file))
  9172. (a-m-a-p (assq 'auto-mode apps))
  9173. (dfile (downcase file))
  9174. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9175. (link (cond ((and (eq line nil)
  9176. (eq search nil))
  9177. file)
  9178. (line
  9179. (concat file "::" (number-to-string line)))
  9180. (search
  9181. (concat file "::" search))))
  9182. (dlink (downcase link))
  9183. (old-buffer (current-buffer))
  9184. (old-pos (point))
  9185. (old-mode major-mode)
  9186. ext cmd link-match-data)
  9187. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9188. (setq ext (match-string 1 dfile))
  9189. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9190. (setq ext (match-string 1 dfile))))
  9191. (cond
  9192. ((member in-emacs '((16) system))
  9193. (setq cmd (cdr (assoc 'system apps))))
  9194. (in-emacs (setq cmd 'emacs))
  9195. (t
  9196. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9197. (and dirp (cdr (assoc 'directory apps)))
  9198. ; first, try matching against apps-dlink
  9199. ; if we get a match here, store the match data for later
  9200. (let ((match (assoc-default dlink apps-dlink
  9201. 'string-match)))
  9202. (if match
  9203. (progn (setq link-match-data (match-data))
  9204. match)
  9205. (progn (setq in-emacs (or in-emacs line search))
  9206. nil))) ; if we have no match in apps-dlink,
  9207. ; always open the file in emacs if line or search
  9208. ; is given (for backwards compatibility)
  9209. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9210. 'string-match)
  9211. (cdr (assoc ext apps))
  9212. (cdr (assoc t apps))))))
  9213. (when (eq cmd 'system)
  9214. (setq cmd (cdr (assoc 'system apps))))
  9215. (when (eq cmd 'default)
  9216. (setq cmd (cdr (assoc t apps))))
  9217. (when (eq cmd 'mailcap)
  9218. (require 'mailcap)
  9219. (mailcap-parse-mailcaps)
  9220. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9221. (command (mailcap-mime-info mime-type)))
  9222. (if (stringp command)
  9223. (setq cmd command)
  9224. (setq cmd 'emacs))))
  9225. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9226. (not (file-exists-p file))
  9227. (not org-open-non-existing-files))
  9228. (error "No such file: %s" file))
  9229. (cond
  9230. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9231. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9232. (while (string-match "['\"]%s['\"]" cmd)
  9233. (setq cmd (replace-match "%s" t t cmd)))
  9234. (while (string-match "%s" cmd)
  9235. (setq cmd (replace-match
  9236. (save-match-data
  9237. (shell-quote-argument
  9238. (convert-standard-filename file)))
  9239. t t cmd)))
  9240. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9241. (save-match-data
  9242. (let ((match-index 1)
  9243. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9244. (set-match-data link-match-data)
  9245. (while (<= match-index number-of-groups)
  9246. (let ((regex (concat "%" (number-to-string match-index)))
  9247. (replace-with (match-string match-index dlink)))
  9248. (while (string-match regex cmd)
  9249. (setq cmd (replace-match replace-with t t cmd))))
  9250. (setq match-index (+ match-index 1)))))
  9251. (save-window-excursion
  9252. (start-process-shell-command cmd nil cmd)
  9253. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9254. ))
  9255. ((or (stringp cmd)
  9256. (eq cmd 'emacs))
  9257. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9258. (widen)
  9259. (if line (org-goto-line line)
  9260. (if search (org-link-search search))))
  9261. ((consp cmd)
  9262. (let ((file (convert-standard-filename file)))
  9263. (save-match-data
  9264. (set-match-data link-match-data)
  9265. (eval cmd))))
  9266. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9267. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9268. (or (not (equal old-buffer (current-buffer)))
  9269. (not (equal old-pos (point))))
  9270. (org-mark-ring-push old-pos old-buffer))))
  9271. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9272. "This function returns non-nil if `entry' uses a regular
  9273. expression which should be matched against the whole link by
  9274. org-open-file.
  9275. It assumes that is the case when the entry uses a regular
  9276. expression which has at least one grouping construct and the
  9277. action is either a lisp form or a command string containing
  9278. '%1', i.e. using at least one subexpression match as a
  9279. parameter."
  9280. (let ((selector (car entry))
  9281. (action (cdr entry)))
  9282. (if (stringp selector)
  9283. (and (> (regexp-opt-depth selector) 0)
  9284. (or (and (stringp action)
  9285. (string-match "%[0-9]" action))
  9286. (consp action)))
  9287. nil)))
  9288. (defun org-default-apps ()
  9289. "Return the default applications for this operating system."
  9290. (cond
  9291. ((eq system-type 'darwin)
  9292. org-file-apps-defaults-macosx)
  9293. ((eq system-type 'windows-nt)
  9294. org-file-apps-defaults-windowsnt)
  9295. (t org-file-apps-defaults-gnu)))
  9296. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9297. "Convert extensions to regular expressions in the cars of LIST.
  9298. Also, weed out any non-string entries, because the return value is used
  9299. only for regexp matching.
  9300. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9301. point to the symbol `emacs', indicating that the file should
  9302. be opened in Emacs."
  9303. (append
  9304. (delq nil
  9305. (mapcar (lambda (x)
  9306. (if (not (stringp (car x)))
  9307. nil
  9308. (if (string-match "\\W" (car x))
  9309. x
  9310. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9311. list))
  9312. (if add-auto-mode
  9313. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9314. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9315. (defun org-file-remote-p (file)
  9316. "Test whether FILE specifies a location on a remote system.
  9317. Return non-nil if the location is indeed remote.
  9318. For example, the filename \"/user@host:/foo\" specifies a location
  9319. on the system \"/user@host:\"."
  9320. (cond ((fboundp 'file-remote-p)
  9321. (file-remote-p file))
  9322. ((fboundp 'tramp-handle-file-remote-p)
  9323. (tramp-handle-file-remote-p file))
  9324. ((and (boundp 'ange-ftp-name-format)
  9325. (string-match (car ange-ftp-name-format) file))
  9326. t)
  9327. (t nil)))
  9328. ;;;; Refiling
  9329. (defun org-get-org-file ()
  9330. "Read a filename, with default directory `org-directory'."
  9331. (let ((default (or org-default-notes-file remember-data-file)))
  9332. (read-file-name (format "File name [%s]: " default)
  9333. (file-name-as-directory org-directory)
  9334. default)))
  9335. (defun org-notes-order-reversed-p ()
  9336. "Check if the current file should receive notes in reversed order."
  9337. (cond
  9338. ((not org-reverse-note-order) nil)
  9339. ((eq t org-reverse-note-order) t)
  9340. ((not (listp org-reverse-note-order)) nil)
  9341. (t (catch 'exit
  9342. (let ((all org-reverse-note-order)
  9343. entry)
  9344. (while (setq entry (pop all))
  9345. (if (string-match (car entry) buffer-file-name)
  9346. (throw 'exit (cdr entry))))
  9347. nil)))))
  9348. (defvar org-refile-target-table nil
  9349. "The list of refile targets, created by `org-refile'.")
  9350. (defvar org-agenda-new-buffers nil
  9351. "Buffers created to visit agenda files.")
  9352. (defvar org-refile-cache nil
  9353. "Cache for refile targets.")
  9354. (defvar org-refile-markers nil
  9355. "All the markers used for caching refile locations.")
  9356. (defun org-refile-marker (pos)
  9357. "Get a new refile marker, but only if caching is in use."
  9358. (if (not org-refile-use-cache)
  9359. pos
  9360. (let ((m (make-marker)))
  9361. (move-marker m pos)
  9362. (push m org-refile-markers)
  9363. m)))
  9364. (defun org-refile-cache-clear ()
  9365. "Clear the refile cache and disable all the markers."
  9366. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9367. (setq org-refile-markers nil)
  9368. (setq org-refile-cache nil)
  9369. (message "Refile cache has been cleared"))
  9370. (defun org-refile-cache-check-set (set)
  9371. "Check if all the markers in the cache still have live buffers."
  9372. (let (marker)
  9373. (catch 'exit
  9374. (while (and set (setq marker (nth 3 (pop set))))
  9375. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9376. (when (and marker (null (marker-buffer marker)))
  9377. (message "not found") (sit-for 3)
  9378. (throw 'exit nil)))
  9379. t)))
  9380. (defun org-refile-cache-put (set &rest identifiers)
  9381. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9382. (let* ((key (sha1 (prin1-to-string identifiers)))
  9383. (entry (assoc key org-refile-cache)))
  9384. (if entry
  9385. (setcdr entry set)
  9386. (push (cons key set) org-refile-cache))))
  9387. (defun org-refile-cache-get (&rest identifiers)
  9388. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9389. (cond
  9390. ((not org-refile-cache) nil)
  9391. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9392. (t
  9393. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9394. org-refile-cache))))
  9395. (and set (org-refile-cache-check-set set) set)))))
  9396. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9397. "Produce a table with refile targets."
  9398. (let ((case-fold-search nil)
  9399. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9400. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9401. targets tgs txt re files f desc descre fast-path-p level pos0)
  9402. (message "Getting targets...")
  9403. (with-current-buffer (or default-buffer (current-buffer))
  9404. (while (setq entry (pop entries))
  9405. (setq files (car entry) desc (cdr entry))
  9406. (setq fast-path-p nil)
  9407. (cond
  9408. ((null files) (setq files (list (current-buffer))))
  9409. ((eq files 'org-agenda-files)
  9410. (setq files (org-agenda-files 'unrestricted)))
  9411. ((and (symbolp files) (fboundp files))
  9412. (setq files (funcall files)))
  9413. ((and (symbolp files) (boundp files))
  9414. (setq files (symbol-value files))))
  9415. (if (stringp files) (setq files (list files)))
  9416. (cond
  9417. ((eq (car desc) :tag)
  9418. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9419. ((eq (car desc) :todo)
  9420. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9421. ((eq (car desc) :regexp)
  9422. (setq descre (cdr desc)))
  9423. ((eq (car desc) :level)
  9424. (setq descre (concat "^\\*\\{" (number-to-string
  9425. (if org-odd-levels-only
  9426. (1- (* 2 (cdr desc)))
  9427. (cdr desc)))
  9428. "\\}[ \t]")))
  9429. ((eq (car desc) :maxlevel)
  9430. (setq fast-path-p t)
  9431. (setq descre (concat "^\\*\\{1," (number-to-string
  9432. (if org-odd-levels-only
  9433. (1- (* 2 (cdr desc)))
  9434. (cdr desc)))
  9435. "\\}[ \t]")))
  9436. (t (error "Bad refiling target description %s" desc)))
  9437. (while (setq f (pop files))
  9438. (with-current-buffer
  9439. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9440. (or
  9441. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9442. (progn
  9443. (if (bufferp f) (setq f (buffer-file-name
  9444. (buffer-base-buffer f))))
  9445. (setq f (and f (expand-file-name f)))
  9446. (if (eq org-refile-use-outline-path 'file)
  9447. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9448. (save-excursion
  9449. (save-restriction
  9450. (widen)
  9451. (goto-char (point-min))
  9452. (while (re-search-forward descre nil t)
  9453. (goto-char (setq pos0 (point-at-bol)))
  9454. (catch 'next
  9455. (when org-refile-target-verify-function
  9456. (save-match-data
  9457. (or (funcall org-refile-target-verify-function)
  9458. (throw 'next t))))
  9459. (when (and (looking-at org-complex-heading-regexp)
  9460. (not (member (match-string 4) excluded-entries))
  9461. (match-string 4))
  9462. (setq level (org-reduced-level
  9463. (- (match-end 1) (match-beginning 1)))
  9464. txt (org-link-display-format (match-string 4))
  9465. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9466. re (format org-complex-heading-regexp-format
  9467. (regexp-quote (match-string 4))))
  9468. (when org-refile-use-outline-path
  9469. (setq txt (mapconcat
  9470. 'org-protect-slash
  9471. (append
  9472. (if (eq org-refile-use-outline-path
  9473. 'file)
  9474. (list (file-name-nondirectory
  9475. (buffer-file-name
  9476. (buffer-base-buffer))))
  9477. (if (eq org-refile-use-outline-path
  9478. 'full-file-path)
  9479. (list (buffer-file-name
  9480. (buffer-base-buffer)))))
  9481. (org-get-outline-path fast-path-p
  9482. level txt)
  9483. (list txt))
  9484. "/")))
  9485. (push (list txt f re (org-refile-marker (point)))
  9486. tgs)))
  9487. (when (= (point) pos0)
  9488. ;; verification function has not moved point
  9489. (goto-char (point-at-eol))))))))
  9490. (when org-refile-use-cache
  9491. (org-refile-cache-put tgs (buffer-file-name) descre))
  9492. (setq targets (append tgs targets))
  9493. ))))
  9494. (message "Getting targets...done")
  9495. (nreverse targets)))
  9496. (defun org-protect-slash (s)
  9497. (while (string-match "/" s)
  9498. (setq s (replace-match "\\" t t s)))
  9499. s)
  9500. (defvar org-olpa (make-vector 20 nil))
  9501. (defun org-get-outline-path (&optional fastp level heading)
  9502. "Return the outline path to the current entry, as a list.
  9503. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9504. routine which makes outline path derivations for an entire file,
  9505. avoiding backtracing. Refile target collection makes use of that."
  9506. (if fastp
  9507. (progn
  9508. (if (> level 19)
  9509. (error "Outline path failure, more than 19 levels"))
  9510. (loop for i from level upto 19 do
  9511. (aset org-olpa i nil))
  9512. (prog1
  9513. (delq nil (append org-olpa nil))
  9514. (aset org-olpa level heading)))
  9515. (let (rtn case-fold-search)
  9516. (save-excursion
  9517. (save-restriction
  9518. (widen)
  9519. (while (org-up-heading-safe)
  9520. (when (looking-at org-complex-heading-regexp)
  9521. (push (org-match-string-no-properties 4) rtn)))
  9522. rtn)))))
  9523. (defun org-format-outline-path (path &optional width prefix)
  9524. "Format the outline path PATH for display.
  9525. Width is the maximum number of characters that is available.
  9526. Prefix is a prefix to be included in the returned string,
  9527. such as the file name."
  9528. (setq width (or width 79))
  9529. (if prefix (setq width (- width (length prefix))))
  9530. (if (not path)
  9531. (or prefix "")
  9532. (let* ((nsteps (length path))
  9533. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9534. (maxwidth (if (<= total-width width)
  9535. 10000 ;; everything fits
  9536. ;; we need to shorten the level headings
  9537. (/ (- width nsteps) nsteps)))
  9538. (org-odd-levels-only nil)
  9539. (n 0)
  9540. (total (1+ (length prefix))))
  9541. (setq maxwidth (max maxwidth 10))
  9542. (concat prefix
  9543. (mapconcat
  9544. (lambda (h)
  9545. (setq n (1+ n))
  9546. (if (and (= n nsteps) (< maxwidth 10000))
  9547. (setq maxwidth (- total-width total)))
  9548. (if (< (length h) maxwidth)
  9549. (progn (setq total (+ total (length h) 1)) h)
  9550. (setq h (substring h 0 (- maxwidth 2))
  9551. total (+ total maxwidth 1))
  9552. (if (string-match "[ \t]+\\'" h)
  9553. (setq h (substring h 0 (match-beginning 0))))
  9554. (setq h (concat h "..")))
  9555. (org-add-props h nil 'face
  9556. (nth (% (1- n) org-n-level-faces)
  9557. org-level-faces))
  9558. h)
  9559. path "/")))))
  9560. (defun org-display-outline-path (&optional file current)
  9561. "Display the current outline path in the echo area."
  9562. (interactive "P")
  9563. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9564. (case-fold-search nil)
  9565. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9566. (if current (setq path (append path
  9567. (save-excursion
  9568. (org-back-to-heading t)
  9569. (if (looking-at org-complex-heading-regexp)
  9570. (list (match-string 4)))))))
  9571. (message "%s"
  9572. (org-format-outline-path
  9573. path
  9574. (1- (frame-width))
  9575. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9576. (defvar org-refile-history nil
  9577. "History for refiling operations.")
  9578. (defvar org-after-refile-insert-hook nil
  9579. "Hook run after `org-refile' has inserted its stuff at the new location.
  9580. Note that this is still *before* the stuff will be removed from
  9581. the *old* location.")
  9582. (defvar org-capture-last-stored-marker)
  9583. (defun org-refile (&optional goto default-buffer rfloc)
  9584. "Move the entry or entries at point to another heading.
  9585. The list of target headings is compiled using the information in
  9586. `org-refile-targets', which see.
  9587. At the target location, the entry is filed as a subitem of the target
  9588. heading. Depending on `org-reverse-note-order', the new subitem will
  9589. either be the first or the last subitem.
  9590. If there is an active region, all entries in that region will be moved.
  9591. However, the region must fulfill the requirement that the first heading
  9592. is the first one sets the top-level of the moved text - at most siblings
  9593. below it are allowed.
  9594. With prefix arg GOTO, the command will only visit the target location
  9595. and not actually move anything.
  9596. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9597. go to the location where the last refiling operation has put the subtree.
  9598. With a prefix argument of `2', refile to the running clock.
  9599. RFLOC can be a refile location obtained in a different way.
  9600. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9601. If you are using target caching (see `org-refile-use-cache'),
  9602. You have to clear the target cache in order to find new targets.
  9603. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9604. prefix argument (`C-u C-u C-u C-c C-w')."
  9605. (interactive "P")
  9606. (if (member goto '(0 (64)))
  9607. (org-refile-cache-clear)
  9608. (let* ((cbuf (current-buffer))
  9609. (regionp (org-region-active-p))
  9610. (region-start (and regionp (region-beginning)))
  9611. (region-end (and regionp (region-end)))
  9612. (region-length (and regionp (- region-end region-start)))
  9613. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9614. pos it nbuf file re level reversed)
  9615. (setq last-command nil)
  9616. (when regionp
  9617. (goto-char region-start)
  9618. (or (bolp) (goto-char (point-at-bol)))
  9619. (setq region-start (point))
  9620. (unless (or (org-kill-is-subtree-p
  9621. (buffer-substring region-start region-end))
  9622. (prog1 org-refile-active-region-within-subtree
  9623. (org-toggle-heading)))
  9624. (error "The region is not a (sequence of) subtree(s)")))
  9625. (if (equal goto '(16))
  9626. (org-refile-goto-last-stored)
  9627. (when (or
  9628. (and (equal goto 2)
  9629. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9630. (prog1
  9631. (setq it (list (or org-clock-heading "running clock")
  9632. (buffer-file-name
  9633. (marker-buffer org-clock-hd-marker))
  9634. ""
  9635. (marker-position org-clock-hd-marker)))
  9636. (setq goto nil)))
  9637. (setq it (or rfloc
  9638. (let (heading-text)
  9639. (save-excursion
  9640. (unless goto
  9641. (org-back-to-heading t)
  9642. (setq heading-text
  9643. (nth 4 (org-heading-components))))
  9644. (org-refile-get-location
  9645. (cond (goto "Goto")
  9646. (regionp "Refile region to")
  9647. (t (concat "Refile subtree \""
  9648. heading-text "\" to")))
  9649. default-buffer
  9650. (and (not (equal '(4) goto))
  9651. org-refile-allow-creating-parent-nodes)
  9652. goto))))))
  9653. (setq file (nth 1 it)
  9654. re (nth 2 it)
  9655. pos (nth 3 it))
  9656. (if (and (not goto)
  9657. pos
  9658. (equal (buffer-file-name) file)
  9659. (if regionp
  9660. (and (>= pos region-start)
  9661. (<= pos region-end))
  9662. (and (>= pos (point))
  9663. (< pos (save-excursion
  9664. (org-end-of-subtree t t))))))
  9665. (error "Cannot refile to position inside the tree or region"))
  9666. (setq nbuf (or (find-buffer-visiting file)
  9667. (find-file-noselect file)))
  9668. (if goto
  9669. (progn
  9670. (org-pop-to-buffer-same-window nbuf)
  9671. (goto-char pos)
  9672. (org-show-context 'org-goto))
  9673. (if regionp
  9674. (progn
  9675. (org-kill-new (buffer-substring region-start region-end))
  9676. (org-save-markers-in-region region-start region-end))
  9677. (org-copy-subtree 1 nil t))
  9678. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9679. (find-file-noselect file)))
  9680. (setq reversed (org-notes-order-reversed-p))
  9681. (save-excursion
  9682. (save-restriction
  9683. (widen)
  9684. (if pos
  9685. (progn
  9686. (goto-char pos)
  9687. (looking-at org-outline-regexp)
  9688. (setq level (org-get-valid-level (funcall outline-level) 1))
  9689. (goto-char
  9690. (if reversed
  9691. (or (outline-next-heading) (point-max))
  9692. (or (save-excursion (org-get-next-sibling))
  9693. (org-end-of-subtree t t)
  9694. (point-max)))))
  9695. (setq level 1)
  9696. (if (not reversed)
  9697. (goto-char (point-max))
  9698. (goto-char (point-min))
  9699. (or (outline-next-heading) (goto-char (point-max)))))
  9700. (if (not (bolp)) (newline))
  9701. (org-paste-subtree level)
  9702. (when org-log-refile
  9703. (org-add-log-setup 'refile nil nil 'findpos
  9704. org-log-refile)
  9705. (unless (eq org-log-refile 'note)
  9706. (save-excursion (org-add-log-note))))
  9707. (and org-auto-align-tags (org-set-tags nil t))
  9708. (bookmark-set "org-refile-last-stored")
  9709. ;; If we are refiling for capture, make sure that the
  9710. ;; last-capture pointers point here
  9711. (when (org-bound-and-true-p org-refile-for-capture)
  9712. (bookmark-set "org-capture-last-stored-marker")
  9713. (move-marker org-capture-last-stored-marker (point)))
  9714. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9715. (run-hooks 'org-after-refile-insert-hook))))
  9716. (if regionp
  9717. (delete-region (point) (+ (point) region-length))
  9718. (org-cut-subtree))
  9719. (when (featurep 'org-inlinetask)
  9720. (org-inlinetask-remove-END-maybe))
  9721. (setq org-markers-to-move nil)
  9722. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9723. (defun org-refile-goto-last-stored ()
  9724. "Go to the location where the last refile was stored."
  9725. (interactive)
  9726. (bookmark-jump "org-refile-last-stored")
  9727. (message "This is the location of the last refile"))
  9728. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9729. no-exclude)
  9730. "Prompt the user for a refile location, using PROMPT.
  9731. PROMPT should not be suffixed with a colon and a space, because
  9732. this function appends the default value from
  9733. `org-refile-history' automatically, if that is not empty.
  9734. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9735. this is used for the GOTO interface."
  9736. (let ((org-refile-targets org-refile-targets)
  9737. (org-refile-use-outline-path org-refile-use-outline-path)
  9738. excluded-entries)
  9739. (when (and (eq major-mode 'org-mode)
  9740. (not org-refile-use-cache)
  9741. (not no-exclude))
  9742. (org-map-tree
  9743. (lambda()
  9744. (setq excluded-entries
  9745. (append excluded-entries (list (org-get-heading t t)))))))
  9746. (setq org-refile-target-table
  9747. (org-refile-get-targets default-buffer excluded-entries)))
  9748. (unless org-refile-target-table
  9749. (error "No refile targets"))
  9750. (let* ((prompt (concat prompt
  9751. (and (car org-refile-history)
  9752. (concat " (default " (car org-refile-history) ")"))
  9753. ": "))
  9754. (cbuf (current-buffer))
  9755. (partial-completion-mode nil)
  9756. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9757. (cfunc (if (and org-refile-use-outline-path
  9758. org-outline-path-complete-in-steps)
  9759. 'org-olpath-completing-read
  9760. 'org-icompleting-read))
  9761. (extra (if org-refile-use-outline-path "/" ""))
  9762. (filename (and cfn (expand-file-name cfn)))
  9763. (tbl (mapcar
  9764. (lambda (x)
  9765. (if (and (not (member org-refile-use-outline-path
  9766. '(file full-file-path)))
  9767. (not (equal filename (nth 1 x))))
  9768. (cons (concat (car x) extra " ("
  9769. (file-name-nondirectory (nth 1 x)) ")")
  9770. (cdr x))
  9771. (cons (concat (car x) extra) (cdr x))))
  9772. org-refile-target-table))
  9773. (completion-ignore-case t)
  9774. pa answ parent-target child parent old-hist)
  9775. (setq old-hist org-refile-history)
  9776. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9777. nil 'org-refile-history (car org-refile-history)))
  9778. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9779. (org-refile-check-position pa)
  9780. (if pa
  9781. (progn
  9782. (when (or (not org-refile-history)
  9783. (not (eq old-hist org-refile-history))
  9784. (not (equal (car pa) (car org-refile-history))))
  9785. (setq org-refile-history
  9786. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9787. org-refile-history
  9788. (cdr org-refile-history))))
  9789. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9790. (pop org-refile-history)))
  9791. pa)
  9792. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9793. (progn
  9794. (setq parent (match-string 1 answ)
  9795. child (match-string 2 answ))
  9796. (setq parent-target (or (assoc parent tbl)
  9797. (assoc (concat parent "/") tbl)))
  9798. (when (and parent-target
  9799. (or (eq new-nodes t)
  9800. (and (eq new-nodes 'confirm)
  9801. (y-or-n-p (format "Create new node \"%s\"? "
  9802. child)))))
  9803. (org-refile-new-child parent-target child)))
  9804. (error "Invalid target location")))))
  9805. (defun org-refile-check-position (refile-pointer)
  9806. "Check if the refile pointer matches the readline to which it points."
  9807. (let* ((file (nth 1 refile-pointer))
  9808. (re (nth 2 refile-pointer))
  9809. (pos (nth 3 refile-pointer))
  9810. buffer)
  9811. (when (org-string-nw-p re)
  9812. (setq buffer (if (markerp pos)
  9813. (marker-buffer pos)
  9814. (or (find-buffer-visiting file)
  9815. (find-file-noselect file))))
  9816. (with-current-buffer buffer
  9817. (save-excursion
  9818. (save-restriction
  9819. (widen)
  9820. (goto-char pos)
  9821. (beginning-of-line 1)
  9822. (unless (org-looking-at-p re)
  9823. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9824. (defun org-refile-new-child (parent-target child)
  9825. "Use refile target PARENT-TARGET to add new CHILD below it."
  9826. (unless parent-target
  9827. (error "Cannot find parent for new node"))
  9828. (let ((file (nth 1 parent-target))
  9829. (pos (nth 3 parent-target))
  9830. level)
  9831. (with-current-buffer (or (find-buffer-visiting file)
  9832. (find-file-noselect file))
  9833. (save-excursion
  9834. (save-restriction
  9835. (widen)
  9836. (if pos
  9837. (goto-char pos)
  9838. (goto-char (point-max))
  9839. (if (not (bolp)) (newline)))
  9840. (when (looking-at org-outline-regexp)
  9841. (setq level (funcall outline-level))
  9842. (org-end-of-subtree t t))
  9843. (org-back-over-empty-lines)
  9844. (insert "\n" (make-string
  9845. (if pos (org-get-valid-level level 1) 1) ?*)
  9846. " " child "\n")
  9847. (beginning-of-line 0)
  9848. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9849. (defun org-olpath-completing-read (prompt collection &rest args)
  9850. "Read an outline path like a file name."
  9851. (let ((thetable collection)
  9852. (org-completion-use-ido nil) ; does not work with ido.
  9853. (org-completion-use-iswitchb nil)) ; or iswitchb
  9854. (apply
  9855. 'org-icompleting-read prompt
  9856. (lambda (string predicate &optional flag)
  9857. (let (rtn r f (l (length string)))
  9858. (cond
  9859. ((eq flag nil)
  9860. ;; try completion
  9861. (try-completion string thetable))
  9862. ((eq flag t)
  9863. ;; all-completions
  9864. (setq rtn (all-completions string thetable predicate))
  9865. (mapcar
  9866. (lambda (x)
  9867. (setq r (substring x l))
  9868. (if (string-match " ([^)]*)$" x)
  9869. (setq f (match-string 0 x))
  9870. (setq f ""))
  9871. (if (string-match "/" r)
  9872. (concat string (substring r 0 (match-end 0)) f)
  9873. x))
  9874. rtn))
  9875. ((eq flag 'lambda)
  9876. ;; exact match?
  9877. (assoc string thetable)))
  9878. ))
  9879. args)))
  9880. ;;;; Dynamic blocks
  9881. (defun org-find-dblock (name)
  9882. "Find the first dynamic block with name NAME in the buffer.
  9883. If not found, stay at current position and return nil."
  9884. (let (pos)
  9885. (save-excursion
  9886. (goto-char (point-min))
  9887. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9888. nil t)
  9889. (match-beginning 0))))
  9890. (if pos (goto-char pos))
  9891. pos))
  9892. (defconst org-dblock-start-re
  9893. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9894. "Matches the start line of a dynamic block, with parameters.")
  9895. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9896. "Matches the end of a dynamic block.")
  9897. (defun org-create-dblock (plist)
  9898. "Create a dynamic block section, with parameters taken from PLIST.
  9899. PLIST must contain a :name entry which is used as name of the block."
  9900. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9901. (end-of-line 1)
  9902. (newline))
  9903. (let ((col (current-column))
  9904. (name (plist-get plist :name)))
  9905. (insert "#+BEGIN: " name)
  9906. (while plist
  9907. (if (eq (car plist) :name)
  9908. (setq plist (cddr plist))
  9909. (insert " " (prin1-to-string (pop plist)))))
  9910. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9911. (beginning-of-line -2)))
  9912. (defun org-prepare-dblock ()
  9913. "Prepare dynamic block for refresh.
  9914. This empties the block, puts the cursor at the insert position and returns
  9915. the property list including an extra property :name with the block name."
  9916. (unless (looking-at org-dblock-start-re)
  9917. (error "Not at a dynamic block"))
  9918. (let* ((begdel (1+ (match-end 0)))
  9919. (name (org-no-properties (match-string 1)))
  9920. (params (append (list :name name)
  9921. (read (concat "(" (match-string 3) ")")))))
  9922. (save-excursion
  9923. (beginning-of-line 1)
  9924. (skip-chars-forward " \t")
  9925. (setq params (plist-put params :indentation-column (current-column))))
  9926. (unless (re-search-forward org-dblock-end-re nil t)
  9927. (error "Dynamic block not terminated"))
  9928. (setq params
  9929. (append params
  9930. (list :content (buffer-substring
  9931. begdel (match-beginning 0)))))
  9932. (delete-region begdel (match-beginning 0))
  9933. (goto-char begdel)
  9934. (open-line 1)
  9935. params))
  9936. (defun org-map-dblocks (&optional command)
  9937. "Apply COMMAND to all dynamic blocks in the current buffer.
  9938. If COMMAND is not given, use `org-update-dblock'."
  9939. (let ((cmd (or command 'org-update-dblock)))
  9940. (save-excursion
  9941. (goto-char (point-min))
  9942. (while (re-search-forward org-dblock-start-re nil t)
  9943. (goto-char (match-beginning 0))
  9944. (save-excursion
  9945. (condition-case nil
  9946. (funcall cmd)
  9947. (error (message "Error during update of dynamic block"))))
  9948. (unless (re-search-forward org-dblock-end-re nil t)
  9949. (error "Dynamic block not terminated"))))))
  9950. (defun org-dblock-update (&optional arg)
  9951. "User command for updating dynamic blocks.
  9952. Update the dynamic block at point. With prefix ARG, update all dynamic
  9953. blocks in the buffer."
  9954. (interactive "P")
  9955. (if arg
  9956. (org-update-all-dblocks)
  9957. (or (looking-at org-dblock-start-re)
  9958. (org-beginning-of-dblock))
  9959. (org-update-dblock)))
  9960. (defun org-update-dblock ()
  9961. "Update the dynamic block at point.
  9962. This means to empty the block, parse for parameters and then call
  9963. the correct writing function."
  9964. (interactive)
  9965. (save-window-excursion
  9966. (let* ((pos (point))
  9967. (line (org-current-line))
  9968. (params (org-prepare-dblock))
  9969. (name (plist-get params :name))
  9970. (indent (plist-get params :indentation-column))
  9971. (cmd (intern (concat "org-dblock-write:" name))))
  9972. (message "Updating dynamic block `%s' at line %d..." name line)
  9973. (funcall cmd params)
  9974. (message "Updating dynamic block `%s' at line %d...done" name line)
  9975. (goto-char pos)
  9976. (when (and indent (> indent 0))
  9977. (setq indent (make-string indent ?\ ))
  9978. (save-excursion
  9979. (org-beginning-of-dblock)
  9980. (forward-line 1)
  9981. (while (not (looking-at org-dblock-end-re))
  9982. (insert indent)
  9983. (beginning-of-line 2))
  9984. (when (looking-at org-dblock-end-re)
  9985. (and (looking-at "[ \t]+")
  9986. (replace-match ""))
  9987. (insert indent)))))))
  9988. (defun org-beginning-of-dblock ()
  9989. "Find the beginning of the dynamic block at point.
  9990. Error if there is no such block at point."
  9991. (let ((pos (point))
  9992. beg)
  9993. (end-of-line 1)
  9994. (if (and (re-search-backward org-dblock-start-re nil t)
  9995. (setq beg (match-beginning 0))
  9996. (re-search-forward org-dblock-end-re nil t)
  9997. (> (match-end 0) pos))
  9998. (goto-char beg)
  9999. (goto-char pos)
  10000. (error "Not in a dynamic block"))))
  10001. (defun org-update-all-dblocks ()
  10002. "Update all dynamic blocks in the buffer.
  10003. This function can be used in a hook."
  10004. (interactive)
  10005. (when (eq major-mode 'org-mode)
  10006. (org-map-dblocks 'org-update-dblock)))
  10007. ;;;; Completion
  10008. (defconst org-additional-option-like-keywords
  10009. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10010. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10011. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10012. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10013. "BEGIN:" "END:"
  10014. "ORGTBL" "TBLFM:" "TBLNAME:"
  10015. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10016. "BEGIN_QUOTE" "END_QUOTE"
  10017. "BEGIN_VERSE" "END_VERSE"
  10018. "BEGIN_CENTER" "END_CENTER"
  10019. "BEGIN_SRC" "END_SRC"
  10020. "BEGIN_RESULT" "END_RESULT"
  10021. "NAME:" "RESULTS:"
  10022. "HEADER:" "HEADERS:"
  10023. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10024. "CAPTION:" "LABEL:"
  10025. "SETUPFILE:"
  10026. "INCLUDE:"
  10027. "BIND:"
  10028. "MACRO:"))
  10029. (defcustom org-structure-template-alist
  10030. '(
  10031. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10032. "<src lang=\"?\">\n\n</src>")
  10033. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10034. "<example>\n?\n</example>")
  10035. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10036. "<quote>\n?\n</quote>")
  10037. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10038. "<verse>\n?\n</verse>")
  10039. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10040. "<center>\n?\n</center>")
  10041. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10042. "<literal style=\"latex\">\n?\n</literal>")
  10043. ("L" "#+LaTeX: "
  10044. "<literal style=\"latex\">?</literal>")
  10045. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10046. "<literal style=\"html\">\n?\n</literal>")
  10047. ("H" "#+HTML: "
  10048. "<literal style=\"html\">?</literal>")
  10049. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10050. ("A" "#+ASCII: ")
  10051. ("i" "#+INDEX: ?"
  10052. "#+INDEX: ?")
  10053. ("I" "#+INCLUDE %file ?"
  10054. "<include file=%file markup=\"?\">")
  10055. )
  10056. "Structure completion elements.
  10057. This is a list of abbreviation keys and values. The value gets inserted
  10058. if you type `<' followed by the key and then press the completion key,
  10059. usually `M-TAB'. %file will be replaced by a file name after prompting
  10060. for the file using completion. The cursor will be placed at the position
  10061. of the `?` in the template.
  10062. There are two templates for each key, the first uses the original Org syntax,
  10063. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10064. the default when the /org-mtags.el/ module has been loaded. See also the
  10065. variable `org-mtags-prefer-muse-templates'."
  10066. :group 'org-completion
  10067. :type '(repeat
  10068. (string :tag "Key")
  10069. (string :tag "Template")
  10070. (string :tag "Muse Template")))
  10071. (defun org-try-structure-completion ()
  10072. "Try to complete a structure template before point.
  10073. This looks for strings like \"<e\" on an otherwise empty line and
  10074. expands them."
  10075. (let ((l (buffer-substring (point-at-bol) (point)))
  10076. a)
  10077. (when (and (looking-at "[ \t]*$")
  10078. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10079. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10080. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10081. (match-beginning 1)) a)
  10082. t)))
  10083. (defun org-complete-expand-structure-template (start cell)
  10084. "Expand a structure template."
  10085. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10086. (rpl (nth (if musep 2 1) cell))
  10087. (ind ""))
  10088. (delete-region start (point))
  10089. (when (string-match "\\`#\\+" rpl)
  10090. (cond
  10091. ((bolp))
  10092. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10093. (setq ind (buffer-substring (point-at-bol) (point))))
  10094. (t (newline))))
  10095. (setq start (point))
  10096. (if (string-match "%file" rpl)
  10097. (setq rpl (replace-match
  10098. (concat
  10099. "\""
  10100. (save-match-data
  10101. (abbreviate-file-name (read-file-name "Include file: ")))
  10102. "\"")
  10103. t t rpl)))
  10104. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10105. (concat "\n" ind)))
  10106. (insert rpl)
  10107. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10108. ;;;; TODO, DEADLINE, Comments
  10109. (defun org-toggle-comment ()
  10110. "Change the COMMENT state of an entry."
  10111. (interactive)
  10112. (save-excursion
  10113. (org-back-to-heading)
  10114. (let (case-fold-search)
  10115. (cond
  10116. ((looking-at (format org-heading-keyword-regexp-format
  10117. org-comment-string))
  10118. (goto-char (match-end 1))
  10119. (looking-at (concat " +" org-comment-string))
  10120. (replace-match "" t t)
  10121. (when (eolp) (insert " ")))
  10122. ((looking-at org-outline-regexp)
  10123. (goto-char (match-end 0))
  10124. (insert org-comment-string " "))))))
  10125. (defvar org-last-todo-state-is-todo nil
  10126. "This is non-nil when the last TODO state change led to a TODO state.
  10127. If the last change removed the TODO tag or switched to DONE, then
  10128. this is nil.")
  10129. (defvar org-setting-tags nil) ; dynamically skipped
  10130. (defvar org-todo-setup-filter-hook nil
  10131. "Hook for functions that pre-filter todo specs.
  10132. Each function takes a todo spec and returns either nil or the spec
  10133. transformed into canonical form." )
  10134. (defvar org-todo-get-default-hook nil
  10135. "Hook for functions that get a default item for todo.
  10136. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10137. nil or a string to be used for the todo mark." )
  10138. (defvar org-agenda-headline-snapshot-before-repeat)
  10139. (defun org-current-effective-time ()
  10140. "Return current time adjusted for `org-extend-today-until' variable"
  10141. (let* ((ct (org-current-time))
  10142. (dct (decode-time ct))
  10143. (ct1
  10144. (if (and org-use-effective-time
  10145. (< (nth 2 dct) org-extend-today-until))
  10146. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10147. ct)))
  10148. ct1))
  10149. (defun org-todo-yesterday (&optional arg)
  10150. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10151. (interactive "P")
  10152. (if (eq major-mode 'org-agenda-mode)
  10153. (apply 'org-agenda-todo-yesterday arg)
  10154. (let* ((hour (third (decode-time
  10155. (org-current-time))))
  10156. (org-extend-today-until (1+ hour)))
  10157. (org-todo arg))))
  10158. (defun org-todo (&optional arg)
  10159. "Change the TODO state of an item.
  10160. The state of an item is given by a keyword at the start of the heading,
  10161. like
  10162. *** TODO Write paper
  10163. *** DONE Call mom
  10164. The different keywords are specified in the variable `org-todo-keywords'.
  10165. By default the available states are \"TODO\" and \"DONE\".
  10166. So for this example: when the item starts with TODO, it is changed to DONE.
  10167. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10168. DONE are present, add TODO at the beginning of the heading.
  10169. With \\[universal-argument] prefix arg, use completion to determine the new \
  10170. state.
  10171. With numeric prefix arg, switch to that state.
  10172. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10173. keywords (nextset).
  10174. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10175. With a numeric prefix arg of 0, inhibit note taking for the change.
  10176. For calling through lisp, arg is also interpreted in the following way:
  10177. 'none -> empty state
  10178. \"\"(empty string) -> switch to empty state
  10179. 'done -> switch to DONE
  10180. 'nextset -> switch to the next set of keywords
  10181. 'previousset -> switch to the previous set of keywords
  10182. \"WAITING\" -> switch to the specified keyword, but only if it
  10183. really is a member of `org-todo-keywords'."
  10184. (interactive "P")
  10185. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10186. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10187. 'region-start-level 'region))
  10188. org-loop-over-headlines-in-active-region)
  10189. (org-map-entries
  10190. `(org-todo ,arg)
  10191. org-loop-over-headlines-in-active-region
  10192. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10193. (if (equal arg '(16)) (setq arg 'nextset))
  10194. (let ((org-blocker-hook org-blocker-hook)
  10195. (case-fold-search nil))
  10196. (when (equal arg '(64))
  10197. (setq arg nil org-blocker-hook nil))
  10198. (when (and org-blocker-hook
  10199. (or org-inhibit-blocking
  10200. (org-entry-get nil "NOBLOCKING")))
  10201. (setq org-blocker-hook nil))
  10202. (save-excursion
  10203. (catch 'exit
  10204. (org-back-to-heading t)
  10205. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10206. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10207. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10208. (let* ((match-data (match-data))
  10209. (startpos (point-at-bol))
  10210. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10211. (org-log-done org-log-done)
  10212. (org-log-repeat org-log-repeat)
  10213. (org-todo-log-states org-todo-log-states)
  10214. (org-inhibit-logging
  10215. (if (equal arg 0)
  10216. (progn (setq arg nil) 'note) org-inhibit-logging))
  10217. (this (match-string 1))
  10218. (hl-pos (match-beginning 0))
  10219. (head (org-get-todo-sequence-head this))
  10220. (ass (assoc head org-todo-kwd-alist))
  10221. (interpret (nth 1 ass))
  10222. (done-word (nth 3 ass))
  10223. (final-done-word (nth 4 ass))
  10224. (last-state (or this ""))
  10225. (completion-ignore-case t)
  10226. (member (member this org-todo-keywords-1))
  10227. (tail (cdr member))
  10228. (state (cond
  10229. ((and org-todo-key-trigger
  10230. (or (and (equal arg '(4))
  10231. (eq org-use-fast-todo-selection 'prefix))
  10232. (and (not arg) org-use-fast-todo-selection
  10233. (not (eq org-use-fast-todo-selection
  10234. 'prefix)))))
  10235. ;; Use fast selection
  10236. (org-fast-todo-selection))
  10237. ((and (equal arg '(4))
  10238. (or (not org-use-fast-todo-selection)
  10239. (not org-todo-key-trigger)))
  10240. ;; Read a state with completion
  10241. (org-icompleting-read
  10242. "State: " (mapcar (lambda(x) (list x))
  10243. org-todo-keywords-1)
  10244. nil t))
  10245. ((eq arg 'right)
  10246. (if this
  10247. (if tail (car tail) nil)
  10248. (car org-todo-keywords-1)))
  10249. ((eq arg 'left)
  10250. (if (equal member org-todo-keywords-1)
  10251. nil
  10252. (if this
  10253. (nth (- (length org-todo-keywords-1)
  10254. (length tail) 2)
  10255. org-todo-keywords-1)
  10256. (org-last org-todo-keywords-1))))
  10257. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10258. (setq arg nil))) ; hack to fall back to cycling
  10259. (arg
  10260. ;; user or caller requests a specific state
  10261. (cond
  10262. ((equal arg "") nil)
  10263. ((eq arg 'none) nil)
  10264. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10265. ((eq arg 'nextset)
  10266. (or (car (cdr (member head org-todo-heads)))
  10267. (car org-todo-heads)))
  10268. ((eq arg 'previousset)
  10269. (let ((org-todo-heads (reverse org-todo-heads)))
  10270. (or (car (cdr (member head org-todo-heads)))
  10271. (car org-todo-heads))))
  10272. ((car (member arg org-todo-keywords-1)))
  10273. ((stringp arg)
  10274. (error "State `%s' not valid in this file" arg))
  10275. ((nth (1- (prefix-numeric-value arg))
  10276. org-todo-keywords-1))))
  10277. ((null member) (or head (car org-todo-keywords-1)))
  10278. ((equal this final-done-word) nil) ;; -> make empty
  10279. ((null tail) nil) ;; -> first entry
  10280. ((memq interpret '(type priority))
  10281. (if (eq this-command last-command)
  10282. (car tail)
  10283. (if (> (length tail) 0)
  10284. (or done-word (car org-done-keywords))
  10285. nil)))
  10286. (t
  10287. (car tail))))
  10288. (state (or
  10289. (run-hook-with-args-until-success
  10290. 'org-todo-get-default-hook state last-state)
  10291. state))
  10292. (next (if state (concat " " state " ") " "))
  10293. (change-plist (list :type 'todo-state-change :from this :to state
  10294. :position startpos))
  10295. dolog now-done-p)
  10296. (when org-blocker-hook
  10297. (setq org-last-todo-state-is-todo
  10298. (not (member this org-done-keywords)))
  10299. (unless (save-excursion
  10300. (save-match-data
  10301. (org-with-wide-buffer
  10302. (run-hook-with-args-until-failure
  10303. 'org-blocker-hook change-plist))))
  10304. (if (org-called-interactively-p 'interactive)
  10305. (error "TODO state change from %s to %s blocked" this state)
  10306. ;; fail silently
  10307. (message "TODO state change from %s to %s blocked" this state)
  10308. (throw 'exit nil))))
  10309. (store-match-data match-data)
  10310. (replace-match next t t)
  10311. (unless (pos-visible-in-window-p hl-pos)
  10312. (message "TODO state changed to %s" (org-trim next)))
  10313. (unless head
  10314. (setq head (org-get-todo-sequence-head state)
  10315. ass (assoc head org-todo-kwd-alist)
  10316. interpret (nth 1 ass)
  10317. done-word (nth 3 ass)
  10318. final-done-word (nth 4 ass)))
  10319. (when (memq arg '(nextset previousset))
  10320. (message "Keyword-Set %d/%d: %s"
  10321. (- (length org-todo-sets) -1
  10322. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10323. (length org-todo-sets)
  10324. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10325. (setq org-last-todo-state-is-todo
  10326. (not (member state org-done-keywords)))
  10327. (setq now-done-p (and (member state org-done-keywords)
  10328. (not (member this org-done-keywords))))
  10329. (and logging (org-local-logging logging))
  10330. (when (and (or org-todo-log-states org-log-done)
  10331. (not (eq org-inhibit-logging t))
  10332. (not (memq arg '(nextset previousset))))
  10333. ;; we need to look at recording a time and note
  10334. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10335. (nth 2 (assoc this org-todo-log-states))))
  10336. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10337. (setq dolog 'time))
  10338. (when (and state
  10339. (member state org-not-done-keywords)
  10340. (not (member this org-not-done-keywords)))
  10341. ;; This is now a todo state and was not one before
  10342. ;; If there was a CLOSED time stamp, get rid of it.
  10343. (org-add-planning-info nil nil 'closed))
  10344. (when (and now-done-p org-log-done)
  10345. ;; It is now done, and it was not done before
  10346. (org-add-planning-info 'closed (org-current-effective-time))
  10347. (if (and (not dolog) (eq 'note org-log-done))
  10348. (org-add-log-setup 'done state this 'findpos 'note)))
  10349. (when (and state dolog)
  10350. ;; This is a non-nil state, and we need to log it
  10351. (org-add-log-setup 'state state this 'findpos dolog)))
  10352. ;; Fixup tag positioning
  10353. (org-todo-trigger-tag-changes state)
  10354. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10355. (when org-provide-todo-statistics
  10356. (org-update-parent-todo-statistics))
  10357. (run-hooks 'org-after-todo-state-change-hook)
  10358. (if (and arg (not (member state org-done-keywords)))
  10359. (setq head (org-get-todo-sequence-head state)))
  10360. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10361. ;; Do we need to trigger a repeat?
  10362. (when now-done-p
  10363. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10364. ;; This is for the agenda, take a snapshot of the headline.
  10365. (save-match-data
  10366. (setq org-agenda-headline-snapshot-before-repeat
  10367. (org-get-heading))))
  10368. (org-auto-repeat-maybe state))
  10369. ;; Fixup cursor location if close to the keyword
  10370. (if (and (outline-on-heading-p)
  10371. (not (bolp))
  10372. (save-excursion (beginning-of-line 1)
  10373. (looking-at org-todo-line-regexp))
  10374. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10375. (progn
  10376. (goto-char (or (match-end 2) (match-end 1)))
  10377. (and (looking-at " ") (just-one-space))))
  10378. (when org-trigger-hook
  10379. (save-excursion
  10380. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10381. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10382. "Block turning an entry into a TODO, using the hierarchy.
  10383. This checks whether the current task should be blocked from state
  10384. changes. Such blocking occurs when:
  10385. 1. The task has children which are not all in a completed state.
  10386. 2. A task has a parent with the property :ORDERED:, and there
  10387. are siblings prior to the current task with incomplete
  10388. status.
  10389. 3. The parent of the task is blocked because it has siblings that should
  10390. be done first, or is child of a block grandparent TODO entry."
  10391. (if (not org-enforce-todo-dependencies)
  10392. t ; if locally turned off don't block
  10393. (catch 'dont-block
  10394. ;; If this is not a todo state change, or if this entry is already DONE,
  10395. ;; do not block
  10396. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10397. (member (plist-get change-plist :from)
  10398. (cons 'done org-done-keywords))
  10399. (member (plist-get change-plist :to)
  10400. (cons 'todo org-not-done-keywords))
  10401. (not (plist-get change-plist :to)))
  10402. (throw 'dont-block t))
  10403. ;; If this task has children, and any are undone, it's blocked
  10404. (save-excursion
  10405. (org-back-to-heading t)
  10406. (let ((this-level (funcall outline-level)))
  10407. (outline-next-heading)
  10408. (let ((child-level (funcall outline-level)))
  10409. (while (and (not (eobp))
  10410. (> child-level this-level))
  10411. ;; this todo has children, check whether they are all
  10412. ;; completed
  10413. (if (and (not (org-entry-is-done-p))
  10414. (org-entry-is-todo-p))
  10415. (throw 'dont-block nil))
  10416. (outline-next-heading)
  10417. (setq child-level (funcall outline-level))))))
  10418. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10419. ;; any previous siblings are undone, it's blocked
  10420. (save-excursion
  10421. (org-back-to-heading t)
  10422. (let* ((pos (point))
  10423. (parent-pos (and (org-up-heading-safe) (point))))
  10424. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10425. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10426. (forward-line 1)
  10427. (re-search-forward org-not-done-heading-regexp pos t))
  10428. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10429. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10430. (while t
  10431. (goto-char parent-pos)
  10432. (if (not (looking-at org-not-done-heading-regexp))
  10433. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10434. (setq pos (point))
  10435. (setq parent-pos (and (org-up-heading-safe) (point)))
  10436. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10437. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10438. (forward-line 1)
  10439. (re-search-forward org-not-done-heading-regexp pos t))
  10440. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10441. (defcustom org-track-ordered-property-with-tag nil
  10442. "Should the ORDERED property also be shown as a tag?
  10443. The ORDERED property decides if an entry should require subtasks to be
  10444. completed in sequence. Since a property is not very visible, setting
  10445. this option means that toggling the ORDERED property with the command
  10446. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10447. not relevant for the behavior, but it makes things more visible.
  10448. Note that toggling the tag with tags commands will not change the property
  10449. and therefore not influence behavior!
  10450. This can be t, meaning the tag ORDERED should be used, It can also be a
  10451. string to select a different tag for this task."
  10452. :group 'org-todo
  10453. :type '(choice
  10454. (const :tag "No tracking" nil)
  10455. (const :tag "Track with ORDERED tag" t)
  10456. (string :tag "Use other tag")))
  10457. (defun org-toggle-ordered-property ()
  10458. "Toggle the ORDERED property of the current entry.
  10459. For better visibility, you can track the value of this property with a tag.
  10460. See variable `org-track-ordered-property-with-tag'."
  10461. (interactive)
  10462. (let* ((t1 org-track-ordered-property-with-tag)
  10463. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10464. (save-excursion
  10465. (org-back-to-heading)
  10466. (if (org-entry-get nil "ORDERED")
  10467. (progn
  10468. (org-delete-property "ORDERED")
  10469. (and tag (org-toggle-tag tag 'off))
  10470. (message "Subtasks can be completed in arbitrary order"))
  10471. (org-entry-put nil "ORDERED" "t")
  10472. (and tag (org-toggle-tag tag 'on))
  10473. (message "Subtasks must be completed in sequence")))))
  10474. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10475. (defun org-block-todo-from-checkboxes (change-plist)
  10476. "Block turning an entry into a TODO, using checkboxes.
  10477. This checks whether the current task should be blocked from state
  10478. changes because there are unchecked boxes in this entry."
  10479. (if (not org-enforce-todo-checkbox-dependencies)
  10480. t ; if locally turned off don't block
  10481. (catch 'dont-block
  10482. ;; If this is not a todo state change, or if this entry is already DONE,
  10483. ;; do not block
  10484. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10485. (member (plist-get change-plist :from)
  10486. (cons 'done org-done-keywords))
  10487. (member (plist-get change-plist :to)
  10488. (cons 'todo org-not-done-keywords))
  10489. (not (plist-get change-plist :to)))
  10490. (throw 'dont-block t))
  10491. ;; If this task has checkboxes that are not checked, it's blocked
  10492. (save-excursion
  10493. (org-back-to-heading t)
  10494. (let ((beg (point)) end)
  10495. (outline-next-heading)
  10496. (setq end (point))
  10497. (goto-char beg)
  10498. (if (org-list-search-forward
  10499. (concat (org-item-beginning-re)
  10500. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10501. "\\[[- ]\\]")
  10502. end t)
  10503. (progn
  10504. (if (boundp 'org-blocked-by-checkboxes)
  10505. (setq org-blocked-by-checkboxes t))
  10506. (throw 'dont-block nil)))))
  10507. t))) ; do not block
  10508. (defun org-entry-blocked-p ()
  10509. "Is the current entry blocked?"
  10510. (if (org-entry-get nil "NOBLOCKING")
  10511. nil ;; Never block this entry
  10512. (not
  10513. (run-hook-with-args-until-failure
  10514. 'org-blocker-hook
  10515. (list :type 'todo-state-change
  10516. :position (point)
  10517. :from 'todo
  10518. :to 'done)))))
  10519. (defun org-update-statistics-cookies (all)
  10520. "Update the statistics cookie, either from TODO or from checkboxes.
  10521. This should be called with the cursor in a line with a statistics cookie."
  10522. (interactive "P")
  10523. (if all
  10524. (progn
  10525. (org-update-checkbox-count 'all)
  10526. (org-map-entries 'org-update-parent-todo-statistics))
  10527. (if (not (org-at-heading-p))
  10528. (org-update-checkbox-count)
  10529. (let ((pos (move-marker (make-marker) (point)))
  10530. end l1 l2)
  10531. (ignore-errors (org-back-to-heading t))
  10532. (if (not (org-at-heading-p))
  10533. (org-update-checkbox-count)
  10534. (setq l1 (org-outline-level))
  10535. (setq end (save-excursion
  10536. (outline-next-heading)
  10537. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10538. (point)))
  10539. (if (and (save-excursion
  10540. (re-search-forward
  10541. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10542. (not (save-excursion (re-search-forward
  10543. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10544. (org-update-checkbox-count)
  10545. (if (and l2 (> l2 l1))
  10546. (progn
  10547. (goto-char end)
  10548. (org-update-parent-todo-statistics))
  10549. (goto-char pos)
  10550. (beginning-of-line 1)
  10551. (while (re-search-forward
  10552. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10553. (point-at-eol) t)
  10554. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10555. (goto-char pos)
  10556. (move-marker pos nil)))))
  10557. (defvar org-entry-property-inherited-from) ;; defined below
  10558. (defun org-update-parent-todo-statistics ()
  10559. "Update any statistics cookie in the parent of the current headline.
  10560. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10561. the entire subtree and this will travel up the hierarchy and update
  10562. statistics everywhere."
  10563. (let* ((prop (save-excursion (org-up-heading-safe)
  10564. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10565. (recursive (or (not org-hierarchical-todo-statistics)
  10566. (and prop (string-match "\\<recursive\\>" prop))))
  10567. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10568. 0))
  10569. (first t)
  10570. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10571. level ltoggle l1 new ndel
  10572. (cnt-all 0) (cnt-done 0) is-percent kwd
  10573. checkbox-beg ov ovs ove cookie-present)
  10574. (catch 'exit
  10575. (save-excursion
  10576. (beginning-of-line 1)
  10577. (setq ltoggle (funcall outline-level))
  10578. ;; Three situations are to consider:
  10579. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10580. ;; to the top-level ancestor on the headline;
  10581. ;; 2. If parent has "recursive" property, repeat up to the
  10582. ;; headline setting that property, taking inheritance into
  10583. ;; account;
  10584. ;; 3. Else, move up to direct parent and proceed only once.
  10585. (while (and (setq level (org-up-heading-safe))
  10586. (or recursive first)
  10587. (>= (point) lim))
  10588. (setq first nil cookie-present nil)
  10589. (unless (and level
  10590. (not (string-match
  10591. "\\<checkbox\\>"
  10592. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10593. "")))))
  10594. (throw 'exit nil))
  10595. (while (re-search-forward box-re (point-at-eol) t)
  10596. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10597. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10598. (save-match-data
  10599. (unless (outline-next-heading) (throw 'exit nil))
  10600. (while (and (looking-at org-complex-heading-regexp)
  10601. (> (setq l1 (length (match-string 1))) level))
  10602. (setq kwd (and (or recursive (= l1 ltoggle))
  10603. (match-string 2)))
  10604. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10605. (and (listp org-provide-todo-statistics)
  10606. (or (member kwd org-provide-todo-statistics)
  10607. (member kwd org-done-keywords))))
  10608. (setq cnt-all (1+ cnt-all))
  10609. (if (eq org-provide-todo-statistics t)
  10610. (and kwd (setq cnt-all (1+ cnt-all)))))
  10611. (and (member kwd org-done-keywords)
  10612. (setq cnt-done (1+ cnt-done)))
  10613. (outline-next-heading)))
  10614. (setq new
  10615. (if is-percent
  10616. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10617. (format "[%d/%d]" cnt-done cnt-all))
  10618. ndel (- (match-end 0) checkbox-beg))
  10619. ;; handle overlays when updating cookie from column view
  10620. (when (setq ov (car (overlays-at checkbox-beg)))
  10621. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10622. (delete-overlay ov))
  10623. (goto-char checkbox-beg)
  10624. (insert new)
  10625. (delete-region (point) (+ (point) ndel))
  10626. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10627. (when ov (move-overlay ov ovs ove)))
  10628. (when cookie-present
  10629. (run-hook-with-args 'org-after-todo-statistics-hook
  10630. cnt-done (- cnt-all cnt-done))))))
  10631. (run-hooks 'org-todo-statistics-hook)))
  10632. (defvar org-after-todo-statistics-hook nil
  10633. "Hook that is called after a TODO statistics cookie has been updated.
  10634. Each function is called with two arguments: the number of not-done entries
  10635. and the number of done entries.
  10636. For example, the following function, when added to this hook, will switch
  10637. an entry to DONE when all children are done, and back to TODO when new
  10638. entries are set to a TODO status. Note that this hook is only called
  10639. when there is a statistics cookie in the headline!
  10640. (defun org-summary-todo (n-done n-not-done)
  10641. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10642. (let (org-log-done org-log-states) ; turn off logging
  10643. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10644. ")
  10645. (defvar org-todo-statistics-hook nil
  10646. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10647. This hook runs even if there is no statistics cookie present, in which case
  10648. `org-after-todo-statistics-hook' would not run.")
  10649. (defun org-todo-trigger-tag-changes (state)
  10650. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10651. (let ((l org-todo-state-tags-triggers)
  10652. changes)
  10653. (when (or (not state) (equal state ""))
  10654. (setq changes (append changes (cdr (assoc "" l)))))
  10655. (when (and (stringp state) (> (length state) 0))
  10656. (setq changes (append changes (cdr (assoc state l)))))
  10657. (when (member state org-not-done-keywords)
  10658. (setq changes (append changes (cdr (assoc 'todo l)))))
  10659. (when (member state org-done-keywords)
  10660. (setq changes (append changes (cdr (assoc 'done l)))))
  10661. (dolist (c changes)
  10662. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10663. (defun org-local-logging (value)
  10664. "Get logging settings from a property VALUE."
  10665. (let* (words w a)
  10666. ;; directly set the variables, they are already local.
  10667. (setq org-log-done nil
  10668. org-log-repeat nil
  10669. org-todo-log-states nil)
  10670. (setq words (org-split-string value))
  10671. (while (setq w (pop words))
  10672. (cond
  10673. ((setq a (assoc w org-startup-options))
  10674. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10675. (set (nth 1 a) (nth 2 a))))
  10676. ((setq a (org-extract-log-state-settings w))
  10677. (and (member (car a) org-todo-keywords-1)
  10678. (push a org-todo-log-states)))))))
  10679. (defun org-get-todo-sequence-head (kwd)
  10680. "Return the head of the TODO sequence to which KWD belongs.
  10681. If KWD is not set, check if there is a text property remembering the
  10682. right sequence."
  10683. (let (p)
  10684. (cond
  10685. ((not kwd)
  10686. (or (get-text-property (point-at-bol) 'org-todo-head)
  10687. (progn
  10688. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10689. nil (point-at-eol)))
  10690. (get-text-property p 'org-todo-head))))
  10691. ((not (member kwd org-todo-keywords-1))
  10692. (car org-todo-keywords-1))
  10693. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10694. (defun org-fast-todo-selection ()
  10695. "Fast TODO keyword selection with single keys.
  10696. Returns the new TODO keyword, or nil if no state change should occur."
  10697. (let* ((fulltable org-todo-key-alist)
  10698. (done-keywords org-done-keywords) ;; needed for the faces.
  10699. (maxlen (apply 'max (mapcar
  10700. (lambda (x)
  10701. (if (stringp (car x)) (string-width (car x)) 0))
  10702. fulltable)))
  10703. (expert nil)
  10704. (fwidth (+ maxlen 3 1 3))
  10705. (ncol (/ (- (window-width) 4) fwidth))
  10706. tg cnt e c tbl
  10707. groups ingroup)
  10708. (save-excursion
  10709. (save-window-excursion
  10710. (if expert
  10711. (set-buffer (get-buffer-create " *Org todo*"))
  10712. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10713. (erase-buffer)
  10714. (org-set-local 'org-done-keywords done-keywords)
  10715. (setq tbl fulltable cnt 0)
  10716. (while (setq e (pop tbl))
  10717. (cond
  10718. ((equal e '(:startgroup))
  10719. (push '() groups) (setq ingroup t)
  10720. (when (not (= cnt 0))
  10721. (setq cnt 0)
  10722. (insert "\n"))
  10723. (insert "{ "))
  10724. ((equal e '(:endgroup))
  10725. (setq ingroup nil cnt 0)
  10726. (insert "}\n"))
  10727. ((equal e '(:newline))
  10728. (when (not (= cnt 0))
  10729. (setq cnt 0)
  10730. (insert "\n")
  10731. (setq e (car tbl))
  10732. (while (equal (car tbl) '(:newline))
  10733. (insert "\n")
  10734. (setq tbl (cdr tbl)))))
  10735. (t
  10736. (setq tg (car e) c (cdr e))
  10737. (if ingroup (push tg (car groups)))
  10738. (setq tg (org-add-props tg nil 'face
  10739. (org-get-todo-face tg)))
  10740. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10741. (insert "[" c "] " tg (make-string
  10742. (- fwidth 4 (length tg)) ?\ ))
  10743. (when (= (setq cnt (1+ cnt)) ncol)
  10744. (insert "\n")
  10745. (if ingroup (insert " "))
  10746. (setq cnt 0)))))
  10747. (insert "\n")
  10748. (goto-char (point-min))
  10749. (if (not expert) (org-fit-window-to-buffer))
  10750. (message "[a-z..]:Set [SPC]:clear")
  10751. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10752. (cond
  10753. ((or (= c ?\C-g)
  10754. (and (= c ?q) (not (rassoc c fulltable))))
  10755. (setq quit-flag t))
  10756. ((= c ?\ ) nil)
  10757. ((setq e (rassoc c fulltable) tg (car e))
  10758. tg)
  10759. (t (setq quit-flag t)))))))
  10760. (defun org-entry-is-todo-p ()
  10761. (member (org-get-todo-state) org-not-done-keywords))
  10762. (defun org-entry-is-done-p ()
  10763. (member (org-get-todo-state) org-done-keywords))
  10764. (defun org-get-todo-state ()
  10765. (save-excursion
  10766. (org-back-to-heading t)
  10767. (and (looking-at org-todo-line-regexp)
  10768. (match-end 2)
  10769. (match-string 2))))
  10770. (defun org-at-date-range-p (&optional inactive-ok)
  10771. "Is the cursor inside a date range?"
  10772. (interactive)
  10773. (save-excursion
  10774. (catch 'exit
  10775. (let ((pos (point)))
  10776. (skip-chars-backward "^[<\r\n")
  10777. (skip-chars-backward "<[")
  10778. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10779. (>= (match-end 0) pos)
  10780. (throw 'exit t))
  10781. (skip-chars-backward "^<[\r\n")
  10782. (skip-chars-backward "<[")
  10783. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10784. (>= (match-end 0) pos)
  10785. (throw 'exit t)))
  10786. nil)))
  10787. (defun org-get-repeat (&optional tagline)
  10788. "Check if there is a deadline/schedule with repeater in this entry."
  10789. (save-match-data
  10790. (save-excursion
  10791. (org-back-to-heading t)
  10792. (and (re-search-forward (if tagline
  10793. (concat tagline "\\s-*" org-repeat-re)
  10794. org-repeat-re)
  10795. (org-entry-end-position) t)
  10796. (match-string-no-properties 1)))))
  10797. (defvar org-last-changed-timestamp)
  10798. (defvar org-last-inserted-timestamp)
  10799. (defvar org-log-post-message)
  10800. (defvar org-log-note-purpose)
  10801. (defvar org-log-note-how)
  10802. (defvar org-log-note-extra)
  10803. (defun org-auto-repeat-maybe (done-word)
  10804. "Check if the current headline contains a repeated deadline/schedule.
  10805. If yes, set TODO state back to what it was and change the base date
  10806. of repeating deadline/scheduled time stamps to new date.
  10807. This function is run automatically after each state change to a DONE state."
  10808. ;; last-state is dynamically scoped into this function
  10809. (let* ((repeat (org-get-repeat))
  10810. (aa (assoc last-state org-todo-kwd-alist))
  10811. (interpret (nth 1 aa))
  10812. (head (nth 2 aa))
  10813. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10814. (msg "Entry repeats: ")
  10815. (org-log-done nil)
  10816. (org-todo-log-states nil)
  10817. re type n what ts time to-state)
  10818. (when repeat
  10819. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10820. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10821. org-todo-repeat-to-state))
  10822. (unless (and to-state (member to-state org-todo-keywords-1))
  10823. (setq to-state (if (eq interpret 'type) last-state head)))
  10824. (org-todo to-state)
  10825. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10826. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10827. (org-time-stamp-format t t))))
  10828. (when org-log-repeat
  10829. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10830. (memq 'org-add-log-note post-command-hook))
  10831. ;; OK, we are already setup for some record
  10832. (if (eq org-log-repeat 'note)
  10833. ;; make sure we take a note, not only a time stamp
  10834. (setq org-log-note-how 'note))
  10835. ;; Set up for taking a record
  10836. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10837. last-state
  10838. 'findpos org-log-repeat)))
  10839. (org-back-to-heading t)
  10840. (org-add-planning-info nil nil 'closed)
  10841. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10842. org-deadline-time-regexp "\\)\\|\\("
  10843. org-ts-regexp "\\)"))
  10844. (while (re-search-forward
  10845. re (save-excursion (outline-next-heading) (point)) t)
  10846. (setq type (if (match-end 1) org-scheduled-string
  10847. (if (match-end 3) org-deadline-string "Plain:"))
  10848. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10849. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10850. (setq n (string-to-number (match-string 2 ts))
  10851. what (match-string 3 ts))
  10852. (if (equal what "w") (setq n (* n 7) what "d"))
  10853. ;; Preparation, see if we need to modify the start date for the change
  10854. (when (match-end 1)
  10855. (setq time (save-match-data (org-time-string-to-time ts)))
  10856. (cond
  10857. ((equal (match-string 1 ts) ".")
  10858. ;; Shift starting date to today
  10859. (org-timestamp-change
  10860. (- (org-today) (time-to-days time))
  10861. 'day))
  10862. ((equal (match-string 1 ts) "+")
  10863. (let ((nshiftmax 10) (nshift 0))
  10864. (while (or (= nshift 0)
  10865. (<= (time-to-days time)
  10866. (time-to-days (current-time))))
  10867. (when (= (incf nshift) nshiftmax)
  10868. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10869. (error "Abort")))
  10870. (org-timestamp-change n (cdr (assoc what whata)))
  10871. (org-at-timestamp-p t)
  10872. (setq ts (match-string 1))
  10873. (setq time (save-match-data (org-time-string-to-time ts)))))
  10874. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10875. ;; rematch, so that we have everything in place for the real shift
  10876. (org-at-timestamp-p t)
  10877. (setq ts (match-string 1))
  10878. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10879. (org-timestamp-change n (cdr (assoc what whata)))
  10880. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10881. (setq org-log-post-message msg)
  10882. (message "%s" msg))))
  10883. (defun org-show-todo-tree (arg)
  10884. "Make a compact tree which shows all headlines marked with TODO.
  10885. The tree will show the lines where the regexp matches, and all higher
  10886. headlines above the match.
  10887. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10888. With a numeric prefix N, construct a sparse tree for the Nth element
  10889. of `org-todo-keywords-1'."
  10890. (interactive "P")
  10891. (let ((case-fold-search nil)
  10892. (kwd-re
  10893. (cond ((null arg) org-not-done-regexp)
  10894. ((equal arg '(4))
  10895. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10896. (mapcar 'list org-todo-keywords-1))))
  10897. (concat "\\("
  10898. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10899. "\\)\\>")))
  10900. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10901. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10902. org-todo-keywords-1)))
  10903. (t (error "Invalid prefix argument: %s" arg)))))
  10904. (message "%d TODO entries found"
  10905. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10906. (defun org-deadline (&optional remove time)
  10907. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10908. With argument REMOVE, remove any deadline from the item.
  10909. With argument TIME, set the deadline at the corresponding date. TIME
  10910. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10911. (interactive "P")
  10912. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10913. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10914. 'region-start-level 'region))
  10915. org-loop-over-headlines-in-active-region)
  10916. (org-map-entries
  10917. `(org-deadline ',remove ,time)
  10918. org-loop-over-headlines-in-active-region
  10919. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10920. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10921. (repeater (and old-date
  10922. (string-match
  10923. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10924. old-date)
  10925. (match-string 1 old-date))))
  10926. (if remove
  10927. (progn
  10928. (when (and old-date org-log-redeadline)
  10929. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10930. org-log-redeadline))
  10931. (org-remove-timestamp-with-keyword org-deadline-string)
  10932. (message "Item no longer has a deadline."))
  10933. (org-add-planning-info 'deadline time 'closed)
  10934. (when (and old-date org-log-redeadline
  10935. (not (equal old-date
  10936. (substring org-last-inserted-timestamp 1 -1))))
  10937. (org-add-log-setup 'redeadline nil old-date 'findpos
  10938. org-log-redeadline))
  10939. (when repeater
  10940. (save-excursion
  10941. (org-back-to-heading t)
  10942. (when (re-search-forward (concat org-deadline-string " "
  10943. org-last-inserted-timestamp)
  10944. (save-excursion
  10945. (outline-next-heading) (point)) t)
  10946. (goto-char (1- (match-end 0)))
  10947. (insert " " repeater)
  10948. (setq org-last-inserted-timestamp
  10949. (concat (substring org-last-inserted-timestamp 0 -1)
  10950. " " repeater
  10951. (substring org-last-inserted-timestamp -1))))))
  10952. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10953. (defun org-schedule (&optional remove time)
  10954. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10955. With argument REMOVE, remove any scheduling date from the item.
  10956. With argument TIME, scheduled at the corresponding date. TIME can
  10957. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10958. (interactive "P")
  10959. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10960. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10961. 'region-start-level 'region))
  10962. org-loop-over-headlines-in-active-region)
  10963. (org-map-entries
  10964. `(org-schedule ',remove ,time)
  10965. org-loop-over-headlines-in-active-region
  10966. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10967. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10968. (repeater (and old-date
  10969. (string-match
  10970. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10971. old-date)
  10972. (match-string 1 old-date))))
  10973. (if remove
  10974. (progn
  10975. (when (and old-date org-log-reschedule)
  10976. (org-add-log-setup 'delschedule nil old-date 'findpos
  10977. org-log-reschedule))
  10978. (org-remove-timestamp-with-keyword org-scheduled-string)
  10979. (message "Item is no longer scheduled."))
  10980. (org-add-planning-info 'scheduled time 'closed)
  10981. (when (and old-date org-log-reschedule
  10982. (not (equal old-date
  10983. (substring org-last-inserted-timestamp 1 -1))))
  10984. (org-add-log-setup 'reschedule nil old-date 'findpos
  10985. org-log-reschedule))
  10986. (when repeater
  10987. (save-excursion
  10988. (org-back-to-heading t)
  10989. (when (re-search-forward (concat org-scheduled-string " "
  10990. org-last-inserted-timestamp)
  10991. (save-excursion
  10992. (outline-next-heading) (point)) t)
  10993. (goto-char (1- (match-end 0)))
  10994. (insert " " repeater)
  10995. (setq org-last-inserted-timestamp
  10996. (concat (substring org-last-inserted-timestamp 0 -1)
  10997. " " repeater
  10998. (substring org-last-inserted-timestamp -1))))))
  10999. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11000. (defun org-get-scheduled-time (pom &optional inherit)
  11001. "Get the scheduled time as a time tuple, of a format suitable
  11002. for calling org-schedule with, or if there is no scheduling,
  11003. returns nil."
  11004. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11005. (when time
  11006. (apply 'encode-time (org-parse-time-string time)))))
  11007. (defun org-get-deadline-time (pom &optional inherit)
  11008. "Get the deadline as a time tuple, of a format suitable for
  11009. calling org-deadline with, or if there is no scheduling, returns
  11010. nil."
  11011. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11012. (when time
  11013. (apply 'encode-time (org-parse-time-string time)))))
  11014. (defun org-remove-timestamp-with-keyword (keyword)
  11015. "Remove all time stamps with KEYWORD in the current entry."
  11016. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11017. beg)
  11018. (save-excursion
  11019. (org-back-to-heading t)
  11020. (setq beg (point))
  11021. (outline-next-heading)
  11022. (while (re-search-backward re beg t)
  11023. (replace-match "")
  11024. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11025. (equal (char-before) ?\ ))
  11026. (backward-delete-char 1)
  11027. (if (string-match "^[ \t]*$" (buffer-substring
  11028. (point-at-bol) (point-at-eol)))
  11029. (delete-region (point-at-bol)
  11030. (min (point-max) (1+ (point-at-eol))))))))))
  11031. (defun org-add-planning-info (what &optional time &rest remove)
  11032. "Insert new timestamp with keyword in the line directly after the headline.
  11033. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11034. If non is given, the user is prompted for a date.
  11035. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11036. be removed."
  11037. (interactive)
  11038. (let (org-time-was-given org-end-time-was-given ts
  11039. end default-time default-input)
  11040. (catch 'exit
  11041. (when (and (memq what '(scheduled deadline))
  11042. (or (not time)
  11043. (and (stringp time)
  11044. (string-match "^[-+]+[0-9]" time))))
  11045. ;; Try to get a default date/time from existing timestamp
  11046. (save-excursion
  11047. (org-back-to-heading t)
  11048. (setq end (save-excursion (outline-next-heading) (point)))
  11049. (when (re-search-forward (if (eq what 'scheduled)
  11050. org-scheduled-time-regexp
  11051. org-deadline-time-regexp)
  11052. end t)
  11053. (setq ts (match-string 1)
  11054. default-time
  11055. (apply 'encode-time (org-parse-time-string ts))
  11056. default-input (and ts (org-get-compact-tod ts))))))
  11057. (when what
  11058. (setq time
  11059. (if (stringp time)
  11060. ;; This is a string (relative or absolute), set proper date
  11061. (apply 'encode-time
  11062. (org-read-date-analyze
  11063. time default-time (decode-time default-time)))
  11064. ;; If necessary, get the time from the user
  11065. (or time (org-read-date nil 'to-time nil nil
  11066. default-time default-input)))))
  11067. (when (and org-insert-labeled-timestamps-at-point
  11068. (member what '(scheduled deadline)))
  11069. (insert
  11070. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11071. (org-insert-time-stamp time org-time-was-given
  11072. nil nil nil (list org-end-time-was-given))
  11073. (setq what nil))
  11074. (save-excursion
  11075. (save-restriction
  11076. (let (col list elt ts buffer-invisibility-spec)
  11077. (org-back-to-heading t)
  11078. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11079. (goto-char (match-end 1))
  11080. (setq col (current-column))
  11081. (goto-char (match-end 0))
  11082. (if (eobp) (insert "\n") (forward-char 1))
  11083. (when (and (not what)
  11084. (not (looking-at
  11085. (concat "[ \t]*"
  11086. org-keyword-time-not-clock-regexp))))
  11087. ;; Nothing to add, nothing to remove...... :-)
  11088. (throw 'exit nil))
  11089. (if (and (not (looking-at org-outline-regexp))
  11090. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11091. "[^\r\n]*"))
  11092. (not (equal (match-string 1) org-clock-string)))
  11093. (narrow-to-region (match-beginning 0) (match-end 0))
  11094. (insert-before-markers "\n")
  11095. (backward-char 1)
  11096. (narrow-to-region (point) (point))
  11097. (and org-adapt-indentation (org-indent-to-column col)))
  11098. ;; Check if we have to remove something.
  11099. (setq list (cons what remove))
  11100. (while list
  11101. (setq elt (pop list))
  11102. (when (or (and (eq elt 'scheduled)
  11103. (re-search-forward org-scheduled-time-regexp nil t))
  11104. (and (eq elt 'deadline)
  11105. (re-search-forward org-deadline-time-regexp nil t))
  11106. (and (eq elt 'closed)
  11107. (re-search-forward org-closed-time-regexp nil t)))
  11108. (replace-match "")
  11109. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11110. (and (looking-at "[ \t]+") (replace-match ""))
  11111. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11112. (when what
  11113. (insert
  11114. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11115. (cond ((eq what 'scheduled) org-scheduled-string)
  11116. ((eq what 'deadline) org-deadline-string)
  11117. ((eq what 'closed) org-closed-string))
  11118. " ")
  11119. (setq ts (org-insert-time-stamp
  11120. time
  11121. (or org-time-was-given
  11122. (and (eq what 'closed) org-log-done-with-time))
  11123. (eq what 'closed)
  11124. nil nil (list org-end-time-was-given)))
  11125. (insert
  11126. (if (not (or (bolp) (eq (char-before) ?\ )
  11127. (memq (char-after) '(32 10))
  11128. (eobp))) " " ""))
  11129. (end-of-line 1))
  11130. (goto-char (point-min))
  11131. (widen)
  11132. (if (and (looking-at "[ \t]*\n")
  11133. (equal (char-before) ?\n))
  11134. (delete-region (1- (point)) (point-at-eol)))
  11135. ts))))))
  11136. (defvar org-log-note-marker (make-marker))
  11137. (defvar org-log-note-purpose nil)
  11138. (defvar org-log-note-state nil)
  11139. (defvar org-log-note-previous-state nil)
  11140. (defvar org-log-note-how nil)
  11141. (defvar org-log-note-extra nil)
  11142. (defvar org-log-note-window-configuration nil)
  11143. (defvar org-log-note-return-to (make-marker))
  11144. (defvar org-log-note-effective-time nil
  11145. "Remembered current time so that dynamically scoped
  11146. `org-extend-today-until' affects tha timestamps in state change
  11147. log")
  11148. (defvar org-log-post-message nil
  11149. "Message to be displayed after a log note has been stored.
  11150. The auto-repeater uses this.")
  11151. (defun org-add-note ()
  11152. "Add a note to the current entry.
  11153. This is done in the same way as adding a state change note."
  11154. (interactive)
  11155. (org-add-log-setup 'note nil nil 'findpos nil))
  11156. (defvar org-property-end-re)
  11157. (defun org-add-log-setup (&optional purpose state prev-state
  11158. findpos how extra)
  11159. "Set up the post command hook to take a note.
  11160. If this is about to TODO state change, the new state is expected in STATE.
  11161. When FINDPOS is non-nil, find the correct position for the note in
  11162. the current entry. If not, assume that it can be inserted at point.
  11163. HOW is an indicator what kind of note should be created.
  11164. EXTRA is additional text that will be inserted into the notes buffer."
  11165. (let* ((org-log-into-drawer (org-log-into-drawer))
  11166. (drawer (cond ((stringp org-log-into-drawer)
  11167. org-log-into-drawer)
  11168. (org-log-into-drawer "LOGBOOK")
  11169. (t nil))))
  11170. (save-restriction
  11171. (save-excursion
  11172. (when findpos
  11173. (org-back-to-heading t)
  11174. (narrow-to-region (point) (save-excursion
  11175. (outline-next-heading) (point)))
  11176. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11177. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11178. "[^\r\n]*\\)?"))
  11179. (goto-char (match-end 0))
  11180. (cond
  11181. (drawer
  11182. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11183. nil t)
  11184. (progn
  11185. (goto-char (match-end 0))
  11186. (or org-log-states-order-reversed
  11187. (and (re-search-forward org-property-end-re nil t)
  11188. (goto-char (1- (match-beginning 0))))))
  11189. (insert "\n:" drawer ":\n:END:")
  11190. (beginning-of-line 0)
  11191. (org-indent-line-function)
  11192. (beginning-of-line 2)
  11193. (org-indent-line-function)
  11194. (end-of-line 0)))
  11195. ((and org-log-state-notes-insert-after-drawers
  11196. (save-excursion
  11197. (forward-line) (looking-at org-drawer-regexp)))
  11198. (forward-line)
  11199. (while (looking-at org-drawer-regexp)
  11200. (goto-char (match-end 0))
  11201. (re-search-forward org-property-end-re (point-max) t)
  11202. (forward-line))
  11203. (forward-line -1)))
  11204. (unless org-log-states-order-reversed
  11205. (and (= (char-after) ?\n) (forward-char 1))
  11206. (org-skip-over-state-notes)
  11207. (skip-chars-backward " \t\n\r")))
  11208. (move-marker org-log-note-marker (point))
  11209. (setq org-log-note-purpose purpose
  11210. org-log-note-state state
  11211. org-log-note-previous-state prev-state
  11212. org-log-note-how how
  11213. org-log-note-extra extra
  11214. org-log-note-effective-time (org-current-effective-time))
  11215. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11216. (defun org-skip-over-state-notes ()
  11217. "Skip past the list of State notes in an entry."
  11218. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11219. (when (ignore-errors (goto-char (org-in-item-p)))
  11220. (let* ((struct (org-list-struct))
  11221. (prevs (org-list-prevs-alist struct)))
  11222. (while (looking-at "[ \t]*- State")
  11223. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11224. (org-list-get-item-end (point) struct)))))))
  11225. (defun org-add-log-note (&optional purpose)
  11226. "Pop up a window for taking a note, and add this note later at point."
  11227. (remove-hook 'post-command-hook 'org-add-log-note)
  11228. (setq org-log-note-window-configuration (current-window-configuration))
  11229. (delete-other-windows)
  11230. (move-marker org-log-note-return-to (point))
  11231. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11232. (goto-char org-log-note-marker)
  11233. (org-switch-to-buffer-other-window "*Org Note*")
  11234. (erase-buffer)
  11235. (if (memq org-log-note-how '(time state))
  11236. (let (current-prefix-arg) (org-store-log-note))
  11237. (let ((org-inhibit-startup t)) (org-mode))
  11238. (insert (format "# Insert note for %s.
  11239. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11240. (cond
  11241. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11242. ((eq org-log-note-purpose 'done) "closed todo item")
  11243. ((eq org-log-note-purpose 'state)
  11244. (format "state change from \"%s\" to \"%s\""
  11245. (or org-log-note-previous-state "")
  11246. (or org-log-note-state "")))
  11247. ((eq org-log-note-purpose 'reschedule)
  11248. "rescheduling")
  11249. ((eq org-log-note-purpose 'delschedule)
  11250. "no longer scheduled")
  11251. ((eq org-log-note-purpose 'redeadline)
  11252. "changing deadline")
  11253. ((eq org-log-note-purpose 'deldeadline)
  11254. "removing deadline")
  11255. ((eq org-log-note-purpose 'refile)
  11256. "refiling")
  11257. ((eq org-log-note-purpose 'note)
  11258. "this entry")
  11259. (t (error "This should not happen")))))
  11260. (if org-log-note-extra (insert org-log-note-extra))
  11261. (org-set-local 'org-finish-function 'org-store-log-note)
  11262. (run-hooks 'org-log-buffer-setup-hook)))
  11263. (defvar org-note-abort nil) ; dynamically scoped
  11264. (defun org-store-log-note ()
  11265. "Finish taking a log note, and insert it to where it belongs."
  11266. (let ((txt (buffer-string))
  11267. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11268. lines ind bul)
  11269. (kill-buffer (current-buffer))
  11270. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11271. (setq txt (replace-match "" t t txt)))
  11272. (if (string-match "\\s-+\\'" txt)
  11273. (setq txt (replace-match "" t t txt)))
  11274. (setq lines (org-split-string txt "\n"))
  11275. (when (and note (string-match "\\S-" note))
  11276. (setq note
  11277. (org-replace-escapes
  11278. note
  11279. (list (cons "%u" (user-login-name))
  11280. (cons "%U" user-full-name)
  11281. (cons "%t" (format-time-string
  11282. (org-time-stamp-format 'long 'inactive)
  11283. org-log-note-effective-time))
  11284. (cons "%T" (format-time-string
  11285. (org-time-stamp-format 'long nil)
  11286. org-log-note-effective-time))
  11287. (cons "%d" (format-time-string
  11288. (org-time-stamp-format nil 'inactive)
  11289. org-log-note-effective-time))
  11290. (cons "%D" (format-time-string
  11291. (org-time-stamp-format nil nil)
  11292. org-log-note-effective-time))
  11293. (cons "%s" (if org-log-note-state
  11294. (concat "\"" org-log-note-state "\"")
  11295. ""))
  11296. (cons "%S" (if org-log-note-previous-state
  11297. (concat "\"" org-log-note-previous-state "\"")
  11298. "\"\"")))))
  11299. (if lines (setq note (concat note " \\\\")))
  11300. (push note lines))
  11301. (when (or current-prefix-arg org-note-abort)
  11302. (when org-log-into-drawer
  11303. (org-remove-empty-drawer-at
  11304. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11305. org-log-note-marker))
  11306. (setq lines nil))
  11307. (when lines
  11308. (with-current-buffer (marker-buffer org-log-note-marker)
  11309. (save-excursion
  11310. (goto-char org-log-note-marker)
  11311. (move-marker org-log-note-marker nil)
  11312. (end-of-line 1)
  11313. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11314. (setq ind (save-excursion
  11315. (if (ignore-errors (goto-char (org-in-item-p)))
  11316. (let ((struct (org-list-struct)))
  11317. (org-list-get-ind
  11318. (org-list-get-top-point struct) struct))
  11319. (skip-chars-backward " \r\t\n")
  11320. (cond
  11321. ((and (org-at-heading-p)
  11322. org-adapt-indentation)
  11323. (1+ (org-current-level)))
  11324. ((org-at-heading-p) 0)
  11325. (t (org-get-indentation))))))
  11326. (setq bul (org-list-bullet-string "-"))
  11327. (org-indent-line-to ind)
  11328. (insert bul (pop lines))
  11329. (let ((ind-body (+ (length bul) ind)))
  11330. (while lines
  11331. (insert "\n")
  11332. (org-indent-line-to ind-body)
  11333. (insert (pop lines))))
  11334. (message "Note stored")
  11335. (org-back-to-heading t)
  11336. (org-cycle-hide-drawers 'children)))))
  11337. (set-window-configuration org-log-note-window-configuration)
  11338. (with-current-buffer (marker-buffer org-log-note-return-to)
  11339. (goto-char org-log-note-return-to))
  11340. (move-marker org-log-note-return-to nil)
  11341. (and org-log-post-message (message "%s" org-log-post-message)))
  11342. (defun org-remove-empty-drawer-at (drawer pos)
  11343. "Remove an empty drawer DRAWER at position POS.
  11344. POS may also be a marker."
  11345. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11346. (save-excursion
  11347. (save-restriction
  11348. (widen)
  11349. (goto-char pos)
  11350. (if (org-in-regexp
  11351. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11352. (replace-match ""))))))
  11353. (defun org-sparse-tree (&optional arg)
  11354. "Create a sparse tree, prompt for the details.
  11355. This command can create sparse trees. You first need to select the type
  11356. of match used to create the tree:
  11357. t Show all TODO entries.
  11358. T Show entries with a specific TODO keyword.
  11359. m Show entries selected by a tags/property match.
  11360. p Enter a property name and its value (both with completion on existing
  11361. names/values) and show entries with that property.
  11362. r Show entries matching a regular expression (`/' can be used as well)
  11363. d Show deadlines due within `org-deadline-warning-days'.
  11364. b Show deadlines and scheduled items before a date.
  11365. a Show deadlines and scheduled items after a date."
  11366. (interactive "P")
  11367. (let (ans kwd value)
  11368. (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")
  11369. (setq ans (read-char-exclusive))
  11370. (cond
  11371. ((equal ans ?d)
  11372. (call-interactively 'org-check-deadlines))
  11373. ((equal ans ?b)
  11374. (call-interactively 'org-check-before-date))
  11375. ((equal ans ?a)
  11376. (call-interactively 'org-check-after-date))
  11377. ((equal ans ?D)
  11378. (call-interactively 'org-check-dates-range))
  11379. ((equal ans ?t)
  11380. (org-show-todo-tree nil))
  11381. ((equal ans ?T)
  11382. (org-show-todo-tree '(4)))
  11383. ((member ans '(?T ?m))
  11384. (call-interactively 'org-match-sparse-tree))
  11385. ((member ans '(?p ?P))
  11386. (setq kwd (org-icompleting-read "Property: "
  11387. (mapcar 'list (org-buffer-property-keys))))
  11388. (setq value (org-icompleting-read "Value: "
  11389. (mapcar 'list (org-property-values kwd))))
  11390. (unless (string-match "\\`{.*}\\'" value)
  11391. (setq value (concat "\"" value "\"")))
  11392. (org-match-sparse-tree arg (concat kwd "=" value)))
  11393. ((member ans '(?r ?R ?/))
  11394. (call-interactively 'org-occur))
  11395. (t (error "No such sparse tree command \"%c\"" ans)))))
  11396. (defvar org-occur-highlights nil
  11397. "List of overlays used for occur matches.")
  11398. (make-variable-buffer-local 'org-occur-highlights)
  11399. (defvar org-occur-parameters nil
  11400. "Parameters of the active org-occur calls.
  11401. This is a list, each call to org-occur pushes as cons cell,
  11402. containing the regular expression and the callback, onto the list.
  11403. The list can contain several entries if `org-occur' has been called
  11404. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11405. will only contain one set of parameters. When the highlights are
  11406. removed (for example with `C-c C-c', or with the next edit (depending
  11407. on `org-remove-highlights-with-change'), this variable is emptied
  11408. as well.")
  11409. (make-variable-buffer-local 'org-occur-parameters)
  11410. (defun org-occur (regexp &optional keep-previous callback)
  11411. "Make a compact tree which shows all matches of REGEXP.
  11412. The tree will show the lines where the regexp matches, and all higher
  11413. headlines above the match. It will also show the heading after the match,
  11414. to make sure editing the matching entry is easy.
  11415. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11416. call to `org-occur' will be kept, to allow stacking of calls to this
  11417. command.
  11418. If CALLBACK is non-nil, it is a function which is called to confirm
  11419. that the match should indeed be shown."
  11420. (interactive "sRegexp: \nP")
  11421. (when (equal regexp "")
  11422. (error "Regexp cannot be empty"))
  11423. (unless keep-previous
  11424. (org-remove-occur-highlights nil nil t))
  11425. (push (cons regexp callback) org-occur-parameters)
  11426. (let ((cnt 0))
  11427. (save-excursion
  11428. (goto-char (point-min))
  11429. (if (or (not keep-previous) ; do not want to keep
  11430. (not org-occur-highlights)) ; no previous matches
  11431. ;; hide everything
  11432. (org-overview))
  11433. (while (re-search-forward regexp nil t)
  11434. (when (or (not callback)
  11435. (save-match-data (funcall callback)))
  11436. (setq cnt (1+ cnt))
  11437. (when org-highlight-sparse-tree-matches
  11438. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11439. (org-show-context 'occur-tree))))
  11440. (when org-remove-highlights-with-change
  11441. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11442. nil 'local))
  11443. (unless org-sparse-tree-open-archived-trees
  11444. (org-hide-archived-subtrees (point-min) (point-max)))
  11445. (run-hooks 'org-occur-hook)
  11446. (if (org-called-interactively-p 'interactive)
  11447. (message "%d match(es) for regexp %s" cnt regexp))
  11448. cnt))
  11449. (defun org-occur-next-match (&optional n reset)
  11450. "Function for `next-error-function' to find sparse tree matches.
  11451. N is the number of matches to move, when negative move backwards.
  11452. RESET is entirely ignored - this function always goes back to the
  11453. starting point when no match is found."
  11454. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11455. (search-func (if (< n 0)
  11456. 'previous-single-char-property-change
  11457. 'next-single-char-property-change))
  11458. (n (abs n))
  11459. (pos (point))
  11460. p1)
  11461. (catch 'exit
  11462. (while (setq p1 (funcall search-func (point) 'org-type))
  11463. (when (equal p1 limit)
  11464. (goto-char pos)
  11465. (error "No more matches"))
  11466. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11467. (setq n (1- n))
  11468. (when (= n 0)
  11469. (goto-char p1)
  11470. (throw 'exit (point))))
  11471. (goto-char p1))
  11472. (goto-char p1)
  11473. (error "No more matches"))))
  11474. (defun org-show-context (&optional key)
  11475. "Make sure point and context are visible.
  11476. How much context is shown depends upon the variables
  11477. `org-show-hierarchy-above', `org-show-following-heading',
  11478. `org-show-entry-below' and `org-show-siblings'."
  11479. (let ((heading-p (org-at-heading-p t))
  11480. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11481. (following-p (org-get-alist-option org-show-following-heading key))
  11482. (entry-p (org-get-alist-option org-show-entry-below key))
  11483. (siblings-p (org-get-alist-option org-show-siblings key)))
  11484. (catch 'exit
  11485. ;; Show heading or entry text
  11486. (if (and heading-p (not entry-p))
  11487. (org-flag-heading nil) ; only show the heading
  11488. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11489. (org-show-hidden-entry))) ; show entire entry
  11490. (when following-p
  11491. ;; Show next sibling, or heading below text
  11492. (save-excursion
  11493. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11494. (org-flag-heading nil))))
  11495. (when siblings-p (org-show-siblings))
  11496. (when hierarchy-p
  11497. ;; show all higher headings, possibly with siblings
  11498. (save-excursion
  11499. (while (and (condition-case nil
  11500. (progn (org-up-heading-all 1) t)
  11501. (error nil))
  11502. (not (bobp)))
  11503. (org-flag-heading nil)
  11504. (when siblings-p (org-show-siblings))))))))
  11505. (defvar org-reveal-start-hook nil
  11506. "Hook run before revealing a location.")
  11507. (defun org-reveal (&optional siblings)
  11508. "Show current entry, hierarchy above it, and the following headline.
  11509. This can be used to show a consistent set of context around locations
  11510. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11511. not t for the search context.
  11512. With optional argument SIBLINGS, on each level of the hierarchy all
  11513. siblings are shown. This repairs the tree structure to what it would
  11514. look like when opened with hierarchical calls to `org-cycle'.
  11515. With double optional argument \\[universal-argument] \\[universal-argument], \
  11516. go to the parent and show the
  11517. entire tree."
  11518. (interactive "P")
  11519. (run-hooks 'org-reveal-start-hook)
  11520. (let ((org-show-hierarchy-above t)
  11521. (org-show-following-heading t)
  11522. (org-show-siblings (if siblings t org-show-siblings)))
  11523. (org-show-context nil))
  11524. (when (equal siblings '(16))
  11525. (save-excursion
  11526. (when (org-up-heading-safe)
  11527. (org-show-subtree)
  11528. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11529. (defun org-highlight-new-match (beg end)
  11530. "Highlight from BEG to END and mark the highlight is an occur headline."
  11531. (let ((ov (make-overlay beg end)))
  11532. (overlay-put ov 'face 'secondary-selection)
  11533. (overlay-put ov 'org-type 'org-occur)
  11534. (push ov org-occur-highlights)))
  11535. (defun org-remove-occur-highlights (&optional beg end noremove)
  11536. "Remove the occur highlights from the buffer.
  11537. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11538. from the `before-change-functions' in the current buffer."
  11539. (interactive)
  11540. (unless org-inhibit-highlight-removal
  11541. (mapc 'delete-overlay org-occur-highlights)
  11542. (setq org-occur-highlights nil)
  11543. (setq org-occur-parameters nil)
  11544. (unless noremove
  11545. (remove-hook 'before-change-functions
  11546. 'org-remove-occur-highlights 'local))))
  11547. ;;;; Priorities
  11548. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11549. "Regular expression matching the priority indicator.")
  11550. (defvar org-remove-priority-next-time nil)
  11551. (defun org-priority-up ()
  11552. "Increase the priority of the current item."
  11553. (interactive)
  11554. (org-priority 'up))
  11555. (defun org-priority-down ()
  11556. "Decrease the priority of the current item."
  11557. (interactive)
  11558. (org-priority 'down))
  11559. (defun org-priority (&optional action)
  11560. "Change the priority of an item by ARG.
  11561. ACTION can be `set', `up', `down', or a character."
  11562. (interactive)
  11563. (unless org-enable-priority-commands
  11564. (error "Priority commands are disabled"))
  11565. (setq action (or action 'set))
  11566. (let (current new news have remove)
  11567. (save-excursion
  11568. (org-back-to-heading t)
  11569. (if (looking-at org-priority-regexp)
  11570. (setq current (string-to-char (match-string 2))
  11571. have t))
  11572. (cond
  11573. ((eq action 'remove)
  11574. (setq remove t new ?\ ))
  11575. ((or (eq action 'set)
  11576. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11577. (if (not (eq action 'set))
  11578. (setq new action)
  11579. (message "Priority %c-%c, SPC to remove: "
  11580. org-highest-priority org-lowest-priority)
  11581. (save-match-data
  11582. (setq new (read-char-exclusive))))
  11583. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11584. (= (upcase org-lowest-priority) org-lowest-priority))
  11585. (setq new (upcase new)))
  11586. (cond ((equal new ?\ ) (setq remove t))
  11587. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11588. (error "Priority must be between `%c' and `%c'"
  11589. org-highest-priority org-lowest-priority))))
  11590. ((eq action 'up)
  11591. (setq new (if have
  11592. (1- current) ; normal cycling
  11593. ;; last priority was empty
  11594. (if (eq last-command this-command)
  11595. org-lowest-priority ; wrap around empty to lowest
  11596. ;; default
  11597. (if org-priority-start-cycle-with-default
  11598. org-default-priority
  11599. (1- org-default-priority))))))
  11600. ((eq action 'down)
  11601. (setq new (if have
  11602. (1+ current) ; normal cycling
  11603. ;; last priority was empty
  11604. (if (eq last-command this-command)
  11605. org-highest-priority ; wrap around empty to highest
  11606. ;; default
  11607. (if org-priority-start-cycle-with-default
  11608. org-default-priority
  11609. (1+ org-default-priority))))))
  11610. (t (error "Invalid action")))
  11611. (if (or (< (upcase new) org-highest-priority)
  11612. (> (upcase new) org-lowest-priority))
  11613. (if (and (memq action '(up down))
  11614. (not have) (not (eq last-command this-command)))
  11615. ;; `new' is from default priority
  11616. (error
  11617. "The default can not be set, see `org-default-priority' why")
  11618. ;; normal cycling: `new' is beyond highest/lowest priority
  11619. ;; and is wrapped around to the empty priority
  11620. (setq remove t)))
  11621. (setq news (format "%c" new))
  11622. (if have
  11623. (if remove
  11624. (replace-match "" t t nil 1)
  11625. (replace-match news t t nil 2))
  11626. (if remove
  11627. (error "No priority cookie found in line")
  11628. (let ((case-fold-search nil))
  11629. (looking-at org-todo-line-regexp))
  11630. (if (match-end 2)
  11631. (progn
  11632. (goto-char (match-end 2))
  11633. (insert " [#" news "]"))
  11634. (goto-char (match-beginning 3))
  11635. (insert "[#" news "] "))))
  11636. (org-preserve-lc (org-set-tags nil 'align)))
  11637. (if remove
  11638. (message "Priority removed")
  11639. (message "Priority of current item set to %s" news))))
  11640. (defun org-get-priority (s)
  11641. "Find priority cookie and return priority."
  11642. (if (functionp org-get-priority-function)
  11643. (funcall org-get-priority-function)
  11644. (save-match-data
  11645. (if (not (string-match org-priority-regexp s))
  11646. (* 1000 (- org-lowest-priority org-default-priority))
  11647. (* 1000 (- org-lowest-priority
  11648. (string-to-char (match-string 2 s))))))))
  11649. ;;;; Tags
  11650. (defvar org-agenda-archives-mode)
  11651. (defvar org-map-continue-from nil
  11652. "Position from where mapping should continue.
  11653. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11654. (defvar org-scanner-tags nil
  11655. "The current tag list while the tags scanner is running.")
  11656. (defvar org-trust-scanner-tags nil
  11657. "Should `org-get-tags-at' use the tags for the scanner.
  11658. This is for internal dynamical scoping only.
  11659. When this is non-nil, the function `org-get-tags-at' will return the value
  11660. of `org-scanner-tags' instead of building the list by itself. This
  11661. can lead to large speed-ups when the tags scanner is used in a file with
  11662. many entries, and when the list of tags is retrieved, for example to
  11663. obtain a list of properties. Building the tags list for each entry in such
  11664. a file becomes an N^2 operation - but with this variable set, it scales
  11665. as N.")
  11666. (defun org-scan-tags (action matcher &optional todo-only start-level)
  11667. "Scan headline tags with inheritance and produce output ACTION.
  11668. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11669. or `agenda' to produce an entry list for an agenda view. It can also be
  11670. a Lisp form or a function that should be called at each matched headline, in
  11671. this case the return value is a list of all return values from these calls.
  11672. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11673. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11674. only lines with a TODO keyword are included in the output.
  11675. START-LEVEL can be a string with asterisks, reducing the scope to
  11676. headlines matching this string."
  11677. (require 'org-agenda)
  11678. (let* ((re (concat "^"
  11679. (if start-level
  11680. ;; Get the correct level to match
  11681. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11682. org-outline-regexp)
  11683. " *\\(\\<\\("
  11684. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11685. (org-re
  11686. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11687. (props (list 'face 'default
  11688. 'done-face 'org-agenda-done
  11689. 'undone-face 'default
  11690. 'mouse-face 'highlight
  11691. 'org-not-done-regexp org-not-done-regexp
  11692. 'org-todo-regexp org-todo-regexp
  11693. 'org-complex-heading-regexp org-complex-heading-regexp
  11694. 'help-echo
  11695. (format "mouse-2 or RET jump to org file %s"
  11696. (abbreviate-file-name
  11697. (or (buffer-file-name (buffer-base-buffer))
  11698. (buffer-name (buffer-base-buffer)))))))
  11699. (case-fold-search nil)
  11700. (org-map-continue-from nil)
  11701. lspos tags tags-list
  11702. (tags-alist (list (cons 0 org-file-tags)))
  11703. (llast 0) rtn rtn1 level category i txt
  11704. todo marker entry priority)
  11705. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11706. (setq action (list 'lambda nil action)))
  11707. (save-excursion
  11708. (goto-char (point-min))
  11709. (when (eq action 'sparse-tree)
  11710. (org-overview)
  11711. (org-remove-occur-highlights))
  11712. (while (re-search-forward re nil t)
  11713. (setq org-map-continue-from nil)
  11714. (catch :skip
  11715. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11716. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11717. (goto-char (setq lspos (match-beginning 0)))
  11718. (setq level (org-reduced-level (funcall outline-level))
  11719. category (org-get-category))
  11720. (setq i llast llast level)
  11721. ;; remove tag lists from same and sublevels
  11722. (while (>= i level)
  11723. (when (setq entry (assoc i tags-alist))
  11724. (setq tags-alist (delete entry tags-alist)))
  11725. (setq i (1- i)))
  11726. ;; add the next tags
  11727. (when tags
  11728. (setq tags (org-split-string tags ":")
  11729. tags-alist
  11730. (cons (cons level tags) tags-alist)))
  11731. ;; compile tags for current headline
  11732. (setq tags-list
  11733. (if org-use-tag-inheritance
  11734. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11735. tags)
  11736. org-scanner-tags tags-list)
  11737. (when org-use-tag-inheritance
  11738. (setcdr (car tags-alist)
  11739. (mapcar (lambda (x)
  11740. (setq x (copy-sequence x))
  11741. (org-add-prop-inherited x))
  11742. (cdar tags-alist))))
  11743. (when (and tags org-use-tag-inheritance
  11744. (or (not (eq t org-use-tag-inheritance))
  11745. org-tags-exclude-from-inheritance))
  11746. ;; selective inheritance, remove uninherited ones
  11747. (setcdr (car tags-alist)
  11748. (org-remove-uninherited-tags (cdar tags-alist))))
  11749. (when (and
  11750. ;; eval matcher only when the todo condition is OK
  11751. (and (or (not todo-only) (member todo org-not-done-keywords))
  11752. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11753. (eval matcher)))
  11754. ;; Call the skipper, but return t if it does not skip,
  11755. ;; so that the `and' form continues evaluating
  11756. (progn
  11757. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11758. t)
  11759. ;; Check if timestamps are deselecting this entry
  11760. (or (not todo-only)
  11761. (and (member todo org-not-done-keywords)
  11762. (or (not org-agenda-tags-todo-honor-ignore-options)
  11763. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11764. ;; Extra check for the archive tag
  11765. ;; FIXME: Does the skipper already do this????
  11766. (or
  11767. (not (member org-archive-tag tags-list))
  11768. ;; we have an archive tag, should we use this anyway?
  11769. (or (not org-agenda-skip-archived-trees)
  11770. (and (eq action 'agenda) org-agenda-archives-mode))))
  11771. ;; select this headline
  11772. (cond
  11773. ((eq action 'sparse-tree)
  11774. (and org-highlight-sparse-tree-matches
  11775. (org-get-heading) (match-end 0)
  11776. (org-highlight-new-match
  11777. (match-beginning 1) (match-end 1)))
  11778. (org-show-context 'tags-tree))
  11779. ((eq action 'agenda)
  11780. (setq txt (org-agenda-format-item
  11781. ""
  11782. (concat
  11783. (if (eq org-tags-match-list-sublevels 'indented)
  11784. (make-string (1- level) ?.) "")
  11785. (org-get-heading))
  11786. category
  11787. tags-list)
  11788. priority (org-get-priority txt))
  11789. (goto-char lspos)
  11790. (setq marker (org-agenda-new-marker))
  11791. (org-add-props txt props
  11792. 'org-marker marker 'org-hd-marker marker 'org-category category
  11793. 'todo-state todo
  11794. 'priority priority 'type "tagsmatch")
  11795. (push txt rtn))
  11796. ((functionp action)
  11797. (setq org-map-continue-from nil)
  11798. (save-excursion
  11799. (setq rtn1 (funcall action))
  11800. (push rtn1 rtn)))
  11801. (t (error "Invalid action")))
  11802. ;; if we are to skip sublevels, jump to end of subtree
  11803. (unless org-tags-match-list-sublevels
  11804. (org-end-of-subtree t)
  11805. (backward-char 1))))
  11806. ;; Get the correct position from where to continue
  11807. (if org-map-continue-from
  11808. (goto-char org-map-continue-from)
  11809. (and (= (point) lspos) (end-of-line 1)))))
  11810. (when (and (eq action 'sparse-tree)
  11811. (not org-sparse-tree-open-archived-trees))
  11812. (org-hide-archived-subtrees (point-min) (point-max)))
  11813. (nreverse rtn)))
  11814. (defun org-remove-uninherited-tags (tags)
  11815. "Remove all tags that are not inherited from the list TAGS."
  11816. (cond
  11817. ((eq org-use-tag-inheritance t)
  11818. (if org-tags-exclude-from-inheritance
  11819. (org-delete-all org-tags-exclude-from-inheritance tags)
  11820. tags))
  11821. ((not org-use-tag-inheritance) nil)
  11822. ((stringp org-use-tag-inheritance)
  11823. (delq nil (mapcar
  11824. (lambda (x)
  11825. (if (and (string-match org-use-tag-inheritance x)
  11826. (not (member x org-tags-exclude-from-inheritance)))
  11827. x nil))
  11828. tags)))
  11829. ((listp org-use-tag-inheritance)
  11830. (delq nil (mapcar
  11831. (lambda (x)
  11832. (if (member x org-use-tag-inheritance) x nil))
  11833. tags)))))
  11834. (defvar todo-only) ;; dynamically scoped
  11835. (defun org-match-sparse-tree (&optional todo-only match)
  11836. "Create a sparse tree according to tags string MATCH.
  11837. MATCH can contain positive and negative selection of tags, like
  11838. \"+WORK+URGENT-WITHBOSS\".
  11839. If optional argument TODO-ONLY is non-nil, only select lines that are
  11840. also TODO lines."
  11841. (interactive "P")
  11842. (org-prepare-agenda-buffers (list (current-buffer)))
  11843. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11844. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11845. (defvar org-cached-props nil)
  11846. (defun org-cached-entry-get (pom property)
  11847. (if (or (eq t org-use-property-inheritance)
  11848. (and (stringp org-use-property-inheritance)
  11849. (string-match org-use-property-inheritance property))
  11850. (and (listp org-use-property-inheritance)
  11851. (member property org-use-property-inheritance)))
  11852. ;; Caching is not possible, check it directly
  11853. (org-entry-get pom property 'inherit)
  11854. ;; Get all properties, so that we can do complicated checks easily
  11855. (cdr (assoc property (or org-cached-props
  11856. (setq org-cached-props
  11857. (org-entry-properties pom)))))))
  11858. (defun org-global-tags-completion-table (&optional files)
  11859. "Return the list of all tags in all agenda buffer/files.
  11860. Optional FILES argument is a list of files to which can be used
  11861. instead of the agenda files."
  11862. (save-excursion
  11863. (org-uniquify
  11864. (delq nil
  11865. (apply 'append
  11866. (mapcar
  11867. (lambda (file)
  11868. (set-buffer (find-file-noselect file))
  11869. (append (org-get-buffer-tags)
  11870. (mapcar (lambda (x) (if (stringp (car-safe x))
  11871. (list (car-safe x)) nil))
  11872. org-tag-alist)))
  11873. (if (and files (car files))
  11874. files
  11875. (org-agenda-files))))))))
  11876. (defun org-make-tags-matcher (match)
  11877. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11878. ;; todo-only is scoped dynamically into this function, and the function
  11879. ;; may change it if the matcher asks for it.
  11880. (unless match
  11881. ;; Get a new match request, with completion
  11882. (let ((org-last-tags-completion-table
  11883. (org-global-tags-completion-table)))
  11884. (setq match (org-completing-read-no-i
  11885. "Match: " 'org-tags-completion-function nil nil nil
  11886. 'org-tags-history))))
  11887. ;; Parse the string and create a lisp form
  11888. (let ((match0 match)
  11889. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11890. minus tag mm
  11891. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11892. orterms term orlist re-p str-p level-p level-op time-p
  11893. prop-p pn pv po gv rest)
  11894. (if (string-match "/+" match)
  11895. ;; match contains also a todo-matching request
  11896. (progn
  11897. (setq tagsmatch (substring match 0 (match-beginning 0))
  11898. todomatch (substring match (match-end 0)))
  11899. (if (string-match "^!" todomatch)
  11900. (setq todo-only t todomatch (substring todomatch 1)))
  11901. (if (string-match "^\\s-*$" todomatch)
  11902. (setq todomatch nil)))
  11903. ;; only matching tags
  11904. (setq tagsmatch match todomatch nil))
  11905. ;; Make the tags matcher
  11906. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11907. (setq tagsmatcher t)
  11908. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11909. (while (setq term (pop orterms))
  11910. (while (and (equal (substring term -1) "\\") orterms)
  11911. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11912. (while (string-match re term)
  11913. (setq rest (substring term (match-end 0))
  11914. minus (and (match-end 1)
  11915. (equal (match-string 1 term) "-"))
  11916. tag (save-match-data (replace-regexp-in-string
  11917. "\\\\-" "-"
  11918. (match-string 2 term)))
  11919. re-p (equal (string-to-char tag) ?{)
  11920. level-p (match-end 4)
  11921. prop-p (match-end 5)
  11922. mm (cond
  11923. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11924. (level-p
  11925. (setq level-op (org-op-to-function (match-string 3 term)))
  11926. `(,level-op level ,(string-to-number
  11927. (match-string 4 term))))
  11928. (prop-p
  11929. (setq pn (match-string 5 term)
  11930. po (match-string 6 term)
  11931. pv (match-string 7 term)
  11932. re-p (equal (string-to-char pv) ?{)
  11933. str-p (equal (string-to-char pv) ?\")
  11934. time-p (save-match-data
  11935. (string-match "^\"[[<].*[]>]\"$" pv))
  11936. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11937. (if time-p (setq pv (org-matcher-time pv)))
  11938. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11939. (cond
  11940. ((equal pn "CATEGORY")
  11941. (setq gv '(get-text-property (point) 'org-category)))
  11942. ((equal pn "TODO")
  11943. (setq gv 'todo))
  11944. (t
  11945. (setq gv `(org-cached-entry-get nil ,pn))))
  11946. (if re-p
  11947. (if (eq po 'org<>)
  11948. `(not (string-match ,pv (or ,gv "")))
  11949. `(string-match ,pv (or ,gv "")))
  11950. (if str-p
  11951. `(,po (or ,gv "") ,pv)
  11952. `(,po (string-to-number (or ,gv ""))
  11953. ,(string-to-number pv) ))))
  11954. (t `(member ,tag tags-list)))
  11955. mm (if minus (list 'not mm) mm)
  11956. term rest)
  11957. (push mm tagsmatcher))
  11958. (push (if (> (length tagsmatcher) 1)
  11959. (cons 'and tagsmatcher)
  11960. (car tagsmatcher))
  11961. orlist)
  11962. (setq tagsmatcher nil))
  11963. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11964. (setq tagsmatcher
  11965. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11966. ;; Make the todo matcher
  11967. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11968. (setq todomatcher t)
  11969. (setq orterms (org-split-string todomatch "|") orlist nil)
  11970. (while (setq term (pop orterms))
  11971. (while (string-match re term)
  11972. (setq minus (and (match-end 1)
  11973. (equal (match-string 1 term) "-"))
  11974. kwd (match-string 2 term)
  11975. re-p (equal (string-to-char kwd) ?{)
  11976. term (substring term (match-end 0))
  11977. mm (if re-p
  11978. `(string-match ,(substring kwd 1 -1) todo)
  11979. (list 'equal 'todo kwd))
  11980. mm (if minus (list 'not mm) mm))
  11981. (push mm todomatcher))
  11982. (push (if (> (length todomatcher) 1)
  11983. (cons 'and todomatcher)
  11984. (car todomatcher))
  11985. orlist)
  11986. (setq todomatcher nil))
  11987. (setq todomatcher (if (> (length orlist) 1)
  11988. (cons 'or orlist) (car orlist))))
  11989. ;; Return the string and lisp forms of the matcher
  11990. (setq matcher (if todomatcher
  11991. (list 'and tagsmatcher todomatcher)
  11992. tagsmatcher))
  11993. (cons match0 matcher)))
  11994. (defun org-op-to-function (op &optional stringp)
  11995. "Turn an operator into the appropriate function."
  11996. (setq op
  11997. (cond
  11998. ((equal op "<" ) '(< string< org-time<))
  11999. ((equal op ">" ) '(> org-string> org-time>))
  12000. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12001. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12002. ((member op '("=" "==")) '(= string= org-time=))
  12003. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12004. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12005. (defun org<> (a b) (not (= a b)))
  12006. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12007. (defun org-string>= (a b) (not (string< a b)))
  12008. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12009. (defun org-string<> (a b) (not (string= a b)))
  12010. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12011. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12012. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12013. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12014. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12015. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12016. (defun org-2ft (s)
  12017. "Convert S to a floating point time.
  12018. If S is already a number, just return it. If it is a string, parse
  12019. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12020. (cond
  12021. ((numberp s) s)
  12022. ((stringp s)
  12023. (condition-case nil
  12024. (float-time (apply 'encode-time (org-parse-time-string s)))
  12025. (error 0.)))
  12026. (t 0.)))
  12027. (defun org-time-today ()
  12028. "Time in seconds today at 0:00.
  12029. Returns the float number of seconds since the beginning of the
  12030. epoch to the beginning of today (00:00)."
  12031. (float-time (apply 'encode-time
  12032. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12033. (defun org-matcher-time (s)
  12034. "Interpret a time comparison value."
  12035. (save-match-data
  12036. (cond
  12037. ((string= s "<now>") (float-time))
  12038. ((string= s "<today>") (org-time-today))
  12039. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12040. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12041. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  12042. (+ (org-time-today)
  12043. (* (string-to-number (match-string 1 s))
  12044. (cdr (assoc (match-string 2 s)
  12045. '(("d" . 86400.0) ("w" . 604800.0)
  12046. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12047. (t (org-2ft s)))))
  12048. (defun org-match-any-p (re list)
  12049. "Does re match any element of list?"
  12050. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12051. (delq nil list))
  12052. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12053. (defvar org-tags-overlay (make-overlay 1 1))
  12054. (org-detach-overlay org-tags-overlay)
  12055. (defun org-get-local-tags-at (&optional pos)
  12056. "Get a list of tags defined in the current headline."
  12057. (org-get-tags-at pos 'local))
  12058. (defun org-get-local-tags ()
  12059. "Get a list of tags defined in the current headline."
  12060. (org-get-tags-at nil 'local))
  12061. (defun org-get-tags-at (&optional pos local)
  12062. "Get a list of all headline tags applicable at POS.
  12063. POS defaults to point. If tags are inherited, the list contains
  12064. the targets in the same sequence as the headlines appear, i.e.
  12065. the tags of the current headline come last.
  12066. When LOCAL is non-nil, only return tags from the current headline,
  12067. ignore inherited ones."
  12068. (interactive)
  12069. (if (and org-trust-scanner-tags
  12070. (or (not pos) (equal pos (point)))
  12071. (not local))
  12072. org-scanner-tags
  12073. (let (tags ltags lastpos parent)
  12074. (save-excursion
  12075. (save-restriction
  12076. (widen)
  12077. (goto-char (or pos (point)))
  12078. (save-match-data
  12079. (catch 'done
  12080. (condition-case nil
  12081. (progn
  12082. (org-back-to-heading t)
  12083. (while (not (equal lastpos (point)))
  12084. (setq lastpos (point))
  12085. (when (looking-at
  12086. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12087. (setq ltags (org-split-string
  12088. (org-match-string-no-properties 1) ":"))
  12089. (when parent
  12090. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12091. (setq tags (append
  12092. (if parent
  12093. (org-remove-uninherited-tags ltags)
  12094. ltags)
  12095. tags)))
  12096. (or org-use-tag-inheritance (throw 'done t))
  12097. (if local (throw 'done t))
  12098. (or (org-up-heading-safe) (error nil))
  12099. (setq parent t)))
  12100. (error nil)))))
  12101. (if local
  12102. tags
  12103. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12104. (defun org-add-prop-inherited (s)
  12105. (add-text-properties 0 (length s) '(inherited t) s)
  12106. s)
  12107. (defun org-toggle-tag (tag &optional onoff)
  12108. "Toggle the tag TAG for the current line.
  12109. If ONOFF is `on' or `off', don't toggle but set to this state."
  12110. (let (res current)
  12111. (save-excursion
  12112. (org-back-to-heading t)
  12113. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12114. (point-at-eol) t)
  12115. (progn
  12116. (setq current (match-string 1))
  12117. (replace-match ""))
  12118. (setq current ""))
  12119. (setq current (nreverse (org-split-string current ":")))
  12120. (cond
  12121. ((eq onoff 'on)
  12122. (setq res t)
  12123. (or (member tag current) (push tag current)))
  12124. ((eq onoff 'off)
  12125. (or (not (member tag current)) (setq current (delete tag current))))
  12126. (t (if (member tag current)
  12127. (setq current (delete tag current))
  12128. (setq res t)
  12129. (push tag current))))
  12130. (end-of-line 1)
  12131. (if current
  12132. (progn
  12133. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12134. (org-set-tags nil t))
  12135. (delete-horizontal-space))
  12136. (run-hooks 'org-after-tags-change-hook))
  12137. res))
  12138. (defun org-align-tags-here (to-col)
  12139. ;; Assumes that this is a headline
  12140. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12141. (beginning-of-line 1)
  12142. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12143. (< pos (match-beginning 2)))
  12144. (progn
  12145. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12146. (goto-char (match-beginning 1))
  12147. (insert " ")
  12148. (delete-region (point) (1+ (match-beginning 2)))
  12149. (setq ncol (max (current-column)
  12150. (1+ col)
  12151. (if (> to-col 0)
  12152. to-col
  12153. (- (abs to-col) tags-l))))
  12154. (setq p (point))
  12155. (insert (make-string (- ncol (current-column)) ?\ ))
  12156. (setq ncol (current-column))
  12157. (when indent-tabs-mode (tabify p (point-at-eol)))
  12158. (org-move-to-column (min ncol col) t))
  12159. (goto-char pos))))
  12160. (defun org-set-tags-command (&optional arg just-align)
  12161. "Call the set-tags command for the current entry."
  12162. (interactive "P")
  12163. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12164. (org-set-tags arg just-align)
  12165. (save-excursion
  12166. (org-back-to-heading t)
  12167. (org-set-tags arg just-align))))
  12168. (defun org-set-tags-to (data)
  12169. "Set the tags of the current entry to DATA, replacing the current tags.
  12170. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12171. If DATA is nil or the empty string, any tags will be removed."
  12172. (interactive "sTags: ")
  12173. (setq data
  12174. (cond
  12175. ((eq data nil) "")
  12176. ((equal data "") "")
  12177. ((stringp data)
  12178. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12179. ":"))
  12180. ((listp data)
  12181. (concat ":" (mapconcat 'identity data ":") ":"))
  12182. (t nil)))
  12183. (when data
  12184. (save-excursion
  12185. (org-back-to-heading t)
  12186. (when (looking-at org-complex-heading-regexp)
  12187. (if (match-end 5)
  12188. (progn
  12189. (goto-char (match-beginning 5))
  12190. (insert data)
  12191. (delete-region (point) (point-at-eol))
  12192. (org-set-tags nil 'align))
  12193. (goto-char (point-at-eol))
  12194. (insert " " data)
  12195. (org-set-tags nil 'align)))
  12196. (beginning-of-line 1)
  12197. (if (looking-at ".*?\\([ \t]+\\)$")
  12198. (delete-region (match-beginning 1) (match-end 1))))))
  12199. (defun org-align-all-tags ()
  12200. "Align the tags i all headings."
  12201. (interactive)
  12202. (save-excursion
  12203. (or (ignore-errors (org-back-to-heading t))
  12204. (outline-next-heading))
  12205. (if (org-at-heading-p)
  12206. (org-set-tags t)
  12207. (message "No headings"))))
  12208. (defvar org-indent-indentation-per-level)
  12209. (defun org-set-tags (&optional arg just-align)
  12210. "Set the tags for the current headline.
  12211. With prefix ARG, realign all tags in headings in the current buffer."
  12212. (interactive "P")
  12213. (let* ((re org-outline-regexp-bol)
  12214. (current (unless arg (org-get-tags-string)))
  12215. (col (current-column))
  12216. (org-setting-tags t)
  12217. table current-tags inherited-tags ; computed below when needed
  12218. tags p0 c0 c1 rpl di tc level)
  12219. (if arg
  12220. (save-excursion
  12221. (goto-char (point-min))
  12222. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12223. (while (re-search-forward re nil t)
  12224. (org-set-tags nil t)
  12225. (end-of-line 1)))
  12226. (message "All tags realigned to column %d" org-tags-column))
  12227. (if just-align
  12228. (setq tags current)
  12229. ;; Get a new set of tags from the user
  12230. (save-excursion
  12231. (setq table (append org-tag-persistent-alist
  12232. (or org-tag-alist (org-get-buffer-tags))
  12233. (and
  12234. org-complete-tags-always-offer-all-agenda-tags
  12235. (org-global-tags-completion-table
  12236. (org-agenda-files))))
  12237. org-last-tags-completion-table table
  12238. current-tags (org-split-string current ":")
  12239. inherited-tags (nreverse
  12240. (nthcdr (length current-tags)
  12241. (nreverse (org-get-tags-at))))
  12242. tags
  12243. (if (or (eq t org-use-fast-tag-selection)
  12244. (and org-use-fast-tag-selection
  12245. (delq nil (mapcar 'cdr table))))
  12246. (org-fast-tag-selection
  12247. current-tags inherited-tags table
  12248. (if org-fast-tag-selection-include-todo
  12249. org-todo-key-alist))
  12250. (let ((org-add-colon-after-tag-completion t))
  12251. (org-trim
  12252. (org-icompleting-read "Tags: "
  12253. 'org-tags-completion-function
  12254. nil nil current 'org-tags-history))))))
  12255. (while (string-match "[-+&]+" tags)
  12256. ;; No boolean logic, just a list
  12257. (setq tags (replace-match ":" t t tags))))
  12258. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12259. (if org-tags-sort-function
  12260. (setq tags (mapconcat 'identity
  12261. (sort (org-split-string
  12262. tags (org-re "[^[:alnum:]_@#%]+"))
  12263. org-tags-sort-function) ":")))
  12264. (if (string-match "\\`[\t ]*\\'" tags)
  12265. (setq tags "")
  12266. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12267. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12268. ;; Insert new tags at the correct column
  12269. (beginning-of-line 1)
  12270. (setq level (or (and (looking-at org-outline-regexp)
  12271. (- (match-end 0) (point) 1))
  12272. 1))
  12273. (cond
  12274. ((and (equal current "") (equal tags "")))
  12275. ((re-search-forward
  12276. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12277. (point-at-eol) t)
  12278. (if (equal tags "")
  12279. (setq rpl "")
  12280. (goto-char (match-beginning 0))
  12281. (setq c0 (current-column)
  12282. ;; compute offset for the case of org-indent-mode active
  12283. di (if org-indent-mode
  12284. (* (1- org-indent-indentation-per-level) (1- level))
  12285. 0)
  12286. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12287. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12288. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12289. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12290. (replace-match rpl t t)
  12291. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12292. tags)
  12293. (t (error "Tags alignment failed")))
  12294. (org-move-to-column col)
  12295. (unless just-align
  12296. (run-hooks 'org-after-tags-change-hook)))))
  12297. (defun org-change-tag-in-region (beg end tag off)
  12298. "Add or remove TAG for each entry in the region.
  12299. This works in the agenda, and also in an org-mode buffer."
  12300. (interactive
  12301. (list (region-beginning) (region-end)
  12302. (let ((org-last-tags-completion-table
  12303. (if (eq major-mode 'org-mode)
  12304. (org-get-buffer-tags)
  12305. (org-global-tags-completion-table))))
  12306. (org-icompleting-read
  12307. "Tag: " 'org-tags-completion-function nil nil nil
  12308. 'org-tags-history))
  12309. (progn
  12310. (message "[s]et or [r]emove? ")
  12311. (equal (read-char-exclusive) ?r))))
  12312. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12313. (let ((agendap (equal major-mode 'org-agenda-mode))
  12314. l1 l2 m buf pos newhead (cnt 0))
  12315. (goto-char end)
  12316. (setq l2 (1- (org-current-line)))
  12317. (goto-char beg)
  12318. (setq l1 (org-current-line))
  12319. (loop for l from l1 to l2 do
  12320. (org-goto-line l)
  12321. (setq m (get-text-property (point) 'org-hd-marker))
  12322. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12323. (and agendap m))
  12324. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12325. pos (if agendap m (point)))
  12326. (with-current-buffer buf
  12327. (save-excursion
  12328. (save-restriction
  12329. (goto-char pos)
  12330. (setq cnt (1+ cnt))
  12331. (org-toggle-tag tag (if off 'off 'on))
  12332. (setq newhead (org-get-heading)))))
  12333. (and agendap (org-agenda-change-all-lines newhead m))))
  12334. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12335. (defun org-tags-completion-function (string predicate &optional flag)
  12336. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12337. (confirm (lambda (x) (stringp (car x)))))
  12338. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12339. (setq s1 (match-string 1 string)
  12340. s2 (match-string 2 string))
  12341. (setq s1 "" s2 string))
  12342. (cond
  12343. ((eq flag nil)
  12344. ;; try completion
  12345. (setq rtn (try-completion s2 ctable confirm))
  12346. (if (stringp rtn)
  12347. (setq rtn
  12348. (concat s1 s2 (substring rtn (length s2))
  12349. (if (and org-add-colon-after-tag-completion
  12350. (assoc rtn ctable))
  12351. ":" ""))))
  12352. rtn)
  12353. ((eq flag t)
  12354. ;; all-completions
  12355. (all-completions s2 ctable confirm)
  12356. )
  12357. ((eq flag 'lambda)
  12358. ;; exact match?
  12359. (assoc s2 ctable)))
  12360. ))
  12361. (defun org-fast-tag-insert (kwd tags face &optional end)
  12362. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12363. (insert (format "%-12s" (concat kwd ":"))
  12364. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12365. (or end "")))
  12366. (defun org-fast-tag-show-exit (flag)
  12367. (save-excursion
  12368. (org-goto-line 3)
  12369. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12370. (replace-match ""))
  12371. (when flag
  12372. (end-of-line 1)
  12373. (org-move-to-column (- (window-width) 19) t)
  12374. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12375. (defun org-set-current-tags-overlay (current prefix)
  12376. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12377. (if (featurep 'xemacs)
  12378. (org-overlay-display org-tags-overlay (concat prefix s)
  12379. 'secondary-selection)
  12380. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12381. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12382. (defvar org-last-tag-selection-key nil)
  12383. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12384. "Fast tag selection with single keys.
  12385. CURRENT is the current list of tags in the headline, INHERITED is the
  12386. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12387. possibly with grouping information. TODO-TABLE is a similar table with
  12388. TODO keywords, should these have keys assigned to them.
  12389. If the keys are nil, a-z are automatically assigned.
  12390. Returns the new tags string, or nil to not change the current settings."
  12391. (let* ((fulltable (append table todo-table))
  12392. (maxlen (apply 'max (mapcar
  12393. (lambda (x)
  12394. (if (stringp (car x)) (string-width (car x)) 0))
  12395. fulltable)))
  12396. (buf (current-buffer))
  12397. (expert (eq org-fast-tag-selection-single-key 'expert))
  12398. (buffer-tags nil)
  12399. (fwidth (+ maxlen 3 1 3))
  12400. (ncol (/ (- (window-width) 4) fwidth))
  12401. (i-face 'org-done)
  12402. (c-face 'org-todo)
  12403. tg cnt e c char c1 c2 ntable tbl rtn
  12404. ov-start ov-end ov-prefix
  12405. (exit-after-next org-fast-tag-selection-single-key)
  12406. (done-keywords org-done-keywords)
  12407. groups ingroup)
  12408. (save-excursion
  12409. (beginning-of-line 1)
  12410. (if (looking-at
  12411. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12412. (setq ov-start (match-beginning 1)
  12413. ov-end (match-end 1)
  12414. ov-prefix "")
  12415. (setq ov-start (1- (point-at-eol))
  12416. ov-end (1+ ov-start))
  12417. (skip-chars-forward "^\n\r")
  12418. (setq ov-prefix
  12419. (concat
  12420. (buffer-substring (1- (point)) (point))
  12421. (if (> (current-column) org-tags-column)
  12422. " "
  12423. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12424. (move-overlay org-tags-overlay ov-start ov-end)
  12425. (save-window-excursion
  12426. (if expert
  12427. (set-buffer (get-buffer-create " *Org tags*"))
  12428. (delete-other-windows)
  12429. (split-window-vertically)
  12430. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12431. (erase-buffer)
  12432. (org-set-local 'org-done-keywords done-keywords)
  12433. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12434. (org-fast-tag-insert "Current" current c-face "\n\n")
  12435. (org-fast-tag-show-exit exit-after-next)
  12436. (org-set-current-tags-overlay current ov-prefix)
  12437. (setq tbl fulltable char ?a cnt 0)
  12438. (while (setq e (pop tbl))
  12439. (cond
  12440. ((equal (car e) :startgroup)
  12441. (push '() groups) (setq ingroup t)
  12442. (when (not (= cnt 0))
  12443. (setq cnt 0)
  12444. (insert "\n"))
  12445. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12446. ((equal (car e) :endgroup)
  12447. (setq ingroup nil cnt 0)
  12448. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12449. ((equal e '(:newline))
  12450. (when (not (= cnt 0))
  12451. (setq cnt 0)
  12452. (insert "\n")
  12453. (setq e (car tbl))
  12454. (while (equal (car tbl) '(:newline))
  12455. (insert "\n")
  12456. (setq tbl (cdr tbl)))))
  12457. (t
  12458. (setq tg (copy-sequence (car e)) c2 nil)
  12459. (if (cdr e)
  12460. (setq c (cdr e))
  12461. ;; automatically assign a character.
  12462. (setq c1 (string-to-char
  12463. (downcase (substring
  12464. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12465. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12466. (while (or (rassoc char ntable) (rassoc char table))
  12467. (setq char (1+ char)))
  12468. (setq c2 c1))
  12469. (setq c (or c2 char)))
  12470. (if ingroup (push tg (car groups)))
  12471. (setq tg (org-add-props tg nil 'face
  12472. (cond
  12473. ((not (assoc tg table))
  12474. (org-get-todo-face tg))
  12475. ((member tg current) c-face)
  12476. ((member tg inherited) i-face)
  12477. (t nil))))
  12478. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12479. (insert "[" c "] " tg (make-string
  12480. (- fwidth 4 (length tg)) ?\ ))
  12481. (push (cons tg c) ntable)
  12482. (when (= (setq cnt (1+ cnt)) ncol)
  12483. (insert "\n")
  12484. (if ingroup (insert " "))
  12485. (setq cnt 0)))))
  12486. (setq ntable (nreverse ntable))
  12487. (insert "\n")
  12488. (goto-char (point-min))
  12489. (if (not expert) (org-fit-window-to-buffer))
  12490. (setq rtn
  12491. (catch 'exit
  12492. (while t
  12493. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12494. (if (not groups) "no " "")
  12495. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12496. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12497. (setq org-last-tag-selection-key c)
  12498. (cond
  12499. ((= c ?\r) (throw 'exit t))
  12500. ((= c ?!)
  12501. (setq groups (not groups))
  12502. (goto-char (point-min))
  12503. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12504. ((= c ?\C-c)
  12505. (if (not expert)
  12506. (org-fast-tag-show-exit
  12507. (setq exit-after-next (not exit-after-next)))
  12508. (setq expert nil)
  12509. (delete-other-windows)
  12510. (set-window-buffer (split-window-vertically) " *Org tags*")
  12511. (org-switch-to-buffer-other-window " *Org tags*")
  12512. (org-fit-window-to-buffer)))
  12513. ((or (= c ?\C-g)
  12514. (and (= c ?q) (not (rassoc c ntable))))
  12515. (org-detach-overlay org-tags-overlay)
  12516. (setq quit-flag t))
  12517. ((= c ?\ )
  12518. (setq current nil)
  12519. (if exit-after-next (setq exit-after-next 'now)))
  12520. ((= c ?\t)
  12521. (condition-case nil
  12522. (setq tg (org-icompleting-read
  12523. "Tag: "
  12524. (or buffer-tags
  12525. (with-current-buffer buf
  12526. (org-get-buffer-tags)))))
  12527. (quit (setq tg "")))
  12528. (when (string-match "\\S-" tg)
  12529. (add-to-list 'buffer-tags (list tg))
  12530. (if (member tg current)
  12531. (setq current (delete tg current))
  12532. (push tg current)))
  12533. (if exit-after-next (setq exit-after-next 'now)))
  12534. ((setq e (rassoc c todo-table) tg (car e))
  12535. (with-current-buffer buf
  12536. (save-excursion (org-todo tg)))
  12537. (if exit-after-next (setq exit-after-next 'now)))
  12538. ((setq e (rassoc c ntable) tg (car e))
  12539. (if (member tg current)
  12540. (setq current (delete tg current))
  12541. (loop for g in groups do
  12542. (if (member tg g)
  12543. (mapc (lambda (x)
  12544. (setq current (delete x current)))
  12545. g)))
  12546. (push tg current))
  12547. (if exit-after-next (setq exit-after-next 'now))))
  12548. ;; Create a sorted list
  12549. (setq current
  12550. (sort current
  12551. (lambda (a b)
  12552. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12553. (if (eq exit-after-next 'now) (throw 'exit t))
  12554. (goto-char (point-min))
  12555. (beginning-of-line 2)
  12556. (delete-region (point) (point-at-eol))
  12557. (org-fast-tag-insert "Current" current c-face)
  12558. (org-set-current-tags-overlay current ov-prefix)
  12559. (while (re-search-forward
  12560. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12561. (setq tg (match-string 1))
  12562. (add-text-properties
  12563. (match-beginning 1) (match-end 1)
  12564. (list 'face
  12565. (cond
  12566. ((member tg current) c-face)
  12567. ((member tg inherited) i-face)
  12568. (t (get-text-property (match-beginning 1) 'face))))))
  12569. (goto-char (point-min)))))
  12570. (org-detach-overlay org-tags-overlay)
  12571. (if rtn
  12572. (mapconcat 'identity current ":")
  12573. nil))))
  12574. (defun org-get-tags-string ()
  12575. "Get the TAGS string in the current headline."
  12576. (unless (org-at-heading-p t)
  12577. (error "Not on a heading"))
  12578. (save-excursion
  12579. (beginning-of-line 1)
  12580. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12581. (org-match-string-no-properties 1)
  12582. "")))
  12583. (defun org-get-tags ()
  12584. "Get the list of tags specified in the current headline."
  12585. (org-split-string (org-get-tags-string) ":"))
  12586. (defun org-get-buffer-tags ()
  12587. "Get a table of all tags used in the buffer, for completion."
  12588. (let (tags)
  12589. (save-excursion
  12590. (goto-char (point-min))
  12591. (while (re-search-forward
  12592. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12593. (when (equal (char-after (point-at-bol 0)) ?*)
  12594. (mapc (lambda (x) (add-to-list 'tags x))
  12595. (org-split-string (org-match-string-no-properties 1) ":")))))
  12596. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12597. (mapcar 'list tags)))
  12598. ;;;; The mapping API
  12599. ;;;###autoload
  12600. (defun org-map-entries (func &optional match scope &rest skip)
  12601. "Call FUNC at each headline selected by MATCH in SCOPE.
  12602. FUNC is a function or a lisp form. The function will be called without
  12603. arguments, with the cursor positioned at the beginning of the headline.
  12604. The return values of all calls to the function will be collected and
  12605. returned as a list.
  12606. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12607. does not need to preserve point. After evaluation, the cursor will be
  12608. moved to the end of the line (presumably of the headline of the
  12609. processed entry) and search continues from there. Under some
  12610. circumstances, this may not produce the wanted results. For example,
  12611. if you have removed (e.g. archived) the current (sub)tree it could
  12612. mean that the next entry will be skipped entirely. In such cases, you
  12613. can specify the position from where search should continue by making
  12614. FUNC set the variable `org-map-continue-from' to the desired buffer
  12615. position.
  12616. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12617. Only headlines that are matched by this query will be considered during
  12618. the iteration. When MATCH is nil or t, all headlines will be
  12619. visited by the iteration.
  12620. SCOPE determines the scope of this command. It can be any of:
  12621. nil The current buffer, respecting the restriction if any
  12622. tree The subtree started with the entry at point
  12623. region The entries within the active region, if any
  12624. region-start-level
  12625. The entries within the active region, but only those at
  12626. the same level than the first one.
  12627. file The current buffer, without restriction
  12628. file-with-archives
  12629. The current buffer, and any archives associated with it
  12630. agenda All agenda files
  12631. agenda-with-archives
  12632. All agenda files with any archive files associated with them
  12633. \(file1 file2 ...)
  12634. If this is a list, all files in the list will be scanned
  12635. The remaining args are treated as settings for the skipping facilities of
  12636. the scanner. The following items can be given here:
  12637. archive skip trees with the archive tag.
  12638. comment skip trees with the COMMENT keyword
  12639. function or Emacs Lisp form:
  12640. will be used as value for `org-agenda-skip-function', so whenever
  12641. the function returns t, FUNC will not be called for that
  12642. entry and search will continue from the point where the
  12643. function leaves it.
  12644. If your function needs to retrieve the tags including inherited tags
  12645. at the *current* entry, you can use the value of the variable
  12646. `org-scanner-tags' which will be much faster than getting the value
  12647. with `org-get-tags-at'. If your function gets properties with
  12648. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12649. to t around the call to `org-entry-properties' to get the same speedup.
  12650. Note that if your function moves around to retrieve tags and properties at
  12651. a *different* entry, you cannot use these techniques."
  12652. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12653. (not (org-region-active-p)))
  12654. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12655. (org-agenda-skip-archived-trees (memq 'archive skip))
  12656. (org-agenda-skip-comment-trees (memq 'comment skip))
  12657. (org-agenda-skip-function
  12658. (car (org-delete-all '(comment archive) skip)))
  12659. (org-tags-match-list-sublevels t)
  12660. (start-level (eq scope 'region-start-level))
  12661. matcher file res
  12662. org-todo-keywords-for-agenda
  12663. org-done-keywords-for-agenda
  12664. org-todo-keyword-alist-for-agenda
  12665. org-drawers-for-agenda
  12666. org-tag-alist-for-agenda)
  12667. (cond
  12668. ((eq match t) (setq matcher t))
  12669. ((eq match nil) (setq matcher t))
  12670. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12671. (save-excursion
  12672. (save-restriction
  12673. (cond ((eq scope 'tree)
  12674. (org-back-to-heading t)
  12675. (org-narrow-to-subtree)
  12676. (setq scope nil))
  12677. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12678. (org-region-active-p))
  12679. ;; If needed, set start-level to a string like "2"
  12680. (when start-level
  12681. (save-excursion
  12682. (goto-char (region-beginning))
  12683. (unless (org-at-heading-p) (outline-next-heading))
  12684. (setq start-level (org-current-level))))
  12685. (narrow-to-region (region-beginning)
  12686. (save-excursion
  12687. (goto-char (region-end))
  12688. (unless (and (bolp) (org-at-heading-p))
  12689. (outline-next-heading))
  12690. (point)))
  12691. (setq scope nil)))
  12692. (if (not scope)
  12693. (progn
  12694. (org-prepare-agenda-buffers
  12695. (list (buffer-file-name (current-buffer))))
  12696. (setq res (org-scan-tags func matcher nil start-level)))
  12697. ;; Get the right scope
  12698. (cond
  12699. ((and scope (listp scope) (symbolp (car scope)))
  12700. (setq scope (eval scope)))
  12701. ((eq scope 'agenda)
  12702. (setq scope (org-agenda-files t)))
  12703. ((eq scope 'agenda-with-archives)
  12704. (setq scope (org-agenda-files t))
  12705. (setq scope (org-add-archive-files scope)))
  12706. ((eq scope 'file)
  12707. (setq scope (list (buffer-file-name))))
  12708. ((eq scope 'file-with-archives)
  12709. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12710. (org-prepare-agenda-buffers scope)
  12711. (while (setq file (pop scope))
  12712. (with-current-buffer (org-find-base-buffer-visiting file)
  12713. (save-excursion
  12714. (save-restriction
  12715. (widen)
  12716. (goto-char (point-min))
  12717. (setq res (append res (org-scan-tags func matcher))))))))))
  12718. res)))
  12719. ;;;; Properties
  12720. ;;; Setting and retrieving properties
  12721. (defconst org-special-properties
  12722. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12723. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12724. "The special properties valid in Org-mode.
  12725. These are properties that are not defined in the property drawer,
  12726. but in some other way.")
  12727. (defconst org-default-properties
  12728. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12729. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12730. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12731. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12732. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12733. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12734. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12735. "Some properties that are used by Org-mode for various purposes.
  12736. Being in this list makes sure that they are offered for completion.")
  12737. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12738. "Regular expression matching the first line of a property drawer.")
  12739. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12740. "Regular expression matching the last line of a property drawer.")
  12741. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12742. "Regular expression matching the first line of a property drawer.")
  12743. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12744. "Regular expression matching the first line of a property drawer.")
  12745. (defconst org-property-drawer-re
  12746. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12747. org-property-end-re "\\)\n?")
  12748. "Matches an entire property drawer.")
  12749. (defconst org-clock-drawer-re
  12750. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12751. org-property-end-re "\\)\n?")
  12752. "Matches an entire clock drawer.")
  12753. (defsubst org-re-property (property)
  12754. "Return a regexp matching PROPERTY.
  12755. Match group 1 will be set to the value "
  12756. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12757. (defun org-property-action ()
  12758. "Do an action on properties."
  12759. (interactive)
  12760. (let (c)
  12761. (org-at-property-p)
  12762. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12763. (setq c (read-char-exclusive))
  12764. (cond
  12765. ((equal c ?s)
  12766. (call-interactively 'org-set-property))
  12767. ((equal c ?d)
  12768. (call-interactively 'org-delete-property))
  12769. ((equal c ?D)
  12770. (call-interactively 'org-delete-property-globally))
  12771. ((equal c ?c)
  12772. (call-interactively 'org-compute-property-at-point))
  12773. (t (error "No such property action %c" c)))))
  12774. (defun org-set-effort (&optional value)
  12775. "Set the effort property of the current entry.
  12776. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12777. allowed value."
  12778. (interactive "P")
  12779. (if (equal value 0) (setq value 10))
  12780. (let* ((completion-ignore-case t)
  12781. (prop org-effort-property)
  12782. (cur (org-entry-get nil prop))
  12783. (allowed (org-property-get-allowed-values nil prop 'table))
  12784. (existing (mapcar 'list (org-property-values prop)))
  12785. rpl
  12786. (val (cond
  12787. ((stringp value) value)
  12788. ((and allowed (integerp value))
  12789. (or (car (nth (1- value) allowed))
  12790. (car (org-last allowed))))
  12791. (allowed
  12792. (message "Select 1-9,0, [RET%s]: %s"
  12793. (if cur (concat "=" cur) "")
  12794. (mapconcat 'car allowed " "))
  12795. (setq rpl (read-char-exclusive))
  12796. (if (equal rpl ?\r)
  12797. cur
  12798. (setq rpl (- rpl ?0))
  12799. (if (equal rpl 0) (setq rpl 10))
  12800. (if (and (> rpl 0) (<= rpl (length allowed)))
  12801. (car (nth (1- rpl) allowed))
  12802. (org-completing-read "Effort: " allowed nil))))
  12803. (t
  12804. (let (org-completion-use-ido org-completion-use-iswitchb)
  12805. (org-completing-read
  12806. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12807. (concat "[" cur "]") "")
  12808. ": ")
  12809. existing nil nil "" nil cur))))))
  12810. (unless (equal (org-entry-get nil prop) val)
  12811. (org-entry-put nil prop val))
  12812. (message "%s is now %s" prop val)))
  12813. (defun org-at-property-p ()
  12814. "Is cursor inside a property drawer?"
  12815. (save-excursion
  12816. (beginning-of-line 1)
  12817. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12818. (save-match-data ;; Used by calling procedures
  12819. (let ((p (point))
  12820. (range (unless (org-before-first-heading-p)
  12821. (org-get-property-block))))
  12822. (and range (<= (car range) p) (< p (cdr range))))))))
  12823. (defun org-get-property-block (&optional beg end force)
  12824. "Return the (beg . end) range of the body of the property drawer.
  12825. BEG and END can be beginning and end of subtree, if not given
  12826. they will be found.
  12827. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12828. (catch 'exit
  12829. (save-excursion
  12830. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12831. (end (or end (progn (outline-next-heading) (point)))))
  12832. (goto-char beg)
  12833. (if (re-search-forward org-property-start-re end t)
  12834. (setq beg (1+ (match-end 0)))
  12835. (if force
  12836. (save-excursion
  12837. (org-insert-property-drawer)
  12838. (setq end (progn (outline-next-heading) (point))))
  12839. (throw 'exit nil))
  12840. (goto-char beg)
  12841. (if (re-search-forward org-property-start-re end t)
  12842. (setq beg (1+ (match-end 0)))))
  12843. (if (re-search-forward org-property-end-re end t)
  12844. (setq end (match-beginning 0))
  12845. (or force (throw 'exit nil))
  12846. (goto-char beg)
  12847. (setq end beg)
  12848. (org-indent-line-function)
  12849. (insert ":END:\n"))
  12850. (cons beg end)))))
  12851. (defun org-entry-properties (&optional pom which specific)
  12852. "Get all properties of the entry at point-or-marker POM.
  12853. This includes the TODO keyword, the tags, time strings for deadline,
  12854. scheduled, and clocking, and any additional properties defined in the
  12855. entry. The return value is an alist, keys may occur multiple times
  12856. if the property key was used several times.
  12857. POM may also be nil, in which case the current entry is used.
  12858. If WHICH is nil or `all', get all properties. If WHICH is
  12859. `special' or `standard', only get that subclass. If WHICH
  12860. is a string only get exactly this property. SPECIFIC can be a string, the
  12861. specific property we are interested in. Specifying it can speed
  12862. things up because then unnecessary parsing is avoided."
  12863. (setq which (or which 'all))
  12864. (org-with-point-at pom
  12865. (let ((clockstr (substring org-clock-string 0 -1))
  12866. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12867. (case-fold-search nil)
  12868. beg end range props sum-props key key1 value string clocksum)
  12869. (save-excursion
  12870. (when (condition-case nil
  12871. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12872. (error nil))
  12873. (setq beg (point))
  12874. (setq sum-props (get-text-property (point) 'org-summaries))
  12875. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12876. (outline-next-heading)
  12877. (setq end (point))
  12878. (when (memq which '(all special))
  12879. ;; Get the special properties, like TODO and tags
  12880. (goto-char beg)
  12881. (when (and (or (not specific) (string= specific "TODO"))
  12882. (looking-at org-todo-line-regexp) (match-end 2))
  12883. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12884. (when (and (or (not specific) (string= specific "PRIORITY"))
  12885. (looking-at org-priority-regexp))
  12886. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12887. (when (or (not specific) (string= specific "FILE"))
  12888. (push (cons "FILE" buffer-file-name) props))
  12889. (when (and (or (not specific) (string= specific "TAGS"))
  12890. (setq value (org-get-tags-string))
  12891. (string-match "\\S-" value))
  12892. (push (cons "TAGS" value) props))
  12893. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12894. (setq value (org-get-tags-at)))
  12895. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12896. ":"))
  12897. props))
  12898. (when (or (not specific) (string= specific "BLOCKED"))
  12899. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12900. (when (or (not specific)
  12901. (member specific
  12902. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12903. "TIMESTAMP" "TIMESTAMP_IA")))
  12904. (catch 'match
  12905. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12906. (setq key (if (match-end 1)
  12907. (substring (org-match-string-no-properties 1)
  12908. 0 -1))
  12909. string (if (equal key clockstr)
  12910. (org-no-properties
  12911. (org-trim
  12912. (buffer-substring
  12913. (match-beginning 3) (goto-char
  12914. (point-at-eol)))))
  12915. (substring (org-match-string-no-properties 3)
  12916. 1 -1)))
  12917. ;; Get the correct property name from the key. This is
  12918. ;; necessary if the user has configured time keywords.
  12919. (setq key1 (concat key ":"))
  12920. (cond
  12921. ((not key)
  12922. (setq key
  12923. (if (= (char-after (match-beginning 3)) ?\[)
  12924. "TIMESTAMP_IA" "TIMESTAMP")))
  12925. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12926. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12927. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12928. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12929. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12930. (progn
  12931. (push (cons key string) props)
  12932. ;; no need to search further if match is found
  12933. (throw 'match t))
  12934. (when (or (equal key "CLOCK") (not (assoc key props)))
  12935. (push (cons key string) props))))))
  12936. )
  12937. (when (memq which '(all standard))
  12938. ;; Get the standard properties, like :PROP: ...
  12939. (setq range (org-get-property-block beg end))
  12940. (when range
  12941. (goto-char (car range))
  12942. (while (re-search-forward
  12943. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12944. (cdr range) t)
  12945. (setq key (org-match-string-no-properties 1)
  12946. value (org-trim (or (org-match-string-no-properties 2) "")))
  12947. (unless (member key excluded)
  12948. (push (cons key (or value "")) props)))))
  12949. (if clocksum
  12950. (push (cons "CLOCKSUM"
  12951. (org-columns-number-to-string (/ (float clocksum) 60.)
  12952. 'add_times))
  12953. props))
  12954. (unless (assoc "CATEGORY" props)
  12955. (push (cons "CATEGORY" (org-get-category)) props))
  12956. (append sum-props (nreverse props)))))))
  12957. (defun org-entry-get (pom property &optional inherit literal-nil)
  12958. "Get value of PROPERTY for entry at point-or-marker POM.
  12959. If INHERIT is non-nil and the entry does not have the property,
  12960. then also check higher levels of the hierarchy.
  12961. If INHERIT is the symbol `selective', use inheritance only if the setting
  12962. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12963. If the property is present but empty, the return value is the empty string.
  12964. If the property is not present at all, nil is returned.
  12965. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12966. do not interpret it as the list atom nil. This is used for inheritance
  12967. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12968. (org-with-point-at pom
  12969. (if (and inherit (if (eq inherit 'selective)
  12970. (org-property-inherit-p property)
  12971. t))
  12972. (org-entry-get-with-inheritance property literal-nil)
  12973. (if (member property org-special-properties)
  12974. ;; We need a special property. Use `org-entry-properties' to
  12975. ;; retrieve it, but specify the wanted property
  12976. (cdr (assoc property (org-entry-properties nil 'special property)))
  12977. (let ((range (unless (org-before-first-heading-p)
  12978. (org-get-property-block)))
  12979. (props (list (or (assoc property org-file-properties)
  12980. (assoc property org-global-properties)
  12981. (assoc property org-global-properties-fixed))))
  12982. val)
  12983. (flet ((ap (key)
  12984. (when (re-search-forward
  12985. (org-re-property key) (cdr range) t)
  12986. (setq props
  12987. (org-update-property-plist
  12988. key
  12989. (if (match-end 1)
  12990. (org-match-string-no-properties 1) "")
  12991. props)))))
  12992. (when (and range (goto-char (car range)))
  12993. (ap property)
  12994. (goto-char (car range))
  12995. (while (ap (concat property "+")))
  12996. (setq val (cdr (assoc property props)))
  12997. (when val (if literal-nil val (org-not-nil val))))))))))
  12998. (defun org-property-or-variable-value (var &optional inherit)
  12999. "Check if there is a property fixing the value of VAR.
  13000. If yes, return this value. If not, return the current value of the variable."
  13001. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13002. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13003. (read prop)
  13004. (symbol-value var))))
  13005. (defun org-entry-delete (pom property)
  13006. "Delete the property PROPERTY from entry at point-or-marker POM."
  13007. (org-with-point-at pom
  13008. (if (member property org-special-properties)
  13009. nil ; cannot delete these properties.
  13010. (let ((range (org-get-property-block)))
  13011. (if (and range
  13012. (goto-char (car range))
  13013. (re-search-forward
  13014. (org-re-property property)
  13015. (cdr range) t))
  13016. (progn
  13017. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13018. t)
  13019. nil)))))
  13020. ;; Multi-values properties are properties that contain multiple values
  13021. ;; These values are assumed to be single words, separated by whitespace.
  13022. (defun org-entry-add-to-multivalued-property (pom property value)
  13023. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13024. (let* ((old (org-entry-get pom property))
  13025. (values (and old (org-split-string old "[ \t]"))))
  13026. (setq value (org-entry-protect-space value))
  13027. (unless (member value values)
  13028. (setq values (cons value values))
  13029. (org-entry-put pom property
  13030. (mapconcat 'identity values " ")))))
  13031. (defun org-entry-remove-from-multivalued-property (pom property value)
  13032. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13033. (let* ((old (org-entry-get pom property))
  13034. (values (and old (org-split-string old "[ \t]"))))
  13035. (setq value (org-entry-protect-space value))
  13036. (when (member value values)
  13037. (setq values (delete value values))
  13038. (org-entry-put pom property
  13039. (mapconcat 'identity values " ")))))
  13040. (defun org-entry-member-in-multivalued-property (pom property value)
  13041. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13042. (let* ((old (org-entry-get pom property))
  13043. (values (and old (org-split-string old "[ \t]"))))
  13044. (setq value (org-entry-protect-space value))
  13045. (member value values)))
  13046. (defun org-entry-get-multivalued-property (pom property)
  13047. "Return a list of values in a multivalued property."
  13048. (let* ((value (org-entry-get pom property))
  13049. (values (and value (org-split-string value "[ \t]"))))
  13050. (mapcar 'org-entry-restore-space values)))
  13051. (defun org-entry-put-multivalued-property (pom property &rest values)
  13052. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13053. VALUES should be a list of strings. Spaces will be protected."
  13054. (org-entry-put pom property
  13055. (mapconcat 'org-entry-protect-space values " "))
  13056. (let* ((value (org-entry-get pom property))
  13057. (values (and value (org-split-string value "[ \t]"))))
  13058. (mapcar 'org-entry-restore-space values)))
  13059. (defun org-entry-protect-space (s)
  13060. "Protect spaces and newline in string S."
  13061. (while (string-match " " s)
  13062. (setq s (replace-match "%20" t t s)))
  13063. (while (string-match "\n" s)
  13064. (setq s (replace-match "%0A" t t s)))
  13065. s)
  13066. (defun org-entry-restore-space (s)
  13067. "Restore spaces and newline in string S."
  13068. (while (string-match "%20" s)
  13069. (setq s (replace-match " " t t s)))
  13070. (while (string-match "%0A" s)
  13071. (setq s (replace-match "\n" t t s)))
  13072. s)
  13073. (defvar org-entry-property-inherited-from (make-marker)
  13074. "Marker pointing to the entry from where a property was inherited.
  13075. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13076. location of the entry where the inheritance search matched. If there was
  13077. no match, the marker will point nowhere.
  13078. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13079. is set.")
  13080. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13081. "Get entry property, and search higher levels if not present.
  13082. The search will stop at the first ancestor which has the property defined.
  13083. If the value found is \"nil\", return nil to show that the property
  13084. should be considered as undefined (this is the meaning of nil here).
  13085. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13086. (move-marker org-entry-property-inherited-from nil)
  13087. (let (tmp)
  13088. (unless (org-before-first-heading-p)
  13089. (save-excursion
  13090. (save-restriction
  13091. (widen)
  13092. (catch 'ex
  13093. (while t
  13094. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13095. (org-back-to-heading t)
  13096. (move-marker org-entry-property-inherited-from (point))
  13097. (throw 'ex tmp))
  13098. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13099. (setq tmp (or tmp
  13100. (cdr (assoc property org-file-properties))
  13101. (cdr (assoc property org-global-properties))
  13102. (cdr (assoc property org-global-properties-fixed))))
  13103. (if literal-nil tmp (org-not-nil tmp))))
  13104. (defvar org-property-changed-functions nil
  13105. "Hook called when the value of a property has changed.
  13106. Each hook function should accept two arguments, the name of the property
  13107. and the new value.")
  13108. (defun org-entry-put (pom property value)
  13109. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13110. (org-with-point-at pom
  13111. (org-back-to-heading t)
  13112. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13113. range)
  13114. (cond
  13115. ((equal property "TODO")
  13116. (when (and (stringp value) (string-match "\\S-" value)
  13117. (not (member value org-todo-keywords-1)))
  13118. (error "\"%s\" is not a valid TODO state" value))
  13119. (if (or (not value)
  13120. (not (string-match "\\S-" value)))
  13121. (setq value 'none))
  13122. (org-todo value)
  13123. (org-set-tags nil 'align))
  13124. ((equal property "PRIORITY")
  13125. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13126. (string-to-char value) ?\ ))
  13127. (org-set-tags nil 'align))
  13128. ((equal property "SCHEDULED")
  13129. (if (re-search-forward org-scheduled-time-regexp end t)
  13130. (cond
  13131. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13132. ((eq value 'later) (org-timestamp-change 1 'day))
  13133. (t (call-interactively 'org-schedule)))
  13134. (call-interactively 'org-schedule)))
  13135. ((equal property "DEADLINE")
  13136. (if (re-search-forward org-deadline-time-regexp end t)
  13137. (cond
  13138. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13139. ((eq value 'later) (org-timestamp-change 1 'day))
  13140. (t (call-interactively 'org-deadline)))
  13141. (call-interactively 'org-deadline)))
  13142. ((member property org-special-properties)
  13143. (error "The %s property can not yet be set with `org-entry-put'"
  13144. property))
  13145. (t ; a non-special property
  13146. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13147. (setq range (org-get-property-block beg end 'force))
  13148. (goto-char (car range))
  13149. (if (re-search-forward
  13150. (org-re-property property) (cdr range) t)
  13151. (progn
  13152. (delete-region (match-beginning 0) (match-end 0))
  13153. (goto-char (match-beginning 0)))
  13154. (goto-char (cdr range))
  13155. (insert "\n")
  13156. (backward-char 1)
  13157. (org-indent-line-function))
  13158. (insert ":" property ":")
  13159. (and value (insert " " value))
  13160. (org-indent-line-function)))))
  13161. (run-hook-with-args 'org-property-changed-functions property value)))
  13162. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13163. "Get all property keys in the current buffer.
  13164. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13165. like tags and TODO state.
  13166. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13167. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13168. and others.
  13169. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13170. formats in the current buffer."
  13171. (let (rtn range cfmt s p)
  13172. (save-excursion
  13173. (save-restriction
  13174. (widen)
  13175. (goto-char (point-min))
  13176. (while (re-search-forward org-property-start-re nil t)
  13177. (setq range (org-get-property-block))
  13178. (goto-char (car range))
  13179. (while (re-search-forward
  13180. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13181. (cdr range) t)
  13182. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13183. (outline-next-heading))))
  13184. (when include-specials
  13185. (setq rtn (append org-special-properties rtn)))
  13186. (when include-defaults
  13187. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13188. (add-to-list 'rtn org-effort-property))
  13189. (when include-columns
  13190. (save-excursion
  13191. (save-restriction
  13192. (widen)
  13193. (goto-char (point-min))
  13194. (while (re-search-forward
  13195. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13196. nil t)
  13197. (setq cfmt (match-string 2) s 0)
  13198. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13199. cfmt s)
  13200. (setq s (match-end 0)
  13201. p (match-string 1 cfmt))
  13202. (unless (or (equal p "ITEM")
  13203. (member p org-special-properties))
  13204. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13205. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13206. (defun org-property-values (key)
  13207. "Return a list of all values of property KEY in the current buffer."
  13208. (save-excursion
  13209. (save-restriction
  13210. (widen)
  13211. (goto-char (point-min))
  13212. (let ((re (org-re-property key))
  13213. values)
  13214. (while (re-search-forward re nil t)
  13215. (add-to-list 'values (org-trim (match-string 1))))
  13216. (delete "" values)))))
  13217. (defun org-insert-property-drawer ()
  13218. "Insert a property drawer into the current entry."
  13219. (org-back-to-heading t)
  13220. (looking-at org-outline-regexp)
  13221. (let ((indent (if org-adapt-indentation
  13222. (- (match-end 0) (match-beginning 0))
  13223. 0))
  13224. (beg (point))
  13225. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13226. end hiddenp)
  13227. (outline-next-heading)
  13228. (setq end (point))
  13229. (goto-char beg)
  13230. (while (re-search-forward re end t))
  13231. (setq hiddenp (outline-invisible-p))
  13232. (end-of-line 1)
  13233. (and (equal (char-after) ?\n) (forward-char 1))
  13234. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13235. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13236. ;; just skip this line
  13237. (beginning-of-line 2)
  13238. ;; Drawer start, find the end
  13239. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13240. (beginning-of-line 1)))
  13241. (org-skip-over-state-notes)
  13242. (skip-chars-backward " \t\n\r")
  13243. (if (eq (char-before) ?*) (forward-char 1))
  13244. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13245. (beginning-of-line 0)
  13246. (org-indent-to-column indent)
  13247. (beginning-of-line 2)
  13248. (org-indent-to-column indent)
  13249. (beginning-of-line 0)
  13250. (if hiddenp
  13251. (save-excursion
  13252. (org-back-to-heading t)
  13253. (hide-entry))
  13254. (org-flag-drawer t))))
  13255. (defun org-insert-drawer (&optional arg drawer)
  13256. "Insert a drawer at point.
  13257. Optional argument DRAWER, when non-nil, is a string representing
  13258. drawer's name. Otherwise, the user is prompted for a name.
  13259. If a region is active, insert the drawer around that region
  13260. instead.
  13261. Point is left between drawer's boundaries."
  13262. (interactive "P")
  13263. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13264. "LOGBOOK"))
  13265. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13266. ;; internally by Org. They are meant to be inserted
  13267. ;; automatically.
  13268. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13269. ;; Remove system drawers from list. Note: For some reason,
  13270. ;; `org-completing-read' ignores the predicate while
  13271. ;; `completing-read' handles it fine.
  13272. (drawer (if arg "PROPERTIES"
  13273. (or drawer
  13274. (completing-read
  13275. "Drawer: " org-drawers
  13276. (lambda (d) (not (member d system-drawers))))))))
  13277. (cond
  13278. ;; With C-u, fall back on `org-insert-property-drawer'
  13279. (arg (org-insert-property-drawer))
  13280. ;; With an active region, insert a drawer at point.
  13281. ((not (org-region-active-p))
  13282. (progn
  13283. (unless (bolp) (insert "\n"))
  13284. (insert (format ":%s:\n\n:END:\n" drawer))
  13285. (forward-line -2)))
  13286. ;; Otherwise, insert the drawer at point
  13287. (t
  13288. (let ((rbeg (region-beginning))
  13289. (rend (copy-marker (region-end))))
  13290. (unwind-protect
  13291. (progn
  13292. (goto-char rbeg)
  13293. (beginning-of-line)
  13294. (when (save-excursion
  13295. (re-search-forward org-outline-regexp-bol rend t))
  13296. (error "Drawers cannot contain headlines"))
  13297. ;; Position point at the beginning of the first
  13298. ;; non-blank line in region. Insert drawer's opening
  13299. ;; there, then indent it.
  13300. (org-skip-whitespace)
  13301. (beginning-of-line)
  13302. (insert ":" drawer ":\n")
  13303. (forward-line -1)
  13304. (indent-for-tab-command)
  13305. ;; Move point to the beginning of the first blank line
  13306. ;; after the last non-blank line in region. Insert
  13307. ;; drawer's closing, then indent it.
  13308. (goto-char rend)
  13309. (skip-chars-backward " \r\t\n")
  13310. (insert "\n:END:")
  13311. (indent-for-tab-command)
  13312. (unless (eolp) (insert "\n")))
  13313. ;; Clear marker, whatever the outcome of insertion is.
  13314. (set-marker rend nil)))))))
  13315. (defvar org-property-set-functions-alist nil
  13316. "Property set function alist.
  13317. Each entry should have the following format:
  13318. (PROPERTY . READ-FUNCTION)
  13319. The read function will be called with the same argument as
  13320. `org-completing-read'.")
  13321. (defun org-set-property-function (property)
  13322. "Get the function that should be used to set PROPERTY.
  13323. This is computed according to `org-property-set-functions-alist'."
  13324. (or (cdr (assoc property org-property-set-functions-alist))
  13325. 'org-completing-read))
  13326. (defun org-read-property-value (property)
  13327. "Read PROPERTY value from user."
  13328. (let* ((completion-ignore-case t)
  13329. (allowed (org-property-get-allowed-values nil property 'table))
  13330. (cur (org-entry-get nil property))
  13331. (prompt (concat property " value"
  13332. (if (and cur (string-match "\\S-" cur))
  13333. (concat " [" cur "]") "") ": "))
  13334. (set-function (org-set-property-function property))
  13335. (val (if allowed
  13336. (funcall set-function prompt allowed nil
  13337. (not (get-text-property 0 'org-unrestricted
  13338. (caar allowed))))
  13339. (let (org-completion-use-ido org-completion-use-iswitchb)
  13340. (funcall set-function prompt
  13341. (mapcar 'list (org-property-values property))
  13342. nil nil "" nil cur)))))
  13343. (if (equal val "")
  13344. cur
  13345. val)))
  13346. (defvar org-last-set-property nil)
  13347. (defun org-read-property-name ()
  13348. "Read a property name."
  13349. (let* ((completion-ignore-case t)
  13350. (keys (org-buffer-property-keys nil t t))
  13351. (default-prop (or (save-excursion
  13352. (save-match-data
  13353. (beginning-of-line)
  13354. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13355. (null (string= (match-string 1) "END"))
  13356. (match-string 1))))
  13357. org-last-set-property))
  13358. (property (org-icompleting-read
  13359. (concat "Property"
  13360. (if default-prop (concat " [" default-prop "]") "")
  13361. ": ")
  13362. (mapcar 'list keys)
  13363. nil nil nil nil
  13364. default-prop
  13365. )))
  13366. (if (member property keys)
  13367. property
  13368. (or (cdr (assoc (downcase property)
  13369. (mapcar (lambda (x) (cons (downcase x) x))
  13370. keys)))
  13371. property))))
  13372. (defun org-set-property (property value)
  13373. "In the current entry, set PROPERTY to VALUE.
  13374. When called interactively, this will prompt for a property name, offering
  13375. completion on existing and default properties. And then it will prompt
  13376. for a value, offering completion either on allowed values (via an inherited
  13377. xxx_ALL property) or on existing values in other instances of this property
  13378. in the current file."
  13379. (interactive (list nil nil))
  13380. (let* ((property (or property (org-read-property-name)))
  13381. (value (or value (org-read-property-value property)))
  13382. (fn (assoc property org-properties-postprocess-alist)))
  13383. (setq org-last-set-property property)
  13384. ;; Possibly postprocess the inserted value:
  13385. (when fn (setq value (funcall (cadr fn) value)))
  13386. (unless (equal (org-entry-get nil property) value)
  13387. (org-entry-put nil property value))))
  13388. (defun org-delete-property (property)
  13389. "In the current entry, delete PROPERTY."
  13390. (interactive
  13391. (let* ((completion-ignore-case t)
  13392. (prop (org-icompleting-read "Property: "
  13393. (org-entry-properties nil 'standard))))
  13394. (list prop)))
  13395. (message "Property %s %s" property
  13396. (if (org-entry-delete nil property)
  13397. "deleted"
  13398. "was not present in the entry")))
  13399. (defun org-delete-property-globally (property)
  13400. "Remove PROPERTY globally, from all entries."
  13401. (interactive
  13402. (let* ((completion-ignore-case t)
  13403. (prop (org-icompleting-read
  13404. "Globally remove property: "
  13405. (mapcar 'list (org-buffer-property-keys)))))
  13406. (list prop)))
  13407. (save-excursion
  13408. (save-restriction
  13409. (widen)
  13410. (goto-char (point-min))
  13411. (let ((cnt 0))
  13412. (while (re-search-forward
  13413. (org-re-property property)
  13414. nil t)
  13415. (setq cnt (1+ cnt))
  13416. (replace-match ""))
  13417. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13418. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13419. (defun org-compute-property-at-point ()
  13420. "Compute the property at point.
  13421. This looks for an enclosing column format, extracts the operator and
  13422. then applies it to the property in the column format's scope."
  13423. (interactive)
  13424. (unless (org-at-property-p)
  13425. (error "Not at a property"))
  13426. (let ((prop (org-match-string-no-properties 2)))
  13427. (org-columns-get-format-and-top-level)
  13428. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13429. (error "No operator defined for property %s" prop))
  13430. (org-columns-compute prop)))
  13431. (defvar org-property-allowed-value-functions nil
  13432. "Hook for functions supplying allowed values for a specific property.
  13433. The functions must take a single argument, the name of the property, and
  13434. return a flat list of allowed values. If \":ETC\" is one of
  13435. the values, this means that these values are intended as defaults for
  13436. completion, but that other values should be allowed too.
  13437. The functions must return nil if they are not responsible for this
  13438. property.")
  13439. (defun org-property-get-allowed-values (pom property &optional table)
  13440. "Get allowed values for the property PROPERTY.
  13441. When TABLE is non-nil, return an alist that can directly be used for
  13442. completion."
  13443. (let (vals)
  13444. (cond
  13445. ((equal property "TODO")
  13446. (setq vals (org-with-point-at pom
  13447. (append org-todo-keywords-1 '("")))))
  13448. ((equal property "PRIORITY")
  13449. (let ((n org-lowest-priority))
  13450. (while (>= n org-highest-priority)
  13451. (push (char-to-string n) vals)
  13452. (setq n (1- n)))))
  13453. ((member property org-special-properties))
  13454. ((setq vals (run-hook-with-args-until-success
  13455. 'org-property-allowed-value-functions property)))
  13456. (t
  13457. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13458. (when (and vals (string-match "\\S-" vals))
  13459. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13460. (setq vals (mapcar (lambda (x)
  13461. (cond ((stringp x) x)
  13462. ((numberp x) (number-to-string x))
  13463. ((symbolp x) (symbol-name x))
  13464. (t "???")))
  13465. vals)))))
  13466. (when (member ":ETC" vals)
  13467. (setq vals (remove ":ETC" vals))
  13468. (org-add-props (car vals) '(org-unrestricted t)))
  13469. (if table (mapcar 'list vals) vals)))
  13470. (defun org-property-previous-allowed-value (&optional previous)
  13471. "Switch to the next allowed value for this property."
  13472. (interactive)
  13473. (org-property-next-allowed-value t))
  13474. (defun org-property-next-allowed-value (&optional previous)
  13475. "Switch to the next allowed value for this property."
  13476. (interactive)
  13477. (unless (org-at-property-p)
  13478. (error "Not at a property"))
  13479. (let* ((key (match-string 2))
  13480. (value (match-string 3))
  13481. (allowed (or (org-property-get-allowed-values (point) key)
  13482. (and (member value '("[ ]" "[-]" "[X]"))
  13483. '("[ ]" "[X]"))))
  13484. nval)
  13485. (unless allowed
  13486. (error "Allowed values for this property have not been defined"))
  13487. (if previous (setq allowed (reverse allowed)))
  13488. (if (member value allowed)
  13489. (setq nval (car (cdr (member value allowed)))))
  13490. (setq nval (or nval (car allowed)))
  13491. (if (equal nval value)
  13492. (error "Only one allowed value for this property"))
  13493. (org-at-property-p)
  13494. (replace-match (concat " :" key ": " nval) t t)
  13495. (org-indent-line-function)
  13496. (beginning-of-line 1)
  13497. (skip-chars-forward " \t")
  13498. (run-hook-with-args 'org-property-changed-functions key nval)))
  13499. (defun org-find-olp (path &optional this-buffer)
  13500. "Return a marker pointing to the entry at outline path OLP.
  13501. If anything goes wrong, throw an error.
  13502. You can wrap this call to catch the error like this:
  13503. (condition-case msg
  13504. (org-mobile-locate-entry (match-string 4))
  13505. (error (nth 1 msg)))
  13506. The return value will then be either a string with the error message,
  13507. or a marker if everything is OK.
  13508. If THIS-BUFFER is set, the outline path does not contain a file,
  13509. only headings."
  13510. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13511. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13512. (level 1)
  13513. (lmin 1)
  13514. (lmax 1)
  13515. limit re end found pos heading cnt flevel)
  13516. (unless buffer (error "File not found :%s" file))
  13517. (with-current-buffer buffer
  13518. (save-excursion
  13519. (save-restriction
  13520. (widen)
  13521. (setq limit (point-max))
  13522. (goto-char (point-min))
  13523. (while (setq heading (pop path))
  13524. (setq re (format org-complex-heading-regexp-format
  13525. (regexp-quote heading)))
  13526. (setq cnt 0 pos (point))
  13527. (while (re-search-forward re end t)
  13528. (setq level (- (match-end 1) (match-beginning 1)))
  13529. (if (and (>= level lmin) (<= level lmax))
  13530. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13531. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13532. lmax heading))
  13533. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13534. lmax heading))
  13535. (goto-char found)
  13536. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13537. (setq end (save-excursion (org-end-of-subtree t t))))
  13538. (when (org-at-heading-p)
  13539. (move-marker (make-marker) (point))))))))
  13540. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13541. "Find node HEADING in BUFFER.
  13542. Return a marker to the heading if it was found, or nil if not.
  13543. If POS-ONLY is set, return just the position instead of a marker.
  13544. The heading text must match exact, but it may have a TODO keyword,
  13545. a priority cookie and tags in the standard locations."
  13546. (with-current-buffer (or buffer (current-buffer))
  13547. (save-excursion
  13548. (save-restriction
  13549. (widen)
  13550. (goto-char (point-min))
  13551. (let (case-fold-search)
  13552. (if (re-search-forward
  13553. (format org-complex-heading-regexp-format
  13554. (regexp-quote heading)) nil t)
  13555. (if pos-only
  13556. (match-beginning 0)
  13557. (move-marker (make-marker) (match-beginning 0)))))))))
  13558. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13559. "Find Org node headline HEADING in all .org files in directory DIR.
  13560. When the target headline is found, return a marker to this location."
  13561. (let ((files (directory-files (or dir default-directory)
  13562. nil "\\`[^.#].*\\.org\\'"))
  13563. file visiting m buffer)
  13564. (catch 'found
  13565. (while (setq file (pop files))
  13566. (message "trying %s" file)
  13567. (setq visiting (org-find-base-buffer-visiting file))
  13568. (setq buffer (or visiting (find-file-noselect file)))
  13569. (setq m (org-find-exact-headline-in-buffer
  13570. heading buffer))
  13571. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13572. (and m (throw 'found m))))))
  13573. (defun org-find-entry-with-id (ident)
  13574. "Locate the entry that contains the ID property with exact value IDENT.
  13575. IDENT can be a string, a symbol or a number, this function will search for
  13576. the string representation of it.
  13577. Return the position where this entry starts, or nil if there is no such entry."
  13578. (interactive "sID: ")
  13579. (let ((id (cond
  13580. ((stringp ident) ident)
  13581. ((symbol-name ident) (symbol-name ident))
  13582. ((numberp ident) (number-to-string ident))
  13583. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13584. (case-fold-search nil))
  13585. (save-excursion
  13586. (save-restriction
  13587. (widen)
  13588. (goto-char (point-min))
  13589. (when (re-search-forward
  13590. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13591. nil t)
  13592. (org-back-to-heading t)
  13593. (point))))))
  13594. ;;;; Timestamps
  13595. (defvar org-last-changed-timestamp nil)
  13596. (defvar org-last-inserted-timestamp nil
  13597. "The last time stamp inserted with `org-insert-time-stamp'.")
  13598. (defvar org-time-was-given) ; dynamically scoped parameter
  13599. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13600. (defvar org-ts-what) ; dynamically scoped parameter
  13601. (defun org-time-stamp (arg &optional inactive)
  13602. "Prompt for a date/time and insert a time stamp.
  13603. If the user specifies a time like HH:MM, or if this command is called
  13604. with a prefix argument, the time stamp will contain date and time.
  13605. Otherwise, only the date will be included. All parts of a date not
  13606. specified by the user will be filled in from the current date/time.
  13607. So if you press just return without typing anything, the time stamp
  13608. will represent the current date/time. If there is already a timestamp
  13609. at the cursor, it will be modified."
  13610. (interactive "P")
  13611. (let* ((ts nil)
  13612. (default-time
  13613. ;; Default time is either today, or, when entering a range,
  13614. ;; the range start.
  13615. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13616. (save-excursion
  13617. (re-search-backward
  13618. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13619. (- (point) 20) t)))
  13620. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13621. (current-time)))
  13622. (default-input (and ts (org-get-compact-tod ts)))
  13623. (repeater (save-excursion
  13624. (save-match-data
  13625. (beginning-of-line)
  13626. (when (re-search-forward
  13627. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13628. (save-excursion (progn (end-of-line) (point))) t)
  13629. (match-string 0)))))
  13630. org-time-was-given org-end-time-was-given time)
  13631. (cond
  13632. ((and (org-at-timestamp-p t)
  13633. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13634. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13635. (insert "--")
  13636. (setq time (let ((this-command this-command))
  13637. (org-read-date arg 'totime nil nil
  13638. default-time default-input inactive)))
  13639. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13640. ((org-at-timestamp-p t)
  13641. (setq time (let ((this-command this-command))
  13642. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13643. (when (org-at-timestamp-p t) ; just to get the match data
  13644. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13645. (replace-match "")
  13646. (setq org-last-changed-timestamp
  13647. (org-insert-time-stamp
  13648. time (or org-time-was-given arg)
  13649. inactive nil nil (list org-end-time-was-given)))
  13650. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13651. (setq org-last-changed-timestamp
  13652. (concat (substring org-last-inserted-timestamp 0 -1)
  13653. " " repeater ">"))))
  13654. (message "Timestamp updated"))
  13655. (t
  13656. (setq time (let ((this-command this-command))
  13657. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13658. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13659. nil nil (list org-end-time-was-given))))))
  13660. ;; FIXME: can we use this for something else, like computing time differences?
  13661. (defun org-get-compact-tod (s)
  13662. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13663. (let* ((t1 (match-string 1 s))
  13664. (h1 (string-to-number (match-string 2 s)))
  13665. (m1 (string-to-number (match-string 3 s)))
  13666. (t2 (and (match-end 4) (match-string 5 s)))
  13667. (h2 (and t2 (string-to-number (match-string 6 s))))
  13668. (m2 (and t2 (string-to-number (match-string 7 s))))
  13669. dh dm)
  13670. (if (not t2)
  13671. t1
  13672. (setq dh (- h2 h1) dm (- m2 m1))
  13673. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13674. (concat t1 "+" (number-to-string dh)
  13675. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13676. (defun org-time-stamp-inactive (&optional arg)
  13677. "Insert an inactive time stamp.
  13678. An inactive time stamp is enclosed in square brackets instead of angle
  13679. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13680. does not link to the calendar and cannot be changed with the S-cursor keys.
  13681. So these are more for recording a certain time/date."
  13682. (interactive "P")
  13683. (org-time-stamp arg 'inactive))
  13684. (defvar org-date-ovl (make-overlay 1 1))
  13685. (overlay-put org-date-ovl 'face 'org-warning)
  13686. (org-detach-overlay org-date-ovl)
  13687. (defvar org-ans1) ; dynamically scoped parameter
  13688. (defvar org-ans2) ; dynamically scoped parameter
  13689. (defvar org-plain-time-of-day-regexp) ; defined below
  13690. (defvar org-overriding-default-time nil) ; dynamically scoped
  13691. (defvar org-read-date-overlay nil)
  13692. (defvar org-dcst nil) ; dynamically scoped
  13693. (defvar org-read-date-history nil)
  13694. (defvar org-read-date-final-answer nil)
  13695. (defvar org-read-date-analyze-futurep nil)
  13696. (defvar org-read-date-analyze-forced-year nil)
  13697. (defvar org-read-date-inactive)
  13698. (defun org-read-date (&optional with-time to-time from-string prompt
  13699. default-time default-input inactive)
  13700. "Read a date, possibly a time, and make things smooth for the user.
  13701. The prompt will suggest to enter an ISO date, but you can also enter anything
  13702. which will at least partially be understood by `parse-time-string'.
  13703. Unrecognized parts of the date will default to the current day, month, year,
  13704. hour and minute. If this command is called to replace a timestamp at point,
  13705. or to enter the second timestamp of a range, the default time is taken
  13706. from the existing stamp. Furthermore, the command prefers the future,
  13707. so if you are giving a date where the year is not given, and the day-month
  13708. combination is already past in the current year, it will assume you
  13709. mean next year. For details, see the manual. A few examples:
  13710. 3-2-5 --> 2003-02-05
  13711. feb 15 --> currentyear-02-15
  13712. 2/15 --> currentyear-02-15
  13713. sep 12 9 --> 2009-09-12
  13714. 12:45 --> today 12:45
  13715. 22 sept 0:34 --> currentyear-09-22 0:34
  13716. 12 --> currentyear-currentmonth-12
  13717. Fri --> nearest Friday (today or later)
  13718. etc.
  13719. Furthermore you can specify a relative date by giving, as the *first* thing
  13720. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13721. change in days weeks, months, years.
  13722. With a single plus or minus, the date is relative to today. With a double
  13723. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13724. +4d --> four days from today
  13725. +4 --> same as above
  13726. +2w --> two weeks from today
  13727. ++5 --> five days from default date
  13728. The function understands only English month and weekday abbreviations,
  13729. but this can be configured with the variables `parse-time-months' and
  13730. `parse-time-weekdays'.
  13731. While prompting, a calendar is popped up - you can also select the
  13732. date with the mouse (button 1). The calendar shows a period of three
  13733. months. To scroll it to other months, use the keys `>' and `<'.
  13734. If you don't like the calendar, turn it off with
  13735. \(setq org-read-date-popup-calendar nil)
  13736. With optional argument TO-TIME, the date will immediately be converted
  13737. to an internal time.
  13738. With an optional argument WITH-TIME, the prompt will suggest to also
  13739. insert a time. Note that when WITH-TIME is not set, you can still
  13740. enter a time, and this function will inform the calling routine about
  13741. this change. The calling routine may then choose to change the format
  13742. used to insert the time stamp into the buffer to include the time.
  13743. With optional argument FROM-STRING, read from this string instead from
  13744. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13745. the time/date that is used for everything that is not specified by the
  13746. user."
  13747. (require 'parse-time)
  13748. (let* ((org-time-stamp-rounding-minutes
  13749. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13750. (org-dcst org-display-custom-times)
  13751. (ct (org-current-time))
  13752. (def (or org-overriding-default-time default-time ct))
  13753. (defdecode (decode-time def))
  13754. (dummy (progn
  13755. (when (< (nth 2 defdecode) org-extend-today-until)
  13756. (setcar (nthcdr 2 defdecode) -1)
  13757. (setcar (nthcdr 1 defdecode) 59)
  13758. (setq def (apply 'encode-time defdecode)
  13759. defdecode (decode-time def)))))
  13760. (calendar-frame-setup nil)
  13761. (calendar-setup nil)
  13762. (calendar-move-hook nil)
  13763. (calendar-view-diary-initially-flag nil)
  13764. (calendar-view-holidays-initially-flag nil)
  13765. (timestr (format-time-string
  13766. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13767. (prompt (concat (if prompt (concat prompt " ") "")
  13768. (format "Date+time [%s]: " timestr)))
  13769. ans (org-ans0 "") org-ans1 org-ans2 final)
  13770. (cond
  13771. (from-string (setq ans from-string))
  13772. (org-read-date-popup-calendar
  13773. (save-excursion
  13774. (save-window-excursion
  13775. (calendar)
  13776. (unwind-protect
  13777. (progn
  13778. (calendar-forward-day (- (time-to-days def)
  13779. (calendar-absolute-from-gregorian
  13780. (calendar-current-date))))
  13781. (org-eval-in-calendar nil t)
  13782. (let* ((old-map (current-local-map))
  13783. (map (copy-keymap calendar-mode-map))
  13784. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13785. (org-defkey map (kbd "RET") 'org-calendar-select)
  13786. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13787. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13788. (org-defkey minibuffer-local-map [(meta shift left)]
  13789. (lambda () (interactive)
  13790. (org-eval-in-calendar '(calendar-backward-month 1))))
  13791. (org-defkey minibuffer-local-map [(meta shift right)]
  13792. (lambda () (interactive)
  13793. (org-eval-in-calendar '(calendar-forward-month 1))))
  13794. (org-defkey minibuffer-local-map [(meta shift up)]
  13795. (lambda () (interactive)
  13796. (org-eval-in-calendar '(calendar-backward-year 1))))
  13797. (org-defkey minibuffer-local-map [(meta shift down)]
  13798. (lambda () (interactive)
  13799. (org-eval-in-calendar '(calendar-forward-year 1))))
  13800. (org-defkey minibuffer-local-map [?\e (shift left)]
  13801. (lambda () (interactive)
  13802. (org-eval-in-calendar '(calendar-backward-month 1))))
  13803. (org-defkey minibuffer-local-map [?\e (shift right)]
  13804. (lambda () (interactive)
  13805. (org-eval-in-calendar '(calendar-forward-month 1))))
  13806. (org-defkey minibuffer-local-map [?\e (shift up)]
  13807. (lambda () (interactive)
  13808. (org-eval-in-calendar '(calendar-backward-year 1))))
  13809. (org-defkey minibuffer-local-map [?\e (shift down)]
  13810. (lambda () (interactive)
  13811. (org-eval-in-calendar '(calendar-forward-year 1))))
  13812. (org-defkey minibuffer-local-map [(shift up)]
  13813. (lambda () (interactive)
  13814. (org-eval-in-calendar '(calendar-backward-week 1))))
  13815. (org-defkey minibuffer-local-map [(shift down)]
  13816. (lambda () (interactive)
  13817. (org-eval-in-calendar '(calendar-forward-week 1))))
  13818. (org-defkey minibuffer-local-map [(shift left)]
  13819. (lambda () (interactive)
  13820. (org-eval-in-calendar '(calendar-backward-day 1))))
  13821. (org-defkey minibuffer-local-map [(shift right)]
  13822. (lambda () (interactive)
  13823. (org-eval-in-calendar '(calendar-forward-day 1))))
  13824. (org-defkey minibuffer-local-map ">"
  13825. (lambda () (interactive)
  13826. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13827. (org-defkey minibuffer-local-map "<"
  13828. (lambda () (interactive)
  13829. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13830. (org-defkey minibuffer-local-map "\C-v"
  13831. (lambda () (interactive)
  13832. (org-eval-in-calendar
  13833. '(calendar-scroll-left-three-months 1))))
  13834. (org-defkey minibuffer-local-map "\M-v"
  13835. (lambda () (interactive)
  13836. (org-eval-in-calendar
  13837. '(calendar-scroll-right-three-months 1))))
  13838. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13839. (unwind-protect
  13840. (progn
  13841. (use-local-map map)
  13842. (setq org-read-date-inactive inactive)
  13843. (add-hook 'post-command-hook 'org-read-date-display)
  13844. (setq org-ans0 (read-string prompt default-input
  13845. 'org-read-date-history nil))
  13846. ;; org-ans0: from prompt
  13847. ;; org-ans1: from mouse click
  13848. ;; org-ans2: from calendar motion
  13849. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13850. (remove-hook 'post-command-hook 'org-read-date-display)
  13851. (use-local-map old-map)
  13852. (when org-read-date-overlay
  13853. (delete-overlay org-read-date-overlay)
  13854. (setq org-read-date-overlay nil)))))
  13855. (bury-buffer "*Calendar*")))))
  13856. (t ; Naked prompt only
  13857. (unwind-protect
  13858. (setq ans (read-string prompt default-input
  13859. 'org-read-date-history timestr))
  13860. (when org-read-date-overlay
  13861. (delete-overlay org-read-date-overlay)
  13862. (setq org-read-date-overlay nil)))))
  13863. (setq final (org-read-date-analyze ans def defdecode))
  13864. (when org-read-date-analyze-forced-year
  13865. (message "Year was forced into %s"
  13866. (if org-read-date-force-compatible-dates
  13867. "compatible range (1970-2037)"
  13868. "range representable on this machine"))
  13869. (ding))
  13870. ;; One round trip to get rid of 34th of August and stuff like that....
  13871. (setq final (decode-time (apply 'encode-time final)))
  13872. (setq org-read-date-final-answer ans)
  13873. (if to-time
  13874. (apply 'encode-time final)
  13875. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13876. (format "%04d-%02d-%02d %02d:%02d"
  13877. (nth 5 final) (nth 4 final) (nth 3 final)
  13878. (nth 2 final) (nth 1 final))
  13879. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13880. (defvar def)
  13881. (defvar defdecode)
  13882. (defvar with-time)
  13883. (defun org-read-date-display ()
  13884. "Display the current date prompt interpretation in the minibuffer."
  13885. (when org-read-date-display-live
  13886. (when org-read-date-overlay
  13887. (delete-overlay org-read-date-overlay))
  13888. (let ((p (point)))
  13889. (end-of-line 1)
  13890. (while (not (equal (buffer-substring
  13891. (max (point-min) (- (point) 4)) (point))
  13892. " "))
  13893. (insert " "))
  13894. (goto-char p))
  13895. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13896. " " (or org-ans1 org-ans2)))
  13897. (org-end-time-was-given nil)
  13898. (f (org-read-date-analyze ans def defdecode))
  13899. (fmts (if org-dcst
  13900. org-time-stamp-custom-formats
  13901. org-time-stamp-formats))
  13902. (fmt (if (or with-time
  13903. (and (boundp 'org-time-was-given) org-time-was-given))
  13904. (cdr fmts)
  13905. (car fmts)))
  13906. (txt (format-time-string fmt (apply 'encode-time f)))
  13907. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13908. (txt (concat "=> " txt)))
  13909. (when (and org-end-time-was-given
  13910. (string-match org-plain-time-of-day-regexp txt))
  13911. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13912. org-end-time-was-given
  13913. (substring txt (match-end 0)))))
  13914. (when org-read-date-analyze-futurep
  13915. (setq txt (concat txt " (=>F)")))
  13916. (setq org-read-date-overlay
  13917. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13918. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13919. (defun org-read-date-analyze (ans def defdecode)
  13920. "Analyze the combined answer of the date prompt."
  13921. ;; FIXME: cleanup and comment
  13922. (let ((nowdecode (decode-time (current-time)))
  13923. delta deltan deltaw deltadef year month day
  13924. hour minute second wday pm h2 m2 tl wday1
  13925. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13926. (setq org-read-date-analyze-futurep nil
  13927. org-read-date-analyze-forced-year nil)
  13928. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13929. (setq ans "+0"))
  13930. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13931. (setq ans (replace-match "" t t ans)
  13932. deltan (car delta)
  13933. deltaw (nth 1 delta)
  13934. deltadef (nth 2 delta)))
  13935. ;; Check if there is an iso week date in there
  13936. ;; If yes, store the info and postpone interpreting it until the rest
  13937. ;; of the parsing is done
  13938. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13939. (setq iso-year (if (match-end 1)
  13940. (org-small-year-to-year
  13941. (string-to-number (match-string 1 ans))))
  13942. iso-weekday (if (match-end 3)
  13943. (string-to-number (match-string 3 ans)))
  13944. iso-week (string-to-number (match-string 2 ans)))
  13945. (setq ans (replace-match "" t t ans)))
  13946. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13947. (when (string-match
  13948. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13949. (setq year (if (match-end 2)
  13950. (string-to-number (match-string 2 ans))
  13951. (progn (setq kill-year t)
  13952. (string-to-number (format-time-string "%Y"))))
  13953. month (string-to-number (match-string 3 ans))
  13954. day (string-to-number (match-string 4 ans)))
  13955. (if (< year 100) (setq year (+ 2000 year)))
  13956. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13957. t nil ans)))
  13958. ;; Help matching dotted european dates
  13959. (when (string-match
  13960. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13961. (setq year (if (match-end 3)
  13962. (string-to-number (match-string 3 ans))
  13963. (progn (setq kill-year t)
  13964. (string-to-number (format-time-string "%Y"))))
  13965. day (string-to-number (match-string 1 ans))
  13966. month (string-to-number (match-string 2 ans))
  13967. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13968. t nil ans)))
  13969. ;; Help matching american dates, like 5/30 or 5/30/7
  13970. (when (string-match
  13971. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13972. (setq year (if (match-end 4)
  13973. (string-to-number (match-string 4 ans))
  13974. (progn (setq kill-year t)
  13975. (string-to-number (format-time-string "%Y"))))
  13976. month (string-to-number (match-string 1 ans))
  13977. day (string-to-number (match-string 2 ans)))
  13978. (if (< year 100) (setq year (+ 2000 year)))
  13979. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13980. t nil ans)))
  13981. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13982. ;; If there is a time with am/pm, and *no* time without it, we convert
  13983. ;; so that matching will be successful.
  13984. (loop for i from 1 to 2 do ; twice, for end time as well
  13985. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13986. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13987. (setq hour (string-to-number (match-string 1 ans))
  13988. minute (if (match-end 3)
  13989. (string-to-number (match-string 3 ans))
  13990. 0)
  13991. pm (equal ?p
  13992. (string-to-char (downcase (match-string 4 ans)))))
  13993. (if (and (= hour 12) (not pm))
  13994. (setq hour 0)
  13995. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13996. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13997. t t ans))))
  13998. ;; Check if a time range is given as a duration
  13999. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14000. (setq hour (string-to-number (match-string 1 ans))
  14001. h2 (+ hour (string-to-number (match-string 3 ans)))
  14002. minute (string-to-number (match-string 2 ans))
  14003. m2 (+ minute (if (match-end 5) (string-to-number
  14004. (match-string 5 ans))0)))
  14005. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14006. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14007. t t ans)))
  14008. ;; Check if there is a time range
  14009. (when (boundp 'org-end-time-was-given)
  14010. (setq org-time-was-given nil)
  14011. (when (and (string-match org-plain-time-of-day-regexp ans)
  14012. (match-end 8))
  14013. (setq org-end-time-was-given (match-string 8 ans))
  14014. (setq ans (concat (substring ans 0 (match-beginning 7))
  14015. (substring ans (match-end 7))))))
  14016. (setq tl (parse-time-string ans)
  14017. day (or (nth 3 tl) (nth 3 defdecode))
  14018. month (or (nth 4 tl)
  14019. (if (and org-read-date-prefer-future
  14020. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14021. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14022. (nth 4 defdecode)))
  14023. year (or (and (not kill-year) (nth 5 tl))
  14024. (if (and org-read-date-prefer-future
  14025. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14026. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14027. (nth 5 defdecode)))
  14028. hour (or (nth 2 tl) (nth 2 defdecode))
  14029. minute (or (nth 1 tl) (nth 1 defdecode))
  14030. second (or (nth 0 tl) 0)
  14031. wday (nth 6 tl))
  14032. (when (and (eq org-read-date-prefer-future 'time)
  14033. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14034. (equal day (nth 3 nowdecode))
  14035. (equal month (nth 4 nowdecode))
  14036. (equal year (nth 5 nowdecode))
  14037. (nth 2 tl)
  14038. (or (< (nth 2 tl) (nth 2 nowdecode))
  14039. (and (= (nth 2 tl) (nth 2 nowdecode))
  14040. (nth 1 tl)
  14041. (< (nth 1 tl) (nth 1 nowdecode)))))
  14042. (setq day (1+ day)
  14043. futurep t))
  14044. ;; Special date definitions below
  14045. (cond
  14046. (iso-week
  14047. ;; There was an iso week
  14048. (require 'cal-iso)
  14049. (setq futurep nil)
  14050. (setq year (or iso-year year)
  14051. day (or iso-weekday wday 1)
  14052. wday nil ; to make sure that the trigger below does not match
  14053. iso-date (calendar-gregorian-from-absolute
  14054. (calendar-absolute-from-iso
  14055. (list iso-week day year))))
  14056. ; FIXME: Should we also push ISO weeks into the future?
  14057. ; (when (and org-read-date-prefer-future
  14058. ; (not iso-year)
  14059. ; (< (calendar-absolute-from-gregorian iso-date)
  14060. ; (time-to-days (current-time))))
  14061. ; (setq year (1+ year)
  14062. ; iso-date (calendar-gregorian-from-absolute
  14063. ; (calendar-absolute-from-iso
  14064. ; (list iso-week day year)))))
  14065. (setq month (car iso-date)
  14066. year (nth 2 iso-date)
  14067. day (nth 1 iso-date)))
  14068. (deltan
  14069. (setq futurep nil)
  14070. (unless deltadef
  14071. (let ((now (decode-time (current-time))))
  14072. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14073. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14074. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14075. ((equal deltaw "m") (setq month (+ month deltan)))
  14076. ((equal deltaw "y") (setq year (+ year deltan)))))
  14077. ((and wday (not (nth 3 tl)))
  14078. (setq futurep nil)
  14079. ;; Weekday was given, but no day, so pick that day in the week
  14080. ;; on or after the derived date.
  14081. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14082. (unless (equal wday wday1)
  14083. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14084. (if (and (boundp 'org-time-was-given)
  14085. (nth 2 tl))
  14086. (setq org-time-was-given t))
  14087. (if (< year 100) (setq year (+ 2000 year)))
  14088. ;; Check of the date is representable
  14089. (if org-read-date-force-compatible-dates
  14090. (progn
  14091. (if (< year 1970)
  14092. (setq year 1970 org-read-date-analyze-forced-year t))
  14093. (if (> year 2037)
  14094. (setq year 2037 org-read-date-analyze-forced-year t)))
  14095. (condition-case nil
  14096. (ignore (encode-time second minute hour day month year))
  14097. (error
  14098. (setq year (nth 5 defdecode))
  14099. (setq org-read-date-analyze-forced-year t))))
  14100. (setq org-read-date-analyze-futurep futurep)
  14101. (list second minute hour day month year)))
  14102. (defvar parse-time-weekdays)
  14103. (defun org-read-date-get-relative (s today default)
  14104. "Check string S for special relative date string.
  14105. TODAY and DEFAULT are internal times, for today and for a default.
  14106. Return shift list (N what def-flag)
  14107. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14108. N is the number of WHATs to shift.
  14109. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14110. the DEFAULT date rather than TODAY."
  14111. (require 'parse-time)
  14112. (when (and
  14113. (string-match
  14114. (concat
  14115. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14116. "\\([0-9]+\\)?"
  14117. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14118. "\\([ \t]\\|$\\)") s)
  14119. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14120. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14121. (string-to-char (substring (match-string 1 s) -1))
  14122. ?+))
  14123. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14124. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14125. (what (if (match-end 3) (match-string 3 s) "d"))
  14126. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14127. (date (if rel default today))
  14128. (wday (nth 6 (decode-time date)))
  14129. delta)
  14130. (if wday1
  14131. (progn
  14132. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14133. (if (= dir ?-) (setq delta (- delta 7)))
  14134. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14135. (list delta "d" rel))
  14136. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14137. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14138. "Turn a user-specified date into the internal representation.
  14139. The internal representation needed by the calendar is (month day year).
  14140. This is a wrapper to handle the brain-dead convention in calendar that
  14141. user function argument order change dependent on argument order."
  14142. (if (boundp 'calendar-date-style)
  14143. (cond
  14144. ((eq calendar-date-style 'american)
  14145. (list arg1 arg2 arg3))
  14146. ((eq calendar-date-style 'european)
  14147. (list arg2 arg1 arg3))
  14148. ((eq calendar-date-style 'iso)
  14149. (list arg2 arg3 arg1)))
  14150. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14151. (if (org-bound-and-true-p european-calendar-style)
  14152. (list arg2 arg1 arg3)
  14153. (list arg1 arg2 arg3)))))
  14154. (defun org-eval-in-calendar (form &optional keepdate)
  14155. "Eval FORM in the calendar window and return to current window.
  14156. Also, store the cursor date in variable org-ans2."
  14157. (let ((sf (selected-frame))
  14158. (sw (selected-window)))
  14159. (select-window (get-buffer-window "*Calendar*" t))
  14160. (eval form)
  14161. (when (and (not keepdate) (calendar-cursor-to-date))
  14162. (let* ((date (calendar-cursor-to-date))
  14163. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14164. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14165. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14166. (select-window sw)
  14167. (org-select-frame-set-input-focus sf)))
  14168. (defun org-calendar-select ()
  14169. "Return to `org-read-date' with the date currently selected.
  14170. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14171. (interactive)
  14172. (when (calendar-cursor-to-date)
  14173. (let* ((date (calendar-cursor-to-date))
  14174. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14175. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14176. (if (active-minibuffer-window) (exit-minibuffer))))
  14177. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14178. "Insert a date stamp for the date given by the internal TIME.
  14179. WITH-HM means use the stamp format that includes the time of the day.
  14180. INACTIVE means use square brackets instead of angular ones, so that the
  14181. stamp will not contribute to the agenda.
  14182. PRE and POST are optional strings to be inserted before and after the
  14183. stamp.
  14184. The command returns the inserted time stamp."
  14185. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14186. stamp)
  14187. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14188. (insert-before-markers (or pre ""))
  14189. (when (listp extra)
  14190. (setq extra (car extra))
  14191. (if (and (stringp extra)
  14192. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14193. (setq extra (format "-%02d:%02d"
  14194. (string-to-number (match-string 1 extra))
  14195. (string-to-number (match-string 2 extra))))
  14196. (setq extra nil)))
  14197. (when extra
  14198. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14199. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14200. (insert-before-markers (or post ""))
  14201. (setq org-last-inserted-timestamp stamp)))
  14202. (defun org-toggle-time-stamp-overlays ()
  14203. "Toggle the use of custom time stamp formats."
  14204. (interactive)
  14205. (setq org-display-custom-times (not org-display-custom-times))
  14206. (unless org-display-custom-times
  14207. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14208. (while (setq p (next-single-property-change p 'display))
  14209. (if (and (get-text-property p 'display)
  14210. (eq (get-text-property p 'face) 'org-date))
  14211. (remove-text-properties
  14212. p (setq p (next-single-property-change p 'display))
  14213. '(display t))))
  14214. (set-buffer-modified-p bmp)))
  14215. (if (featurep 'xemacs)
  14216. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14217. (org-restart-font-lock)
  14218. (setq org-table-may-need-update t)
  14219. (if org-display-custom-times
  14220. (message "Time stamps are overlaid with custom format")
  14221. (message "Time stamp overlays removed")))
  14222. (defun org-display-custom-time (beg end)
  14223. "Overlay modified time stamp format over timestamp between BEG and END."
  14224. (let* ((ts (buffer-substring beg end))
  14225. t1 w1 with-hm tf time str w2 (off 0))
  14226. (save-match-data
  14227. (setq t1 (org-parse-time-string ts t))
  14228. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14229. (setq off (- (match-end 0) (match-beginning 0)))))
  14230. (setq end (- end off))
  14231. (setq w1 (- end beg)
  14232. with-hm (and (nth 1 t1) (nth 2 t1))
  14233. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14234. time (org-fix-decoded-time t1)
  14235. str (org-add-props
  14236. (format-time-string
  14237. (substring tf 1 -1) (apply 'encode-time time))
  14238. nil 'mouse-face 'highlight)
  14239. w2 (length str))
  14240. (if (not (= w2 w1))
  14241. (add-text-properties (1+ beg) (+ 2 beg)
  14242. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14243. (if (featurep 'xemacs)
  14244. (progn
  14245. (put-text-property beg end 'invisible t)
  14246. (put-text-property beg end 'end-glyph (make-glyph str)))
  14247. (put-text-property beg end 'display str))))
  14248. (defun org-translate-time (string)
  14249. "Translate all timestamps in STRING to custom format.
  14250. But do this only if the variable `org-display-custom-times' is set."
  14251. (when org-display-custom-times
  14252. (save-match-data
  14253. (let* ((start 0)
  14254. (re org-ts-regexp-both)
  14255. t1 with-hm inactive tf time str beg end)
  14256. (while (setq start (string-match re string start))
  14257. (setq beg (match-beginning 0)
  14258. end (match-end 0)
  14259. t1 (save-match-data
  14260. (org-parse-time-string (substring string beg end) t))
  14261. with-hm (and (nth 1 t1) (nth 2 t1))
  14262. inactive (equal (substring string beg (1+ beg)) "[")
  14263. tf (funcall (if with-hm 'cdr 'car)
  14264. org-time-stamp-custom-formats)
  14265. time (org-fix-decoded-time t1)
  14266. str (format-time-string
  14267. (concat
  14268. (if inactive "[" "<") (substring tf 1 -1)
  14269. (if inactive "]" ">"))
  14270. (apply 'encode-time time))
  14271. string (replace-match str t t string)
  14272. start (+ start (length str)))))))
  14273. string)
  14274. (defun org-fix-decoded-time (time)
  14275. "Set 0 instead of nil for the first 6 elements of time.
  14276. Don't touch the rest."
  14277. (let ((n 0))
  14278. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14279. (defun org-days-to-time (timestamp-string)
  14280. "Difference between TIMESTAMP-STRING and now in days."
  14281. (- (time-to-days (org-time-string-to-time timestamp-string))
  14282. (time-to-days (current-time))))
  14283. (defun org-deadline-close (timestamp-string &optional ndays)
  14284. "Is the time in TIMESTAMP-STRING close to the current date?"
  14285. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14286. (and (< (org-days-to-time timestamp-string) ndays)
  14287. (not (org-entry-is-done-p))))
  14288. (defun org-get-wdays (ts)
  14289. "Get the deadline lead time appropriate for timestring TS."
  14290. (cond
  14291. ((<= org-deadline-warning-days 0)
  14292. ;; 0 or negative, enforce this value no matter what
  14293. (- org-deadline-warning-days))
  14294. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14295. ;; lead time is specified.
  14296. (floor (* (string-to-number (match-string 1 ts))
  14297. (cdr (assoc (match-string 2 ts)
  14298. '(("d" . 1) ("w" . 7)
  14299. ("m" . 30.4) ("y" . 365.25)))))))
  14300. ;; go for the default.
  14301. (t org-deadline-warning-days)))
  14302. (defun org-calendar-select-mouse (ev)
  14303. "Return to `org-read-date' with the date currently selected.
  14304. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14305. (interactive "e")
  14306. (mouse-set-point ev)
  14307. (when (calendar-cursor-to-date)
  14308. (let* ((date (calendar-cursor-to-date))
  14309. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14310. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14311. (if (active-minibuffer-window) (exit-minibuffer))))
  14312. (defun org-check-deadlines (ndays)
  14313. "Check if there are any deadlines due or past due.
  14314. A deadline is considered due if it happens within `org-deadline-warning-days'
  14315. days from today's date. If the deadline appears in an entry marked DONE,
  14316. it is not shown. The prefix arg NDAYS can be used to test that many
  14317. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14318. (interactive "P")
  14319. (let* ((org-warn-days
  14320. (cond
  14321. ((equal ndays '(4)) 100000)
  14322. (ndays (prefix-numeric-value ndays))
  14323. (t (abs org-deadline-warning-days))))
  14324. (case-fold-search nil)
  14325. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14326. (callback
  14327. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14328. (message "%d deadlines past-due or due within %d days"
  14329. (org-occur regexp nil callback)
  14330. org-warn-days)))
  14331. (defun org-check-before-date (date)
  14332. "Check if there are deadlines or scheduled entries before DATE."
  14333. (interactive (list (org-read-date)))
  14334. (let ((case-fold-search nil)
  14335. (regexp (concat "\\<\\(" org-deadline-string
  14336. "\\|" org-scheduled-string
  14337. "\\) *<\\([^>]+\\)>"))
  14338. (callback
  14339. (lambda () (time-less-p
  14340. (org-time-string-to-time (match-string 2))
  14341. (org-time-string-to-time date)))))
  14342. (message "%d entries before %s"
  14343. (org-occur regexp nil callback) date)))
  14344. (defun org-check-after-date (date)
  14345. "Check if there are deadlines or scheduled entries after DATE."
  14346. (interactive (list (org-read-date)))
  14347. (let ((case-fold-search nil)
  14348. (regexp (concat "\\<\\(" org-deadline-string
  14349. "\\|" org-scheduled-string
  14350. "\\) *<\\([^>]+\\)>"))
  14351. (callback
  14352. (lambda () (not
  14353. (time-less-p
  14354. (org-time-string-to-time (match-string 2))
  14355. (org-time-string-to-time date))))))
  14356. (message "%d entries after %s"
  14357. (org-occur regexp nil callback) date)))
  14358. (defun org-check-dates-range (start-date end-date)
  14359. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14360. (interactive (list (org-read-date nil nil nil "Range starts")
  14361. (org-read-date nil nil nil "Range end")))
  14362. (let ((case-fold-search nil)
  14363. (regexp (concat "\\<\\(" org-deadline-string
  14364. "\\|" org-scheduled-string
  14365. "\\) *<\\([^>]+\\)>"))
  14366. (callback
  14367. (lambda ()
  14368. (let ((match (match-string 2)))
  14369. (and
  14370. (not (time-less-p
  14371. (org-time-string-to-time match)
  14372. (org-time-string-to-time start-date)))
  14373. (time-less-p
  14374. (org-time-string-to-time match)
  14375. (org-time-string-to-time end-date)))))))
  14376. (message "%d entries between %s and %s"
  14377. (org-occur regexp nil callback) start-date end-date)))
  14378. (defun org-evaluate-time-range (&optional to-buffer)
  14379. "Evaluate a time range by computing the difference between start and end.
  14380. Normally the result is just printed in the echo area, but with prefix arg
  14381. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14382. If the time range is actually in a table, the result is inserted into the
  14383. next column.
  14384. For time difference computation, a year is assumed to be exactly 365
  14385. days in order to avoid rounding problems."
  14386. (interactive "P")
  14387. (or
  14388. (org-clock-update-time-maybe)
  14389. (save-excursion
  14390. (unless (org-at-date-range-p t)
  14391. (goto-char (point-at-bol))
  14392. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14393. (if (not (org-at-date-range-p t))
  14394. (error "Not at a time-stamp range, and none found in current line")))
  14395. (let* ((ts1 (match-string 1))
  14396. (ts2 (match-string 2))
  14397. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14398. (match-end (match-end 0))
  14399. (time1 (org-time-string-to-time ts1))
  14400. (time2 (org-time-string-to-time ts2))
  14401. (t1 (org-float-time time1))
  14402. (t2 (org-float-time time2))
  14403. (diff (abs (- t2 t1)))
  14404. (negative (< (- t2 t1) 0))
  14405. ;; (ys (floor (* 365 24 60 60)))
  14406. (ds (* 24 60 60))
  14407. (hs (* 60 60))
  14408. (fy "%dy %dd %02d:%02d")
  14409. (fy1 "%dy %dd")
  14410. (fd "%dd %02d:%02d")
  14411. (fd1 "%dd")
  14412. (fh "%02d:%02d")
  14413. y d h m align)
  14414. (if havetime
  14415. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14416. y 0
  14417. d (floor (/ diff ds)) diff (mod diff ds)
  14418. h (floor (/ diff hs)) diff (mod diff hs)
  14419. m (floor (/ diff 60)))
  14420. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14421. y 0
  14422. d (floor (+ (/ diff ds) 0.5))
  14423. h 0 m 0))
  14424. (if (not to-buffer)
  14425. (message "%s" (org-make-tdiff-string y d h m))
  14426. (if (org-at-table-p)
  14427. (progn
  14428. (goto-char match-end)
  14429. (setq align t)
  14430. (and (looking-at " *|") (goto-char (match-end 0))))
  14431. (goto-char match-end))
  14432. (if (looking-at
  14433. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14434. (replace-match ""))
  14435. (if negative (insert " -"))
  14436. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14437. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14438. (insert " " (format fh h m))))
  14439. (if align (org-table-align))
  14440. (message "Time difference inserted")))))
  14441. (defun org-make-tdiff-string (y d h m)
  14442. (let ((fmt "")
  14443. (l nil))
  14444. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14445. l (push y l)))
  14446. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14447. l (push d l)))
  14448. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14449. l (push h l)))
  14450. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14451. l (push m l)))
  14452. (apply 'format fmt (nreverse l))))
  14453. (defun org-time-string-to-time (s &optional buffer pos)
  14454. (condition-case errdata
  14455. (apply 'encode-time (org-parse-time-string s))
  14456. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14457. s (if (not (and buffer pos))
  14458. ""
  14459. (format " at %d in buffer `%s'" pos buffer))
  14460. (cdr errdata)))))
  14461. (defun org-time-string-to-seconds (s)
  14462. (org-float-time (org-time-string-to-time s)))
  14463. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14464. "Convert a time stamp to an absolute day number.
  14465. If there is a specifier for a cyclic time stamp, get the closest date to
  14466. DAYNR.
  14467. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14468. The variable date is bound by the calendar when this is called."
  14469. (cond
  14470. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14471. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14472. daynr
  14473. (+ daynr 1000)))
  14474. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14475. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14476. (time-to-days (current-time))) (match-string 0 s)
  14477. prefer show-all))
  14478. (t (time-to-days
  14479. (condition-case errdata
  14480. (apply 'encode-time (org-parse-time-string s))
  14481. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14482. s (if (not (and buffer pos))
  14483. ""
  14484. (format " at %d in buffer `%s'" pos buffer))
  14485. (cdr errdata))))))))
  14486. (defun org-days-to-iso-week (days)
  14487. "Return the iso week number."
  14488. (require 'cal-iso)
  14489. (car (calendar-iso-from-absolute days)))
  14490. (defun org-small-year-to-year (year)
  14491. "Convert 2-digit years into 4-digit years.
  14492. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14493. The year 2000 cannot be abbreviated. Any year larger than 99
  14494. is returned unchanged."
  14495. (if (< year 38)
  14496. (setq year (+ 2000 year))
  14497. (if (< year 100)
  14498. (setq year (+ 1900 year))))
  14499. year)
  14500. (defun org-time-from-absolute (d)
  14501. "Return the time corresponding to date D.
  14502. D may be an absolute day number, or a calendar-type list (month day year)."
  14503. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14504. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14505. (defun org-calendar-holiday ()
  14506. "List of holidays, for Diary display in Org-mode."
  14507. (require 'holidays)
  14508. (let ((hl (funcall
  14509. (if (fboundp 'calendar-check-holidays)
  14510. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14511. (if hl (mapconcat 'identity hl "; "))))
  14512. (defun org-diary-sexp-entry (sexp entry date)
  14513. "Process a SEXP diary ENTRY for DATE."
  14514. (require 'diary-lib)
  14515. (let ((result (if calendar-debug-sexp
  14516. (let ((stack-trace-on-error t))
  14517. (eval (car (read-from-string sexp))))
  14518. (condition-case nil
  14519. (eval (car (read-from-string sexp)))
  14520. (error
  14521. (beep)
  14522. (message "Bad sexp at line %d in %s: %s"
  14523. (org-current-line)
  14524. (buffer-file-name) sexp)
  14525. (sleep-for 2))))))
  14526. (cond ((stringp result) (split-string result "; "))
  14527. ((and (consp result)
  14528. (not (consp (cdr result)))
  14529. (stringp (cdr result))) (cdr result))
  14530. ((and (consp result)
  14531. (stringp (car result))) result)
  14532. (result entry)
  14533. (t nil))))
  14534. (defun org-diary-to-ical-string (frombuf)
  14535. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14536. This uses the icalendar.el library."
  14537. (let* ((tmpdir (if (featurep 'xemacs)
  14538. (temp-directory)
  14539. temporary-file-directory))
  14540. (tmpfile (make-temp-name
  14541. (expand-file-name "orgics" tmpdir)))
  14542. buf rtn b e)
  14543. (with-current-buffer frombuf
  14544. (icalendar-export-region (point-min) (point-max) tmpfile)
  14545. (setq buf (find-buffer-visiting tmpfile))
  14546. (set-buffer buf)
  14547. (goto-char (point-min))
  14548. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14549. (setq b (match-beginning 0)))
  14550. (goto-char (point-max))
  14551. (if (re-search-backward "^END:VEVENT" nil t)
  14552. (setq e (match-end 0)))
  14553. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14554. (kill-buffer buf)
  14555. (delete-file tmpfile)
  14556. rtn))
  14557. (defun org-closest-date (start current change prefer show-all)
  14558. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14559. When PREFER is `past', return a date that is either CURRENT or past.
  14560. When PREFER is `future', return a date that is either CURRENT or future.
  14561. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14562. ;; Make the proper lists from the dates
  14563. (catch 'exit
  14564. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14565. dn dw sday cday n1 n2 n0
  14566. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14567. (setq start (org-date-to-gregorian start)
  14568. current (org-date-to-gregorian
  14569. (if show-all
  14570. current
  14571. (time-to-days (current-time))))
  14572. sday (calendar-absolute-from-gregorian start)
  14573. cday (calendar-absolute-from-gregorian current))
  14574. (if (<= cday sday) (throw 'exit sday))
  14575. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14576. (setq dn (string-to-number (match-string 1 change))
  14577. dw (cdr (assoc (match-string 2 change) a1)))
  14578. (error "Invalid change specifier: %s" change))
  14579. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14580. (cond
  14581. ((eq dw 'day)
  14582. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14583. n2 (+ n1 dn)))
  14584. ((eq dw 'year)
  14585. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14586. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14587. (setq date1 (list m d y1)
  14588. n1 (calendar-absolute-from-gregorian date1)
  14589. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14590. n2 (calendar-absolute-from-gregorian date2)))
  14591. ((eq dw 'month)
  14592. ;; approx number of month between the two dates
  14593. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14594. ;; How often does dn fit in there?
  14595. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14596. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14597. m (+ m nm)
  14598. ny (floor (/ m 12))
  14599. y (+ y ny)
  14600. m (- m (* ny 12)))
  14601. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14602. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14603. (setq m2 (+ m dn) y2 y)
  14604. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14605. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14606. (while (<= n2 cday)
  14607. (setq n1 n2 m m2 y y2)
  14608. (setq m2 (+ m dn) y2 y)
  14609. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14610. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14611. ;; Make sure n1 is the earlier date
  14612. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14613. (if show-all
  14614. (cond
  14615. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14616. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14617. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14618. (cond
  14619. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14620. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14621. (t (if (= cday n1) n1 n2)))))))
  14622. (defun org-date-to-gregorian (date)
  14623. "Turn any specification of DATE into a Gregorian date for the calendar."
  14624. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14625. ((and (listp date) (= (length date) 3)) date)
  14626. ((stringp date)
  14627. (setq date (org-parse-time-string date))
  14628. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14629. ((listp date)
  14630. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14631. (defun org-parse-time-string (s &optional nodefault)
  14632. "Parse the standard Org-mode time string.
  14633. This should be a lot faster than the normal `parse-time-string'.
  14634. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14635. hour and minute fields will be nil if not given."
  14636. (if (string-match org-ts-regexp0 s)
  14637. (list 0
  14638. (if (or (match-beginning 8) (not nodefault))
  14639. (string-to-number (or (match-string 8 s) "0")))
  14640. (if (or (match-beginning 7) (not nodefault))
  14641. (string-to-number (or (match-string 7 s) "0")))
  14642. (string-to-number (match-string 4 s))
  14643. (string-to-number (match-string 3 s))
  14644. (string-to-number (match-string 2 s))
  14645. nil nil nil)
  14646. (error "Not a standard Org-mode time string: %s" s)))
  14647. (defun org-timestamp-up (&optional arg)
  14648. "Increase the date item at the cursor by one.
  14649. If the cursor is on the year, change the year. If it is on the month,
  14650. the day or the time, change that.
  14651. With prefix ARG, change by that many units."
  14652. (interactive "p")
  14653. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14654. (defun org-timestamp-down (&optional arg)
  14655. "Decrease the date item at the cursor by one.
  14656. If the cursor is on the year, change the year. If it is on the month,
  14657. the day or the time, change that.
  14658. With prefix ARG, change by that many units."
  14659. (interactive "p")
  14660. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14661. (defun org-timestamp-up-day (&optional arg)
  14662. "Increase the date in the time stamp by one day.
  14663. With prefix ARG, change that many days."
  14664. (interactive "p")
  14665. (if (and (not (org-at-timestamp-p t))
  14666. (org-at-heading-p))
  14667. (org-todo 'up)
  14668. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14669. (defun org-timestamp-down-day (&optional arg)
  14670. "Decrease the date in the time stamp by one day.
  14671. With prefix ARG, change that many days."
  14672. (interactive "p")
  14673. (if (and (not (org-at-timestamp-p t))
  14674. (org-at-heading-p))
  14675. (org-todo 'down)
  14676. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14677. (defun org-at-timestamp-p (&optional inactive-ok)
  14678. "Determine if the cursor is in or at a timestamp."
  14679. (interactive)
  14680. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14681. (pos (point))
  14682. (ans (or (looking-at tsr)
  14683. (save-excursion
  14684. (skip-chars-backward "^[<\n\r\t")
  14685. (if (> (point) (point-min)) (backward-char 1))
  14686. (and (looking-at tsr)
  14687. (> (- (match-end 0) pos) -1))))))
  14688. (and ans
  14689. (boundp 'org-ts-what)
  14690. (setq org-ts-what
  14691. (cond
  14692. ((= pos (match-beginning 0)) 'bracket)
  14693. ;; Point is considered to be "on the bracket" whether
  14694. ;; it's really on it or right after it.
  14695. ((or (= pos (1- (match-end 0)))
  14696. (= pos (match-end 0))) 'bracket)
  14697. ((org-pos-in-match-range pos 2) 'year)
  14698. ((org-pos-in-match-range pos 3) 'month)
  14699. ((org-pos-in-match-range pos 7) 'hour)
  14700. ((org-pos-in-match-range pos 8) 'minute)
  14701. ((or (org-pos-in-match-range pos 4)
  14702. (org-pos-in-match-range pos 5)) 'day)
  14703. ((and (> pos (or (match-end 8) (match-end 5)))
  14704. (< pos (match-end 0)))
  14705. (- pos (or (match-end 8) (match-end 5))))
  14706. (t 'day))))
  14707. ans))
  14708. (defun org-toggle-timestamp-type ()
  14709. "Toggle the type (<active> or [inactive]) of a time stamp."
  14710. (interactive)
  14711. (when (org-at-timestamp-p t)
  14712. (let ((beg (match-beginning 0)) (end (match-end 0))
  14713. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14714. (save-excursion
  14715. (goto-char beg)
  14716. (while (re-search-forward "[][<>]" end t)
  14717. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14718. t t)))
  14719. (message "Timestamp is now %sactive"
  14720. (if (equal (char-after beg) ?<) "" "in")))))
  14721. (defun org-timestamp-change (n &optional what updown)
  14722. "Change the date in the time stamp at point.
  14723. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14724. `year', `minute', `second'. If WHAT is not given, the cursor position
  14725. in the timestamp determines what will be changed."
  14726. (let ((origin (point)) origin-cat
  14727. with-hm inactive
  14728. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14729. org-ts-what
  14730. extra rem
  14731. ts time time0)
  14732. (if (not (org-at-timestamp-p t))
  14733. (error "Not at a timestamp"))
  14734. (if (and (not what) (eq org-ts-what 'bracket))
  14735. (org-toggle-timestamp-type)
  14736. ;; Point isn't on brackets. Remember the part of the time-stamp
  14737. ;; the point was in. Indeed, size of time-stamps may change,
  14738. ;; but point must be kept in the same category nonetheless.
  14739. (setq origin-cat org-ts-what)
  14740. (if (and (not what) (not (eq org-ts-what 'day))
  14741. org-display-custom-times
  14742. (get-text-property (point) 'display)
  14743. (not (get-text-property (1- (point)) 'display)))
  14744. (setq org-ts-what 'day))
  14745. (setq org-ts-what (or what org-ts-what)
  14746. inactive (= (char-after (match-beginning 0)) ?\[)
  14747. ts (match-string 0))
  14748. (replace-match "")
  14749. (if (string-match
  14750. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14751. ts)
  14752. (setq extra (match-string 1 ts)))
  14753. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14754. (setq with-hm t))
  14755. (setq time0 (org-parse-time-string ts))
  14756. (when (and updown
  14757. (eq org-ts-what 'minute)
  14758. (not current-prefix-arg))
  14759. ;; This looks like s-up and s-down. Change by one rounding step.
  14760. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14761. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14762. (setcar (cdr time0) (+ (nth 1 time0)
  14763. (if (> n 0) (- rem) (- dm rem))))))
  14764. (setq time
  14765. (encode-time (or (car time0) 0)
  14766. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14767. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14768. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14769. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14770. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14771. (nthcdr 6 time0)))
  14772. (when (and (member org-ts-what '(hour minute))
  14773. extra
  14774. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14775. (setq extra (org-modify-ts-extra
  14776. extra
  14777. (if (eq org-ts-what 'hour) 2 5)
  14778. n dm)))
  14779. (when (integerp org-ts-what)
  14780. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14781. (if (eq what 'calendar)
  14782. (let ((cal-date (org-get-date-from-calendar)))
  14783. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14784. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14785. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14786. (setcar time0 (or (car time0) 0))
  14787. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14788. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14789. (setq time (apply 'encode-time time0))))
  14790. ;; Insert the new time-stamp, and ensure point stays in the same
  14791. ;; category as before (i.e. not after the last position in that
  14792. ;; category).
  14793. (let ((pos (point)))
  14794. ;; Stay before inserted string. `save-excursion' is of no use.
  14795. (setq org-last-changed-timestamp
  14796. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14797. (goto-char pos))
  14798. (save-match-data
  14799. (looking-at org-ts-regexp3)
  14800. (goto-char (cond
  14801. ;; `day' category ends before `hour' if any, or at
  14802. ;; the end of the day name.
  14803. ((eq origin-cat 'day)
  14804. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14805. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14806. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14807. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14808. ;; `year' and `month' have both fixed size: point
  14809. ;; couldn't have moved into another part.
  14810. (t origin))))
  14811. ;; Update clock if on a CLOCK line.
  14812. (org-clock-update-time-maybe)
  14813. ;; Try to recenter the calendar window, if any.
  14814. (if (and org-calendar-follow-timestamp-change
  14815. (get-buffer-window "*Calendar*" t)
  14816. (memq org-ts-what '(day month year)))
  14817. (org-recenter-calendar (time-to-days time))))))
  14818. (defun org-modify-ts-extra (s pos n dm)
  14819. "Change the different parts of the lead-time and repeat fields in timestamp."
  14820. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14821. ng h m new rem)
  14822. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14823. (cond
  14824. ((or (org-pos-in-match-range pos 2)
  14825. (org-pos-in-match-range pos 3))
  14826. (setq m (string-to-number (match-string 3 s))
  14827. h (string-to-number (match-string 2 s)))
  14828. (if (org-pos-in-match-range pos 2)
  14829. (setq h (+ h n))
  14830. (setq n (* dm (org-no-warnings (signum n))))
  14831. (when (not (= 0 (setq rem (% m dm))))
  14832. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14833. (setq m (+ m n)))
  14834. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14835. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14836. (setq h (min 24 (max 0 h)))
  14837. (setq ng 1 new (format "-%02d:%02d" h m)))
  14838. ((org-pos-in-match-range pos 6)
  14839. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14840. ((org-pos-in-match-range pos 5)
  14841. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14842. ((org-pos-in-match-range pos 9)
  14843. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14844. ((org-pos-in-match-range pos 8)
  14845. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14846. (when ng
  14847. (setq s (concat
  14848. (substring s 0 (match-beginning ng))
  14849. new
  14850. (substring s (match-end ng))))))
  14851. s))
  14852. (defun org-recenter-calendar (date)
  14853. "If the calendar is visible, recenter it to DATE."
  14854. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14855. (when cwin
  14856. (let ((calendar-move-hook nil))
  14857. (with-selected-window cwin
  14858. (calendar-goto-date (if (listp date) date
  14859. (calendar-gregorian-from-absolute date))))))))
  14860. (defun org-goto-calendar (&optional arg)
  14861. "Go to the Emacs calendar at the current date.
  14862. If there is a time stamp in the current line, go to that date.
  14863. A prefix ARG can be used to force the current date."
  14864. (interactive "P")
  14865. (let ((tsr org-ts-regexp) diff
  14866. (calendar-move-hook nil)
  14867. (calendar-view-holidays-initially-flag nil)
  14868. (calendar-view-diary-initially-flag nil))
  14869. (if (or (org-at-timestamp-p)
  14870. (save-excursion
  14871. (beginning-of-line 1)
  14872. (looking-at (concat ".*" tsr))))
  14873. (let ((d1 (time-to-days (current-time)))
  14874. (d2 (time-to-days
  14875. (org-time-string-to-time (match-string 1)))))
  14876. (setq diff (- d2 d1))))
  14877. (calendar)
  14878. (calendar-goto-today)
  14879. (if (and diff (not arg)) (calendar-forward-day diff))))
  14880. (defun org-get-date-from-calendar ()
  14881. "Return a list (month day year) of date at point in calendar."
  14882. (with-current-buffer "*Calendar*"
  14883. (save-match-data
  14884. (calendar-cursor-to-date))))
  14885. (defun org-date-from-calendar ()
  14886. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14887. If there is already a time stamp at the cursor position, update it."
  14888. (interactive)
  14889. (if (org-at-timestamp-p t)
  14890. (org-timestamp-change 0 'calendar)
  14891. (let ((cal-date (org-get-date-from-calendar)))
  14892. (org-insert-time-stamp
  14893. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14894. (defun org-minutes-to-hh:mm-string (m)
  14895. "Compute H:MM from a number of minutes."
  14896. (let ((h (/ m 60)))
  14897. (setq m (- m (* 60 h)))
  14898. (format org-time-clocksum-format h m)))
  14899. (defun org-hh:mm-string-to-minutes (s)
  14900. "Convert a string H:MM to a number of minutes.
  14901. If the string is just a number, interpret it as minutes.
  14902. In fact, the first hh:mm or number in the string will be taken,
  14903. there can be extra stuff in the string.
  14904. If no number is found, the return value is 0."
  14905. (cond
  14906. ((integerp s) s)
  14907. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14908. (+ (* (string-to-number (match-string 1 s)) 60)
  14909. (string-to-number (match-string 2 s))))
  14910. ((string-match "\\([0-9]+\\)" s)
  14911. (string-to-number (match-string 1 s)))
  14912. (t 0)))
  14913. (defcustom org-effort-durations
  14914. `(("h" . 60)
  14915. ("d" . ,(* 60 8))
  14916. ("w" . ,(* 60 8 5))
  14917. ("m" . ,(* 60 8 5 4))
  14918. ("y" . ,(* 60 8 5 40)))
  14919. "Conversion factor to minutes for an effort modifier.
  14920. Each entry has the form (MODIFIER . MINUTES).
  14921. In an effort string, a number followed by MODIFIER is multiplied
  14922. by the specified number of MINUTES to obtain an effort in
  14923. minutes.
  14924. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14925. effort string \"2hours\" is equivalent to 120 minutes."
  14926. :group 'org-agenda
  14927. :version "24.1"
  14928. :type '(alist :key-type (string :tag "Modifier")
  14929. :value-type (number :tag "Minutes")))
  14930. (defun org-duration-string-to-minutes (s)
  14931. "Convert a duration string S to minutes.
  14932. A bare number is interpreted as minutes, modifiers can be set by
  14933. customizing `org-effort-durations' (which see).
  14934. Entries containing a colon are interpreted as H:MM by
  14935. `org-hh:mm-string-to-minutes'."
  14936. (let ((result 0)
  14937. (re (concat "\\([0-9]+\\) *\\("
  14938. (regexp-opt (mapcar 'car org-effort-durations))
  14939. "\\)")))
  14940. (while (string-match re s)
  14941. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14942. (string-to-number (match-string 1 s))))
  14943. (setq s (replace-match "" nil t s)))
  14944. (incf result (org-hh:mm-string-to-minutes s))
  14945. result))
  14946. ;;;; Files
  14947. (defun org-save-all-org-buffers ()
  14948. "Save all Org-mode buffers without user confirmation."
  14949. (interactive)
  14950. (message "Saving all Org-mode buffers...")
  14951. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14952. (when (featurep 'org-id) (org-id-locations-save))
  14953. (message "Saving all Org-mode buffers... done"))
  14954. (defun org-revert-all-org-buffers ()
  14955. "Revert all Org-mode buffers.
  14956. Prompt for confirmation when there are unsaved changes.
  14957. Be sure you know what you are doing before letting this function
  14958. overwrite your changes.
  14959. This function is useful in a setup where one tracks org files
  14960. with a version control system, to revert on one machine after pulling
  14961. changes from another. I believe the procedure must be like this:
  14962. 1. M-x org-save-all-org-buffers
  14963. 2. Pull changes from the other machine, resolve conflicts
  14964. 3. M-x org-revert-all-org-buffers"
  14965. (interactive)
  14966. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14967. (error "Abort"))
  14968. (save-excursion
  14969. (save-window-excursion
  14970. (mapc
  14971. (lambda (b)
  14972. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14973. (with-current-buffer b buffer-file-name))
  14974. (org-pop-to-buffer-same-window b)
  14975. (revert-buffer t 'no-confirm)))
  14976. (buffer-list))
  14977. (when (and (featurep 'org-id) org-id-track-globally)
  14978. (org-id-locations-load)))))
  14979. ;;;; Agenda files
  14980. ;;;###autoload
  14981. (defun org-switchb (&optional arg)
  14982. "Switch between Org buffers.
  14983. With one prefix argument, restrict available buffers to files.
  14984. With two prefix arguments, restrict available buffers to agenda files.
  14985. Defaults to `iswitchb' for buffer name completion.
  14986. Set `org-completion-use-ido' to make it use ido instead."
  14987. (interactive "P")
  14988. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14989. ((equal arg '(16)) (org-buffer-list 'agenda))
  14990. (t (org-buffer-list))))
  14991. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14992. (org-completion-use-ido org-completion-use-ido))
  14993. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14994. (setq org-completion-use-iswitchb t))
  14995. (org-pop-to-buffer-same-window
  14996. (org-icompleting-read "Org buffer: "
  14997. (mapcar 'list (mapcar 'buffer-name blist))
  14998. nil t))))
  14999. ;;; Define some older names previously used for this functionality
  15000. ;;;###autoload
  15001. (defalias 'org-ido-switchb 'org-switchb)
  15002. ;;;###autoload
  15003. (defalias 'org-iswitchb 'org-switchb)
  15004. (defun org-buffer-list (&optional predicate exclude-tmp)
  15005. "Return a list of Org buffers.
  15006. PREDICATE can be `export', `files' or `agenda'.
  15007. export restrict the list to Export buffers.
  15008. files restrict the list to buffers visiting Org files.
  15009. agenda restrict the list to buffers visiting agenda files.
  15010. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15011. (let* ((bfn nil)
  15012. (agenda-files (and (eq predicate 'agenda)
  15013. (mapcar 'file-truename (org-agenda-files t))))
  15014. (filter
  15015. (cond
  15016. ((eq predicate 'files)
  15017. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  15018. ((eq predicate 'export)
  15019. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15020. ((eq predicate 'agenda)
  15021. (lambda (b)
  15022. (with-current-buffer b
  15023. (and (eq major-mode 'org-mode)
  15024. (setq bfn (buffer-file-name b))
  15025. (member (file-truename bfn) agenda-files)))))
  15026. (t (lambda (b) (with-current-buffer b
  15027. (or (eq major-mode 'org-mode)
  15028. (string-match "\*Org .*Export"
  15029. (buffer-name b)))))))))
  15030. (delq nil
  15031. (mapcar
  15032. (lambda(b)
  15033. (if (and (funcall filter b)
  15034. (or (not exclude-tmp)
  15035. (not (string-match "tmp" (buffer-name b)))))
  15036. b
  15037. nil))
  15038. (buffer-list)))))
  15039. (defun org-agenda-files (&optional unrestricted archives)
  15040. "Get the list of agenda files.
  15041. Optional UNRESTRICTED means return the full list even if a restriction
  15042. is currently in place.
  15043. When ARCHIVES is t, include all archive files that are really being
  15044. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15045. `org-agenda-archives-mode' is t."
  15046. (let ((files
  15047. (cond
  15048. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15049. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15050. ((listp org-agenda-files) org-agenda-files)
  15051. (t (error "Invalid value of `org-agenda-files'")))))
  15052. (setq files (apply 'append
  15053. (mapcar (lambda (f)
  15054. (if (file-directory-p f)
  15055. (directory-files
  15056. f t org-agenda-file-regexp)
  15057. (list f)))
  15058. files)))
  15059. (when org-agenda-skip-unavailable-files
  15060. (setq files (delq nil
  15061. (mapcar (function
  15062. (lambda (file)
  15063. (and (file-readable-p file) file)))
  15064. files))))
  15065. (when (or (eq archives t)
  15066. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15067. (setq files (org-add-archive-files files)))
  15068. files))
  15069. (defun org-agenda-file-p (&optional file)
  15070. "Return non-nil, if FILE is an agenda file.
  15071. If FILE is omitted, use the file associated with the current
  15072. buffer."
  15073. (member (or file (buffer-file-name))
  15074. (org-agenda-files t)))
  15075. (defun org-edit-agenda-file-list ()
  15076. "Edit the list of agenda files.
  15077. Depending on setup, this either uses customize to edit the variable
  15078. `org-agenda-files', or it visits the file that is holding the list. In the
  15079. latter case, the buffer is set up in a way that saving it automatically kills
  15080. the buffer and restores the previous window configuration."
  15081. (interactive)
  15082. (if (stringp org-agenda-files)
  15083. (let ((cw (current-window-configuration)))
  15084. (find-file org-agenda-files)
  15085. (org-set-local 'org-window-configuration cw)
  15086. (org-add-hook 'after-save-hook
  15087. (lambda ()
  15088. (set-window-configuration
  15089. (prog1 org-window-configuration
  15090. (kill-buffer (current-buffer))))
  15091. (org-install-agenda-files-menu)
  15092. (message "New agenda file list installed"))
  15093. nil 'local)
  15094. (message "%s" (substitute-command-keys
  15095. "Edit list and finish with \\[save-buffer]")))
  15096. (customize-variable 'org-agenda-files)))
  15097. (defun org-store-new-agenda-file-list (list)
  15098. "Set new value for the agenda file list and save it correctly."
  15099. (if (stringp org-agenda-files)
  15100. (let ((fe (org-read-agenda-file-list t)) b u)
  15101. (while (setq b (find-buffer-visiting org-agenda-files))
  15102. (kill-buffer b))
  15103. (with-temp-file org-agenda-files
  15104. (insert
  15105. (mapconcat
  15106. (lambda (f) ;; Keep un-expanded entries.
  15107. (if (setq u (assoc f fe))
  15108. (cdr u)
  15109. f))
  15110. list "\n")
  15111. "\n")))
  15112. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15113. (org-insert-mode-line-in-empty-file nil))
  15114. (setq org-agenda-files list)
  15115. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15116. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15117. "Read the list of agenda files from a file.
  15118. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15119. filenames, used by `org-store-new-agenda-file-list' to write back
  15120. un-expanded file names."
  15121. (when (file-directory-p org-agenda-files)
  15122. (error "`org-agenda-files' cannot be a single directory"))
  15123. (when (stringp org-agenda-files)
  15124. (with-temp-buffer
  15125. (insert-file-contents org-agenda-files)
  15126. (mapcar
  15127. (lambda (f)
  15128. (let ((e (expand-file-name (substitute-in-file-name f)
  15129. org-directory)))
  15130. (if pair-with-expansion
  15131. (cons e f)
  15132. e)))
  15133. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15134. ;;;###autoload
  15135. (defun org-cycle-agenda-files ()
  15136. "Cycle through the files in `org-agenda-files'.
  15137. If the current buffer visits an agenda file, find the next one in the list.
  15138. If the current buffer does not, find the first agenda file."
  15139. (interactive)
  15140. (let* ((fs (org-agenda-files t))
  15141. (files (append fs (list (car fs))))
  15142. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15143. file)
  15144. (unless files (error "No agenda files"))
  15145. (catch 'exit
  15146. (while (setq file (pop files))
  15147. (if (equal (file-truename file) tcf)
  15148. (when (car files)
  15149. (find-file (car files))
  15150. (throw 'exit t))))
  15151. (find-file (car fs)))
  15152. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15153. (defun org-agenda-file-to-front (&optional to-end)
  15154. "Move/add the current file to the top of the agenda file list.
  15155. If the file is not present in the list, it is added to the front. If it is
  15156. present, it is moved there. With optional argument TO-END, add/move to the
  15157. end of the list."
  15158. (interactive "P")
  15159. (let ((org-agenda-skip-unavailable-files nil)
  15160. (file-alist (mapcar (lambda (x)
  15161. (cons (file-truename x) x))
  15162. (org-agenda-files t)))
  15163. (ctf (file-truename buffer-file-name))
  15164. x had)
  15165. (setq x (assoc ctf file-alist) had x)
  15166. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15167. (if to-end
  15168. (setq file-alist (append (delq x file-alist) (list x)))
  15169. (setq file-alist (cons x (delq x file-alist))))
  15170. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15171. (org-install-agenda-files-menu)
  15172. (message "File %s to %s of agenda file list"
  15173. (if had "moved" "added") (if to-end "end" "front"))))
  15174. (defun org-remove-file (&optional file)
  15175. "Remove current file from the list of files in variable `org-agenda-files'.
  15176. These are the files which are being checked for agenda entries.
  15177. Optional argument FILE means use this file instead of the current."
  15178. (interactive)
  15179. (let* ((org-agenda-skip-unavailable-files nil)
  15180. (file (or file buffer-file-name))
  15181. (true-file (file-truename file))
  15182. (afile (abbreviate-file-name file))
  15183. (files (delq nil (mapcar
  15184. (lambda (x)
  15185. (if (equal true-file
  15186. (file-truename x))
  15187. nil x))
  15188. (org-agenda-files t)))))
  15189. (if (not (= (length files) (length (org-agenda-files t))))
  15190. (progn
  15191. (org-store-new-agenda-file-list files)
  15192. (org-install-agenda-files-menu)
  15193. (message "Removed file: %s" afile))
  15194. (message "File was not in list: %s (not removed)" afile))))
  15195. (defun org-file-menu-entry (file)
  15196. (vector file (list 'find-file file) t))
  15197. (defun org-check-agenda-file (file)
  15198. "Make sure FILE exists. If not, ask user what to do."
  15199. (when (not (file-exists-p file))
  15200. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15201. (abbreviate-file-name file))
  15202. (let ((r (downcase (read-char-exclusive))))
  15203. (cond
  15204. ((equal r ?r)
  15205. (org-remove-file file)
  15206. (throw 'nextfile t))
  15207. (t (error "Abort"))))))
  15208. (defun org-get-agenda-file-buffer (file)
  15209. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15210. it to the list of buffers which might be released later."
  15211. (let ((buf (org-find-base-buffer-visiting file)))
  15212. (if buf
  15213. buf ; just return it
  15214. ;; Make a new buffer and remember it
  15215. (setq buf (find-file-noselect file))
  15216. (if buf (push buf org-agenda-new-buffers))
  15217. buf)))
  15218. (defun org-release-buffers (blist)
  15219. "Release all buffers in list, asking the user for confirmation when needed.
  15220. When a buffer is unmodified, it is just killed. When modified, it is saved
  15221. \(if the user agrees) and then killed."
  15222. (let (buf file)
  15223. (while (setq buf (pop blist))
  15224. (setq file (buffer-file-name buf))
  15225. (when (and (buffer-modified-p buf)
  15226. file
  15227. (y-or-n-p (format "Save file %s? " file)))
  15228. (with-current-buffer buf (save-buffer)))
  15229. (kill-buffer buf))))
  15230. (defun org-prepare-agenda-buffers (files)
  15231. "Create buffers for all agenda files, protect archived trees and comments."
  15232. (interactive)
  15233. (let ((pa '(:org-archived t))
  15234. (pc '(:org-comment t))
  15235. (pall '(:org-archived t :org-comment t))
  15236. (inhibit-read-only t)
  15237. (rea (concat ":" org-archive-tag ":"))
  15238. bmp file re)
  15239. (save-excursion
  15240. (save-restriction
  15241. (while (setq file (pop files))
  15242. (catch 'nextfile
  15243. (if (bufferp file)
  15244. (set-buffer file)
  15245. (org-check-agenda-file file)
  15246. (set-buffer (org-get-agenda-file-buffer file)))
  15247. (widen)
  15248. (setq bmp (buffer-modified-p))
  15249. (org-refresh-category-properties)
  15250. (setq org-todo-keywords-for-agenda
  15251. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15252. (setq org-done-keywords-for-agenda
  15253. (append org-done-keywords-for-agenda org-done-keywords))
  15254. (setq org-todo-keyword-alist-for-agenda
  15255. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15256. (setq org-drawers-for-agenda
  15257. (append org-drawers-for-agenda org-drawers))
  15258. (setq org-tag-alist-for-agenda
  15259. (append org-tag-alist-for-agenda org-tag-alist))
  15260. (save-excursion
  15261. (remove-text-properties (point-min) (point-max) pall)
  15262. (when org-agenda-skip-archived-trees
  15263. (goto-char (point-min))
  15264. (while (re-search-forward rea nil t)
  15265. (if (org-at-heading-p t)
  15266. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15267. (goto-char (point-min))
  15268. (setq re (format org-heading-keyword-regexp-format
  15269. org-comment-string))
  15270. (while (re-search-forward re nil t)
  15271. (add-text-properties
  15272. (match-beginning 0) (org-end-of-subtree t) pc)))
  15273. (set-buffer-modified-p bmp)))))
  15274. (setq org-todo-keywords-for-agenda
  15275. (org-uniquify org-todo-keywords-for-agenda))
  15276. (setq org-todo-keyword-alist-for-agenda
  15277. (org-uniquify org-todo-keyword-alist-for-agenda)
  15278. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15279. ;;;; Embedded LaTeX
  15280. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15281. "Keymap for the minor `org-cdlatex-mode'.")
  15282. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15283. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15284. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15285. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15286. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15287. (defvar org-cdlatex-texmathp-advice-is-done nil
  15288. "Flag remembering if we have applied the advice to texmathp already.")
  15289. (define-minor-mode org-cdlatex-mode
  15290. "Toggle the minor `org-cdlatex-mode'.
  15291. This mode supports entering LaTeX environment and math in LaTeX fragments
  15292. in Org-mode.
  15293. \\{org-cdlatex-mode-map}"
  15294. nil " OCDL" nil
  15295. (when org-cdlatex-mode
  15296. (require 'cdlatex)
  15297. (run-hooks 'cdlatex-mode-hook)
  15298. (cdlatex-compute-tables))
  15299. (unless org-cdlatex-texmathp-advice-is-done
  15300. (setq org-cdlatex-texmathp-advice-is-done t)
  15301. (defadvice texmathp (around org-math-always-on activate)
  15302. "Always return t in org-mode buffers.
  15303. This is because we want to insert math symbols without dollars even outside
  15304. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15305. an embedded LaTeX fragment, let texmathp do its job.
  15306. \\[org-cdlatex-mode-map]"
  15307. (interactive)
  15308. (let (p)
  15309. (cond
  15310. ((not (eq major-mode 'org-mode)) ad-do-it)
  15311. ((eq this-command 'cdlatex-math-symbol)
  15312. (setq ad-return-value t
  15313. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15314. (t
  15315. (let ((p (org-inside-LaTeX-fragment-p)))
  15316. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15317. (setq ad-return-value t
  15318. texmathp-why '("Org-mode embedded math" . 0))
  15319. (if p ad-do-it)))))))))
  15320. (defun turn-on-org-cdlatex ()
  15321. "Unconditionally turn on `org-cdlatex-mode'."
  15322. (org-cdlatex-mode 1))
  15323. (defun org-inside-LaTeX-fragment-p ()
  15324. "Test if point is inside a LaTeX fragment.
  15325. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15326. sequence appearing also before point.
  15327. Even though the matchers for math are configurable, this function assumes
  15328. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15329. delimiters are skipped when they have been removed by customization.
  15330. The return value is nil, or a cons cell with the delimiter and the
  15331. position of this delimiter.
  15332. This function does a reasonably good job, but can locally be fooled by
  15333. for example currency specifications. For example it will assume being in
  15334. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15335. fragments that are properly closed, but during editing, we have to live
  15336. with the uncertainty caused by missing closing delimiters. This function
  15337. looks only before point, not after."
  15338. (catch 'exit
  15339. (let ((pos (point))
  15340. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15341. (lim (progn
  15342. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15343. (point)))
  15344. dd-on str (start 0) m re)
  15345. (goto-char pos)
  15346. (when dodollar
  15347. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15348. re (nth 1 (assoc "$" org-latex-regexps)))
  15349. (while (string-match re str start)
  15350. (cond
  15351. ((= (match-end 0) (length str))
  15352. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15353. ((= (match-end 0) (- (length str) 5))
  15354. (throw 'exit nil))
  15355. (t (setq start (match-end 0))))))
  15356. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15357. (goto-char pos)
  15358. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15359. (and (match-beginning 2) (throw 'exit nil))
  15360. ;; count $$
  15361. (while (re-search-backward "\\$\\$" lim t)
  15362. (setq dd-on (not dd-on)))
  15363. (goto-char pos)
  15364. (if dd-on (cons "$$" m))))))
  15365. (defun org-inside-latex-macro-p ()
  15366. "Is point inside a LaTeX macro or its arguments?"
  15367. (save-match-data
  15368. (org-in-regexp
  15369. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15370. (defun org-try-cdlatex-tab ()
  15371. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15372. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15373. - inside a LaTeX fragment, or
  15374. - after the first word in a line, where an abbreviation expansion could
  15375. insert a LaTeX environment."
  15376. (when org-cdlatex-mode
  15377. (cond
  15378. ;; Before any word on the line: No expansion possible.
  15379. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15380. ;; Just after first word on the line: Expand it. Make sure it
  15381. ;; cannot happen on headlines, though.
  15382. ((save-excursion
  15383. (skip-chars-backward "a-zA-Z0-9*")
  15384. (skip-chars-backward " \t")
  15385. (and (bolp) (not (org-at-heading-p))))
  15386. (cdlatex-tab) t)
  15387. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15388. (defun org-cdlatex-underscore-caret (&optional arg)
  15389. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15390. Revert to the normal definition outside of these fragments."
  15391. (interactive "P")
  15392. (if (org-inside-LaTeX-fragment-p)
  15393. (call-interactively 'cdlatex-sub-superscript)
  15394. (let (org-cdlatex-mode)
  15395. (call-interactively (key-binding (vector last-input-event))))))
  15396. (defun org-cdlatex-math-modify (&optional arg)
  15397. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15398. Revert to the normal definition outside of these fragments."
  15399. (interactive "P")
  15400. (if (org-inside-LaTeX-fragment-p)
  15401. (call-interactively 'cdlatex-math-modify)
  15402. (let (org-cdlatex-mode)
  15403. (call-interactively (key-binding (vector last-input-event))))))
  15404. (defvar org-latex-fragment-image-overlays nil
  15405. "List of overlays carrying the images of latex fragments.")
  15406. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15407. (defun org-remove-latex-fragment-image-overlays ()
  15408. "Remove all overlays with LaTeX fragment images in current buffer."
  15409. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15410. (setq org-latex-fragment-image-overlays nil))
  15411. (defun org-preview-latex-fragment (&optional subtree)
  15412. "Preview the LaTeX fragment at point, or all locally or globally.
  15413. If the cursor is in a LaTeX fragment, create the image and overlay
  15414. it over the source code. If there is no fragment at point, display
  15415. all fragments in the current text, from one headline to the next. With
  15416. prefix SUBTREE, display all fragments in the current subtree. With a
  15417. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15418. the cursor is before the first headline,
  15419. display all fragments in the buffer.
  15420. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15421. (interactive "P")
  15422. (unless buffer-file-name
  15423. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15424. (org-remove-latex-fragment-image-overlays)
  15425. (save-excursion
  15426. (save-restriction
  15427. (let (beg end at msg)
  15428. (cond
  15429. ((or (equal subtree '(16))
  15430. (not (save-excursion
  15431. (re-search-backward org-outline-regexp-bol nil t))))
  15432. (setq beg (point-min) end (point-max)
  15433. msg "Creating images for buffer...%s"))
  15434. ((equal subtree '(4))
  15435. (org-back-to-heading)
  15436. (setq beg (point) end (org-end-of-subtree t)
  15437. msg "Creating images for subtree...%s"))
  15438. (t
  15439. (if (setq at (org-inside-LaTeX-fragment-p))
  15440. (goto-char (max (point-min) (- (cdr at) 2)))
  15441. (org-back-to-heading))
  15442. (setq beg (point) end (progn (outline-next-heading) (point))
  15443. msg (if at "Creating image...%s"
  15444. "Creating images for entry...%s"))))
  15445. (message msg "")
  15446. (narrow-to-region beg end)
  15447. (goto-char beg)
  15448. (org-format-latex
  15449. (concat "ltxpng/" (file-name-sans-extension
  15450. (file-name-nondirectory
  15451. buffer-file-name)))
  15452. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15453. (message msg "done. Use `C-c C-c' to remove images.")))))
  15454. (defvar org-latex-regexps
  15455. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15456. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15457. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15458. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15459. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15460. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15461. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15462. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15463. "Regular expressions for matching embedded LaTeX.")
  15464. (defvar org-export-have-math nil) ;; dynamic scoping
  15465. (defun org-format-latex (prefix &optional dir overlays msg at
  15466. forbuffer processing-type)
  15467. "Replace LaTeX fragments with links to an image, and produce images.
  15468. Some of the options can be changed using the variable
  15469. `org-format-latex-options'."
  15470. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15471. (let* ((prefixnodir (file-name-nondirectory prefix))
  15472. (absprefix (expand-file-name prefix dir))
  15473. (todir (file-name-directory absprefix))
  15474. (opt org-format-latex-options)
  15475. (matchers (plist-get opt :matchers))
  15476. (re-list org-latex-regexps)
  15477. (org-format-latex-header-extra
  15478. (plist-get (org-infile-export-plist) :latex-header-extra))
  15479. (cnt 0) txt hash link beg end re e checkdir
  15480. executables-checked string
  15481. m n block-type block linkfile movefile ov)
  15482. ;; Check the different regular expressions
  15483. (while (setq e (pop re-list))
  15484. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15485. block (if block-type "\n\n" ""))
  15486. (when (member m matchers)
  15487. (goto-char (point-min))
  15488. (while (re-search-forward re nil t)
  15489. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15490. (not (get-text-property (match-beginning n)
  15491. 'org-protected))
  15492. (or (not overlays)
  15493. (not (eq (get-char-property (match-beginning n)
  15494. 'org-overlay-type)
  15495. 'org-latex-overlay))))
  15496. (setq org-export-have-math t)
  15497. (cond
  15498. ((eq processing-type 'verbatim)
  15499. ;; Leave the text verbatim, just protect it
  15500. (add-text-properties (match-beginning n) (match-end n)
  15501. '(org-protected t)))
  15502. ((eq processing-type 'mathjax)
  15503. ;; Prepare for MathJax processing
  15504. (setq string (match-string n))
  15505. (if (member m '("$" "$1"))
  15506. (save-excursion
  15507. (delete-region (match-beginning n) (match-end n))
  15508. (goto-char (match-beginning n))
  15509. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15510. "\\)")
  15511. '(org-protected t))))
  15512. (add-text-properties (match-beginning n) (match-end n)
  15513. '(org-protected t))))
  15514. ((eq processing-type 'dvipng)
  15515. ;; Process to an image
  15516. (setq txt (match-string n)
  15517. beg (match-beginning n) end (match-end n)
  15518. cnt (1+ cnt))
  15519. (let (print-length print-level) ; make sure full list is printed
  15520. (setq hash (sha1 (prin1-to-string
  15521. (list org-format-latex-header
  15522. org-format-latex-header-extra
  15523. org-export-latex-default-packages-alist
  15524. org-export-latex-packages-alist
  15525. org-format-latex-options
  15526. forbuffer txt)))
  15527. linkfile (format "%s_%s.png" prefix hash)
  15528. movefile (format "%s_%s.png" absprefix hash)))
  15529. (setq link (concat block "[[file:" linkfile "]]" block))
  15530. (if msg (message msg cnt))
  15531. (goto-char beg)
  15532. (unless checkdir ; make sure the directory exists
  15533. (setq checkdir t)
  15534. (or (file-directory-p todir) (make-directory todir t)))
  15535. (unless executables-checked
  15536. (org-check-external-command
  15537. "latex" "needed to convert LaTeX fragments to images")
  15538. (org-check-external-command
  15539. "dvipng" "needed to convert LaTeX fragments to images")
  15540. (setq executables-checked t))
  15541. (unless (file-exists-p movefile)
  15542. (org-create-formula-image
  15543. txt movefile opt forbuffer))
  15544. (if overlays
  15545. (progn
  15546. (mapc (lambda (o)
  15547. (if (eq (overlay-get o 'org-overlay-type)
  15548. 'org-latex-overlay)
  15549. (delete-overlay o)))
  15550. (overlays-in beg end))
  15551. (setq ov (make-overlay beg end))
  15552. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15553. (if (featurep 'xemacs)
  15554. (progn
  15555. (overlay-put ov 'invisible t)
  15556. (overlay-put
  15557. ov 'end-glyph
  15558. (make-glyph (vector 'png :file movefile))))
  15559. (overlay-put
  15560. ov 'display
  15561. (list 'image :type 'png :file movefile :ascent 'center)))
  15562. (push ov org-latex-fragment-image-overlays)
  15563. (goto-char end))
  15564. (delete-region beg end)
  15565. (insert (org-add-props link
  15566. (list 'org-latex-src
  15567. (replace-regexp-in-string
  15568. "\"" "" txt)
  15569. 'org-latex-src-embed-type
  15570. (if block-type 'paragraph 'character))))))
  15571. ((eq processing-type 'mathml)
  15572. ;; Process to MathML
  15573. (unless executables-checked
  15574. (unless (save-match-data (org-format-latex-mathml-available-p))
  15575. (error "LaTeX to MathML converter not configured"))
  15576. (setq executables-checked t))
  15577. (setq txt (match-string n)
  15578. beg (match-beginning n) end (match-end n)
  15579. cnt (1+ cnt))
  15580. (if msg (message msg cnt))
  15581. (goto-char beg)
  15582. (delete-region beg end)
  15583. (insert (org-format-latex-as-mathml
  15584. txt block-type prefix dir)))
  15585. (t
  15586. (error "Unknown conversion type %s for latex fragments"
  15587. processing-type)))))))))
  15588. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15589. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15590. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15591. sucessful, return the portion between \"<math...> </math>\"
  15592. elements otherwise return nil. When MATHML-FILE is specified,
  15593. write the results in to that file. When invoked as an
  15594. interactive command, prompt for LATEX-FRAG, with initial value
  15595. set to the current active region and echo the results for user
  15596. inspection."
  15597. (interactive (list (let ((frag (when (region-active-p)
  15598. (buffer-substring-no-properties
  15599. (region-beginning) (region-end)))))
  15600. (read-string "LaTeX Fragment: " frag nil frag))))
  15601. (unless latex-frag (error "Invalid latex-frag"))
  15602. (let* ((tmp-in-file (file-relative-name
  15603. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15604. (ignore (write-region latex-frag nil tmp-in-file))
  15605. (tmp-out-file (file-relative-name
  15606. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15607. (cmd (format-spec
  15608. org-latex-to-mathml-convert-command
  15609. `((?j . ,(shell-quote-argument
  15610. (expand-file-name org-latex-to-mathml-jar-file)))
  15611. (?I . ,(shell-quote-argument tmp-in-file))
  15612. (?o . ,(shell-quote-argument tmp-out-file)))))
  15613. mathml shell-command-output)
  15614. (when (org-called-interactively-p 'any)
  15615. (unless (org-format-latex-mathml-available-p)
  15616. (error "LaTeX to MathML converter not configured")))
  15617. (message "Running %s" cmd)
  15618. (setq shell-command-output (shell-command-to-string cmd))
  15619. (setq mathml
  15620. (when (file-readable-p tmp-out-file)
  15621. (with-current-buffer (find-file-noselect tmp-out-file t)
  15622. (goto-char (point-min))
  15623. (when (re-search-forward
  15624. (concat
  15625. (regexp-quote
  15626. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15627. "\\(.\\|\n\\)*"
  15628. (regexp-quote "</math>")) nil t)
  15629. (prog1 (match-string 0) (kill-buffer))))))
  15630. (cond
  15631. (mathml
  15632. (setq mathml
  15633. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15634. (when mathml-file
  15635. (write-region mathml nil mathml-file))
  15636. (when (org-called-interactively-p 'any)
  15637. (message mathml)))
  15638. ((message "LaTeX to MathML conversion failed")
  15639. (message shell-command-output)))
  15640. (delete-file tmp-in-file)
  15641. (when (file-exists-p tmp-out-file)
  15642. (delete-file tmp-out-file))
  15643. mathml))
  15644. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15645. prefix &optional dir)
  15646. "Use `org-create-math-formula' but check local cache first."
  15647. (let* ((absprefix (expand-file-name prefix dir))
  15648. (print-length nil) (print-level nil)
  15649. (formula-id (concat
  15650. "formula-"
  15651. (sha1
  15652. (prin1-to-string
  15653. (list latex-frag
  15654. org-latex-to-mathml-convert-command)))))
  15655. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15656. (formula-cache-dir (file-name-directory formula-cache)))
  15657. (unless (file-directory-p formula-cache-dir)
  15658. (make-directory formula-cache-dir t))
  15659. (unless (file-exists-p formula-cache)
  15660. (org-create-math-formula latex-frag formula-cache))
  15661. (if (file-exists-p formula-cache)
  15662. ;; Successful conversion. Return the link to MathML file.
  15663. (org-add-props
  15664. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15665. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15666. 'org-latex-src-embed-type (if latex-frag-type
  15667. 'paragraph 'character)))
  15668. ;; Failed conversion. Return the LaTeX fragment verbatim
  15669. (add-text-properties
  15670. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15671. latex-frag)))
  15672. ;; This function borrows from Ganesh Swami's latex2png.el
  15673. (defun org-create-formula-image (string tofile options buffer)
  15674. "This calls dvipng."
  15675. (require 'org-latex)
  15676. (let* ((tmpdir (if (featurep 'xemacs)
  15677. (temp-directory)
  15678. temporary-file-directory))
  15679. (texfilebase (make-temp-name
  15680. (expand-file-name "orgtex" tmpdir)))
  15681. (texfile (concat texfilebase ".tex"))
  15682. (dvifile (concat texfilebase ".dvi"))
  15683. (pngfile (concat texfilebase ".png"))
  15684. (fnh (if (featurep 'xemacs)
  15685. (font-height (face-font 'default))
  15686. (face-attribute 'default :height nil)))
  15687. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15688. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15689. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15690. "Black"))
  15691. (bg (or (plist-get options (if buffer :background :html-background))
  15692. "Transparent")))
  15693. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15694. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15695. (with-temp-file texfile
  15696. (insert (org-splice-latex-header
  15697. org-format-latex-header
  15698. org-export-latex-default-packages-alist
  15699. org-export-latex-packages-alist t
  15700. org-format-latex-header-extra))
  15701. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15702. (require 'org-latex)
  15703. (org-export-latex-fix-inputenc))
  15704. (let ((dir default-directory))
  15705. (condition-case nil
  15706. (progn
  15707. (cd tmpdir)
  15708. (call-process "latex" nil nil nil texfile))
  15709. (error nil))
  15710. (cd dir))
  15711. (if (not (file-exists-p dvifile))
  15712. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15713. (condition-case nil
  15714. (if (featurep 'xemacs)
  15715. (call-process "dvipng" nil nil nil
  15716. "-fg" fg "-bg" bg
  15717. "-T" "tight"
  15718. "-o" pngfile
  15719. dvifile)
  15720. (call-process "dvipng" nil nil nil
  15721. "-fg" fg "-bg" bg
  15722. "-D" dpi
  15723. ;;"-x" scale "-y" scale
  15724. "-T" "tight"
  15725. "-o" pngfile
  15726. dvifile))
  15727. (error nil))
  15728. (if (not (file-exists-p pngfile))
  15729. (if org-format-latex-signal-error
  15730. (error "Failed to create png file from %s" texfile)
  15731. (message "Failed to create png file from %s" texfile)
  15732. nil)
  15733. ;; Use the requested file name and clean up
  15734. (copy-file pngfile tofile 'replace)
  15735. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15736. (delete-file (concat texfilebase e)))
  15737. pngfile))))
  15738. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15739. "Fill a LaTeX header template TPL.
  15740. In the template, the following place holders will be recognized:
  15741. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15742. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15743. [PACKAGES] \\usepackage statements for PKG
  15744. [NO-PACKAGES] do not include PKG
  15745. [EXTRA] the string EXTRA
  15746. [NO-EXTRA] do not include EXTRA
  15747. For backward compatibility, if both the positive and the negative place
  15748. holder is missing, the positive one (without the \"NO-\") will be
  15749. assumed to be present at the end of the template.
  15750. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15751. EXTRA is a string.
  15752. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15753. (let (rpl (end ""))
  15754. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15755. (setq rpl (if (or (match-end 1) (not def-pkg))
  15756. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15757. tpl (replace-match rpl t t tpl))
  15758. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15759. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15760. (setq rpl (if (or (match-end 1) (not pkg))
  15761. "" (org-latex-packages-to-string pkg snippets-p t))
  15762. tpl (replace-match rpl t t tpl))
  15763. (if pkg (setq end
  15764. (concat end "\n"
  15765. (org-latex-packages-to-string pkg snippets-p)))))
  15766. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15767. (setq rpl (if (or (match-end 1) (not extra))
  15768. "" (concat extra "\n"))
  15769. tpl (replace-match rpl t t tpl))
  15770. (if (and extra (string-match "\\S-" extra))
  15771. (setq end (concat end "\n" extra))))
  15772. (if (string-match "\\S-" end)
  15773. (concat tpl "\n" end)
  15774. tpl)))
  15775. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15776. "Turn an alist of packages into a string with the \\usepackage macros."
  15777. (setq pkg (mapconcat (lambda(p)
  15778. (cond
  15779. ((stringp p) p)
  15780. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15781. (format "%% Package %s omitted" (cadr p)))
  15782. ((equal "" (car p))
  15783. (format "\\usepackage{%s}" (cadr p)))
  15784. (t
  15785. (format "\\usepackage[%s]{%s}"
  15786. (car p) (cadr p)))))
  15787. pkg
  15788. "\n"))
  15789. (if newline (concat pkg "\n") pkg))
  15790. (defun org-dvipng-color (attr)
  15791. "Return an rgb color specification for dvipng."
  15792. (apply 'format "rgb %s %s %s"
  15793. (mapcar 'org-normalize-color
  15794. (if (featurep 'xemacs)
  15795. (color-rgb-components
  15796. (face-property 'default
  15797. (cond ((eq attr :foreground) 'foreground)
  15798. ((eq attr :background) 'background))))
  15799. (color-values (face-attribute 'default attr nil))))))
  15800. (defun org-normalize-color (value)
  15801. "Return string to be used as color value for an RGB component."
  15802. (format "%g" (/ value 65535.0)))
  15803. ;; Image display
  15804. (defvar org-inline-image-overlays nil)
  15805. (make-variable-buffer-local 'org-inline-image-overlays)
  15806. (defun org-toggle-inline-images (&optional include-linked)
  15807. "Toggle the display of inline images.
  15808. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15809. (interactive "P")
  15810. (if org-inline-image-overlays
  15811. (progn
  15812. (org-remove-inline-images)
  15813. (message "Inline image display turned off"))
  15814. (org-display-inline-images include-linked)
  15815. (if org-inline-image-overlays
  15816. (message "%d images displayed inline"
  15817. (length org-inline-image-overlays))
  15818. (message "No images to display inline"))))
  15819. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15820. "Display inline images.
  15821. Normally only links without a description part are inlined, because this
  15822. is how it will work for export. When INCLUDE-LINKED is set, also links
  15823. with a description part will be inlined. This can be nice for a quick
  15824. look at those images, but it does not reflect what exported files will look
  15825. like.
  15826. When REFRESH is set, refresh existing images between BEG and END.
  15827. This will create new image displays only if necessary.
  15828. BEG and END default to the buffer boundaries."
  15829. (interactive "P")
  15830. (unless refresh
  15831. (org-remove-inline-images)
  15832. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15833. (save-excursion
  15834. (save-restriction
  15835. (widen)
  15836. (setq beg (or beg (point-min)) end (or end (point-max)))
  15837. (goto-char (point-min))
  15838. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15839. (substring (org-image-file-name-regexp) 0 -2)
  15840. "\\)\\]" (if include-linked "" "\\]")))
  15841. old file ov img)
  15842. (while (re-search-forward re end t)
  15843. (setq old (get-char-property-and-overlay (match-beginning 1)
  15844. 'org-image-overlay))
  15845. (setq file (expand-file-name
  15846. (concat (or (match-string 3) "") (match-string 4))))
  15847. (when (file-exists-p file)
  15848. (if (and (car-safe old) refresh)
  15849. (image-refresh (overlay-get (cdr old) 'display))
  15850. (setq img (save-match-data (create-image file)))
  15851. (when img
  15852. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15853. (overlay-put ov 'display img)
  15854. (overlay-put ov 'face 'default)
  15855. (overlay-put ov 'org-image-overlay t)
  15856. (overlay-put ov 'modification-hooks
  15857. (list 'org-display-inline-modification-hook))
  15858. (push ov org-inline-image-overlays)))))))))
  15859. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15860. "Remove inline-display overlay if a corresponding region is modified."
  15861. (let ((inhibit-modification-hooks t))
  15862. (when (and ov after)
  15863. (delete ov org-inline-image-overlays)
  15864. (delete-overlay ov))))
  15865. (defun org-remove-inline-images ()
  15866. "Remove inline display of images."
  15867. (interactive)
  15868. (mapc 'delete-overlay org-inline-image-overlays)
  15869. (setq org-inline-image-overlays nil))
  15870. ;;;; Key bindings
  15871. ;; Outline functions from `outline-mode-prefix-map'
  15872. ;; that can be remapped in Org:
  15873. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15874. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15875. (define-key org-mode-map [remap outline-forward-same-level]
  15876. 'org-forward-same-level)
  15877. (define-key org-mode-map [remap outline-backward-same-level]
  15878. 'org-backward-same-level)
  15879. (define-key org-mode-map [remap show-branches]
  15880. 'org-kill-note-or-show-branches)
  15881. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15882. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15883. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15884. ;; Outline functions from `outline-mode-prefix-map'
  15885. ;; that can not be remapped in Org:
  15886. ;; - the column "key binding" shows whether the Outline function is still
  15887. ;; available in Org mode on the same key that it has been bound to in
  15888. ;; Outline mode:
  15889. ;; - "overridden": key used for a different functionality in Org mode
  15890. ;; - else: key still bound to the same Outline function in Org mode
  15891. ;; | Outline function | key binding | Org replacement |
  15892. ;; |------------------------------------+-------------+-----------------------|
  15893. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15894. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15895. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15896. ;; | `hide-subtree' | overridden | visibility cycling |
  15897. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15898. ;; | `hide-body' | overridden | no replacement |
  15899. ;; | `show-all' | overridden | no replacement |
  15900. ;; | `hide-entry' | overridden | visibility cycling |
  15901. ;; | `show-entry' | overridden | no replacement |
  15902. ;; | `hide-leaves' | overridden | no replacement |
  15903. ;; | `hide-sublevels' | overridden | no replacement |
  15904. ;; | `hide-other' | overridden | no replacement |
  15905. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15906. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15907. ;; Make `C-c C-x' a prefix key
  15908. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15909. ;; TAB key with modifiers
  15910. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15911. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15912. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15913. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15914. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15915. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15916. ;; The following line is necessary under Suse GNU/Linux
  15917. (unless (featurep 'xemacs)
  15918. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15919. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15920. (define-key org-mode-map [backtab] 'org-shifttab)
  15921. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15922. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15923. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15924. ;; Cursor keys with modifiers
  15925. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15926. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15927. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15928. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15929. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15930. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15931. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15932. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15933. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15934. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15935. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15936. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15937. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15938. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15939. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15940. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15941. ;; Babel keys
  15942. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15943. (mapc (lambda (pair)
  15944. (define-key org-babel-map (car pair) (cdr pair)))
  15945. org-babel-key-bindings)
  15946. ;;; Extra keys for tty access.
  15947. ;; We only set them when really needed because otherwise the
  15948. ;; menus don't show the simple keys
  15949. (when (or org-use-extra-keys
  15950. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15951. (not window-system))
  15952. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15953. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15954. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15955. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15956. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15957. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15958. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15959. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15960. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15961. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15962. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15963. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15964. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15965. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15966. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15967. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15968. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15969. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15970. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15971. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15972. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15973. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15974. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15975. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15976. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15977. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15978. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15979. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15980. ;; All the other keys
  15981. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15982. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15983. (if (boundp 'narrow-map)
  15984. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15985. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15986. (if (boundp 'narrow-map)
  15987. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15988. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15989. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15990. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15991. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15992. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15993. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15994. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  15995. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15996. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15997. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15998. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15999. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16000. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16001. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16002. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16003. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16004. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16005. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16006. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16007. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16008. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16009. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16010. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16011. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16012. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16013. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16014. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16015. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16016. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16017. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16018. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16019. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16020. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16021. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16022. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16023. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16024. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16025. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16026. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16027. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16028. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16029. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16030. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16031. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16032. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16033. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16034. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16035. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16036. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16037. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16038. (org-defkey org-mode-map "\C-m" 'org-return)
  16039. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16040. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16041. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16042. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16043. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16044. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16045. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16046. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16047. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16048. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16049. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16050. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16051. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16052. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16053. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16054. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16055. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16056. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16057. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16058. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16059. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16060. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16061. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16062. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16063. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16064. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16065. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16066. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16067. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16068. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16069. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16070. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16071. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16072. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16073. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16074. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16075. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16076. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16077. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16078. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16079. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16080. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16081. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16082. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16083. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16084. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16085. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16086. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16087. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16088. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16089. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16090. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16091. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16092. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16093. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16094. (when (featurep 'xemacs)
  16095. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16096. (defconst org-speed-commands-default
  16097. '(
  16098. ("Outline Navigation")
  16099. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16100. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16101. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16102. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16103. ("u" . (org-speed-move-safe 'outline-up-heading))
  16104. ("j" . org-goto)
  16105. ("g" . (org-refile t))
  16106. ("Outline Visibility")
  16107. ("c" . org-cycle)
  16108. ("C" . org-shifttab)
  16109. (" " . org-display-outline-path)
  16110. ("Outline Structure Editing")
  16111. ("U" . org-shiftmetaup)
  16112. ("D" . org-shiftmetadown)
  16113. ("r" . org-metaright)
  16114. ("l" . org-metaleft)
  16115. ("R" . org-shiftmetaright)
  16116. ("L" . org-shiftmetaleft)
  16117. ("i" . (progn (forward-char 1) (call-interactively
  16118. 'org-insert-heading-respect-content)))
  16119. ("^" . org-sort)
  16120. ("w" . org-refile)
  16121. ("a" . org-archive-subtree-default-with-confirmation)
  16122. ("." . org-mark-subtree)
  16123. ("Clock Commands")
  16124. ("I" . org-clock-in)
  16125. ("O" . org-clock-out)
  16126. ("Meta Data Editing")
  16127. ("t" . org-todo)
  16128. ("0" . (org-priority ?\ ))
  16129. ("1" . (org-priority ?A))
  16130. ("2" . (org-priority ?B))
  16131. ("3" . (org-priority ?C))
  16132. (";" . org-set-tags-command)
  16133. ("e" . org-set-effort)
  16134. ("Agenda Views etc")
  16135. ("v" . org-agenda)
  16136. ("/" . org-sparse-tree)
  16137. ("Misc")
  16138. ("o" . org-open-at-point)
  16139. ("?" . org-speed-command-help)
  16140. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16141. (">" . (org-agenda-remove-restriction-lock))
  16142. )
  16143. "The default speed commands.")
  16144. (defun org-print-speed-command (e)
  16145. (if (> (length (car e)) 1)
  16146. (progn
  16147. (princ "\n")
  16148. (princ (car e))
  16149. (princ "\n")
  16150. (princ (make-string (length (car e)) ?-))
  16151. (princ "\n"))
  16152. (princ (car e))
  16153. (princ " ")
  16154. (if (symbolp (cdr e))
  16155. (princ (symbol-name (cdr e)))
  16156. (prin1 (cdr e)))
  16157. (princ "\n")))
  16158. (defun org-speed-command-help ()
  16159. "Show the available speed commands."
  16160. (interactive)
  16161. (if (not org-use-speed-commands)
  16162. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16163. (with-output-to-temp-buffer "*Help*"
  16164. (princ "User-defined Speed commands\n===========================\n")
  16165. (mapc 'org-print-speed-command org-speed-commands-user)
  16166. (princ "\n")
  16167. (princ "Built-in Speed commands\n=======================\n")
  16168. (mapc 'org-print-speed-command org-speed-commands-default))
  16169. (with-current-buffer "*Help*"
  16170. (setq truncate-lines t))))
  16171. (defun org-speed-move-safe (cmd)
  16172. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16173. If not, return to the original position and throw an error."
  16174. (interactive)
  16175. (let ((pos (point)))
  16176. (call-interactively cmd)
  16177. (unless (and (bolp) (org-at-heading-p))
  16178. (goto-char pos)
  16179. (error "Boundary reached while executing %s" cmd))))
  16180. (defvar org-self-insert-command-undo-counter 0)
  16181. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16182. (defvar org-speed-command nil)
  16183. (defun org-speed-command-default-hook (keys)
  16184. "Hook for activating single-letter speed commands.
  16185. `org-speed-commands-default' specifies a minimal command set.
  16186. Use `org-speed-commands-user' for further customization."
  16187. (when (or (and (bolp) (looking-at org-outline-regexp))
  16188. (and (functionp org-use-speed-commands)
  16189. (funcall org-use-speed-commands)))
  16190. (cdr (assoc keys (append org-speed-commands-user
  16191. org-speed-commands-default)))))
  16192. (defun org-babel-speed-command-hook (keys)
  16193. "Hook for activating single-letter code block commands."
  16194. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16195. (cdr (assoc keys org-babel-key-bindings))))
  16196. (defcustom org-speed-command-hook
  16197. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16198. "Hook for activating speed commands at strategic locations.
  16199. Hook functions are called in sequence until a valid handler is
  16200. found.
  16201. Each hook takes a single argument, a user-pressed command key
  16202. which is also a `self-insert-command' from the global map.
  16203. Within the hook, examine the cursor position and the command key
  16204. and return nil or a valid handler as appropriate. Handler could
  16205. be one of an interactive command, a function, or a form.
  16206. Set `org-use-speed-commands' to non-nil value to enable this
  16207. hook. The default setting is `org-speed-command-default-hook'."
  16208. :group 'org-structure
  16209. :version "24.1"
  16210. :type 'hook)
  16211. (defun org-self-insert-command (N)
  16212. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16213. If the cursor is in a table looking at whitespace, the whitespace is
  16214. overwritten, and the table is not marked as requiring realignment."
  16215. (interactive "p")
  16216. (org-check-before-invisible-edit 'insert)
  16217. (cond
  16218. ((and org-use-speed-commands
  16219. (setq org-speed-command
  16220. (run-hook-with-args-until-success
  16221. 'org-speed-command-hook (this-command-keys))))
  16222. (cond
  16223. ((commandp org-speed-command)
  16224. (setq this-command org-speed-command)
  16225. (call-interactively org-speed-command))
  16226. ((functionp org-speed-command)
  16227. (funcall org-speed-command))
  16228. ((and org-speed-command (listp org-speed-command))
  16229. (eval org-speed-command))
  16230. (t (let (org-use-speed-commands)
  16231. (call-interactively 'org-self-insert-command)))))
  16232. ((and
  16233. (org-table-p)
  16234. (progn
  16235. ;; check if we blank the field, and if that triggers align
  16236. (and (featurep 'org-table) org-table-auto-blank-field
  16237. (member last-command
  16238. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16239. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16240. ;; got extra space, this field does not determine column width
  16241. (let (org-table-may-need-update) (org-table-blank-field))
  16242. ;; no extra space, this field may determine column width
  16243. (org-table-blank-field)))
  16244. t)
  16245. (eq N 1)
  16246. (looking-at "[^|\n]* |"))
  16247. (let (org-table-may-need-update)
  16248. (goto-char (1- (match-end 0)))
  16249. (delete-char -1)
  16250. (goto-char (match-beginning 0))
  16251. (self-insert-command N)))
  16252. (t
  16253. (setq org-table-may-need-update t)
  16254. (self-insert-command N)
  16255. (org-fix-tags-on-the-fly)
  16256. (if org-self-insert-cluster-for-undo
  16257. (if (not (eq last-command 'org-self-insert-command))
  16258. (setq org-self-insert-command-undo-counter 1)
  16259. (if (>= org-self-insert-command-undo-counter 20)
  16260. (setq org-self-insert-command-undo-counter 1)
  16261. (and (> org-self-insert-command-undo-counter 0)
  16262. buffer-undo-list (listp buffer-undo-list)
  16263. (not (cadr buffer-undo-list)) ; remove nil entry
  16264. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16265. (setq org-self-insert-command-undo-counter
  16266. (1+ org-self-insert-command-undo-counter))))))))
  16267. (defun org-check-before-invisible-edit (kind)
  16268. "Check is editing if kind KIND would be dangerous with invisible text around.
  16269. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16270. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16271. (if (and org-catch-invisible-edits
  16272. (or (not (boundp 'visible-mode)) (not visible-mode))
  16273. (or (get-char-property (point) 'invisible)
  16274. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16275. ;; OK, we need to take a closer look
  16276. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16277. (invisible-before-point (if (bobp) nil (get-char-property
  16278. (1- (point)) 'invisible)))
  16279. (border-and-ok-direction
  16280. (or
  16281. ;; Check if we are acting predictably before invisible text
  16282. (and invisible-at-point (not invisible-before-point)
  16283. (memq kind '(insert delete-backward)))
  16284. ;; Check if we are acting predictably after invisible text
  16285. ;; This works not well, and I have turned it off. It seems
  16286. ;; better to always show and stop after invisible text.
  16287. ;; (and (not invisible-at-point) invisible-before-point
  16288. ;; (memq kind '(insert delete)))
  16289. )))
  16290. (when (or (memq invisible-at-point '(outline org-hide-block))
  16291. (memq invisible-before-point '(outline org-hide-block)))
  16292. (if (eq org-catch-invisible-edits 'error)
  16293. (error "Editing in invisible areas is prohibited - make visible first"))
  16294. ;; Make the area visible
  16295. (save-excursion
  16296. (if invisible-before-point
  16297. (goto-char (previous-single-char-property-change
  16298. (point) 'invisible)))
  16299. (org-cycle))
  16300. (cond
  16301. ((eq org-catch-invisible-edits 'show)
  16302. ;; That's it, we do the edit after showing
  16303. (message
  16304. "Unfolding invisible region around point before editing")
  16305. (sit-for 1))
  16306. ((and (eq org-catch-invisible-edits 'smart)
  16307. border-and-ok-direction)
  16308. (message "Unfolding invisible region around point before editing"))
  16309. (t
  16310. ;; Don't do the edit, make the user repeat it in full visibility
  16311. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16312. (defun org-fix-tags-on-the-fly ()
  16313. (when (and (equal (char-after (point-at-bol)) ?*)
  16314. (org-at-heading-p))
  16315. (org-align-tags-here org-tags-column)))
  16316. (defun org-delete-backward-char (N)
  16317. "Like `delete-backward-char', insert whitespace at field end in tables.
  16318. When deleting backwards, in tables this function will insert whitespace in
  16319. front of the next \"|\" separator, to keep the table aligned. The table will
  16320. still be marked for re-alignment if the field did fill the entire column,
  16321. because, in this case the deletion might narrow the column."
  16322. (interactive "p")
  16323. (org-check-before-invisible-edit 'delete-backward)
  16324. (if (and (org-table-p)
  16325. (eq N 1)
  16326. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16327. (looking-at ".*?|"))
  16328. (let ((pos (point))
  16329. (noalign (looking-at "[^|\n\r]* |"))
  16330. (c org-table-may-need-update))
  16331. (backward-delete-char N)
  16332. (if (not overwrite-mode)
  16333. (progn
  16334. (skip-chars-forward "^|")
  16335. (insert " ")
  16336. (goto-char (1- pos))))
  16337. ;; noalign: if there were two spaces at the end, this field
  16338. ;; does not determine the width of the column.
  16339. (if noalign (setq org-table-may-need-update c)))
  16340. (backward-delete-char N)
  16341. (org-fix-tags-on-the-fly)))
  16342. (defun org-delete-char (N)
  16343. "Like `delete-char', but insert whitespace at field end in tables.
  16344. When deleting characters, in tables this function will insert whitespace in
  16345. front of the next \"|\" separator, to keep the table aligned. The table will
  16346. still be marked for re-alignment if the field did fill the entire column,
  16347. because, in this case the deletion might narrow the column."
  16348. (interactive "p")
  16349. (org-check-before-invisible-edit 'delete)
  16350. (if (and (org-table-p)
  16351. (not (bolp))
  16352. (not (= (char-after) ?|))
  16353. (eq N 1))
  16354. (if (looking-at ".*?|")
  16355. (let ((pos (point))
  16356. (noalign (looking-at "[^|\n\r]* |"))
  16357. (c org-table-may-need-update))
  16358. (replace-match (concat
  16359. (substring (match-string 0) 1 -1)
  16360. " |"))
  16361. (goto-char pos)
  16362. ;; noalign: if there were two spaces at the end, this field
  16363. ;; does not determine the width of the column.
  16364. (if noalign (setq org-table-may-need-update c)))
  16365. (delete-char N))
  16366. (delete-char N)
  16367. (org-fix-tags-on-the-fly)))
  16368. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16369. (put 'org-self-insert-command 'delete-selection t)
  16370. (put 'orgtbl-self-insert-command 'delete-selection t)
  16371. (put 'org-delete-char 'delete-selection 'supersede)
  16372. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16373. (put 'org-yank 'delete-selection 'yank)
  16374. ;; Make `flyspell-mode' delay after some commands
  16375. (put 'org-self-insert-command 'flyspell-delayed t)
  16376. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16377. (put 'org-delete-char 'flyspell-delayed t)
  16378. (put 'org-delete-backward-char 'flyspell-delayed t)
  16379. ;; Make pabbrev-mode expand after org-mode commands
  16380. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16381. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16382. ;; How to do this: Measure non-white length of current string
  16383. ;; If equal to column width, we should realign.
  16384. (defun org-remap (map &rest commands)
  16385. "In MAP, remap the functions given in COMMANDS.
  16386. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16387. (let (new old)
  16388. (while commands
  16389. (setq old (pop commands) new (pop commands))
  16390. (if (fboundp 'command-remapping)
  16391. (org-defkey map (vector 'remap old) new)
  16392. (substitute-key-definition old new map global-map)))))
  16393. (when (eq org-enable-table-editor 'optimized)
  16394. ;; If the user wants maximum table support, we need to hijack
  16395. ;; some standard editing functions
  16396. (org-remap org-mode-map
  16397. 'self-insert-command 'org-self-insert-command
  16398. 'delete-char 'org-delete-char
  16399. 'delete-backward-char 'org-delete-backward-char)
  16400. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16401. (defvar org-ctrl-c-ctrl-c-hook nil
  16402. "Hook for functions attaching themselves to `C-c C-c'.
  16403. This can be used to add additional functionality to the C-c C-c
  16404. key which executes context-dependent commands. This hook is run
  16405. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16406. run after the last test.
  16407. Each function will be called with no arguments. The function
  16408. must check if the context is appropriate for it to act. If yes,
  16409. it should do its thing and then return a non-nil value. If the
  16410. context is wrong, just do nothing and return nil.")
  16411. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16412. "Hook for functions attaching themselves to `C-c C-c'.
  16413. This can be used to add additional functionality to the C-c C-c
  16414. key which executes context-dependent commands. This hook is run
  16415. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16416. before the first test.
  16417. Each function will be called with no arguments. The function
  16418. must check if the context is appropriate for it to act. If yes,
  16419. it should do its thing and then return a non-nil value. If the
  16420. context is wrong, just do nothing and return nil.")
  16421. (defvar org-tab-first-hook nil
  16422. "Hook for functions to attach themselves to TAB.
  16423. See `org-ctrl-c-ctrl-c-hook' for more information.
  16424. This hook runs as the first action when TAB is pressed, even before
  16425. `org-cycle' messes around with the `outline-regexp' to cater for
  16426. inline tasks and plain list item folding.
  16427. If any function in this hook returns t, any other actions that
  16428. would have been caused by TAB (such as table field motion or visibility
  16429. cycling) will not occur.")
  16430. (defvar org-tab-after-check-for-table-hook nil
  16431. "Hook for functions to attach themselves to TAB.
  16432. See `org-ctrl-c-ctrl-c-hook' for more information.
  16433. This hook runs after it has been established that the cursor is not in a
  16434. table, but before checking if the cursor is in a headline or if global cycling
  16435. should be done.
  16436. If any function in this hook returns t, not other actions like visibility
  16437. cycling will be done.")
  16438. (defvar org-tab-after-check-for-cycling-hook nil
  16439. "Hook for functions to attach themselves to TAB.
  16440. See `org-ctrl-c-ctrl-c-hook' for more information.
  16441. This hook runs after it has been established that not table field motion and
  16442. not visibility should be done because of current context. This is probably
  16443. the place where a package like yasnippets can hook in.")
  16444. (defvar org-tab-before-tab-emulation-hook nil
  16445. "Hook for functions to attach themselves to TAB.
  16446. See `org-ctrl-c-ctrl-c-hook' for more information.
  16447. This hook runs after every other options for TAB have been exhausted, but
  16448. before indentation and \t insertion takes place.")
  16449. (defvar org-metaleft-hook nil
  16450. "Hook for functions attaching themselves to `M-left'.
  16451. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16452. (defvar org-metaright-hook nil
  16453. "Hook for functions attaching themselves to `M-right'.
  16454. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16455. (defvar org-metaup-hook nil
  16456. "Hook for functions attaching themselves to `M-up'.
  16457. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16458. (defvar org-metadown-hook nil
  16459. "Hook for functions attaching themselves to `M-down'.
  16460. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16461. (defvar org-shiftmetaleft-hook nil
  16462. "Hook for functions attaching themselves to `M-S-left'.
  16463. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16464. (defvar org-shiftmetaright-hook nil
  16465. "Hook for functions attaching themselves to `M-S-right'.
  16466. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16467. (defvar org-shiftmetaup-hook nil
  16468. "Hook for functions attaching themselves to `M-S-up'.
  16469. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16470. (defvar org-shiftmetadown-hook nil
  16471. "Hook for functions attaching themselves to `M-S-down'.
  16472. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16473. (defvar org-metareturn-hook nil
  16474. "Hook for functions attaching themselves to `M-RET'.
  16475. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16476. (defvar org-shiftup-hook nil
  16477. "Hook for functions attaching themselves to `S-up'.
  16478. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16479. (defvar org-shiftup-final-hook nil
  16480. "Hook for functions attaching themselves to `S-up'.
  16481. This one runs after all other options except shift-select have been excluded.
  16482. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16483. (defvar org-shiftdown-hook nil
  16484. "Hook for functions attaching themselves to `S-down'.
  16485. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16486. (defvar org-shiftdown-final-hook nil
  16487. "Hook for functions attaching themselves to `S-down'.
  16488. This one runs after all other options except shift-select have been excluded.
  16489. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16490. (defvar org-shiftleft-hook nil
  16491. "Hook for functions attaching themselves to `S-left'.
  16492. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16493. (defvar org-shiftleft-final-hook nil
  16494. "Hook for functions attaching themselves to `S-left'.
  16495. This one runs after all other options except shift-select have been excluded.
  16496. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16497. (defvar org-shiftright-hook nil
  16498. "Hook for functions attaching themselves to `S-right'.
  16499. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16500. (defvar org-shiftright-final-hook nil
  16501. "Hook for functions attaching themselves to `S-right'.
  16502. This one runs after all other options except shift-select have been excluded.
  16503. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16504. (defun org-modifier-cursor-error ()
  16505. "Throw an error, a modified cursor command was applied in wrong context."
  16506. (error "This command is active in special context like tables, headlines or items"))
  16507. (defun org-shiftselect-error ()
  16508. "Throw an error because Shift-Cursor command was applied in wrong context."
  16509. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16510. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16511. (error "This command works only in special context like headlines or timestamps")))
  16512. (defun org-call-for-shift-select (cmd)
  16513. (let ((this-command-keys-shift-translated t))
  16514. (call-interactively cmd)))
  16515. (defun org-shifttab (&optional arg)
  16516. "Global visibility cycling or move to previous table field.
  16517. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16518. on context.
  16519. See the individual commands for more information."
  16520. (interactive "P")
  16521. (cond
  16522. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16523. ((integerp arg)
  16524. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16525. (message "Content view to level: %d" arg)
  16526. (org-content (prefix-numeric-value arg2))
  16527. (setq org-cycle-global-status 'overview)))
  16528. (t (call-interactively 'org-global-cycle))))
  16529. (defun org-shiftmetaleft ()
  16530. "Promote subtree or delete table column.
  16531. Calls `org-promote-subtree', `org-outdent-item',
  16532. or `org-table-delete-column', depending on context.
  16533. See the individual commands for more information."
  16534. (interactive)
  16535. (cond
  16536. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16537. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16538. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16539. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16540. (t (org-modifier-cursor-error))))
  16541. (defun org-shiftmetaright ()
  16542. "Demote subtree or insert table column.
  16543. Calls `org-demote-subtree', `org-indent-item',
  16544. or `org-table-insert-column', depending on context.
  16545. See the individual commands for more information."
  16546. (interactive)
  16547. (cond
  16548. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16549. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16550. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16551. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16552. (t (org-modifier-cursor-error))))
  16553. (defun org-shiftmetaup (&optional arg)
  16554. "Move subtree up or kill table row.
  16555. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16556. `org-move-item-up' depending on context. See the individual commands
  16557. for more information."
  16558. (interactive "P")
  16559. (cond
  16560. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16561. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16562. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16563. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16564. (t (org-modifier-cursor-error))))
  16565. (defun org-shiftmetadown (&optional arg)
  16566. "Move subtree down or insert table row.
  16567. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16568. `org-move-item-down', depending on context. See the individual
  16569. commands for more information."
  16570. (interactive "P")
  16571. (cond
  16572. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16573. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16574. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16575. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16576. (t (org-modifier-cursor-error))))
  16577. (defsubst org-hidden-tree-error ()
  16578. (error
  16579. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16580. (defun org-metaleft (&optional arg)
  16581. "Promote heading or move table column to left.
  16582. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16583. With no specific context, calls the Emacs default `backward-word'.
  16584. See the individual commands for more information."
  16585. (interactive "P")
  16586. (cond
  16587. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16588. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16589. ((org-with-limited-levels
  16590. (or (org-at-heading-p)
  16591. (and (org-region-active-p)
  16592. (save-excursion
  16593. (goto-char (region-beginning))
  16594. (org-at-heading-p)))))
  16595. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16596. (call-interactively 'org-do-promote))
  16597. ;; At an inline task.
  16598. ((org-at-heading-p)
  16599. (call-interactively 'org-inlinetask-promote))
  16600. ((or (org-at-item-p)
  16601. (and (org-region-active-p)
  16602. (save-excursion
  16603. (goto-char (region-beginning))
  16604. (org-at-item-p))))
  16605. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16606. (call-interactively 'org-outdent-item))
  16607. (t (call-interactively 'backward-word))))
  16608. (defun org-metaright (&optional arg)
  16609. "Demote a subtree, a list item or move table column to right.
  16610. In front of a drawer or a block keyword, indent it correctly.
  16611. With no specific context, calls the Emacs default `forward-word'.
  16612. See the individual commands for more information."
  16613. (interactive "P")
  16614. (cond
  16615. ((run-hook-with-args-until-success 'org-metaright-hook))
  16616. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16617. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16618. ((org-at-block-p) (call-interactively 'org-indent-block))
  16619. ((org-with-limited-levels
  16620. (or (org-at-heading-p)
  16621. (and (org-region-active-p)
  16622. (save-excursion
  16623. (goto-char (region-beginning))
  16624. (org-at-heading-p)))))
  16625. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16626. (call-interactively 'org-do-demote))
  16627. ;; At an inline task.
  16628. ((org-at-heading-p)
  16629. (call-interactively 'org-inlinetask-demote))
  16630. ((or (org-at-item-p)
  16631. (and (org-region-active-p)
  16632. (save-excursion
  16633. (goto-char (region-beginning))
  16634. (org-at-item-p))))
  16635. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16636. (call-interactively 'org-indent-item))
  16637. (t (call-interactively 'forward-word))))
  16638. (defun org-check-for-hidden (what)
  16639. "Check if there are hidden headlines/items in the current visual line.
  16640. WHAT can be either `headlines' or `items'. If the current line is
  16641. an outline or item heading and it has a folded subtree below it,
  16642. this function returns t, nil otherwise."
  16643. (let ((re (cond
  16644. ((eq what 'headlines) org-outline-regexp-bol)
  16645. ((eq what 'items) (org-item-beginning-re))
  16646. (t (error "This should not happen"))))
  16647. beg end)
  16648. (save-excursion
  16649. (catch 'exit
  16650. (unless (org-region-active-p)
  16651. (setq beg (point-at-bol))
  16652. (beginning-of-line 2)
  16653. (while (and (not (eobp)) ;; this is like `next-line'
  16654. (get-char-property (1- (point)) 'invisible))
  16655. (beginning-of-line 2))
  16656. (setq end (point))
  16657. (goto-char beg)
  16658. (goto-char (point-at-eol))
  16659. (setq end (max end (point)))
  16660. (while (re-search-forward re end t)
  16661. (if (get-char-property (match-beginning 0) 'invisible)
  16662. (throw 'exit t))))
  16663. nil))))
  16664. (defun org-metaup (&optional arg)
  16665. "Move subtree up or move table row up.
  16666. Calls `org-move-subtree-up' or `org-table-move-row' or
  16667. `org-move-item-up', depending on context. See the individual commands
  16668. for more information."
  16669. (interactive "P")
  16670. (cond
  16671. ((run-hook-with-args-until-success 'org-metaup-hook))
  16672. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16673. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16674. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16675. (t (transpose-lines 1) (beginning-of-line -1))))
  16676. (defun org-metadown (&optional arg)
  16677. "Move subtree down or move table row down.
  16678. Calls `org-move-subtree-down' or `org-table-move-row' or
  16679. `org-move-item-down', depending on context. See the individual
  16680. commands for more information."
  16681. (interactive "P")
  16682. (cond
  16683. ((run-hook-with-args-until-success 'org-metadown-hook))
  16684. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16685. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16686. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16687. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16688. (defun org-shiftup (&optional arg)
  16689. "Increase item in timestamp or increase priority of current headline.
  16690. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16691. depending on context. See the individual commands for more information."
  16692. (interactive "P")
  16693. (cond
  16694. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16695. ((and org-support-shift-select (org-region-active-p))
  16696. (org-call-for-shift-select 'previous-line))
  16697. ((org-at-timestamp-p t)
  16698. (call-interactively (if org-edit-timestamp-down-means-later
  16699. 'org-timestamp-down 'org-timestamp-up)))
  16700. ((and (not (eq org-support-shift-select 'always))
  16701. org-enable-priority-commands
  16702. (org-at-heading-p))
  16703. (call-interactively 'org-priority-up))
  16704. ((and (not org-support-shift-select) (org-at-item-p))
  16705. (call-interactively 'org-previous-item))
  16706. ((org-clocktable-try-shift 'up arg))
  16707. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16708. (org-support-shift-select
  16709. (org-call-for-shift-select 'previous-line))
  16710. (t (org-shiftselect-error))))
  16711. (defun org-shiftdown (&optional arg)
  16712. "Decrease item in timestamp or decrease priority of current headline.
  16713. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16714. depending on context. See the individual commands for more information."
  16715. (interactive "P")
  16716. (cond
  16717. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16718. ((and org-support-shift-select (org-region-active-p))
  16719. (org-call-for-shift-select 'next-line))
  16720. ((org-at-timestamp-p t)
  16721. (call-interactively (if org-edit-timestamp-down-means-later
  16722. 'org-timestamp-up 'org-timestamp-down)))
  16723. ((and (not (eq org-support-shift-select 'always))
  16724. org-enable-priority-commands
  16725. (org-at-heading-p))
  16726. (call-interactively 'org-priority-down))
  16727. ((and (not org-support-shift-select) (org-at-item-p))
  16728. (call-interactively 'org-next-item))
  16729. ((org-clocktable-try-shift 'down arg))
  16730. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16731. (org-support-shift-select
  16732. (org-call-for-shift-select 'next-line))
  16733. (t (org-shiftselect-error))))
  16734. (defun org-shiftright (&optional arg)
  16735. "Cycle the thing at point or in the current line, depending on context.
  16736. Depending on context, this does one of the following:
  16737. - switch a timestamp at point one day into the future
  16738. - on a headline, switch to the next TODO keyword.
  16739. - on an item, switch entire list to the next bullet type
  16740. - on a property line, switch to the next allowed value
  16741. - on a clocktable definition line, move time block into the future"
  16742. (interactive "P")
  16743. (cond
  16744. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16745. ((and org-support-shift-select (org-region-active-p))
  16746. (org-call-for-shift-select 'forward-char))
  16747. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16748. ((and (not (eq org-support-shift-select 'always))
  16749. (org-at-heading-p))
  16750. (let ((org-inhibit-logging
  16751. (not org-treat-S-cursor-todo-selection-as-state-change))
  16752. (org-inhibit-blocking
  16753. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16754. (org-call-with-arg 'org-todo 'right)))
  16755. ((or (and org-support-shift-select
  16756. (not (eq org-support-shift-select 'always))
  16757. (org-at-item-bullet-p))
  16758. (and (not org-support-shift-select) (org-at-item-p)))
  16759. (org-call-with-arg 'org-cycle-list-bullet nil))
  16760. ((and (not (eq org-support-shift-select 'always))
  16761. (org-at-property-p))
  16762. (call-interactively 'org-property-next-allowed-value))
  16763. ((org-clocktable-try-shift 'right arg))
  16764. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16765. (org-support-shift-select
  16766. (org-call-for-shift-select 'forward-char))
  16767. (t (org-shiftselect-error))))
  16768. (defun org-shiftleft (&optional arg)
  16769. "Cycle the thing at point or in the current line, depending on context.
  16770. Depending on context, this does one of the following:
  16771. - switch a timestamp at point one day into the past
  16772. - on a headline, switch to the previous TODO keyword.
  16773. - on an item, switch entire list to the previous bullet type
  16774. - on a property line, switch to the previous allowed value
  16775. - on a clocktable definition line, move time block into the past"
  16776. (interactive "P")
  16777. (cond
  16778. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16779. ((and org-support-shift-select (org-region-active-p))
  16780. (org-call-for-shift-select 'backward-char))
  16781. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16782. ((and (not (eq org-support-shift-select 'always))
  16783. (org-at-heading-p))
  16784. (let ((org-inhibit-logging
  16785. (not org-treat-S-cursor-todo-selection-as-state-change))
  16786. (org-inhibit-blocking
  16787. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16788. (org-call-with-arg 'org-todo 'left)))
  16789. ((or (and org-support-shift-select
  16790. (not (eq org-support-shift-select 'always))
  16791. (org-at-item-bullet-p))
  16792. (and (not org-support-shift-select) (org-at-item-p)))
  16793. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16794. ((and (not (eq org-support-shift-select 'always))
  16795. (org-at-property-p))
  16796. (call-interactively 'org-property-previous-allowed-value))
  16797. ((org-clocktable-try-shift 'left arg))
  16798. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16799. (org-support-shift-select
  16800. (org-call-for-shift-select 'backward-char))
  16801. (t (org-shiftselect-error))))
  16802. (defun org-shiftcontrolright ()
  16803. "Switch to next TODO set."
  16804. (interactive)
  16805. (cond
  16806. ((and org-support-shift-select (org-region-active-p))
  16807. (org-call-for-shift-select 'forward-word))
  16808. ((and (not (eq org-support-shift-select 'always))
  16809. (org-at-heading-p))
  16810. (org-call-with-arg 'org-todo 'nextset))
  16811. (org-support-shift-select
  16812. (org-call-for-shift-select 'forward-word))
  16813. (t (org-shiftselect-error))))
  16814. (defun org-shiftcontrolleft ()
  16815. "Switch to previous TODO set."
  16816. (interactive)
  16817. (cond
  16818. ((and org-support-shift-select (org-region-active-p))
  16819. (org-call-for-shift-select 'backward-word))
  16820. ((and (not (eq org-support-shift-select 'always))
  16821. (org-at-heading-p))
  16822. (org-call-with-arg 'org-todo 'previousset))
  16823. (org-support-shift-select
  16824. (org-call-for-shift-select 'backward-word))
  16825. (t (org-shiftselect-error))))
  16826. (defun org-shiftcontrolup ()
  16827. "Change timestamps synchronously up in CLOCK log lines."
  16828. (interactive)
  16829. (cond ((and (not org-support-shift-select)
  16830. (org-at-clock-log-p)
  16831. (org-at-timestamp-p t))
  16832. (org-clock-timestamps-up))
  16833. (t (org-shiftselect-error))))
  16834. (defun org-shiftcontroldown ()
  16835. "Change timestamps synchronously down in CLOCK log lines."
  16836. (interactive)
  16837. (cond ((and (not org-support-shift-select)
  16838. (org-at-clock-log-p)
  16839. (org-at-timestamp-p t))
  16840. (org-clock-timestamps-down))
  16841. (t (org-shiftselect-error))))
  16842. (defun org-ctrl-c-ret ()
  16843. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16844. (interactive)
  16845. (cond
  16846. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16847. (t (call-interactively 'org-insert-heading))))
  16848. (defun org-find-visible ()
  16849. (let ((s (point)))
  16850. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16851. (get-char-property s 'invisible)))
  16852. s))
  16853. (defun org-find-invisible ()
  16854. (let ((s (point)))
  16855. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16856. (not (get-char-property s 'invisible))))
  16857. s))
  16858. (defun org-copy-visible (beg end)
  16859. "Copy the visible parts of the region."
  16860. (interactive "r")
  16861. (let (snippets s)
  16862. (save-excursion
  16863. (save-restriction
  16864. (narrow-to-region beg end)
  16865. (setq s (goto-char (point-min)))
  16866. (while (not (= (point) (point-max)))
  16867. (goto-char (org-find-invisible))
  16868. (push (buffer-substring s (point)) snippets)
  16869. (setq s (goto-char (org-find-visible))))))
  16870. (kill-new (apply 'concat (nreverse snippets)))))
  16871. (defun org-copy-special ()
  16872. "Copy region in table or copy current subtree.
  16873. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16874. See the individual commands for more information."
  16875. (interactive)
  16876. (call-interactively
  16877. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16878. (defun org-cut-special ()
  16879. "Cut region in table or cut current subtree.
  16880. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16881. See the individual commands for more information."
  16882. (interactive)
  16883. (call-interactively
  16884. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16885. (defun org-paste-special (arg)
  16886. "Paste rectangular region into table, or past subtree relative to level.
  16887. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16888. See the individual commands for more information."
  16889. (interactive "P")
  16890. (if (org-at-table-p)
  16891. (org-table-paste-rectangle)
  16892. (org-paste-subtree arg)))
  16893. (defun org-edit-special (&optional arg)
  16894. "Call a special editor for the stuff at point.
  16895. When at a table, call the formula editor with `org-table-edit-formulas'.
  16896. When at the first line of an src example, call `org-edit-src-code'.
  16897. When in an #+include line, visit the include file. Otherwise call
  16898. `ffap' to visit the file at point."
  16899. (interactive)
  16900. ;; possibly prep session before editing source
  16901. (when arg
  16902. (let* ((info (org-babel-get-src-block-info))
  16903. (lang (nth 0 info))
  16904. (params (nth 2 info))
  16905. (session (cdr (assoc :session params))))
  16906. (when (and info session) ;; we are in a source-code block with a session
  16907. (funcall
  16908. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16909. (cond ;; proceed with `org-edit-special'
  16910. ((save-excursion
  16911. (beginning-of-line 1)
  16912. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16913. (find-file (org-trim (match-string 1))))
  16914. ((org-edit-src-code))
  16915. ((org-edit-fixed-width-region))
  16916. ((org-at-table.el-p)
  16917. (org-edit-src-code))
  16918. ((or (org-at-table-p)
  16919. (save-excursion
  16920. (beginning-of-line 1)
  16921. (looking-at "[ \t]*#\\+TBLFM:")))
  16922. (call-interactively 'org-table-edit-formulas))
  16923. (t (call-interactively 'ffap))))
  16924. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16925. (defun org-ctrl-c-ctrl-c (&optional arg)
  16926. "Set tags in headline, or update according to changed information at point.
  16927. This command does many different things, depending on context:
  16928. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16929. this is what we do.
  16930. - If the cursor is on a statistics cookie, update it.
  16931. - If the cursor is in a headline, prompt for tags and insert them
  16932. into the current line, aligned to `org-tags-column'. When called
  16933. with prefix arg, realign all tags in the current buffer.
  16934. - If the cursor is in one of the special #+KEYWORD lines, this
  16935. triggers scanning the buffer for these lines and updating the
  16936. information.
  16937. - If the cursor is inside a table, realign the table. This command
  16938. works even if the automatic table editor has been turned off.
  16939. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16940. the entire table.
  16941. - If the cursor is at a footnote reference or definition, jump to
  16942. the corresponding definition or references, respectively.
  16943. - If the cursor is a the beginning of a dynamic block, update it.
  16944. - If the current buffer is a capture buffer, close note and file it.
  16945. - If the cursor is on a <<<target>>>, update radio targets and
  16946. corresponding links in this buffer.
  16947. - If the cursor is on a numbered item in a plain list, renumber the
  16948. ordered list.
  16949. - If the cursor is on a checkbox, toggle it.
  16950. - If the cursor is on a code block, evaluate it. The variable
  16951. `org-confirm-babel-evaluate' can be used to control prompting
  16952. before code block evaluation, by default every code block
  16953. evaluation requires confirmation. Code block evaluation can be
  16954. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16955. (interactive "P")
  16956. (let ((org-enable-table-editor t))
  16957. (cond
  16958. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16959. org-occur-highlights
  16960. org-latex-fragment-image-overlays)
  16961. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16962. (org-remove-occur-highlights)
  16963. (org-remove-latex-fragment-image-overlays)
  16964. (message "Temporary highlights/overlays removed from current buffer"))
  16965. ((and (local-variable-p 'org-finish-function (current-buffer))
  16966. (fboundp org-finish-function))
  16967. (funcall org-finish-function))
  16968. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16969. ((org-in-regexp org-ts-regexp-both)
  16970. (org-timestamp-change 0 'day))
  16971. ((or (looking-at org-property-start-re)
  16972. (org-at-property-p))
  16973. (call-interactively 'org-property-action))
  16974. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  16975. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16976. (or (org-at-heading-p) (org-at-item-p)))
  16977. (call-interactively 'org-update-statistics-cookies))
  16978. ((org-at-heading-p) (call-interactively 'org-set-tags))
  16979. ((org-at-table.el-p)
  16980. (message "Use C-c ' to edit table.el tables"))
  16981. ((org-at-table-p)
  16982. (org-table-maybe-eval-formula)
  16983. (if arg
  16984. (call-interactively 'org-table-recalculate)
  16985. (org-table-maybe-recalculate-line))
  16986. (call-interactively 'org-table-align)
  16987. (orgtbl-send-table 'maybe))
  16988. ((or (org-footnote-at-reference-p)
  16989. (org-footnote-at-definition-p))
  16990. (call-interactively 'org-footnote-action))
  16991. ((org-at-item-checkbox-p)
  16992. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16993. ;; list only if at top item.
  16994. (let* ((cbox (match-string 1))
  16995. (struct (org-list-struct))
  16996. (old-struct (copy-tree struct))
  16997. (parents (org-list-parents-alist struct))
  16998. (orderedp (org-entry-get nil "ORDERED"))
  16999. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17000. block-item)
  17001. ;; Use a light version of `org-toggle-checkbox' to avoid
  17002. ;; computing list structure twice.
  17003. (let ((new-box (cond
  17004. ((equal arg '(16)) "[-]")
  17005. ((equal arg '(4)) nil)
  17006. ((equal "[X]" cbox) "[ ]")
  17007. (t "[X]"))))
  17008. (if (and firstp arg)
  17009. ;; If at first item of sub-list, remove check-box from
  17010. ;; every item at the same level.
  17011. (mapc
  17012. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17013. (org-list-get-all-items
  17014. (point-at-bol) struct (org-list-prevs-alist struct)))
  17015. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17016. ;; Replicate `org-list-write-struct', while grabbing a return
  17017. ;; value from `org-list-struct-fix-box'.
  17018. (org-list-struct-fix-ind struct parents 2)
  17019. (org-list-struct-fix-item-end struct)
  17020. (let ((prevs (org-list-prevs-alist struct)))
  17021. (org-list-struct-fix-bul struct prevs)
  17022. (org-list-struct-fix-ind struct parents)
  17023. (setq block-item
  17024. (org-list-struct-fix-box struct parents prevs orderedp)))
  17025. (org-list-struct-apply-struct struct old-struct)
  17026. (org-update-checkbox-count-maybe)
  17027. (when block-item
  17028. (message
  17029. "Checkboxes were removed due to unchecked box at line %d"
  17030. (org-current-line block-item)))
  17031. (when firstp (org-list-send-list 'maybe))))
  17032. ((org-at-item-p)
  17033. ;; Cursor at an item: repair list. Do checkbox related actions
  17034. ;; only if function was called with an argument. Send list only
  17035. ;; if at top item.
  17036. (let* ((struct (org-list-struct))
  17037. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17038. old-struct)
  17039. (when arg
  17040. (setq old-struct (copy-tree struct))
  17041. (if firstp
  17042. ;; If at first item of sub-list, add check-box to every
  17043. ;; item at the same level.
  17044. (mapc
  17045. (lambda (pos)
  17046. (unless (org-list-get-checkbox pos struct)
  17047. (org-list-set-checkbox pos struct "[ ]")))
  17048. (org-list-get-all-items
  17049. (point-at-bol) struct (org-list-prevs-alist struct)))
  17050. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17051. (org-list-write-struct
  17052. struct (org-list-parents-alist struct) old-struct)
  17053. (when arg (org-update-checkbox-count-maybe))
  17054. (when firstp (org-list-send-list 'maybe))))
  17055. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17056. ;; Dynamic block
  17057. (beginning-of-line 1)
  17058. (save-excursion (org-update-dblock)))
  17059. ((save-excursion
  17060. (beginning-of-line 1)
  17061. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  17062. (cond
  17063. ((equal (match-string 1) "TBLFM")
  17064. ;; Recalculate the table before this line
  17065. (save-excursion
  17066. (beginning-of-line 1)
  17067. (skip-chars-backward " \r\n\t")
  17068. (if (org-at-table-p)
  17069. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17070. (t
  17071. (let ((org-inhibit-startup-visibility-stuff t)
  17072. (org-startup-align-all-tables nil))
  17073. (when (boundp 'org-table-coordinate-overlays)
  17074. (mapc 'delete-overlay org-table-coordinate-overlays)
  17075. (setq org-table-coordinate-overlays nil))
  17076. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17077. (message "Local setup has been refreshed"))))
  17078. ((org-clock-update-time-maybe))
  17079. (t
  17080. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17081. (error "C-c C-c can do nothing useful at this location"))))))
  17082. (defun org-mode-restart ()
  17083. "Restart Org-mode, to scan again for special lines.
  17084. Also updates the keyword regular expressions."
  17085. (interactive)
  17086. (org-mode)
  17087. (message "Org-mode restarted"))
  17088. (defun org-kill-note-or-show-branches ()
  17089. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17090. (interactive)
  17091. (if (not org-finish-function)
  17092. (progn
  17093. (hide-subtree)
  17094. (call-interactively 'show-branches))
  17095. (let ((org-note-abort t))
  17096. (funcall org-finish-function))))
  17097. (defun org-return (&optional indent)
  17098. "Goto next table row or insert a newline.
  17099. Calls `org-table-next-row' or `newline', depending on context.
  17100. See the individual commands for more information."
  17101. (interactive)
  17102. (cond
  17103. ((or (bobp) (org-in-src-block-p))
  17104. (if indent (newline-and-indent) (newline)))
  17105. ((org-at-table-p)
  17106. (org-table-justify-field-maybe)
  17107. (call-interactively 'org-table-next-row))
  17108. ;; when `newline-and-indent' is called within a list, make sure
  17109. ;; text moved stays inside the item.
  17110. ((and (org-in-item-p) indent)
  17111. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17112. (progn
  17113. (save-match-data (newline))
  17114. (org-indent-line-to (length (match-string 0))))
  17115. (let ((ind (org-get-indentation)))
  17116. (newline)
  17117. (if (org-looking-back org-list-end-re)
  17118. (org-indent-line-function)
  17119. (org-indent-line-to ind)))))
  17120. ((and org-return-follows-link
  17121. (let ((tprop (get-text-property (point) 'face)))
  17122. (or (eq tprop 'org-link)
  17123. (and (listp tprop) (memq 'org-link tprop)))))
  17124. (call-interactively 'org-open-at-point))
  17125. ((and (org-at-heading-p)
  17126. (looking-at
  17127. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17128. (org-show-entry)
  17129. (end-of-line 1)
  17130. (newline))
  17131. (t (if indent (newline-and-indent) (newline)))))
  17132. (defun org-return-indent ()
  17133. "Goto next table row or insert a newline and indent.
  17134. Calls `org-table-next-row' or `newline-and-indent', depending on
  17135. context. See the individual commands for more information."
  17136. (interactive)
  17137. (org-return t))
  17138. (defun org-ctrl-c-star ()
  17139. "Compute table, or change heading status of lines.
  17140. Calls `org-table-recalculate' or `org-toggle-heading',
  17141. depending on context."
  17142. (interactive)
  17143. (cond
  17144. ((org-at-table-p)
  17145. (call-interactively 'org-table-recalculate))
  17146. (t
  17147. ;; Convert all lines in region to list items
  17148. (call-interactively 'org-toggle-heading))))
  17149. (defun org-ctrl-c-minus ()
  17150. "Insert separator line in table or modify bullet status of line.
  17151. Also turns a plain line or a region of lines into list items.
  17152. Calls `org-table-insert-hline', `org-toggle-item', or
  17153. `org-cycle-list-bullet', depending on context."
  17154. (interactive)
  17155. (cond
  17156. ((org-at-table-p)
  17157. (call-interactively 'org-table-insert-hline))
  17158. ((org-region-active-p)
  17159. (call-interactively 'org-toggle-item))
  17160. ((org-in-item-p)
  17161. (call-interactively 'org-cycle-list-bullet))
  17162. (t
  17163. (call-interactively 'org-toggle-item))))
  17164. (defun org-toggle-item (arg)
  17165. "Convert headings or normal lines to items, items to normal lines.
  17166. If there is no active region, only the current line is considered.
  17167. If the first non blank line in the region is an headline, convert
  17168. all headlines to items, shifting text accordingly.
  17169. If it is an item, convert all items to normal lines.
  17170. If it is normal text, change region into an item. With a prefix
  17171. argument ARG, change each line in region into an item."
  17172. (interactive "P")
  17173. (let ((shift-text
  17174. (function
  17175. ;; Shift text in current section to IND, from point to END.
  17176. ;; The function leaves point to END line.
  17177. (lambda (ind end)
  17178. (let ((min-i 1000) (end (copy-marker end)))
  17179. ;; First determine the minimum indentation (MIN-I) of
  17180. ;; the text.
  17181. (save-excursion
  17182. (catch 'exit
  17183. (while (< (point) end)
  17184. (let ((i (org-get-indentation)))
  17185. (cond
  17186. ;; Skip blank lines and inline tasks.
  17187. ((looking-at "^[ \t]*$"))
  17188. ((looking-at org-outline-regexp-bol))
  17189. ;; We can't find less than 0 indentation.
  17190. ((zerop i) (throw 'exit (setq min-i 0)))
  17191. ((< i min-i) (setq min-i i))))
  17192. (forward-line))))
  17193. ;; Then indent each line so that a line indented to
  17194. ;; MIN-I becomes indented to IND. Ignore blank lines
  17195. ;; and inline tasks in the process.
  17196. (let ((delta (- ind min-i)))
  17197. (while (< (point) end)
  17198. (unless (or (looking-at "^[ \t]*$")
  17199. (looking-at org-outline-regexp-bol))
  17200. (org-indent-line-to (+ (org-get-indentation) delta)))
  17201. (forward-line)))))))
  17202. (skip-blanks
  17203. (function
  17204. ;; Return beginning of first non-blank line, starting from
  17205. ;; line at POS.
  17206. (lambda (pos)
  17207. (save-excursion
  17208. (goto-char pos)
  17209. (skip-chars-forward " \r\t\n")
  17210. (point-at-bol)))))
  17211. beg end)
  17212. ;; Determine boundaries of changes.
  17213. (if (org-region-active-p)
  17214. (setq beg (funcall skip-blanks (region-beginning))
  17215. end (copy-marker (region-end)))
  17216. (setq beg (funcall skip-blanks (point-at-bol))
  17217. end (copy-marker (point-at-eol))))
  17218. ;; Depending on the starting line, choose an action on the text
  17219. ;; between BEG and END.
  17220. (org-with-limited-levels
  17221. (save-excursion
  17222. (goto-char beg)
  17223. (cond
  17224. ;; Case 1. Start at an item: de-itemize. Note that it only
  17225. ;; happens when a region is active: `org-ctrl-c-minus'
  17226. ;; would call `org-cycle-list-bullet' otherwise.
  17227. ((org-at-item-p)
  17228. (while (< (point) end)
  17229. (when (org-at-item-p)
  17230. (skip-chars-forward " \t")
  17231. (delete-region (point) (match-end 0)))
  17232. (forward-line)))
  17233. ;; Case 2. Start at an heading: convert to items.
  17234. ((org-at-heading-p)
  17235. (let* ((bul (org-list-bullet-string "-"))
  17236. (bul-len (length bul))
  17237. ;; Indentation of the first heading. It should be
  17238. ;; relative to the indentation of its parent, if any.
  17239. (start-ind (save-excursion
  17240. (cond
  17241. ((not org-adapt-indentation) 0)
  17242. ((not (outline-previous-heading)) 0)
  17243. (t (length (match-string 0))))))
  17244. ;; Level of first heading. Further headings will be
  17245. ;; compared to it to determine hierarchy in the list.
  17246. (ref-level (org-reduced-level (org-outline-level))))
  17247. (while (< (point) end)
  17248. (let* ((level (org-reduced-level (org-outline-level)))
  17249. (delta (max 0 (- level ref-level))))
  17250. ;; If current headline is less indented than the first
  17251. ;; one, set it as reference, in order to preserve
  17252. ;; subtrees.
  17253. (when (< level ref-level) (setq ref-level level))
  17254. (replace-match bul t t)
  17255. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17256. ;; Ensure all text down to END (or SECTION-END) belongs
  17257. ;; to the newly created item.
  17258. (let ((section-end (save-excursion
  17259. (or (outline-next-heading) (point)))))
  17260. (forward-line)
  17261. (funcall shift-text
  17262. (+ start-ind (* (1+ delta) bul-len))
  17263. (min end section-end)))))))
  17264. ;; Case 3. Normal line with ARG: turn each non-item line into
  17265. ;; an item.
  17266. (arg
  17267. (while (< (point) end)
  17268. (unless (or (org-at-heading-p) (org-at-item-p))
  17269. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17270. (replace-match
  17271. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17272. (forward-line)))
  17273. ;; Case 4. Normal line without ARG: make the first line of
  17274. ;; region an item, and shift indentation of others
  17275. ;; lines to set them as item's body.
  17276. (t (let* ((bul (org-list-bullet-string "-"))
  17277. (bul-len (length bul))
  17278. (ref-ind (org-get-indentation)))
  17279. (skip-chars-forward " \t")
  17280. (insert bul)
  17281. (forward-line)
  17282. (while (< (point) end)
  17283. ;; Ensure that lines less indented than first one
  17284. ;; still get included in item body.
  17285. (funcall shift-text
  17286. (+ ref-ind bul-len)
  17287. (min end (save-excursion (or (outline-next-heading)
  17288. (point)))))
  17289. (forward-line)))))))))
  17290. (defun org-toggle-heading (&optional nstars)
  17291. "Convert headings to normal text, or items or text to headings.
  17292. If there is no active region, only the current line is considered.
  17293. If the first non blank line is an headline, remove the stars from
  17294. all headlines in the region.
  17295. If it is a plain list item, turn all plain list items into headings.
  17296. If it is a normal line, turn each and every normal line (i.e. not
  17297. an heading or an item) in the region into a heading.
  17298. When converting a line into a heading, the number of stars is chosen
  17299. such that the lines become children of the current entry. However,
  17300. when a prefix argument is given, its value determines the number of
  17301. stars to add."
  17302. (interactive "P")
  17303. (let ((skip-blanks
  17304. (function
  17305. ;; Return beginning of first non-blank line, starting from
  17306. ;; line at POS.
  17307. (lambda (pos)
  17308. (save-excursion
  17309. (goto-char pos)
  17310. (skip-chars-forward " \r\t\n")
  17311. (point-at-bol)))))
  17312. beg end)
  17313. ;; Determine boundaries of changes. If region ends at a bol, do
  17314. ;; not consider the last line to be in the region.
  17315. (if (org-region-active-p)
  17316. (setq beg (funcall skip-blanks (region-beginning))
  17317. end (copy-marker (save-excursion
  17318. (goto-char (region-end))
  17319. (if (bolp) (point) (point-at-eol)))))
  17320. (setq beg (funcall skip-blanks (point-at-bol))
  17321. end (copy-marker (point-at-eol))))
  17322. ;; Ensure inline tasks don't count as headings.
  17323. (org-with-limited-levels
  17324. (save-excursion
  17325. (goto-char beg)
  17326. (cond
  17327. ;; Case 1. Started at an heading: de-star headings.
  17328. ((org-at-heading-p)
  17329. (while (< (point) end)
  17330. (when (org-at-heading-p t)
  17331. (looking-at org-outline-regexp) (replace-match ""))
  17332. (forward-line)))
  17333. ;; Case 2. Started at an item: change items into headlines.
  17334. ;; One star will be added by `org-list-to-subtree'.
  17335. ((org-at-item-p)
  17336. (let* ((stars (make-string
  17337. (if nstars
  17338. ;; subtract the star that will be added again by
  17339. ;; `org-list-to-subtree'
  17340. (1- (prefix-numeric-value current-prefix-arg))
  17341. (or (org-current-level) 0))
  17342. ?*))
  17343. (add-stars
  17344. (cond (nstars "") ; stars from prefix only
  17345. ((equal stars "") "") ; before first heading
  17346. (org-odd-levels-only "*") ; inside heading, odd
  17347. (t "")))) ; inside heading, oddeven
  17348. (while (< (point) end)
  17349. (when (org-at-item-p)
  17350. ;; Pay attention to cases when region ends before list.
  17351. (let* ((struct (org-list-struct))
  17352. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17353. (save-restriction
  17354. (narrow-to-region (point) list-end)
  17355. (insert
  17356. (org-list-to-subtree
  17357. (org-list-parse-list t)
  17358. '(:istart (concat stars add-stars (funcall get-stars depth))
  17359. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17360. (forward-line))))
  17361. ;; Case 3. Started at normal text: make every line an heading,
  17362. ;; skipping headlines and items.
  17363. (t (let* ((stars (make-string
  17364. (if nstars
  17365. (prefix-numeric-value current-prefix-arg)
  17366. (or (org-current-level) 0))
  17367. ?*))
  17368. (add-stars
  17369. (cond (nstars "") ; stars from prefix only
  17370. ((equal stars "") "*") ; before first heading
  17371. (org-odd-levels-only "**") ; inside heading, odd
  17372. (t "*"))) ; inside heading, oddeven
  17373. (rpl (concat stars add-stars " ")))
  17374. (while (< (point) end)
  17375. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17376. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17377. (replace-match (concat rpl (match-string 2))))
  17378. (forward-line)))))))))
  17379. (defun org-meta-return (&optional arg)
  17380. "Insert a new heading or wrap a region in a table.
  17381. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17382. See the individual commands for more information."
  17383. (interactive "P")
  17384. (cond
  17385. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17386. ((or (org-at-drawer-p) (org-at-property-p))
  17387. (newline-and-indent))
  17388. ((org-at-table-p)
  17389. (call-interactively 'org-table-wrap-region))
  17390. (t (call-interactively 'org-insert-heading))))
  17391. ;;; Menu entries
  17392. ;; Define the Org-mode menus
  17393. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17394. '("Tbl"
  17395. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17396. ["Next Field" org-cycle (org-at-table-p)]
  17397. ["Previous Field" org-shifttab (org-at-table-p)]
  17398. ["Next Row" org-return (org-at-table-p)]
  17399. "--"
  17400. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17401. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17402. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17403. "--"
  17404. ("Column"
  17405. ["Move Column Left" org-metaleft (org-at-table-p)]
  17406. ["Move Column Right" org-metaright (org-at-table-p)]
  17407. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17408. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17409. ("Row"
  17410. ["Move Row Up" org-metaup (org-at-table-p)]
  17411. ["Move Row Down" org-metadown (org-at-table-p)]
  17412. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17413. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17414. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17415. "--"
  17416. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17417. ("Rectangle"
  17418. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17419. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17420. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17421. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17422. "--"
  17423. ("Calculate"
  17424. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17425. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17426. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17427. "--"
  17428. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17429. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17430. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17431. "--"
  17432. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17433. "--"
  17434. ["Sum Column/Rectangle" org-table-sum
  17435. (or (org-at-table-p) (org-region-active-p))]
  17436. ["Which Column?" org-table-current-column (org-at-table-p)])
  17437. ["Debug Formulas"
  17438. org-table-toggle-formula-debugger
  17439. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17440. ["Show Col/Row Numbers"
  17441. org-table-toggle-coordinate-overlays
  17442. :style toggle
  17443. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17444. "--"
  17445. ["Create" org-table-create (and (not (org-at-table-p))
  17446. org-enable-table-editor)]
  17447. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17448. ["Import from File" org-table-import (not (org-at-table-p))]
  17449. ["Export to File" org-table-export (org-at-table-p)]
  17450. "--"
  17451. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17452. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17453. '("Org"
  17454. ("Show/Hide"
  17455. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17456. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17457. ["Sparse Tree..." org-sparse-tree t]
  17458. ["Reveal Context" org-reveal t]
  17459. ["Show All" show-all t]
  17460. "--"
  17461. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17462. "--"
  17463. ["New Heading" org-insert-heading t]
  17464. ("Navigate Headings"
  17465. ["Up" outline-up-heading t]
  17466. ["Next" outline-next-visible-heading t]
  17467. ["Previous" outline-previous-visible-heading t]
  17468. ["Next Same Level" outline-forward-same-level t]
  17469. ["Previous Same Level" outline-backward-same-level t]
  17470. "--"
  17471. ["Jump" org-goto t])
  17472. ("Edit Structure"
  17473. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17474. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17475. "--"
  17476. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17477. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17478. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17479. "--"
  17480. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17481. "--"
  17482. ["Copy visible text" org-copy-visible t]
  17483. "--"
  17484. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17485. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17486. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17487. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17488. "--"
  17489. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17490. "--"
  17491. ["Convert to odd levels" org-convert-to-odd-levels t]
  17492. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17493. ("Editing"
  17494. ["Emphasis..." org-emphasize t]
  17495. ["Edit Source Example" org-edit-special t]
  17496. "--"
  17497. ["Footnote new/jump" org-footnote-action t]
  17498. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17499. ("Archive"
  17500. ["Archive (default method)" org-archive-subtree-default t]
  17501. "--"
  17502. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17503. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17504. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17505. )
  17506. "--"
  17507. ("Hyperlinks"
  17508. ["Store Link (Global)" org-store-link t]
  17509. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17510. ["Insert Link" org-insert-link t]
  17511. ["Follow Link" org-open-at-point t]
  17512. "--"
  17513. ["Next link" org-next-link t]
  17514. ["Previous link" org-previous-link t]
  17515. "--"
  17516. ["Descriptive Links"
  17517. org-toggle-link-display
  17518. :style radio
  17519. :selected org-descriptive-links
  17520. ]
  17521. ["Literal Links"
  17522. org-toggle-link-display
  17523. :style radio
  17524. :selected (not org-descriptive-links)])
  17525. "--"
  17526. ("TODO Lists"
  17527. ["TODO/DONE/-" org-todo t]
  17528. ("Select keyword"
  17529. ["Next keyword" org-shiftright (org-at-heading-p)]
  17530. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17531. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17532. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17533. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17534. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17535. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17536. "--"
  17537. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17538. :selected org-enforce-todo-dependencies :style toggle :active t]
  17539. "Settings for tree at point"
  17540. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17541. :selected (org-entry-get nil "ORDERED")
  17542. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17543. ["Do Children parallel" org-toggle-ordered-property :style radio
  17544. :selected (not (org-entry-get nil "ORDERED"))
  17545. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17546. "--"
  17547. ["Set Priority" org-priority t]
  17548. ["Priority Up" org-shiftup t]
  17549. ["Priority Down" org-shiftdown t]
  17550. "--"
  17551. ["Get news from all feeds" org-feed-update-all t]
  17552. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17553. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17554. ("TAGS and Properties"
  17555. ["Set Tags" org-set-tags-command t]
  17556. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17557. "--"
  17558. ["Set property" org-set-property t]
  17559. ["Column view of properties" org-columns t]
  17560. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17561. ("Dates and Scheduling"
  17562. ["Timestamp" org-time-stamp t]
  17563. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17564. ("Change Date"
  17565. ["1 Day Later" org-shiftright t]
  17566. ["1 Day Earlier" org-shiftleft t]
  17567. ["1 ... Later" org-shiftup t]
  17568. ["1 ... Earlier" org-shiftdown t])
  17569. ["Compute Time Range" org-evaluate-time-range t]
  17570. ["Schedule Item" org-schedule t]
  17571. ["Deadline" org-deadline t]
  17572. "--"
  17573. ["Custom time format" org-toggle-time-stamp-overlays
  17574. :style radio :selected org-display-custom-times]
  17575. "--"
  17576. ["Goto Calendar" org-goto-calendar t]
  17577. ["Date from Calendar" org-date-from-calendar t]
  17578. "--"
  17579. ["Start/Restart Timer" org-timer-start t]
  17580. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17581. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17582. ["Insert Timer String" org-timer t]
  17583. ["Insert Timer Item" org-timer-item t])
  17584. ("Logging work"
  17585. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17586. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17587. ["Clock out" org-clock-out t]
  17588. ["Clock cancel" org-clock-cancel t]
  17589. "--"
  17590. ["Mark as default task" org-clock-mark-default-task t]
  17591. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17592. ["Goto running clock" org-clock-goto t]
  17593. "--"
  17594. ["Display times" org-clock-display t]
  17595. ["Create clock table" org-clock-report t]
  17596. "--"
  17597. ["Record DONE time"
  17598. (progn (setq org-log-done (not org-log-done))
  17599. (message "Switching to %s will %s record a timestamp"
  17600. (car org-done-keywords)
  17601. (if org-log-done "automatically" "not")))
  17602. :style toggle :selected org-log-done])
  17603. "--"
  17604. ["Agenda Command..." org-agenda t]
  17605. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17606. ("File List for Agenda")
  17607. ("Special views current file"
  17608. ["TODO Tree" org-show-todo-tree t]
  17609. ["Check Deadlines" org-check-deadlines t]
  17610. ["Timeline" org-timeline t]
  17611. ["Tags/Property tree" org-match-sparse-tree t])
  17612. "--"
  17613. ["Export/Publish..." org-export t]
  17614. ("LaTeX"
  17615. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17616. :selected org-cdlatex-mode]
  17617. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17618. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17619. ["Modify math symbol" org-cdlatex-math-modify
  17620. (org-inside-LaTeX-fragment-p)]
  17621. ["Insert citation" org-reftex-citation t]
  17622. "--"
  17623. ["Template for BEAMER" org-insert-beamer-options-template t])
  17624. "--"
  17625. ("MobileOrg"
  17626. ["Push Files and Views" org-mobile-push t]
  17627. ["Get Captured and Flagged" org-mobile-pull t]
  17628. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17629. "--"
  17630. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17631. "--"
  17632. ("Documentation"
  17633. ["Show Version" org-version t]
  17634. ["Info Documentation" org-info t])
  17635. ("Customize"
  17636. ["Browse Org Group" org-customize t]
  17637. "--"
  17638. ["Expand This Menu" org-create-customize-menu
  17639. (fboundp 'customize-menu-create)])
  17640. ["Send bug report" org-submit-bug-report t]
  17641. "--"
  17642. ("Refresh/Reload"
  17643. ["Refresh setup current buffer" org-mode-restart t]
  17644. ["Reload Org (after update)" org-reload t]
  17645. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17646. ))
  17647. (defun org-info (&optional node)
  17648. "Read documentation for Org-mode in the info system.
  17649. With optional NODE, go directly to that node."
  17650. (interactive)
  17651. (info (format "(org)%s" (or node ""))))
  17652. ;;;###autoload
  17653. (defun org-submit-bug-report ()
  17654. "Submit a bug report on Org-mode via mail.
  17655. Don't hesitate to report any problems or inaccurate documentation.
  17656. If you don't have setup sending mail from (X)Emacs, please copy the
  17657. output buffer into your mail program, as it gives us important
  17658. information about your Org-mode version and configuration."
  17659. (interactive)
  17660. (require 'reporter)
  17661. (org-load-modules-maybe)
  17662. (org-require-autoloaded-modules)
  17663. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17664. (reporter-submit-bug-report
  17665. "emacs-orgmode@gnu.org"
  17666. (org-version)
  17667. (let (list)
  17668. (save-window-excursion
  17669. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17670. (delete-other-windows)
  17671. (erase-buffer)
  17672. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17673. We would like to add your full Org-mode and Outline configuration to the
  17674. bug report. This greatly simplifies the work of the maintainer and
  17675. other experts on the mailing list.
  17676. HOWEVER, some variables you have customized may contain private
  17677. information. The names of customers, colleagues, or friends, might
  17678. appear in the form of file names, tags, todo states, or search strings.
  17679. If you answer yes to the prompt, you might want to check and remove
  17680. such private information before sending the email.")
  17681. (add-text-properties (point-min) (point-max) '(face org-warning))
  17682. (when (yes-or-no-p "Include your Org-mode configuration ")
  17683. (mapatoms
  17684. (lambda (v)
  17685. (and (boundp v)
  17686. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17687. (or (and (symbol-value v)
  17688. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17689. (and
  17690. (get v 'custom-type) (get v 'standard-value)
  17691. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17692. (push v list)))))
  17693. (kill-buffer (get-buffer "*Warn about privacy*"))
  17694. list))
  17695. nil nil
  17696. "Remember to cover the basics, that is, what you expected to happen and
  17697. what in fact did happen. You don't know how to make a good report? See
  17698. http://orgmode.org/manual/Feedback.html#Feedback
  17699. Your bug report will be posted to the Org-mode mailing list.
  17700. ------------------------------------------------------------------------")
  17701. (save-excursion
  17702. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17703. (replace-match "\\1Bug: \\3 [\\2]")))))
  17704. (defun org-install-agenda-files-menu ()
  17705. (let ((bl (buffer-list)))
  17706. (save-excursion
  17707. (while bl
  17708. (set-buffer (pop bl))
  17709. (if (eq major-mode 'org-mode) (setq bl nil)))
  17710. (when (eq major-mode 'org-mode)
  17711. (easy-menu-change
  17712. '("Org") "File List for Agenda"
  17713. (append
  17714. (list
  17715. ["Edit File List" (org-edit-agenda-file-list) t]
  17716. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17717. ["Remove Current File from List" org-remove-file t]
  17718. ["Cycle through agenda files" org-cycle-agenda-files t]
  17719. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17720. "--")
  17721. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17722. ;;;; Documentation
  17723. ;;;###autoload
  17724. (defun org-require-autoloaded-modules ()
  17725. (interactive)
  17726. (mapc 'require
  17727. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17728. org-docbook org-exp org-html org-icalendar
  17729. org-id org-latex
  17730. org-publish org-remember org-table
  17731. org-timer org-xoxo)))
  17732. ;;;###autoload
  17733. (defun org-reload (&optional uncompiled)
  17734. "Reload all org lisp files.
  17735. With prefix arg UNCOMPILED, load the uncompiled versions."
  17736. (interactive "P")
  17737. (require 'find-func)
  17738. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17739. (dir-org (file-name-directory (org-find-library-name "org")))
  17740. (dir-org-contrib (ignore-errors
  17741. (file-name-directory
  17742. (org-find-library-name "org-contribdir"))))
  17743. (babel-files
  17744. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17745. (append (list nil "comint" "eval" "exp" "keys"
  17746. "lob" "ref" "table" "tangle")
  17747. (delq nil
  17748. (mapcar
  17749. (lambda (lang)
  17750. (when (cdr lang) (symbol-name (car lang))))
  17751. org-babel-load-languages)))))
  17752. (files
  17753. (append (directory-files dir-org t file-re)
  17754. babel-files
  17755. (and dir-org-contrib
  17756. (directory-files dir-org-contrib t file-re))))
  17757. (remove-re (concat (if (featurep 'xemacs)
  17758. "org-colview" "org-colview-xemacs")
  17759. "\\'")))
  17760. (setq files (mapcar 'file-name-sans-extension files))
  17761. (setq files (mapcar
  17762. (lambda (x) (if (string-match remove-re x) nil x))
  17763. files))
  17764. (setq files (delq nil files))
  17765. (mapc
  17766. (lambda (f)
  17767. (when (featurep (intern (file-name-nondirectory f)))
  17768. (if (and (not uncompiled)
  17769. (file-exists-p (concat f ".elc")))
  17770. (load (concat f ".elc") nil nil t)
  17771. (load (concat f ".el") nil nil t))))
  17772. files))
  17773. (org-version))
  17774. ;;;###autoload
  17775. (defun org-customize ()
  17776. "Call the customize function with org as argument."
  17777. (interactive)
  17778. (org-load-modules-maybe)
  17779. (org-require-autoloaded-modules)
  17780. (customize-browse 'org))
  17781. (defun org-create-customize-menu ()
  17782. "Create a full customization menu for Org-mode, insert it into the menu."
  17783. (interactive)
  17784. (org-load-modules-maybe)
  17785. (org-require-autoloaded-modules)
  17786. (if (fboundp 'customize-menu-create)
  17787. (progn
  17788. (easy-menu-change
  17789. '("Org") "Customize"
  17790. `(["Browse Org group" org-customize t]
  17791. "--"
  17792. ,(customize-menu-create 'org)
  17793. ["Set" Custom-set t]
  17794. ["Save" Custom-save t]
  17795. ["Reset to Current" Custom-reset-current t]
  17796. ["Reset to Saved" Custom-reset-saved t]
  17797. ["Reset to Standard Settings" Custom-reset-standard t]))
  17798. (message "\"Org\"-menu now contains full customization menu"))
  17799. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17800. ;;;; Miscellaneous stuff
  17801. ;;; Generally useful functions
  17802. (defun org-get-at-bol (property)
  17803. "Get text property PROPERTY at beginning of line."
  17804. (get-text-property (point-at-bol) property))
  17805. (defun org-find-text-property-in-string (prop s)
  17806. "Return the first non-nil value of property PROP in string S."
  17807. (or (get-text-property 0 prop s)
  17808. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17809. prop s)))
  17810. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17811. "Display the given MESSAGE as a warning."
  17812. (if (fboundp 'display-warning)
  17813. (display-warning 'org message
  17814. (if (featurep 'xemacs) 'warning :warning))
  17815. (let ((buf (get-buffer-create "*Org warnings*")))
  17816. (with-current-buffer buf
  17817. (goto-char (point-max))
  17818. (insert "Warning (Org): " message)
  17819. (unless (bolp)
  17820. (newline)))
  17821. (display-buffer buf)
  17822. (sit-for 0))))
  17823. (defun org-eval (form)
  17824. "Eval FORM and return result."
  17825. (condition-case error
  17826. (eval form)
  17827. (error (format "%%![Error: %s]" error))))
  17828. (defun org-in-clocktable-p ()
  17829. "Check if the cursor is in a clocktable."
  17830. (let ((pos (point)) start)
  17831. (save-excursion
  17832. (end-of-line 1)
  17833. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  17834. (setq start (match-beginning 0))
  17835. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  17836. (>= (match-end 0) pos)
  17837. start))))
  17838. (defun org-in-commented-line ()
  17839. "Is point in a line starting with `#'?"
  17840. (equal (char-after (point-at-bol)) ?#))
  17841. (defun org-in-indented-comment-line ()
  17842. "Is point in a line starting with `#' after some white space?"
  17843. (save-excursion
  17844. (save-match-data
  17845. (goto-char (point-at-bol))
  17846. (looking-at "[ \t]*#"))))
  17847. (defun org-in-verbatim-emphasis ()
  17848. (save-match-data
  17849. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17850. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17851. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17852. (if (and marker (marker-buffer marker)
  17853. (buffer-live-p (marker-buffer marker)))
  17854. (progn
  17855. (org-pop-to-buffer-same-window (marker-buffer marker))
  17856. (if (or (> marker (point-max)) (< marker (point-min)))
  17857. (widen))
  17858. (goto-char marker)
  17859. (org-show-context 'org-goto))
  17860. (if bookmark
  17861. (bookmark-jump bookmark)
  17862. (error "Cannot find location"))))
  17863. (defun org-quote-csv-field (s)
  17864. "Quote field for inclusion in CSV material."
  17865. (if (string-match "[\",]" s)
  17866. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17867. s))
  17868. (defun org-force-self-insert (N)
  17869. "Needed to enforce self-insert under remapping."
  17870. (interactive "p")
  17871. (self-insert-command N))
  17872. (defun org-string-width (s)
  17873. "Compute width of string, ignoring invisible characters.
  17874. This ignores character with invisibility property `org-link', and also
  17875. characters with property `org-cwidth', because these will become invisible
  17876. upon the next fontification round."
  17877. (let (b l)
  17878. (when (or (eq t buffer-invisibility-spec)
  17879. (assq 'org-link buffer-invisibility-spec))
  17880. (while (setq b (text-property-any 0 (length s)
  17881. 'invisible 'org-link s))
  17882. (setq s (concat (substring s 0 b)
  17883. (substring s (or (next-single-property-change
  17884. b 'invisible s) (length s)))))))
  17885. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17886. (setq s (concat (substring s 0 b)
  17887. (substring s (or (next-single-property-change
  17888. b 'org-cwidth s) (length s))))))
  17889. (setq l (string-width s) b -1)
  17890. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17891. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17892. l))
  17893. (defun org-shorten-string (s maxlength)
  17894. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17895. If the string is shorter or has length MAXLENGTH, just return the
  17896. original string. If it is longer, the functions finds a space in the
  17897. string, breaks this string off at that locations and adds three dots
  17898. as ellipsis. Including the ellipsis, the string will not be longer
  17899. than MAXLENGTH. If finding a good breaking point in the string does
  17900. not work, the string is just chopped off in the middle of a word
  17901. if necessary."
  17902. (if (<= (length s) maxlength)
  17903. s
  17904. (let* ((n (max (- maxlength 4) 1))
  17905. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17906. (if (string-match re s)
  17907. (concat (match-string 1 s) "...")
  17908. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17909. (defun org-get-indentation (&optional line)
  17910. "Get the indentation of the current line, interpreting tabs.
  17911. When LINE is given, assume it represents a line and compute its indentation."
  17912. (if line
  17913. (if (string-match "^ *" (org-remove-tabs line))
  17914. (match-end 0))
  17915. (save-excursion
  17916. (beginning-of-line 1)
  17917. (skip-chars-forward " \t")
  17918. (current-column))))
  17919. (defun org-get-string-indentation (s)
  17920. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17921. (let ((n -1) (i 0) (w tab-width) c)
  17922. (catch 'exit
  17923. (while (< (setq n (1+ n)) (length s))
  17924. (setq c (aref s n))
  17925. (cond ((= c ?\ ) (setq i (1+ i)))
  17926. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17927. (t (throw 'exit t)))))
  17928. i))
  17929. (defun org-remove-tabs (s &optional width)
  17930. "Replace tabulators in S with spaces.
  17931. Assumes that s is a single line, starting in column 0."
  17932. (setq width (or width tab-width))
  17933. (while (string-match "\t" s)
  17934. (setq s (replace-match
  17935. (make-string
  17936. (- (* width (/ (+ (match-beginning 0) width) width))
  17937. (match-beginning 0)) ?\ )
  17938. t t s)))
  17939. s)
  17940. (defun org-fix-indentation (line ind)
  17941. "Fix indentation in LINE.
  17942. IND is a cons cell with target and minimum indentation.
  17943. If the current indentation in LINE is smaller than the minimum,
  17944. leave it alone. If it is larger than ind, set it to the target."
  17945. (let* ((l (org-remove-tabs line))
  17946. (i (org-get-indentation l))
  17947. (i1 (car ind)) (i2 (cdr ind)))
  17948. (if (>= i i2) (setq l (substring line i2)))
  17949. (if (> i1 0)
  17950. (concat (make-string i1 ?\ ) l)
  17951. l)))
  17952. (defun org-remove-indentation (code &optional n)
  17953. "Remove the maximum common indentation from the lines in CODE.
  17954. N may optionally be the number of spaces to remove."
  17955. (with-temp-buffer
  17956. (insert code)
  17957. (org-do-remove-indentation n)
  17958. (buffer-string)))
  17959. (defun org-do-remove-indentation (&optional n)
  17960. "Remove the maximum common indentation from the buffer."
  17961. (untabify (point-min) (point-max))
  17962. (let ((min 10000) re)
  17963. (if n
  17964. (setq min n)
  17965. (goto-char (point-min))
  17966. (while (re-search-forward "^ *[^ \n]" nil t)
  17967. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17968. (unless (or (= min 0) (= min 10000))
  17969. (setq re (format "^ \\{%d\\}" min))
  17970. (goto-char (point-min))
  17971. (while (re-search-forward re nil t)
  17972. (replace-match "")
  17973. (end-of-line 1))
  17974. min)))
  17975. (defun org-fill-template (template alist)
  17976. "Find each %key of ALIST in TEMPLATE and replace it."
  17977. (let ((case-fold-search nil)
  17978. entry key value)
  17979. (setq alist (sort (copy-sequence alist)
  17980. (lambda (a b) (< (length (car a)) (length (car b))))))
  17981. (while (setq entry (pop alist))
  17982. (setq template
  17983. (replace-regexp-in-string
  17984. (concat "%" (regexp-quote (car entry)))
  17985. (cdr entry) template t t)))
  17986. template))
  17987. (defun org-base-buffer (buffer)
  17988. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17989. (if (not buffer)
  17990. buffer
  17991. (or (buffer-base-buffer buffer)
  17992. buffer)))
  17993. (defun org-trim (s)
  17994. "Remove whitespace at beginning and end of string."
  17995. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17996. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17997. s)
  17998. (defun org-wrap (string &optional width lines)
  17999. "Wrap string to either a number of lines, or a width in characters.
  18000. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18001. that costs. If there is a word longer than WIDTH, the text is actually
  18002. wrapped to the length of that word.
  18003. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18004. many lines, whatever width that takes.
  18005. The return value is a list of lines, without newlines at the end."
  18006. (let* ((words (org-split-string string "[ \t\n]+"))
  18007. (maxword (apply 'max (mapcar 'org-string-width words)))
  18008. w ll)
  18009. (cond (width
  18010. (org-do-wrap words (max maxword width)))
  18011. (lines
  18012. (setq w maxword)
  18013. (setq ll (org-do-wrap words maxword))
  18014. (if (<= (length ll) lines)
  18015. ll
  18016. (setq ll words)
  18017. (while (> (length ll) lines)
  18018. (setq w (1+ w))
  18019. (setq ll (org-do-wrap words w)))
  18020. ll))
  18021. (t (error "Cannot wrap this")))))
  18022. (defun org-do-wrap (words width)
  18023. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18024. (let (lines line)
  18025. (while words
  18026. (setq line (pop words))
  18027. (while (and words (< (+ (length line) (length (car words))) width))
  18028. (setq line (concat line " " (pop words))))
  18029. (setq lines (push line lines)))
  18030. (nreverse lines)))
  18031. (defun org-split-string (string &optional separators)
  18032. "Splits STRING into substrings at SEPARATORS.
  18033. No empty strings are returned if there are matches at the beginning
  18034. and end of string."
  18035. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18036. (start 0)
  18037. notfirst
  18038. (list nil))
  18039. (while (and (string-match rexp string
  18040. (if (and notfirst
  18041. (= start (match-beginning 0))
  18042. (< start (length string)))
  18043. (1+ start) start))
  18044. (< (match-beginning 0) (length string)))
  18045. (setq notfirst t)
  18046. (or (eq (match-beginning 0) 0)
  18047. (and (eq (match-beginning 0) (match-end 0))
  18048. (eq (match-beginning 0) start))
  18049. (setq list
  18050. (cons (substring string start (match-beginning 0))
  18051. list)))
  18052. (setq start (match-end 0)))
  18053. (or (eq start (length string))
  18054. (setq list
  18055. (cons (substring string start)
  18056. list)))
  18057. (nreverse list)))
  18058. (defun org-quote-vert (s)
  18059. "Replace \"|\" with \"\\vert\"."
  18060. (while (string-match "|" s)
  18061. (setq s (replace-match "\\vert" t t s)))
  18062. s)
  18063. (defun org-uuidgen-p (s)
  18064. "Is S an ID created by UUIDGEN?"
  18065. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18066. (defun org-in-src-block-p nil
  18067. "Whether point is in a code source block."
  18068. (let (ov)
  18069. (when (setq ov (overlays-at (point)))
  18070. (memq 'org-block-background
  18071. (overlay-properties
  18072. (car ov))))))
  18073. (defun org-context ()
  18074. "Return a list of contexts of the current cursor position.
  18075. If several contexts apply, all are returned.
  18076. Each context entry is a list with a symbol naming the context, and
  18077. two positions indicating start and end of the context. Possible
  18078. contexts are:
  18079. :headline anywhere in a headline
  18080. :headline-stars on the leading stars in a headline
  18081. :todo-keyword on a TODO keyword (including DONE) in a headline
  18082. :tags on the TAGS in a headline
  18083. :priority on the priority cookie in a headline
  18084. :item on the first line of a plain list item
  18085. :item-bullet on the bullet/number of a plain list item
  18086. :checkbox on the checkbox in a plain list item
  18087. :table in an org-mode table
  18088. :table-special on a special filed in a table
  18089. :table-table in a table.el table
  18090. :clocktable in a clocktable
  18091. :src-block in a source block
  18092. :link on a hyperlink
  18093. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18094. :target on a <<target>>
  18095. :radio-target on a <<<radio-target>>>
  18096. :latex-fragment on a LaTeX fragment
  18097. :latex-preview on a LaTeX fragment with overlaid preview image
  18098. This function expects the position to be visible because it uses font-lock
  18099. faces as a help to recognize the following contexts: :table-special, :link,
  18100. and :keyword."
  18101. (let* ((f (get-text-property (point) 'face))
  18102. (faces (if (listp f) f (list f)))
  18103. (case-fold-search t)
  18104. (p (point)) clist o)
  18105. ;; First the large context
  18106. (cond
  18107. ((org-at-heading-p t)
  18108. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18109. (when (progn
  18110. (beginning-of-line 1)
  18111. (looking-at org-todo-line-tags-regexp))
  18112. (push (org-point-in-group p 1 :headline-stars) clist)
  18113. (push (org-point-in-group p 2 :todo-keyword) clist)
  18114. (push (org-point-in-group p 4 :tags) clist))
  18115. (goto-char p)
  18116. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18117. (if (looking-at "\\[#[A-Z0-9]\\]")
  18118. (push (org-point-in-group p 0 :priority) clist)))
  18119. ((org-at-item-p)
  18120. (push (org-point-in-group p 2 :item-bullet) clist)
  18121. (push (list :item (point-at-bol)
  18122. (save-excursion (org-end-of-item) (point)))
  18123. clist)
  18124. (and (org-at-item-checkbox-p)
  18125. (push (org-point-in-group p 0 :checkbox) clist)))
  18126. ((org-at-table-p)
  18127. (push (list :table (org-table-begin) (org-table-end)) clist)
  18128. (if (memq 'org-formula faces)
  18129. (push (list :table-special
  18130. (previous-single-property-change p 'face)
  18131. (next-single-property-change p 'face)) clist)))
  18132. ((org-at-table-p 'any)
  18133. (push (list :table-table) clist)))
  18134. (goto-char p)
  18135. ;; New the "medium" contexts: clocktables, source blocks
  18136. (cond ((org-in-clocktable-p)
  18137. (push (list :clocktable
  18138. (and (or (looking-at "#\\+BEGIN: clocktable")
  18139. (search-backward "#+BEGIN: clocktable" nil t))
  18140. (match-beginning 0))
  18141. (and (re-search-forward "#\\+END:?" nil t)
  18142. (match-end 0))) clist))
  18143. ((org-in-src-block-p)
  18144. (push (list :src-block
  18145. (and (or (looking-at "#\\+BEGIN_SRC")
  18146. (search-backward "#+BEGIN_SRC" nil t))
  18147. (match-beginning 0))
  18148. (and (search-forward "#+END_SRC" nil t)
  18149. (match-beginning 0))) clist)))
  18150. (goto-char p)
  18151. ;; Now the small context
  18152. (cond
  18153. ((org-at-timestamp-p)
  18154. (push (org-point-in-group p 0 :timestamp) clist))
  18155. ((memq 'org-link faces)
  18156. (push (list :link
  18157. (previous-single-property-change p 'face)
  18158. (next-single-property-change p 'face)) clist))
  18159. ((memq 'org-special-keyword faces)
  18160. (push (list :keyword
  18161. (previous-single-property-change p 'face)
  18162. (next-single-property-change p 'face)) clist))
  18163. ((org-at-target-p)
  18164. (push (org-point-in-group p 0 :target) clist)
  18165. (goto-char (1- (match-beginning 0)))
  18166. (if (looking-at org-radio-target-regexp)
  18167. (push (org-point-in-group p 0 :radio-target) clist))
  18168. (goto-char p))
  18169. ((setq o (car (delq nil
  18170. (mapcar
  18171. (lambda (x)
  18172. (if (memq x org-latex-fragment-image-overlays) x))
  18173. (overlays-at (point))))))
  18174. (push (list :latex-fragment
  18175. (overlay-start o) (overlay-end o)) clist)
  18176. (push (list :latex-preview
  18177. (overlay-start o) (overlay-end o)) clist))
  18178. ((org-inside-LaTeX-fragment-p)
  18179. ;; FIXME: positions wrong.
  18180. (push (list :latex-fragment (point) (point)) clist)))
  18181. (setq clist (nreverse (delq nil clist)))
  18182. clist))
  18183. ;; FIXME: Compare with at-regexp-p Do we need both?
  18184. (defun org-in-regexp (re &optional nlines visually)
  18185. "Check if point is inside a match of regexp.
  18186. Normally only the current line is checked, but you can include NLINES extra
  18187. lines both before and after point into the search.
  18188. If VISUALLY is set, require that the cursor is not after the match but
  18189. really on, so that the block visually is on the match."
  18190. (catch 'exit
  18191. (let ((pos (point))
  18192. (eol (point-at-eol (+ 1 (or nlines 0))))
  18193. (inc (if visually 1 0)))
  18194. (save-excursion
  18195. (beginning-of-line (- 1 (or nlines 0)))
  18196. (while (re-search-forward re eol t)
  18197. (if (and (<= (match-beginning 0) pos)
  18198. (>= (+ inc (match-end 0)) pos))
  18199. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18200. (defun org-at-regexp-p (regexp)
  18201. "Is point inside a match of REGEXP in the current line?"
  18202. (catch 'exit
  18203. (save-excursion
  18204. (let ((pos (point)) (end (point-at-eol)))
  18205. (beginning-of-line 1)
  18206. (while (re-search-forward regexp end t)
  18207. (if (and (<= (match-beginning 0) pos)
  18208. (>= (match-end 0) pos))
  18209. (throw 'exit t)))
  18210. nil))))
  18211. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18212. "Non-nil when point is between matches of START-RE and END-RE.
  18213. Also return a non-nil value when point is on one of the matches.
  18214. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18215. buffer positions. Default values are the positions of headlines
  18216. surrounding the point.
  18217. The functions returns a cons cell whose car (resp. cdr) is the
  18218. position before START-RE (resp. after END-RE)."
  18219. (save-match-data
  18220. (let ((pos (point))
  18221. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18222. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18223. beg end)
  18224. (save-excursion
  18225. ;; Point is on a block when on START-RE or if START-RE can be
  18226. ;; found before it...
  18227. (and (or (org-at-regexp-p start-re)
  18228. (re-search-backward start-re limit-up t))
  18229. (setq beg (match-beginning 0))
  18230. ;; ... and END-RE after it...
  18231. (goto-char (match-end 0))
  18232. (re-search-forward end-re limit-down t)
  18233. (> (setq end (match-end 0)) pos)
  18234. ;; ... without another START-RE in-between.
  18235. (goto-char (match-beginning 0))
  18236. (not (re-search-backward start-re (1+ beg) t))
  18237. ;; Return value.
  18238. (cons beg end))))))
  18239. (defun org-in-block-p (names)
  18240. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18241. NAMES is a list of strings containing names of blocks.
  18242. Return first block name matched, or nil. Beware that in case of
  18243. nested blocks, the returned name may not belong to the closest
  18244. block from point."
  18245. (save-match-data
  18246. (catch 'exit
  18247. (let ((case-fold-search t)
  18248. (lim-up (save-excursion (outline-previous-heading)))
  18249. (lim-down (save-excursion (outline-next-heading))))
  18250. (mapc (lambda (name)
  18251. (let ((n (regexp-quote name)))
  18252. (when (org-between-regexps-p
  18253. (concat "^[ \t]*#\\+begin_" n)
  18254. (concat "^[ \t]*#\\+end_" n)
  18255. lim-up lim-down)
  18256. (throw 'exit n))))
  18257. names))
  18258. nil)))
  18259. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18260. "Call `multi-occur' with buffers for all agenda files."
  18261. (interactive "sOrg-files matching: \np")
  18262. (let* ((files (org-agenda-files))
  18263. (tnames (mapcar 'file-truename files))
  18264. (extra org-agenda-text-search-extra-files)
  18265. f)
  18266. (when (eq (car extra) 'agenda-archives)
  18267. (setq extra (cdr extra))
  18268. (setq files (org-add-archive-files files)))
  18269. (while (setq f (pop extra))
  18270. (unless (member (file-truename f) tnames)
  18271. (add-to-list 'files f 'append)
  18272. (add-to-list 'tnames (file-truename f) 'append)))
  18273. (multi-occur
  18274. (mapcar (lambda (x)
  18275. (with-current-buffer
  18276. (or (get-file-buffer x) (find-file-noselect x))
  18277. (widen)
  18278. (current-buffer)))
  18279. files)
  18280. regexp)))
  18281. (if (boundp 'occur-mode-find-occurrence-hook)
  18282. ;; Emacs 23
  18283. (add-hook 'occur-mode-find-occurrence-hook
  18284. (lambda ()
  18285. (when (eq major-mode 'org-mode)
  18286. (org-reveal))))
  18287. ;; Emacs 22
  18288. (defadvice occur-mode-goto-occurrence
  18289. (after org-occur-reveal activate)
  18290. (and (eq major-mode 'org-mode) (org-reveal)))
  18291. (defadvice occur-mode-goto-occurrence-other-window
  18292. (after org-occur-reveal activate)
  18293. (and (eq major-mode 'org-mode) (org-reveal)))
  18294. (defadvice occur-mode-display-occurrence
  18295. (after org-occur-reveal activate)
  18296. (when (eq major-mode 'org-mode)
  18297. (let ((pos (occur-mode-find-occurrence)))
  18298. (with-current-buffer (marker-buffer pos)
  18299. (save-excursion
  18300. (goto-char pos)
  18301. (org-reveal)))))))
  18302. (defun org-occur-link-in-agenda-files ()
  18303. "Create a link and search for it in the agendas.
  18304. The link is not stored in `org-stored-links', it is just created
  18305. for the search purpose."
  18306. (interactive)
  18307. (let ((link (condition-case nil
  18308. (org-store-link nil)
  18309. (error "Unable to create a link to here"))))
  18310. (org-occur-in-agenda-files (regexp-quote link))))
  18311. (defun org-uniquify (list)
  18312. "Remove duplicate elements from LIST."
  18313. (let (res)
  18314. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18315. res))
  18316. (defun org-delete-all (elts list)
  18317. "Remove all elements in ELTS from LIST."
  18318. (while elts
  18319. (setq list (delete (pop elts) list)))
  18320. list)
  18321. (defun org-count (cl-item cl-seq)
  18322. "Count the number of occurrences of ITEM in SEQ.
  18323. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18324. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18325. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18326. (while (< cl-start cl-end)
  18327. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18328. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18329. (setq cl-start (1+ cl-start)))
  18330. cl-count))
  18331. (defun org-remove-if (predicate seq)
  18332. "Remove everything from SEQ that fulfills PREDICATE."
  18333. (let (res e)
  18334. (while seq
  18335. (setq e (pop seq))
  18336. (if (not (funcall predicate e)) (push e res)))
  18337. (nreverse res)))
  18338. (defun org-remove-if-not (predicate seq)
  18339. "Remove everything from SEQ that does not fulfill PREDICATE."
  18340. (let (res e)
  18341. (while seq
  18342. (setq e (pop seq))
  18343. (if (funcall predicate e) (push e res)))
  18344. (nreverse res)))
  18345. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18346. "Reduce two-argument FUNCTION across SEQ.
  18347. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18348. \":initial-value\" removed."
  18349. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18350. (cadr (memq :initial-value cl-keys)))
  18351. (cl-seq (pop cl-seq))
  18352. (t (funcall cl-func)))))
  18353. (while cl-seq
  18354. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18355. cl-accum))
  18356. (defun org-back-over-empty-lines ()
  18357. "Move backwards over whitespace, to the beginning of the first empty line.
  18358. Returns the number of empty lines passed."
  18359. (let ((pos (point)))
  18360. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18361. (skip-chars-backward " \t\n\r")
  18362. (unless (eobp)
  18363. (forward-line -1)))
  18364. (beginning-of-line 2)
  18365. (goto-char (min (point) pos))
  18366. (count-lines (point) pos)))
  18367. (defun org-skip-whitespace ()
  18368. (skip-chars-forward " \t\n\r"))
  18369. (defun org-point-in-group (point group &optional context)
  18370. "Check if POINT is in match-group GROUP.
  18371. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18372. match. If the match group does not exist or point is not inside it,
  18373. return nil."
  18374. (and (match-beginning group)
  18375. (>= point (match-beginning group))
  18376. (<= point (match-end group))
  18377. (if context
  18378. (list context (match-beginning group) (match-end group))
  18379. t)))
  18380. (defun org-switch-to-buffer-other-window (&rest args)
  18381. "Switch to buffer in a second window on the current frame.
  18382. In particular, do not allow pop-up frames.
  18383. Returns the newly created buffer."
  18384. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18385. special-display-function)
  18386. (apply 'switch-to-buffer-other-window args)))
  18387. (defun org-combine-plists (&rest plists)
  18388. "Create a single property list from all plists in PLISTS.
  18389. The process starts by copying the first list, and then setting properties
  18390. from the other lists. Settings in the last list are the most significant
  18391. ones and overrule settings in the other lists."
  18392. (let ((rtn (copy-sequence (pop plists)))
  18393. p v ls)
  18394. (while plists
  18395. (setq ls (pop plists))
  18396. (while ls
  18397. (setq p (pop ls) v (pop ls))
  18398. (setq rtn (plist-put rtn p v))))
  18399. rtn))
  18400. (defun org-move-line-down (arg)
  18401. "Move the current line down. With prefix argument, move it past ARG lines."
  18402. (interactive "p")
  18403. (let ((col (current-column))
  18404. beg end pos)
  18405. (beginning-of-line 1) (setq beg (point))
  18406. (beginning-of-line 2) (setq end (point))
  18407. (beginning-of-line (+ 1 arg))
  18408. (setq pos (move-marker (make-marker) (point)))
  18409. (insert (delete-and-extract-region beg end))
  18410. (goto-char pos)
  18411. (org-move-to-column col)))
  18412. (defun org-move-line-up (arg)
  18413. "Move the current line up. With prefix argument, move it past ARG lines."
  18414. (interactive "p")
  18415. (let ((col (current-column))
  18416. beg end pos)
  18417. (beginning-of-line 1) (setq beg (point))
  18418. (beginning-of-line 2) (setq end (point))
  18419. (beginning-of-line (- arg))
  18420. (setq pos (move-marker (make-marker) (point)))
  18421. (insert (delete-and-extract-region beg end))
  18422. (goto-char pos)
  18423. (org-move-to-column col)))
  18424. (defun org-replace-escapes (string table)
  18425. "Replace %-escapes in STRING with values in TABLE.
  18426. TABLE is an association list with keys like \"%a\" and string values.
  18427. The sequences in STRING may contain normal field width and padding information,
  18428. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18429. so values can contain further %-escapes if they are define later in TABLE."
  18430. (let ((tbl (copy-alist table))
  18431. (case-fold-search nil)
  18432. (pchg 0)
  18433. e re rpl)
  18434. (while (setq e (pop tbl))
  18435. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18436. (when (and (cdr e) (string-match re (cdr e)))
  18437. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18438. (safe "SREF"))
  18439. (add-text-properties 0 3 (list 'sref sref) safe)
  18440. (setcdr e (replace-match safe t t (cdr e)))))
  18441. (while (string-match re string)
  18442. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18443. (cdr e)))
  18444. (setq string (replace-match rpl t t string))))
  18445. (while (setq pchg (next-property-change pchg string))
  18446. (let ((sref (get-text-property pchg 'sref string)))
  18447. (when (and sref (string-match "SREF" string pchg))
  18448. (setq string (replace-match sref t t string)))))
  18449. string))
  18450. (defun org-sublist (list start end)
  18451. "Return a section of LIST, from START to END.
  18452. Counting starts at 1."
  18453. (let (rtn (c start))
  18454. (setq list (nthcdr (1- start) list))
  18455. (while (and list (<= c end))
  18456. (push (pop list) rtn)
  18457. (setq c (1+ c)))
  18458. (nreverse rtn)))
  18459. (defun org-find-base-buffer-visiting (file)
  18460. "Like `find-buffer-visiting' but always return the base buffer and
  18461. not an indirect buffer."
  18462. (let ((buf (or (get-file-buffer file)
  18463. (find-buffer-visiting file))))
  18464. (if buf
  18465. (or (buffer-base-buffer buf) buf)
  18466. nil)))
  18467. (defun org-image-file-name-regexp (&optional extensions)
  18468. "Return regexp matching the file names of images.
  18469. If EXTENSIONS is given, only match these."
  18470. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18471. (image-file-name-regexp)
  18472. (let ((image-file-name-extensions
  18473. (or extensions
  18474. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18475. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18476. (concat "\\."
  18477. (regexp-opt (nconc (mapcar 'upcase
  18478. image-file-name-extensions)
  18479. image-file-name-extensions)
  18480. t)
  18481. "\\'"))))
  18482. (defun org-file-image-p (file &optional extensions)
  18483. "Return non-nil if FILE is an image."
  18484. (save-match-data
  18485. (string-match (org-image-file-name-regexp extensions) file)))
  18486. (defun org-get-cursor-date ()
  18487. "Return the date at cursor in as a time.
  18488. This works in the calendar and in the agenda, anywhere else it just
  18489. returns the current time."
  18490. (let (date day defd)
  18491. (cond
  18492. ((eq major-mode 'calendar-mode)
  18493. (setq date (calendar-cursor-to-date)
  18494. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18495. ((eq major-mode 'org-agenda-mode)
  18496. (setq day (get-text-property (point) 'day))
  18497. (if day
  18498. (setq date (calendar-gregorian-from-absolute day)
  18499. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18500. (nth 2 date))))))
  18501. (or defd (current-time))))
  18502. (defvar org-agenda-action-marker (make-marker)
  18503. "Marker pointing to the entry for the next agenda action.")
  18504. (defun org-mark-entry-for-agenda-action ()
  18505. "Mark the current entry as target of an agenda action.
  18506. Agenda actions are actions executed from the agenda with the key `k',
  18507. which make use of the date at the cursor."
  18508. (interactive)
  18509. (move-marker org-agenda-action-marker
  18510. (save-excursion (org-back-to-heading t) (point))
  18511. (current-buffer))
  18512. (message
  18513. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18514. (defun org-mark-subtree ()
  18515. "Mark the current subtree.
  18516. This puts point at the start of the current subtree, and mark at the end.
  18517. If point is in an inline task, mark that task instead."
  18518. (interactive)
  18519. (let ((inline-task-p
  18520. (and (featurep 'org-inlinetask)
  18521. (org-inlinetask-in-task-p)))
  18522. (beg))
  18523. ;; Get beginning of subtree
  18524. (cond
  18525. (inline-task-p (org-inlinetask-goto-beginning))
  18526. ((org-at-heading-p) (beginning-of-line))
  18527. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18528. (setq beg (point))
  18529. ;; Get end of it
  18530. (if inline-task-p
  18531. (org-inlinetask-goto-end)
  18532. (org-end-of-subtree))
  18533. ;; Mark zone
  18534. (push-mark (point) nil t)
  18535. (goto-char beg)))
  18536. ;;; Paragraph filling stuff.
  18537. ;; We want this to be just right, so use the full arsenal.
  18538. (defun org-indent-line-function ()
  18539. "Indent line depending on context."
  18540. (interactive)
  18541. (let* ((pos (point))
  18542. (itemp (org-at-item-p))
  18543. (case-fold-search t)
  18544. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18545. (inline-task-p (and (featurep 'org-inlinetask)
  18546. (org-inlinetask-in-task-p)))
  18547. (inline-re (and inline-task-p
  18548. (org-inlinetask-outline-regexp)))
  18549. column)
  18550. (beginning-of-line 1)
  18551. (cond
  18552. ;; Comments
  18553. ((looking-at "# ") (setq column 0))
  18554. ;; Headings
  18555. ((looking-at org-outline-regexp) (setq column 0))
  18556. ;; Included files
  18557. ((looking-at "#\\+include:") (setq column 0))
  18558. ;; Footnote definition
  18559. ((looking-at org-footnote-definition-re) (setq column 0))
  18560. ;; Literal examples
  18561. ((looking-at "[ \t]*:\\( \\|$\\)")
  18562. (setq column (org-get-indentation))) ; do nothing
  18563. ;; Lists
  18564. ((ignore-errors (goto-char (org-in-item-p)))
  18565. (setq column (if itemp
  18566. (org-get-indentation)
  18567. (org-list-item-body-column (point))))
  18568. (goto-char pos))
  18569. ;; Drawers
  18570. ((and (looking-at "[ \t]*:END:")
  18571. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18572. (save-excursion
  18573. (goto-char (1- (match-beginning 1)))
  18574. (setq column (current-column))))
  18575. ;; Special blocks
  18576. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18577. (save-excursion
  18578. (re-search-backward
  18579. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18580. (setq column (org-get-indentation (match-string 0))))
  18581. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18582. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18583. (save-excursion
  18584. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18585. (setq column
  18586. (cond ((equal (downcase (match-string 1)) "src")
  18587. ;; src blocks: let `org-edit-src-exit' handle them
  18588. (org-get-indentation))
  18589. ((equal (downcase (match-string 1)) "example")
  18590. (max (org-get-indentation)
  18591. (org-get-indentation (match-string 0))))
  18592. (t
  18593. (org-get-indentation (match-string 0))))))
  18594. ;; This line has nothing special, look at the previous relevant
  18595. ;; line to compute indentation
  18596. (t
  18597. (beginning-of-line 0)
  18598. (while (and (not (bobp))
  18599. (not (looking-at org-drawer-regexp))
  18600. ;; When point started in an inline task, do not move
  18601. ;; above task starting line.
  18602. (not (and inline-task-p (looking-at inline-re)))
  18603. ;; Skip drawers, blocks, empty lines, verbatim,
  18604. ;; comments, tables, footnotes definitions, lists,
  18605. ;; inline tasks.
  18606. (or (and (looking-at "[ \t]*:END:")
  18607. (re-search-backward org-drawer-regexp nil t))
  18608. (and (looking-at "[ \t]*#\\+end_")
  18609. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18610. (looking-at "[ \t]*[\n:#|]")
  18611. (looking-at org-footnote-definition-re)
  18612. (and (ignore-errors (goto-char (org-in-item-p)))
  18613. (goto-char
  18614. (org-list-get-top-point (org-list-struct))))
  18615. (and (not inline-task-p)
  18616. (featurep 'org-inlinetask)
  18617. (org-inlinetask-in-task-p)
  18618. (or (org-inlinetask-goto-beginning) t))))
  18619. (beginning-of-line 0))
  18620. (cond
  18621. ;; There was an heading above.
  18622. ((looking-at "\\*+[ \t]+")
  18623. (if (not org-adapt-indentation)
  18624. (setq column 0)
  18625. (goto-char (match-end 0))
  18626. (setq column (current-column))))
  18627. ;; A drawer had started and is unfinished
  18628. ((looking-at org-drawer-regexp)
  18629. (goto-char (1- (match-beginning 1)))
  18630. (setq column (current-column)))
  18631. ;; Else, nothing noticeable found: get indentation and go on.
  18632. (t (setq column (org-get-indentation))))))
  18633. ;; Now apply indentation and move cursor accordingly
  18634. (goto-char pos)
  18635. (if (<= (current-column) (current-indentation))
  18636. (org-indent-line-to column)
  18637. (save-excursion (org-indent-line-to column)))
  18638. ;; Special polishing for properties, see `org-property-format'
  18639. (setq column (current-column))
  18640. (beginning-of-line 1)
  18641. (if (looking-at
  18642. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18643. (replace-match (concat (match-string 1)
  18644. (format org-property-format
  18645. (match-string 2) (match-string 3)))
  18646. t t))
  18647. (org-move-to-column column)))
  18648. (defun org-indent-drawer ()
  18649. "Indent the drawer at point."
  18650. (interactive)
  18651. (let ((p (point))
  18652. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18653. (match-end 0)))
  18654. (folded
  18655. (save-excursion
  18656. (end-of-line)
  18657. (when (overlays-at (point))
  18658. (member 'invisible (overlay-properties
  18659. (car (overlays-at (point)))))))))
  18660. (when folded (org-cycle))
  18661. (indent-for-tab-command)
  18662. (while (and (move-beginning-of-line 2) (< (point) e))
  18663. (indent-for-tab-command))
  18664. (goto-char p)
  18665. (when folded (org-cycle)))
  18666. (message "Drawer at point indented"))
  18667. (defun org-indent-block ()
  18668. "Indent the block at point."
  18669. (interactive)
  18670. (let ((p (point))
  18671. (case-fold-search t)
  18672. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18673. (match-end 0)))
  18674. (folded
  18675. (save-excursion
  18676. (end-of-line)
  18677. (when (overlays-at (point))
  18678. (member 'invisible (overlay-properties
  18679. (car (overlays-at (point)))))))))
  18680. (when folded (org-cycle))
  18681. (indent-for-tab-command)
  18682. (while (and (move-beginning-of-line 2) (< (point) e))
  18683. (indent-for-tab-command))
  18684. (goto-char p)
  18685. (when folded (org-cycle)))
  18686. (message "Block at point indented"))
  18687. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18688. "Variable to store copy of `adaptive-fill-regexp'.
  18689. Since `adaptive-fill-regexp' is set to never match, we need to
  18690. store a backup of its value before entering `org-mode' so that
  18691. the functionality can be provided as a fall-back.")
  18692. (defun org-set-autofill-regexps ()
  18693. (interactive)
  18694. ;; In the paragraph separator we include headlines, because filling
  18695. ;; text in a line directly attached to a headline would otherwise
  18696. ;; fill the headline as well.
  18697. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18698. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18699. ;; The paragraph starter includes hand-formatted lists.
  18700. (org-set-local
  18701. 'paragraph-start
  18702. (concat
  18703. "\f" "\\|"
  18704. "[ ]*$" "\\|"
  18705. org-outline-regexp "\\|"
  18706. "[ \t]*#" "\\|"
  18707. (org-item-re) "\\|"
  18708. "[ \t]*[:|]" "\\|"
  18709. "\\$\\$" "\\|"
  18710. "\\\\\\(begin\\|end\\|[][]\\)"))
  18711. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18712. ;; But only if the user has not turned off tables or fixed-width regions
  18713. (org-set-local
  18714. 'auto-fill-inhibit-regexp
  18715. (concat org-outline-regexp
  18716. "\\|#\\+"
  18717. "\\|[ \t]*" org-keyword-time-regexp
  18718. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18719. (concat
  18720. "\\|[ \t]*["
  18721. (if org-enable-table-editor "|" "")
  18722. (if org-enable-fixed-width-editor ":" "")
  18723. "]"))))
  18724. ;; We use our own fill-paragraph function, to make sure that tables
  18725. ;; and fixed-width regions are not wrapped. That function will pass
  18726. ;; through to `fill-paragraph' when appropriate.
  18727. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18728. ;; Prevent auto-fill from inserting unwanted new items.
  18729. (if (boundp 'fill-nobreak-predicate)
  18730. (org-set-local
  18731. 'fill-nobreak-predicate
  18732. (org-uniquify
  18733. (append fill-nobreak-predicate
  18734. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  18735. ;; Adaptive filling: To get full control, first make sure that
  18736. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18737. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18738. (org-set-local 'org-adaptive-fill-regexp-backup
  18739. adaptive-fill-regexp))
  18740. (org-set-local 'adaptive-fill-regexp "\000")
  18741. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18742. (org-set-local 'adaptive-fill-function
  18743. 'org-adaptive-fill-function)
  18744. (org-set-local
  18745. 'align-mode-rules-list
  18746. '((org-in-buffer-settings
  18747. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18748. (modes . '(org-mode))))))
  18749. (defun org-fill-item-nobreak-p ()
  18750. "Non-nil when a line break at point would insert a new item."
  18751. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18752. (defun org-fill-line-break-nobreak-p ()
  18753. "Non-nil when a line break at point would create an Org line break."
  18754. (save-excursion
  18755. (skip-chars-backward "[ \t]")
  18756. (skip-chars-backward "\\\\")
  18757. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  18758. (defun org-fill-paragraph (&optional justify)
  18759. "Re-align a table, pass through to fill-paragraph if no table."
  18760. (let ((table-p (org-at-table-p))
  18761. (table.el-p (org-at-table.el-p))
  18762. (itemp (org-in-item-p)))
  18763. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18764. (save-excursion (goto-char (point-at-bol))
  18765. (looking-at org-outline-regexp)))
  18766. t) ; skip headlines
  18767. (table.el-p t) ; skip table.el tables
  18768. (table-p (org-table-align) t) ; align Org tables
  18769. (itemp ; align text in items
  18770. (let* ((struct (save-excursion (goto-char itemp)
  18771. (org-list-struct)))
  18772. (parents (org-list-parents-alist struct))
  18773. (children (org-list-get-children itemp struct parents))
  18774. beg end prev next prefix)
  18775. ;; Determine in which part of item point is: before
  18776. ;; first child, after last child, between two
  18777. ;; sub-lists, or simply in item if there's no child.
  18778. (cond
  18779. ((not children)
  18780. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18781. beg itemp
  18782. end (org-list-get-item-end itemp struct)))
  18783. ((< (point) (setq next (car children)))
  18784. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18785. beg itemp
  18786. end next))
  18787. ((> (point) (setq prev (car (last children))))
  18788. (setq beg (org-list-get-item-end prev struct)
  18789. end (org-list-get-item-end itemp struct)
  18790. prefix (save-excursion
  18791. (goto-char beg)
  18792. (skip-chars-forward " \t")
  18793. (make-string (current-column) ?\ ))))
  18794. (t (catch 'exit
  18795. (while (setq next (pop children))
  18796. (if (> (point) next)
  18797. (setq prev next)
  18798. (setq beg (org-list-get-item-end prev struct)
  18799. end next
  18800. prefix (save-excursion
  18801. (goto-char beg)
  18802. (skip-chars-forward " \t")
  18803. (make-string (current-column) ?\ )))
  18804. (throw 'exit nil))))))
  18805. ;; Use `fill-paragraph' with buffer narrowed to item
  18806. ;; without any child, and with our computed PREFIX.
  18807. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18808. (save-restriction
  18809. (narrow-to-region beg end)
  18810. (save-excursion (fill-paragraph justify)))) t))
  18811. ;; Special case where point is not in a list but is on
  18812. ;; a paragraph adjacent to a list: make sure this paragraph
  18813. ;; doesn't get merged with the end of the list by narrowing
  18814. ;; buffer first.
  18815. ((save-excursion (forward-paragraph -1)
  18816. (setq itemp (org-in-item-p)))
  18817. (let ((struct (save-excursion (goto-char itemp)
  18818. (org-list-struct))))
  18819. (save-restriction
  18820. (narrow-to-region (org-list-get-bottom-point struct)
  18821. (save-excursion (forward-paragraph 1)
  18822. (point)))
  18823. (fill-paragraph justify) t)))
  18824. ;; Else simply call `fill-paragraph'.
  18825. (t nil))))
  18826. ;; For reference, this is the default value of adaptive-fill-regexp
  18827. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18828. (defun org-adaptive-fill-function ()
  18829. "Return a fill prefix for org-mode files."
  18830. (let (itemp)
  18831. (save-excursion
  18832. (cond
  18833. ;; Comment line
  18834. ((looking-at "#[ \t]+")
  18835. (match-string-no-properties 0))
  18836. ;; Plain list item
  18837. ((org-at-item-p)
  18838. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18839. ;; Point is in a list after `backward-paragraph': original
  18840. ;; point wasn't in the list, or filling would have been taken
  18841. ;; care of by `org-auto-fill-function', but the list and the
  18842. ;; real paragraph are not separated by a blank line. Thus, move
  18843. ;; point after the list to go back to real paragraph and
  18844. ;; determine fill-prefix.
  18845. ((setq itemp (org-in-item-p))
  18846. (goto-char itemp)
  18847. (let* ((struct (org-list-struct))
  18848. (bottom (org-list-get-bottom-point struct)))
  18849. (goto-char bottom)
  18850. (make-string (org-get-indentation) ?\ )))
  18851. ;; Other text
  18852. ((looking-at org-adaptive-fill-regexp-backup)
  18853. (match-string-no-properties 0))))))
  18854. (defun org-auto-fill-function ()
  18855. "Auto-fill function."
  18856. (let (itemp prefix)
  18857. ;; When in a list, compute an appropriate fill-prefix and make
  18858. ;; sure it will be used by `do-auto-fill'.
  18859. (if (setq itemp (org-in-item-p))
  18860. (progn
  18861. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18862. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18863. (do-auto-fill)))
  18864. ;; Else just use `do-auto-fill'.
  18865. (do-auto-fill))))
  18866. ;;; Other stuff.
  18867. (defun org-toggle-fixed-width-section (arg)
  18868. "Toggle the fixed-width export.
  18869. If there is no active region, the QUOTE keyword at the current headline is
  18870. inserted or removed. When present, it causes the text between this headline
  18871. and the next to be exported as fixed-width text, and unmodified.
  18872. If there is an active region, this command adds or removes a colon as the
  18873. first character of this line. If the first character of a line is a colon,
  18874. this line is also exported in fixed-width font."
  18875. (interactive "P")
  18876. (let* ((cc 0)
  18877. (regionp (org-region-active-p))
  18878. (beg (if regionp (region-beginning) (point)))
  18879. (end (if regionp (region-end)))
  18880. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18881. (case-fold-search nil)
  18882. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18883. off)
  18884. (if regionp
  18885. (save-excursion
  18886. (goto-char beg)
  18887. (setq cc (current-column))
  18888. (beginning-of-line 1)
  18889. (setq off (looking-at re))
  18890. (while (> nlines 0)
  18891. (setq nlines (1- nlines))
  18892. (beginning-of-line 1)
  18893. (cond
  18894. (arg
  18895. (org-move-to-column cc t)
  18896. (insert ": \n")
  18897. (forward-line -1))
  18898. ((and off (looking-at re))
  18899. (replace-match "" t t nil 1))
  18900. ((not off) (org-move-to-column cc t) (insert ": ")))
  18901. (forward-line 1)))
  18902. (save-excursion
  18903. (org-back-to-heading)
  18904. (cond
  18905. ((looking-at (format org-heading-keyword-regexp-format
  18906. org-quote-string))
  18907. (goto-char (match-end 1))
  18908. (looking-at (concat " +" org-quote-string))
  18909. (replace-match "" t t)
  18910. (when (eolp) (insert " ")))
  18911. ((looking-at org-outline-regexp)
  18912. (goto-char (match-end 0))
  18913. (insert org-quote-string " ")))))))
  18914. (defun org-reftex-citation ()
  18915. "Use reftex-citation to insert a citation into the buffer.
  18916. This looks for a line like
  18917. #+BIBLIOGRAPHY: foo plain option:-d
  18918. and derives from it that foo.bib is the bibliography file relevant
  18919. for this document. It then installs the necessary environment for RefTeX
  18920. to work in this buffer and calls `reftex-citation' to insert a citation
  18921. into the buffer.
  18922. Export of such citations to both LaTeX and HTML is handled by the contributed
  18923. package org-exp-bibtex by Taru Karttunen."
  18924. (interactive)
  18925. (let ((reftex-docstruct-symbol 'rds)
  18926. (reftex-cite-format "\\cite{%l}")
  18927. rds bib)
  18928. (save-excursion
  18929. (save-restriction
  18930. (widen)
  18931. (let ((case-fold-search t)
  18932. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18933. (if (not (save-excursion
  18934. (or (re-search-forward re nil t)
  18935. (re-search-backward re nil t))))
  18936. (error "No bibliography defined in file")
  18937. (setq bib (concat (match-string 1) ".bib")
  18938. rds (list (list 'bib bib)))))))
  18939. (call-interactively 'reftex-citation)))
  18940. ;;;; Functions extending outline functionality
  18941. (defun org-beginning-of-line (&optional arg)
  18942. "Go to the beginning of the current line. If that is invisible, continue
  18943. to a visible line beginning. This makes the function of C-a more intuitive.
  18944. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18945. first attempt, and only move to after the tags when the cursor is already
  18946. beyond the end of the headline."
  18947. (interactive "P")
  18948. (let ((pos (point))
  18949. (special (if (consp org-special-ctrl-a/e)
  18950. (car org-special-ctrl-a/e)
  18951. org-special-ctrl-a/e))
  18952. refpos)
  18953. (if (org-bound-and-true-p line-move-visual)
  18954. (beginning-of-visual-line 1)
  18955. (beginning-of-line 1))
  18956. (if (and arg (fboundp 'move-beginning-of-line))
  18957. (call-interactively 'move-beginning-of-line)
  18958. (if (bobp)
  18959. nil
  18960. (backward-char 1)
  18961. (if (org-truely-invisible-p)
  18962. (while (and (not (bobp)) (org-truely-invisible-p))
  18963. (backward-char 1)
  18964. (beginning-of-line 1))
  18965. (forward-char 1))))
  18966. (when special
  18967. (cond
  18968. ((and (looking-at org-complex-heading-regexp)
  18969. (= (char-after (match-end 1)) ?\ ))
  18970. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18971. (point-at-eol)))
  18972. (goto-char
  18973. (if (eq special t)
  18974. (cond ((> pos refpos) refpos)
  18975. ((= pos (point)) refpos)
  18976. (t (point)))
  18977. (cond ((> pos (point)) (point))
  18978. ((not (eq last-command this-command)) (point))
  18979. (t refpos)))))
  18980. ((org-at-item-p)
  18981. ;; Being at an item and not looking at an the item means point
  18982. ;; was previously moved to beginning of a visual line, whiche
  18983. ;; doesn't contain the item. Therefore, do nothing special,
  18984. ;; just stay here.
  18985. (when (looking-at org-list-full-item-re)
  18986. ;; Set special position at first white space character after
  18987. ;; bullet, and check-box, if any.
  18988. (let ((after-bullet
  18989. (let ((box (match-end 3)))
  18990. (if (not box) (match-end 1)
  18991. (let ((after (char-after box)))
  18992. (if (and after (= after ? )) (1+ box) box))))))
  18993. ;; Special case: Move point to special position when
  18994. ;; currently after it or at beginning of line.
  18995. (if (eq special t)
  18996. (when (or (> pos after-bullet) (= (point) pos))
  18997. (goto-char after-bullet))
  18998. ;; Reversed case: Move point to special position when
  18999. ;; point was already at beginning of line and command is
  19000. ;; repeated.
  19001. (when (and (= (point) pos) (eq last-command this-command))
  19002. (goto-char after-bullet))))))))
  19003. (org-no-warnings
  19004. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19005. (defun org-end-of-line (&optional arg)
  19006. "Go to the end of the line.
  19007. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19008. first attempt, and only move to after the tags when the cursor is already
  19009. beyond the end of the headline."
  19010. (interactive "P")
  19011. (let ((special (if (consp org-special-ctrl-a/e)
  19012. (cdr org-special-ctrl-a/e)
  19013. org-special-ctrl-a/e)))
  19014. (cond
  19015. ((or (not special) arg
  19016. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19017. (call-interactively
  19018. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19019. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19020. (t 'end-of-line))))
  19021. ((org-at-heading-p)
  19022. (let ((pos (point)))
  19023. (beginning-of-line 1)
  19024. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19025. (if (eq special t)
  19026. (if (or (< pos (match-beginning 1))
  19027. (= pos (match-end 0)))
  19028. (goto-char (match-beginning 1))
  19029. (goto-char (match-end 0)))
  19030. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19031. (goto-char (match-end 0))
  19032. (goto-char (match-beginning 1))))
  19033. (call-interactively (if (fboundp 'move-end-of-line)
  19034. 'move-end-of-line
  19035. 'end-of-line)))))
  19036. ((org-at-drawer-p)
  19037. (move-end-of-line 1)
  19038. (when (overlays-at (1- (point))) (backward-char 1)))
  19039. ;; At an item: Move before any hidden text.
  19040. (t (call-interactively
  19041. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19042. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19043. (t 'end-of-line)))))
  19044. (org-no-warnings
  19045. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19046. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19047. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19048. (defun org-backward-sentence (&optional arg)
  19049. "Go to beginning of sentence, or beginning of table field.
  19050. This will call `backward-sentence' or `org-table-beginning-of-field',
  19051. depending on context."
  19052. (interactive "P")
  19053. (cond
  19054. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19055. (t (call-interactively 'backward-sentence))))
  19056. (defun org-forward-sentence (&optional arg)
  19057. "Go to end of sentence, or end of table field.
  19058. This will call `forward-sentence' or `org-table-end-of-field',
  19059. depending on context."
  19060. (interactive "P")
  19061. (cond
  19062. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19063. (t (call-interactively 'forward-sentence))))
  19064. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19065. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19066. (defun org-kill-line (&optional arg)
  19067. "Kill line, to tags or end of line."
  19068. (interactive "P")
  19069. (cond
  19070. ((or (not org-special-ctrl-k)
  19071. (bolp)
  19072. (not (org-at-heading-p)))
  19073. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19074. org-ctrl-k-protect-subtree)
  19075. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19076. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19077. (error "C-k aborted - would kill hidden subtree")))
  19078. (call-interactively
  19079. (if visual-line-mode 'kill-visual-line 'kill-line)))
  19080. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19081. (kill-region (point) (match-beginning 1))
  19082. (org-set-tags nil t))
  19083. (t (kill-region (point) (point-at-eol)))))
  19084. (define-key org-mode-map "\C-k" 'org-kill-line)
  19085. (defun org-yank (&optional arg)
  19086. "Yank. If the kill is a subtree, treat it specially.
  19087. This command will look at the current kill and check if is a single
  19088. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19089. cursor is at the beginning of a line or after the stars of a currently
  19090. empty headline, then the yank is handled specially. How exactly depends
  19091. on the value of the following variables, both set by default.
  19092. org-yank-folded-subtrees
  19093. When set, the subtree(s) will be folded after insertion, but only
  19094. if doing so would now swallow text after the yanked text.
  19095. org-yank-adjusted-subtrees
  19096. When set, the subtree will be promoted or demoted in order to
  19097. fit into the local outline tree structure, which means that the level
  19098. will be adjusted so that it becomes the smaller one of the two
  19099. *visible* surrounding headings.
  19100. Any prefix to this command will cause `yank' to be called directly with
  19101. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19102. will just
  19103. plainly yank the text as it is.
  19104. \[1] The test checks if the first non-white line is a heading
  19105. and if there are no other headings with fewer stars."
  19106. (interactive "P")
  19107. (org-yank-generic 'yank arg))
  19108. (defun org-yank-generic (command arg)
  19109. "Perform some yank-like command.
  19110. This function implements the behavior described in the `org-yank'
  19111. documentation. However, it has been generalized to work for any
  19112. interactive command with similar behavior."
  19113. ;; pretend to be command COMMAND
  19114. (setq this-command command)
  19115. (if arg
  19116. (call-interactively command)
  19117. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19118. (and (org-kill-is-subtree-p)
  19119. (or (bolp)
  19120. (and (looking-at "[ \t]*$")
  19121. (string-match
  19122. "\\`\\*+\\'"
  19123. (buffer-substring (point-at-bol) (point)))))))
  19124. swallowp)
  19125. (cond
  19126. ((and subtreep org-yank-folded-subtrees)
  19127. (let ((beg (point))
  19128. end)
  19129. (if (and subtreep org-yank-adjusted-subtrees)
  19130. (org-paste-subtree nil nil 'for-yank)
  19131. (call-interactively command))
  19132. (setq end (point))
  19133. (goto-char beg)
  19134. (when (and (bolp) subtreep
  19135. (not (setq swallowp
  19136. (org-yank-folding-would-swallow-text beg end))))
  19137. (org-with-limited-levels
  19138. (or (looking-at org-outline-regexp)
  19139. (re-search-forward org-outline-regexp-bol end t))
  19140. (while (and (< (point) end) (looking-at org-outline-regexp))
  19141. (hide-subtree)
  19142. (org-cycle-show-empty-lines 'folded)
  19143. (condition-case nil
  19144. (outline-forward-same-level 1)
  19145. (error (goto-char end))))))
  19146. (when swallowp
  19147. (message
  19148. "Inserted text not folded because that would swallow text"))
  19149. (goto-char end)
  19150. (skip-chars-forward " \t\n\r")
  19151. (beginning-of-line 1)
  19152. (push-mark beg 'nomsg)))
  19153. ((and subtreep org-yank-adjusted-subtrees)
  19154. (let ((beg (point-at-bol)))
  19155. (org-paste-subtree nil nil 'for-yank)
  19156. (push-mark beg 'nomsg)))
  19157. (t
  19158. (call-interactively command))))))
  19159. (defun org-yank-folding-would-swallow-text (beg end)
  19160. "Would hide-subtree at BEG swallow any text after END?"
  19161. (let (level)
  19162. (org-with-limited-levels
  19163. (save-excursion
  19164. (goto-char beg)
  19165. (when (or (looking-at org-outline-regexp)
  19166. (re-search-forward org-outline-regexp-bol end t))
  19167. (setq level (org-outline-level)))
  19168. (goto-char end)
  19169. (skip-chars-forward " \t\r\n\v\f")
  19170. (if (or (eobp)
  19171. (and (bolp) (looking-at org-outline-regexp)
  19172. (<= (org-outline-level) level)))
  19173. nil ; Nothing would be swallowed
  19174. t))))) ; something would swallow
  19175. (define-key org-mode-map "\C-y" 'org-yank)
  19176. (defun org-truely-invisible-p ()
  19177. "Check if point is at a character currently not visible.
  19178. This version does not only check the character property, but also
  19179. `visible-mode'."
  19180. ;; Early versions of noutline don't have `outline-invisible-p'.
  19181. (if (org-bound-and-true-p visible-mode)
  19182. nil
  19183. (outline-invisible-p)))
  19184. (defun org-invisible-p2 ()
  19185. "Check if point is at a character currently not visible."
  19186. (save-excursion
  19187. (if (and (eolp) (not (bobp))) (backward-char 1))
  19188. ;; Early versions of noutline don't have `outline-invisible-p'.
  19189. (outline-invisible-p)))
  19190. (defun org-back-to-heading (&optional invisible-ok)
  19191. "Call `outline-back-to-heading', but provide a better error message."
  19192. (condition-case nil
  19193. (outline-back-to-heading invisible-ok)
  19194. (error (error "Before first headline at position %d in buffer %s"
  19195. (point) (current-buffer)))))
  19196. (defun org-beginning-of-defun ()
  19197. "Go to the beginning of the subtree, i.e. back to the heading."
  19198. (org-back-to-heading))
  19199. (defun org-end-of-defun ()
  19200. "Go to the end of the subtree."
  19201. (org-end-of-subtree nil t))
  19202. (defun org-before-first-heading-p ()
  19203. "Before first heading?"
  19204. (save-excursion
  19205. (end-of-line)
  19206. (null (re-search-backward org-outline-regexp-bol nil t))))
  19207. (defun org-at-heading-p (&optional ignored)
  19208. (outline-on-heading-p t))
  19209. ;; Compatibility alias with Org versions < 7.8.03
  19210. (defalias 'org-on-heading-p 'org-at-heading-p)
  19211. (defun org-at-drawer-p nil
  19212. "Is cursor at a drawer keyword?"
  19213. (save-excursion
  19214. (move-beginning-of-line 1)
  19215. (looking-at org-drawer-regexp)))
  19216. (defun org-at-block-p nil
  19217. "Is cursor at a block keyword?"
  19218. (save-excursion
  19219. (move-beginning-of-line 1)
  19220. (looking-at org-block-regexp)))
  19221. (defun org-point-at-end-of-empty-headline ()
  19222. "If point is at the end of an empty headline, return t, else nil.
  19223. If the heading only contains a TODO keyword, it is still still considered
  19224. empty."
  19225. (and (looking-at "[ \t]*$")
  19226. (save-excursion
  19227. (beginning-of-line 1)
  19228. (let ((case-fold-search nil))
  19229. (looking-at org-todo-line-regexp)))
  19230. (string= (match-string 3) "")))
  19231. (defun org-at-heading-or-item-p ()
  19232. (or (org-at-heading-p) (org-at-item-p)))
  19233. (defun org-at-target-p ()
  19234. (or (org-in-regexp org-radio-target-regexp)
  19235. (org-in-regexp org-target-regexp)))
  19236. ;; Compatibility alias with Org versions < 7.8.03
  19237. (defalias 'org-on-target-p 'org-at-target-p)
  19238. (defun org-up-heading-all (arg)
  19239. "Move to the heading line of which the present line is a subheading.
  19240. This function considers both visible and invisible heading lines.
  19241. With argument, move up ARG levels."
  19242. (if (fboundp 'outline-up-heading-all)
  19243. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19244. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19245. (defun org-up-heading-safe ()
  19246. "Move to the heading line of which the present line is a subheading.
  19247. This version will not throw an error. It will return the level of the
  19248. headline found, or nil if no higher level is found.
  19249. Also, this function will be a lot faster than `outline-up-heading',
  19250. because it relies on stars being the outline starters. This can really
  19251. make a significant difference in outlines with very many siblings."
  19252. (let (start-level re)
  19253. (org-back-to-heading t)
  19254. (setq start-level (funcall outline-level))
  19255. (if (equal start-level 1)
  19256. nil
  19257. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19258. (if (re-search-backward re nil t)
  19259. (funcall outline-level)))))
  19260. (defun org-first-sibling-p ()
  19261. "Is this heading the first child of its parents?"
  19262. (interactive)
  19263. (let ((re org-outline-regexp-bol)
  19264. level l)
  19265. (unless (org-at-heading-p t)
  19266. (error "Not at a heading"))
  19267. (setq level (funcall outline-level))
  19268. (save-excursion
  19269. (if (not (re-search-backward re nil t))
  19270. t
  19271. (setq l (funcall outline-level))
  19272. (< l level)))))
  19273. (defun org-goto-sibling (&optional previous)
  19274. "Goto the next sibling, even if it is invisible.
  19275. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19276. when a sibling was found. When none is found, return nil and don't
  19277. move point."
  19278. (let ((fun (if previous 're-search-backward 're-search-forward))
  19279. (pos (point))
  19280. (re org-outline-regexp-bol)
  19281. level l)
  19282. (when (condition-case nil (org-back-to-heading t) (error nil))
  19283. (setq level (funcall outline-level))
  19284. (catch 'exit
  19285. (or previous (forward-char 1))
  19286. (while (funcall fun re nil t)
  19287. (setq l (funcall outline-level))
  19288. (when (< l level) (goto-char pos) (throw 'exit nil))
  19289. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19290. (goto-char pos)
  19291. nil))))
  19292. (defun org-show-siblings ()
  19293. "Show all siblings of the current headline."
  19294. (save-excursion
  19295. (while (org-goto-sibling) (org-flag-heading nil)))
  19296. (save-excursion
  19297. (while (org-goto-sibling 'previous)
  19298. (org-flag-heading nil))))
  19299. (defun org-goto-first-child ()
  19300. "Goto the first child, even if it is invisible.
  19301. Return t when a child was found. Otherwise don't move point and
  19302. return nil."
  19303. (let (level (pos (point)) (re org-outline-regexp-bol))
  19304. (when (condition-case nil (org-back-to-heading t) (error nil))
  19305. (setq level (outline-level))
  19306. (forward-char 1)
  19307. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19308. (progn (goto-char (match-beginning 0)) t)
  19309. (goto-char pos) nil))))
  19310. (defun org-show-hidden-entry ()
  19311. "Show an entry where even the heading is hidden."
  19312. (save-excursion
  19313. (org-show-entry)))
  19314. (defun org-flag-heading (flag &optional entry)
  19315. "Flag the current heading. FLAG non-nil means make invisible.
  19316. When ENTRY is non-nil, show the entire entry."
  19317. (save-excursion
  19318. (org-back-to-heading t)
  19319. ;; Check if we should show the entire entry
  19320. (if entry
  19321. (progn
  19322. (org-show-entry)
  19323. (save-excursion
  19324. (and (outline-next-heading)
  19325. (org-flag-heading nil))))
  19326. (outline-flag-region (max (point-min) (1- (point)))
  19327. (save-excursion (outline-end-of-heading) (point))
  19328. flag))))
  19329. (defun org-get-next-sibling ()
  19330. "Move to next heading of the same level, and return point.
  19331. If there is no such heading, return nil.
  19332. This is like outline-next-sibling, but invisible headings are ok."
  19333. (let ((level (funcall outline-level)))
  19334. (outline-next-heading)
  19335. (while (and (not (eobp)) (> (funcall outline-level) level))
  19336. (outline-next-heading))
  19337. (if (or (eobp) (< (funcall outline-level) level))
  19338. nil
  19339. (point))))
  19340. (defun org-get-last-sibling ()
  19341. "Move to previous heading of the same level, and return point.
  19342. If there is no such heading, return nil."
  19343. (let ((opoint (point))
  19344. (level (funcall outline-level)))
  19345. (outline-previous-heading)
  19346. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19347. (while (and (> (funcall outline-level) level)
  19348. (not (bobp)))
  19349. (outline-previous-heading))
  19350. (if (< (funcall outline-level) level)
  19351. nil
  19352. (point)))))
  19353. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19354. ;; This contains an exact copy of the original function, but it uses
  19355. ;; `org-back-to-heading', to make it work also in invisible
  19356. ;; trees. And is uses an invisible-OK argument.
  19357. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19358. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19359. ;; with many children and grandchildren etc, this can be much faster
  19360. ;; than the outline version.
  19361. (org-back-to-heading invisible-OK)
  19362. (let ((first t)
  19363. (level (funcall outline-level)))
  19364. (if (and (eq major-mode 'org-mode) (< level 1000))
  19365. ;; A true heading (not a plain list item), in Org-mode
  19366. ;; This means we can easily find the end by looking
  19367. ;; only for the right number of stars. Using a regexp to do
  19368. ;; this is so much faster than using a Lisp loop.
  19369. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19370. (forward-char 1)
  19371. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19372. ;; something else, do it the slow way
  19373. (while (and (not (eobp))
  19374. (or first (> (funcall outline-level) level)))
  19375. (setq first nil)
  19376. (outline-next-heading)))
  19377. (unless to-heading
  19378. (if (memq (preceding-char) '(?\n ?\^M))
  19379. (progn
  19380. ;; Go to end of line before heading
  19381. (forward-char -1)
  19382. (if (memq (preceding-char) '(?\n ?\^M))
  19383. ;; leave blank line before heading
  19384. (forward-char -1))))))
  19385. (point))
  19386. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19387. "Use Org version in org-mode, for dramatic speed-up."
  19388. (if (eq major-mode 'org-mode)
  19389. (progn
  19390. (org-end-of-subtree nil t)
  19391. (unless (eobp) (backward-char 1)))
  19392. ad-do-it))
  19393. (defun org-end-of-meta-data-and-drawers ()
  19394. "Jump to the first text after meta data and drawers in the current entry.
  19395. This will move over empty lines, lines with planning time stamps,
  19396. clocking lines, and drawers."
  19397. (org-back-to-heading t)
  19398. (let ((end (save-excursion (outline-next-heading) (point)))
  19399. (re (concat "\\(" org-drawer-regexp "\\)"
  19400. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19401. (forward-line 1)
  19402. (while (re-search-forward re end t)
  19403. (if (not (match-end 1))
  19404. ;; empty or planning line
  19405. (forward-line 1)
  19406. ;; a drawer, find the end
  19407. (re-search-forward "^[ \t]*:END:" end 'move)
  19408. (forward-line 1)))
  19409. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19410. (point)))
  19411. (defun org-forward-same-level (arg &optional invisible-ok)
  19412. "Move forward to the arg'th subheading at same level as this one.
  19413. Stop at the first and last subheadings of a superior heading.
  19414. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19415. it wil also look at invisible ones."
  19416. (interactive "p")
  19417. (org-back-to-heading invisible-ok)
  19418. (org-at-heading-p)
  19419. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19420. (re (format "^\\*\\{1,%d\\} " level))
  19421. l)
  19422. (forward-char 1)
  19423. (while (> arg 0)
  19424. (while (and (re-search-forward re nil 'move)
  19425. (setq l (- (match-end 0) (match-beginning 0) 1))
  19426. (= l level)
  19427. (not invisible-ok)
  19428. (progn (backward-char 1) (outline-invisible-p)))
  19429. (if (< l level) (setq arg 1)))
  19430. (setq arg (1- arg)))
  19431. (beginning-of-line 1)))
  19432. (defun org-backward-same-level (arg &optional invisible-ok)
  19433. "Move backward to the arg'th subheading at same level as this one.
  19434. Stop at the first and last subheadings of a superior heading."
  19435. (interactive "p")
  19436. (org-back-to-heading)
  19437. (org-at-heading-p)
  19438. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19439. (re (format "^\\*\\{1,%d\\} " level))
  19440. l)
  19441. (while (> arg 0)
  19442. (while (and (re-search-backward re nil 'move)
  19443. (setq l (- (match-end 0) (match-beginning 0) 1))
  19444. (= l level)
  19445. (not invisible-ok)
  19446. (outline-invisible-p))
  19447. (if (< l level) (setq arg 1)))
  19448. (setq arg (1- arg)))))
  19449. (defun org-show-subtree ()
  19450. "Show everything after this heading at deeper levels."
  19451. (outline-flag-region
  19452. (point)
  19453. (save-excursion
  19454. (org-end-of-subtree t t))
  19455. nil))
  19456. (defun org-show-entry ()
  19457. "Show the body directly following this heading.
  19458. Show the heading too, if it is currently invisible."
  19459. (interactive)
  19460. (save-excursion
  19461. (condition-case nil
  19462. (progn
  19463. (org-back-to-heading t)
  19464. (outline-flag-region
  19465. (max (point-min) (1- (point)))
  19466. (save-excursion
  19467. (if (re-search-forward
  19468. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19469. (match-beginning 1)
  19470. (point-max)))
  19471. nil)
  19472. (org-cycle-hide-drawers 'children))
  19473. (error nil))))
  19474. (defun org-make-options-regexp (kwds &optional extra)
  19475. "Make a regular expression for keyword lines."
  19476. (concat
  19477. "^"
  19478. "#?[ \t]*\\+\\("
  19479. (mapconcat 'regexp-quote kwds "\\|")
  19480. (if extra (concat "\\|" extra))
  19481. "\\):[ \t]*"
  19482. "\\(.*\\)"))
  19483. ;; Make isearch reveal the necessary context
  19484. (defun org-isearch-end ()
  19485. "Reveal context after isearch exits."
  19486. (when isearch-success ; only if search was successful
  19487. (if (featurep 'xemacs)
  19488. ;; Under XEmacs, the hook is run in the correct place,
  19489. ;; we directly show the context.
  19490. (org-show-context 'isearch)
  19491. ;; In Emacs the hook runs *before* restoring the overlays.
  19492. ;; So we have to use a one-time post-command-hook to do this.
  19493. ;; (Emacs 22 has a special variable, see function `org-mode')
  19494. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19495. isearch-mode-end-hook-quit)
  19496. ;; Only when the isearch was not quitted.
  19497. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19498. 'append 'local)))))
  19499. (defun org-isearch-post-command ()
  19500. "Remove self from hook, and show context."
  19501. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19502. (org-show-context 'isearch))
  19503. ;;;; Integration with and fixes for other packages
  19504. ;;; Imenu support
  19505. (defvar org-imenu-markers nil
  19506. "All markers currently used by Imenu.")
  19507. (make-variable-buffer-local 'org-imenu-markers)
  19508. (defun org-imenu-new-marker (&optional pos)
  19509. "Return a new marker for use by Imenu, and remember the marker."
  19510. (let ((m (make-marker)))
  19511. (move-marker m (or pos (point)))
  19512. (push m org-imenu-markers)
  19513. m))
  19514. (defun org-imenu-get-tree ()
  19515. "Produce the index for Imenu."
  19516. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19517. (setq org-imenu-markers nil)
  19518. (let* ((n org-imenu-depth)
  19519. (re (concat "^" (org-get-limited-outline-regexp)))
  19520. (subs (make-vector (1+ n) nil))
  19521. (last-level 0)
  19522. m level head)
  19523. (save-excursion
  19524. (save-restriction
  19525. (widen)
  19526. (goto-char (point-max))
  19527. (while (re-search-backward re nil t)
  19528. (setq level (org-reduced-level (funcall outline-level)))
  19529. (when (and (<= level n)
  19530. (looking-at org-complex-heading-regexp))
  19531. (setq head (org-link-display-format
  19532. (org-match-string-no-properties 4))
  19533. m (org-imenu-new-marker))
  19534. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19535. (if (>= level last-level)
  19536. (push (cons head m) (aref subs level))
  19537. (push (cons head (aref subs (1+ level))) (aref subs level))
  19538. (loop for i from (1+ level) to n do (aset subs i nil)))
  19539. (setq last-level level)))))
  19540. (aref subs 1)))
  19541. (eval-after-load "imenu"
  19542. '(progn
  19543. (add-hook 'imenu-after-jump-hook
  19544. (lambda ()
  19545. (if (eq major-mode 'org-mode)
  19546. (org-show-context 'org-goto))))))
  19547. (defun org-link-display-format (link)
  19548. "Replace a link with either the description, or the link target
  19549. if no description is present"
  19550. (save-match-data
  19551. (if (string-match org-bracket-link-analytic-regexp link)
  19552. (replace-match (if (match-end 5)
  19553. (match-string 5 link)
  19554. (concat (match-string 1 link)
  19555. (match-string 3 link)))
  19556. nil t link)
  19557. link)))
  19558. (defun org-toggle-link-display ()
  19559. "Toggle the literal or descriptive display of links."
  19560. (interactive)
  19561. (if org-descriptive-links
  19562. (progn (org-remove-from-invisibility-spec '(org-link))
  19563. (org-restart-font-lock)
  19564. (setq org-descriptive-links nil))
  19565. (progn (add-to-invisibility-spec '(org-link))
  19566. (org-restart-font-lock)
  19567. (setq org-descriptive-links t))))
  19568. ;; Speedbar support
  19569. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19570. "Overlay marking the agenda restriction line in speedbar.")
  19571. (overlay-put org-speedbar-restriction-lock-overlay
  19572. 'face 'org-agenda-restriction-lock)
  19573. (overlay-put org-speedbar-restriction-lock-overlay
  19574. 'help-echo "Agendas are currently limited to this item.")
  19575. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19576. (defun org-speedbar-set-agenda-restriction ()
  19577. "Restrict future agenda commands to the location at point in speedbar.
  19578. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19579. (interactive)
  19580. (require 'org-agenda)
  19581. (let (p m tp np dir txt)
  19582. (cond
  19583. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19584. 'org-imenu t))
  19585. (setq m (get-text-property p 'org-imenu-marker))
  19586. (with-current-buffer (marker-buffer m)
  19587. (goto-char m)
  19588. (org-agenda-set-restriction-lock 'subtree)))
  19589. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19590. 'speedbar-function 'speedbar-find-file))
  19591. (setq tp (previous-single-property-change
  19592. (1+ p) 'speedbar-function)
  19593. np (next-single-property-change
  19594. tp 'speedbar-function)
  19595. dir (speedbar-line-directory)
  19596. txt (buffer-substring-no-properties (or tp (point-min))
  19597. (or np (point-max))))
  19598. (with-current-buffer (find-file-noselect
  19599. (let ((default-directory dir))
  19600. (expand-file-name txt)))
  19601. (unless (eq major-mode 'org-mode)
  19602. (error "Cannot restrict to non-Org-mode file"))
  19603. (org-agenda-set-restriction-lock 'file)))
  19604. (t (error "Don't know how to restrict Org-mode's agenda")))
  19605. (move-overlay org-speedbar-restriction-lock-overlay
  19606. (point-at-bol) (point-at-eol))
  19607. (setq current-prefix-arg nil)
  19608. (org-agenda-maybe-redo)))
  19609. (eval-after-load "speedbar"
  19610. '(progn
  19611. (speedbar-add-supported-extension ".org")
  19612. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19613. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19614. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19615. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19616. (add-hook 'speedbar-visiting-tag-hook
  19617. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19618. ;;; Fixes and Hacks for problems with other packages
  19619. ;; Make flyspell not check words in links, to not mess up our keymap
  19620. (defun org-mode-flyspell-verify ()
  19621. "Don't let flyspell put overlays at active buttons, or on
  19622. {todo,all-time,additional-option-like}-keywords."
  19623. (let ((pos (max (1- (point)) (point-min)))
  19624. (word (thing-at-point 'word)))
  19625. (and (not (get-text-property pos 'keymap))
  19626. (not (get-text-property pos 'org-no-flyspell))
  19627. (not (member word org-todo-keywords-1))
  19628. (not (member word org-all-time-keywords))
  19629. (not (member word org-additional-option-like-keywords)))))
  19630. (defun org-remove-flyspell-overlays-in (beg end)
  19631. "Remove flyspell overlays in region."
  19632. (and (org-bound-and-true-p flyspell-mode)
  19633. (fboundp 'flyspell-delete-region-overlays)
  19634. (flyspell-delete-region-overlays beg end))
  19635. (add-text-properties beg end '(org-no-flyspell t)))
  19636. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19637. (eval-after-load "bookmark"
  19638. '(if (boundp 'bookmark-after-jump-hook)
  19639. ;; We can use the hook
  19640. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19641. ;; Hook not available, use advice
  19642. (defadvice bookmark-jump (after org-make-visible activate)
  19643. "Make the position visible."
  19644. (org-bookmark-jump-unhide))))
  19645. ;; Make sure saveplace shows the location if it was hidden
  19646. (eval-after-load "saveplace"
  19647. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19648. "Make the position visible."
  19649. (org-bookmark-jump-unhide)))
  19650. ;; Make sure ecb shows the location if it was hidden
  19651. (eval-after-load "ecb"
  19652. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19653. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19654. (if (eq major-mode 'org-mode)
  19655. (org-show-context))))
  19656. (defun org-bookmark-jump-unhide ()
  19657. "Unhide the current position, to show the bookmark location."
  19658. (and (eq major-mode 'org-mode)
  19659. (or (outline-invisible-p)
  19660. (save-excursion (goto-char (max (point-min) (1- (point))))
  19661. (outline-invisible-p)))
  19662. (org-show-context 'bookmark-jump)))
  19663. ;; Make session.el ignore our circular variable
  19664. (eval-after-load "session"
  19665. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19666. ;;;; Experimental code
  19667. (defun org-closed-in-range ()
  19668. "Sparse tree of items closed in a certain time range.
  19669. Still experimental, may disappear in the future."
  19670. (interactive)
  19671. ;; Get the time interval from the user.
  19672. (let* ((time1 (org-float-time
  19673. (org-read-date nil 'to-time nil "Starting date: ")))
  19674. (time2 (org-float-time
  19675. (org-read-date nil 'to-time nil "End date:")))
  19676. ;; callback function
  19677. (callback (lambda ()
  19678. (let ((time
  19679. (org-float-time
  19680. (apply 'encode-time
  19681. (org-parse-time-string
  19682. (match-string 1))))))
  19683. ;; check if time in interval
  19684. (and (>= time time1) (<= time time2))))))
  19685. ;; make tree, check each match with the callback
  19686. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19687. ;;;; Finish up
  19688. (provide 'org)
  19689. (run-hooks 'org-load-hook)
  19690. ;;; org.el ends here