org.el 805 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.8.03
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  100. (declare-function org-at-clock-log-p "org-clock" ())
  101. (declare-function org-clock-timestamps-up "org-clock" ())
  102. (declare-function org-clock-timestamps-down "org-clock" ())
  103. ;; babel
  104. (require 'ob)
  105. (require 'ob-table)
  106. (require 'ob-lob)
  107. (require 'ob-ref)
  108. (require 'ob-tangle)
  109. (require 'ob-comint)
  110. (require 'ob-keys)
  111. ;; load languages based on value of `org-babel-load-languages'
  112. (defvar org-babel-load-languages)
  113. ;;;###autoload
  114. (defun org-babel-do-load-languages (sym value)
  115. "Load the languages defined in `org-babel-load-languages'."
  116. (set-default sym value)
  117. (mapc (lambda (pair)
  118. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  119. (if active
  120. (progn
  121. (require (intern (concat "ob-" lang))))
  122. (progn
  123. (funcall 'fmakunbound
  124. (intern (concat "org-babel-execute:" lang)))
  125. (funcall 'fmakunbound
  126. (intern (concat "org-babel-expand-body:" lang)))))))
  127. org-babel-load-languages))
  128. (defcustom org-babel-load-languages '((emacs-lisp . t))
  129. "Languages which can be evaluated in Org-mode buffers.
  130. This list can be used to load support for any of the languages
  131. below, note that each language will depend on a different set of
  132. system executables and/or Emacs modes. When a language is
  133. \"loaded\", then code blocks in that language can be evaluated
  134. with `org-babel-execute-src-block' bound by default to C-c
  135. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  136. be set to remove code block evaluation from the C-c C-c
  137. keybinding. By default only Emacs Lisp (which has no
  138. requirements) is loaded."
  139. :group 'org-babel
  140. :set 'org-babel-do-load-languages
  141. :version "24.1"
  142. :type '(alist :tag "Babel Languages"
  143. :key-type
  144. (choice
  145. (const :tag "Awk" awk)
  146. (const :tag "C" C)
  147. (const :tag "R" R)
  148. (const :tag "Asymptote" asymptote)
  149. (const :tag "Calc" calc)
  150. (const :tag "Clojure" clojure)
  151. (const :tag "CSS" css)
  152. (const :tag "Ditaa" ditaa)
  153. (const :tag "Dot" dot)
  154. (const :tag "Emacs Lisp" emacs-lisp)
  155. (const :tag "Fortran" fortran)
  156. (const :tag "Gnuplot" gnuplot)
  157. (const :tag "Haskell" haskell)
  158. (const :tag "IO" io)
  159. (const :tag "Java" java)
  160. (const :tag "Javascript" js)
  161. (const :tag "LaTeX" latex)
  162. (const :tag "Ledger" ledger)
  163. (const :tag "Lilypond" lilypond)
  164. (const :tag "Maxima" maxima)
  165. (const :tag "Matlab" matlab)
  166. (const :tag "Mscgen" mscgen)
  167. (const :tag "Ocaml" ocaml)
  168. (const :tag "Octave" octave)
  169. (const :tag "Org" org)
  170. (const :tag "Perl" perl)
  171. (const :tag "Pico Lisp" picolisp)
  172. (const :tag "PlantUML" plantuml)
  173. (const :tag "Python" python)
  174. (const :tag "Ruby" ruby)
  175. (const :tag "Sass" sass)
  176. (const :tag "Scala" scala)
  177. (const :tag "Scheme" scheme)
  178. (const :tag "Screen" screen)
  179. (const :tag "Shell Script" sh)
  180. (const :tag "Shen" shen)
  181. (const :tag "Sql" sql)
  182. (const :tag "Sqlite" sqlite))
  183. :value-type (boolean :tag "Activate" :value t)))
  184. ;;;; Customization variables
  185. (defcustom org-clone-delete-id nil
  186. "Remove ID property of clones of a subtree.
  187. When non-nil, clones of a subtree don't inherit the ID property.
  188. Otherwise they inherit the ID property with a new unique
  189. identifier."
  190. :type 'boolean
  191. :version "24.1"
  192. :group 'org-id)
  193. ;;; Version
  194. (defconst org-version "7.8.03"
  195. "The version number of the file org.el.")
  196. ;;;###autoload
  197. (defun org-version (&optional here)
  198. "Show the org-mode version in the echo area.
  199. With prefix arg HERE, insert it at point."
  200. (interactive "P")
  201. (let* ((origin default-directory)
  202. (version org-version)
  203. (git-version)
  204. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  205. (when (and (file-exists-p (expand-file-name ".git" dir))
  206. (executable-find "git"))
  207. (unwind-protect
  208. (progn
  209. (cd dir)
  210. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  211. (with-current-buffer "*Shell Command Output*"
  212. (goto-char (point-min))
  213. (setq git-version (buffer-substring (point) (point-at-eol))))
  214. (subst-char-in-string ?- ?. git-version t)
  215. (when (string-match "\\S-"
  216. (shell-command-to-string
  217. "git diff-index --name-only HEAD --"))
  218. (setq git-version (concat git-version ".dirty")))
  219. (setq version (concat version " (" git-version ")"))))
  220. (cd origin)))
  221. (setq version (format "Org-mode version %s" version))
  222. (if here (insert version))
  223. (message version)))
  224. ;;; Compatibility constants
  225. ;;; The custom variables
  226. (defgroup org nil
  227. "Outline-based notes management and organizer."
  228. :tag "Org"
  229. :group 'outlines
  230. :group 'calendar)
  231. (defcustom org-mode-hook nil
  232. "Mode hook for Org-mode, run after the mode was turned on."
  233. :group 'org
  234. :type 'hook)
  235. (defcustom org-load-hook nil
  236. "Hook that is run after org.el has been loaded."
  237. :group 'org
  238. :type 'hook)
  239. (defcustom org-log-buffer-setup-hook nil
  240. "Hook that is run after an Org log buffer is created."
  241. :group 'org
  242. :version "24.1"
  243. :type 'hook)
  244. (defvar org-modules) ; defined below
  245. (defvar org-modules-loaded nil
  246. "Have the modules been loaded already?")
  247. (defun org-load-modules-maybe (&optional force)
  248. "Load all extensions listed in `org-modules'."
  249. (when (or force (not org-modules-loaded))
  250. (mapc (lambda (ext)
  251. (condition-case nil (require ext)
  252. (error (message "Problems while trying to load feature `%s'" ext))))
  253. org-modules)
  254. (setq org-modules-loaded t)))
  255. (defun org-set-modules (var value)
  256. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  257. (set var value)
  258. (when (featurep 'org)
  259. (org-load-modules-maybe 'force)))
  260. (when (org-bound-and-true-p org-modules)
  261. (let ((a (member 'org-infojs org-modules)))
  262. (and a (setcar a 'org-jsinfo))))
  263. (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)
  264. "Modules that should always be loaded together with org.el.
  265. If a description starts with <C>, the file is not part of Emacs
  266. and loading it will require that you have downloaded and properly installed
  267. the org-mode distribution.
  268. You can also use this system to load external packages (i.e. neither Org
  269. core modules, nor modules from the CONTRIB directory). Just add symbols
  270. to the end of the list. If the package is called org-xyz.el, then you need
  271. to add the symbol `xyz', and the package must have a call to
  272. (provide 'org-xyz)"
  273. :group 'org
  274. :set 'org-set-modules
  275. :type
  276. '(set :greedy t
  277. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  278. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  279. (const :tag " crypt: Encryption of subtrees" org-crypt)
  280. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  281. (const :tag " docview: Links to doc-view buffers" org-docview)
  282. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  283. (const :tag " id: Global IDs for identifying entries" org-id)
  284. (const :tag " info: Links to Info nodes" org-info)
  285. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  286. (const :tag " habit: Track your consistency with habits" org-habit)
  287. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  288. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  289. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  290. (const :tag " mew Links to Mew folders/messages" org-mew)
  291. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  292. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  293. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  294. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  295. (const :tag " vm: Links to VM folders/messages" org-vm)
  296. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  297. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  298. (const :tag " mouse: Additional mouse support" org-mouse)
  299. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  300. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  301. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  302. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  303. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  304. (const :tag "C collector: Collect properties into tables" org-collector)
  305. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  306. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  307. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  308. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  309. (const :tag "C eval: Include command output as text" org-eval)
  310. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  311. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  312. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  313. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  314. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  315. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  316. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  317. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  318. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  319. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  320. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  321. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  322. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  323. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  324. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  325. (const :tag "C registry: A registry for Org-mode links" org-registry)
  326. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  327. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  328. (const :tag "C secretary: Team management with org-mode" org-secretary)
  329. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  330. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  331. (const :tag "C track: Keep up with Org-mode development" org-track)
  332. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  333. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  334. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  335. (defcustom org-support-shift-select nil
  336. "Non-nil means make shift-cursor commands select text when possible.
  337. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  338. start selecting a region, or enlarge regions started in this way.
  339. In Org-mode, in special contexts, these same keys are used for
  340. other purposes, important enough to compete with shift selection.
  341. Org tries to balance these needs by supporting `shift-select-mode'
  342. outside these special contexts, under control of this variable.
  343. The default of this variable is nil, to avoid confusing behavior. Shifted
  344. cursor keys will then execute Org commands in the following contexts:
  345. - on a headline, changing TODO state (left/right) and priority (up/down)
  346. - on a time stamp, changing the time
  347. - in a plain list item, changing the bullet type
  348. - in a property definition line, switching between allowed values
  349. - in the BEGIN line of a clock table (changing the time block).
  350. Outside these contexts, the commands will throw an error.
  351. When this variable is t and the cursor is not in a special
  352. context, Org-mode will support shift-selection for making and
  353. enlarging regions. To make this more effective, the bullet
  354. cycling will no longer happen anywhere in an item line, but only
  355. if the cursor is exactly on the bullet.
  356. If you set this variable to the symbol `always', then the keys
  357. will not be special in headlines, property lines, and item lines,
  358. to make shift selection work there as well. If this is what you
  359. want, you can use the following alternative commands: `C-c C-t'
  360. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  361. can be used to switch TODO sets, `C-c -' to cycle item bullet
  362. types, and properties can be edited by hand or in column view.
  363. However, when the cursor is on a timestamp, shift-cursor commands
  364. will still edit the time stamp - this is just too good to give up.
  365. XEmacs user should have this variable set to nil, because
  366. `shift-select-mode' is in Emacs 23 or later only."
  367. :group 'org
  368. :type '(choice
  369. (const :tag "Never" nil)
  370. (const :tag "When outside special context" t)
  371. (const :tag "Everywhere except timestamps" always)))
  372. (defcustom org-loop-over-headlines-in-active-region nil
  373. "Shall some commands act upon headlines in the active region?
  374. When set to `t', some commands will be performed in all headlines
  375. within the active region.
  376. When set to `start-level', some commands will be performed in all
  377. headlines within the active region, provided that these headlines
  378. are of the same level than the first one.
  379. When set to a string, those commands will be performed on the
  380. matching headlines within the active region. Such string must be
  381. a tags/property/todo match as it is used in the agenda tags view.
  382. The list of commands is: `org-schedule', `org-deadline',
  383. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  384. `org-archive-to-archive-sibling'. The archiving commands skip
  385. already archived entries."
  386. :type '(choice (const :tag "Don't loop" nil)
  387. (const :tag "All headlines in active region" t)
  388. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  389. (string :tag "Tags/Property/Todo matcher"))
  390. :version "24.1"
  391. :group 'org-todo
  392. :group 'org-archive)
  393. (defgroup org-startup nil
  394. "Options concerning startup of Org-mode."
  395. :tag "Org Startup"
  396. :group 'org)
  397. (defcustom org-startup-folded t
  398. "Non-nil means entering Org-mode will switch to OVERVIEW.
  399. This can also be configured on a per-file basis by adding one of
  400. the following lines anywhere in the buffer:
  401. #+STARTUP: fold (or `overview', this is equivalent)
  402. #+STARTUP: nofold (or `showall', this is equivalent)
  403. #+STARTUP: content
  404. #+STARTUP: showeverything"
  405. :group 'org-startup
  406. :type '(choice
  407. (const :tag "nofold: show all" nil)
  408. (const :tag "fold: overview" t)
  409. (const :tag "content: all headlines" content)
  410. (const :tag "show everything, even drawers" showeverything)))
  411. (defcustom org-startup-truncated t
  412. "Non-nil means entering Org-mode will set `truncate-lines'.
  413. This is useful since some lines containing links can be very long and
  414. uninteresting. Also tables look terrible when wrapped."
  415. :group 'org-startup
  416. :type 'boolean)
  417. (defcustom org-startup-indented nil
  418. "Non-nil means turn on `org-indent-mode' on startup.
  419. This can also be configured on a per-file basis by adding one of
  420. the following lines anywhere in the buffer:
  421. #+STARTUP: indent
  422. #+STARTUP: noindent"
  423. :group 'org-structure
  424. :type '(choice
  425. (const :tag "Not" nil)
  426. (const :tag "Globally (slow on startup in large files)" t)))
  427. (defcustom org-use-sub-superscripts t
  428. "Non-nil means interpret \"_\" and \"^\" for export.
  429. When this option is turned on, you can use TeX-like syntax for sub- and
  430. superscripts. Several characters after \"_\" or \"^\" will be
  431. considered as a single item - so grouping with {} is normally not
  432. needed. For example, the following things will be parsed as single
  433. sub- or superscripts.
  434. 10^24 or 10^tau several digits will be considered 1 item.
  435. 10^-12 or 10^-tau a leading sign with digits or a word
  436. x^2-y^3 will be read as x^2 - y^3, because items are
  437. terminated by almost any nonword/nondigit char.
  438. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  439. Still, ambiguity is possible - so when in doubt use {} to enclose the
  440. sub/superscript. If you set this variable to the symbol `{}',
  441. the braces are *required* in order to trigger interpretations as
  442. sub/superscript. This can be helpful in documents that need \"_\"
  443. frequently in plain text.
  444. Not all export backends support this, but HTML does.
  445. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  446. :group 'org-startup
  447. :group 'org-export-translation
  448. :version "24.1"
  449. :type '(choice
  450. (const :tag "Always interpret" t)
  451. (const :tag "Only with braces" {})
  452. (const :tag "Never interpret" nil)))
  453. (if (fboundp 'defvaralias)
  454. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  455. (defcustom org-startup-with-beamer-mode nil
  456. "Non-nil means turn on `org-beamer-mode' on startup.
  457. This can also be configured on a per-file basis by adding one of
  458. the following lines anywhere in the buffer:
  459. #+STARTUP: beamer"
  460. :group 'org-startup
  461. :version "24.1"
  462. :type 'boolean)
  463. (defcustom org-startup-align-all-tables nil
  464. "Non-nil means align all tables when visiting a file.
  465. This is useful when the column width in tables is forced with <N> cookies
  466. in table fields. Such tables will look correct only after the first re-align.
  467. This can also be configured on a per-file basis by adding one of
  468. the following lines anywhere in the buffer:
  469. #+STARTUP: align
  470. #+STARTUP: noalign"
  471. :group 'org-startup
  472. :type 'boolean)
  473. (defcustom org-startup-with-inline-images nil
  474. "Non-nil means show inline images when loading a new Org file.
  475. This can also be configured on a per-file basis by adding one of
  476. the following lines anywhere in the buffer:
  477. #+STARTUP: inlineimages
  478. #+STARTUP: noinlineimages"
  479. :group 'org-startup
  480. :version "24.1"
  481. :type 'boolean)
  482. (defcustom org-insert-mode-line-in-empty-file nil
  483. "Non-nil means insert the first line setting Org-mode in empty files.
  484. When the function `org-mode' is called interactively in an empty file, this
  485. normally means that the file name does not automatically trigger Org-mode.
  486. To ensure that the file will always be in Org-mode in the future, a
  487. line enforcing Org-mode will be inserted into the buffer, if this option
  488. has been set."
  489. :group 'org-startup
  490. :type 'boolean)
  491. (defcustom org-replace-disputed-keys nil
  492. "Non-nil means use alternative key bindings for some keys.
  493. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  494. These keys are also used by other packages like shift-selection-mode'
  495. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  496. If you want to use Org-mode together with one of these other modes,
  497. or more generally if you would like to move some Org-mode commands to
  498. other keys, set this variable and configure the keys with the variable
  499. `org-disputed-keys'.
  500. This option is only relevant at load-time of Org-mode, and must be set
  501. *before* org.el is loaded. Changing it requires a restart of Emacs to
  502. become effective."
  503. :group 'org-startup
  504. :type 'boolean)
  505. (defcustom org-use-extra-keys nil
  506. "Non-nil means use extra key sequence definitions for certain commands.
  507. This happens automatically if you run XEmacs or if `window-system'
  508. is nil. This variable lets you do the same manually. You must
  509. set it before loading org.
  510. Example: on Carbon Emacs 22 running graphically, with an external
  511. keyboard on a Powerbook, the default way of setting M-left might
  512. not work for either Alt or ESC. Setting this variable will make
  513. it work for ESC."
  514. :group 'org-startup
  515. :type 'boolean)
  516. (if (fboundp 'defvaralias)
  517. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  518. (defcustom org-disputed-keys
  519. '(([(shift up)] . [(meta p)])
  520. ([(shift down)] . [(meta n)])
  521. ([(shift left)] . [(meta -)])
  522. ([(shift right)] . [(meta +)])
  523. ([(control shift right)] . [(meta shift +)])
  524. ([(control shift left)] . [(meta shift -)]))
  525. "Keys for which Org-mode and other modes compete.
  526. This is an alist, cars are the default keys, second element specifies
  527. the alternative to use when `org-replace-disputed-keys' is t.
  528. Keys can be specified in any syntax supported by `define-key'.
  529. The value of this option takes effect only at Org-mode's startup,
  530. therefore you'll have to restart Emacs to apply it after changing."
  531. :group 'org-startup
  532. :type 'alist)
  533. (defun org-key (key)
  534. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  535. Or return the original if not disputed.
  536. Also apply the translations defined in `org-xemacs-key-equivalents'."
  537. (when org-replace-disputed-keys
  538. (let* ((nkey (key-description key))
  539. (x (org-find-if (lambda (x)
  540. (equal (key-description (car x)) nkey))
  541. org-disputed-keys)))
  542. (setq key (if x (cdr x) key))))
  543. (when (featurep 'xemacs)
  544. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  545. key)
  546. (defun org-find-if (predicate seq)
  547. (catch 'exit
  548. (while seq
  549. (if (funcall predicate (car seq))
  550. (throw 'exit (car seq))
  551. (pop seq)))))
  552. (defun org-defkey (keymap key def)
  553. "Define a key, possibly translated, as returned by `org-key'."
  554. (define-key keymap (org-key key) def))
  555. (defcustom org-ellipsis nil
  556. "The ellipsis to use in the Org-mode outline.
  557. When nil, just use the standard three dots. When a string, use that instead,
  558. When a face, use the standard 3 dots, but with the specified face.
  559. The change affects only Org-mode (which will then use its own display table).
  560. Changing this requires executing `M-x org-mode' in a buffer to become
  561. effective."
  562. :group 'org-startup
  563. :type '(choice (const :tag "Default" nil)
  564. (face :tag "Face" :value org-warning)
  565. (string :tag "String" :value "...#")))
  566. (defvar org-display-table nil
  567. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  568. (defgroup org-keywords nil
  569. "Keywords in Org-mode."
  570. :tag "Org Keywords"
  571. :group 'org)
  572. (defcustom org-deadline-string "DEADLINE:"
  573. "String to mark deadline entries.
  574. A deadline is this string, followed by a time stamp. Should be a word,
  575. terminated by a colon. You can insert a schedule keyword and
  576. a timestamp with \\[org-deadline].
  577. Changes become only effective after restarting Emacs."
  578. :group 'org-keywords
  579. :type 'string)
  580. (defcustom org-scheduled-string "SCHEDULED:"
  581. "String to mark scheduled TODO entries.
  582. A schedule is this string, followed by a time stamp. Should be a word,
  583. terminated by a colon. You can insert a schedule keyword and
  584. a timestamp with \\[org-schedule].
  585. Changes become only effective after restarting Emacs."
  586. :group 'org-keywords
  587. :type 'string)
  588. (defcustom org-closed-string "CLOSED:"
  589. "String used as the prefix for timestamps logging closing a TODO entry."
  590. :group 'org-keywords
  591. :type 'string)
  592. (defcustom org-clock-string "CLOCK:"
  593. "String used as prefix for timestamps clocking work hours on an item."
  594. :group 'org-keywords
  595. :type 'string)
  596. (defcustom org-comment-string "COMMENT"
  597. "Entries starting with this keyword will never be exported.
  598. An entry can be toggled between COMMENT and normal with
  599. \\[org-toggle-comment].
  600. Changes become only effective after restarting Emacs."
  601. :group 'org-keywords
  602. :type 'string)
  603. (defcustom org-quote-string "QUOTE"
  604. "Entries starting with this keyword will be exported in fixed-width font.
  605. Quoting applies only to the text in the entry following the headline, and does
  606. not extend beyond the next headline, even if that is lower level.
  607. An entry can be toggled between QUOTE and normal with
  608. \\[org-toggle-fixed-width-section]."
  609. :group 'org-keywords
  610. :type 'string)
  611. (defconst org-repeat-re
  612. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  613. "Regular expression for specifying repeated events.
  614. After a match, group 1 contains the repeat expression.")
  615. (defgroup org-structure nil
  616. "Options concerning the general structure of Org-mode files."
  617. :tag "Org Structure"
  618. :group 'org)
  619. (defgroup org-reveal-location nil
  620. "Options about how to make context of a location visible."
  621. :tag "Org Reveal Location"
  622. :group 'org-structure)
  623. (defconst org-context-choice
  624. '(choice
  625. (const :tag "Always" t)
  626. (const :tag "Never" nil)
  627. (repeat :greedy t :tag "Individual contexts"
  628. (cons
  629. (choice :tag "Context"
  630. (const agenda)
  631. (const org-goto)
  632. (const occur-tree)
  633. (const tags-tree)
  634. (const link-search)
  635. (const mark-goto)
  636. (const bookmark-jump)
  637. (const isearch)
  638. (const default))
  639. (boolean))))
  640. "Contexts for the reveal options.")
  641. (defcustom org-show-hierarchy-above '((default . t))
  642. "Non-nil means show full hierarchy when revealing a location.
  643. Org-mode often shows locations in an org-mode file which might have
  644. been invisible before. When this is set, the hierarchy of headings
  645. above the exposed location is shown.
  646. Turning this off for example for sparse trees makes them very compact.
  647. Instead of t, this can also be an alist specifying this option for different
  648. contexts. Valid contexts are
  649. agenda when exposing an entry from the agenda
  650. org-goto when using the command `org-goto' on key C-c C-j
  651. occur-tree when using the command `org-occur' on key C-c /
  652. tags-tree when constructing a sparse tree based on tags matches
  653. link-search when exposing search matches associated with a link
  654. mark-goto when exposing the jump goal of a mark
  655. bookmark-jump when exposing a bookmark location
  656. isearch when exiting from an incremental search
  657. default default for all contexts not set explicitly"
  658. :group 'org-reveal-location
  659. :type org-context-choice)
  660. (defcustom org-show-following-heading '((default . nil))
  661. "Non-nil means show following heading when revealing a location.
  662. Org-mode often shows locations in an org-mode file which might have
  663. been invisible before. When this is set, the heading following the
  664. match is shown.
  665. Turning this off for example for sparse trees makes them very compact,
  666. but makes it harder to edit the location of the match. In such a case,
  667. use the command \\[org-reveal] to show more context.
  668. Instead of t, this can also be an alist specifying this option for different
  669. contexts. See `org-show-hierarchy-above' for valid contexts."
  670. :group 'org-reveal-location
  671. :type org-context-choice)
  672. (defcustom org-show-siblings '((default . nil) (isearch t))
  673. "Non-nil means show all sibling heading when revealing a location.
  674. Org-mode often shows locations in an org-mode file which might have
  675. been invisible before. When this is set, the sibling of the current entry
  676. heading are all made visible. If `org-show-hierarchy-above' is t,
  677. the same happens on each level of the hierarchy above the current entry.
  678. By default this is on for the isearch context, off for all other contexts.
  679. Turning this off for example for sparse trees makes them very compact,
  680. but makes it harder to edit the location of the match. In such a case,
  681. use the command \\[org-reveal] to show more context.
  682. Instead of t, this can also be an alist specifying this option for different
  683. contexts. See `org-show-hierarchy-above' for valid contexts."
  684. :group 'org-reveal-location
  685. :type org-context-choice)
  686. (defcustom org-show-entry-below '((default . nil))
  687. "Non-nil means show the entry below a headline when revealing a location.
  688. Org-mode often shows locations in an org-mode file which might have
  689. been invisible before. When this is set, the text below the headline that is
  690. exposed is also shown.
  691. By default this is off for all contexts.
  692. Instead of t, this can also be an alist specifying this option for different
  693. contexts. See `org-show-hierarchy-above' for valid contexts."
  694. :group 'org-reveal-location
  695. :type org-context-choice)
  696. (defcustom org-indirect-buffer-display 'other-window
  697. "How should indirect tree buffers be displayed?
  698. This applies to indirect buffers created with the commands
  699. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  700. Valid values are:
  701. current-window Display in the current window
  702. other-window Just display in another window.
  703. dedicated-frame Create one new frame, and re-use it each time.
  704. new-frame Make a new frame each time. Note that in this case
  705. previously-made indirect buffers are kept, and you need to
  706. kill these buffers yourself."
  707. :group 'org-structure
  708. :group 'org-agenda-windows
  709. :type '(choice
  710. (const :tag "In current window" current-window)
  711. (const :tag "In current frame, other window" other-window)
  712. (const :tag "Each time a new frame" new-frame)
  713. (const :tag "One dedicated frame" dedicated-frame)))
  714. (defcustom org-use-speed-commands nil
  715. "Non-nil means activate single letter commands at beginning of a headline.
  716. This may also be a function to test for appropriate locations where speed
  717. commands should be active."
  718. :group 'org-structure
  719. :type '(choice
  720. (const :tag "Never" nil)
  721. (const :tag "At beginning of headline stars" t)
  722. (function)))
  723. (defcustom org-speed-commands-user nil
  724. "Alist of additional speed commands.
  725. This list will be checked before `org-speed-commands-default'
  726. when the variable `org-use-speed-commands' is non-nil
  727. and when the cursor is at the beginning of a headline.
  728. The car if each entry is a string with a single letter, which must
  729. be assigned to `self-insert-command' in the global map.
  730. The cdr is either a command to be called interactively, a function
  731. to be called, or a form to be evaluated.
  732. An entry that is just a list with a single string will be interpreted
  733. as a descriptive headline that will be added when listing the speed
  734. commands in the Help buffer using the `?' speed command."
  735. :group 'org-structure
  736. :type '(repeat :value ("k" . ignore)
  737. (choice :value ("k" . ignore)
  738. (list :tag "Descriptive Headline" (string :tag "Headline"))
  739. (cons :tag "Letter and Command"
  740. (string :tag "Command letter")
  741. (choice
  742. (function)
  743. (sexp))))))
  744. (defgroup org-cycle nil
  745. "Options concerning visibility cycling in Org-mode."
  746. :tag "Org Cycle"
  747. :group 'org-structure)
  748. (defcustom org-cycle-skip-children-state-if-no-children t
  749. "Non-nil means skip CHILDREN state in entries that don't have any."
  750. :group 'org-cycle
  751. :type 'boolean)
  752. (defcustom org-cycle-max-level nil
  753. "Maximum level which should still be subject to visibility cycling.
  754. Levels higher than this will, for cycling, be treated as text, not a headline.
  755. When `org-odd-levels-only' is set, a value of N in this variable actually
  756. means 2N-1 stars as the limiting headline.
  757. When nil, cycle all levels.
  758. Note that the limiting level of cycling is also influenced by
  759. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  760. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  761. than its value."
  762. :group 'org-cycle
  763. :type '(choice
  764. (const :tag "No limit" nil)
  765. (integer :tag "Maximum level")))
  766. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  767. "Names of drawers. Drawers are not opened by cycling on the headline above.
  768. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  769. this:
  770. :DRAWERNAME:
  771. .....
  772. :END:
  773. The drawer \"PROPERTIES\" is special for capturing properties through
  774. the property API.
  775. Drawers can be defined on the per-file basis with a line like:
  776. #+DRAWERS: HIDDEN STATE PROPERTIES"
  777. :group 'org-structure
  778. :group 'org-cycle
  779. :type '(repeat (string :tag "Drawer Name")))
  780. (defcustom org-hide-block-startup nil
  781. "Non-nil means entering Org-mode will fold all blocks.
  782. This can also be set in on a per-file basis with
  783. #+STARTUP: hideblocks
  784. #+STARTUP: showblocks"
  785. :group 'org-startup
  786. :group 'org-cycle
  787. :type 'boolean)
  788. (defcustom org-cycle-global-at-bob nil
  789. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  790. This makes it possible to do global cycling without having to use S-TAB or
  791. \\[universal-argument] TAB. For this special case to work, the first line \
  792. of the buffer
  793. must not be a headline - it may be empty or some other text. When used in
  794. this way, `org-cycle-hook' is disables temporarily, to make sure the
  795. cursor stays at the beginning of the buffer.
  796. When this option is nil, don't do anything special at the beginning
  797. of the buffer."
  798. :group 'org-cycle
  799. :type 'boolean)
  800. (defcustom org-cycle-level-after-item/entry-creation t
  801. "Non-nil means cycle entry level or item indentation in new empty entries.
  802. When the cursor is at the end of an empty headline, i.e with only stars
  803. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  804. and then all possible ancestor states, before returning to the original state.
  805. This makes data entry extremely fast: M-RET to create a new headline,
  806. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  807. When the cursor is at the end of an empty plain list item, one TAB will
  808. make it a subitem, two or more tabs will back up to make this an item
  809. higher up in the item hierarchy."
  810. :group 'org-cycle
  811. :type 'boolean)
  812. (defcustom org-cycle-emulate-tab t
  813. "Where should `org-cycle' emulate TAB.
  814. nil Never
  815. white Only in completely white lines
  816. whitestart Only at the beginning of lines, before the first non-white char
  817. t Everywhere except in headlines
  818. exc-hl-bol Everywhere except at the start of a headline
  819. If TAB is used in a place where it does not emulate TAB, the current subtree
  820. visibility is cycled."
  821. :group 'org-cycle
  822. :type '(choice (const :tag "Never" nil)
  823. (const :tag "Only in completely white lines" white)
  824. (const :tag "Before first char in a line" whitestart)
  825. (const :tag "Everywhere except in headlines" t)
  826. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  827. ))
  828. (defcustom org-cycle-separator-lines 2
  829. "Number of empty lines needed to keep an empty line between collapsed trees.
  830. If you leave an empty line between the end of a subtree and the following
  831. headline, this empty line is hidden when the subtree is folded.
  832. Org-mode will leave (exactly) one empty line visible if the number of
  833. empty lines is equal or larger to the number given in this variable.
  834. So the default 2 means at least 2 empty lines after the end of a subtree
  835. are needed to produce free space between a collapsed subtree and the
  836. following headline.
  837. If the number is negative, and the number of empty lines is at least -N,
  838. all empty lines are shown.
  839. Special case: when 0, never leave empty lines in collapsed view."
  840. :group 'org-cycle
  841. :type 'integer)
  842. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  843. (defcustom org-pre-cycle-hook nil
  844. "Hook that is run before visibility cycling is happening.
  845. The function(s) in this hook must accept a single argument which indicates
  846. the new state that will be set right after running this hook. The
  847. argument is a symbol. Before a global state change, it can have the values
  848. `overview', `content', or `all'. Before a local state change, it can have
  849. the values `folded', `children', or `subtree'."
  850. :group 'org-cycle
  851. :type 'hook)
  852. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  853. org-cycle-hide-drawers
  854. org-cycle-show-empty-lines
  855. org-optimize-window-after-visibility-change)
  856. "Hook that is run after `org-cycle' has changed the buffer visibility.
  857. The function(s) in this hook must accept a single argument which indicates
  858. the new state that was set by the most recent `org-cycle' command. The
  859. argument is a symbol. After a global state change, it can have the values
  860. `overview', `content', or `all'. After a local state change, it can have
  861. the values `folded', `children', or `subtree'."
  862. :group 'org-cycle
  863. :type 'hook)
  864. (defgroup org-edit-structure nil
  865. "Options concerning structure editing in Org-mode."
  866. :tag "Org Edit Structure"
  867. :group 'org-structure)
  868. (defcustom org-odd-levels-only nil
  869. "Non-nil means skip even levels and only use odd levels for the outline.
  870. This has the effect that two stars are being added/taken away in
  871. promotion/demotion commands. It also influences how levels are
  872. handled by the exporters.
  873. Changing it requires restart of `font-lock-mode' to become effective
  874. for fontification also in regions already fontified.
  875. You may also set this on a per-file basis by adding one of the following
  876. lines to the buffer:
  877. #+STARTUP: odd
  878. #+STARTUP: oddeven"
  879. :group 'org-edit-structure
  880. :group 'org-appearance
  881. :type 'boolean)
  882. (defcustom org-adapt-indentation t
  883. "Non-nil means adapt indentation to outline node level.
  884. When this variable is set, Org assumes that you write outlines by
  885. indenting text in each node to align with the headline (after the stars).
  886. The following issues are influenced by this variable:
  887. - When this is set and the *entire* text in an entry is indented, the
  888. indentation is increased by one space in a demotion command, and
  889. decreased by one in a promotion command. If any line in the entry
  890. body starts with text at column 0, indentation is not changed at all.
  891. - Property drawers and planning information is inserted indented when
  892. this variable s set. When nil, they will not be indented.
  893. - TAB indents a line relative to context. The lines below a headline
  894. will be indented when this variable is set.
  895. Note that this is all about true indentation, by adding and removing
  896. space characters. See also `org-indent.el' which does level-dependent
  897. indentation in a virtual way, i.e. at display time in Emacs."
  898. :group 'org-edit-structure
  899. :type 'boolean)
  900. (defcustom org-special-ctrl-a/e nil
  901. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  902. When t, `C-a' will bring back the cursor to the beginning of the
  903. headline text, i.e. after the stars and after a possible TODO
  904. keyword. In an item, this will be the position after bullet and
  905. check-box, if any. When the cursor is already at that position,
  906. another `C-a' will bring it to the beginning of the line.
  907. `C-e' will jump to the end of the headline, ignoring the presence
  908. of tags in the headline. A second `C-e' will then jump to the
  909. true end of the line, after any tags. This also means that, when
  910. this variable is non-nil, `C-e' also will never jump beyond the
  911. end of the heading of a folded section, i.e. not after the
  912. ellipses.
  913. When set to the symbol `reversed', the first `C-a' or `C-e' works
  914. normally, going to the true line boundary first. Only a directly
  915. following, identical keypress will bring the cursor to the
  916. special positions.
  917. This may also be a cons cell where the behavior for `C-a' and
  918. `C-e' is set separately."
  919. :group 'org-edit-structure
  920. :type '(choice
  921. (const :tag "off" nil)
  922. (const :tag "on: after stars/bullet and before tags first" t)
  923. (const :tag "reversed: true line boundary first" reversed)
  924. (cons :tag "Set C-a and C-e separately"
  925. (choice :tag "Special C-a"
  926. (const :tag "off" nil)
  927. (const :tag "on: after stars/bullet first" t)
  928. (const :tag "reversed: before stars/bullet first" reversed))
  929. (choice :tag "Special C-e"
  930. (const :tag "off" nil)
  931. (const :tag "on: before tags first" t)
  932. (const :tag "reversed: after tags first" reversed)))))
  933. (if (fboundp 'defvaralias)
  934. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  935. (defcustom org-special-ctrl-k nil
  936. "Non-nil means `C-k' will behave specially in headlines.
  937. When nil, `C-k' will call the default `kill-line' command.
  938. When t, the following will happen while the cursor is in the headline:
  939. - When the cursor is at the beginning of a headline, kill the entire
  940. line and possible the folded subtree below the line.
  941. - When in the middle of the headline text, kill the headline up to the tags.
  942. - When after the headline text, kill the tags."
  943. :group 'org-edit-structure
  944. :type 'boolean)
  945. (defcustom org-ctrl-k-protect-subtree nil
  946. "Non-nil means, do not delete a hidden subtree with C-k.
  947. When set to the symbol `error', simply throw an error when C-k is
  948. used to kill (part-of) a headline that has hidden text behind it.
  949. Any other non-nil value will result in a query to the user, if it is
  950. OK to kill that hidden subtree. When nil, kill without remorse."
  951. :group 'org-edit-structure
  952. :version "24.1"
  953. :type '(choice
  954. (const :tag "Do not protect hidden subtrees" nil)
  955. (const :tag "Protect hidden subtrees with a security query" t)
  956. (const :tag "Never kill a hidden subtree with C-k" error)))
  957. (defcustom org-catch-invisible-edits nil
  958. "Check if in invisible region before inserting or deleting a character.
  959. Valid values are:
  960. nil Do not check, so just do invisible edits.
  961. error Throw an error and do nothing.
  962. show Make point visible, and do the requested edit.
  963. show-and-error Make point visible, then throw an error and abort the edit.
  964. smart Make point visible, and do insertion/deletion if it is
  965. adjacent to visible text and the change feels predictable.
  966. Never delete a previously invisible character or add in the
  967. middle or right after an invisible region. Basically, this
  968. allows insertion and backward-delete right before ellipses.
  969. FIXME: maybe in this case we should not even show?"
  970. :group 'org-edit-structure
  971. :version "24.1"
  972. :type '(choice
  973. (const :tag "Do not check" nil)
  974. (const :tag "Throw error when trying to edit" error)
  975. (const :tag "Unhide, but do not do the edit" show-and-error)
  976. (const :tag "Show invisible part and do the edit" show)
  977. (const :tag "Be smart and do the right thing" smart)))
  978. (defcustom org-yank-folded-subtrees t
  979. "Non-nil means when yanking subtrees, fold them.
  980. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  981. it starts with a heading and all other headings in it are either children
  982. or siblings, then fold all the subtrees. However, do this only if no
  983. text after the yank would be swallowed into a folded tree by this action."
  984. :group 'org-edit-structure
  985. :type 'boolean)
  986. (defcustom org-yank-adjusted-subtrees nil
  987. "Non-nil means when yanking subtrees, adjust the level.
  988. With this setting, `org-paste-subtree' is used to insert the subtree, see
  989. this function for details."
  990. :group 'org-edit-structure
  991. :type 'boolean)
  992. (defcustom org-M-RET-may-split-line '((default . t))
  993. "Non-nil means M-RET will split the line at the cursor position.
  994. When nil, it will go to the end of the line before making a
  995. new line.
  996. You may also set this option in a different way for different
  997. contexts. Valid contexts are:
  998. headline when creating a new headline
  999. item when creating a new item
  1000. table in a table field
  1001. default the value to be used for all contexts not explicitly
  1002. customized"
  1003. :group 'org-structure
  1004. :group 'org-table
  1005. :type '(choice
  1006. (const :tag "Always" t)
  1007. (const :tag "Never" nil)
  1008. (repeat :greedy t :tag "Individual contexts"
  1009. (cons
  1010. (choice :tag "Context"
  1011. (const headline)
  1012. (const item)
  1013. (const table)
  1014. (const default))
  1015. (boolean)))))
  1016. (defcustom org-insert-heading-respect-content nil
  1017. "Non-nil means insert new headings after the current subtree.
  1018. When nil, the new heading is created directly after the current line.
  1019. The commands \\[org-insert-heading-respect-content] and
  1020. \\[org-insert-todo-heading-respect-content] turn this variable on
  1021. for the duration of the command."
  1022. :group 'org-structure
  1023. :type 'boolean)
  1024. (defcustom org-blank-before-new-entry '((heading . auto)
  1025. (plain-list-item . auto))
  1026. "Should `org-insert-heading' leave a blank line before new heading/item?
  1027. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1028. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1029. which case Org will look at the surrounding headings/items and try to
  1030. make an intelligent decision whether to insert a blank line or not.
  1031. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1032. set, the setting here is ignored and no empty line is inserted, to avoid
  1033. breaking the list structure."
  1034. :group 'org-edit-structure
  1035. :type '(list
  1036. (cons (const heading)
  1037. (choice (const :tag "Never" nil)
  1038. (const :tag "Always" t)
  1039. (const :tag "Auto" auto)))
  1040. (cons (const plain-list-item)
  1041. (choice (const :tag "Never" nil)
  1042. (const :tag "Always" t)
  1043. (const :tag "Auto" auto)))))
  1044. (defcustom org-insert-heading-hook nil
  1045. "Hook being run after inserting a new heading."
  1046. :group 'org-edit-structure
  1047. :type 'hook)
  1048. (defcustom org-enable-fixed-width-editor t
  1049. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1050. This currently only means they are never auto-wrapped.
  1051. When nil, such lines will be treated like ordinary lines.
  1052. See also the QUOTE keyword."
  1053. :group 'org-edit-structure
  1054. :type 'boolean)
  1055. (defcustom org-goto-auto-isearch t
  1056. "Non-nil means typing characters in `org-goto' starts incremental search."
  1057. :group 'org-edit-structure
  1058. :type 'boolean)
  1059. (defgroup org-sparse-trees nil
  1060. "Options concerning sparse trees in Org-mode."
  1061. :tag "Org Sparse Trees"
  1062. :group 'org-structure)
  1063. (defcustom org-highlight-sparse-tree-matches t
  1064. "Non-nil means highlight all matches that define a sparse tree.
  1065. The highlights will automatically disappear the next time the buffer is
  1066. changed by an edit command."
  1067. :group 'org-sparse-trees
  1068. :type 'boolean)
  1069. (defcustom org-remove-highlights-with-change t
  1070. "Non-nil means any change to the buffer will remove temporary highlights.
  1071. Such highlights are created by `org-occur' and `org-clock-display'.
  1072. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1073. The highlights created by `org-preview-latex-fragment' always need
  1074. `C-c C-c' to be removed."
  1075. :group 'org-sparse-trees
  1076. :group 'org-time
  1077. :type 'boolean)
  1078. (defcustom org-occur-hook '(org-first-headline-recenter)
  1079. "Hook that is run after `org-occur' has constructed a sparse tree.
  1080. This can be used to recenter the window to show as much of the structure
  1081. as possible."
  1082. :group 'org-sparse-trees
  1083. :type 'hook)
  1084. (defgroup org-imenu-and-speedbar nil
  1085. "Options concerning imenu and speedbar in Org-mode."
  1086. :tag "Org Imenu and Speedbar"
  1087. :group 'org-structure)
  1088. (defcustom org-imenu-depth 2
  1089. "The maximum level for Imenu access to Org-mode headlines.
  1090. This also applied for speedbar access."
  1091. :group 'org-imenu-and-speedbar
  1092. :type 'integer)
  1093. (defgroup org-table nil
  1094. "Options concerning tables in Org-mode."
  1095. :tag "Org Table"
  1096. :group 'org)
  1097. (defcustom org-enable-table-editor 'optimized
  1098. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1099. When nil, such lines will be treated like ordinary lines.
  1100. When equal to the symbol `optimized', the table editor will be optimized to
  1101. do the following:
  1102. - Automatic overwrite mode in front of whitespace in table fields.
  1103. This makes the structure of the table stay in tact as long as the edited
  1104. field does not exceed the column width.
  1105. - Minimize the number of realigns. Normally, the table is aligned each time
  1106. TAB or RET are pressed to move to another field. With optimization this
  1107. happens only if changes to a field might have changed the column width.
  1108. Optimization requires replacing the functions `self-insert-command',
  1109. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1110. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1111. very good at guessing when a re-align will be necessary, but you can always
  1112. force one with \\[org-ctrl-c-ctrl-c].
  1113. If you would like to use the optimized version in Org-mode, but the
  1114. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1115. This variable can be used to turn on and off the table editor during a session,
  1116. but in order to toggle optimization, a restart is required.
  1117. See also the variable `org-table-auto-blank-field'."
  1118. :group 'org-table
  1119. :type '(choice
  1120. (const :tag "off" nil)
  1121. (const :tag "on" t)
  1122. (const :tag "on, optimized" optimized)))
  1123. (defcustom org-self-insert-cluster-for-undo t
  1124. "Non-nil means cluster self-insert commands for undo when possible.
  1125. If this is set, then, like in the Emacs command loop, 20 consecutive
  1126. characters will be undone together.
  1127. This is configurable, because there is some impact on typing performance."
  1128. :group 'org-table
  1129. :type 'boolean)
  1130. (defcustom org-table-tab-recognizes-table.el t
  1131. "Non-nil means TAB will automatically notice a table.el table.
  1132. When it sees such a table, it moves point into it and - if necessary -
  1133. calls `table-recognize-table'."
  1134. :group 'org-table-editing
  1135. :type 'boolean)
  1136. (defgroup org-link nil
  1137. "Options concerning links in Org-mode."
  1138. :tag "Org Link"
  1139. :group 'org)
  1140. (defvar org-link-abbrev-alist-local nil
  1141. "Buffer-local version of `org-link-abbrev-alist', which see.
  1142. The value of this is taken from the #+LINK lines.")
  1143. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1144. (defcustom org-link-abbrev-alist nil
  1145. "Alist of link abbreviations.
  1146. The car of each element is a string, to be replaced at the start of a link.
  1147. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1148. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1149. [[linkkey:tag][description]]
  1150. The 'linkkey' must be a word word, starting with a letter, followed
  1151. by letters, numbers, '-' or '_'.
  1152. If REPLACE is a string, the tag will simply be appended to create the link.
  1153. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1154. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1155. be inserted at that point (see the function `url-hexify-string').
  1156. REPLACE may also be a function that will be called with the tag as the
  1157. only argument to create the link, which should be returned as a string.
  1158. See the manual for examples."
  1159. :group 'org-link
  1160. :type '(repeat
  1161. (cons
  1162. (string :tag "Protocol")
  1163. (choice
  1164. (string :tag "Format")
  1165. (function)))))
  1166. (defcustom org-descriptive-links t
  1167. "Non-nil means Org will display descriptive links.
  1168. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1169. \"Org Website\", hiding the link itself and just displaying its
  1170. description. When set to `nil', Org will display the full links
  1171. literally.
  1172. You can interactively set the value of this variable by calling
  1173. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1174. :group 'org-link
  1175. :type 'boolean)
  1176. (defcustom org-link-file-path-type 'adaptive
  1177. "How the path name in file links should be stored.
  1178. Valid values are:
  1179. relative Relative to the current directory, i.e. the directory of the file
  1180. into which the link is being inserted.
  1181. absolute Absolute path, if possible with ~ for home directory.
  1182. noabbrev Absolute path, no abbreviation of home directory.
  1183. adaptive Use relative path for files in the current directory and sub-
  1184. directories of it. For other files, use an absolute path."
  1185. :group 'org-link
  1186. :type '(choice
  1187. (const relative)
  1188. (const absolute)
  1189. (const noabbrev)
  1190. (const adaptive)))
  1191. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1192. "Types of links that should be activated in Org-mode files.
  1193. This is a list of symbols, each leading to the activation of a certain link
  1194. type. In principle, it does not hurt to turn on most link types - there may
  1195. be a small gain when turning off unused link types. The types are:
  1196. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1197. angle Links in angular brackets that may contain whitespace like
  1198. <bbdb:Carsten Dominik>.
  1199. plain Plain links in normal text, no whitespace, like http://google.com.
  1200. radio Text that is matched by a radio target, see manual for details.
  1201. tag Tag settings in a headline (link to tag search).
  1202. date Time stamps (link to calendar).
  1203. footnote Footnote labels.
  1204. Changing this variable requires a restart of Emacs to become effective."
  1205. :group 'org-link
  1206. :type '(set :greedy t
  1207. (const :tag "Double bracket links" bracket)
  1208. (const :tag "Angular bracket links" angle)
  1209. (const :tag "Plain text links" plain)
  1210. (const :tag "Radio target matches" radio)
  1211. (const :tag "Tags" tag)
  1212. (const :tag "Timestamps" date)
  1213. (const :tag "Footnotes" footnote)))
  1214. (defcustom org-make-link-description-function nil
  1215. "Function to use to generate link descriptions from links.
  1216. If nil the link location will be used. This function must take
  1217. two parameters; the first is the link and the second the
  1218. description `org-insert-link' has generated, and should return the
  1219. description to use."
  1220. :group 'org-link
  1221. :type 'function)
  1222. (defgroup org-link-store nil
  1223. "Options concerning storing links in Org-mode."
  1224. :tag "Org Store Link"
  1225. :group 'org-link)
  1226. (defcustom org-email-link-description-format "Email %c: %.30s"
  1227. "Format of the description part of a link to an email or usenet message.
  1228. The following %-escapes will be replaced by corresponding information:
  1229. %F full \"From\" field
  1230. %f name, taken from \"From\" field, address if no name
  1231. %T full \"To\" field
  1232. %t first name in \"To\" field, address if no name
  1233. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1234. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1235. %s subject
  1236. %d date
  1237. %m message-id.
  1238. You may use normal field width specification between the % and the letter.
  1239. This is for example useful to limit the length of the subject.
  1240. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1241. :group 'org-link-store
  1242. :type 'string)
  1243. (defcustom org-from-is-user-regexp
  1244. (let (r1 r2)
  1245. (when (and user-mail-address (not (string= user-mail-address "")))
  1246. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1247. (when (and user-full-name (not (string= user-full-name "")))
  1248. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1249. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1250. "Regexp matched against the \"From:\" header of an email or usenet message.
  1251. It should match if the message is from the user him/herself."
  1252. :group 'org-link-store
  1253. :type 'regexp)
  1254. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1255. "Non-nil means storing a link to an Org file will use entry IDs.
  1256. Note that before this variable is even considered, org-id must be loaded,
  1257. so please customize `org-modules' and turn it on.
  1258. The variable can have the following values:
  1259. t Create an ID if needed to make a link to the current entry.
  1260. create-if-interactive
  1261. If `org-store-link' is called directly (interactively, as a user
  1262. command), do create an ID to support the link. But when doing the
  1263. job for remember, only use the ID if it already exists. The
  1264. purpose of this setting is to avoid proliferation of unwanted
  1265. IDs, just because you happen to be in an Org file when you
  1266. call `org-remember' that automatically and preemptively
  1267. creates a link. If you do want to get an ID link in a remember
  1268. template to an entry not having an ID, create it first by
  1269. explicitly creating a link to it, using `C-c C-l' first.
  1270. create-if-interactive-and-no-custom-id
  1271. Like create-if-interactive, but do not create an ID if there is
  1272. a CUSTOM_ID property defined in the entry. This is the default.
  1273. use-existing
  1274. Use existing ID, do not create one.
  1275. nil Never use an ID to make a link, instead link using a text search for
  1276. the headline text."
  1277. :group 'org-link-store
  1278. :type '(choice
  1279. (const :tag "Create ID to make link" t)
  1280. (const :tag "Create if storing link interactively"
  1281. create-if-interactive)
  1282. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1283. create-if-interactive-and-no-custom-id)
  1284. (const :tag "Only use existing" use-existing)
  1285. (const :tag "Do not use ID to create link" nil)))
  1286. (defcustom org-context-in-file-links t
  1287. "Non-nil means file links from `org-store-link' contain context.
  1288. A search string will be added to the file name with :: as separator and
  1289. used to find the context when the link is activated by the command
  1290. `org-open-at-point'. When this option is t, the entire active region
  1291. will be placed in the search string of the file link. If set to a
  1292. positive integer, only the first n lines of context will be stored.
  1293. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1294. negates this setting for the duration of the command."
  1295. :group 'org-link-store
  1296. :type '(choice boolean integer))
  1297. (defcustom org-keep-stored-link-after-insertion nil
  1298. "Non-nil means keep link in list for entire session.
  1299. The command `org-store-link' adds a link pointing to the current
  1300. location to an internal list. These links accumulate during a session.
  1301. The command `org-insert-link' can be used to insert links into any
  1302. Org-mode file (offering completion for all stored links). When this
  1303. option is nil, every link which has been inserted once using \\[org-insert-link]
  1304. will be removed from the list, to make completing the unused links
  1305. more efficient."
  1306. :group 'org-link-store
  1307. :type 'boolean)
  1308. (defgroup org-link-follow nil
  1309. "Options concerning following links in Org-mode."
  1310. :tag "Org Follow Link"
  1311. :group 'org-link)
  1312. (defcustom org-link-translation-function nil
  1313. "Function to translate links with different syntax to Org syntax.
  1314. This can be used to translate links created for example by the Planner
  1315. or emacs-wiki packages to Org syntax.
  1316. The function must accept two parameters, a TYPE containing the link
  1317. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1318. which is everything after the link protocol. It should return a cons
  1319. with possibly modified values of type and path.
  1320. Org contains a function for this, so if you set this variable to
  1321. `org-translate-link-from-planner', you should be able follow many
  1322. links created by planner."
  1323. :group 'org-link-follow
  1324. :type 'function)
  1325. (defcustom org-follow-link-hook nil
  1326. "Hook that is run after a link has been followed."
  1327. :group 'org-link-follow
  1328. :type 'hook)
  1329. (defcustom org-tab-follows-link nil
  1330. "Non-nil means on links TAB will follow the link.
  1331. Needs to be set before org.el is loaded.
  1332. This really should not be used, it does not make sense, and the
  1333. implementation is bad."
  1334. :group 'org-link-follow
  1335. :type 'boolean)
  1336. (defcustom org-return-follows-link nil
  1337. "Non-nil means on links RET will follow the link."
  1338. :group 'org-link-follow
  1339. :type 'boolean)
  1340. (defcustom org-mouse-1-follows-link
  1341. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1342. "Non-nil means mouse-1 on a link will follow the link.
  1343. A longer mouse click will still set point. Does not work on XEmacs.
  1344. Needs to be set before org.el is loaded."
  1345. :group 'org-link-follow
  1346. :type 'boolean)
  1347. (defcustom org-mark-ring-length 4
  1348. "Number of different positions to be recorded in the ring.
  1349. Changing this requires a restart of Emacs to work correctly."
  1350. :group 'org-link-follow
  1351. :type 'integer)
  1352. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1353. "Non-nil means internal links in Org files must exactly match a headline.
  1354. When nil, the link search tries to match a phrase with all words
  1355. in the search text."
  1356. :group 'org-link-follow
  1357. :version "24.1"
  1358. :type '(choice
  1359. (const :tag "Use fuzzy text search" nil)
  1360. (const :tag "Match only exact headline" t)
  1361. (const :tag "Match exact headline or query to create it"
  1362. query-to-create)))
  1363. (defcustom org-link-frame-setup
  1364. '((vm . vm-visit-folder-other-frame)
  1365. (vm-imap . vm-visit-imap-folder-other-frame)
  1366. (gnus . org-gnus-no-new-news)
  1367. (file . find-file-other-window)
  1368. (wl . wl-other-frame))
  1369. "Setup the frame configuration for following links.
  1370. When following a link with Emacs, it may often be useful to display
  1371. this link in another window or frame. This variable can be used to
  1372. set this up for the different types of links.
  1373. For VM, use any of
  1374. `vm-visit-folder'
  1375. `vm-visit-folder-other-window'
  1376. `vm-visit-folder-other-frame'
  1377. For Gnus, use any of
  1378. `gnus'
  1379. `gnus-other-frame'
  1380. `org-gnus-no-new-news'
  1381. For FILE, use any of
  1382. `find-file'
  1383. `find-file-other-window'
  1384. `find-file-other-frame'
  1385. For Wanderlust use any of
  1386. `wl'
  1387. `wl-other-frame'
  1388. For the calendar, use the variable `calendar-setup'.
  1389. For BBDB, it is currently only possible to display the matches in
  1390. another window."
  1391. :group 'org-link-follow
  1392. :type '(list
  1393. (cons (const vm)
  1394. (choice
  1395. (const vm-visit-folder)
  1396. (const vm-visit-folder-other-window)
  1397. (const vm-visit-folder-other-frame)))
  1398. (cons (const gnus)
  1399. (choice
  1400. (const gnus)
  1401. (const gnus-other-frame)
  1402. (const org-gnus-no-new-news)))
  1403. (cons (const file)
  1404. (choice
  1405. (const find-file)
  1406. (const find-file-other-window)
  1407. (const find-file-other-frame)))
  1408. (cons (const wl)
  1409. (choice
  1410. (const wl)
  1411. (const wl-other-frame)))))
  1412. (defcustom org-display-internal-link-with-indirect-buffer nil
  1413. "Non-nil means use indirect buffer to display infile links.
  1414. Activating internal links (from one location in a file to another location
  1415. in the same file) normally just jumps to the location. When the link is
  1416. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1417. is displayed in
  1418. another window. When this option is set, the other window actually displays
  1419. an indirect buffer clone of the current buffer, to avoid any visibility
  1420. changes to the current buffer."
  1421. :group 'org-link-follow
  1422. :type 'boolean)
  1423. (defcustom org-open-non-existing-files nil
  1424. "Non-nil means `org-open-file' will open non-existing files.
  1425. When nil, an error will be generated.
  1426. This variable applies only to external applications because they
  1427. might choke on non-existing files. If the link is to a file that
  1428. will be opened in Emacs, the variable is ignored."
  1429. :group 'org-link-follow
  1430. :type 'boolean)
  1431. (defcustom org-open-directory-means-index-dot-org nil
  1432. "Non-nil means a link to a directory really means to index.org.
  1433. When nil, following a directory link will run dired or open a finder/explorer
  1434. window on that directory."
  1435. :group 'org-link-follow
  1436. :type 'boolean)
  1437. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1438. "Function and arguments to call for following mailto links.
  1439. This is a list with the first element being a Lisp function, and the
  1440. remaining elements being arguments to the function. In string arguments,
  1441. %a will be replaced by the address, and %s will be replaced by the subject
  1442. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1443. :group 'org-link-follow
  1444. :type '(choice
  1445. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1446. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1447. (const :tag "message-mail" (message-mail "%a" "%s"))
  1448. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1449. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1450. "Non-nil means ask for confirmation before executing shell links.
  1451. Shell links can be dangerous: just think about a link
  1452. [[shell:rm -rf ~/*][Google Search]]
  1453. This link would show up in your Org-mode document as \"Google Search\",
  1454. but really it would remove your entire home directory.
  1455. Therefore we advise against setting this variable to nil.
  1456. Just change it to `y-or-n-p' if you want to confirm with a
  1457. single keystroke rather than having to type \"yes\"."
  1458. :group 'org-link-follow
  1459. :type '(choice
  1460. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1461. (const :tag "with y-or-n (faster)" y-or-n-p)
  1462. (const :tag "no confirmation (dangerous)" nil)))
  1463. (put 'org-confirm-shell-link-function
  1464. 'safe-local-variable
  1465. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1466. (defcustom org-confirm-shell-link-not-regexp ""
  1467. "A regexp to skip confirmation for shell links."
  1468. :group 'org-link-follow
  1469. :version "24.1"
  1470. :type 'regexp)
  1471. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1472. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1473. Elisp links can be dangerous: just think about a link
  1474. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1475. This link would show up in your Org-mode document as \"Google Search\",
  1476. but really it would remove your entire home directory.
  1477. Therefore we advise against setting this variable to nil.
  1478. Just change it to `y-or-n-p' if you want to confirm with a
  1479. single keystroke rather than having to type \"yes\"."
  1480. :group 'org-link-follow
  1481. :type '(choice
  1482. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1483. (const :tag "with y-or-n (faster)" y-or-n-p)
  1484. (const :tag "no confirmation (dangerous)" nil)))
  1485. (put 'org-confirm-shell-link-function
  1486. 'safe-local-variable
  1487. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1488. (defcustom org-confirm-elisp-link-not-regexp ""
  1489. "A regexp to skip confirmation for Elisp links."
  1490. :group 'org-link-follow
  1491. :version "24.1"
  1492. :type 'regexp)
  1493. (defconst org-file-apps-defaults-gnu
  1494. '((remote . emacs)
  1495. (system . mailcap)
  1496. (t . mailcap))
  1497. "Default file applications on a UNIX or GNU/Linux system.
  1498. See `org-file-apps'.")
  1499. (defconst org-file-apps-defaults-macosx
  1500. '((remote . emacs)
  1501. (t . "open %s")
  1502. (system . "open %s")
  1503. ("ps.gz" . "gv %s")
  1504. ("eps.gz" . "gv %s")
  1505. ("dvi" . "xdvi %s")
  1506. ("fig" . "xfig %s"))
  1507. "Default file applications on a MacOS X system.
  1508. The system \"open\" is known as a default, but we use X11 applications
  1509. for some files for which the OS does not have a good default.
  1510. See `org-file-apps'.")
  1511. (defconst org-file-apps-defaults-windowsnt
  1512. (list
  1513. '(remote . emacs)
  1514. (cons t
  1515. (list (if (featurep 'xemacs)
  1516. 'mswindows-shell-execute
  1517. 'w32-shell-execute)
  1518. "open" 'file))
  1519. (cons 'system
  1520. (list (if (featurep 'xemacs)
  1521. 'mswindows-shell-execute
  1522. 'w32-shell-execute)
  1523. "open" 'file)))
  1524. "Default file applications on a Windows NT system.
  1525. The system \"open\" is used for most files.
  1526. See `org-file-apps'.")
  1527. (defcustom org-file-apps
  1528. '(
  1529. (auto-mode . emacs)
  1530. ("\\.mm\\'" . default)
  1531. ("\\.x?html?\\'" . default)
  1532. ("\\.pdf\\'" . default)
  1533. )
  1534. "External applications for opening `file:path' items in a document.
  1535. Org-mode uses system defaults for different file types, but
  1536. you can use this variable to set the application for a given file
  1537. extension. The entries in this list are cons cells where the car identifies
  1538. files and the cdr the corresponding command. Possible values for the
  1539. file identifier are
  1540. \"string\" A string as a file identifier can be interpreted in different
  1541. ways, depending on its contents:
  1542. - Alphanumeric characters only:
  1543. Match links with this file extension.
  1544. Example: (\"pdf\" . \"evince %s\")
  1545. to open PDFs with evince.
  1546. - Regular expression: Match links where the
  1547. filename matches the regexp. If you want to
  1548. use groups here, use shy groups.
  1549. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1550. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1551. to open *.html and *.xhtml with firefox.
  1552. - Regular expression which contains (non-shy) groups:
  1553. Match links where the whole link, including \"::\", and
  1554. anything after that, matches the regexp.
  1555. In a custom command string, %1, %2, etc. are replaced with
  1556. the parts of the link that were matched by the groups.
  1557. For backwards compatibility, if a command string is given
  1558. that does not use any of the group matches, this case is
  1559. handled identically to the second one (i.e. match against
  1560. file name only).
  1561. In a custom lisp form, you can access the group matches with
  1562. (match-string n link).
  1563. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1564. to open [[file:document.pdf::5]] with evince at page 5.
  1565. `directory' Matches a directory
  1566. `remote' Matches a remote file, accessible through tramp or efs.
  1567. Remote files most likely should be visited through Emacs
  1568. because external applications cannot handle such paths.
  1569. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1570. so all files Emacs knows how to handle. Using this with
  1571. command `emacs' will open most files in Emacs. Beware that this
  1572. will also open html files inside Emacs, unless you add
  1573. (\"html\" . default) to the list as well.
  1574. t Default for files not matched by any of the other options.
  1575. `system' The system command to open files, like `open' on Windows
  1576. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1577. that will be selected if you call `C-c C-o' with a double
  1578. \\[universal-argument] \\[universal-argument] prefix.
  1579. Possible values for the command are:
  1580. `emacs' The file will be visited by the current Emacs process.
  1581. `default' Use the default application for this file type, which is the
  1582. association for t in the list, most likely in the system-specific
  1583. part.
  1584. This can be used to overrule an unwanted setting in the
  1585. system-specific variable.
  1586. `system' Use the system command for opening files, like \"open\".
  1587. This command is specified by the entry whose car is `system'.
  1588. Most likely, the system-specific version of this variable
  1589. does define this command, but you can overrule/replace it
  1590. here.
  1591. string A command to be executed by a shell; %s will be replaced
  1592. by the path to the file.
  1593. sexp A Lisp form which will be evaluated. The file path will
  1594. be available in the Lisp variable `file'.
  1595. For more examples, see the system specific constants
  1596. `org-file-apps-defaults-macosx'
  1597. `org-file-apps-defaults-windowsnt'
  1598. `org-file-apps-defaults-gnu'."
  1599. :group 'org-link-follow
  1600. :type '(repeat
  1601. (cons (choice :value ""
  1602. (string :tag "Extension")
  1603. (const :tag "System command to open files" system)
  1604. (const :tag "Default for unrecognized files" t)
  1605. (const :tag "Remote file" remote)
  1606. (const :tag "Links to a directory" directory)
  1607. (const :tag "Any files that have Emacs modes"
  1608. auto-mode))
  1609. (choice :value ""
  1610. (const :tag "Visit with Emacs" emacs)
  1611. (const :tag "Use default" default)
  1612. (const :tag "Use the system command" system)
  1613. (string :tag "Command")
  1614. (sexp :tag "Lisp form")))))
  1615. (defgroup org-refile nil
  1616. "Options concerning refiling entries in Org-mode."
  1617. :tag "Org Refile"
  1618. :group 'org)
  1619. (defcustom org-directory "~/org"
  1620. "Directory with org files.
  1621. This is just a default location to look for Org files. There is no need
  1622. at all to put your files into this directory. It is only used in the
  1623. following situations:
  1624. 1. When a remember template specifies a target file that is not an
  1625. absolute path. The path will then be interpreted relative to
  1626. `org-directory'
  1627. 2. When a remember note is filed away in an interactive way (when exiting the
  1628. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1629. with `org-directory' as the default path."
  1630. :group 'org-refile
  1631. :group 'org-remember
  1632. :type 'directory)
  1633. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1634. "Default target for storing notes.
  1635. Used as a fall back file for org-remember.el and org-capture.el, for
  1636. templates that do not specify a target file."
  1637. :group 'org-refile
  1638. :group 'org-remember
  1639. :type '(choice
  1640. (const :tag "Default from remember-data-file" nil)
  1641. file))
  1642. (defcustom org-goto-interface 'outline
  1643. "The default interface to be used for `org-goto'.
  1644. Allowed values are:
  1645. outline The interface shows an outline of the relevant file
  1646. and the correct heading is found by moving through
  1647. the outline or by searching with incremental search.
  1648. outline-path-completion Headlines in the current buffer are offered via
  1649. completion. This is the interface also used by
  1650. the refile command."
  1651. :group 'org-refile
  1652. :type '(choice
  1653. (const :tag "Outline" outline)
  1654. (const :tag "Outline-path-completion" outline-path-completion)))
  1655. (defcustom org-goto-max-level 5
  1656. "Maximum target level when running `org-goto' with refile interface."
  1657. :group 'org-refile
  1658. :type 'integer)
  1659. (defcustom org-reverse-note-order nil
  1660. "Non-nil means store new notes at the beginning of a file or entry.
  1661. When nil, new notes will be filed to the end of a file or entry.
  1662. This can also be a list with cons cells of regular expressions that
  1663. are matched against file names, and values."
  1664. :group 'org-remember
  1665. :group 'org-refile
  1666. :type '(choice
  1667. (const :tag "Reverse always" t)
  1668. (const :tag "Reverse never" nil)
  1669. (repeat :tag "By file name regexp"
  1670. (cons regexp boolean))))
  1671. (defcustom org-log-refile nil
  1672. "Information to record when a task is refiled.
  1673. Possible values are:
  1674. nil Don't add anything
  1675. time Add a time stamp to the task
  1676. note Prompt for a note and add it with template `org-log-note-headings'
  1677. This option can also be set with on a per-file-basis with
  1678. #+STARTUP: nologrefile
  1679. #+STARTUP: logrefile
  1680. #+STARTUP: lognoterefile
  1681. You can have local logging settings for a subtree by setting the LOGGING
  1682. property to one or more of these keywords.
  1683. When bulk-refiling from the agenda, the value `note' is forbidden and
  1684. will temporarily be changed to `time'."
  1685. :group 'org-refile
  1686. :group 'org-progress
  1687. :version "24.1"
  1688. :type '(choice
  1689. (const :tag "No logging" nil)
  1690. (const :tag "Record timestamp" time)
  1691. (const :tag "Record timestamp with note." note)))
  1692. (defcustom org-refile-targets nil
  1693. "Targets for refiling entries with \\[org-refile].
  1694. This is a list of cons cells. Each cell contains:
  1695. - a specification of the files to be considered, either a list of files,
  1696. or a symbol whose function or variable value will be used to retrieve
  1697. a file name or a list of file names. If you use `org-agenda-files' for
  1698. that, all agenda files will be scanned for targets. Nil means consider
  1699. headings in the current buffer.
  1700. - A specification of how to find candidate refile targets. This may be
  1701. any of:
  1702. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1703. This tag has to be present in all target headlines, inheritance will
  1704. not be considered.
  1705. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1706. todo keyword.
  1707. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1708. headlines that are refiling targets.
  1709. - a cons cell (:level . N). Any headline of level N is considered a target.
  1710. Note that, when `org-odd-levels-only' is set, level corresponds to
  1711. order in hierarchy, not to the number of stars.
  1712. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1713. Note that, when `org-odd-levels-only' is set, level corresponds to
  1714. order in hierarchy, not to the number of stars.
  1715. Each element of this list generates a set of possible targets.
  1716. The union of these sets is presented (with completion) to
  1717. the user by `org-refile'.
  1718. You can set the variable `org-refile-target-verify-function' to a function
  1719. to verify each headline found by the simple criteria above.
  1720. When this variable is nil, all top-level headlines in the current buffer
  1721. are used, equivalent to the value `((nil . (:level . 1))'."
  1722. :group 'org-refile
  1723. :type '(repeat
  1724. (cons
  1725. (choice :value org-agenda-files
  1726. (const :tag "All agenda files" org-agenda-files)
  1727. (const :tag "Current buffer" nil)
  1728. (function) (variable) (file))
  1729. (choice :tag "Identify target headline by"
  1730. (cons :tag "Specific tag" (const :value :tag) (string))
  1731. (cons :tag "TODO keyword" (const :value :todo) (string))
  1732. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1733. (cons :tag "Level number" (const :value :level) (integer))
  1734. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1735. (defcustom org-refile-target-verify-function nil
  1736. "Function to verify if the headline at point should be a refile target.
  1737. The function will be called without arguments, with point at the
  1738. beginning of the headline. It should return t and leave point
  1739. where it is if the headline is a valid target for refiling.
  1740. If the target should not be selected, the function must return nil.
  1741. In addition to this, it may move point to a place from where the search
  1742. should be continued. For example, the function may decide that the entire
  1743. subtree of the current entry should be excluded and move point to the end
  1744. of the subtree."
  1745. :group 'org-refile
  1746. :type 'function)
  1747. (defcustom org-refile-use-cache nil
  1748. "Non-nil means cache refile targets to speed up the process.
  1749. The cache for a particular file will be updated automatically when
  1750. the buffer has been killed, or when any of the marker used for flagging
  1751. refile targets no longer points at a live buffer.
  1752. If you have added new entries to a buffer that might themselves be targets,
  1753. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1754. find that easier, `C-u C-u C-u C-c C-w'."
  1755. :group 'org-refile
  1756. :version "24.1"
  1757. :type 'boolean)
  1758. (defcustom org-refile-use-outline-path nil
  1759. "Non-nil means provide refile targets as paths.
  1760. So a level 3 headline will be available as level1/level2/level3.
  1761. When the value is `file', also include the file name (without directory)
  1762. into the path. In this case, you can also stop the completion after
  1763. the file name, to get entries inserted as top level in the file.
  1764. When `full-file-path', include the full file path."
  1765. :group 'org-refile
  1766. :type '(choice
  1767. (const :tag "Not" nil)
  1768. (const :tag "Yes" t)
  1769. (const :tag "Start with file name" file)
  1770. (const :tag "Start with full file path" full-file-path)))
  1771. (defcustom org-outline-path-complete-in-steps t
  1772. "Non-nil means complete the outline path in hierarchical steps.
  1773. When Org-mode uses the refile interface to select an outline path
  1774. \(see variable `org-refile-use-outline-path'), the completion of
  1775. the path can be done is a single go, or if can be done in steps down
  1776. the headline hierarchy. Going in steps is probably the best if you
  1777. do not use a special completion package like `ido' or `icicles'.
  1778. However, when using these packages, going in one step can be very
  1779. fast, while still showing the whole path to the entry."
  1780. :group 'org-refile
  1781. :type 'boolean)
  1782. (defcustom org-refile-allow-creating-parent-nodes nil
  1783. "Non-nil means allow to create new nodes as refile targets.
  1784. New nodes are then created by adding \"/new node name\" to the completion
  1785. of an existing node. When the value of this variable is `confirm',
  1786. new node creation must be confirmed by the user (recommended)
  1787. When nil, the completion must match an existing entry.
  1788. Note that, if the new heading is not seen by the criteria
  1789. listed in `org-refile-targets', multiple instances of the same
  1790. heading would be created by trying again to file under the new
  1791. heading."
  1792. :group 'org-refile
  1793. :type '(choice
  1794. (const :tag "Never" nil)
  1795. (const :tag "Always" t)
  1796. (const :tag "Prompt for confirmation" confirm)))
  1797. (defcustom org-refile-active-region-within-subtree nil
  1798. "Non-nil means also refile active region within a subtree.
  1799. By default `org-refile' doesn't allow refiling regions if they
  1800. don't contain a set of subtrees, but it might be convenient to
  1801. do so sometimes: in that case, the first line of the region is
  1802. converted to a headline before refiling."
  1803. :group 'org-refile
  1804. :version "24.1"
  1805. :type 'boolean)
  1806. (defgroup org-todo nil
  1807. "Options concerning TODO items in Org-mode."
  1808. :tag "Org TODO"
  1809. :group 'org)
  1810. (defgroup org-progress nil
  1811. "Options concerning Progress logging in Org-mode."
  1812. :tag "Org Progress"
  1813. :group 'org-time)
  1814. (defvar org-todo-interpretation-widgets
  1815. '((:tag "Sequence (cycling hits every state)" sequence)
  1816. (:tag "Type (cycling directly to DONE)" type))
  1817. "The available interpretation symbols for customizing `org-todo-keywords'.
  1818. Interested libraries should add to this list.")
  1819. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1820. "List of TODO entry keyword sequences and their interpretation.
  1821. \\<org-mode-map>This is a list of sequences.
  1822. Each sequence starts with a symbol, either `sequence' or `type',
  1823. indicating if the keywords should be interpreted as a sequence of
  1824. action steps, or as different types of TODO items. The first
  1825. keywords are states requiring action - these states will select a headline
  1826. for inclusion into the global TODO list Org-mode produces. If one of
  1827. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1828. signify that no further action is necessary. If \"|\" is not found,
  1829. the last keyword is treated as the only DONE state of the sequence.
  1830. The command \\[org-todo] cycles an entry through these states, and one
  1831. additional state where no keyword is present. For details about this
  1832. cycling, see the manual.
  1833. TODO keywords and interpretation can also be set on a per-file basis with
  1834. the special #+SEQ_TODO and #+TYP_TODO lines.
  1835. Each keyword can optionally specify a character for fast state selection
  1836. \(in combination with the variable `org-use-fast-todo-selection')
  1837. and specifiers for state change logging, using the same syntax
  1838. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1839. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1840. indicates to record a time stamp each time this state is selected.
  1841. Each keyword may also specify if a timestamp or a note should be
  1842. recorded when entering or leaving the state, by adding additional
  1843. characters in the parenthesis after the keyword. This looks like this:
  1844. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1845. record only the time of the state change. With X and Y being either
  1846. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1847. Y when leaving the state if and only if the *target* state does not
  1848. define X. You may omit any of the fast-selection key or X or /Y,
  1849. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1850. For backward compatibility, this variable may also be just a list
  1851. of keywords - in this case the interpretation (sequence or type) will be
  1852. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1853. :group 'org-todo
  1854. :group 'org-keywords
  1855. :type '(choice
  1856. (repeat :tag "Old syntax, just keywords"
  1857. (string :tag "Keyword"))
  1858. (repeat :tag "New syntax"
  1859. (cons
  1860. (choice
  1861. :tag "Interpretation"
  1862. ;;Quick and dirty way to see
  1863. ;;`org-todo-interpretations'. This takes the
  1864. ;;place of item arguments
  1865. :convert-widget
  1866. (lambda (widget)
  1867. (widget-put widget
  1868. :args (mapcar
  1869. #'(lambda (x)
  1870. (widget-convert
  1871. (cons 'const x)))
  1872. org-todo-interpretation-widgets))
  1873. widget))
  1874. (repeat
  1875. (string :tag "Keyword"))))))
  1876. (defvar org-todo-keywords-1 nil
  1877. "All TODO and DONE keywords active in a buffer.")
  1878. (make-variable-buffer-local 'org-todo-keywords-1)
  1879. (defvar org-todo-keywords-for-agenda nil)
  1880. (defvar org-done-keywords-for-agenda nil)
  1881. (defvar org-drawers-for-agenda nil)
  1882. (defvar org-todo-keyword-alist-for-agenda nil)
  1883. (defvar org-tag-alist-for-agenda nil)
  1884. (defvar org-agenda-contributing-files nil)
  1885. (defvar org-not-done-keywords nil)
  1886. (make-variable-buffer-local 'org-not-done-keywords)
  1887. (defvar org-done-keywords nil)
  1888. (make-variable-buffer-local 'org-done-keywords)
  1889. (defvar org-todo-heads nil)
  1890. (make-variable-buffer-local 'org-todo-heads)
  1891. (defvar org-todo-sets nil)
  1892. (make-variable-buffer-local 'org-todo-sets)
  1893. (defvar org-todo-log-states nil)
  1894. (make-variable-buffer-local 'org-todo-log-states)
  1895. (defvar org-todo-kwd-alist nil)
  1896. (make-variable-buffer-local 'org-todo-kwd-alist)
  1897. (defvar org-todo-key-alist nil)
  1898. (make-variable-buffer-local 'org-todo-key-alist)
  1899. (defvar org-todo-key-trigger nil)
  1900. (make-variable-buffer-local 'org-todo-key-trigger)
  1901. (defcustom org-todo-interpretation 'sequence
  1902. "Controls how TODO keywords are interpreted.
  1903. This variable is in principle obsolete and is only used for
  1904. backward compatibility, if the interpretation of todo keywords is
  1905. not given already in `org-todo-keywords'. See that variable for
  1906. more information."
  1907. :group 'org-todo
  1908. :group 'org-keywords
  1909. :type '(choice (const sequence)
  1910. (const type)))
  1911. (defcustom org-use-fast-todo-selection t
  1912. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1913. This variable describes if and under what circumstances the cycling
  1914. mechanism for TODO keywords will be replaced by a single-key, direct
  1915. selection scheme.
  1916. When nil, fast selection is never used.
  1917. When the symbol `prefix', it will be used when `org-todo' is called with
  1918. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1919. in an agenda buffer.
  1920. When t, fast selection is used by default. In this case, the prefix
  1921. argument forces cycling instead.
  1922. In all cases, the special interface is only used if access keys have actually
  1923. been assigned by the user, i.e. if keywords in the configuration are followed
  1924. by a letter in parenthesis, like TODO(t)."
  1925. :group 'org-todo
  1926. :type '(choice
  1927. (const :tag "Never" nil)
  1928. (const :tag "By default" t)
  1929. (const :tag "Only with C-u C-c C-t" prefix)))
  1930. (defcustom org-provide-todo-statistics t
  1931. "Non-nil means update todo statistics after insert and toggle.
  1932. ALL-HEADLINES means update todo statistics by including headlines
  1933. with no TODO keyword as well, counting them as not done.
  1934. A list of TODO keywords means the same, but skip keywords that are
  1935. not in this list.
  1936. When this is set, todo statistics is updated in the parent of the
  1937. current entry each time a todo state is changed."
  1938. :group 'org-todo
  1939. :type '(choice
  1940. (const :tag "Yes, only for TODO entries" t)
  1941. (const :tag "Yes, including all entries" 'all-headlines)
  1942. (repeat :tag "Yes, for TODOs in this list"
  1943. (string :tag "TODO keyword"))
  1944. (other :tag "No TODO statistics" nil)))
  1945. (defcustom org-hierarchical-todo-statistics t
  1946. "Non-nil means TODO statistics covers just direct children.
  1947. When nil, all entries in the subtree are considered.
  1948. This has only an effect if `org-provide-todo-statistics' is set.
  1949. To set this to nil for only a single subtree, use a COOKIE_DATA
  1950. property and include the word \"recursive\" into the value."
  1951. :group 'org-todo
  1952. :type 'boolean)
  1953. (defcustom org-after-todo-state-change-hook nil
  1954. "Hook which is run after the state of a TODO item was changed.
  1955. The new state (a string with a TODO keyword, or nil) is available in the
  1956. Lisp variable `state'."
  1957. :group 'org-todo
  1958. :type 'hook)
  1959. (defvar org-blocker-hook nil
  1960. "Hook for functions that are allowed to block a state change.
  1961. Each function gets as its single argument a property list, see
  1962. `org-trigger-hook' for more information about this list.
  1963. If any of the functions in this hook returns nil, the state change
  1964. is blocked.")
  1965. (defvar org-trigger-hook nil
  1966. "Hook for functions that are triggered by a state change.
  1967. Each function gets as its single argument a property list with at least
  1968. the following elements:
  1969. (:type type-of-change :position pos-at-entry-start
  1970. :from old-state :to new-state)
  1971. Depending on the type, more properties may be present.
  1972. This mechanism is currently implemented for:
  1973. TODO state changes
  1974. ------------------
  1975. :type todo-state-change
  1976. :from previous state (keyword as a string), or nil, or a symbol
  1977. 'todo' or 'done', to indicate the general type of state.
  1978. :to new state, like in :from")
  1979. (defcustom org-enforce-todo-dependencies nil
  1980. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1981. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1982. be blocked if any prior sibling is not yet done.
  1983. Finally, if the parent is blocked because of ordered siblings of its own,
  1984. the child will also be blocked.
  1985. This variable needs to be set before org.el is loaded, and you need to
  1986. restart Emacs after a change to make the change effective. The only way
  1987. to change is while Emacs is running is through the customize interface."
  1988. :set (lambda (var val)
  1989. (set var val)
  1990. (if val
  1991. (add-hook 'org-blocker-hook
  1992. 'org-block-todo-from-children-or-siblings-or-parent)
  1993. (remove-hook 'org-blocker-hook
  1994. 'org-block-todo-from-children-or-siblings-or-parent)))
  1995. :group 'org-todo
  1996. :type 'boolean)
  1997. (defcustom org-enforce-todo-checkbox-dependencies nil
  1998. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1999. When this is nil, checkboxes have no influence on switching TODO states.
  2000. When non-nil, you first need to check off all check boxes before the TODO
  2001. entry can be switched to DONE.
  2002. This variable needs to be set before org.el is loaded, and you need to
  2003. restart Emacs after a change to make the change effective. The only way
  2004. to change is while Emacs is running is through the customize interface."
  2005. :set (lambda (var val)
  2006. (set var val)
  2007. (if val
  2008. (add-hook 'org-blocker-hook
  2009. 'org-block-todo-from-checkboxes)
  2010. (remove-hook 'org-blocker-hook
  2011. 'org-block-todo-from-checkboxes)))
  2012. :group 'org-todo
  2013. :type 'boolean)
  2014. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2015. "Non-nil means inserting a TODO heading is treated as state change.
  2016. So when the command \\[org-insert-todo-heading] is used, state change
  2017. logging will apply if appropriate. When nil, the new TODO item will
  2018. be inserted directly, and no logging will take place."
  2019. :group 'org-todo
  2020. :type 'boolean)
  2021. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2022. "Non-nil means switching TODO states with S-cursor counts as state change.
  2023. This is the default behavior. However, setting this to nil allows a
  2024. convenient way to select a TODO state and bypass any logging associated
  2025. with that."
  2026. :group 'org-todo
  2027. :type 'boolean)
  2028. (defcustom org-todo-state-tags-triggers nil
  2029. "Tag changes that should be triggered by TODO state changes.
  2030. This is a list. Each entry is
  2031. (state-change (tag . flag) .......)
  2032. State-change can be a string with a state, and empty string to indicate the
  2033. state that has no TODO keyword, or it can be one of the symbols `todo'
  2034. or `done', meaning any not-done or done state, respectively."
  2035. :group 'org-todo
  2036. :group 'org-tags
  2037. :type '(repeat
  2038. (cons (choice :tag "When changing to"
  2039. (const :tag "Not-done state" todo)
  2040. (const :tag "Done state" done)
  2041. (string :tag "State"))
  2042. (repeat
  2043. (cons :tag "Tag action"
  2044. (string :tag "Tag")
  2045. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2046. (defcustom org-log-done nil
  2047. "Information to record when a task moves to the DONE state.
  2048. Possible values are:
  2049. nil Don't add anything, just change the keyword
  2050. time Add a time stamp to the task
  2051. note Prompt for a note and add it with template `org-log-note-headings'
  2052. This option can also be set with on a per-file-basis with
  2053. #+STARTUP: nologdone
  2054. #+STARTUP: logdone
  2055. #+STARTUP: lognotedone
  2056. You can have local logging settings for a subtree by setting the LOGGING
  2057. property to one or more of these keywords."
  2058. :group 'org-todo
  2059. :group 'org-progress
  2060. :type '(choice
  2061. (const :tag "No logging" nil)
  2062. (const :tag "Record CLOSED timestamp" time)
  2063. (const :tag "Record CLOSED timestamp with note." note)))
  2064. ;; Normalize old uses of org-log-done.
  2065. (cond
  2066. ((eq org-log-done t) (setq org-log-done 'time))
  2067. ((and (listp org-log-done) (memq 'done org-log-done))
  2068. (setq org-log-done 'note)))
  2069. (defcustom org-log-reschedule nil
  2070. "Information to record when the scheduling date of a tasks is modified.
  2071. Possible values are:
  2072. nil Don't add anything, just change the date
  2073. time Add a time stamp to the task
  2074. note Prompt for a note and add it with template `org-log-note-headings'
  2075. This option can also be set with on a per-file-basis with
  2076. #+STARTUP: nologreschedule
  2077. #+STARTUP: logreschedule
  2078. #+STARTUP: lognotereschedule"
  2079. :group 'org-todo
  2080. :group 'org-progress
  2081. :type '(choice
  2082. (const :tag "No logging" nil)
  2083. (const :tag "Record timestamp" time)
  2084. (const :tag "Record timestamp with note." note)))
  2085. (defcustom org-log-redeadline nil
  2086. "Information to record when the deadline date of a tasks is modified.
  2087. Possible values are:
  2088. nil Don't add anything, just change the date
  2089. time Add a time stamp to the task
  2090. note Prompt for a note and add it with template `org-log-note-headings'
  2091. This option can also be set with on a per-file-basis with
  2092. #+STARTUP: nologredeadline
  2093. #+STARTUP: logredeadline
  2094. #+STARTUP: lognoteredeadline
  2095. You can have local logging settings for a subtree by setting the LOGGING
  2096. property to one or more of these keywords."
  2097. :group 'org-todo
  2098. :group 'org-progress
  2099. :type '(choice
  2100. (const :tag "No logging" nil)
  2101. (const :tag "Record timestamp" time)
  2102. (const :tag "Record timestamp with note." note)))
  2103. (defcustom org-log-note-clock-out nil
  2104. "Non-nil means record a note when clocking out of an item.
  2105. This can also be configured on a per-file basis by adding one of
  2106. the following lines anywhere in the buffer:
  2107. #+STARTUP: lognoteclock-out
  2108. #+STARTUP: nolognoteclock-out"
  2109. :group 'org-todo
  2110. :group 'org-progress
  2111. :type 'boolean)
  2112. (defcustom org-log-done-with-time t
  2113. "Non-nil means the CLOSED time stamp will contain date and time.
  2114. When nil, only the date will be recorded."
  2115. :group 'org-progress
  2116. :type 'boolean)
  2117. (defcustom org-log-note-headings
  2118. '((done . "CLOSING NOTE %t")
  2119. (state . "State %-12s from %-12S %t")
  2120. (note . "Note taken on %t")
  2121. (reschedule . "Rescheduled from %S on %t")
  2122. (delschedule . "Not scheduled, was %S on %t")
  2123. (redeadline . "New deadline from %S on %t")
  2124. (deldeadline . "Removed deadline, was %S on %t")
  2125. (refile . "Refiled on %t")
  2126. (clock-out . ""))
  2127. "Headings for notes added to entries.
  2128. The value is an alist, with the car being a symbol indicating the note
  2129. context, and the cdr is the heading to be used. The heading may also be the
  2130. empty string.
  2131. %t in the heading will be replaced by a time stamp.
  2132. %T will be an active time stamp instead the default inactive one
  2133. %d will be replaced by a short-format time stamp.
  2134. %D will be replaced by an active short-format time stamp.
  2135. %s will be replaced by the new TODO state, in double quotes.
  2136. %S will be replaced by the old TODO state, in double quotes.
  2137. %u will be replaced by the user name.
  2138. %U will be replaced by the full user name.
  2139. In fact, it is not a good idea to change the `state' entry, because
  2140. agenda log mode depends on the format of these entries."
  2141. :group 'org-todo
  2142. :group 'org-progress
  2143. :type '(list :greedy t
  2144. (cons (const :tag "Heading when closing an item" done) string)
  2145. (cons (const :tag
  2146. "Heading when changing todo state (todo sequence only)"
  2147. state) string)
  2148. (cons (const :tag "Heading when just taking a note" note) string)
  2149. (cons (const :tag "Heading when clocking out" clock-out) string)
  2150. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2151. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2152. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2153. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2154. (cons (const :tag "Heading when refiling" refile) string)))
  2155. (unless (assq 'note org-log-note-headings)
  2156. (push '(note . "%t") org-log-note-headings))
  2157. (defcustom org-log-into-drawer nil
  2158. "Non-nil means insert state change notes and time stamps into a drawer.
  2159. When nil, state changes notes will be inserted after the headline and
  2160. any scheduling and clock lines, but not inside a drawer.
  2161. The value of this variable should be the name of the drawer to use.
  2162. LOGBOOK is proposed as the default drawer for this purpose, you can
  2163. also set this to a string to define the drawer of your choice.
  2164. A value of t is also allowed, representing \"LOGBOOK\".
  2165. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2166. will be ignored.
  2167. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2168. a subtree."
  2169. :group 'org-todo
  2170. :group 'org-progress
  2171. :type '(choice
  2172. (const :tag "Not into a drawer" nil)
  2173. (const :tag "LOGBOOK" t)
  2174. (string :tag "Other")))
  2175. (if (fboundp 'defvaralias)
  2176. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2177. (defun org-log-into-drawer ()
  2178. "Return the value of `org-log-into-drawer', but let properties overrule.
  2179. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2180. used instead of the default value."
  2181. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2182. (cond
  2183. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2184. ((equal p "t") "LOGBOOK")
  2185. (t p))))
  2186. (defcustom org-log-state-notes-insert-after-drawers nil
  2187. "Non-nil means insert state change notes after any drawers in entry.
  2188. Only the drawers that *immediately* follow the headline and the
  2189. deadline/scheduled line are skipped.
  2190. When nil, insert notes right after the heading and perhaps the line
  2191. with deadline/scheduling if present.
  2192. This variable will have no effect if `org-log-into-drawer' is
  2193. set."
  2194. :group 'org-todo
  2195. :group 'org-progress
  2196. :type 'boolean)
  2197. (defcustom org-log-states-order-reversed t
  2198. "Non-nil means the latest state note will be directly after heading.
  2199. When nil, the state change notes will be ordered according to time."
  2200. :group 'org-todo
  2201. :group 'org-progress
  2202. :type 'boolean)
  2203. (defcustom org-todo-repeat-to-state nil
  2204. "The TODO state to which a repeater should return the repeating task.
  2205. By default this is the first task in a TODO sequence, or the previous state
  2206. in a TODO_TYP set. But you can specify another task here.
  2207. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2208. :group 'org-todo
  2209. :version "24.1"
  2210. :type '(choice (const :tag "Head of sequence" nil)
  2211. (string :tag "Specific state")))
  2212. (defcustom org-log-repeat 'time
  2213. "Non-nil means record moving through the DONE state when triggering repeat.
  2214. An auto-repeating task is immediately switched back to TODO when
  2215. marked DONE. If you are not logging state changes (by adding \"@\"
  2216. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2217. record a closing note, there will be no record of the task moving
  2218. through DONE. This variable forces taking a note anyway.
  2219. nil Don't force a record
  2220. time Record a time stamp
  2221. note Record a note
  2222. This option can also be set with on a per-file-basis with
  2223. #+STARTUP: logrepeat
  2224. #+STARTUP: lognoterepeat
  2225. #+STARTUP: nologrepeat
  2226. You can have local logging settings for a subtree by setting the LOGGING
  2227. property to one or more of these keywords."
  2228. :group 'org-todo
  2229. :group 'org-progress
  2230. :type '(choice
  2231. (const :tag "Don't force a record" nil)
  2232. (const :tag "Force recording the DONE state" time)
  2233. (const :tag "Force recording a note with the DONE state" note)))
  2234. (defgroup org-priorities nil
  2235. "Priorities in Org-mode."
  2236. :tag "Org Priorities"
  2237. :group 'org-todo)
  2238. (defcustom org-enable-priority-commands t
  2239. "Non-nil means priority commands are active.
  2240. When nil, these commands will be disabled, so that you never accidentally
  2241. set a priority."
  2242. :group 'org-priorities
  2243. :type 'boolean)
  2244. (defcustom org-highest-priority ?A
  2245. "The highest priority of TODO items. A character like ?A, ?B etc.
  2246. Must have a smaller ASCII number than `org-lowest-priority'."
  2247. :group 'org-priorities
  2248. :type 'character)
  2249. (defcustom org-lowest-priority ?C
  2250. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2251. Must have a larger ASCII number than `org-highest-priority'."
  2252. :group 'org-priorities
  2253. :type 'character)
  2254. (defcustom org-default-priority ?B
  2255. "The default priority of TODO items.
  2256. This is the priority an item gets if no explicit priority is given.
  2257. When starting to cycle on an empty priority the first step in the cycle
  2258. depends on `org-priority-start-cycle-with-default'. The resulting first
  2259. step priority must not exceed the range from `org-highest-priority' to
  2260. `org-lowest-priority' which means that `org-default-priority' has to be
  2261. in this range exclusive or inclusive the range boundaries. Else the
  2262. first step refuses to set the default and the second will fall back
  2263. to (depending on the command used) the highest or lowest priority."
  2264. :group 'org-priorities
  2265. :type 'character)
  2266. (defcustom org-priority-start-cycle-with-default t
  2267. "Non-nil means start with default priority when starting to cycle.
  2268. When this is nil, the first step in the cycle will be (depending on the
  2269. command used) one higher or lower than the default priority.
  2270. See also `org-default-priority'."
  2271. :group 'org-priorities
  2272. :type 'boolean)
  2273. (defcustom org-get-priority-function nil
  2274. "Function to extract the priority from a string.
  2275. The string is normally the headline. If this is nil Org computes the
  2276. priority from the priority cookie like [#A] in the headline. It returns
  2277. an integer, increasing by 1000 for each priority level.
  2278. The user can set a different function here, which should take a string
  2279. as an argument and return the numeric priority."
  2280. :group 'org-priorities
  2281. :version "24.1"
  2282. :type 'function)
  2283. (defgroup org-time nil
  2284. "Options concerning time stamps and deadlines in Org-mode."
  2285. :tag "Org Time"
  2286. :group 'org)
  2287. (defcustom org-insert-labeled-timestamps-at-point nil
  2288. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2289. When nil, these labeled time stamps are forces into the second line of an
  2290. entry, just after the headline. When scheduling from the global TODO list,
  2291. the time stamp will always be forced into the second line."
  2292. :group 'org-time
  2293. :type 'boolean)
  2294. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2295. "Formats for `format-time-string' which are used for time stamps.
  2296. It is not recommended to change this constant.")
  2297. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2298. "Number of minutes to round time stamps to.
  2299. These are two values, the first applies when first creating a time stamp.
  2300. The second applies when changing it with the commands `S-up' and `S-down'.
  2301. When changing the time stamp, this means that it will change in steps
  2302. of N minutes, as given by the second value.
  2303. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2304. numbers should be factors of 60, so for example 5, 10, 15.
  2305. When this is larger than 1, you can still force an exact time stamp by using
  2306. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2307. and by using a prefix arg to `S-up/down' to specify the exact number
  2308. of minutes to shift."
  2309. :group 'org-time
  2310. :get #'(lambda (var) ; Make sure both elements are there
  2311. (if (integerp (default-value var))
  2312. (list (default-value var) 5)
  2313. (default-value var)))
  2314. :type '(list
  2315. (integer :tag "when inserting times")
  2316. (integer :tag "when modifying times")))
  2317. ;; Normalize old customizations of this variable.
  2318. (when (integerp org-time-stamp-rounding-minutes)
  2319. (setq org-time-stamp-rounding-minutes
  2320. (list org-time-stamp-rounding-minutes
  2321. org-time-stamp-rounding-minutes)))
  2322. (defcustom org-display-custom-times nil
  2323. "Non-nil means overlay custom formats over all time stamps.
  2324. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2325. To turn this on on a per-file basis, insert anywhere in the file:
  2326. #+STARTUP: customtime"
  2327. :group 'org-time
  2328. :set 'set-default
  2329. :type 'sexp)
  2330. (make-variable-buffer-local 'org-display-custom-times)
  2331. (defcustom org-time-stamp-custom-formats
  2332. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2333. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2334. These are overlaid over the default ISO format if the variable
  2335. `org-display-custom-times' is set. Time like %H:%M should be at the
  2336. end of the second format. The custom formats are also honored by export
  2337. commands, if custom time display is turned on at the time of export."
  2338. :group 'org-time
  2339. :type 'sexp)
  2340. (defun org-time-stamp-format (&optional long inactive)
  2341. "Get the right format for a time string."
  2342. (let ((f (if long (cdr org-time-stamp-formats)
  2343. (car org-time-stamp-formats))))
  2344. (if inactive
  2345. (concat "[" (substring f 1 -1) "]")
  2346. f)))
  2347. (defcustom org-time-clocksum-format "%d:%02d"
  2348. "The format string used when creating CLOCKSUM lines.
  2349. This is also used when org-mode generates a time duration."
  2350. :group 'org-time
  2351. :type 'string)
  2352. (defcustom org-time-clocksum-use-fractional nil
  2353. "If non-nil, \\[org-clock-display] uses fractional times.
  2354. org-mode generates a time duration."
  2355. :group 'org-time
  2356. :type 'boolean)
  2357. (defcustom org-time-clocksum-fractional-format "%.2f"
  2358. "The format string used when creating CLOCKSUM lines, or when
  2359. org-mode generates a time duration."
  2360. :group 'org-time
  2361. :type 'string)
  2362. (defcustom org-deadline-warning-days 14
  2363. "No. of days before expiration during which a deadline becomes active.
  2364. This variable governs the display in sparse trees and in the agenda.
  2365. When 0 or negative, it means use this number (the absolute value of it)
  2366. even if a deadline has a different individual lead time specified.
  2367. Custom commands can set this variable in the options section."
  2368. :group 'org-time
  2369. :group 'org-agenda-daily/weekly
  2370. :type 'integer)
  2371. (defcustom org-read-date-prefer-future t
  2372. "Non-nil means assume future for incomplete date input from user.
  2373. This affects the following situations:
  2374. 1. The user gives a month but not a year.
  2375. For example, if it is April and you enter \"feb 2\", this will be read
  2376. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2377. 2. The user gives a day, but no month.
  2378. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2379. read this as the third of *next* month. However, if you enter \"17\",
  2380. it will be considered as *this* month.
  2381. If you set this variable to the symbol `time', then also the following
  2382. will work:
  2383. 3. If the user gives a time, but no day. If the time is before now,
  2384. to will be interpreted as tomorrow.
  2385. Currently none of this works for ISO week specifications.
  2386. When this option is nil, the current day, month and year will always be
  2387. used as defaults.
  2388. See also `org-agenda-jump-prefer-future'."
  2389. :group 'org-time
  2390. :type '(choice
  2391. (const :tag "Never" nil)
  2392. (const :tag "Check month and day" t)
  2393. (const :tag "Check month, day, and time" time)))
  2394. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2395. "Should the agenda jump command prefer the future for incomplete dates?
  2396. The default is to do the same as configured in `org-read-date-prefer-future'.
  2397. But you can also set a deviating value here.
  2398. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2399. :group 'org-agenda
  2400. :group 'org-time
  2401. :version "24.1"
  2402. :type '(choice
  2403. (const :tag "Use org-read-date-prefer-future"
  2404. org-read-date-prefer-future)
  2405. (const :tag "Never" nil)
  2406. (const :tag "Always" t)))
  2407. (defcustom org-read-date-force-compatible-dates t
  2408. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2409. Depending on the system Emacs is running on, certain dates cannot
  2410. be represented with the type used internally to represent time.
  2411. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2412. correctly. Some systems allow for earlier dates, some for later,
  2413. some for both. One way to find out it to insert any date into an
  2414. Org buffer, putting the cursor on the year and hitting S-up and
  2415. S-down to test the range.
  2416. When this variable is set to t, the date/time prompt will not let
  2417. you specify dates outside the 1970-2037 range, so it is certain that
  2418. these dates will work in whatever version of Emacs you are
  2419. running, and also that you can move a file from one Emacs implementation
  2420. to another. WHenever Org is forcing the year for you, it will display
  2421. a message and beep.
  2422. When this variable is nil, Org will check if the date is
  2423. representable in the specific Emacs implementation you are using.
  2424. If not, it will force a year, usually the current year, and beep
  2425. to remind you. Currently this setting is not recommended because
  2426. the likelihood that you will open your Org files in an Emacs that
  2427. has limited date range is not negligible.
  2428. A workaround for this problem is to use diary sexp dates for time
  2429. stamps outside of this range."
  2430. :group 'org-time
  2431. :version "24.1"
  2432. :type 'boolean)
  2433. (defcustom org-read-date-display-live t
  2434. "Non-nil means display current interpretation of date prompt live.
  2435. This display will be in an overlay, in the minibuffer."
  2436. :group 'org-time
  2437. :type 'boolean)
  2438. (defcustom org-read-date-popup-calendar t
  2439. "Non-nil means pop up a calendar when prompting for a date.
  2440. In the calendar, the date can be selected with mouse-1. However, the
  2441. minibuffer will also be active, and you can simply enter the date as well.
  2442. When nil, only the minibuffer will be available."
  2443. :group 'org-time
  2444. :type 'boolean)
  2445. (if (fboundp 'defvaralias)
  2446. (defvaralias 'org-popup-calendar-for-date-prompt
  2447. 'org-read-date-popup-calendar))
  2448. (defcustom org-read-date-minibuffer-setup-hook nil
  2449. "Hook to be used to set up keys for the date/time interface.
  2450. Add key definitions to `minibuffer-local-map', which will be a temporary
  2451. copy."
  2452. :group 'org-time
  2453. :type 'hook)
  2454. (defcustom org-extend-today-until 0
  2455. "The hour when your day really ends. Must be an integer.
  2456. This has influence for the following applications:
  2457. - When switching the agenda to \"today\". It it is still earlier than
  2458. the time given here, the day recognized as TODAY is actually yesterday.
  2459. - When a date is read from the user and it is still before the time given
  2460. here, the current date and time will be assumed to be yesterday, 23:59.
  2461. Also, timestamps inserted in remember templates follow this rule.
  2462. IMPORTANT: This is a feature whose implementation is and likely will
  2463. remain incomplete. Really, it is only here because past midnight seems to
  2464. be the favorite working time of John Wiegley :-)"
  2465. :group 'org-time
  2466. :type 'integer)
  2467. (defcustom org-use-effective-time nil
  2468. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2469. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2470. \"effective time\" of any timestamps between midnight and 8am will be
  2471. 23:59 of the previous day."
  2472. :group 'boolean
  2473. :version "24.1"
  2474. :type 'integer)
  2475. (defcustom org-edit-timestamp-down-means-later nil
  2476. "Non-nil means S-down will increase the time in a time stamp.
  2477. When nil, S-up will increase."
  2478. :group 'org-time
  2479. :type 'boolean)
  2480. (defcustom org-calendar-follow-timestamp-change t
  2481. "Non-nil means make the calendar window follow timestamp changes.
  2482. When a timestamp is modified and the calendar window is visible, it will be
  2483. moved to the new date."
  2484. :group 'org-time
  2485. :type 'boolean)
  2486. (defgroup org-tags nil
  2487. "Options concerning tags in Org-mode."
  2488. :tag "Org Tags"
  2489. :group 'org)
  2490. (defcustom org-tag-alist nil
  2491. "List of tags allowed in Org-mode files.
  2492. When this list is nil, Org-mode will base TAG input on what is already in the
  2493. buffer.
  2494. The value of this variable is an alist, the car of each entry must be a
  2495. keyword as a string, the cdr may be a character that is used to select
  2496. that tag through the fast-tag-selection interface.
  2497. See the manual for details."
  2498. :group 'org-tags
  2499. :type '(repeat
  2500. (choice
  2501. (cons (string :tag "Tag name")
  2502. (character :tag "Access char"))
  2503. (list :tag "Start radio group"
  2504. (const :startgroup)
  2505. (option (string :tag "Group description")))
  2506. (list :tag "End radio group"
  2507. (const :endgroup)
  2508. (option (string :tag "Group description")))
  2509. (const :tag "New line" (:newline)))))
  2510. (defcustom org-tag-persistent-alist nil
  2511. "List of tags that will always appear in all Org-mode files.
  2512. This is in addition to any in buffer settings or customizations
  2513. of `org-tag-alist'.
  2514. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2515. The value of this variable is an alist, the car of each entry must be a
  2516. keyword as a string, the cdr may be a character that is used to select
  2517. that tag through the fast-tag-selection interface.
  2518. See the manual for details.
  2519. To disable these tags on a per-file basis, insert anywhere in the file:
  2520. #+STARTUP: noptag"
  2521. :group 'org-tags
  2522. :type '(repeat
  2523. (choice
  2524. (cons (string :tag "Tag name")
  2525. (character :tag "Access char"))
  2526. (const :tag "Start radio group" (:startgroup))
  2527. (const :tag "End radio group" (:endgroup))
  2528. (const :tag "New line" (:newline)))))
  2529. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2530. "If non-nil, always offer completion for all tags of all agenda files.
  2531. Instead of customizing this variable directly, you might want to
  2532. set it locally for capture buffers, because there no list of
  2533. tags in that file can be created dynamically (there are none).
  2534. (add-hook 'org-capture-mode-hook
  2535. (lambda ()
  2536. (set (make-local-variable
  2537. 'org-complete-tags-always-offer-all-agenda-tags)
  2538. t)))"
  2539. :group 'org-tags
  2540. :version "24.1"
  2541. :type 'boolean)
  2542. (defvar org-file-tags nil
  2543. "List of tags that can be inherited by all entries in the file.
  2544. The tags will be inherited if the variable `org-use-tag-inheritance'
  2545. says they should be.
  2546. This variable is populated from #+FILETAGS lines.")
  2547. (defcustom org-use-fast-tag-selection 'auto
  2548. "Non-nil means use fast tag selection scheme.
  2549. This is a special interface to select and deselect tags with single keys.
  2550. When nil, fast selection is never used.
  2551. When the symbol `auto', fast selection is used if and only if selection
  2552. characters for tags have been configured, either through the variable
  2553. `org-tag-alist' or through a #+TAGS line in the buffer.
  2554. When t, fast selection is always used and selection keys are assigned
  2555. automatically if necessary."
  2556. :group 'org-tags
  2557. :type '(choice
  2558. (const :tag "Always" t)
  2559. (const :tag "Never" nil)
  2560. (const :tag "When selection characters are configured" 'auto)))
  2561. (defcustom org-fast-tag-selection-single-key nil
  2562. "Non-nil means fast tag selection exits after first change.
  2563. When nil, you have to press RET to exit it.
  2564. During fast tag selection, you can toggle this flag with `C-c'.
  2565. This variable can also have the value `expert'. In this case, the window
  2566. displaying the tags menu is not even shown, until you press C-c again."
  2567. :group 'org-tags
  2568. :type '(choice
  2569. (const :tag "No" nil)
  2570. (const :tag "Yes" t)
  2571. (const :tag "Expert" expert)))
  2572. (defvar org-fast-tag-selection-include-todo nil
  2573. "Non-nil means fast tags selection interface will also offer TODO states.
  2574. This is an undocumented feature, you should not rely on it.")
  2575. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2576. "The column to which tags should be indented in a headline.
  2577. If this number is positive, it specifies the column. If it is negative,
  2578. it means that the tags should be flushright to that column. For example,
  2579. -80 works well for a normal 80 character screen.
  2580. When 0, place tags directly after headline text, with only one space in
  2581. between."
  2582. :group 'org-tags
  2583. :type 'integer)
  2584. (defcustom org-auto-align-tags t
  2585. "Non-nil keeps tags aligned when modifying headlines.
  2586. Some operations (i.e. demoting) change the length of a headline and
  2587. therefore shift the tags around. With this option turned on, after
  2588. each such operation the tags are again aligned to `org-tags-column'."
  2589. :group 'org-tags
  2590. :type 'boolean)
  2591. (defcustom org-use-tag-inheritance t
  2592. "Non-nil means tags in levels apply also for sublevels.
  2593. When nil, only the tags directly given in a specific line apply there.
  2594. This may also be a list of tags that should be inherited, or a regexp that
  2595. matches tags that should be inherited. Additional control is possible
  2596. with the variable `org-tags-exclude-from-inheritance' which gives an
  2597. explicit list of tags to be excluded from inheritance., even if the value of
  2598. `org-use-tag-inheritance' would select it for inheritance.
  2599. If this option is t, a match early-on in a tree can lead to a large
  2600. number of matches in the subtree when constructing the agenda or creating
  2601. a sparse tree. If you only want to see the first match in a tree during
  2602. a search, check out the variable `org-tags-match-list-sublevels'."
  2603. :group 'org-tags
  2604. :type '(choice
  2605. (const :tag "Not" nil)
  2606. (const :tag "Always" t)
  2607. (repeat :tag "Specific tags" (string :tag "Tag"))
  2608. (regexp :tag "Tags matched by regexp")))
  2609. (defcustom org-tags-exclude-from-inheritance nil
  2610. "List of tags that should never be inherited.
  2611. This is a way to exclude a few tags from inheritance. For way to do
  2612. the opposite, to actively allow inheritance for selected tags,
  2613. see the variable `org-use-tag-inheritance'."
  2614. :group 'org-tags
  2615. :type '(repeat (string :tag "Tag")))
  2616. (defun org-tag-inherit-p (tag)
  2617. "Check if TAG is one that should be inherited."
  2618. (cond
  2619. ((member tag org-tags-exclude-from-inheritance) nil)
  2620. ((eq org-use-tag-inheritance t) t)
  2621. ((not org-use-tag-inheritance) nil)
  2622. ((stringp org-use-tag-inheritance)
  2623. (string-match org-use-tag-inheritance tag))
  2624. ((listp org-use-tag-inheritance)
  2625. (member tag org-use-tag-inheritance))
  2626. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2627. (defcustom org-tags-match-list-sublevels t
  2628. "Non-nil means list also sublevels of headlines matching a search.
  2629. This variable applies to tags/property searches, and also to stuck
  2630. projects because this search is based on a tags match as well.
  2631. When set to the symbol `indented', sublevels are indented with
  2632. leading dots.
  2633. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2634. the sublevels of a headline matching a tag search often also match
  2635. the same search. Listing all of them can create very long lists.
  2636. Setting this variable to nil causes subtrees of a match to be skipped.
  2637. This variable is semi-obsolete and probably should always be true. It
  2638. is better to limit inheritance to certain tags using the variables
  2639. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2640. :group 'org-tags
  2641. :type '(choice
  2642. (const :tag "No, don't list them" nil)
  2643. (const :tag "Yes, do list them" t)
  2644. (const :tag "List them, indented with leading dots" indented)))
  2645. (defcustom org-tags-sort-function nil
  2646. "When set, tags are sorted using this function as a comparator."
  2647. :group 'org-tags
  2648. :type '(choice
  2649. (const :tag "No sorting" nil)
  2650. (const :tag "Alphabetical" string<)
  2651. (const :tag "Reverse alphabetical" string>)
  2652. (function :tag "Custom function" nil)))
  2653. (defvar org-tags-history nil
  2654. "History of minibuffer reads for tags.")
  2655. (defvar org-last-tags-completion-table nil
  2656. "The last used completion table for tags.")
  2657. (defvar org-after-tags-change-hook nil
  2658. "Hook that is run after the tags in a line have changed.")
  2659. (defgroup org-properties nil
  2660. "Options concerning properties in Org-mode."
  2661. :tag "Org Properties"
  2662. :group 'org)
  2663. (defcustom org-property-format "%-10s %s"
  2664. "How property key/value pairs should be formatted by `indent-line'.
  2665. When `indent-line' hits a property definition, it will format the line
  2666. according to this format, mainly to make sure that the values are
  2667. lined-up with respect to each other."
  2668. :group 'org-properties
  2669. :type 'string)
  2670. (defcustom org-properties-postprocess-alist nil
  2671. "Alist of properties and functions to adjust inserted values.
  2672. Elements of this alist must be of the form
  2673. ([string] [function])
  2674. where [string] must be a property name and [function] must be a
  2675. lambda expression: this lambda expression must take one argument,
  2676. the value to adjust, and return the new value as a string.
  2677. For example, this element will allow the property \"Remaining\"
  2678. to be updated wrt the relation between the \"Effort\" property
  2679. and the clock summary:
  2680. ((\"Remaining\" (lambda(value)
  2681. (let ((clocksum (org-clock-sum-current-item))
  2682. (effort (org-duration-string-to-minutes
  2683. (org-entry-get (point) \"Effort\"))))
  2684. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2685. :group 'org-properties
  2686. :version "24.1"
  2687. :type 'alist)
  2688. (defcustom org-use-property-inheritance nil
  2689. "Non-nil means properties apply also for sublevels.
  2690. This setting is chiefly used during property searches. Turning it on can
  2691. cause significant overhead when doing a search, which is why it is not
  2692. on by default.
  2693. When nil, only the properties directly given in the current entry count.
  2694. When t, every property is inherited. The value may also be a list of
  2695. properties that should have inheritance, or a regular expression matching
  2696. properties that should be inherited.
  2697. However, note that some special properties use inheritance under special
  2698. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2699. and the properties ending in \"_ALL\" when they are used as descriptor
  2700. for valid values of a property.
  2701. Note for programmers:
  2702. When querying an entry with `org-entry-get', you can control if inheritance
  2703. should be used. By default, `org-entry-get' looks only at the local
  2704. properties. You can request inheritance by setting the inherit argument
  2705. to t (to force inheritance) or to `selective' (to respect the setting
  2706. in this variable)."
  2707. :group 'org-properties
  2708. :type '(choice
  2709. (const :tag "Not" nil)
  2710. (const :tag "Always" t)
  2711. (repeat :tag "Specific properties" (string :tag "Property"))
  2712. (regexp :tag "Properties matched by regexp")))
  2713. (defun org-property-inherit-p (property)
  2714. "Check if PROPERTY is one that should be inherited."
  2715. (cond
  2716. ((eq org-use-property-inheritance t) t)
  2717. ((not org-use-property-inheritance) nil)
  2718. ((stringp org-use-property-inheritance)
  2719. (string-match org-use-property-inheritance property))
  2720. ((listp org-use-property-inheritance)
  2721. (member property org-use-property-inheritance))
  2722. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2723. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2724. "The default column format, if no other format has been defined.
  2725. This variable can be set on the per-file basis by inserting a line
  2726. #+COLUMNS: %25ITEM ....."
  2727. :group 'org-properties
  2728. :type 'string)
  2729. (defcustom org-columns-ellipses ".."
  2730. "The ellipses to be used when a field in column view is truncated.
  2731. When this is the empty string, as many characters as possible are shown,
  2732. but then there will be no visual indication that the field has been truncated.
  2733. When this is a string of length N, the last N characters of a truncated
  2734. field are replaced by this string. If the column is narrower than the
  2735. ellipses string, only part of the ellipses string will be shown."
  2736. :group 'org-properties
  2737. :type 'string)
  2738. (defcustom org-columns-modify-value-for-display-function nil
  2739. "Function that modifies values for display in column view.
  2740. For example, it can be used to cut out a certain part from a time stamp.
  2741. The function must take 2 arguments:
  2742. column-title The title of the column (*not* the property name)
  2743. value The value that should be modified.
  2744. The function should return the value that should be displayed,
  2745. or nil if the normal value should be used."
  2746. :group 'org-properties
  2747. :type 'function)
  2748. (defcustom org-effort-property "Effort"
  2749. "The property that is being used to keep track of effort estimates.
  2750. Effort estimates given in this property need to have the format H:MM."
  2751. :group 'org-properties
  2752. :group 'org-progress
  2753. :type '(string :tag "Property"))
  2754. (defconst org-global-properties-fixed
  2755. '(("VISIBILITY_ALL" . "folded children content all")
  2756. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2757. "List of property/value pairs that can be inherited by any entry.
  2758. These are fixed values, for the preset properties. The user variable
  2759. that can be used to add to this list is `org-global-properties'.
  2760. The entries in this list are cons cells where the car is a property
  2761. name and cdr is a string with the value. If the value represents
  2762. multiple items like an \"_ALL\" property, separate the items by
  2763. spaces.")
  2764. (defcustom org-global-properties nil
  2765. "List of property/value pairs that can be inherited by any entry.
  2766. This list will be combined with the constant `org-global-properties-fixed'.
  2767. The entries in this list are cons cells where the car is a property
  2768. name and cdr is a string with the value.
  2769. You can set buffer-local values for the same purpose in the variable
  2770. `org-file-properties' this by adding lines like
  2771. #+PROPERTY: NAME VALUE"
  2772. :group 'org-properties
  2773. :type '(repeat
  2774. (cons (string :tag "Property")
  2775. (string :tag "Value"))))
  2776. (defvar org-file-properties nil
  2777. "List of property/value pairs that can be inherited by any entry.
  2778. Valid for the current buffer.
  2779. This variable is populated from #+PROPERTY lines.")
  2780. (make-variable-buffer-local 'org-file-properties)
  2781. (defgroup org-agenda nil
  2782. "Options concerning agenda views in Org-mode."
  2783. :tag "Org Agenda"
  2784. :group 'org)
  2785. (defvar org-category nil
  2786. "Variable used by org files to set a category for agenda display.
  2787. Such files should use a file variable to set it, for example
  2788. # -*- mode: org; org-category: \"ELisp\"
  2789. or contain a special line
  2790. #+CATEGORY: ELisp
  2791. If the file does not specify a category, then file's base name
  2792. is used instead.")
  2793. (make-variable-buffer-local 'org-category)
  2794. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2795. (defcustom org-agenda-files nil
  2796. "The files to be used for agenda display.
  2797. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2798. \\[org-remove-file]. You can also use customize to edit the list.
  2799. If an entry is a directory, all files in that directory that are matched by
  2800. `org-agenda-file-regexp' will be part of the file list.
  2801. If the value of the variable is not a list but a single file name, then
  2802. the list of agenda files is actually stored and maintained in that file, one
  2803. agenda file per line. In this file paths can be given relative to
  2804. `org-directory'. Tilde expansion and environment variable substitution
  2805. are also made."
  2806. :group 'org-agenda
  2807. :type '(choice
  2808. (repeat :tag "List of files and directories" file)
  2809. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2810. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2811. "Regular expression to match files for `org-agenda-files'.
  2812. If any element in the list in that variable contains a directory instead
  2813. of a normal file, all files in that directory that are matched by this
  2814. regular expression will be included."
  2815. :group 'org-agenda
  2816. :type 'regexp)
  2817. (defcustom org-agenda-text-search-extra-files nil
  2818. "List of extra files to be searched by text search commands.
  2819. These files will be search in addition to the agenda files by the
  2820. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2821. Note that these files will only be searched for text search commands,
  2822. not for the other agenda views like todo lists, tag searches or the weekly
  2823. agenda. This variable is intended to list notes and possibly archive files
  2824. that should also be searched by these two commands.
  2825. In fact, if the first element in the list is the symbol `agenda-archives',
  2826. than all archive files of all agenda files will be added to the search
  2827. scope."
  2828. :group 'org-agenda
  2829. :type '(set :greedy t
  2830. (const :tag "Agenda Archives" agenda-archives)
  2831. (repeat :inline t (file))))
  2832. (if (fboundp 'defvaralias)
  2833. (defvaralias 'org-agenda-multi-occur-extra-files
  2834. 'org-agenda-text-search-extra-files))
  2835. (defcustom org-agenda-skip-unavailable-files nil
  2836. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2837. A nil value means to remove them, after a query, from the list."
  2838. :group 'org-agenda
  2839. :type 'boolean)
  2840. (defcustom org-calendar-to-agenda-key [?c]
  2841. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2842. The command `org-calendar-goto-agenda' will be bound to this key. The
  2843. default is the character `c' because then `c' can be used to switch back and
  2844. forth between agenda and calendar."
  2845. :group 'org-agenda
  2846. :type 'sexp)
  2847. (defcustom org-calendar-agenda-action-key [?k]
  2848. "The key to be installed in `calendar-mode-map' for agenda-action.
  2849. The command `org-agenda-action' will be bound to this key. The
  2850. default is the character `k' because we use the same key in the agenda."
  2851. :group 'org-agenda
  2852. :type 'sexp)
  2853. (defcustom org-calendar-insert-diary-entry-key [?i]
  2854. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2855. This option is irrelevant until `org-agenda-diary-file' has been configured
  2856. to point to an Org-mode file. When that is the case, the command
  2857. `org-agenda-diary-entry' will be bound to the key given here, by default
  2858. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2859. if you want to continue doing this, you need to change this to a different
  2860. key."
  2861. :group 'org-agenda
  2862. :type 'sexp)
  2863. (defcustom org-agenda-diary-file 'diary-file
  2864. "File to which to add new entries with the `i' key in agenda and calendar.
  2865. When this is the symbol `diary-file', the functionality in the Emacs
  2866. calendar will be used to add entries to the `diary-file'. But when this
  2867. points to a file, `org-agenda-diary-entry' will be used instead."
  2868. :group 'org-agenda
  2869. :type '(choice
  2870. (const :tag "The standard Emacs diary file" diary-file)
  2871. (file :tag "Special Org file diary entries")))
  2872. (eval-after-load "calendar"
  2873. '(progn
  2874. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2875. 'org-calendar-goto-agenda)
  2876. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2877. 'org-agenda-action)
  2878. (add-hook 'calendar-mode-hook
  2879. (lambda ()
  2880. (unless (eq org-agenda-diary-file 'diary-file)
  2881. (define-key calendar-mode-map
  2882. org-calendar-insert-diary-entry-key
  2883. 'org-agenda-diary-entry))))))
  2884. (defgroup org-latex nil
  2885. "Options for embedding LaTeX code into Org-mode."
  2886. :tag "Org LaTeX"
  2887. :group 'org)
  2888. (defcustom org-format-latex-options
  2889. '(:foreground default :background default :scale 1.0
  2890. :html-foreground "Black" :html-background "Transparent"
  2891. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2892. "Options for creating images from LaTeX fragments.
  2893. This is a property list with the following properties:
  2894. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2895. `default' means use the foreground of the default face.
  2896. :background the background color, or \"Transparent\".
  2897. `default' means use the background of the default face.
  2898. :scale a scaling factor for the size of the images, to get more pixels
  2899. :html-foreground, :html-background, :html-scale
  2900. the same numbers for HTML export.
  2901. :matchers a list indicating which matchers should be used to
  2902. find LaTeX fragments. Valid members of this list are:
  2903. \"begin\" find environments
  2904. \"$1\" find single characters surrounded by $.$
  2905. \"$\" find math expressions surrounded by $...$
  2906. \"$$\" find math expressions surrounded by $$....$$
  2907. \"\\(\" find math expressions surrounded by \\(...\\)
  2908. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2909. :group 'org-latex
  2910. :type 'plist)
  2911. (defcustom org-format-latex-signal-error t
  2912. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2913. When nil, just push out a message."
  2914. :group 'org-latex
  2915. :version "24.1"
  2916. :type 'boolean)
  2917. (defcustom org-latex-to-mathml-jar-file nil
  2918. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2919. Use this to specify additional executable file say a jar file.
  2920. When using MathToWeb as the converter, specify the full-path to
  2921. your mathtoweb.jar file."
  2922. :group 'org-latex
  2923. :version "24.1"
  2924. :type '(choice
  2925. (const :tag "None" nil)
  2926. (file :tag "JAR file" :must-match t)))
  2927. (defcustom org-latex-to-mathml-convert-command nil
  2928. "Command to convert LaTeX fragments to MathML.
  2929. Replace format-specifiers in the command as noted below and use
  2930. `shell-command' to convert LaTeX to MathML.
  2931. %j: Executable file in fully expanded form as specified by
  2932. `org-latex-to-mathml-jar-file'.
  2933. %I: Input LaTeX file in fully expanded form
  2934. %o: Output MathML file
  2935. This command is used by `org-create-math-formula'.
  2936. When using MathToWeb as the converter, set this to
  2937. \"java -jar %j -unicode -force -df %o %I\"."
  2938. :group 'org-latex
  2939. :version "24.1"
  2940. :type '(choice
  2941. (const :tag "None" nil)
  2942. (string :tag "\nShell command")))
  2943. (defun org-format-latex-mathml-available-p ()
  2944. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2945. (save-match-data
  2946. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2947. org-latex-to-mathml-convert-command)
  2948. (let ((executable (car (split-string
  2949. org-latex-to-mathml-convert-command))))
  2950. (when (executable-find executable)
  2951. (if (string-match
  2952. "%j" org-latex-to-mathml-convert-command)
  2953. (file-readable-p org-latex-to-mathml-jar-file)
  2954. t))))))
  2955. (defcustom org-format-latex-header "\\documentclass{article}
  2956. \\usepackage[usenames]{color}
  2957. \\usepackage{amsmath}
  2958. \\usepackage[mathscr]{eucal}
  2959. \\pagestyle{empty} % do not remove
  2960. \[PACKAGES]
  2961. \[DEFAULT-PACKAGES]
  2962. % The settings below are copied from fullpage.sty
  2963. \\setlength{\\textwidth}{\\paperwidth}
  2964. \\addtolength{\\textwidth}{-3cm}
  2965. \\setlength{\\oddsidemargin}{1.5cm}
  2966. \\addtolength{\\oddsidemargin}{-2.54cm}
  2967. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2968. \\setlength{\\textheight}{\\paperheight}
  2969. \\addtolength{\\textheight}{-\\headheight}
  2970. \\addtolength{\\textheight}{-\\headsep}
  2971. \\addtolength{\\textheight}{-\\footskip}
  2972. \\addtolength{\\textheight}{-3cm}
  2973. \\setlength{\\topmargin}{1.5cm}
  2974. \\addtolength{\\topmargin}{-2.54cm}"
  2975. "The document header used for processing LaTeX fragments.
  2976. It is imperative that this header make sure that no page number
  2977. appears on the page. The package defined in the variables
  2978. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2979. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2980. will be appended."
  2981. :group 'org-latex
  2982. :type 'string)
  2983. (defvar org-format-latex-header-extra nil)
  2984. (defun org-set-packages-alist (var val)
  2985. "Set the packages alist and make sure it has 3 elements per entry."
  2986. (set var (mapcar (lambda (x)
  2987. (if (and (consp x) (= (length x) 2))
  2988. (list (car x) (nth 1 x) t)
  2989. x))
  2990. val)))
  2991. (defun org-get-packages-alist (var)
  2992. "Get the packages alist and make sure it has 3 elements per entry."
  2993. (mapcar (lambda (x)
  2994. (if (and (consp x) (= (length x) 2))
  2995. (list (car x) (nth 1 x) t)
  2996. x))
  2997. (default-value var)))
  2998. ;; The following variables are defined here because is it also used
  2999. ;; when formatting latex fragments. Originally it was part of the
  3000. ;; LaTeX exporter, which is why the name includes "export".
  3001. (defcustom org-export-latex-default-packages-alist
  3002. '(("AUTO" "inputenc" t)
  3003. ("T1" "fontenc" t)
  3004. ("" "fixltx2e" nil)
  3005. ("" "graphicx" t)
  3006. ("" "longtable" nil)
  3007. ("" "float" nil)
  3008. ("" "wrapfig" nil)
  3009. ("" "soul" t)
  3010. ("" "textcomp" t)
  3011. ("" "marvosym" t)
  3012. ("" "wasysym" t)
  3013. ("" "latexsym" t)
  3014. ("" "amssymb" t)
  3015. ("" "hyperref" nil)
  3016. "\\tolerance=1000"
  3017. )
  3018. "Alist of default packages to be inserted in the header.
  3019. Change this only if one of the packages here causes an incompatibility
  3020. with another package you are using.
  3021. The packages in this list are needed by one part or another of Org-mode
  3022. to function properly.
  3023. - inputenc, fontenc: for basic font and character selection
  3024. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3025. for interpreting the entities in `org-entities'. You can skip some of these
  3026. packages if you don't use any of the symbols in it.
  3027. - graphicx: for including images
  3028. - float, wrapfig: for figure placement
  3029. - longtable: for long tables
  3030. - hyperref: for cross references
  3031. Therefore you should not modify this variable unless you know what you
  3032. are doing. The one reason to change it anyway is that you might be loading
  3033. some other package that conflicts with one of the default packages.
  3034. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3035. If SNIPPET-FLAG is t, the package also needs to be included when
  3036. compiling LaTeX snippets into images for inclusion into HTML."
  3037. :group 'org-export-latex
  3038. :set 'org-set-packages-alist
  3039. :get 'org-get-packages-alist
  3040. :version "24.1"
  3041. :type '(repeat
  3042. (choice
  3043. (list :tag "options/package pair"
  3044. (string :tag "options")
  3045. (string :tag "package")
  3046. (boolean :tag "Snippet"))
  3047. (string :tag "A line of LaTeX"))))
  3048. (defcustom org-export-latex-packages-alist nil
  3049. "Alist of packages to be inserted in every LaTeX header.
  3050. These will be inserted after `org-export-latex-default-packages-alist'.
  3051. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3052. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3053. turning LaTeX snippets into images for inclusion into HTML.
  3054. Make sure that you only list packages here which:
  3055. - you want in every file
  3056. - do not conflict with the default packages in
  3057. `org-export-latex-default-packages-alist'
  3058. - do not conflict with the setup in `org-format-latex-header'."
  3059. :group 'org-export-latex
  3060. :set 'org-set-packages-alist
  3061. :get 'org-get-packages-alist
  3062. :type '(repeat
  3063. (choice
  3064. (list :tag "options/package pair"
  3065. (string :tag "options")
  3066. (string :tag "package")
  3067. (boolean :tag "Snippet"))
  3068. (string :tag "A line of LaTeX"))))
  3069. (defgroup org-appearance nil
  3070. "Settings for Org-mode appearance."
  3071. :tag "Org Appearance"
  3072. :group 'org)
  3073. (defcustom org-level-color-stars-only nil
  3074. "Non-nil means fontify only the stars in each headline.
  3075. When nil, the entire headline is fontified.
  3076. Changing it requires restart of `font-lock-mode' to become effective
  3077. also in regions already fontified."
  3078. :group 'org-appearance
  3079. :type 'boolean)
  3080. (defcustom org-hide-leading-stars nil
  3081. "Non-nil means hide the first N-1 stars in a headline.
  3082. This works by using the face `org-hide' for these stars. This
  3083. face is white for a light background, and black for a dark
  3084. background. You may have to customize the face `org-hide' to
  3085. make this work.
  3086. Changing it requires restart of `font-lock-mode' to become effective
  3087. also in regions already fontified.
  3088. You may also set this on a per-file basis by adding one of the following
  3089. lines to the buffer:
  3090. #+STARTUP: hidestars
  3091. #+STARTUP: showstars"
  3092. :group 'org-appearance
  3093. :type 'boolean)
  3094. (defcustom org-hidden-keywords nil
  3095. "List of symbols corresponding to keywords to be hidden the org buffer.
  3096. For example, a value '(title) for this list will make the document's title
  3097. appear in the buffer without the initial #+TITLE: keyword."
  3098. :group 'org-appearance
  3099. :version "24.1"
  3100. :type '(set (const :tag "#+AUTHOR" author)
  3101. (const :tag "#+DATE" date)
  3102. (const :tag "#+EMAIL" email)
  3103. (const :tag "#+TITLE" title)))
  3104. (defcustom org-fontify-done-headline nil
  3105. "Non-nil means change the face of a headline if it is marked DONE.
  3106. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3107. When this is non-nil, the headline after the keyword is set to the
  3108. `org-headline-done' as an additional indication."
  3109. :group 'org-appearance
  3110. :type 'boolean)
  3111. (defcustom org-fontify-emphasized-text t
  3112. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3113. Changing this variable requires a restart of Emacs to take effect."
  3114. :group 'org-appearance
  3115. :type 'boolean)
  3116. (defcustom org-fontify-whole-heading-line nil
  3117. "Non-nil means fontify the whole line for headings.
  3118. This is useful when setting a background color for the
  3119. org-level-* faces."
  3120. :group 'org-appearance
  3121. :type 'boolean)
  3122. (defcustom org-highlight-latex-fragments-and-specials nil
  3123. "Non-nil means fontify what is treated specially by the exporters."
  3124. :group 'org-appearance
  3125. :type 'boolean)
  3126. (defcustom org-hide-emphasis-markers nil
  3127. "Non-nil mean font-lock should hide the emphasis marker characters."
  3128. :group 'org-appearance
  3129. :type 'boolean)
  3130. (defcustom org-pretty-entities nil
  3131. "Non-nil means show entities as UTF8 characters.
  3132. When nil, the \\name form remains in the buffer."
  3133. :group 'org-appearance
  3134. :version "24.1"
  3135. :type 'boolean)
  3136. (defcustom org-pretty-entities-include-sub-superscripts t
  3137. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3138. :group 'org-appearance
  3139. :version "24.1"
  3140. :type 'boolean)
  3141. (defvar org-emph-re nil
  3142. "Regular expression for matching emphasis.
  3143. After a match, the match groups contain these elements:
  3144. 0 The match of the full regular expression, including the characters
  3145. before and after the proper match
  3146. 1 The character before the proper match, or empty at beginning of line
  3147. 2 The proper match, including the leading and trailing markers
  3148. 3 The leading marker like * or /, indicating the type of highlighting
  3149. 4 The text between the emphasis markers, not including the markers
  3150. 5 The character after the match, empty at the end of a line")
  3151. (defvar org-verbatim-re nil
  3152. "Regular expression for matching verbatim text.")
  3153. (defvar org-emphasis-regexp-components) ; defined just below
  3154. (defvar org-emphasis-alist) ; defined just below
  3155. (defun org-set-emph-re (var val)
  3156. "Set variable and compute the emphasis regular expression."
  3157. (set var val)
  3158. (when (and (boundp 'org-emphasis-alist)
  3159. (boundp 'org-emphasis-regexp-components)
  3160. org-emphasis-alist org-emphasis-regexp-components)
  3161. (let* ((e org-emphasis-regexp-components)
  3162. (pre (car e))
  3163. (post (nth 1 e))
  3164. (border (nth 2 e))
  3165. (body (nth 3 e))
  3166. (nl (nth 4 e))
  3167. (body1 (concat body "*?"))
  3168. (markers (mapconcat 'car org-emphasis-alist ""))
  3169. (vmarkers (mapconcat
  3170. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3171. org-emphasis-alist "")))
  3172. ;; make sure special characters appear at the right position in the class
  3173. (if (string-match "\\^" markers)
  3174. (setq markers (concat (replace-match "" t t markers) "^")))
  3175. (if (string-match "-" markers)
  3176. (setq markers (concat (replace-match "" t t markers) "-")))
  3177. (if (string-match "\\^" vmarkers)
  3178. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3179. (if (string-match "-" vmarkers)
  3180. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3181. (if (> nl 0)
  3182. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3183. (int-to-string nl) "\\}")))
  3184. ;; Make the regexp
  3185. (setq org-emph-re
  3186. (concat "\\([" pre "]\\|^\\)"
  3187. "\\("
  3188. "\\([" markers "]\\)"
  3189. "\\("
  3190. "[^" border "]\\|"
  3191. "[^" border "]"
  3192. body1
  3193. "[^" border "]"
  3194. "\\)"
  3195. "\\3\\)"
  3196. "\\([" post "]\\|$\\)"))
  3197. (setq org-verbatim-re
  3198. (concat "\\([" pre "]\\|^\\)"
  3199. "\\("
  3200. "\\([" vmarkers "]\\)"
  3201. "\\("
  3202. "[^" border "]\\|"
  3203. "[^" border "]"
  3204. body1
  3205. "[^" border "]"
  3206. "\\)"
  3207. "\\3\\)"
  3208. "\\([" post "]\\|$\\)")))))
  3209. (defcustom org-emphasis-regexp-components
  3210. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3211. "Components used to build the regular expression for emphasis.
  3212. This is a list with five entries. Terminology: In an emphasis string
  3213. like \" *strong word* \", we call the initial space PREMATCH, the final
  3214. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3215. and \"trong wor\" is the body. The different components in this variable
  3216. specify what is allowed/forbidden in each part:
  3217. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3218. post Chars allowed as postmatch. End of line will be allowed too.
  3219. border The chars *forbidden* as border characters.
  3220. body-regexp A regexp like \".\" to match a body character. Don't use
  3221. non-shy groups here, and don't allow newline here.
  3222. newline The maximum number of newlines allowed in an emphasis exp.
  3223. Use customize to modify this, or restart Emacs after changing it."
  3224. :group 'org-appearance
  3225. :set 'org-set-emph-re
  3226. :type '(list
  3227. (sexp :tag "Allowed chars in pre ")
  3228. (sexp :tag "Allowed chars in post ")
  3229. (sexp :tag "Forbidden chars in border ")
  3230. (sexp :tag "Regexp for body ")
  3231. (integer :tag "number of newlines allowed")
  3232. (option (boolean :tag "Please ignore this button"))))
  3233. (defcustom org-emphasis-alist
  3234. `(("*" bold "<b>" "</b>")
  3235. ("/" italic "<i>" "</i>")
  3236. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3237. ("=" org-code "<code>" "</code>" verbatim)
  3238. ("~" org-verbatim "<code>" "</code>" verbatim)
  3239. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3240. "<del>" "</del>")
  3241. )
  3242. "Special syntax for emphasized text.
  3243. Text starting and ending with a special character will be emphasized, for
  3244. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3245. characters, the face to be used by font-lock for highlighting in Org-mode
  3246. Emacs buffers, and the HTML tags to be used for this.
  3247. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3248. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3249. Use customize to modify this, or restart Emacs after changing it."
  3250. :group 'org-appearance
  3251. :set 'org-set-emph-re
  3252. :type '(repeat
  3253. (list
  3254. (string :tag "Marker character")
  3255. (choice
  3256. (face :tag "Font-lock-face")
  3257. (plist :tag "Face property list"))
  3258. (string :tag "HTML start tag")
  3259. (string :tag "HTML end tag")
  3260. (option (const verbatim)))))
  3261. (defvar org-protecting-blocks
  3262. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3263. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3264. This is needed for font-lock setup.")
  3265. ;;; Miscellaneous options
  3266. (defgroup org-completion nil
  3267. "Completion in Org-mode."
  3268. :tag "Org Completion"
  3269. :group 'org)
  3270. (defcustom org-completion-use-ido nil
  3271. "Non-nil means use ido completion wherever possible.
  3272. Note that `ido-mode' must be active for this variable to be relevant.
  3273. If you decide to turn this variable on, you might well want to turn off
  3274. `org-outline-path-complete-in-steps'.
  3275. See also `org-completion-use-iswitchb'."
  3276. :group 'org-completion
  3277. :type 'boolean)
  3278. (defcustom org-completion-use-iswitchb nil
  3279. "Non-nil means use iswitchb completion wherever possible.
  3280. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3281. If you decide to turn this variable on, you might well want to turn off
  3282. `org-outline-path-complete-in-steps'.
  3283. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3284. :group 'org-completion
  3285. :type 'boolean)
  3286. (defcustom org-completion-fallback-command 'hippie-expand
  3287. "The expansion command called by \\[pcomplete] in normal context.
  3288. Normal means, no org-mode-specific context."
  3289. :group 'org-completion
  3290. :type 'function)
  3291. ;;; Functions and variables from their packages
  3292. ;; Declared here to avoid compiler warnings
  3293. ;; XEmacs only
  3294. (defvar outline-mode-menu-heading)
  3295. (defvar outline-mode-menu-show)
  3296. (defvar outline-mode-menu-hide)
  3297. (defvar zmacs-regions) ; XEmacs regions
  3298. ;; Emacs only
  3299. (defvar mark-active)
  3300. ;; Various packages
  3301. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3302. (declare-function calendar-forward-day "cal-move" (arg))
  3303. (declare-function calendar-goto-date "cal-move" (date))
  3304. (declare-function calendar-goto-today "cal-move" ())
  3305. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3306. (defvar calc-embedded-close-formula)
  3307. (defvar calc-embedded-open-formula)
  3308. (declare-function cdlatex-tab "ext:cdlatex" ())
  3309. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3310. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3311. (defvar font-lock-unfontify-region-function)
  3312. (declare-function iswitchb-read-buffer "iswitchb"
  3313. (prompt &optional default require-match start matches-set))
  3314. (defvar iswitchb-temp-buflist)
  3315. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3316. (defvar org-agenda-tags-todo-honor-ignore-options)
  3317. (declare-function org-agenda-skip "org-agenda" ())
  3318. (declare-function
  3319. org-agenda-format-item "org-agenda"
  3320. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3321. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3322. (declare-function org-agenda-change-all-lines "org-agenda"
  3323. (newhead hdmarker &optional fixface just-this))
  3324. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3325. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3326. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3327. (beg end))
  3328. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3329. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3330. "org-agenda" (&optional end))
  3331. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3332. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3333. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3334. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3335. (declare-function org-indent-mode "org-indent" (&optional arg))
  3336. (declare-function parse-time-string "parse-time" (string))
  3337. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3338. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3339. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3340. (defvar remember-data-file)
  3341. (defvar texmathp-why)
  3342. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3343. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3344. (defvar w3m-current-url)
  3345. (defvar w3m-current-title)
  3346. (defvar org-latex-regexps)
  3347. ;;; Autoload and prepare some org modules
  3348. ;; Some table stuff that needs to be defined here, because it is used
  3349. ;; by the functions setting up org-mode or checking for table context.
  3350. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3351. "Detect an org-type or table-type table.")
  3352. (defconst org-table-line-regexp "^[ \t]*|"
  3353. "Detect an org-type table line.")
  3354. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3355. "Detect an org-type table line.")
  3356. (defconst org-table-hline-regexp "^[ \t]*|-"
  3357. "Detect an org-type table hline.")
  3358. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3359. "Detect a table-type table hline.")
  3360. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3361. "Detect the first line outside a table when searching from within it.
  3362. This works for both table types.")
  3363. ;; Autoload the functions in org-table.el that are needed by functions here.
  3364. (eval-and-compile
  3365. (org-autoload "org-table"
  3366. '(org-table-align org-table-begin org-table-blank-field
  3367. org-table-convert org-table-convert-region org-table-copy-down
  3368. org-table-copy-region org-table-create
  3369. org-table-create-or-convert-from-region
  3370. org-table-create-with-table.el org-table-current-dline
  3371. org-table-cut-region org-table-delete-column org-table-edit-field
  3372. org-table-edit-formulas org-table-end org-table-eval-formula
  3373. org-table-export org-table-field-info
  3374. org-table-get-stored-formulas org-table-goto-column
  3375. org-table-hline-and-move org-table-import org-table-insert-column
  3376. org-table-insert-hline org-table-insert-row org-table-iterate
  3377. org-table-justify-field-maybe org-table-kill-row
  3378. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3379. org-table-move-column org-table-move-column-left
  3380. org-table-move-column-right org-table-move-row
  3381. org-table-move-row-down org-table-move-row-up
  3382. org-table-next-field org-table-next-row org-table-paste-rectangle
  3383. org-table-previous-field org-table-recalculate
  3384. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3385. org-table-toggle-coordinate-overlays
  3386. org-table-toggle-formula-debugger org-table-wrap-region
  3387. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3388. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3389. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3390. (defun org-at-table-p (&optional table-type)
  3391. "Return t if the cursor is inside an org-type table.
  3392. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3393. (if org-enable-table-editor
  3394. (save-excursion
  3395. (beginning-of-line 1)
  3396. (looking-at (if table-type org-table-any-line-regexp
  3397. org-table-line-regexp)))
  3398. nil))
  3399. (defsubst org-table-p () (org-at-table-p))
  3400. (defun org-at-table.el-p ()
  3401. "Return t if and only if we are at a table.el table."
  3402. (and (org-at-table-p 'any)
  3403. (save-excursion
  3404. (goto-char (org-table-begin 'any))
  3405. (looking-at org-table1-hline-regexp))))
  3406. (defun org-table-recognize-table.el ()
  3407. "If there is a table.el table nearby, recognize it and move into it."
  3408. (if org-table-tab-recognizes-table.el
  3409. (if (org-at-table.el-p)
  3410. (progn
  3411. (beginning-of-line 1)
  3412. (if (looking-at org-table-dataline-regexp)
  3413. nil
  3414. (if (looking-at org-table1-hline-regexp)
  3415. (progn
  3416. (beginning-of-line 2)
  3417. (if (looking-at org-table-any-border-regexp)
  3418. (beginning-of-line -1)))))
  3419. (if (re-search-forward "|" (org-table-end t) t)
  3420. (progn
  3421. (require 'table)
  3422. (if (table--at-cell-p (point))
  3423. t
  3424. (message "recognizing table.el table...")
  3425. (table-recognize-table)
  3426. (message "recognizing table.el table...done")))
  3427. (error "This should not happen"))
  3428. t)
  3429. nil)
  3430. nil))
  3431. (defun org-at-table-hline-p ()
  3432. "Return t if the cursor is inside a hline in a table."
  3433. (if org-enable-table-editor
  3434. (save-excursion
  3435. (beginning-of-line 1)
  3436. (looking-at org-table-hline-regexp))
  3437. nil))
  3438. (defvar org-table-clean-did-remove-column nil)
  3439. (defun org-table-map-tables (function &optional quietly)
  3440. "Apply FUNCTION to the start of all tables in the buffer."
  3441. (save-excursion
  3442. (save-restriction
  3443. (widen)
  3444. (goto-char (point-min))
  3445. (while (re-search-forward org-table-any-line-regexp nil t)
  3446. (unless quietly
  3447. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3448. (beginning-of-line 1)
  3449. (when (looking-at org-table-line-regexp)
  3450. (save-excursion (funcall function))
  3451. (or (looking-at org-table-line-regexp)
  3452. (forward-char 1)))
  3453. (re-search-forward org-table-any-border-regexp nil 1))))
  3454. (unless quietly (message "Mapping tables: done")))
  3455. ;; Declare and autoload functions from org-exp.el & Co
  3456. (declare-function org-default-export-plist "org-exp")
  3457. (declare-function org-infile-export-plist "org-exp")
  3458. (declare-function org-get-current-options "org-exp")
  3459. (eval-and-compile
  3460. (org-autoload "org-exp"
  3461. '(org-export org-export-visible
  3462. org-insert-export-options-template
  3463. org-table-clean-before-export))
  3464. (org-autoload "org-ascii"
  3465. '(org-export-as-ascii org-export-ascii-preprocess
  3466. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3467. org-export-region-as-ascii))
  3468. (org-autoload "org-latex"
  3469. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3470. org-replace-region-by-latex org-export-region-as-latex
  3471. org-export-as-latex org-export-as-pdf
  3472. org-export-as-pdf-and-open))
  3473. (org-autoload "org-html"
  3474. '(org-export-as-html-and-open
  3475. org-export-as-html-batch org-export-as-html-to-buffer
  3476. org-replace-region-by-html org-export-region-as-html
  3477. org-export-as-html))
  3478. (org-autoload "org-docbook"
  3479. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3480. org-replace-region-by-docbook org-export-region-as-docbook
  3481. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3482. org-export-as-docbook))
  3483. (org-autoload "org-icalendar"
  3484. '(org-export-icalendar-this-file
  3485. org-export-icalendar-all-agenda-files
  3486. org-export-icalendar-combine-agenda-files))
  3487. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3488. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3489. ;; Declare and autoload functions from org-agenda.el
  3490. (eval-and-compile
  3491. (org-autoload "org-agenda"
  3492. '(org-agenda org-agenda-list org-search-view
  3493. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3494. org-diary org-agenda-to-appt
  3495. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3496. ;; Autoload org-remember
  3497. (eval-and-compile
  3498. (org-autoload "org-remember"
  3499. '(org-remember-insinuate org-remember-annotation
  3500. org-remember-apply-template org-remember org-remember-handler)))
  3501. (eval-and-compile
  3502. (org-autoload "org-capture"
  3503. '(org-capture org-capture-insert-template-here
  3504. org-capture-import-remember-templates)))
  3505. ;; Autoload org-clock.el
  3506. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3507. (beg end))
  3508. (declare-function org-clock-update-mode-line "org-clock" ())
  3509. (declare-function org-resolve-clocks "org-clock"
  3510. (&optional also-non-dangling-p prompt last-valid))
  3511. (defvar org-clock-start-time)
  3512. (defvar org-clock-marker (make-marker)
  3513. "Marker recording the last clock-in.")
  3514. (defvar org-clock-hd-marker (make-marker)
  3515. "Marker recording the last clock-in, but the headline position.")
  3516. (defvar org-clock-heading ""
  3517. "The heading of the current clock entry.")
  3518. (defun org-clock-is-active ()
  3519. "Return non-nil if clock is currently running.
  3520. The return value is actually the clock marker."
  3521. (marker-buffer org-clock-marker))
  3522. (eval-and-compile
  3523. (org-autoload
  3524. "org-clock"
  3525. '(org-clock-in org-clock-out org-clock-cancel
  3526. org-clock-goto org-clock-sum org-clock-display
  3527. org-clock-remove-overlays org-clock-report
  3528. org-clocktable-shift org-dblock-write:clocktable
  3529. org-get-clocktable org-resolve-clocks)))
  3530. (defun org-clock-update-time-maybe ()
  3531. "If this is a CLOCK line, update it and return t.
  3532. Otherwise, return nil."
  3533. (interactive)
  3534. (save-excursion
  3535. (beginning-of-line 1)
  3536. (skip-chars-forward " \t")
  3537. (when (looking-at org-clock-string)
  3538. (let ((re (concat "[ \t]*" org-clock-string
  3539. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3540. "\\([ \t]*=>.*\\)?\\)?"))
  3541. ts te h m s neg)
  3542. (cond
  3543. ((not (looking-at re))
  3544. nil)
  3545. ((not (match-end 2))
  3546. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3547. (> org-clock-marker (point))
  3548. (<= org-clock-marker (point-at-eol)))
  3549. ;; The clock is running here
  3550. (setq org-clock-start-time
  3551. (apply 'encode-time
  3552. (org-parse-time-string (match-string 1))))
  3553. (org-clock-update-mode-line)))
  3554. (t
  3555. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3556. (end-of-line 1)
  3557. (setq ts (match-string 1)
  3558. te (match-string 3))
  3559. (setq s (- (org-float-time
  3560. (apply 'encode-time (org-parse-time-string te)))
  3561. (org-float-time
  3562. (apply 'encode-time (org-parse-time-string ts))))
  3563. neg (< s 0)
  3564. s (abs s)
  3565. h (floor (/ s 3600))
  3566. s (- s (* 3600 h))
  3567. m (floor (/ s 60))
  3568. s (- s (* 60 s)))
  3569. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3570. t))))))
  3571. (defun org-check-running-clock ()
  3572. "Check if the current buffer contains the running clock.
  3573. If yes, offer to stop it and to save the buffer with the changes."
  3574. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3575. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3576. (buffer-name))))
  3577. (org-clock-out)
  3578. (when (y-or-n-p "Save changed buffer?")
  3579. (save-buffer))))
  3580. (defun org-clocktable-try-shift (dir n)
  3581. "Check if this line starts a clock table, if yes, shift the time block."
  3582. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3583. (org-clocktable-shift dir n)))
  3584. ;; Autoload org-timer.el
  3585. (eval-and-compile
  3586. (org-autoload
  3587. "org-timer"
  3588. '(org-timer-start org-timer org-timer-item
  3589. org-timer-change-times-in-region
  3590. org-timer-set-timer
  3591. org-timer-reset-timers
  3592. org-timer-show-remaining-time)))
  3593. ;; Autoload org-feed.el
  3594. (eval-and-compile
  3595. (org-autoload
  3596. "org-feed"
  3597. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3598. ;; Autoload org-indent.el
  3599. ;; Define the variable already here, to make sure we have it.
  3600. (defvar org-indent-mode nil
  3601. "Non-nil if Org-Indent mode is enabled.
  3602. Use the command `org-indent-mode' to change this variable.")
  3603. (eval-and-compile
  3604. (org-autoload
  3605. "org-indent"
  3606. '(org-indent-mode)))
  3607. ;; Autoload org-mobile.el
  3608. (eval-and-compile
  3609. (org-autoload
  3610. "org-mobile"
  3611. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3612. ;; Autoload archiving code
  3613. ;; The stuff that is needed for cycling and tags has to be defined here.
  3614. (defgroup org-archive nil
  3615. "Options concerning archiving in Org-mode."
  3616. :tag "Org Archive"
  3617. :group 'org-structure)
  3618. (defcustom org-archive-location "%s_archive::"
  3619. "The location where subtrees should be archived.
  3620. The value of this variable is a string, consisting of two parts,
  3621. separated by a double-colon. The first part is a filename and
  3622. the second part is a headline.
  3623. When the filename is omitted, archiving happens in the same file.
  3624. %s in the filename will be replaced by the current file
  3625. name (without the directory part). Archiving to a different file
  3626. is useful to keep archived entries from contributing to the
  3627. Org-mode Agenda.
  3628. The archived entries will be filed as subtrees of the specified
  3629. headline. When the headline is omitted, the subtrees are simply
  3630. filed away at the end of the file, as top-level entries. Also in
  3631. the heading you can use %s to represent the file name, this can be
  3632. useful when using the same archive for a number of different files.
  3633. Here are a few examples:
  3634. \"%s_archive::\"
  3635. If the current file is Projects.org, archive in file
  3636. Projects.org_archive, as top-level trees. This is the default.
  3637. \"::* Archived Tasks\"
  3638. Archive in the current file, under the top-level headline
  3639. \"* Archived Tasks\".
  3640. \"~/org/archive.org::\"
  3641. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3642. \"~/org/archive.org::* From %s\"
  3643. Archive in file ~/org/archive.org (absolute path), under headlines
  3644. \"From FILENAME\" where file name is the current file name.
  3645. \"basement::** Finished Tasks\"
  3646. Archive in file ./basement (relative path), as level 3 trees
  3647. below the level 2 heading \"** Finished Tasks\".
  3648. You may set this option on a per-file basis by adding to the buffer a
  3649. line like
  3650. #+ARCHIVE: basement::** Finished Tasks
  3651. You may also define it locally for a subtree by setting an ARCHIVE property
  3652. in the entry. If such a property is found in an entry, or anywhere up
  3653. the hierarchy, it will be used."
  3654. :group 'org-archive
  3655. :type 'string)
  3656. (defcustom org-archive-tag "ARCHIVE"
  3657. "The tag that marks a subtree as archived.
  3658. An archived subtree does not open during visibility cycling, and does
  3659. not contribute to the agenda listings.
  3660. After changing this, font-lock must be restarted in the relevant buffers to
  3661. get the proper fontification."
  3662. :group 'org-archive
  3663. :group 'org-keywords
  3664. :type 'string)
  3665. (defcustom org-agenda-skip-archived-trees t
  3666. "Non-nil means the agenda will skip any items located in archived trees.
  3667. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3668. variable is no longer recommended, you should leave it at the value t.
  3669. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3670. :group 'org-archive
  3671. :group 'org-agenda-skip
  3672. :type 'boolean)
  3673. (defcustom org-columns-skip-archived-trees t
  3674. "Non-nil means ignore archived trees when creating column view."
  3675. :group 'org-archive
  3676. :group 'org-properties
  3677. :type 'boolean)
  3678. (defcustom org-cycle-open-archived-trees nil
  3679. "Non-nil means `org-cycle' will open archived trees.
  3680. An archived tree is a tree marked with the tag ARCHIVE.
  3681. When nil, archived trees will stay folded. You can still open them with
  3682. normal outline commands like `show-all', but not with the cycling commands."
  3683. :group 'org-archive
  3684. :group 'org-cycle
  3685. :type 'boolean)
  3686. (defcustom org-sparse-tree-open-archived-trees nil
  3687. "Non-nil means sparse tree construction shows matches in archived trees.
  3688. When nil, matches in these trees are highlighted, but the trees are kept in
  3689. collapsed state."
  3690. :group 'org-archive
  3691. :group 'org-sparse-trees
  3692. :type 'boolean)
  3693. (defun org-cycle-hide-archived-subtrees (state)
  3694. "Re-hide all archived subtrees after a visibility state change."
  3695. (when (and (not org-cycle-open-archived-trees)
  3696. (not (memq state '(overview folded))))
  3697. (save-excursion
  3698. (let* ((globalp (memq state '(contents all)))
  3699. (beg (if globalp (point-min) (point)))
  3700. (end (if globalp (point-max) (org-end-of-subtree t))))
  3701. (org-hide-archived-subtrees beg end)
  3702. (goto-char beg)
  3703. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3704. (message "%s" (substitute-command-keys
  3705. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3706. (defun org-force-cycle-archived ()
  3707. "Cycle subtree even if it is archived."
  3708. (interactive)
  3709. (setq this-command 'org-cycle)
  3710. (let ((org-cycle-open-archived-trees t))
  3711. (call-interactively 'org-cycle)))
  3712. (defun org-hide-archived-subtrees (beg end)
  3713. "Re-hide all archived subtrees after a visibility state change."
  3714. (save-excursion
  3715. (let* ((re (concat ":" org-archive-tag ":")))
  3716. (goto-char beg)
  3717. (while (re-search-forward re end t)
  3718. (when (org-at-heading-p)
  3719. (org-flag-subtree t)
  3720. (org-end-of-subtree t))))))
  3721. (defun org-flag-subtree (flag)
  3722. (save-excursion
  3723. (org-back-to-heading t)
  3724. (outline-end-of-heading)
  3725. (outline-flag-region (point)
  3726. (progn (org-end-of-subtree t) (point))
  3727. flag)))
  3728. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3729. (eval-and-compile
  3730. (org-autoload "org-archive"
  3731. '(org-add-archive-files org-archive-subtree
  3732. org-archive-to-archive-sibling org-toggle-archive-tag
  3733. org-archive-subtree-default
  3734. org-archive-subtree-default-with-confirmation)))
  3735. ;; Autoload Column View Code
  3736. (declare-function org-columns-number-to-string "org-colview")
  3737. (declare-function org-columns-get-format-and-top-level "org-colview")
  3738. (declare-function org-columns-compute "org-colview")
  3739. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3740. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3741. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3742. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3743. ;; Autoload ID code
  3744. (declare-function org-id-store-link "org-id")
  3745. (declare-function org-id-locations-load "org-id")
  3746. (declare-function org-id-locations-save "org-id")
  3747. (defvar org-id-track-globally)
  3748. (org-autoload "org-id"
  3749. '(org-id-get-create org-id-new org-id-copy org-id-get
  3750. org-id-get-with-outline-path-completion
  3751. org-id-get-with-outline-drilling org-id-store-link
  3752. org-id-goto org-id-find org-id-store-link))
  3753. ;; Autoload Plotting Code
  3754. (org-autoload "org-plot"
  3755. '(org-plot/gnuplot))
  3756. ;;; Variables for pre-computed regular expressions, all buffer local
  3757. (defvar org-drawer-regexp nil
  3758. "Matches first line of a hidden block.")
  3759. (make-variable-buffer-local 'org-drawer-regexp)
  3760. (defvar org-todo-regexp nil
  3761. "Matches any of the TODO state keywords.")
  3762. (make-variable-buffer-local 'org-todo-regexp)
  3763. (defvar org-not-done-regexp nil
  3764. "Matches any of the TODO state keywords except the last one.")
  3765. (make-variable-buffer-local 'org-not-done-regexp)
  3766. (defvar org-not-done-heading-regexp nil
  3767. "Matches a TODO headline that is not done.")
  3768. (make-variable-buffer-local 'org-not-done-regexp)
  3769. (defvar org-todo-line-regexp nil
  3770. "Matches a headline and puts TODO state into group 2 if present.")
  3771. (make-variable-buffer-local 'org-todo-line-regexp)
  3772. (defvar org-complex-heading-regexp nil
  3773. "Matches a headline and puts everything into groups:
  3774. group 1: the stars
  3775. group 2: The todo keyword, maybe
  3776. group 3: Priority cookie
  3777. group 4: True headline
  3778. group 5: Tags")
  3779. (make-variable-buffer-local 'org-complex-heading-regexp)
  3780. (defvar org-complex-heading-regexp-format nil
  3781. "Printf format to make regexp to match an exact headline.
  3782. This regexp will match the headline of any node which has the
  3783. exact headline text that is put into the format, but may have any
  3784. TODO state, priority and tags.")
  3785. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3786. (defvar org-todo-line-tags-regexp nil
  3787. "Matches a headline and puts TODO state into group 2 if present.
  3788. Also put tags into group 4 if tags are present.")
  3789. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3790. (defvar org-ds-keyword-length 12
  3791. "Maximum length of the Deadline and SCHEDULED keywords.")
  3792. (make-variable-buffer-local 'org-ds-keyword-length)
  3793. (defvar org-deadline-regexp nil
  3794. "Matches the DEADLINE keyword.")
  3795. (make-variable-buffer-local 'org-deadline-regexp)
  3796. (defvar org-deadline-time-regexp nil
  3797. "Matches the DEADLINE keyword together with a time stamp.")
  3798. (make-variable-buffer-local 'org-deadline-time-regexp)
  3799. (defvar org-deadline-line-regexp nil
  3800. "Matches the DEADLINE keyword and the rest of the line.")
  3801. (make-variable-buffer-local 'org-deadline-line-regexp)
  3802. (defvar org-scheduled-regexp nil
  3803. "Matches the SCHEDULED keyword.")
  3804. (make-variable-buffer-local 'org-scheduled-regexp)
  3805. (defvar org-scheduled-time-regexp nil
  3806. "Matches the SCHEDULED keyword together with a time stamp.")
  3807. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3808. (defvar org-closed-time-regexp nil
  3809. "Matches the CLOSED keyword together with a time stamp.")
  3810. (make-variable-buffer-local 'org-closed-time-regexp)
  3811. (defvar org-keyword-time-regexp nil
  3812. "Matches any of the 4 keywords, together with the time stamp.")
  3813. (make-variable-buffer-local 'org-keyword-time-regexp)
  3814. (defvar org-keyword-time-not-clock-regexp nil
  3815. "Matches any of the 3 keywords, together with the time stamp.")
  3816. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3817. (defvar org-maybe-keyword-time-regexp nil
  3818. "Matches a timestamp, possibly preceded by a keyword.")
  3819. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3820. (defvar org-planning-or-clock-line-re nil
  3821. "Matches a line with planning or clock info.")
  3822. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3823. (defvar org-all-time-keywords nil
  3824. "List of time keywords.")
  3825. (make-variable-buffer-local 'org-all-time-keywords)
  3826. (defconst org-plain-time-of-day-regexp
  3827. (concat
  3828. "\\(\\<[012]?[0-9]"
  3829. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3830. "\\(--?"
  3831. "\\(\\<[012]?[0-9]"
  3832. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3833. "\\)?")
  3834. "Regular expression to match a plain time or time range.
  3835. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3836. groups carry important information:
  3837. 0 the full match
  3838. 1 the first time, range or not
  3839. 8 the second time, if it is a range.")
  3840. (defconst org-plain-time-extension-regexp
  3841. (concat
  3842. "\\(\\<[012]?[0-9]"
  3843. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3844. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3845. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3846. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3847. groups carry important information:
  3848. 0 the full match
  3849. 7 hours of duration
  3850. 9 minutes of duration")
  3851. (defconst org-stamp-time-of-day-regexp
  3852. (concat
  3853. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3854. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3855. "\\(--?"
  3856. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3857. "Regular expression to match a timestamp time or time range.
  3858. After a match, the following groups carry important information:
  3859. 0 the full match
  3860. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3861. 2 the first time, range or not
  3862. 4 the second time, if it is a range.")
  3863. (defconst org-startup-options
  3864. '(("fold" org-startup-folded t)
  3865. ("overview" org-startup-folded t)
  3866. ("nofold" org-startup-folded nil)
  3867. ("showall" org-startup-folded nil)
  3868. ("showeverything" org-startup-folded showeverything)
  3869. ("content" org-startup-folded content)
  3870. ("indent" org-startup-indented t)
  3871. ("noindent" org-startup-indented nil)
  3872. ("hidestars" org-hide-leading-stars t)
  3873. ("showstars" org-hide-leading-stars nil)
  3874. ("odd" org-odd-levels-only t)
  3875. ("oddeven" org-odd-levels-only nil)
  3876. ("align" org-startup-align-all-tables t)
  3877. ("noalign" org-startup-align-all-tables nil)
  3878. ("inlineimages" org-startup-with-inline-images t)
  3879. ("noinlineimages" org-startup-with-inline-images nil)
  3880. ("customtime" org-display-custom-times t)
  3881. ("logdone" org-log-done time)
  3882. ("lognotedone" org-log-done note)
  3883. ("nologdone" org-log-done nil)
  3884. ("lognoteclock-out" org-log-note-clock-out t)
  3885. ("nolognoteclock-out" org-log-note-clock-out nil)
  3886. ("logrepeat" org-log-repeat state)
  3887. ("lognoterepeat" org-log-repeat note)
  3888. ("nologrepeat" org-log-repeat nil)
  3889. ("logreschedule" org-log-reschedule time)
  3890. ("lognotereschedule" org-log-reschedule note)
  3891. ("nologreschedule" org-log-reschedule nil)
  3892. ("logredeadline" org-log-redeadline time)
  3893. ("lognoteredeadline" org-log-redeadline note)
  3894. ("nologredeadline" org-log-redeadline nil)
  3895. ("logrefile" org-log-refile time)
  3896. ("lognoterefile" org-log-refile note)
  3897. ("nologrefile" org-log-refile nil)
  3898. ("fninline" org-footnote-define-inline t)
  3899. ("nofninline" org-footnote-define-inline nil)
  3900. ("fnlocal" org-footnote-section nil)
  3901. ("fnauto" org-footnote-auto-label t)
  3902. ("fnprompt" org-footnote-auto-label nil)
  3903. ("fnconfirm" org-footnote-auto-label confirm)
  3904. ("fnplain" org-footnote-auto-label plain)
  3905. ("fnadjust" org-footnote-auto-adjust t)
  3906. ("nofnadjust" org-footnote-auto-adjust nil)
  3907. ("constcgs" constants-unit-system cgs)
  3908. ("constSI" constants-unit-system SI)
  3909. ("noptag" org-tag-persistent-alist nil)
  3910. ("hideblocks" org-hide-block-startup t)
  3911. ("nohideblocks" org-hide-block-startup nil)
  3912. ("beamer" org-startup-with-beamer-mode t)
  3913. ("entitiespretty" org-pretty-entities t)
  3914. ("entitiesplain" org-pretty-entities nil))
  3915. "Variable associated with STARTUP options for org-mode.
  3916. Each element is a list of three items: The startup options as written
  3917. in the #+STARTUP line, the corresponding variable, and the value to
  3918. set this variable to if the option is found. An optional forth element PUSH
  3919. means to push this value onto the list in the variable.")
  3920. (defun org-update-property-plist (key val props)
  3921. "Update PROPS with KEY and VAL."
  3922. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3923. (key (if appending (substring key 0 (- (length key) 1)) key))
  3924. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3925. (previous (cdr (assoc key props))))
  3926. (if appending
  3927. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3928. (cons (cons key val) remainder))))
  3929. (defconst org-block-regexp
  3930. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3931. "Regular expression for hiding blocks.")
  3932. (defconst org-heading-keyword-regexp-format
  3933. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3934. "Printf format for a regexp matching an headline with some keyword.
  3935. This regexp will match the headline of any node which has the
  3936. exact keyword that is put into the format. The keyword isn't in
  3937. any group by default, but the stars and the body are.")
  3938. (defconst org-heading-keyword-maybe-regexp-format
  3939. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3940. "Printf format for a regexp matching an headline, possibly with some keyword.
  3941. This regexp can match any headline with the specified keyword, or
  3942. without a keyword. The keyword isn't in any group by default,
  3943. but the stars and the body are.")
  3944. (defun org-set-regexps-and-options ()
  3945. "Precompute regular expressions for current buffer."
  3946. (when (eq major-mode 'org-mode)
  3947. (org-set-local 'org-todo-kwd-alist nil)
  3948. (org-set-local 'org-todo-key-alist nil)
  3949. (org-set-local 'org-todo-key-trigger nil)
  3950. (org-set-local 'org-todo-keywords-1 nil)
  3951. (org-set-local 'org-done-keywords nil)
  3952. (org-set-local 'org-todo-heads nil)
  3953. (org-set-local 'org-todo-sets nil)
  3954. (org-set-local 'org-todo-log-states nil)
  3955. (org-set-local 'org-file-properties nil)
  3956. (org-set-local 'org-file-tags nil)
  3957. (let ((re (org-make-options-regexp
  3958. '("CATEGORY" "TODO" "COLUMNS"
  3959. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3960. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3961. "OPTIONS")
  3962. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3963. (splitre "[ \t]+")
  3964. (scripts org-use-sub-superscripts)
  3965. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3966. tail sep kws1 prio props ftags drawers beamer-p
  3967. ext-setup-or-nil setup-contents (start 0))
  3968. (save-excursion
  3969. (save-restriction
  3970. (widen)
  3971. (goto-char (point-min))
  3972. (while (or (and ext-setup-or-nil
  3973. (string-match re ext-setup-or-nil start)
  3974. (setq start (match-end 0)))
  3975. (and (setq ext-setup-or-nil nil start 0)
  3976. (re-search-forward re nil t)))
  3977. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3978. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3979. (if (stringp value) (setq value (org-trim value)))
  3980. (cond
  3981. ((equal key "CATEGORY")
  3982. (setq cat value))
  3983. ((member key '("SEQ_TODO" "TODO"))
  3984. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3985. ((equal key "TYP_TODO")
  3986. (push (cons 'type (org-split-string value splitre)) kwds))
  3987. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3988. ;; general TODO-like setup
  3989. (push (cons (intern (downcase (match-string 1 key)))
  3990. (org-split-string value splitre)) kwds))
  3991. ((equal key "TAGS")
  3992. (setq tags (append tags (if tags '("\\n") nil)
  3993. (org-split-string value splitre))))
  3994. ((equal key "COLUMNS")
  3995. (org-set-local 'org-columns-default-format value))
  3996. ((equal key "LINK")
  3997. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3998. (push (cons (match-string 1 value)
  3999. (org-trim (match-string 2 value)))
  4000. links)))
  4001. ((equal key "PRIORITIES")
  4002. (setq prio (org-split-string value " +")))
  4003. ((equal key "PROPERTY")
  4004. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4005. (setq props (org-update-property-plist (match-string 1 value)
  4006. (match-string 2 value)
  4007. props))))
  4008. ((equal key "FILETAGS")
  4009. (when (string-match "\\S-" value)
  4010. (setq ftags
  4011. (append
  4012. ftags
  4013. (apply 'append
  4014. (mapcar (lambda (x) (org-split-string x ":"))
  4015. (org-split-string value)))))))
  4016. ((equal key "DRAWERS")
  4017. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4018. ((equal key "CONSTANTS")
  4019. (setq const (append const (org-split-string value splitre))))
  4020. ((equal key "STARTUP")
  4021. (let ((opts (org-split-string value splitre))
  4022. l var val)
  4023. (while (setq l (pop opts))
  4024. (when (setq l (assoc l org-startup-options))
  4025. (setq var (nth 1 l) val (nth 2 l))
  4026. (if (not (nth 3 l))
  4027. (set (make-local-variable var) val)
  4028. (if (not (listp (symbol-value var)))
  4029. (set (make-local-variable var) nil))
  4030. (set (make-local-variable var) (symbol-value var))
  4031. (add-to-list var val))))))
  4032. ((equal key "ARCHIVE")
  4033. (setq arch value)
  4034. (remove-text-properties 0 (length arch)
  4035. '(face t fontified t) arch))
  4036. ((equal key "LATEX_CLASS")
  4037. (setq beamer-p (equal value "beamer")))
  4038. ((equal key "OPTIONS")
  4039. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4040. (setq scripts (read (match-string 2 value)))))
  4041. ((equal key "SETUPFILE")
  4042. (setq setup-contents (org-file-contents
  4043. (expand-file-name
  4044. (org-remove-double-quotes value))
  4045. 'noerror))
  4046. (if (not ext-setup-or-nil)
  4047. (setq ext-setup-or-nil setup-contents start 0)
  4048. (setq ext-setup-or-nil
  4049. (concat (substring ext-setup-or-nil 0 start)
  4050. "\n" setup-contents "\n"
  4051. (substring ext-setup-or-nil start)))))))
  4052. ;; search for property blocks
  4053. (goto-char (point-min))
  4054. (while (re-search-forward org-block-regexp nil t)
  4055. (when (equal "PROPERTY" (upcase (match-string 1)))
  4056. (setq value (replace-regexp-in-string
  4057. "[\n\r]" " " (match-string 4)))
  4058. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4059. (setq props (org-update-property-plist (match-string 1 value)
  4060. (match-string 2 value)
  4061. props)))))))
  4062. (org-set-local 'org-use-sub-superscripts scripts)
  4063. (when cat
  4064. (org-set-local 'org-category (intern cat))
  4065. (push (cons "CATEGORY" cat) props))
  4066. (when prio
  4067. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4068. (setq prio (mapcar 'string-to-char prio))
  4069. (org-set-local 'org-highest-priority (nth 0 prio))
  4070. (org-set-local 'org-lowest-priority (nth 1 prio))
  4071. (org-set-local 'org-default-priority (nth 2 prio)))
  4072. (and props (org-set-local 'org-file-properties (nreverse props)))
  4073. (and ftags (org-set-local 'org-file-tags
  4074. (mapcar 'org-add-prop-inherited ftags)))
  4075. (and drawers (org-set-local 'org-drawers drawers))
  4076. (and arch (org-set-local 'org-archive-location arch))
  4077. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4078. ;; Process the TODO keywords
  4079. (unless kwds
  4080. ;; Use the global values as if they had been given locally.
  4081. (setq kwds (default-value 'org-todo-keywords))
  4082. (if (stringp (car kwds))
  4083. (setq kwds (list (cons org-todo-interpretation
  4084. (default-value 'org-todo-keywords)))))
  4085. (setq kwds (reverse kwds)))
  4086. (setq kwds (nreverse kwds))
  4087. (let (inter kws kw)
  4088. (while (setq kws (pop kwds))
  4089. (let ((kws (or
  4090. (run-hook-with-args-until-success
  4091. 'org-todo-setup-filter-hook kws)
  4092. kws)))
  4093. (setq inter (pop kws) sep (member "|" kws)
  4094. kws0 (delete "|" (copy-sequence kws))
  4095. kwsa nil
  4096. kws1 (mapcar
  4097. (lambda (x)
  4098. ;; 1 2
  4099. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4100. (progn
  4101. (setq kw (match-string 1 x)
  4102. key (and (match-end 2) (match-string 2 x))
  4103. log (org-extract-log-state-settings x))
  4104. (push (cons kw (and key (string-to-char key))) kwsa)
  4105. (and log (push log org-todo-log-states))
  4106. kw)
  4107. (error "Invalid TODO keyword %s" x)))
  4108. kws0)
  4109. kwsa (if kwsa (append '((:startgroup))
  4110. (nreverse kwsa)
  4111. '((:endgroup))))
  4112. hw (car kws1)
  4113. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4114. tail (list inter hw (car dws) (org-last dws))))
  4115. (add-to-list 'org-todo-heads hw 'append)
  4116. (push kws1 org-todo-sets)
  4117. (setq org-done-keywords (append org-done-keywords dws nil))
  4118. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4119. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4120. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4121. (setq org-todo-sets (nreverse org-todo-sets)
  4122. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4123. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4124. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4125. ;; Process the constants
  4126. (when const
  4127. (let (e cst)
  4128. (while (setq e (pop const))
  4129. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4130. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4131. (setq org-table-formula-constants-local cst)))
  4132. ;; Process the tags.
  4133. (when tags
  4134. (let (e tgs)
  4135. (while (setq e (pop tags))
  4136. (cond
  4137. ((equal e "{") (push '(:startgroup) tgs))
  4138. ((equal e "}") (push '(:endgroup) tgs))
  4139. ((equal e "\\n") (push '(:newline) tgs))
  4140. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4141. (push (cons (match-string 1 e)
  4142. (string-to-char (match-string 2 e)))
  4143. tgs))
  4144. (t (push (list e) tgs))))
  4145. (org-set-local 'org-tag-alist nil)
  4146. (while (setq e (pop tgs))
  4147. (or (and (stringp (car e))
  4148. (assoc (car e) org-tag-alist))
  4149. (push e org-tag-alist)))))
  4150. ;; Compute the regular expressions and other local variables.
  4151. ;; Using `org-outline-regexp-bol' would complicate them much,
  4152. ;; because of the fixed white space at the end of that string.
  4153. (if (not org-done-keywords)
  4154. (setq org-done-keywords (and org-todo-keywords-1
  4155. (list (org-last org-todo-keywords-1)))))
  4156. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4157. (length org-scheduled-string)
  4158. (length org-clock-string)
  4159. (length org-closed-string)))
  4160. org-drawer-regexp
  4161. (concat "^[ \t]*:\\("
  4162. (mapconcat 'regexp-quote org-drawers "\\|")
  4163. "\\):[ \t]*$")
  4164. org-not-done-keywords
  4165. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4166. org-todo-regexp
  4167. (concat "\\("
  4168. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4169. "\\)")
  4170. org-not-done-regexp
  4171. (concat "\\("
  4172. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4173. "\\)")
  4174. org-not-done-heading-regexp
  4175. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4176. org-todo-line-regexp
  4177. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4178. org-complex-heading-regexp
  4179. (concat "^\\(\\*+\\)"
  4180. "\\(?: +" org-todo-regexp "\\)?"
  4181. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4182. "\\(?: +\\(.*?\\)\\)?"
  4183. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4184. "[ \t]*$")
  4185. org-complex-heading-regexp-format
  4186. (concat "^\\(\\*+\\)"
  4187. "\\(?: +" org-todo-regexp "\\)?"
  4188. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4189. "\\(?: +"
  4190. ;; Stats cookies can be stuck to body.
  4191. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4192. "\\(%s\\)"
  4193. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4194. "\\)"
  4195. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4196. "[ \t]*$")
  4197. org-todo-line-tags-regexp
  4198. (concat "^\\(\\*+\\)"
  4199. "\\(?: +" org-todo-regexp "\\)?"
  4200. "\\(?: +\\(.*?\\)\\)?"
  4201. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4202. "[ \t]*$")
  4203. org-deadline-regexp (concat "\\<" org-deadline-string)
  4204. org-deadline-time-regexp
  4205. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4206. org-deadline-line-regexp
  4207. (concat "\\<\\(" org-deadline-string "\\).*")
  4208. org-scheduled-regexp
  4209. (concat "\\<" org-scheduled-string)
  4210. org-scheduled-time-regexp
  4211. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4212. org-closed-time-regexp
  4213. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4214. org-keyword-time-regexp
  4215. (concat "\\<\\(" org-scheduled-string
  4216. "\\|" org-deadline-string
  4217. "\\|" org-closed-string
  4218. "\\|" org-clock-string "\\)"
  4219. " *[[<]\\([^]>]+\\)[]>]")
  4220. org-keyword-time-not-clock-regexp
  4221. (concat "\\<\\(" org-scheduled-string
  4222. "\\|" org-deadline-string
  4223. "\\|" org-closed-string
  4224. "\\)"
  4225. " *[[<]\\([^]>]+\\)[]>]")
  4226. org-maybe-keyword-time-regexp
  4227. (concat "\\(\\<\\(" org-scheduled-string
  4228. "\\|" org-deadline-string
  4229. "\\|" org-closed-string
  4230. "\\|" org-clock-string "\\)\\)?"
  4231. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4232. org-planning-or-clock-line-re
  4233. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4234. "\\|" org-deadline-string
  4235. "\\|" org-closed-string "\\|" org-clock-string
  4236. "\\)\\>\\)")
  4237. org-all-time-keywords
  4238. (mapcar (lambda (w) (substring w 0 -1))
  4239. (list org-scheduled-string org-deadline-string
  4240. org-clock-string org-closed-string))
  4241. )
  4242. (org-compute-latex-and-specials-regexp)
  4243. (org-set-font-lock-defaults))))
  4244. (defun org-file-contents (file &optional noerror)
  4245. "Return the contents of FILE, as a string."
  4246. (if (or (not file)
  4247. (not (file-readable-p file)))
  4248. (if noerror
  4249. (progn
  4250. (message "Cannot read file \"%s\"" file)
  4251. (ding) (sit-for 2)
  4252. "")
  4253. (error "Cannot read file \"%s\"" file))
  4254. (with-temp-buffer
  4255. (insert-file-contents file)
  4256. (buffer-string))))
  4257. (defun org-extract-log-state-settings (x)
  4258. "Extract the log state setting from a TODO keyword string.
  4259. This will extract info from a string like \"WAIT(w@/!)\"."
  4260. (let (kw key log1 log2)
  4261. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4262. (setq kw (match-string 1 x)
  4263. key (and (match-end 2) (match-string 2 x))
  4264. log1 (and (match-end 3) (match-string 3 x))
  4265. log2 (and (match-end 4) (match-string 4 x)))
  4266. (and (or log1 log2)
  4267. (list kw
  4268. (and log1 (if (equal log1 "!") 'time 'note))
  4269. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4270. (defun org-remove-keyword-keys (list)
  4271. "Remove a pair of parenthesis at the end of each string in LIST."
  4272. (mapcar (lambda (x)
  4273. (if (string-match "(.*)$" x)
  4274. (substring x 0 (match-beginning 0))
  4275. x))
  4276. list))
  4277. (defun org-assign-fast-keys (alist)
  4278. "Assign fast keys to a keyword-key alist.
  4279. Respect keys that are already there."
  4280. (let (new e (alt ?0))
  4281. (while (setq e (pop alist))
  4282. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4283. (cdr e)) ;; Key already assigned.
  4284. (push e new)
  4285. (let ((clist (string-to-list (downcase (car e))))
  4286. (used (append new alist)))
  4287. (when (= (car clist) ?@)
  4288. (pop clist))
  4289. (while (and clist (rassoc (car clist) used))
  4290. (pop clist))
  4291. (unless clist
  4292. (while (rassoc alt used)
  4293. (incf alt)))
  4294. (push (cons (car e) (or (car clist) alt)) new))))
  4295. (nreverse new)))
  4296. ;;; Some variables used in various places
  4297. (defvar org-window-configuration nil
  4298. "Used in various places to store a window configuration.")
  4299. (defvar org-selected-window nil
  4300. "Used in various places to store a window configuration.")
  4301. (defvar org-finish-function nil
  4302. "Function to be called when `C-c C-c' is used.
  4303. This is for getting out of special buffers like remember.")
  4304. ;; FIXME: Occasionally check by commenting these, to make sure
  4305. ;; no other functions uses these, forgetting to let-bind them.
  4306. (defvar entry)
  4307. (defvar last-state)
  4308. (defvar date)
  4309. ;; Defined somewhere in this file, but used before definition.
  4310. (defvar org-entities) ;; defined in org-entities.el
  4311. (defvar org-struct-menu)
  4312. (defvar org-org-menu)
  4313. (defvar org-tbl-menu)
  4314. ;;;; Define the Org-mode
  4315. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4316. (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"))
  4317. ;; We use a before-change function to check if a table might need
  4318. ;; an update.
  4319. (defvar org-table-may-need-update t
  4320. "Indicates that a table might need an update.
  4321. This variable is set by `org-before-change-function'.
  4322. `org-table-align' sets it back to nil.")
  4323. (defun org-before-change-function (beg end)
  4324. "Every change indicates that a table might need an update."
  4325. (setq org-table-may-need-update t))
  4326. (defvar org-mode-map)
  4327. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4328. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4329. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4330. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4331. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4332. (defvar org-table-buffer-is-an nil)
  4333. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4334. ;; some places -- e.g. see the macro org-with-limited-levels.
  4335. ;;
  4336. ;; In Org buffers, the value of `outline-regexp' is that of
  4337. ;; `org-outline-regexp'. The only function still directly relying on
  4338. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4339. ;; job when `orgstruct-mode' is active.
  4340. (defvar org-outline-regexp "\\*+ "
  4341. "Regexp to match Org headlines.")
  4342. (defconst org-outline-regexp-bol "^\\*+ "
  4343. "Regexp to match Org headlines.
  4344. This is similar to `org-outline-regexp' but additionally makes
  4345. sure that we are at the beginning of the line.")
  4346. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4347. "Matches an headline, putting stars and text into groups.
  4348. Stars are put in group 1 and the trimmed body in group 2.")
  4349. ;;;###autoload
  4350. (define-derived-mode org-mode outline-mode "Org"
  4351. "Outline-based notes management and organizer, alias
  4352. \"Carsten's outline-mode for keeping track of everything.\"
  4353. Org-mode develops organizational tasks around a NOTES file which
  4354. contains information about projects as plain text. Org-mode is
  4355. implemented on top of outline-mode, which is ideal to keep the content
  4356. of large files well structured. It supports ToDo items, deadlines and
  4357. time stamps, which magically appear in the diary listing of the Emacs
  4358. calendar. Tables are easily created with a built-in table editor.
  4359. Plain text URL-like links connect to websites, emails (VM), Usenet
  4360. messages (Gnus), BBDB entries, and any files related to the project.
  4361. For printing and sharing of notes, an Org-mode file (or a part of it)
  4362. can be exported as a structured ASCII or HTML file.
  4363. The following commands are available:
  4364. \\{org-mode-map}"
  4365. ;; Get rid of Outline menus, they are not needed
  4366. ;; Need to do this here because define-derived-mode sets up
  4367. ;; the keymap so late. Still, it is a waste to call this each time
  4368. ;; we switch another buffer into org-mode.
  4369. (if (featurep 'xemacs)
  4370. (when (boundp 'outline-mode-menu-heading)
  4371. ;; Assume this is Greg's port, it uses easymenu
  4372. (easy-menu-remove outline-mode-menu-heading)
  4373. (easy-menu-remove outline-mode-menu-show)
  4374. (easy-menu-remove outline-mode-menu-hide))
  4375. (define-key org-mode-map [menu-bar headings] 'undefined)
  4376. (define-key org-mode-map [menu-bar hide] 'undefined)
  4377. (define-key org-mode-map [menu-bar show] 'undefined))
  4378. (org-load-modules-maybe)
  4379. (easy-menu-add org-org-menu)
  4380. (easy-menu-add org-tbl-menu)
  4381. (org-install-agenda-files-menu)
  4382. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4383. (add-to-invisibility-spec '(org-cwidth))
  4384. (add-to-invisibility-spec '(org-hide-block . t))
  4385. (when (featurep 'xemacs)
  4386. (org-set-local 'line-move-ignore-invisible t))
  4387. (org-set-local 'outline-regexp org-outline-regexp)
  4388. (org-set-local 'outline-level 'org-outline-level)
  4389. (setq bidi-paragraph-direction 'left-to-right)
  4390. (when (and org-ellipsis
  4391. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4392. (fboundp 'make-glyph-code))
  4393. (unless org-display-table
  4394. (setq org-display-table (make-display-table)))
  4395. (set-display-table-slot
  4396. org-display-table 4
  4397. (vconcat (mapcar
  4398. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4399. org-ellipsis)))
  4400. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4401. (setq buffer-display-table org-display-table))
  4402. (org-set-regexps-and-options)
  4403. (when (and org-tag-faces (not org-tags-special-faces-re))
  4404. ;; tag faces set outside customize.... force initialization.
  4405. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4406. ;; Calc embedded
  4407. (org-set-local 'calc-embedded-open-mode "# ")
  4408. (modify-syntax-entry ?@ "w")
  4409. (if org-startup-truncated (setq truncate-lines t))
  4410. (org-set-local 'font-lock-unfontify-region-function
  4411. 'org-unfontify-region)
  4412. ;; Activate before-change-function
  4413. (org-set-local 'org-table-may-need-update t)
  4414. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4415. 'local)
  4416. ;; Check for running clock before killing a buffer
  4417. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4418. ;; Paragraphs and auto-filling
  4419. (org-set-autofill-regexps)
  4420. (setq indent-line-function 'org-indent-line-function)
  4421. (org-update-radio-target-regexp)
  4422. ;; Beginning/end of defun
  4423. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4424. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4425. ;; Next error for sparse trees
  4426. (org-set-local 'next-error-function 'org-occur-next-match)
  4427. ;; Make sure dependence stuff works reliably, even for users who set it
  4428. ;; too late :-(
  4429. (if org-enforce-todo-dependencies
  4430. (add-hook 'org-blocker-hook
  4431. 'org-block-todo-from-children-or-siblings-or-parent)
  4432. (remove-hook 'org-blocker-hook
  4433. 'org-block-todo-from-children-or-siblings-or-parent))
  4434. (if org-enforce-todo-checkbox-dependencies
  4435. (add-hook 'org-blocker-hook
  4436. 'org-block-todo-from-checkboxes)
  4437. (remove-hook 'org-blocker-hook
  4438. 'org-block-todo-from-checkboxes))
  4439. ;; Comment characters
  4440. (org-set-local 'comment-start "#")
  4441. (org-set-local 'comment-padding " ")
  4442. ;; Align options lines
  4443. (org-set-local
  4444. 'align-mode-rules-list
  4445. '((org-in-buffer-settings
  4446. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4447. (modes . '(org-mode)))))
  4448. ;; Imenu
  4449. (org-set-local 'imenu-create-index-function
  4450. 'org-imenu-get-tree)
  4451. ;; Make isearch reveal context
  4452. (if (or (featurep 'xemacs)
  4453. (not (boundp 'outline-isearch-open-invisible-function)))
  4454. ;; Emacs 21 and XEmacs make use of the hook
  4455. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4456. ;; Emacs 22 deals with this through a special variable
  4457. (org-set-local 'outline-isearch-open-invisible-function
  4458. (lambda (&rest ignore) (org-show-context 'isearch))))
  4459. ;; Turn on org-beamer-mode?
  4460. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4461. ;; Setup the pcomplete hooks
  4462. (set (make-local-variable 'pcomplete-command-completion-function)
  4463. 'org-pcomplete-initial)
  4464. (set (make-local-variable 'pcomplete-command-name-function)
  4465. 'org-command-at-point)
  4466. (set (make-local-variable 'pcomplete-default-completion-function)
  4467. 'ignore)
  4468. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4469. 'org-parse-arguments)
  4470. (set (make-local-variable 'pcomplete-termination-string) "")
  4471. (set (make-local-variable 'face-remapping-alist)
  4472. '((default org-default)))
  4473. ;; If empty file that did not turn on org-mode automatically, make it to.
  4474. (if (and org-insert-mode-line-in-empty-file
  4475. (org-called-interactively-p 'any)
  4476. (= (point-min) (point-max)))
  4477. (insert "# -*- mode: org -*-\n\n"))
  4478. (unless org-inhibit-startup
  4479. (when org-startup-align-all-tables
  4480. (let ((bmp (buffer-modified-p)))
  4481. (org-table-map-tables 'org-table-align 'quietly)
  4482. (set-buffer-modified-p bmp)))
  4483. (when org-startup-with-inline-images
  4484. (org-display-inline-images))
  4485. (when org-startup-indented
  4486. (require 'org-indent)
  4487. (org-indent-mode 1))
  4488. (unless org-inhibit-startup-visibility-stuff
  4489. (org-set-startup-visibility))))
  4490. (when (fboundp 'abbrev-table-put)
  4491. (abbrev-table-put org-mode-abbrev-table
  4492. :parents (list text-mode-abbrev-table)))
  4493. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4494. (defun org-current-time ()
  4495. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4496. (if (> (car org-time-stamp-rounding-minutes) 1)
  4497. (let ((r (car org-time-stamp-rounding-minutes))
  4498. (time (decode-time)))
  4499. (apply 'encode-time
  4500. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4501. (nthcdr 2 time))))
  4502. (current-time)))
  4503. (defun org-today ()
  4504. "Return today date, considering `org-extend-today-until'."
  4505. (time-to-days
  4506. (time-subtract (current-time)
  4507. (list 0 (* 3600 org-extend-today-until) 0))))
  4508. ;;;; Font-Lock stuff, including the activators
  4509. (defvar org-mouse-map (make-sparse-keymap))
  4510. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4511. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4512. (when org-mouse-1-follows-link
  4513. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4514. (when org-tab-follows-link
  4515. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4516. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4517. (require 'font-lock)
  4518. (defconst org-non-link-chars "]\t\n\r<>")
  4519. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4520. "shell" "elisp" "doi" "message"))
  4521. (defvar org-link-types-re nil
  4522. "Matches a link that has a url-like prefix like \"http:\"")
  4523. (defvar org-link-re-with-space nil
  4524. "Matches a link with spaces, optional angular brackets around it.")
  4525. (defvar org-link-re-with-space2 nil
  4526. "Matches a link with spaces, optional angular brackets around it.")
  4527. (defvar org-link-re-with-space3 nil
  4528. "Matches a link with spaces, only for internal part in bracket links.")
  4529. (defvar org-angle-link-re nil
  4530. "Matches link with angular brackets, spaces are allowed.")
  4531. (defvar org-plain-link-re nil
  4532. "Matches plain link, without spaces.")
  4533. (defvar org-bracket-link-regexp nil
  4534. "Matches a link in double brackets.")
  4535. (defvar org-bracket-link-analytic-regexp nil
  4536. "Regular expression used to analyze links.
  4537. Here is what the match groups contain after a match:
  4538. 1: http:
  4539. 2: http
  4540. 3: path
  4541. 4: [desc]
  4542. 5: desc")
  4543. (defvar org-bracket-link-analytic-regexp++ nil
  4544. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4545. (defvar org-any-link-re nil
  4546. "Regular expression matching any link.")
  4547. (defcustom org-match-sexp-depth 3
  4548. "Number of stacked braces for sub/superscript matching.
  4549. This has to be set before loading org.el to be effective."
  4550. :group 'org-export-translation ; ??????????????????????????/
  4551. :type 'integer)
  4552. (defun org-create-multibrace-regexp (left right n)
  4553. "Create a regular expression which will match a balanced sexp.
  4554. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4555. as single character strings.
  4556. The regexp returned will match the entire expression including the
  4557. delimiters. It will also define a single group which contains the
  4558. match except for the outermost delimiters. The maximum depth of
  4559. stacked delimiters is N. Escaping delimiters is not possible."
  4560. (let* ((nothing (concat "[^" left right "]*?"))
  4561. (or "\\|")
  4562. (re nothing)
  4563. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4564. (while (> n 1)
  4565. (setq n (1- n)
  4566. re (concat re or next)
  4567. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4568. (concat left "\\(" re "\\)" right)))
  4569. (defvar org-match-substring-regexp
  4570. (concat
  4571. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4572. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4573. "\\|"
  4574. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4575. "\\|"
  4576. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4577. "The regular expression matching a sub- or superscript.")
  4578. (defvar org-match-substring-with-braces-regexp
  4579. (concat
  4580. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4581. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4582. "\\)")
  4583. "The regular expression matching a sub- or superscript, forcing braces.")
  4584. (defun org-make-link-regexps ()
  4585. "Update the link regular expressions.
  4586. This should be called after the variable `org-link-types' has changed."
  4587. (setq org-link-types-re
  4588. (concat
  4589. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4590. org-link-re-with-space
  4591. (concat
  4592. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4593. "\\([^" org-non-link-chars " ]"
  4594. "[^" org-non-link-chars "]*"
  4595. "[^" org-non-link-chars " ]\\)>?")
  4596. org-link-re-with-space2
  4597. (concat
  4598. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4599. "\\([^" org-non-link-chars " ]"
  4600. "[^\t\n\r]*"
  4601. "[^" org-non-link-chars " ]\\)>?")
  4602. org-link-re-with-space3
  4603. (concat
  4604. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4605. "\\([^" org-non-link-chars " ]"
  4606. "[^\t\n\r]*\\)")
  4607. org-angle-link-re
  4608. (concat
  4609. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4610. "\\([^" org-non-link-chars " ]"
  4611. "[^" org-non-link-chars "]*"
  4612. "\\)>")
  4613. org-plain-link-re
  4614. (concat
  4615. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4616. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4617. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4618. org-bracket-link-regexp
  4619. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4620. org-bracket-link-analytic-regexp
  4621. (concat
  4622. "\\[\\["
  4623. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4624. "\\([^]]+\\)"
  4625. "\\]"
  4626. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4627. "\\]")
  4628. org-bracket-link-analytic-regexp++
  4629. (concat
  4630. "\\[\\["
  4631. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4632. "\\([^]]+\\)"
  4633. "\\]"
  4634. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4635. "\\]")
  4636. org-any-link-re
  4637. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4638. org-angle-link-re "\\)\\|\\("
  4639. org-plain-link-re "\\)")))
  4640. (org-make-link-regexps)
  4641. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4642. "Regular expression for fast time stamp matching.")
  4643. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4644. "Regular expression for fast time stamp matching.")
  4645. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4646. "Regular expression matching time strings for analysis.
  4647. This one does not require the space after the date, so it can be used
  4648. on a string that terminates immediately after the date.")
  4649. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4650. "Regular expression matching time strings for analysis.")
  4651. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4652. "Regular expression matching time stamps, with groups.")
  4653. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4654. "Regular expression matching time stamps (also [..]), with groups.")
  4655. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4656. "Regular expression matching a time stamp range.")
  4657. (defconst org-tr-regexp-both
  4658. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4659. "Regular expression matching a time stamp range.")
  4660. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4661. org-ts-regexp "\\)?")
  4662. "Regular expression matching a time stamp or time stamp range.")
  4663. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4664. org-ts-regexp-both "\\)?")
  4665. "Regular expression matching a time stamp or time stamp range.
  4666. The time stamps may be either active or inactive.")
  4667. (defvar org-emph-face nil)
  4668. (defun org-do-emphasis-faces (limit)
  4669. "Run through the buffer and add overlays to emphasized strings."
  4670. (let (rtn a)
  4671. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4672. (if (not (= (char-after (match-beginning 3))
  4673. (char-after (match-beginning 4))))
  4674. (progn
  4675. (setq rtn t)
  4676. (setq a (assoc (match-string 3) org-emphasis-alist))
  4677. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4678. 'face
  4679. (nth 1 a))
  4680. (and (nth 4 a)
  4681. (org-remove-flyspell-overlays-in
  4682. (match-beginning 0) (match-end 0)))
  4683. (add-text-properties (match-beginning 2) (match-end 2)
  4684. '(font-lock-multiline t org-emphasis t))
  4685. (when org-hide-emphasis-markers
  4686. (add-text-properties (match-end 4) (match-beginning 5)
  4687. '(invisible org-link))
  4688. (add-text-properties (match-beginning 3) (match-end 3)
  4689. '(invisible org-link)))))
  4690. (backward-char 1))
  4691. rtn))
  4692. (defun org-emphasize (&optional char)
  4693. "Insert or change an emphasis, i.e. a font like bold or italic.
  4694. If there is an active region, change that region to a new emphasis.
  4695. If there is no region, just insert the marker characters and position
  4696. the cursor between them.
  4697. CHAR should be either the marker character, or the first character of the
  4698. HTML tag associated with that emphasis. If CHAR is a space, the means
  4699. to remove the emphasis of the selected region.
  4700. If char is not given (for example in an interactive call) it
  4701. will be prompted for."
  4702. (interactive)
  4703. (let ((eal org-emphasis-alist) e det
  4704. (erc org-emphasis-regexp-components)
  4705. (prompt "")
  4706. (string "") beg end move tag c s)
  4707. (if (org-region-active-p)
  4708. (setq beg (region-beginning) end (region-end)
  4709. string (buffer-substring beg end))
  4710. (setq move t))
  4711. (while (setq e (pop eal))
  4712. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4713. c (aref tag 0))
  4714. (push (cons c (string-to-char (car e))) det)
  4715. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4716. (substring tag 1)))))
  4717. (setq det (nreverse det))
  4718. (unless char
  4719. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4720. (setq char (read-char-exclusive)))
  4721. (setq char (or (cdr (assoc char det)) char))
  4722. (if (equal char ?\ )
  4723. (setq s "" move nil)
  4724. (unless (assoc (char-to-string char) org-emphasis-alist)
  4725. (error "No such emphasis marker: \"%c\"" char))
  4726. (setq s (char-to-string char)))
  4727. (while (and (> (length string) 1)
  4728. (equal (substring string 0 1) (substring string -1))
  4729. (assoc (substring string 0 1) org-emphasis-alist))
  4730. (setq string (substring string 1 -1)))
  4731. (setq string (concat s string s))
  4732. (if beg (delete-region beg end))
  4733. (unless (or (bolp)
  4734. (string-match (concat "[" (nth 0 erc) "\n]")
  4735. (char-to-string (char-before (point)))))
  4736. (insert " "))
  4737. (unless (or (eobp)
  4738. (string-match (concat "[" (nth 1 erc) "\n]")
  4739. (char-to-string (char-after (point)))))
  4740. (insert " ") (backward-char 1))
  4741. (insert string)
  4742. (and move (backward-char 1))))
  4743. (defconst org-nonsticky-props
  4744. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4745. (defsubst org-rear-nonsticky-at (pos)
  4746. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4747. (defun org-activate-plain-links (limit)
  4748. "Run through the buffer and add overlays to links."
  4749. (catch 'exit
  4750. (let (f)
  4751. (if (re-search-forward org-plain-link-re limit t)
  4752. (progn
  4753. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4754. (setq f (get-text-property (match-beginning 0) 'face))
  4755. (if (or (eq f 'org-tag)
  4756. (and (listp f) (memq 'org-tag f)))
  4757. nil
  4758. (add-text-properties (match-beginning 0) (match-end 0)
  4759. (list 'mouse-face 'highlight
  4760. 'face 'org-link
  4761. 'keymap org-mouse-map))
  4762. (org-rear-nonsticky-at (match-end 0)))
  4763. t)))))
  4764. (defun org-activate-code (limit)
  4765. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4766. (progn
  4767. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4768. (remove-text-properties (match-beginning 0) (match-end 0)
  4769. '(display t invisible t intangible t))
  4770. t)))
  4771. (defcustom org-src-fontify-natively nil
  4772. "When non-nil, fontify code in code blocks."
  4773. :type 'boolean
  4774. :version "24.1"
  4775. :group 'org-appearance
  4776. :group 'org-babel)
  4777. (defun org-fontify-meta-lines-and-blocks (limit)
  4778. (condition-case nil
  4779. (org-fontify-meta-lines-and-blocks-1 limit)
  4780. (error (message "org-mode fontification error"))))
  4781. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4782. "Fontify #+ lines and blocks, in the correct ways."
  4783. (let ((case-fold-search t))
  4784. (if (re-search-forward
  4785. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4786. limit t)
  4787. (let ((beg (match-beginning 0))
  4788. (block-start (match-end 0))
  4789. (block-end nil)
  4790. (lang (match-string 7))
  4791. (beg1 (line-beginning-position 2))
  4792. (dc1 (downcase (match-string 2)))
  4793. (dc3 (downcase (match-string 3)))
  4794. end end1 quoting block-type ovl)
  4795. (cond
  4796. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4797. ;; a single line of backend-specific content
  4798. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4799. (remove-text-properties (match-beginning 0) (match-end 0)
  4800. '(display t invisible t intangible t))
  4801. (add-text-properties (match-beginning 1) (match-end 3)
  4802. '(font-lock-fontified t face org-meta-line))
  4803. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4804. '(font-lock-fontified t face org-block))
  4805. ; for backend-specific code
  4806. t)
  4807. ((and (match-end 4) (equal dc3 "begin"))
  4808. ;; Truly a block
  4809. (setq block-type (downcase (match-string 5))
  4810. quoting (member block-type org-protecting-blocks))
  4811. (when (re-search-forward
  4812. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4813. nil t) ;; on purpose, we look further than LIMIT
  4814. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4815. (setq block-end (match-beginning 0))
  4816. (when quoting
  4817. (remove-text-properties beg end
  4818. '(display t invisible t intangible t)))
  4819. (add-text-properties
  4820. beg end
  4821. '(font-lock-fontified t font-lock-multiline t))
  4822. (add-text-properties beg beg1 '(face org-meta-line))
  4823. (add-text-properties end1 (min (point-max) (1+ end))
  4824. '(face org-meta-line)) ; for end_src
  4825. (cond
  4826. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4827. (org-src-font-lock-fontify-block lang block-start block-end)
  4828. ;; remove old background overlays
  4829. (mapc (lambda (ov)
  4830. (if (eq (overlay-get ov 'face) 'org-block-background)
  4831. (delete-overlay ov)))
  4832. (overlays-at (/ (+ beg1 block-end) 2)))
  4833. ;; add a background overlay
  4834. (setq ovl (make-overlay beg1 block-end))
  4835. (overlay-put ovl 'face 'org-block-background)
  4836. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4837. (quoting
  4838. (add-text-properties beg1 (min (point-max) (1+ end1))
  4839. '(face org-block))) ; end of source block
  4840. ((not org-fontify-quote-and-verse-blocks))
  4841. ((string= block-type "quote")
  4842. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4843. ((string= block-type "verse")
  4844. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4845. (add-text-properties beg beg1 '(face org-block-begin-line))
  4846. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4847. t))
  4848. ((member dc1 '("title:" "author:" "email:" "date:"))
  4849. (add-text-properties
  4850. beg (match-end 3)
  4851. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4852. '(font-lock-fontified t invisible t)
  4853. '(font-lock-fontified t face org-document-info-keyword)))
  4854. (add-text-properties
  4855. (match-beginning 6) (match-end 6)
  4856. (if (string-equal dc1 "title:")
  4857. '(font-lock-fontified t face org-document-title)
  4858. '(font-lock-fontified t face org-document-info))))
  4859. ((not (member (char-after beg) '(?\ ?\t)))
  4860. ;; just any other in-buffer setting, but not indented
  4861. (add-text-properties
  4862. beg (1+ (match-end 0))
  4863. '(font-lock-fontified t face org-meta-line))
  4864. t)
  4865. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4866. "orgtbl:" "tblfm:" "tblname:" "results:"
  4867. "call:" "header:" "headers:" "name:"))
  4868. (and (match-end 4) (equal dc3 "attr")))
  4869. (add-text-properties
  4870. beg (match-end 0)
  4871. '(font-lock-fontified t face org-meta-line))
  4872. t)
  4873. ((member dc3 '(" " ""))
  4874. (add-text-properties
  4875. beg (match-end 0)
  4876. '(font-lock-fontified t face font-lock-comment-face)))
  4877. (t nil))))))
  4878. (defun org-strip-protective-commas (beg end)
  4879. "Strip protective commas between BEG and END in the current buffer."
  4880. (interactive "r")
  4881. (save-excursion
  4882. (save-match-data
  4883. (goto-char beg)
  4884. (let ((front-line (save-excursion
  4885. (re-search-forward
  4886. "[^[:space:]]" end t)
  4887. (goto-char (match-beginning 0))
  4888. (current-column))))
  4889. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4890. (goto-char (match-beginning 1))
  4891. (when (= (current-column) front-line)
  4892. (replace-match "" nil nil nil 1)))))))
  4893. (defun org-activate-angle-links (limit)
  4894. "Run through the buffer and add overlays to links."
  4895. (if (re-search-forward org-angle-link-re limit t)
  4896. (progn
  4897. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4898. (add-text-properties (match-beginning 0) (match-end 0)
  4899. (list 'mouse-face 'highlight
  4900. 'keymap org-mouse-map))
  4901. (org-rear-nonsticky-at (match-end 0))
  4902. t)))
  4903. (defun org-activate-footnote-links (limit)
  4904. "Run through the buffer and add overlays to footnotes."
  4905. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4906. (when fn
  4907. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4908. (org-remove-flyspell-overlays-in beg end)
  4909. (add-text-properties beg end
  4910. (list 'mouse-face 'highlight
  4911. 'keymap org-mouse-map
  4912. 'help-echo
  4913. (if (= (point-at-bol) beg)
  4914. "Footnote definition"
  4915. "Footnote reference")
  4916. 'font-lock-fontified t
  4917. 'font-lock-multiline t
  4918. 'face 'org-footnote))))))
  4919. (defun org-activate-bracket-links (limit)
  4920. "Run through the buffer and add overlays to bracketed links."
  4921. (if (re-search-forward org-bracket-link-regexp limit t)
  4922. (let* ((help (concat "LINK: "
  4923. (org-match-string-no-properties 1)))
  4924. ;; FIXME: above we should remove the escapes.
  4925. ;; but that requires another match, protecting match data,
  4926. ;; a lot of overhead for font-lock.
  4927. (ip (org-maybe-intangible
  4928. (list 'invisible 'org-link
  4929. 'keymap org-mouse-map 'mouse-face 'highlight
  4930. 'font-lock-multiline t 'help-echo help)))
  4931. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4932. 'font-lock-multiline t 'help-echo help)))
  4933. ;; We need to remove the invisible property here. Table narrowing
  4934. ;; may have made some of this invisible.
  4935. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4936. (remove-text-properties (match-beginning 0) (match-end 0)
  4937. '(invisible nil))
  4938. (if (match-end 3)
  4939. (progn
  4940. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4941. (org-rear-nonsticky-at (match-beginning 3))
  4942. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4943. (org-rear-nonsticky-at (match-end 3))
  4944. (add-text-properties (match-end 3) (match-end 0) ip)
  4945. (org-rear-nonsticky-at (match-end 0)))
  4946. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4947. (org-rear-nonsticky-at (match-beginning 1))
  4948. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4949. (org-rear-nonsticky-at (match-end 1))
  4950. (add-text-properties (match-end 1) (match-end 0) ip)
  4951. (org-rear-nonsticky-at (match-end 0)))
  4952. t)))
  4953. (defun org-activate-dates (limit)
  4954. "Run through the buffer and add overlays to dates."
  4955. (if (re-search-forward org-tsr-regexp-both limit t)
  4956. (progn
  4957. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4958. (add-text-properties (match-beginning 0) (match-end 0)
  4959. (list 'mouse-face 'highlight
  4960. 'keymap org-mouse-map))
  4961. (org-rear-nonsticky-at (match-end 0))
  4962. (when org-display-custom-times
  4963. (if (match-end 3)
  4964. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4965. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4966. t)))
  4967. (defvar org-target-link-regexp nil
  4968. "Regular expression matching radio targets in plain text.")
  4969. (make-variable-buffer-local 'org-target-link-regexp)
  4970. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4971. "Regular expression matching a link target.")
  4972. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4973. "Regular expression matching a radio target.")
  4974. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4975. "Regular expression matching any target.")
  4976. (defun org-activate-target-links (limit)
  4977. "Run through the buffer and add overlays to target matches."
  4978. (when org-target-link-regexp
  4979. (let ((case-fold-search t))
  4980. (if (re-search-forward org-target-link-regexp limit t)
  4981. (progn
  4982. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4983. (add-text-properties (match-beginning 0) (match-end 0)
  4984. (list 'mouse-face 'highlight
  4985. 'keymap org-mouse-map
  4986. 'help-echo "Radio target link"
  4987. 'org-linked-text t))
  4988. (org-rear-nonsticky-at (match-end 0))
  4989. t)))))
  4990. (defun org-update-radio-target-regexp ()
  4991. "Find all radio targets in this file and update the regular expression."
  4992. (interactive)
  4993. (when (memq 'radio org-activate-links)
  4994. (setq org-target-link-regexp
  4995. (org-make-target-link-regexp (org-all-targets 'radio)))
  4996. (org-restart-font-lock)))
  4997. (defun org-hide-wide-columns (limit)
  4998. (let (s e)
  4999. (setq s (text-property-any (point) (or limit (point-max))
  5000. 'org-cwidth t))
  5001. (when s
  5002. (setq e (next-single-property-change s 'org-cwidth))
  5003. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5004. (goto-char e)
  5005. t)))
  5006. (defvar org-latex-and-specials-regexp nil
  5007. "Regular expression for highlighting export special stuff.")
  5008. (defvar org-match-substring-regexp)
  5009. (defvar org-match-substring-with-braces-regexp)
  5010. ;; This should be with the exporter code, but we also use if for font-locking
  5011. (defconst org-export-html-special-string-regexps
  5012. '(("\\\\-" . "&shy;")
  5013. ("---\\([^-]\\)" . "&mdash;\\1")
  5014. ("--\\([^-]\\)" . "&ndash;\\1")
  5015. ("\\.\\.\\." . "&hellip;"))
  5016. "Regular expressions for special string conversion.")
  5017. (defun org-compute-latex-and-specials-regexp ()
  5018. "Compute regular expression for stuff treated specially by exporters."
  5019. (if (not org-highlight-latex-fragments-and-specials)
  5020. (org-set-local 'org-latex-and-specials-regexp nil)
  5021. (require 'org-exp)
  5022. (let*
  5023. ((matchers (plist-get org-format-latex-options :matchers))
  5024. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5025. org-latex-regexps)))
  5026. (org-export-allow-BIND nil)
  5027. (options (org-combine-plists (org-default-export-plist)
  5028. (org-infile-export-plist)))
  5029. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5030. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5031. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5032. (org-export-html-expand (plist-get options :expand-quoted-html))
  5033. (org-export-with-special-strings (plist-get options :special-strings))
  5034. (re-sub
  5035. (cond
  5036. ((equal org-export-with-sub-superscripts '{})
  5037. (list org-match-substring-with-braces-regexp))
  5038. (org-export-with-sub-superscripts
  5039. (list org-match-substring-regexp))
  5040. (t nil)))
  5041. (re-latex
  5042. (if org-export-with-LaTeX-fragments
  5043. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5044. (re-macros
  5045. (if org-export-with-TeX-macros
  5046. (list (concat "\\\\"
  5047. (regexp-opt
  5048. (append
  5049. (delq nil
  5050. (mapcar 'car-safe
  5051. (append org-entities-user
  5052. org-entities)))
  5053. (if (boundp 'org-latex-entities)
  5054. (mapcar (lambda (x)
  5055. (or (car-safe x) x))
  5056. org-latex-entities)
  5057. nil))
  5058. 'words))) ; FIXME
  5059. ))
  5060. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5061. (re-special (if org-export-with-special-strings
  5062. (mapcar (lambda (x) (car x))
  5063. org-export-html-special-string-regexps)))
  5064. (re-rest
  5065. (delq nil
  5066. (list
  5067. (if org-export-html-expand "@<[^>\n]+>")
  5068. ))))
  5069. (org-set-local
  5070. 'org-latex-and-specials-regexp
  5071. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5072. re-rest) "\\|")))))
  5073. (defun org-do-latex-and-special-faces (limit)
  5074. "Run through the buffer and add overlays to links."
  5075. (when org-latex-and-specials-regexp
  5076. (let (rtn d)
  5077. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5078. limit t))
  5079. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5080. 'face))
  5081. '(org-code org-verbatim underline)))
  5082. (progn
  5083. (setq rtn t
  5084. d (cond ((member (char-after (1+ (match-beginning 0)))
  5085. '(?_ ?^)) 1)
  5086. (t 0)))
  5087. (font-lock-prepend-text-property
  5088. (+ d (match-beginning 0)) (match-end 0)
  5089. 'face 'org-latex-and-export-specials)
  5090. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5091. '(font-lock-multiline t)))))
  5092. rtn)))
  5093. (defun org-restart-font-lock ()
  5094. "Restart `font-lock-mode', to force refontification."
  5095. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5096. (font-lock-mode -1)
  5097. (font-lock-mode 1)))
  5098. (defun org-all-targets (&optional radio)
  5099. "Return a list of all targets in this file.
  5100. With optional argument RADIO, only find radio targets."
  5101. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5102. rtn)
  5103. (save-excursion
  5104. (goto-char (point-min))
  5105. (while (re-search-forward re nil t)
  5106. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5107. rtn)))
  5108. (defun org-make-target-link-regexp (targets)
  5109. "Make regular expression matching all strings in TARGETS.
  5110. The regular expression finds the targets also if there is a line break
  5111. between words."
  5112. (and targets
  5113. (concat
  5114. "\\<\\("
  5115. (mapconcat
  5116. (lambda (x)
  5117. (setq x (regexp-quote x))
  5118. (while (string-match " +" x)
  5119. (setq x (replace-match "\\s-+" t t x)))
  5120. x)
  5121. targets
  5122. "\\|")
  5123. "\\)\\>")))
  5124. (defun org-activate-tags (limit)
  5125. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5126. (progn
  5127. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5128. (add-text-properties (match-beginning 1) (match-end 1)
  5129. (list 'mouse-face 'highlight
  5130. 'keymap org-mouse-map))
  5131. (org-rear-nonsticky-at (match-end 1))
  5132. t)))
  5133. (defun org-outline-level ()
  5134. "Compute the outline level of the heading at point.
  5135. This function assumes that the cursor is at the beginning of a line matched
  5136. by `outline-regexp'. Otherwise it returns garbage.
  5137. If this is called at a normal headline, the level is the number of stars.
  5138. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5139. (save-excursion
  5140. (looking-at org-outline-regexp)
  5141. (1- (- (match-end 0) (match-beginning 0)))))
  5142. (defvar org-font-lock-keywords nil)
  5143. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5144. "Regular expression matching a property line.")
  5145. (defvar org-font-lock-hook nil
  5146. "Functions to be called for special font lock stuff.")
  5147. (defvar org-font-lock-set-keywords-hook nil
  5148. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5149. This is called after `org-font-lock-extra-keywords' is defined, but before
  5150. it is installed to be used by font lock. This can be useful if something
  5151. needs to be inserted at a specific position in the font-lock sequence.")
  5152. (defun org-font-lock-hook (limit)
  5153. (run-hook-with-args 'org-font-lock-hook limit))
  5154. (defun org-set-font-lock-defaults ()
  5155. (let* ((em org-fontify-emphasized-text)
  5156. (lk org-activate-links)
  5157. (org-font-lock-extra-keywords
  5158. (list
  5159. ;; Call the hook
  5160. '(org-font-lock-hook)
  5161. ;; Headlines
  5162. `(,(if org-fontify-whole-heading-line
  5163. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5164. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5165. (1 (org-get-level-face 1))
  5166. (2 (org-get-level-face 2))
  5167. (3 (org-get-level-face 3)))
  5168. ;; Table lines
  5169. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5170. (1 'org-table t))
  5171. ;; Table internals
  5172. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5173. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5174. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5175. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5176. ;; Drawers
  5177. (list org-drawer-regexp '(0 'org-special-keyword t))
  5178. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5179. ;; Properties
  5180. (list org-property-re
  5181. '(1 'org-special-keyword t)
  5182. '(3 'org-property-value t))
  5183. ;; Links
  5184. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5185. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5186. (if (memq 'plain lk) '(org-activate-plain-links))
  5187. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5188. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5189. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5190. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5191. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5192. '(org-hide-wide-columns (0 nil append))
  5193. ;; TODO keyword
  5194. (list (format org-heading-keyword-regexp-format
  5195. org-todo-regexp)
  5196. '(2 (org-get-todo-face 2) t))
  5197. ;; DONE
  5198. (if org-fontify-done-headline
  5199. (list (format org-heading-keyword-regexp-format
  5200. (concat
  5201. "\\(?:"
  5202. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5203. "\\)"))
  5204. '(2 'org-headline-done t))
  5205. nil)
  5206. ;; Priorities
  5207. '(org-font-lock-add-priority-faces)
  5208. ;; Tags
  5209. '(org-font-lock-add-tag-faces)
  5210. ;; Special keywords
  5211. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5212. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5213. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5214. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5215. ;; Emphasis
  5216. (if em
  5217. (if (featurep 'xemacs)
  5218. '(org-do-emphasis-faces (0 nil append))
  5219. '(org-do-emphasis-faces)))
  5220. ;; Checkboxes
  5221. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5222. 1 'org-checkbox prepend)
  5223. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5224. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5225. (0 (org-get-checkbox-statistics-face) t)))
  5226. ;; Description list items
  5227. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5228. 1 'bold prepend)
  5229. ;; ARCHIVEd headings
  5230. (list (concat
  5231. org-outline-regexp-bol
  5232. "\\(.*:" org-archive-tag ":.*\\)")
  5233. '(1 'org-archived prepend))
  5234. ;; Specials
  5235. '(org-do-latex-and-special-faces)
  5236. '(org-fontify-entities)
  5237. '(org-raise-scripts)
  5238. ;; Code
  5239. '(org-activate-code (1 'org-code t))
  5240. ;; COMMENT
  5241. (list (format org-heading-keyword-regexp-format
  5242. (concat "\\("
  5243. org-comment-string "\\|" org-quote-string
  5244. "\\)"))
  5245. '(2 'org-special-keyword t))
  5246. '("^#.*" (0 'font-lock-comment-face t))
  5247. ;; Blocks and meta lines
  5248. '(org-fontify-meta-lines-and-blocks)
  5249. )))
  5250. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5251. (run-hooks 'org-font-lock-set-keywords-hook)
  5252. ;; Now set the full font-lock-keywords
  5253. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5254. (org-set-local 'font-lock-defaults
  5255. '(org-font-lock-keywords t nil nil backward-paragraph))
  5256. (kill-local-variable 'font-lock-keywords) nil))
  5257. (defun org-toggle-pretty-entities ()
  5258. "Toggle the composition display of entities as UTF8 characters."
  5259. (interactive)
  5260. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5261. (org-restart-font-lock)
  5262. (if org-pretty-entities
  5263. (message "Entities are displayed as UTF8 characters")
  5264. (save-restriction
  5265. (widen)
  5266. (org-decompose-region (point-min) (point-max))
  5267. (message "Entities are displayed plain"))))
  5268. (defun org-fontify-entities (limit)
  5269. "Find an entity to fontify."
  5270. (let (ee)
  5271. (when org-pretty-entities
  5272. (catch 'match
  5273. (while (re-search-forward
  5274. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5275. limit t)
  5276. (if (and (not (org-in-indented-comment-line))
  5277. (setq ee (org-entity-get (match-string 1)))
  5278. (= (length (nth 6 ee)) 1))
  5279. (progn
  5280. (add-text-properties
  5281. (match-beginning 0) (match-end 1)
  5282. (list 'font-lock-fontified t))
  5283. (compose-region (match-beginning 0) (match-end 1)
  5284. (nth 6 ee) nil)
  5285. (backward-char 1)
  5286. (throw 'match t))))
  5287. nil))))
  5288. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5289. "Fontify string S like in Org-mode."
  5290. (with-temp-buffer
  5291. (insert s)
  5292. (let ((org-odd-levels-only odd-levels))
  5293. (org-mode)
  5294. (font-lock-fontify-buffer)
  5295. (buffer-string))))
  5296. (defvar org-m nil)
  5297. (defvar org-l nil)
  5298. (defvar org-f nil)
  5299. (defun org-get-level-face (n)
  5300. "Get the right face for match N in font-lock matching of headlines."
  5301. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5302. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5303. (if org-cycle-level-faces
  5304. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5305. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5306. (cond
  5307. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5308. ((eq n 2) org-f)
  5309. (t (if org-level-color-stars-only nil org-f))))
  5310. (defun org-get-todo-face (kwd)
  5311. "Get the right face for a TODO keyword KWD.
  5312. If KWD is a number, get the corresponding match group."
  5313. (if (numberp kwd) (setq kwd (match-string kwd)))
  5314. (or (org-face-from-face-or-color
  5315. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5316. (and (member kwd org-done-keywords) 'org-done)
  5317. 'org-todo))
  5318. (defun org-face-from-face-or-color (context inherit face-or-color)
  5319. "Create a face list that inherits INHERIT, but sets the foreground color.
  5320. When FACE-OR-COLOR is not a string, just return it."
  5321. (if (stringp face-or-color)
  5322. (list :inherit inherit
  5323. (cdr (assoc context org-faces-easy-properties))
  5324. face-or-color)
  5325. face-or-color))
  5326. (defun org-font-lock-add-tag-faces (limit)
  5327. "Add the special tag faces."
  5328. (when (and org-tag-faces org-tags-special-faces-re)
  5329. (while (re-search-forward org-tags-special-faces-re limit t)
  5330. (add-text-properties (match-beginning 1) (match-end 1)
  5331. (list 'face (org-get-tag-face 1)
  5332. 'font-lock-fontified t))
  5333. (backward-char 1))))
  5334. (defun org-font-lock-add-priority-faces (limit)
  5335. "Add the special priority faces."
  5336. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5337. (add-text-properties
  5338. (match-beginning 0) (match-end 0)
  5339. (list 'face (or (org-face-from-face-or-color
  5340. 'priority 'org-special-keyword
  5341. (cdr (assoc (char-after (match-beginning 1))
  5342. org-priority-faces)))
  5343. 'org-special-keyword)
  5344. 'font-lock-fontified t))))
  5345. (defun org-get-tag-face (kwd)
  5346. "Get the right face for a TODO keyword KWD.
  5347. If KWD is a number, get the corresponding match group."
  5348. (if (numberp kwd) (setq kwd (match-string kwd)))
  5349. (or (org-face-from-face-or-color
  5350. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5351. 'org-tag))
  5352. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5353. "Remove fontification and activation overlays from links."
  5354. (font-lock-default-unfontify-region beg end)
  5355. (let* ((buffer-undo-list t)
  5356. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5357. (inhibit-modification-hooks t)
  5358. deactivate-mark buffer-file-name buffer-file-truename)
  5359. (org-decompose-region beg end)
  5360. (remove-text-properties beg end
  5361. '(mouse-face t keymap t org-linked-text t
  5362. invisible t intangible t
  5363. org-no-flyspell t org-emphasis t))
  5364. (org-remove-font-lock-display-properties beg end)))
  5365. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5366. ((raise 0.3) (height 0.7))
  5367. ((raise -0.5))
  5368. ((raise 0.5)))
  5369. "Display properties for showing superscripts and subscripts.")
  5370. (defun org-remove-font-lock-display-properties (beg end)
  5371. "Remove specific display properties that have been added by font lock.
  5372. The will remove the raise properties that are used to show superscripts
  5373. and subscripts."
  5374. (let (next prop)
  5375. (while (< beg end)
  5376. (setq next (next-single-property-change beg 'display nil end)
  5377. prop (get-text-property beg 'display))
  5378. (if (member prop org-script-display)
  5379. (put-text-property beg next 'display nil))
  5380. (setq beg next))))
  5381. (defun org-raise-scripts (limit)
  5382. "Add raise properties to sub/superscripts."
  5383. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5384. (if (re-search-forward
  5385. (if (eq org-use-sub-superscripts t)
  5386. org-match-substring-regexp
  5387. org-match-substring-with-braces-regexp)
  5388. limit t)
  5389. (let* ((pos (point)) table-p comment-p
  5390. (mpos (match-beginning 3))
  5391. (emph-p (get-text-property mpos 'org-emphasis))
  5392. (link-p (get-text-property mpos 'mouse-face))
  5393. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5394. (goto-char (point-at-bol))
  5395. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5396. comment-p (org-looking-at-p "[ \t]*#"))
  5397. (goto-char pos)
  5398. ;; FIXME: Should we go back one character here, for a_b^c
  5399. ;; (goto-char (1- pos)) ;????????????????????
  5400. (if (or comment-p emph-p link-p keyw-p)
  5401. t
  5402. (put-text-property (match-beginning 3) (match-end 0)
  5403. 'display
  5404. (if (equal (char-after (match-beginning 2)) ?^)
  5405. (nth (if table-p 3 1) org-script-display)
  5406. (nth (if table-p 2 0) org-script-display)))
  5407. (add-text-properties (match-beginning 2) (match-end 2)
  5408. (list 'invisible t
  5409. 'org-dwidth t 'org-dwidth-n 1))
  5410. (if (and (eq (char-after (match-beginning 3)) ?{)
  5411. (eq (char-before (match-end 3)) ?}))
  5412. (progn
  5413. (add-text-properties
  5414. (match-beginning 3) (1+ (match-beginning 3))
  5415. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5416. (add-text-properties
  5417. (1- (match-end 3)) (match-end 3)
  5418. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5419. t)))))
  5420. ;;;; Visibility cycling, including org-goto and indirect buffer
  5421. ;;; Cycling
  5422. (defvar org-cycle-global-status nil)
  5423. (make-variable-buffer-local 'org-cycle-global-status)
  5424. (defvar org-cycle-subtree-status nil)
  5425. (make-variable-buffer-local 'org-cycle-subtree-status)
  5426. ;;;###autoload
  5427. (defvar org-inlinetask-min-level)
  5428. (defun org-cycle (&optional arg)
  5429. "TAB-action and visibility cycling for Org-mode.
  5430. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5431. is outline visibility cycling, but it also invokes other actions
  5432. in special contexts.
  5433. - When this function is called with a prefix argument, rotate the entire
  5434. buffer through 3 states (global cycling)
  5435. 1. OVERVIEW: Show only top-level headlines.
  5436. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5437. 3. SHOW ALL: Show everything.
  5438. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5439. determined by the variable `org-startup-folded', and by any VISIBILITY
  5440. properties in the buffer.
  5441. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5442. including any drawers.
  5443. - When inside a table, re-align the table and move to the next field.
  5444. - When point is at the beginning of a headline, rotate the subtree started
  5445. by this line through 3 different states (local cycling)
  5446. 1. FOLDED: Only the main headline is shown.
  5447. 2. CHILDREN: The main headline and the direct children are shown.
  5448. From this state, you can move to one of the children
  5449. and zoom in further.
  5450. 3. SUBTREE: Show the entire subtree, including body text.
  5451. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5452. - When point is at the beginning of an empty headline and the variable
  5453. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5454. of the headline by demoting and promoting it to likely levels. This
  5455. speeds up creation document structure by pressing TAB once or several
  5456. times right after creating a new headline.
  5457. - When there is a numeric prefix, go up to a heading with level ARG, do
  5458. a `show-subtree' and return to the previous cursor position. If ARG
  5459. is negative, go up that many levels.
  5460. - When point is not at the beginning of a headline, execute the global
  5461. binding for TAB, which is re-indenting the line. See the option
  5462. `org-cycle-emulate-tab' for details.
  5463. - Special case: if point is at the beginning of the buffer and there is
  5464. no headline in line 1, this function will act as if called with prefix arg
  5465. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5466. But only if also the variable `org-cycle-global-at-bob' is t."
  5467. (interactive "P")
  5468. (org-load-modules-maybe)
  5469. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5470. (and org-cycle-level-after-item/entry-creation
  5471. (or (org-cycle-level)
  5472. (org-cycle-item-indentation))))
  5473. (let* ((limit-level
  5474. (or org-cycle-max-level
  5475. (and (boundp 'org-inlinetask-min-level)
  5476. org-inlinetask-min-level
  5477. (1- org-inlinetask-min-level))))
  5478. (nstars (and limit-level
  5479. (if org-odd-levels-only
  5480. (and limit-level (1- (* limit-level 2)))
  5481. limit-level)))
  5482. (org-outline-regexp
  5483. (if (not (eq major-mode 'org-mode))
  5484. outline-regexp
  5485. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5486. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5487. (not (looking-at org-outline-regexp))))
  5488. (org-cycle-hook
  5489. (if bob-special
  5490. (delq 'org-optimize-window-after-visibility-change
  5491. (copy-sequence org-cycle-hook))
  5492. org-cycle-hook))
  5493. (pos (point)))
  5494. (if (or bob-special (equal arg '(4)))
  5495. ;; special case: use global cycling
  5496. (setq arg t))
  5497. (cond
  5498. ((equal arg '(16))
  5499. (setq last-command 'dummy)
  5500. (org-set-startup-visibility)
  5501. (message "Startup visibility, plus VISIBILITY properties"))
  5502. ((equal arg '(64))
  5503. (show-all)
  5504. (message "Entire buffer visible, including drawers"))
  5505. ;; Table: enter it or move to the next field.
  5506. ((org-at-table-p 'any)
  5507. (if (org-at-table.el-p)
  5508. (message "Use C-c ' to edit table.el tables")
  5509. (if arg (org-table-edit-field t)
  5510. (org-table-justify-field-maybe)
  5511. (call-interactively 'org-table-next-field))))
  5512. ((run-hook-with-args-until-success
  5513. 'org-tab-after-check-for-table-hook))
  5514. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5515. ((eq arg t) (org-cycle-internal-global))
  5516. ;; Drawers: delegate to `org-flag-drawer'.
  5517. ((and org-drawers org-drawer-regexp
  5518. (save-excursion
  5519. (beginning-of-line 1)
  5520. (looking-at org-drawer-regexp)))
  5521. (org-flag-drawer ; toggle block visibility
  5522. (not (get-char-property (match-end 0) 'invisible))))
  5523. ;; Show-subtree, ARG levels up from here.
  5524. ((integerp arg)
  5525. (save-excursion
  5526. (org-back-to-heading)
  5527. (outline-up-heading (if (< arg 0) (- arg)
  5528. (- (funcall outline-level) arg)))
  5529. (org-show-subtree)))
  5530. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5531. ((and (featurep 'org-inlinetask)
  5532. (org-inlinetask-at-task-p)
  5533. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5534. (org-inlinetask-toggle-visibility))
  5535. ((org-try-cdlatex-tab))
  5536. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5537. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5538. (save-excursion (beginning-of-line 1)
  5539. (looking-at org-outline-regexp)))
  5540. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5541. (org-cycle-internal-local))
  5542. ;; From there: TAB emulation and template completion.
  5543. (buffer-read-only (org-back-to-heading))
  5544. ((run-hook-with-args-until-success
  5545. 'org-tab-after-check-for-cycling-hook))
  5546. ((org-try-structure-completion))
  5547. ((run-hook-with-args-until-success
  5548. 'org-tab-before-tab-emulation-hook))
  5549. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5550. (or (not (bolp))
  5551. (not (looking-at org-outline-regexp))))
  5552. (call-interactively (global-key-binding "\t")))
  5553. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5554. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5555. (or (and (eq org-cycle-emulate-tab 'white)
  5556. (= (match-end 0) (point-at-eol)))
  5557. (and (eq org-cycle-emulate-tab 'whitestart)
  5558. (>= (match-end 0) pos))))
  5559. t
  5560. (eq org-cycle-emulate-tab t))
  5561. (call-interactively (global-key-binding "\t")))
  5562. (t (save-excursion
  5563. (org-back-to-heading)
  5564. (org-cycle)))))))
  5565. (defun org-cycle-internal-global ()
  5566. "Do the global cycling action."
  5567. ;; Hack to avoid display of messages for .org attachments in Gnus
  5568. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5569. (cond
  5570. ((and (eq last-command this-command)
  5571. (eq org-cycle-global-status 'overview))
  5572. ;; We just created the overview - now do table of contents
  5573. ;; This can be slow in very large buffers, so indicate action
  5574. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5575. (unless ga (message "CONTENTS..."))
  5576. (org-content)
  5577. (unless ga (message "CONTENTS...done"))
  5578. (setq org-cycle-global-status 'contents)
  5579. (run-hook-with-args 'org-cycle-hook 'contents))
  5580. ((and (eq last-command this-command)
  5581. (eq org-cycle-global-status 'contents))
  5582. ;; We just showed the table of contents - now show everything
  5583. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5584. (show-all)
  5585. (unless ga (message "SHOW ALL"))
  5586. (setq org-cycle-global-status 'all)
  5587. (run-hook-with-args 'org-cycle-hook 'all))
  5588. (t
  5589. ;; Default action: go to overview
  5590. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5591. (org-overview)
  5592. (unless ga (message "OVERVIEW"))
  5593. (setq org-cycle-global-status 'overview)
  5594. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5595. (defun org-cycle-internal-local ()
  5596. "Do the local cycling action."
  5597. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5598. ;; First, determine end of headline (EOH), end of subtree or item
  5599. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5600. (save-excursion
  5601. (if (org-at-item-p)
  5602. (progn
  5603. (beginning-of-line)
  5604. (setq struct (org-list-struct))
  5605. (setq eoh (point-at-eol))
  5606. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5607. (setq has-children (org-list-has-child-p (point) struct)))
  5608. (org-back-to-heading)
  5609. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5610. (setq eos (save-excursion
  5611. (org-end-of-subtree t)
  5612. (unless (eobp)
  5613. (skip-chars-forward " \t\n"))
  5614. (if (eobp) (point) (1- (point)))))
  5615. (setq has-children
  5616. (or (save-excursion
  5617. (let ((level (funcall outline-level)))
  5618. (outline-next-heading)
  5619. (and (org-at-heading-p t)
  5620. (> (funcall outline-level) level))))
  5621. (save-excursion
  5622. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5623. ;; Determine end invisible part of buffer (EOL)
  5624. (beginning-of-line 2)
  5625. ;; XEmacs doesn't have `next-single-char-property-change'
  5626. (if (featurep 'xemacs)
  5627. (while (and (not (eobp)) ;; this is like `next-line'
  5628. (get-char-property (1- (point)) 'invisible))
  5629. (beginning-of-line 2))
  5630. (while (and (not (eobp)) ;; this is like `next-line'
  5631. (get-char-property (1- (point)) 'invisible))
  5632. (goto-char (next-single-char-property-change (point) 'invisible))
  5633. (and (eolp) (beginning-of-line 2))))
  5634. (setq eol (point)))
  5635. ;; Find out what to do next and set `this-command'
  5636. (cond
  5637. ((= eos eoh)
  5638. ;; Nothing is hidden behind this heading
  5639. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5640. (message "EMPTY ENTRY")
  5641. (setq org-cycle-subtree-status nil)
  5642. (save-excursion
  5643. (goto-char eos)
  5644. (outline-next-heading)
  5645. (if (outline-invisible-p) (org-flag-heading nil))))
  5646. ((and (or (>= eol eos)
  5647. (not (string-match "\\S-" (buffer-substring eol eos))))
  5648. (or has-children
  5649. (not (setq children-skipped
  5650. org-cycle-skip-children-state-if-no-children))))
  5651. ;; Entire subtree is hidden in one line: children view
  5652. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5653. (if (org-at-item-p)
  5654. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5655. (org-show-entry)
  5656. (org-with-limited-levels (show-children))
  5657. (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5658. (org-cycle-hide-drawers 'subtree))
  5659. ;; Fold every list in subtree to top-level items.
  5660. (when (eq org-cycle-include-plain-lists 'integrate)
  5661. (save-excursion
  5662. (org-back-to-heading)
  5663. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5664. (beginning-of-line 1)
  5665. (let* ((struct (org-list-struct))
  5666. (prevs (org-list-prevs-alist struct))
  5667. (end (org-list-get-bottom-point struct)))
  5668. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5669. (org-list-get-all-items (point) struct prevs))
  5670. (goto-char end))))))
  5671. (message "CHILDREN")
  5672. (save-excursion
  5673. (goto-char eos)
  5674. (outline-next-heading)
  5675. (if (outline-invisible-p) (org-flag-heading nil)))
  5676. (setq org-cycle-subtree-status 'children)
  5677. (run-hook-with-args 'org-cycle-hook 'children))
  5678. ((or children-skipped
  5679. (and (eq last-command this-command)
  5680. (eq org-cycle-subtree-status 'children)))
  5681. ;; We just showed the children, or no children are there,
  5682. ;; now show everything.
  5683. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5684. (outline-flag-region eoh eos nil)
  5685. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5686. (setq org-cycle-subtree-status 'subtree)
  5687. (run-hook-with-args 'org-cycle-hook 'subtree))
  5688. (t
  5689. ;; Default action: hide the subtree.
  5690. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5691. (outline-flag-region eoh eos t)
  5692. (message "FOLDED")
  5693. (setq org-cycle-subtree-status 'folded)
  5694. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5695. ;;;###autoload
  5696. (defun org-global-cycle (&optional arg)
  5697. "Cycle the global visibility. For details see `org-cycle'.
  5698. With \\[universal-argument] prefix arg, switch to startup visibility.
  5699. With a numeric prefix, show all headlines up to that level."
  5700. (interactive "P")
  5701. (let ((org-cycle-include-plain-lists
  5702. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5703. (cond
  5704. ((integerp arg)
  5705. (show-all)
  5706. (hide-sublevels arg)
  5707. (setq org-cycle-global-status 'contents))
  5708. ((equal arg '(4))
  5709. (org-set-startup-visibility)
  5710. (message "Startup visibility, plus VISIBILITY properties."))
  5711. (t
  5712. (org-cycle '(4))))))
  5713. (defun org-set-startup-visibility ()
  5714. "Set the visibility required by startup options and properties."
  5715. (cond
  5716. ((eq org-startup-folded t)
  5717. (org-cycle '(4)))
  5718. ((eq org-startup-folded 'content)
  5719. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5720. (org-cycle '(4)) (org-cycle '(4)))))
  5721. (unless (eq org-startup-folded 'showeverything)
  5722. (if org-hide-block-startup (org-hide-block-all))
  5723. (org-set-visibility-according-to-property 'no-cleanup)
  5724. (org-cycle-hide-archived-subtrees 'all)
  5725. (org-cycle-hide-drawers 'all)
  5726. (org-cycle-show-empty-lines t)))
  5727. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5728. "Switch subtree visibilities according to :VISIBILITY: property."
  5729. (interactive)
  5730. (let (org-show-entry-below state)
  5731. (save-excursion
  5732. (goto-char (point-min))
  5733. (while (re-search-forward
  5734. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5735. nil t)
  5736. (setq state (match-string 1))
  5737. (save-excursion
  5738. (org-back-to-heading t)
  5739. (hide-subtree)
  5740. (org-reveal)
  5741. (cond
  5742. ((equal state '("fold" "folded"))
  5743. (hide-subtree))
  5744. ((equal state "children")
  5745. (org-show-hidden-entry)
  5746. (show-children))
  5747. ((equal state "content")
  5748. (save-excursion
  5749. (save-restriction
  5750. (org-narrow-to-subtree)
  5751. (org-content))))
  5752. ((member state '("all" "showall"))
  5753. (show-subtree)))))
  5754. (unless no-cleanup
  5755. (org-cycle-hide-archived-subtrees 'all)
  5756. (org-cycle-hide-drawers 'all)
  5757. (org-cycle-show-empty-lines 'all)))))
  5758. ;; This function uses outline-regexp instead of the more fundamental
  5759. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5760. ;; buffers, where outline-regexp is needed.
  5761. (defun org-overview ()
  5762. "Switch to overview mode, showing only top-level headlines.
  5763. Really, this shows all headlines with level equal or greater than the level
  5764. of the first headline in the buffer. This is important, because if the
  5765. first headline is not level one, then (hide-sublevels 1) gives confusing
  5766. results."
  5767. (interactive)
  5768. (let ((level (save-excursion
  5769. (goto-char (point-min))
  5770. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5771. (progn
  5772. (goto-char (match-beginning 0))
  5773. (funcall outline-level))))))
  5774. (and level (hide-sublevels level))))
  5775. (defun org-content (&optional arg)
  5776. "Show all headlines in the buffer, like a table of contents.
  5777. With numerical argument N, show content up to level N."
  5778. (interactive "P")
  5779. (save-excursion
  5780. ;; Visit all headings and show their offspring
  5781. (and (integerp arg) (org-overview))
  5782. (goto-char (point-max))
  5783. (catch 'exit
  5784. (while (and (progn (condition-case nil
  5785. (outline-previous-visible-heading 1)
  5786. (error (goto-char (point-min))))
  5787. t)
  5788. (looking-at org-outline-regexp))
  5789. (if (integerp arg)
  5790. (show-children (1- arg))
  5791. (show-branches))
  5792. (if (bobp) (throw 'exit nil))))))
  5793. (defun org-optimize-window-after-visibility-change (state)
  5794. "Adjust the window after a change in outline visibility.
  5795. This function is the default value of the hook `org-cycle-hook'."
  5796. (when (get-buffer-window (current-buffer))
  5797. (cond
  5798. ((eq state 'content) nil)
  5799. ((eq state 'all) nil)
  5800. ((eq state 'folded) nil)
  5801. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5802. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5803. (defun org-remove-empty-overlays-at (pos)
  5804. "Remove outline overlays that do not contain non-white stuff."
  5805. (mapc
  5806. (lambda (o)
  5807. (and (eq 'outline (overlay-get o 'invisible))
  5808. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5809. (overlay-end o))))
  5810. (delete-overlay o)))
  5811. (overlays-at pos)))
  5812. (defun org-clean-visibility-after-subtree-move ()
  5813. "Fix visibility issues after moving a subtree."
  5814. ;; First, find a reasonable region to look at:
  5815. ;; Start two siblings above, end three below
  5816. (let* ((beg (save-excursion
  5817. (and (org-get-last-sibling)
  5818. (org-get-last-sibling))
  5819. (point)))
  5820. (end (save-excursion
  5821. (and (org-get-next-sibling)
  5822. (org-get-next-sibling)
  5823. (org-get-next-sibling))
  5824. (if (org-at-heading-p)
  5825. (point-at-eol)
  5826. (point))))
  5827. (level (looking-at "\\*+"))
  5828. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5829. (save-excursion
  5830. (save-restriction
  5831. (narrow-to-region beg end)
  5832. (when re
  5833. ;; Properly fold already folded siblings
  5834. (goto-char (point-min))
  5835. (while (re-search-forward re nil t)
  5836. (if (and (not (outline-invisible-p))
  5837. (save-excursion
  5838. (goto-char (point-at-eol)) (outline-invisible-p)))
  5839. (hide-entry))))
  5840. (org-cycle-show-empty-lines 'overview)
  5841. (org-cycle-hide-drawers 'overview)))))
  5842. (defun org-cycle-show-empty-lines (state)
  5843. "Show empty lines above all visible headlines.
  5844. The region to be covered depends on STATE when called through
  5845. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5846. entire buffer covered. Note that an empty line is only shown if there
  5847. are at least `org-cycle-separator-lines' empty lines before the headline."
  5848. (when (not (= org-cycle-separator-lines 0))
  5849. (save-excursion
  5850. (let* ((n (abs org-cycle-separator-lines))
  5851. (re (cond
  5852. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5853. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5854. (t (let ((ns (number-to-string (- n 2))))
  5855. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5856. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5857. beg end b e)
  5858. (cond
  5859. ((memq state '(overview contents t))
  5860. (setq beg (point-min) end (point-max)))
  5861. ((memq state '(children folded))
  5862. (setq beg (point) end (progn (org-end-of-subtree t t)
  5863. (beginning-of-line 2)
  5864. (point)))))
  5865. (when beg
  5866. (goto-char beg)
  5867. (while (re-search-forward re end t)
  5868. (unless (get-char-property (match-end 1) 'invisible)
  5869. (setq e (match-end 1))
  5870. (if (< org-cycle-separator-lines 0)
  5871. (setq b (save-excursion
  5872. (goto-char (match-beginning 0))
  5873. (org-back-over-empty-lines)
  5874. (if (save-excursion
  5875. (goto-char (max (point-min) (1- (point))))
  5876. (org-at-heading-p))
  5877. (1- (point))
  5878. (point))))
  5879. (setq b (match-beginning 1)))
  5880. (outline-flag-region b e nil)))))))
  5881. ;; Never hide empty lines at the end of the file.
  5882. (save-excursion
  5883. (goto-char (point-max))
  5884. (outline-previous-heading)
  5885. (outline-end-of-heading)
  5886. (if (and (looking-at "[ \t\n]+")
  5887. (= (match-end 0) (point-max)))
  5888. (outline-flag-region (point) (match-end 0) nil))))
  5889. (defun org-show-empty-lines-in-parent ()
  5890. "Move to the parent and re-show empty lines before visible headlines."
  5891. (save-excursion
  5892. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5893. (org-cycle-show-empty-lines context))))
  5894. (defun org-files-list ()
  5895. "Return `org-agenda-files' list, plus all open org-mode files.
  5896. This is useful for operations that need to scan all of a user's
  5897. open and agenda-wise Org files."
  5898. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5899. (dolist (buf (buffer-list))
  5900. (with-current-buffer buf
  5901. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5902. (let ((file (expand-file-name (buffer-file-name))))
  5903. (unless (member file files)
  5904. (push file files))))))
  5905. files))
  5906. (defsubst org-entry-beginning-position ()
  5907. "Return the beginning position of the current entry."
  5908. (save-excursion (outline-back-to-heading t) (point)))
  5909. (defsubst org-entry-end-position ()
  5910. "Return the end position of the current entry."
  5911. (save-excursion (outline-next-heading) (point)))
  5912. (defun org-cycle-hide-drawers (state)
  5913. "Re-hide all drawers after a visibility state change."
  5914. (when (and (eq major-mode 'org-mode)
  5915. (not (memq state '(overview folded contents))))
  5916. (save-excursion
  5917. (let* ((globalp (memq state '(contents all)))
  5918. (beg (if globalp (point-min) (point)))
  5919. (end (if globalp (point-max)
  5920. (if (eq state 'children)
  5921. (save-excursion (outline-next-heading) (point))
  5922. (org-end-of-subtree t)))))
  5923. (goto-char beg)
  5924. (while (re-search-forward org-drawer-regexp end t)
  5925. (org-flag-drawer t))))))
  5926. (defun org-flag-drawer (flag)
  5927. (save-excursion
  5928. (beginning-of-line 1)
  5929. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5930. (let ((b (match-end 0)))
  5931. (if (re-search-forward
  5932. "^[ \t]*:END:"
  5933. (save-excursion (outline-next-heading) (point)) t)
  5934. (outline-flag-region b (point-at-eol) flag)
  5935. (error ":END: line missing at position %s" b))))))
  5936. (defun org-subtree-end-visible-p ()
  5937. "Is the end of the current subtree visible?"
  5938. (pos-visible-in-window-p
  5939. (save-excursion (org-end-of-subtree t) (point))))
  5940. (defun org-first-headline-recenter (&optional N)
  5941. "Move cursor to the first headline and recenter the headline.
  5942. Optional argument N means put the headline into the Nth line of the window."
  5943. (goto-char (point-min))
  5944. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5945. (beginning-of-line)
  5946. (recenter (prefix-numeric-value N))))
  5947. ;;; Saving and restoring visibility
  5948. (defun org-outline-overlay-data (&optional use-markers)
  5949. "Return a list of the locations of all outline overlays.
  5950. These are overlays with the `invisible' property value `outline'.
  5951. The return value is a list of cons cells, with start and stop
  5952. positions for each overlay.
  5953. If USE-MARKERS is set, return the positions as markers."
  5954. (let (beg end)
  5955. (save-excursion
  5956. (save-restriction
  5957. (widen)
  5958. (delq nil
  5959. (mapcar (lambda (o)
  5960. (when (eq (overlay-get o 'invisible) 'outline)
  5961. (setq beg (overlay-start o)
  5962. end (overlay-end o))
  5963. (and beg end (> end beg)
  5964. (if use-markers
  5965. (cons (move-marker (make-marker) beg)
  5966. (move-marker (make-marker) end))
  5967. (cons beg end)))))
  5968. (overlays-in (point-min) (point-max))))))))
  5969. (defun org-set-outline-overlay-data (data)
  5970. "Create visibility overlays for all positions in DATA.
  5971. DATA should have been made by `org-outline-overlay-data'."
  5972. (let (o)
  5973. (save-excursion
  5974. (save-restriction
  5975. (widen)
  5976. (show-all)
  5977. (mapc (lambda (c)
  5978. (outline-flag-region (car c) (cdr c) t))
  5979. data)))))
  5980. ;;; Folding of blocks
  5981. (defvar org-hide-block-overlays nil
  5982. "Overlays hiding blocks.")
  5983. (make-variable-buffer-local 'org-hide-block-overlays)
  5984. (defun org-block-map (function &optional start end)
  5985. "Call FUNCTION at the head of all source blocks in the current buffer.
  5986. Optional arguments START and END can be used to limit the range."
  5987. (let ((start (or start (point-min)))
  5988. (end (or end (point-max))))
  5989. (save-excursion
  5990. (goto-char start)
  5991. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5992. (save-excursion
  5993. (save-match-data
  5994. (goto-char (match-beginning 0))
  5995. (funcall function)))))))
  5996. (defun org-hide-block-toggle-all ()
  5997. "Toggle the visibility of all blocks in the current buffer."
  5998. (org-block-map #'org-hide-block-toggle))
  5999. (defun org-hide-block-all ()
  6000. "Fold all blocks in the current buffer."
  6001. (interactive)
  6002. (org-show-block-all)
  6003. (org-block-map #'org-hide-block-toggle-maybe))
  6004. (defun org-show-block-all ()
  6005. "Unfold all blocks in the current buffer."
  6006. (interactive)
  6007. (mapc 'delete-overlay org-hide-block-overlays)
  6008. (setq org-hide-block-overlays nil))
  6009. (defun org-hide-block-toggle-maybe ()
  6010. "Toggle visibility of block at point."
  6011. (interactive)
  6012. (let ((case-fold-search t))
  6013. (if (save-excursion
  6014. (beginning-of-line 1)
  6015. (looking-at org-block-regexp))
  6016. (progn (org-hide-block-toggle)
  6017. t) ;; to signal that we took action
  6018. nil))) ;; to signal that we did not
  6019. (defun org-hide-block-toggle (&optional force)
  6020. "Toggle the visibility of the current block."
  6021. (interactive)
  6022. (save-excursion
  6023. (beginning-of-line)
  6024. (if (re-search-forward org-block-regexp nil t)
  6025. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6026. (end (match-end 0)) ;; end of entire body
  6027. ov)
  6028. (if (memq t (mapcar (lambda (overlay)
  6029. (eq (overlay-get overlay 'invisible)
  6030. 'org-hide-block))
  6031. (overlays-at start)))
  6032. (if (or (not force) (eq force 'off))
  6033. (mapc (lambda (ov)
  6034. (when (member ov org-hide-block-overlays)
  6035. (setq org-hide-block-overlays
  6036. (delq ov org-hide-block-overlays)))
  6037. (when (eq (overlay-get ov 'invisible)
  6038. 'org-hide-block)
  6039. (delete-overlay ov)))
  6040. (overlays-at start)))
  6041. (setq ov (make-overlay start end))
  6042. (overlay-put ov 'invisible 'org-hide-block)
  6043. ;; make the block accessible to isearch
  6044. (overlay-put
  6045. ov 'isearch-open-invisible
  6046. (lambda (ov)
  6047. (when (member ov org-hide-block-overlays)
  6048. (setq org-hide-block-overlays
  6049. (delq ov org-hide-block-overlays)))
  6050. (when (eq (overlay-get ov 'invisible)
  6051. 'org-hide-block)
  6052. (delete-overlay ov))))
  6053. (push ov org-hide-block-overlays)))
  6054. (error "Not looking at a source block"))))
  6055. ;; org-tab-after-check-for-cycling-hook
  6056. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6057. ;; Remove overlays when changing major mode
  6058. (add-hook 'org-mode-hook
  6059. (lambda () (org-add-hook 'change-major-mode-hook
  6060. 'org-show-block-all 'append 'local)))
  6061. ;;; Org-goto
  6062. (defvar org-goto-window-configuration nil)
  6063. (defvar org-goto-marker nil)
  6064. (defvar org-goto-map
  6065. (let ((map (make-sparse-keymap)))
  6066. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6067. (while (setq cmd (pop cmds))
  6068. (substitute-key-definition cmd cmd map global-map)))
  6069. (suppress-keymap map)
  6070. (org-defkey map "\C-m" 'org-goto-ret)
  6071. (org-defkey map [(return)] 'org-goto-ret)
  6072. (org-defkey map [(left)] 'org-goto-left)
  6073. (org-defkey map [(right)] 'org-goto-right)
  6074. (org-defkey map [(control ?g)] 'org-goto-quit)
  6075. (org-defkey map "\C-i" 'org-cycle)
  6076. (org-defkey map [(tab)] 'org-cycle)
  6077. (org-defkey map [(down)] 'outline-next-visible-heading)
  6078. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6079. (if org-goto-auto-isearch
  6080. (if (fboundp 'define-key-after)
  6081. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6082. nil)
  6083. (org-defkey map "q" 'org-goto-quit)
  6084. (org-defkey map "n" 'outline-next-visible-heading)
  6085. (org-defkey map "p" 'outline-previous-visible-heading)
  6086. (org-defkey map "f" 'outline-forward-same-level)
  6087. (org-defkey map "b" 'outline-backward-same-level)
  6088. (org-defkey map "u" 'outline-up-heading))
  6089. (org-defkey map "/" 'org-occur)
  6090. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6091. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6092. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6093. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6094. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6095. map))
  6096. (defconst org-goto-help
  6097. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6098. RET=jump to location [Q]uit and return to previous location
  6099. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6100. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6101. ;; FIXME: Docstring does not mention both interfaces
  6102. (defun org-goto (&optional alternative-interface)
  6103. "Look up a different location in the current file, keeping current visibility.
  6104. When you want look-up or go to a different location in a document, the
  6105. fastest way is often to fold the entire buffer and then dive into the tree.
  6106. This method has the disadvantage, that the previous location will be folded,
  6107. which may not be what you want.
  6108. This command works around this by showing a copy of the current buffer
  6109. in an indirect buffer, in overview mode. You can dive into the tree in
  6110. that copy, use org-occur and incremental search to find a location.
  6111. When pressing RET or `Q', the command returns to the original buffer in
  6112. which the visibility is still unchanged. After RET it will also jump to
  6113. the location selected in the indirect buffer and expose the headline
  6114. hierarchy above."
  6115. (interactive "P")
  6116. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6117. (org-refile-use-outline-path t)
  6118. (org-refile-target-verify-function nil)
  6119. (interface
  6120. (if (not alternative-interface)
  6121. org-goto-interface
  6122. (if (eq org-goto-interface 'outline)
  6123. 'outline-path-completion
  6124. 'outline)))
  6125. (org-goto-start-pos (point))
  6126. (selected-point
  6127. (if (eq interface 'outline)
  6128. (car (org-get-location (current-buffer) org-goto-help))
  6129. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6130. (org-refile-check-position pa)
  6131. (nth 3 pa)))))
  6132. (if selected-point
  6133. (progn
  6134. (org-mark-ring-push org-goto-start-pos)
  6135. (goto-char selected-point)
  6136. (if (or (outline-invisible-p) (org-invisible-p2))
  6137. (org-show-context 'org-goto)))
  6138. (message "Quit"))))
  6139. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6140. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6141. (defvar org-goto-local-auto-isearch-map) ; defined below
  6142. (defun org-get-location (buf help)
  6143. "Let the user select a location in the Org-mode buffer BUF.
  6144. This function uses a recursive edit. It returns the selected position
  6145. or nil."
  6146. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6147. (isearch-hide-immediately nil)
  6148. (isearch-search-fun-function
  6149. (lambda () 'org-goto-local-search-headings))
  6150. (org-goto-selected-point org-goto-exit-command)
  6151. (pop-up-frames nil)
  6152. (special-display-buffer-names nil)
  6153. (special-display-regexps nil)
  6154. (special-display-function nil))
  6155. (save-excursion
  6156. (save-window-excursion
  6157. (delete-other-windows)
  6158. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6159. (org-pop-to-buffer-same-window
  6160. (condition-case nil
  6161. (make-indirect-buffer (current-buffer) "*org-goto*")
  6162. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6163. (with-output-to-temp-buffer "*Help*"
  6164. (princ help))
  6165. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6166. (setq buffer-read-only nil)
  6167. (let ((org-startup-truncated t)
  6168. (org-startup-folded nil)
  6169. (org-startup-align-all-tables nil))
  6170. (org-mode)
  6171. (org-overview))
  6172. (setq buffer-read-only t)
  6173. (if (and (boundp 'org-goto-start-pos)
  6174. (integer-or-marker-p org-goto-start-pos))
  6175. (let ((org-show-hierarchy-above t)
  6176. (org-show-siblings t)
  6177. (org-show-following-heading t))
  6178. (goto-char org-goto-start-pos)
  6179. (and (outline-invisible-p) (org-show-context)))
  6180. (goto-char (point-min)))
  6181. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6182. (message "Select location and press RET")
  6183. (use-local-map org-goto-map)
  6184. (recursive-edit)
  6185. ))
  6186. (kill-buffer "*org-goto*")
  6187. (cons org-goto-selected-point org-goto-exit-command)))
  6188. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6189. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6190. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6191. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6192. (defun org-goto-local-search-headings (string bound noerror)
  6193. "Search and make sure that any matches are in headlines."
  6194. (catch 'return
  6195. (while (if isearch-forward
  6196. (search-forward string bound noerror)
  6197. (search-backward string bound noerror))
  6198. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6199. (and (member :headline context)
  6200. (not (member :tags context))))
  6201. (throw 'return (point))))))
  6202. (defun org-goto-local-auto-isearch ()
  6203. "Start isearch."
  6204. (interactive)
  6205. (goto-char (point-min))
  6206. (let ((keys (this-command-keys)))
  6207. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6208. (isearch-mode t)
  6209. (isearch-process-search-char (string-to-char keys)))))
  6210. (defun org-goto-ret (&optional arg)
  6211. "Finish `org-goto' by going to the new location."
  6212. (interactive "P")
  6213. (setq org-goto-selected-point (point)
  6214. org-goto-exit-command 'return)
  6215. (throw 'exit nil))
  6216. (defun org-goto-left ()
  6217. "Finish `org-goto' by going to the new location."
  6218. (interactive)
  6219. (if (org-at-heading-p)
  6220. (progn
  6221. (beginning-of-line 1)
  6222. (setq org-goto-selected-point (point)
  6223. org-goto-exit-command 'left)
  6224. (throw 'exit nil))
  6225. (error "Not on a heading")))
  6226. (defun org-goto-right ()
  6227. "Finish `org-goto' by going to the new location."
  6228. (interactive)
  6229. (if (org-at-heading-p)
  6230. (progn
  6231. (setq org-goto-selected-point (point)
  6232. org-goto-exit-command 'right)
  6233. (throw 'exit nil))
  6234. (error "Not on a heading")))
  6235. (defun org-goto-quit ()
  6236. "Finish `org-goto' without cursor motion."
  6237. (interactive)
  6238. (setq org-goto-selected-point nil)
  6239. (setq org-goto-exit-command 'quit)
  6240. (throw 'exit nil))
  6241. ;;; Indirect buffer display of subtrees
  6242. (defvar org-indirect-dedicated-frame nil
  6243. "This is the frame being used for indirect tree display.")
  6244. (defvar org-last-indirect-buffer nil)
  6245. (defun org-tree-to-indirect-buffer (&optional arg)
  6246. "Create indirect buffer and narrow it to current subtree.
  6247. With numerical prefix ARG, go up to this level and then take that tree.
  6248. If ARG is negative, go up that many levels.
  6249. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6250. indirect buffer previously made with this command, to avoid proliferation of
  6251. indirect buffers. However, when you call the command with a \
  6252. \\[universal-argument] prefix, or
  6253. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6254. is kept so that you can work with several indirect buffers at the same time.
  6255. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6256. \\[universal-argument] prefix also
  6257. requests that a new frame be made for the new buffer, so that the dedicated
  6258. frame is not changed."
  6259. (interactive "P")
  6260. (let ((cbuf (current-buffer))
  6261. (cwin (selected-window))
  6262. (pos (point))
  6263. beg end level heading ibuf)
  6264. (save-excursion
  6265. (org-back-to-heading t)
  6266. (when (numberp arg)
  6267. (setq level (org-outline-level))
  6268. (if (< arg 0) (setq arg (+ level arg)))
  6269. (while (> (setq level (org-outline-level)) arg)
  6270. (outline-up-heading 1 t)))
  6271. (setq beg (point)
  6272. heading (org-get-heading))
  6273. (org-end-of-subtree t t)
  6274. (if (org-at-heading-p) (backward-char 1))
  6275. (setq end (point)))
  6276. (if (and (buffer-live-p org-last-indirect-buffer)
  6277. (not (eq org-indirect-buffer-display 'new-frame))
  6278. (not arg))
  6279. (kill-buffer org-last-indirect-buffer))
  6280. (setq ibuf (org-get-indirect-buffer cbuf)
  6281. org-last-indirect-buffer ibuf)
  6282. (cond
  6283. ((or (eq org-indirect-buffer-display 'new-frame)
  6284. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6285. (select-frame (make-frame))
  6286. (delete-other-windows)
  6287. (org-pop-to-buffer-same-window ibuf)
  6288. (org-set-frame-title heading))
  6289. ((eq org-indirect-buffer-display 'dedicated-frame)
  6290. (raise-frame
  6291. (select-frame (or (and org-indirect-dedicated-frame
  6292. (frame-live-p org-indirect-dedicated-frame)
  6293. org-indirect-dedicated-frame)
  6294. (setq org-indirect-dedicated-frame (make-frame)))))
  6295. (delete-other-windows)
  6296. (org-pop-to-buffer-same-window ibuf)
  6297. (org-set-frame-title (concat "Indirect: " heading)))
  6298. ((eq org-indirect-buffer-display 'current-window)
  6299. (org-pop-to-buffer-same-window ibuf))
  6300. ((eq org-indirect-buffer-display 'other-window)
  6301. (pop-to-buffer ibuf))
  6302. (t (error "Invalid value")))
  6303. (if (featurep 'xemacs)
  6304. (save-excursion (org-mode) (turn-on-font-lock)))
  6305. (narrow-to-region beg end)
  6306. (show-all)
  6307. (goto-char pos)
  6308. (run-hook-with-args 'org-cycle-hook 'all)
  6309. (and (window-live-p cwin) (select-window cwin))))
  6310. (defun org-get-indirect-buffer (&optional buffer)
  6311. (setq buffer (or buffer (current-buffer)))
  6312. (let ((n 1) (base (buffer-name buffer)) bname)
  6313. (while (buffer-live-p
  6314. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6315. (setq n (1+ n)))
  6316. (condition-case nil
  6317. (make-indirect-buffer buffer bname 'clone)
  6318. (error (make-indirect-buffer buffer bname)))))
  6319. (defun org-set-frame-title (title)
  6320. "Set the title of the current frame to the string TITLE."
  6321. ;; FIXME: how to name a single frame in XEmacs???
  6322. (unless (featurep 'xemacs)
  6323. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6324. ;;;; Structure editing
  6325. ;;; Inserting headlines
  6326. (defun org-previous-line-empty-p ()
  6327. (save-excursion
  6328. (and (not (bobp))
  6329. (or (beginning-of-line 0) t)
  6330. (save-match-data
  6331. (looking-at "[ \t]*$")))))
  6332. (defun org-insert-heading (&optional force-heading invisible-ok)
  6333. "Insert a new heading or item with same depth at point.
  6334. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6335. If point is at the beginning of a headline, insert a sibling before the
  6336. current headline. If point is not at the beginning, split the line,
  6337. create the new headline with the text in the current line after point
  6338. \(but see also the variable `org-M-RET-may-split-line').
  6339. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6340. This is important for non-interactive uses of the command."
  6341. (interactive "P")
  6342. (if (or (= (buffer-size) 0)
  6343. (and (not (save-excursion
  6344. (and (ignore-errors (org-back-to-heading invisible-ok))
  6345. (org-at-heading-p))))
  6346. (or force-heading (not (org-in-item-p)))))
  6347. (progn
  6348. (insert "\n* ")
  6349. (run-hooks 'org-insert-heading-hook))
  6350. (when (or force-heading (not (org-insert-item)))
  6351. (let* ((empty-line-p nil)
  6352. (level nil)
  6353. (on-heading (org-at-heading-p))
  6354. (head (save-excursion
  6355. (condition-case nil
  6356. (progn
  6357. (org-back-to-heading invisible-ok)
  6358. (when (and (not on-heading)
  6359. (featurep 'org-inlinetask)
  6360. (integerp org-inlinetask-min-level)
  6361. (>= (length (match-string 0))
  6362. org-inlinetask-min-level))
  6363. ;; Find a heading level before the inline task
  6364. (while (and (setq level (org-up-heading-safe))
  6365. (>= level org-inlinetask-min-level)))
  6366. (if (org-at-heading-p)
  6367. (org-back-to-heading invisible-ok)
  6368. (error "This should not happen")))
  6369. (setq empty-line-p (org-previous-line-empty-p))
  6370. (match-string 0))
  6371. (error "*"))))
  6372. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6373. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6374. pos hide-previous previous-pos)
  6375. (cond
  6376. ((and (org-at-heading-p) (bolp)
  6377. (or (bobp)
  6378. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6379. ;; insert before the current line
  6380. (open-line (if blank 2 1)))
  6381. ((and (bolp)
  6382. (not org-insert-heading-respect-content)
  6383. (or (bobp)
  6384. (save-excursion
  6385. (backward-char 1) (not (outline-invisible-p)))))
  6386. ;; insert right here
  6387. nil)
  6388. (t
  6389. ;; somewhere in the line
  6390. (save-excursion
  6391. (setq previous-pos (point-at-bol))
  6392. (end-of-line)
  6393. (setq hide-previous (outline-invisible-p)))
  6394. (and org-insert-heading-respect-content (org-show-subtree))
  6395. (let ((split
  6396. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6397. (save-excursion
  6398. (let ((p (point)))
  6399. (goto-char (point-at-bol))
  6400. (and (looking-at org-complex-heading-regexp)
  6401. (match-beginning 4)
  6402. (> p (match-beginning 4)))))))
  6403. tags pos)
  6404. (cond
  6405. (org-insert-heading-respect-content
  6406. (org-end-of-subtree nil t)
  6407. (when (featurep 'org-inlinetask)
  6408. (while (and (not (eobp))
  6409. (looking-at "\\(\\*+\\)[ \t]+")
  6410. (>= (length (match-string 1))
  6411. org-inlinetask-min-level))
  6412. (org-end-of-subtree nil t)))
  6413. (or (bolp) (newline))
  6414. (or (org-previous-line-empty-p)
  6415. (and blank (newline)))
  6416. (open-line 1))
  6417. ((org-at-heading-p)
  6418. (when hide-previous
  6419. (show-children)
  6420. (org-show-entry))
  6421. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6422. (setq tags (and (match-end 2) (match-string 2)))
  6423. (and (match-end 1)
  6424. (delete-region (match-beginning 1) (match-end 1)))
  6425. (setq pos (point-at-bol))
  6426. (or split (end-of-line 1))
  6427. (delete-horizontal-space)
  6428. (if (string-match "\\`\\*+\\'"
  6429. (buffer-substring (point-at-bol) (point)))
  6430. (insert " "))
  6431. (newline (if blank 2 1))
  6432. (when tags
  6433. (save-excursion
  6434. (goto-char pos)
  6435. (end-of-line 1)
  6436. (insert " " tags)
  6437. (org-set-tags nil 'align))))
  6438. (t
  6439. (or split (end-of-line 1))
  6440. (newline (if blank 2 1)))))))
  6441. (insert head) (just-one-space)
  6442. (setq pos (point))
  6443. (end-of-line 1)
  6444. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6445. (when (and org-insert-heading-respect-content hide-previous)
  6446. (save-excursion
  6447. (goto-char previous-pos)
  6448. (hide-subtree)))
  6449. (run-hooks 'org-insert-heading-hook)))))
  6450. (defun org-get-heading (&optional no-tags no-todo)
  6451. "Return the heading of the current entry, without the stars.
  6452. When NO-TAGS is non-nil, don't include tags.
  6453. When NO-TODO is non-nil, don't include TODO keywords."
  6454. (save-excursion
  6455. (org-back-to-heading t)
  6456. (cond
  6457. ((and no-tags no-todo)
  6458. (looking-at org-complex-heading-regexp)
  6459. (match-string 4))
  6460. (no-tags
  6461. (looking-at (concat org-outline-regexp
  6462. "\\(.*?\\)"
  6463. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6464. (match-string 1))
  6465. (no-todo
  6466. (looking-at org-todo-line-regexp)
  6467. (match-string 3))
  6468. (t (looking-at org-heading-regexp)
  6469. (match-string 2)))))
  6470. (defun org-heading-components ()
  6471. "Return the components of the current heading.
  6472. This is a list with the following elements:
  6473. - the level as an integer
  6474. - the reduced level, different if `org-odd-levels-only' is set.
  6475. - the TODO keyword, or nil
  6476. - the priority character, like ?A, or nil if no priority is given
  6477. - the headline text itself, or the tags string if no headline text
  6478. - the tags string, or nil."
  6479. (save-excursion
  6480. (org-back-to-heading t)
  6481. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6482. (list (length (match-string 1))
  6483. (org-reduced-level (length (match-string 1)))
  6484. (org-match-string-no-properties 2)
  6485. (and (match-end 3) (aref (match-string 3) 2))
  6486. (org-match-string-no-properties 4)
  6487. (org-match-string-no-properties 5)))))
  6488. (defun org-get-entry ()
  6489. "Get the entry text, after heading, entire subtree."
  6490. (save-excursion
  6491. (org-back-to-heading t)
  6492. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6493. (defun org-insert-heading-after-current ()
  6494. "Insert a new heading with same level as current, after current subtree."
  6495. (interactive)
  6496. (org-back-to-heading)
  6497. (org-insert-heading)
  6498. (org-move-subtree-down)
  6499. (end-of-line 1))
  6500. (defun org-insert-heading-respect-content ()
  6501. (interactive)
  6502. (let ((org-insert-heading-respect-content t))
  6503. (org-insert-heading t)))
  6504. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6505. (interactive "P")
  6506. (let ((org-insert-heading-respect-content t))
  6507. (org-insert-todo-heading force-state t)))
  6508. (defun org-insert-todo-heading (arg &optional force-heading)
  6509. "Insert a new heading with the same level and TODO state as current heading.
  6510. If the heading has no TODO state, or if the state is DONE, use the first
  6511. state (TODO by default). Also with prefix arg, force first state."
  6512. (interactive "P")
  6513. (when (or force-heading (not (org-insert-item 'checkbox)))
  6514. (org-insert-heading force-heading)
  6515. (save-excursion
  6516. (org-back-to-heading)
  6517. (outline-previous-heading)
  6518. (looking-at org-todo-line-regexp))
  6519. (let*
  6520. ((new-mark-x
  6521. (if (or arg
  6522. (not (match-beginning 2))
  6523. (member (match-string 2) org-done-keywords))
  6524. (car org-todo-keywords-1)
  6525. (match-string 2)))
  6526. (new-mark
  6527. (or
  6528. (run-hook-with-args-until-success
  6529. 'org-todo-get-default-hook new-mark-x nil)
  6530. new-mark-x)))
  6531. (beginning-of-line 1)
  6532. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6533. (if org-treat-insert-todo-heading-as-state-change
  6534. (org-todo new-mark)
  6535. (insert new-mark " "))))
  6536. (when org-provide-todo-statistics
  6537. (org-update-parent-todo-statistics))))
  6538. (defun org-insert-subheading (arg)
  6539. "Insert a new subheading and demote it.
  6540. Works for outline headings and for plain lists alike."
  6541. (interactive "P")
  6542. (org-insert-heading arg)
  6543. (cond
  6544. ((org-at-heading-p) (org-do-demote))
  6545. ((org-at-item-p) (org-indent-item))))
  6546. (defun org-insert-todo-subheading (arg)
  6547. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6548. Works for outline headings and for plain lists alike."
  6549. (interactive "P")
  6550. (org-insert-todo-heading arg)
  6551. (cond
  6552. ((org-at-heading-p) (org-do-demote))
  6553. ((org-at-item-p) (org-indent-item))))
  6554. ;;; Promotion and Demotion
  6555. (defvar org-after-demote-entry-hook nil
  6556. "Hook run after an entry has been demoted.
  6557. The cursor will be at the beginning of the entry.
  6558. When a subtree is being demoted, the hook will be called for each node.")
  6559. (defvar org-after-promote-entry-hook nil
  6560. "Hook run after an entry has been promoted.
  6561. The cursor will be at the beginning of the entry.
  6562. When a subtree is being promoted, the hook will be called for each node.")
  6563. (defun org-promote-subtree ()
  6564. "Promote the entire subtree.
  6565. See also `org-promote'."
  6566. (interactive)
  6567. (save-excursion
  6568. (org-with-limited-levels (org-map-tree 'org-promote)))
  6569. (org-fix-position-after-promote))
  6570. (defun org-demote-subtree ()
  6571. "Demote the entire subtree. See `org-demote'.
  6572. See also `org-promote'."
  6573. (interactive)
  6574. (save-excursion
  6575. (org-with-limited-levels (org-map-tree 'org-demote)))
  6576. (org-fix-position-after-promote))
  6577. (defun org-do-promote ()
  6578. "Promote the current heading higher up the tree.
  6579. If the region is active in `transient-mark-mode', promote all headings
  6580. in the region."
  6581. (interactive)
  6582. (save-excursion
  6583. (if (org-region-active-p)
  6584. (org-map-region 'org-promote (region-beginning) (region-end))
  6585. (org-promote)))
  6586. (org-fix-position-after-promote))
  6587. (defun org-do-demote ()
  6588. "Demote the current heading lower down the tree.
  6589. If the region is active in `transient-mark-mode', demote all headings
  6590. in the region."
  6591. (interactive)
  6592. (save-excursion
  6593. (if (org-region-active-p)
  6594. (org-map-region 'org-demote (region-beginning) (region-end))
  6595. (org-demote)))
  6596. (org-fix-position-after-promote))
  6597. (defun org-fix-position-after-promote ()
  6598. "Make sure that after pro/demotion cursor position is right."
  6599. (let ((pos (point)))
  6600. (when (save-excursion
  6601. (beginning-of-line 1)
  6602. (looking-at org-todo-line-regexp)
  6603. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6604. (cond ((eobp) (insert " "))
  6605. ((eolp) (insert " "))
  6606. ((equal (char-after) ?\ ) (forward-char 1))))))
  6607. (defun org-current-level ()
  6608. "Return the level of the current entry, or nil if before the first headline.
  6609. The level is the number of stars at the beginning of the headline."
  6610. (save-excursion
  6611. (org-with-limited-levels
  6612. (if (ignore-errors (org-back-to-heading t))
  6613. (funcall outline-level)))))
  6614. (defun org-get-previous-line-level ()
  6615. "Return the outline depth of the last headline before the current line.
  6616. Returns 0 for the first headline in the buffer, and nil if before the
  6617. first headline."
  6618. (let ((current-level (org-current-level))
  6619. (prev-level (when (> (line-number-at-pos) 1)
  6620. (save-excursion
  6621. (beginning-of-line 0)
  6622. (org-current-level)))))
  6623. (cond ((null current-level) nil) ; Before first headline
  6624. ((null prev-level) 0) ; At first headline
  6625. (prev-level))))
  6626. (defun org-reduced-level (l)
  6627. "Compute the effective level of a heading.
  6628. This takes into account the setting of `org-odd-levels-only'."
  6629. (cond
  6630. ((zerop l) 0)
  6631. (org-odd-levels-only (1+ (floor (/ l 2))))
  6632. (t l)))
  6633. (defun org-level-increment ()
  6634. "Return the number of stars that will be added or removed at a
  6635. time to headlines when structure editing, based on the value of
  6636. `org-odd-levels-only'."
  6637. (if org-odd-levels-only 2 1))
  6638. (defun org-get-valid-level (level &optional change)
  6639. "Rectify a level change under the influence of `org-odd-levels-only'
  6640. LEVEL is a current level, CHANGE is by how much the level should be
  6641. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6642. even level numbers will become the next higher odd number."
  6643. (if org-odd-levels-only
  6644. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6645. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6646. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6647. (max 1 (+ level (or change 0)))))
  6648. (if (boundp 'define-obsolete-function-alias)
  6649. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6650. (define-obsolete-function-alias 'org-get-legal-level
  6651. 'org-get-valid-level)
  6652. (define-obsolete-function-alias 'org-get-legal-level
  6653. 'org-get-valid-level "23.1")))
  6654. (defun org-promote ()
  6655. "Promote the current heading higher up the tree.
  6656. If the region is active in `transient-mark-mode', promote all headings
  6657. in the region."
  6658. (org-back-to-heading t)
  6659. (let* ((level (save-match-data (funcall outline-level)))
  6660. (after-change-functions (remove 'flyspell-after-change-function
  6661. after-change-functions))
  6662. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6663. (diff (abs (- level (length up-head) -1))))
  6664. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6665. (replace-match up-head nil t)
  6666. ;; Fixup tag positioning
  6667. (and org-auto-align-tags (org-set-tags nil t))
  6668. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6669. (run-hooks 'org-after-promote-entry-hook)))
  6670. (defun org-demote ()
  6671. "Demote the current heading lower down the tree.
  6672. If the region is active in `transient-mark-mode', demote all headings
  6673. in the region."
  6674. (org-back-to-heading t)
  6675. (let* ((level (save-match-data (funcall outline-level)))
  6676. (after-change-functions (remove 'flyspell-after-change-function
  6677. after-change-functions))
  6678. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6679. (diff (abs (- level (length down-head) -1))))
  6680. (replace-match down-head nil t)
  6681. ;; Fixup tag positioning
  6682. (and org-auto-align-tags (org-set-tags nil t))
  6683. (if org-adapt-indentation (org-fixup-indentation diff))
  6684. (run-hooks 'org-after-demote-entry-hook)))
  6685. (defun org-cycle-level ()
  6686. "Cycle the level of an empty headline through possible states.
  6687. This goes first to child, then to parent, level, then up the hierarchy.
  6688. After top level, it switches back to sibling level."
  6689. (interactive)
  6690. (let ((org-adapt-indentation nil))
  6691. (when (org-point-at-end-of-empty-headline)
  6692. (setq this-command 'org-cycle-level) ; Only needed for caching
  6693. (let ((cur-level (org-current-level))
  6694. (prev-level (org-get-previous-line-level)))
  6695. (cond
  6696. ;; If first headline in file, promote to top-level.
  6697. ((= prev-level 0)
  6698. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6699. do (org-do-promote)))
  6700. ;; If same level as prev, demote one.
  6701. ((= prev-level cur-level)
  6702. (org-do-demote))
  6703. ;; If parent is top-level, promote to top level if not already.
  6704. ((= prev-level 1)
  6705. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6706. do (org-do-promote)))
  6707. ;; If top-level, return to prev-level.
  6708. ((= cur-level 1)
  6709. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6710. do (org-do-demote)))
  6711. ;; If less than prev-level, promote one.
  6712. ((< cur-level prev-level)
  6713. (org-do-promote))
  6714. ;; If deeper than prev-level, promote until higher than
  6715. ;; prev-level.
  6716. ((> cur-level prev-level)
  6717. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6718. do (org-do-promote))))
  6719. t))))
  6720. (defun org-map-tree (fun)
  6721. "Call FUN for every heading underneath the current one."
  6722. (org-back-to-heading)
  6723. (let ((level (funcall outline-level)))
  6724. (save-excursion
  6725. (funcall fun)
  6726. (while (and (progn
  6727. (outline-next-heading)
  6728. (> (funcall outline-level) level))
  6729. (not (eobp)))
  6730. (funcall fun)))))
  6731. (defun org-map-region (fun beg end)
  6732. "Call FUN for every heading between BEG and END."
  6733. (let ((org-ignore-region t))
  6734. (save-excursion
  6735. (setq end (copy-marker end))
  6736. (goto-char beg)
  6737. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6738. (< (point) end))
  6739. (funcall fun))
  6740. (while (and (progn
  6741. (outline-next-heading)
  6742. (< (point) end))
  6743. (not (eobp)))
  6744. (funcall fun)))))
  6745. (defvar org-property-end-re) ; silence byte-compiler
  6746. (defun org-fixup-indentation (diff)
  6747. "Change the indentation in the current entry by DIFF.
  6748. However, if any line in the current entry has no indentation, or if it
  6749. would end up with no indentation after the change, nothing at all is done."
  6750. (save-excursion
  6751. (let ((end (save-excursion (outline-next-heading)
  6752. (point-marker)))
  6753. (prohibit (if (> diff 0)
  6754. "^\\S-"
  6755. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6756. col)
  6757. (unless (save-excursion (end-of-line 1)
  6758. (re-search-forward prohibit end t))
  6759. (while (and (< (point) end)
  6760. (re-search-forward "^[ \t]+" end t))
  6761. (goto-char (match-end 0))
  6762. (setq col (current-column))
  6763. (if (< diff 0) (replace-match ""))
  6764. (org-indent-to-column (+ diff col))))
  6765. (move-marker end nil))))
  6766. (defun org-convert-to-odd-levels ()
  6767. "Convert an org-mode file with all levels allowed to one with odd levels.
  6768. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6769. level 5 etc."
  6770. (interactive)
  6771. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6772. (let ((outline-level 'org-outline-level)
  6773. (org-odd-levels-only nil) n)
  6774. (save-excursion
  6775. (goto-char (point-min))
  6776. (while (re-search-forward "^\\*\\*+ " nil t)
  6777. (setq n (- (length (match-string 0)) 2))
  6778. (while (>= (setq n (1- n)) 0)
  6779. (org-demote))
  6780. (end-of-line 1))))))
  6781. (defun org-convert-to-oddeven-levels ()
  6782. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6783. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6784. file contains a section with an even level, conversion would
  6785. destroy the structure of the file. An error is signaled in this
  6786. case."
  6787. (interactive)
  6788. (goto-char (point-min))
  6789. ;; First check if there are no even levels
  6790. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6791. (org-show-context t)
  6792. (error "Not all levels are odd in this file. Conversion not possible"))
  6793. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6794. (let ((outline-regexp org-outline-regexp)
  6795. (outline-level 'org-outline-level)
  6796. (org-odd-levels-only nil) n)
  6797. (save-excursion
  6798. (goto-char (point-min))
  6799. (while (re-search-forward "^\\*\\*+ " nil t)
  6800. (setq n (/ (1- (length (match-string 0))) 2))
  6801. (while (>= (setq n (1- n)) 0)
  6802. (org-promote))
  6803. (end-of-line 1))))))
  6804. (defun org-tr-level (n)
  6805. "Make N odd if required."
  6806. (if org-odd-levels-only (1+ (/ n 2)) n))
  6807. ;;; Vertical tree motion, cutting and pasting of subtrees
  6808. (defun org-move-subtree-up (&optional arg)
  6809. "Move the current subtree up past ARG headlines of the same level."
  6810. (interactive "p")
  6811. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6812. (defun org-move-subtree-down (&optional arg)
  6813. "Move the current subtree down past ARG headlines of the same level."
  6814. (interactive "p")
  6815. (setq arg (prefix-numeric-value arg))
  6816. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6817. 'org-get-last-sibling))
  6818. (ins-point (make-marker))
  6819. (cnt (abs arg))
  6820. (col (current-column))
  6821. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6822. ;; Select the tree
  6823. (org-back-to-heading)
  6824. (setq beg0 (point))
  6825. (save-excursion
  6826. (setq ne-beg (org-back-over-empty-lines))
  6827. (setq beg (point)))
  6828. (save-match-data
  6829. (save-excursion (outline-end-of-heading)
  6830. (setq folded (outline-invisible-p)))
  6831. (outline-end-of-subtree))
  6832. (outline-next-heading)
  6833. (setq ne-end (org-back-over-empty-lines))
  6834. (setq end (point))
  6835. (goto-char beg0)
  6836. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6837. ;; include less whitespace
  6838. (save-excursion
  6839. (goto-char beg)
  6840. (forward-line (- ne-beg ne-end))
  6841. (setq beg (point))))
  6842. ;; Find insertion point, with error handling
  6843. (while (> cnt 0)
  6844. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6845. (progn (goto-char beg0)
  6846. (error "Cannot move past superior level or buffer limit")))
  6847. (setq cnt (1- cnt)))
  6848. (if (> arg 0)
  6849. ;; Moving forward - still need to move over subtree
  6850. (progn (org-end-of-subtree t t)
  6851. (save-excursion
  6852. (org-back-over-empty-lines)
  6853. (or (bolp) (newline)))))
  6854. (setq ne-ins (org-back-over-empty-lines))
  6855. (move-marker ins-point (point))
  6856. (setq txt (buffer-substring beg end))
  6857. (org-save-markers-in-region beg end)
  6858. (delete-region beg end)
  6859. (org-remove-empty-overlays-at beg)
  6860. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6861. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6862. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6863. (let ((bbb (point)))
  6864. (insert-before-markers txt)
  6865. (org-reinstall-markers-in-region bbb)
  6866. (move-marker ins-point bbb))
  6867. (or (bolp) (insert "\n"))
  6868. (setq ins-end (point))
  6869. (goto-char ins-point)
  6870. (org-skip-whitespace)
  6871. (when (and (< arg 0)
  6872. (org-first-sibling-p)
  6873. (> ne-ins ne-beg))
  6874. ;; Move whitespace back to beginning
  6875. (save-excursion
  6876. (goto-char ins-end)
  6877. (let ((kill-whole-line t))
  6878. (kill-line (- ne-ins ne-beg)) (point)))
  6879. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6880. (move-marker ins-point nil)
  6881. (if folded
  6882. (hide-subtree)
  6883. (org-show-entry)
  6884. (show-children)
  6885. (org-cycle-hide-drawers 'children))
  6886. (org-clean-visibility-after-subtree-move)
  6887. ;; move back to the initial column we were at
  6888. (move-to-column col)))
  6889. (defvar org-subtree-clip ""
  6890. "Clipboard for cut and paste of subtrees.
  6891. This is actually only a copy of the kill, because we use the normal kill
  6892. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6893. (defvar org-subtree-clip-folded nil
  6894. "Was the last copied subtree folded?
  6895. This is used to fold the tree back after pasting.")
  6896. (defun org-cut-subtree (&optional n)
  6897. "Cut the current subtree into the clipboard.
  6898. With prefix arg N, cut this many sequential subtrees.
  6899. This is a short-hand for marking the subtree and then cutting it."
  6900. (interactive "p")
  6901. (org-copy-subtree n 'cut))
  6902. (defun org-copy-subtree (&optional n cut force-store-markers)
  6903. "Cut the current subtree into the clipboard.
  6904. With prefix arg N, cut this many sequential subtrees.
  6905. This is a short-hand for marking the subtree and then copying it.
  6906. If CUT is non-nil, actually cut the subtree.
  6907. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6908. of some markers in the region, even if CUT is non-nil. This is
  6909. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6910. (interactive "p")
  6911. (let (beg end folded (beg0 (point)))
  6912. (if (org-called-interactively-p 'any)
  6913. (org-back-to-heading nil) ; take what looks like a subtree
  6914. (org-back-to-heading t)) ; take what is really there
  6915. (org-back-over-empty-lines)
  6916. (setq beg (point))
  6917. (skip-chars-forward " \t\r\n")
  6918. (save-match-data
  6919. (save-excursion (outline-end-of-heading)
  6920. (setq folded (outline-invisible-p)))
  6921. (condition-case nil
  6922. (org-forward-same-level (1- n) t)
  6923. (error nil))
  6924. (org-end-of-subtree t t))
  6925. (org-back-over-empty-lines)
  6926. (setq end (point))
  6927. (goto-char beg0)
  6928. (when (> end beg)
  6929. (setq org-subtree-clip-folded folded)
  6930. (when (or cut force-store-markers)
  6931. (org-save-markers-in-region beg end))
  6932. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6933. (setq org-subtree-clip (current-kill 0))
  6934. (message "%s: Subtree(s) with %d characters"
  6935. (if cut "Cut" "Copied")
  6936. (length org-subtree-clip)))))
  6937. (defun org-paste-subtree (&optional level tree for-yank)
  6938. "Paste the clipboard as a subtree, with modification of headline level.
  6939. The entire subtree is promoted or demoted in order to match a new headline
  6940. level.
  6941. If the cursor is at the beginning of a headline, the same level as
  6942. that headline is used to paste the tree
  6943. If not, the new level is derived from the *visible* headings
  6944. before and after the insertion point, and taken to be the inferior headline
  6945. level of the two. So if the previous visible heading is level 3 and the
  6946. next is level 4 (or vice versa), level 4 will be used for insertion.
  6947. This makes sure that the subtree remains an independent subtree and does
  6948. not swallow low level entries.
  6949. You can also force a different level, either by using a numeric prefix
  6950. argument, or by inserting the heading marker by hand. For example, if the
  6951. cursor is after \"*****\", then the tree will be shifted to level 5.
  6952. If optional TREE is given, use this text instead of the kill ring.
  6953. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6954. move back over whitespace before inserting, and move point to the end of
  6955. the inserted text when done."
  6956. (interactive "P")
  6957. (setq tree (or tree (and kill-ring (current-kill 0))))
  6958. (unless (org-kill-is-subtree-p tree)
  6959. (error "%s"
  6960. (substitute-command-keys
  6961. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6962. (org-with-limited-levels
  6963. (let* ((visp (not (outline-invisible-p)))
  6964. (txt tree)
  6965. (^re_ "\\(\\*+\\)[ \t]*")
  6966. (old-level (if (string-match org-outline-regexp-bol txt)
  6967. (- (match-end 0) (match-beginning 0) 1)
  6968. -1))
  6969. (force-level (cond (level (prefix-numeric-value level))
  6970. ((and (looking-at "[ \t]*$")
  6971. (string-match
  6972. "^\\*+$" (buffer-substring
  6973. (point-at-bol) (point))))
  6974. (- (match-end 1) (match-beginning 1)))
  6975. ((and (bolp)
  6976. (looking-at org-outline-regexp))
  6977. (- (match-end 0) (point) 1))
  6978. (t nil)))
  6979. (previous-level (save-excursion
  6980. (condition-case nil
  6981. (progn
  6982. (outline-previous-visible-heading 1)
  6983. (if (looking-at ^re_)
  6984. (- (match-end 0) (match-beginning 0) 1)
  6985. 1))
  6986. (error 1))))
  6987. (next-level (save-excursion
  6988. (condition-case nil
  6989. (progn
  6990. (or (looking-at org-outline-regexp)
  6991. (outline-next-visible-heading 1))
  6992. (if (looking-at ^re_)
  6993. (- (match-end 0) (match-beginning 0) 1)
  6994. 1))
  6995. (error 1))))
  6996. (new-level (or force-level (max previous-level next-level)))
  6997. (shift (if (or (= old-level -1)
  6998. (= new-level -1)
  6999. (= old-level new-level))
  7000. 0
  7001. (- new-level old-level)))
  7002. (delta (if (> shift 0) -1 1))
  7003. (func (if (> shift 0) 'org-demote 'org-promote))
  7004. (org-odd-levels-only nil)
  7005. beg end newend)
  7006. ;; Remove the forced level indicator
  7007. (if force-level
  7008. (delete-region (point-at-bol) (point)))
  7009. ;; Paste
  7010. (beginning-of-line (if (bolp) 1 2))
  7011. (unless for-yank (org-back-over-empty-lines))
  7012. (setq beg (point))
  7013. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7014. (insert-before-markers txt)
  7015. (unless (string-match "\n\\'" txt) (insert "\n"))
  7016. (setq newend (point))
  7017. (org-reinstall-markers-in-region beg)
  7018. (setq end (point))
  7019. (goto-char beg)
  7020. (skip-chars-forward " \t\n\r")
  7021. (setq beg (point))
  7022. (if (and (outline-invisible-p) visp)
  7023. (save-excursion (outline-show-heading)))
  7024. ;; Shift if necessary
  7025. (unless (= shift 0)
  7026. (save-restriction
  7027. (narrow-to-region beg end)
  7028. (while (not (= shift 0))
  7029. (org-map-region func (point-min) (point-max))
  7030. (setq shift (+ delta shift)))
  7031. (goto-char (point-min))
  7032. (setq newend (point-max))))
  7033. (when (or (org-called-interactively-p 'interactive) for-yank)
  7034. (message "Clipboard pasted as level %d subtree" new-level))
  7035. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7036. kill-ring
  7037. (eq org-subtree-clip (current-kill 0))
  7038. org-subtree-clip-folded)
  7039. ;; The tree was folded before it was killed/copied
  7040. (hide-subtree))
  7041. (and for-yank (goto-char newend)))))
  7042. (defun org-kill-is-subtree-p (&optional txt)
  7043. "Check if the current kill is an outline subtree, or a set of trees.
  7044. Returns nil if kill does not start with a headline, or if the first
  7045. headline level is not the largest headline level in the tree.
  7046. So this will actually accept several entries of equal levels as well,
  7047. which is OK for `org-paste-subtree'.
  7048. If optional TXT is given, check this string instead of the current kill."
  7049. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7050. (re (org-get-limited-outline-regexp))
  7051. (^re (concat "^" re))
  7052. (start-level (and kill
  7053. (string-match
  7054. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7055. kill)
  7056. (- (match-end 2) (match-beginning 2) 1)))
  7057. (start (1+ (or (match-beginning 2) -1))))
  7058. (if (not start-level)
  7059. (progn
  7060. nil) ;; does not even start with a heading
  7061. (catch 'exit
  7062. (while (setq start (string-match ^re kill (1+ start)))
  7063. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7064. (throw 'exit nil)))
  7065. t))))
  7066. (defvar org-markers-to-move nil
  7067. "Markers that should be moved with a cut-and-paste operation.
  7068. Those markers are stored together with their positions relative to
  7069. the start of the region.")
  7070. (defun org-save-markers-in-region (beg end)
  7071. "Check markers in region.
  7072. If these markers are between BEG and END, record their position relative
  7073. to BEG, so that after moving the block of text, we can put the markers back
  7074. into place.
  7075. This function gets called just before an entry or tree gets cut from the
  7076. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7077. called immediately, to move the markers with the entries."
  7078. (setq org-markers-to-move nil)
  7079. (when (featurep 'org-clock)
  7080. (org-clock-save-markers-for-cut-and-paste beg end))
  7081. (when (featurep 'org-agenda)
  7082. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7083. (defun org-check-and-save-marker (marker beg end)
  7084. "Check if MARKER is between BEG and END.
  7085. If yes, remember the marker and the distance to BEG."
  7086. (when (and (marker-buffer marker)
  7087. (equal (marker-buffer marker) (current-buffer)))
  7088. (if (and (>= marker beg) (< marker end))
  7089. (push (cons marker (- marker beg)) org-markers-to-move))))
  7090. (defun org-reinstall-markers-in-region (beg)
  7091. "Move all remembered markers to their position relative to BEG."
  7092. (mapc (lambda (x)
  7093. (move-marker (car x) (+ beg (cdr x))))
  7094. org-markers-to-move)
  7095. (setq org-markers-to-move nil))
  7096. (defun org-narrow-to-subtree ()
  7097. "Narrow buffer to the current subtree."
  7098. (interactive)
  7099. (save-excursion
  7100. (save-match-data
  7101. (org-with-limited-levels
  7102. (narrow-to-region
  7103. (progn (org-back-to-heading t) (point))
  7104. (progn (org-end-of-subtree t t)
  7105. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7106. (point)))))))
  7107. (defun org-narrow-to-block ()
  7108. "Narrow buffer to the current block."
  7109. (interactive)
  7110. (let* ((case-fold-search t)
  7111. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7112. "^[ \t]*#\\+end_.*")))
  7113. (if blockp
  7114. (narrow-to-region (car blockp) (cdr blockp))
  7115. (error "Not in a block"))))
  7116. (eval-when-compile
  7117. (defvar org-property-drawer-re))
  7118. (defvar org-property-start-re) ;; defined below
  7119. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7120. "Clone the task (subtree) at point N times.
  7121. The clones will be inserted as siblings.
  7122. In interactive use, the user will be prompted for the number of
  7123. clones to be produced, and for a time SHIFT, which may be a
  7124. repeater as used in time stamps, for example `+3d'.
  7125. When a valid repeater is given and the entry contains any time
  7126. stamps, the clones will become a sequence in time, with time
  7127. stamps in the subtree shifted for each clone produced. If SHIFT
  7128. is nil or the empty string, time stamps will be left alone. The
  7129. ID property of the original subtree is removed.
  7130. If the original subtree did contain time stamps with a repeater,
  7131. the following will happen:
  7132. - the repeater will be removed in each clone
  7133. - an additional clone will be produced, with the current, unshifted
  7134. date(s) in the entry.
  7135. - the original entry will be placed *after* all the clones, with
  7136. repeater intact.
  7137. - the start days in the repeater in the original entry will be shifted
  7138. to past the last clone.
  7139. In this way you can spell out a number of instances of a repeating task,
  7140. and still retain the repeater to cover future instances of the task."
  7141. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7142. (let (beg end template task idprop
  7143. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7144. (drawer-re org-drawer-regexp))
  7145. (if (not (and (integerp n) (> n 0)))
  7146. (error "Invalid number of replications %s" n))
  7147. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7148. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7149. shift)))
  7150. (error "Invalid shift specification %s" shift))
  7151. (when doshift
  7152. (setq shift-n (string-to-number (match-string 1 shift))
  7153. shift-what (cdr (assoc (match-string 2 shift)
  7154. '(("d" . day) ("w" . week)
  7155. ("m" . month) ("y" . year))))))
  7156. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7157. (setq nmin 1 nmax n)
  7158. (org-back-to-heading t)
  7159. (setq beg (point))
  7160. (setq idprop (org-entry-get nil "ID"))
  7161. (org-end-of-subtree t t)
  7162. (or (bolp) (insert "\n"))
  7163. (setq end (point))
  7164. (setq template (buffer-substring beg end))
  7165. (when (and doshift
  7166. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[dwmy][^<>\n]*>" template))
  7167. (delete-region beg end)
  7168. (setq end beg)
  7169. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7170. (goto-char end)
  7171. (loop for n from nmin to nmax do
  7172. ;; prepare clone
  7173. (with-temp-buffer
  7174. (insert template)
  7175. (org-mode)
  7176. (goto-char (point-min))
  7177. (org-show-subtree)
  7178. (and idprop (if org-clone-delete-id
  7179. (org-entry-delete nil "ID")
  7180. (org-id-get-create t)))
  7181. (unless (= n 0)
  7182. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7183. (kill-whole-line))
  7184. (goto-char (point-min))
  7185. (while (re-search-forward drawer-re nil t)
  7186. (mapc (lambda (d)
  7187. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7188. (goto-char (point-min))
  7189. (when doshift
  7190. (while (re-search-forward org-ts-regexp-both nil t)
  7191. (org-timestamp-change (* n shift-n) shift-what))
  7192. (unless (= n n-no-remove)
  7193. (goto-char (point-min))
  7194. (while (re-search-forward org-ts-regexp nil t)
  7195. (save-excursion
  7196. (goto-char (match-beginning 0))
  7197. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[dwmy]\\)")
  7198. (delete-region (match-beginning 1) (match-end 1)))))))
  7199. (setq task (buffer-string)))
  7200. (insert task))
  7201. (goto-char beg)))
  7202. ;;; Outline Sorting
  7203. (defun org-sort (with-case)
  7204. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7205. Optional argument WITH-CASE means sort case-sensitively."
  7206. (interactive "P")
  7207. (cond
  7208. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7209. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7210. (t
  7211. (org-call-with-arg 'org-sort-entries with-case))))
  7212. (defun org-sort-remove-invisible (s)
  7213. (remove-text-properties 0 (length s) org-rm-props s)
  7214. (while (string-match org-bracket-link-regexp s)
  7215. (setq s (replace-match (if (match-end 2)
  7216. (match-string 3 s)
  7217. (match-string 1 s)) t t s)))
  7218. s)
  7219. (defvar org-priority-regexp) ; defined later in the file
  7220. (defvar org-after-sorting-entries-or-items-hook nil
  7221. "Hook that is run after a bunch of entries or items have been sorted.
  7222. When children are sorted, the cursor is in the parent line when this
  7223. hook gets called. When a region or a plain list is sorted, the cursor
  7224. will be in the first entry of the sorted region/list.")
  7225. (defun org-sort-entries
  7226. (&optional with-case sorting-type getkey-func compare-func property)
  7227. "Sort entries on a certain level of an outline tree.
  7228. If there is an active region, the entries in the region are sorted.
  7229. Else, if the cursor is before the first entry, sort the top-level items.
  7230. Else, the children of the entry at point are sorted.
  7231. Sorting can be alphabetically, numerically, by date/time as given by
  7232. a time stamp, by a property or by priority.
  7233. The command prompts for the sorting type unless it has been given to the
  7234. function through the SORTING-TYPE argument, which needs to be a character,
  7235. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7236. precise meaning of each character:
  7237. n Numerically, by converting the beginning of the entry/item to a number.
  7238. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7239. t By date/time, either the first active time stamp in the entry, or, if
  7240. none exist, by the first inactive one.
  7241. s By the scheduled date/time.
  7242. d By deadline date/time.
  7243. c By creation time, which is assumed to be the first inactive time stamp
  7244. at the beginning of a line.
  7245. p By priority according to the cookie.
  7246. r By the value of a property.
  7247. Capital letters will reverse the sort order.
  7248. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7249. called with point at the beginning of the record. It must return either
  7250. a string or a number that should serve as the sorting key for that record.
  7251. Comparing entries ignores case by default. However, with an optional argument
  7252. WITH-CASE, the sorting considers case as well."
  7253. (interactive "P")
  7254. (let ((case-func (if with-case 'identity 'downcase))
  7255. start beg end stars re re2
  7256. txt what tmp)
  7257. ;; Find beginning and end of region to sort
  7258. (cond
  7259. ((org-region-active-p)
  7260. ;; we will sort the region
  7261. (setq end (region-end)
  7262. what "region")
  7263. (goto-char (region-beginning))
  7264. (if (not (org-at-heading-p)) (outline-next-heading))
  7265. (setq start (point)))
  7266. ((or (org-at-heading-p)
  7267. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7268. ;; we will sort the children of the current headline
  7269. (org-back-to-heading)
  7270. (setq start (point)
  7271. end (progn (org-end-of-subtree t t)
  7272. (or (bolp) (insert "\n"))
  7273. (org-back-over-empty-lines)
  7274. (point))
  7275. what "children")
  7276. (goto-char start)
  7277. (show-subtree)
  7278. (outline-next-heading))
  7279. (t
  7280. ;; we will sort the top-level entries in this file
  7281. (goto-char (point-min))
  7282. (or (org-at-heading-p) (outline-next-heading))
  7283. (setq start (point))
  7284. (goto-char (point-max))
  7285. (beginning-of-line 1)
  7286. (when (looking-at ".*?\\S-")
  7287. ;; File ends in a non-white line
  7288. (end-of-line 1)
  7289. (insert "\n"))
  7290. (setq end (point-max))
  7291. (setq what "top-level")
  7292. (goto-char start)
  7293. (show-all)))
  7294. (setq beg (point))
  7295. (if (>= beg end) (error "Nothing to sort"))
  7296. (looking-at "\\(\\*+\\)")
  7297. (setq stars (match-string 1)
  7298. re (concat "^" (regexp-quote stars) " +")
  7299. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7300. txt (buffer-substring beg end))
  7301. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7302. (if (and (not (equal stars "*")) (string-match re2 txt))
  7303. (error "Region to sort contains a level above the first entry"))
  7304. (unless sorting-type
  7305. (message
  7306. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7307. [t]ime [s]cheduled [d]eadline [c]reated
  7308. A/N/T/S/D/C/P/O/F means reversed:"
  7309. what)
  7310. (setq sorting-type (read-char-exclusive))
  7311. (and (= (downcase sorting-type) ?f)
  7312. (setq getkey-func
  7313. (org-icompleting-read "Sort using function: "
  7314. obarray 'fboundp t nil nil))
  7315. (setq getkey-func (intern getkey-func)))
  7316. (and (= (downcase sorting-type) ?r)
  7317. (setq property
  7318. (org-icompleting-read "Property: "
  7319. (mapcar 'list (org-buffer-property-keys t))
  7320. nil t))))
  7321. (message "Sorting entries...")
  7322. (save-restriction
  7323. (narrow-to-region start end)
  7324. (let ((dcst (downcase sorting-type))
  7325. (case-fold-search nil)
  7326. (now (current-time)))
  7327. (sort-subr
  7328. (/= dcst sorting-type)
  7329. ;; This function moves to the beginning character of the "record" to
  7330. ;; be sorted.
  7331. (lambda nil
  7332. (if (re-search-forward re nil t)
  7333. (goto-char (match-beginning 0))
  7334. (goto-char (point-max))))
  7335. ;; This function moves to the last character of the "record" being
  7336. ;; sorted.
  7337. (lambda nil
  7338. (save-match-data
  7339. (condition-case nil
  7340. (outline-forward-same-level 1)
  7341. (error
  7342. (goto-char (point-max))))))
  7343. ;; This function returns the value that gets sorted against.
  7344. (lambda nil
  7345. (cond
  7346. ((= dcst ?n)
  7347. (if (looking-at org-complex-heading-regexp)
  7348. (string-to-number (match-string 4))
  7349. nil))
  7350. ((= dcst ?a)
  7351. (if (looking-at org-complex-heading-regexp)
  7352. (funcall case-func (match-string 4))
  7353. nil))
  7354. ((= dcst ?t)
  7355. (let ((end (save-excursion (outline-next-heading) (point))))
  7356. (if (or (re-search-forward org-ts-regexp end t)
  7357. (re-search-forward org-ts-regexp-both end t))
  7358. (org-time-string-to-seconds (match-string 0))
  7359. (org-float-time now))))
  7360. ((= dcst ?c)
  7361. (let ((end (save-excursion (outline-next-heading) (point))))
  7362. (if (re-search-forward
  7363. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7364. end t)
  7365. (org-time-string-to-seconds (match-string 0))
  7366. (org-float-time now))))
  7367. ((= dcst ?s)
  7368. (let ((end (save-excursion (outline-next-heading) (point))))
  7369. (if (re-search-forward org-scheduled-time-regexp end t)
  7370. (org-time-string-to-seconds (match-string 1))
  7371. (org-float-time now))))
  7372. ((= dcst ?d)
  7373. (let ((end (save-excursion (outline-next-heading) (point))))
  7374. (if (re-search-forward org-deadline-time-regexp end t)
  7375. (org-time-string-to-seconds (match-string 1))
  7376. (org-float-time now))))
  7377. ((= dcst ?p)
  7378. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7379. (string-to-char (match-string 2))
  7380. org-default-priority))
  7381. ((= dcst ?r)
  7382. (or (org-entry-get nil property) ""))
  7383. ((= dcst ?o)
  7384. (if (looking-at org-complex-heading-regexp)
  7385. (- 9999 (length (member (match-string 2)
  7386. org-todo-keywords-1)))))
  7387. ((= dcst ?f)
  7388. (if getkey-func
  7389. (progn
  7390. (setq tmp (funcall getkey-func))
  7391. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7392. tmp)
  7393. (error "Invalid key function `%s'" getkey-func)))
  7394. (t (error "Invalid sorting type `%c'" sorting-type))))
  7395. nil
  7396. (cond
  7397. ((= dcst ?a) 'string<)
  7398. ((= dcst ?f) compare-func)
  7399. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7400. (t nil)))))
  7401. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7402. (message "Sorting entries...done")))
  7403. (defun org-do-sort (table what &optional with-case sorting-type)
  7404. "Sort TABLE of WHAT according to SORTING-TYPE.
  7405. The user will be prompted for the SORTING-TYPE if the call to this
  7406. function does not specify it. WHAT is only for the prompt, to indicate
  7407. what is being sorted. The sorting key will be extracted from
  7408. the car of the elements of the table.
  7409. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7410. (unless sorting-type
  7411. (message
  7412. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7413. what)
  7414. (setq sorting-type (read-char-exclusive)))
  7415. (let ((dcst (downcase sorting-type))
  7416. extractfun comparefun)
  7417. ;; Define the appropriate functions
  7418. (cond
  7419. ((= dcst ?n)
  7420. (setq extractfun 'string-to-number
  7421. comparefun (if (= dcst sorting-type) '< '>)))
  7422. ((= dcst ?a)
  7423. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7424. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7425. comparefun (if (= dcst sorting-type)
  7426. 'string<
  7427. (lambda (a b) (and (not (string< a b))
  7428. (not (string= a b)))))))
  7429. ((= dcst ?t)
  7430. (setq extractfun
  7431. (lambda (x)
  7432. (if (or (string-match org-ts-regexp x)
  7433. (string-match org-ts-regexp-both x))
  7434. (org-float-time
  7435. (org-time-string-to-time (match-string 0 x)))
  7436. 0))
  7437. comparefun (if (= dcst sorting-type) '< '>)))
  7438. (t (error "Invalid sorting type `%c'" sorting-type)))
  7439. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7440. table)
  7441. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7442. ;;; The orgstruct minor mode
  7443. ;; Define a minor mode which can be used in other modes in order to
  7444. ;; integrate the org-mode structure editing commands.
  7445. ;; This is really a hack, because the org-mode structure commands use
  7446. ;; keys which normally belong to the major mode. Here is how it
  7447. ;; works: The minor mode defines all the keys necessary to operate the
  7448. ;; structure commands, but wraps the commands into a function which
  7449. ;; tests if the cursor is currently at a headline or a plain list
  7450. ;; item. If that is the case, the structure command is used,
  7451. ;; temporarily setting many Org-mode variables like regular
  7452. ;; expressions for filling etc. However, when any of those keys is
  7453. ;; used at a different location, function uses `key-binding' to look
  7454. ;; up if the key has an associated command in another currently active
  7455. ;; keymap (minor modes, major mode, global), and executes that
  7456. ;; command. There might be problems if any of the keys is otherwise
  7457. ;; used as a prefix key.
  7458. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7459. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7460. ;; addresses this by checking explicitly for both bindings.
  7461. (defvar orgstruct-mode-map (make-sparse-keymap)
  7462. "Keymap for the minor `orgstruct-mode'.")
  7463. (defvar org-local-vars nil
  7464. "List of local variables, for use by `orgstruct-mode'.")
  7465. ;;;###autoload
  7466. (define-minor-mode orgstruct-mode
  7467. "Toggle the minor mode `orgstruct-mode'.
  7468. This mode is for using Org-mode structure commands in other
  7469. modes. The following keys behave as if Org-mode were active, if
  7470. the cursor is on a headline, or on a plain list item (both as
  7471. defined by Org-mode).
  7472. M-up Move entry/item up
  7473. M-down Move entry/item down
  7474. M-left Promote
  7475. M-right Demote
  7476. M-S-up Move entry/item up
  7477. M-S-down Move entry/item down
  7478. M-S-left Promote subtree
  7479. M-S-right Demote subtree
  7480. M-q Fill paragraph and items like in Org-mode
  7481. C-c ^ Sort entries
  7482. C-c - Cycle list bullet
  7483. TAB Cycle item visibility
  7484. M-RET Insert new heading/item
  7485. S-M-RET Insert new TODO heading / Checkbox item
  7486. C-c C-c Set tags / toggle checkbox"
  7487. nil " OrgStruct" nil
  7488. (org-load-modules-maybe)
  7489. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7490. ;;;###autoload
  7491. (defun turn-on-orgstruct ()
  7492. "Unconditionally turn on `orgstruct-mode'."
  7493. (orgstruct-mode 1))
  7494. (defun orgstruct++-mode (&optional arg)
  7495. "Toggle `orgstruct-mode', the enhanced version of it.
  7496. In addition to setting orgstruct-mode, this also exports all indentation
  7497. and autofilling variables from org-mode into the buffer. It will also
  7498. recognize item context in multiline items.
  7499. Note that turning off orgstruct-mode will *not* remove the
  7500. indentation/paragraph settings. This can only be done by refreshing the
  7501. major mode, for example with \\[normal-mode]."
  7502. (interactive "P")
  7503. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7504. (if (< arg 1)
  7505. (orgstruct-mode -1)
  7506. (orgstruct-mode 1)
  7507. (let (var val)
  7508. (mapc
  7509. (lambda (x)
  7510. (when (string-match
  7511. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7512. (symbol-name (car x)))
  7513. (setq var (car x) val (nth 1 x))
  7514. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7515. org-local-vars)
  7516. (org-set-local 'orgstruct-is-++ t))))
  7517. (defvar orgstruct-is-++ nil
  7518. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7519. (make-variable-buffer-local 'orgstruct-is-++)
  7520. ;;;###autoload
  7521. (defun turn-on-orgstruct++ ()
  7522. "Unconditionally turn on `orgstruct++-mode'."
  7523. (orgstruct++-mode 1))
  7524. (defun orgstruct-error ()
  7525. "Error when there is no default binding for a structure key."
  7526. (interactive)
  7527. (error "This key has no function outside structure elements"))
  7528. (defun orgstruct-setup ()
  7529. "Setup orgstruct keymaps."
  7530. (let ((nfunc 0)
  7531. (bindings
  7532. (list
  7533. '([(meta up)] org-metaup)
  7534. '([(meta down)] org-metadown)
  7535. '([(meta left)] org-metaleft)
  7536. '([(meta right)] org-metaright)
  7537. '([(meta shift up)] org-shiftmetaup)
  7538. '([(meta shift down)] org-shiftmetadown)
  7539. '([(meta shift left)] org-shiftmetaleft)
  7540. '([(meta shift right)] org-shiftmetaright)
  7541. '([?\e (up)] org-metaup)
  7542. '([?\e (down)] org-metadown)
  7543. '([?\e (left)] org-metaleft)
  7544. '([?\e (right)] org-metaright)
  7545. '([?\e (shift up)] org-shiftmetaup)
  7546. '([?\e (shift down)] org-shiftmetadown)
  7547. '([?\e (shift left)] org-shiftmetaleft)
  7548. '([?\e (shift right)] org-shiftmetaright)
  7549. '([(shift up)] org-shiftup)
  7550. '([(shift down)] org-shiftdown)
  7551. '([(shift left)] org-shiftleft)
  7552. '([(shift right)] org-shiftright)
  7553. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7554. '("\M-q" fill-paragraph)
  7555. '("\C-c^" org-sort)
  7556. '("\C-c-" org-cycle-list-bullet)))
  7557. elt key fun cmd)
  7558. (while (setq elt (pop bindings))
  7559. (setq nfunc (1+ nfunc))
  7560. (setq key (org-key (car elt))
  7561. fun (nth 1 elt)
  7562. cmd (orgstruct-make-binding fun nfunc key))
  7563. (org-defkey orgstruct-mode-map key cmd))
  7564. ;; Special treatment needed for TAB and RET
  7565. (org-defkey orgstruct-mode-map [(tab)]
  7566. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7567. (org-defkey orgstruct-mode-map "\C-i"
  7568. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7569. (org-defkey orgstruct-mode-map "\M-\C-m"
  7570. (orgstruct-make-binding 'org-insert-heading 105
  7571. "\M-\C-m" [(meta return)]))
  7572. (org-defkey orgstruct-mode-map [(meta return)]
  7573. (orgstruct-make-binding 'org-insert-heading 106
  7574. [(meta return)] "\M-\C-m"))
  7575. (org-defkey orgstruct-mode-map [(shift meta return)]
  7576. (orgstruct-make-binding 'org-insert-todo-heading 107
  7577. [(meta return)] "\M-\C-m"))
  7578. (org-defkey orgstruct-mode-map "\e\C-m"
  7579. (orgstruct-make-binding 'org-insert-heading 108
  7580. "\e\C-m" [?\e (return)]))
  7581. (org-defkey orgstruct-mode-map [?\e (return)]
  7582. (orgstruct-make-binding 'org-insert-heading 109
  7583. [?\e (return)] "\e\C-m"))
  7584. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7585. (orgstruct-make-binding 'org-insert-todo-heading 110
  7586. [?\e (return)] "\e\C-m"))
  7587. (unless org-local-vars
  7588. (setq org-local-vars (org-get-local-variables)))
  7589. t))
  7590. (defun orgstruct-make-binding (fun n &rest keys)
  7591. "Create a function for binding in the structure minor mode.
  7592. FUN is the command to call inside a table. N is used to create a unique
  7593. command name. KEYS are keys that should be checked in for a command
  7594. to execute outside of tables."
  7595. (eval
  7596. (list 'defun
  7597. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7598. '(arg)
  7599. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7600. "Outside of structure, run the binding of `"
  7601. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7602. "'.")
  7603. '(interactive "p")
  7604. (list 'if
  7605. `(org-context-p 'headline 'item
  7606. (and orgstruct-is-++
  7607. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7608. 'item-body))
  7609. (list 'org-run-like-in-org-mode (list 'quote fun))
  7610. (list 'let '(orgstruct-mode)
  7611. (list 'call-interactively
  7612. (append '(or)
  7613. (mapcar (lambda (k)
  7614. (list 'key-binding k))
  7615. keys)
  7616. '('orgstruct-error))))))))
  7617. (defun org-context-p (&rest contexts)
  7618. "Check if local context is any of CONTEXTS.
  7619. Possible values in the list of contexts are `table', `headline', and `item'."
  7620. (let ((pos (point)))
  7621. (goto-char (point-at-bol))
  7622. (prog1 (or (and (memq 'table contexts)
  7623. (looking-at "[ \t]*|"))
  7624. (and (memq 'headline contexts)
  7625. (looking-at org-outline-regexp))
  7626. (and (memq 'item contexts)
  7627. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7628. (and (memq 'item-body contexts)
  7629. (org-in-item-p)))
  7630. (goto-char pos))))
  7631. (defun org-get-local-variables ()
  7632. "Return a list of all local variables in an org-mode buffer."
  7633. (let (varlist)
  7634. (with-current-buffer (get-buffer-create "*Org tmp*")
  7635. (erase-buffer)
  7636. (org-mode)
  7637. (setq varlist (buffer-local-variables)))
  7638. (kill-buffer "*Org tmp*")
  7639. (delq nil
  7640. (mapcar
  7641. (lambda (x)
  7642. (setq x
  7643. (if (symbolp x)
  7644. (list x)
  7645. (list (car x) (list 'quote (cdr x)))))
  7646. (if (string-match
  7647. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7648. (symbol-name (car x)))
  7649. x nil))
  7650. varlist))))
  7651. (defun org-clone-local-variables (from-buffer &optional regexp)
  7652. "Clone local variables from FROM-BUFFER.
  7653. Optional argument REGEXP selects variables to clone."
  7654. (mapc
  7655. (lambda (pair)
  7656. (and (symbolp (car pair))
  7657. (or (null regexp)
  7658. (string-match regexp (symbol-name (car pair))))
  7659. (set (make-local-variable (car pair))
  7660. (cdr pair))))
  7661. (buffer-local-variables from-buffer)))
  7662. ;;;###autoload
  7663. (defun org-run-like-in-org-mode (cmd)
  7664. "Run a command, pretending that the current buffer is in Org-mode.
  7665. This will temporarily bind local variables that are typically bound in
  7666. Org-mode to the values they have in Org-mode, and then interactively
  7667. call CMD."
  7668. (org-load-modules-maybe)
  7669. (unless org-local-vars
  7670. (setq org-local-vars (org-get-local-variables)))
  7671. (eval (list 'let org-local-vars
  7672. (list 'call-interactively (list 'quote cmd)))))
  7673. ;;;; Archiving
  7674. (defun org-get-category (&optional pos force-refresh)
  7675. "Get the category applying to position POS."
  7676. (save-match-data
  7677. (if force-refresh (org-refresh-category-properties))
  7678. (let ((pos (or pos (point))))
  7679. (or (get-text-property pos 'org-category)
  7680. (progn (org-refresh-category-properties)
  7681. (get-text-property pos 'org-category))))))
  7682. (defun org-refresh-category-properties ()
  7683. "Refresh category text properties in the buffer."
  7684. (let ((def-cat (cond
  7685. ((null org-category)
  7686. (if buffer-file-name
  7687. (file-name-sans-extension
  7688. (file-name-nondirectory buffer-file-name))
  7689. "???"))
  7690. ((symbolp org-category) (symbol-name org-category))
  7691. (t org-category)))
  7692. beg end cat pos optionp)
  7693. (org-unmodified
  7694. (save-excursion
  7695. (save-restriction
  7696. (widen)
  7697. (goto-char (point-min))
  7698. (put-text-property (point) (point-max) 'org-category def-cat)
  7699. (while (re-search-forward
  7700. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7701. (setq pos (match-end 0)
  7702. optionp (equal (char-after (match-beginning 0)) ?#)
  7703. cat (org-trim (match-string 2)))
  7704. (if optionp
  7705. (setq beg (point-at-bol) end (point-max))
  7706. (org-back-to-heading t)
  7707. (setq beg (point) end (org-end-of-subtree t t)))
  7708. (put-text-property beg end 'org-category cat)
  7709. (put-text-property beg end 'org-category-position beg)
  7710. (goto-char pos)))))))
  7711. ;;;; Link Stuff
  7712. ;;; Link abbreviations
  7713. (defun org-link-expand-abbrev (link)
  7714. "Apply replacements as defined in `org-link-abbrev-alist."
  7715. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7716. (let* ((key (match-string 1 link))
  7717. (as (or (assoc key org-link-abbrev-alist-local)
  7718. (assoc key org-link-abbrev-alist)))
  7719. (tag (and (match-end 2) (match-string 3 link)))
  7720. rpl)
  7721. (if (not as)
  7722. link
  7723. (setq rpl (cdr as))
  7724. (cond
  7725. ((symbolp rpl) (funcall rpl tag))
  7726. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7727. ((string-match "%h" rpl)
  7728. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7729. (t (concat rpl tag)))))
  7730. link))
  7731. ;;; Storing and inserting links
  7732. (defvar org-insert-link-history nil
  7733. "Minibuffer history for links inserted with `org-insert-link'.")
  7734. (defvar org-stored-links nil
  7735. "Contains the links stored with `org-store-link'.")
  7736. (defvar org-store-link-plist nil
  7737. "Plist with info about the most recently link created with `org-store-link'.")
  7738. (defvar org-link-protocols nil
  7739. "Link protocols added to Org-mode using `org-add-link-type'.")
  7740. (defvar org-store-link-functions nil
  7741. "List of functions that are called to create and store a link.
  7742. Each function will be called in turn until one returns a non-nil
  7743. value. Each function should check if it is responsible for creating
  7744. this link (for example by looking at the major mode).
  7745. If not, it must exit and return nil.
  7746. If yes, it should return a non-nil value after a calling
  7747. `org-store-link-props' with a list of properties and values.
  7748. Special properties are:
  7749. :type The link prefix, like \"http\". This must be given.
  7750. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7751. This is obligatory as well.
  7752. :description Optional default description for the second pair
  7753. of brackets in an Org-mode link. The user can still change
  7754. this when inserting this link into an Org-mode buffer.
  7755. In addition to these, any additional properties can be specified
  7756. and then used in remember templates.")
  7757. (defun org-add-link-type (type &optional follow export)
  7758. "Add TYPE to the list of `org-link-types'.
  7759. Re-compute all regular expressions depending on `org-link-types'
  7760. FOLLOW and EXPORT are two functions.
  7761. FOLLOW should take the link path as the single argument and do whatever
  7762. is necessary to follow the link, for example find a file or display
  7763. a mail message.
  7764. EXPORT should format the link path for export to one of the export formats.
  7765. It should be a function accepting three arguments:
  7766. path the path of the link, the text after the prefix (like \"http:\")
  7767. desc the description of the link, if any, or a description added by
  7768. org-export-normalize-links if there is none
  7769. format the export format, a symbol like `html' or `latex' or `ascii'..
  7770. The function may use the FORMAT information to return different values
  7771. depending on the format. The return value will be put literally into
  7772. the exported file. If the return value is nil, this means Org should
  7773. do what it normally does with links which do not have EXPORT defined.
  7774. Org-mode has a built-in default for exporting links. If you are happy with
  7775. this default, there is no need to define an export function for the link
  7776. type. For a simple example of an export function, see `org-bbdb.el'."
  7777. (add-to-list 'org-link-types type t)
  7778. (org-make-link-regexps)
  7779. (if (assoc type org-link-protocols)
  7780. (setcdr (assoc type org-link-protocols) (list follow export))
  7781. (push (list type follow export) org-link-protocols)))
  7782. (defvar org-agenda-buffer-name)
  7783. ;;;###autoload
  7784. (defun org-store-link (arg)
  7785. "\\<org-mode-map>Store an org-link to the current location.
  7786. This link is added to `org-stored-links' and can later be inserted
  7787. into an org-buffer with \\[org-insert-link].
  7788. For some link types, a prefix arg is interpreted:
  7789. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7790. For file links, arg negates `org-context-in-file-links'."
  7791. (interactive "P")
  7792. (org-load-modules-maybe)
  7793. (setq org-store-link-plist nil) ; reset
  7794. (org-with-limited-levels
  7795. (let (link cpltxt desc description search txt custom-id agenda-link)
  7796. (cond
  7797. ((run-hook-with-args-until-success 'org-store-link-functions)
  7798. (setq link (plist-get org-store-link-plist :link)
  7799. desc (or (plist-get org-store-link-plist :description) link)))
  7800. ((equal (buffer-name) "*Org Edit Src Example*")
  7801. (let (label gc)
  7802. (while (or (not label)
  7803. (save-excursion
  7804. (save-restriction
  7805. (widen)
  7806. (goto-char (point-min))
  7807. (re-search-forward
  7808. (regexp-quote (format org-coderef-label-format label))
  7809. nil t))))
  7810. (when label (message "Label exists already") (sit-for 2))
  7811. (setq label (read-string "Code line label: " label)))
  7812. (end-of-line 1)
  7813. (setq link (format org-coderef-label-format label))
  7814. (setq gc (- 79 (length link)))
  7815. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7816. (insert link)
  7817. (setq link (concat "(" label ")") desc nil)))
  7818. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7819. ;; We are in the agenda, link to referenced location
  7820. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7821. (get-text-property (point) 'org-marker))))
  7822. (when m
  7823. (org-with-point-at m
  7824. (setq agenda-link
  7825. (if (org-called-interactively-p 'any)
  7826. (call-interactively 'org-store-link)
  7827. (org-store-link nil)))))))
  7828. ((eq major-mode 'calendar-mode)
  7829. (let ((cd (calendar-cursor-to-date)))
  7830. (setq link
  7831. (format-time-string
  7832. (car org-time-stamp-formats)
  7833. (apply 'encode-time
  7834. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7835. nil nil nil))))
  7836. (org-store-link-props :type "calendar" :date cd)))
  7837. ((eq major-mode 'w3-mode)
  7838. (setq cpltxt (if (and (buffer-name)
  7839. (not (string-match "Untitled" (buffer-name))))
  7840. (buffer-name)
  7841. (url-view-url t))
  7842. link (org-make-link (url-view-url t)))
  7843. (org-store-link-props :type "w3" :url (url-view-url t)))
  7844. ((eq major-mode 'w3m-mode)
  7845. (setq cpltxt (or w3m-current-title w3m-current-url)
  7846. link (org-make-link w3m-current-url))
  7847. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7848. ((setq search (run-hook-with-args-until-success
  7849. 'org-create-file-search-functions))
  7850. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7851. "::" search))
  7852. (setq cpltxt (or description link)))
  7853. ((eq major-mode 'image-mode)
  7854. (setq cpltxt (concat "file:"
  7855. (abbreviate-file-name buffer-file-name))
  7856. link (org-make-link cpltxt))
  7857. (org-store-link-props :type "image" :file buffer-file-name))
  7858. ((eq major-mode 'dired-mode)
  7859. ;; link to the file in the current line
  7860. (let ((file (dired-get-filename nil t)))
  7861. (setq file (if file
  7862. (abbreviate-file-name
  7863. (expand-file-name (dired-get-filename nil t)))
  7864. ;; otherwise, no file so use current directory.
  7865. default-directory))
  7866. (setq cpltxt (concat "file:" file)
  7867. link (org-make-link cpltxt))))
  7868. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7869. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7870. (cond
  7871. ((org-in-regexp "<<\\(.*?\\)>>")
  7872. (setq cpltxt
  7873. (concat "file:"
  7874. (abbreviate-file-name
  7875. (buffer-file-name (buffer-base-buffer)))
  7876. "::" (match-string 1))
  7877. link (org-make-link cpltxt)))
  7878. ((and (featurep 'org-id)
  7879. (or (eq org-link-to-org-use-id t)
  7880. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7881. (org-called-interactively-p 'any))
  7882. (and (eq org-link-to-org-use-id
  7883. 'create-if-interactive-and-no-custom-id)
  7884. (org-called-interactively-p 'any)
  7885. (not custom-id))
  7886. (and org-link-to-org-use-id
  7887. (org-entry-get nil "ID"))))
  7888. ;; We can make a link using the ID.
  7889. (setq link (condition-case nil
  7890. (prog1 (org-id-store-link)
  7891. (setq desc (plist-get org-store-link-plist
  7892. :description)))
  7893. (error
  7894. ;; probably before first headline, link to file only
  7895. (concat "file:"
  7896. (abbreviate-file-name
  7897. (buffer-file-name (buffer-base-buffer))))))))
  7898. (t
  7899. ;; Just link to current headline
  7900. (setq cpltxt (concat "file:"
  7901. (abbreviate-file-name
  7902. (buffer-file-name (buffer-base-buffer)))))
  7903. ;; Add a context search string
  7904. (when (org-xor org-context-in-file-links arg)
  7905. (setq txt (cond
  7906. ((org-at-heading-p) nil)
  7907. ((org-region-active-p)
  7908. (buffer-substring (region-beginning) (region-end)))
  7909. (t nil)))
  7910. (when (or (null txt) (string-match "\\S-" txt))
  7911. (setq cpltxt
  7912. (concat cpltxt "::"
  7913. (condition-case nil
  7914. (org-make-org-heading-search-string txt)
  7915. (error "")))
  7916. desc (or (nth 4 (ignore-errors
  7917. (org-heading-components))) "NONE"))))
  7918. (if (string-match "::\\'" cpltxt)
  7919. (setq cpltxt (substring cpltxt 0 -2)))
  7920. (setq link (org-make-link cpltxt)))))
  7921. ((buffer-file-name (buffer-base-buffer))
  7922. ;; Just link to this file here.
  7923. (setq cpltxt (concat "file:"
  7924. (abbreviate-file-name
  7925. (buffer-file-name (buffer-base-buffer)))))
  7926. ;; Add a context string
  7927. (when (org-xor org-context-in-file-links arg)
  7928. (setq txt (if (org-region-active-p)
  7929. (buffer-substring (region-beginning) (region-end))
  7930. (buffer-substring (point-at-bol) (point-at-eol))))
  7931. ;; Only use search option if there is some text.
  7932. (when (string-match "\\S-" txt)
  7933. (setq cpltxt
  7934. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7935. desc "NONE")))
  7936. (setq link (org-make-link cpltxt)))
  7937. ((org-called-interactively-p 'interactive)
  7938. (error "Cannot link to a buffer which is not visiting a file"))
  7939. (t (setq link nil)))
  7940. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7941. (setq link (or link cpltxt)
  7942. desc (or desc cpltxt))
  7943. (if (equal desc "NONE") (setq desc nil))
  7944. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7945. (progn
  7946. (setq org-stored-links
  7947. (cons (list link desc) org-stored-links))
  7948. (message "Stored: %s" (or desc link))
  7949. (when custom-id
  7950. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7951. "::#" custom-id))
  7952. (setq org-stored-links
  7953. (cons (list link desc) org-stored-links))))
  7954. (or agenda-link (and link (org-make-link-string link desc)))))))
  7955. (defun org-store-link-props (&rest plist)
  7956. "Store link properties, extract names and addresses."
  7957. (let (x adr)
  7958. (when (setq x (plist-get plist :from))
  7959. (setq adr (mail-extract-address-components x))
  7960. (setq plist (plist-put plist :fromname (car adr)))
  7961. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7962. (when (setq x (plist-get plist :to))
  7963. (setq adr (mail-extract-address-components x))
  7964. (setq plist (plist-put plist :toname (car adr)))
  7965. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7966. (let ((from (plist-get plist :from))
  7967. (to (plist-get plist :to)))
  7968. (when (and from to org-from-is-user-regexp)
  7969. (setq plist
  7970. (plist-put plist :fromto
  7971. (if (string-match org-from-is-user-regexp from)
  7972. (concat "to %t")
  7973. (concat "from %f"))))))
  7974. (setq org-store-link-plist plist))
  7975. (defun org-add-link-props (&rest plist)
  7976. "Add these properties to the link property list."
  7977. (let (key value)
  7978. (while plist
  7979. (setq key (pop plist) value (pop plist))
  7980. (setq org-store-link-plist
  7981. (plist-put org-store-link-plist key value)))))
  7982. (defun org-email-link-description (&optional fmt)
  7983. "Return the description part of an email link.
  7984. This takes information from `org-store-link-plist' and formats it
  7985. according to FMT (default from `org-email-link-description-format')."
  7986. (setq fmt (or fmt org-email-link-description-format))
  7987. (let* ((p org-store-link-plist)
  7988. (to (plist-get p :toaddress))
  7989. (from (plist-get p :fromaddress))
  7990. (table
  7991. (list
  7992. (cons "%c" (plist-get p :fromto))
  7993. (cons "%F" (plist-get p :from))
  7994. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7995. (cons "%T" (plist-get p :to))
  7996. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7997. (cons "%s" (plist-get p :subject))
  7998. (cons "%d" (plist-get p :date))
  7999. (cons "%m" (plist-get p :message-id)))))
  8000. (when (string-match "%c" fmt)
  8001. ;; Check if the user wrote this message
  8002. (if (and org-from-is-user-regexp from to
  8003. (save-match-data (string-match org-from-is-user-regexp from)))
  8004. (setq fmt (replace-match "to %t" t t fmt))
  8005. (setq fmt (replace-match "from %f" t t fmt))))
  8006. (org-replace-escapes fmt table)))
  8007. (defun org-make-org-heading-search-string (&optional string heading)
  8008. "Make search string for STRING or current headline."
  8009. (interactive)
  8010. (let ((s (or string (org-get-heading)))
  8011. (lines org-context-in-file-links))
  8012. (unless (and string (not heading))
  8013. ;; We are using a headline, clean up garbage in there.
  8014. (if (string-match org-todo-regexp s)
  8015. (setq s (replace-match "" t t s)))
  8016. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8017. (setq s (replace-match "" t t s)))
  8018. (setq s (org-trim s))
  8019. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8020. org-comment-string "\\)") s)
  8021. (setq s (replace-match "" t t s)))
  8022. (while (string-match org-ts-regexp s)
  8023. (setq s (replace-match "" t t s))))
  8024. (or string (setq s (concat "*" s))) ; Add * for headlines
  8025. (when (and string (integerp lines) (> lines 0))
  8026. (let ((slines (org-split-string s "\n")))
  8027. (when (< lines (length slines))
  8028. (setq s (mapconcat
  8029. 'identity
  8030. (reverse (nthcdr (- (length slines) lines)
  8031. (reverse slines))) "\n")))))
  8032. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8033. (defun org-make-link (&rest strings)
  8034. "Concatenate STRINGS."
  8035. (apply 'concat strings))
  8036. (defun org-make-link-string (link &optional description)
  8037. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8038. (unless (string-match "\\S-" link)
  8039. (error "Empty link"))
  8040. (when (and description
  8041. (stringp description)
  8042. (not (string-match "\\S-" description)))
  8043. (setq description nil))
  8044. (when (stringp description)
  8045. ;; Remove brackets from the description, they are fatal.
  8046. (while (string-match "\\[" description)
  8047. (setq description (replace-match "{" t t description)))
  8048. (while (string-match "\\]" description)
  8049. (setq description (replace-match "}" t t description))))
  8050. (when (equal link description)
  8051. ;; No description needed, it is identical
  8052. (setq description nil))
  8053. (when (and (not description)
  8054. (not (string-match (org-image-file-name-regexp) link))
  8055. (not (equal link (org-link-escape link))))
  8056. (setq description (org-extract-attributes link)))
  8057. (setq link
  8058. (cond ((string-match (org-image-file-name-regexp) link) link)
  8059. ((string-match org-link-types-re link)
  8060. (concat (match-string 1 link)
  8061. (org-link-escape (substring link (match-end 1)))))
  8062. (t (org-link-escape link))))
  8063. (concat "[[" link "]"
  8064. (if description (concat "[" description "]") "")
  8065. "]"))
  8066. (defconst org-link-escape-chars
  8067. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8068. "List of characters that should be escaped in link.
  8069. This is the list that is used for internal purposes.")
  8070. (defvar org-url-encoding-use-url-hexify nil)
  8071. (defconst org-link-escape-chars-browser
  8072. '(?\ )
  8073. "List of escapes for characters that are problematic in links.
  8074. This is the list that is used before handing over to the browser.")
  8075. (defun org-link-escape (text &optional table merge)
  8076. "Return percent escaped representation of TEXT.
  8077. TEXT is a string with the text to escape.
  8078. Optional argument TABLE is a list with characters that should be
  8079. escaped. When nil, `org-link-escape-chars' is used.
  8080. If optional argument MERGE is set, merge TABLE into
  8081. `org-link-escape-chars'."
  8082. (if (and org-url-encoding-use-url-hexify (not table))
  8083. (url-hexify-string text)
  8084. (cond
  8085. ((and table merge)
  8086. (mapc (lambda (defchr)
  8087. (unless (member defchr table)
  8088. (setq table (cons defchr table)))) org-link-escape-chars))
  8089. ((null table)
  8090. (setq table org-link-escape-chars)))
  8091. (mapconcat
  8092. (lambda (char)
  8093. (if (or (member char table)
  8094. (< char 32) (= char 37) (> char 126))
  8095. (mapconcat (lambda (sequence-element)
  8096. (format "%%%.2X" sequence-element))
  8097. (or (encode-coding-char char 'utf-8)
  8098. (error "Unable to percent escape character: %s"
  8099. (char-to-string char))) "")
  8100. (char-to-string char))) text "")))
  8101. (defun org-link-unescape (str)
  8102. "Unhex hexified Unicode strings as returned from the JavaScript function
  8103. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8104. (unless (and (null str) (string= "" str))
  8105. (let ((pos 0) (case-fold-search t) unhexed)
  8106. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8107. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8108. (setq str (replace-match unhexed t t str))
  8109. (setq pos (+ pos (length unhexed))))))
  8110. str)
  8111. (defun org-link-unescape-compound (hex)
  8112. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8113. Note: this function also decodes single byte encodings like
  8114. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8115. (save-match-data
  8116. (let* ((bytes (cdr (split-string hex "%")))
  8117. (ret "")
  8118. (eat 0)
  8119. (sum 0))
  8120. (while bytes
  8121. (let* ((val (string-to-number (pop bytes) 16))
  8122. (shift-xor
  8123. (if (= 0 eat)
  8124. (cond
  8125. ((>= val 252) (cons 6 252))
  8126. ((>= val 248) (cons 5 248))
  8127. ((>= val 240) (cons 4 240))
  8128. ((>= val 224) (cons 3 224))
  8129. ((>= val 192) (cons 2 192))
  8130. (t (cons 0 0)))
  8131. (cons 6 128))))
  8132. (if (>= val 192) (setq eat (car shift-xor)))
  8133. (setq val (logxor val (cdr shift-xor)))
  8134. (setq sum (+ (lsh sum (car shift-xor)) val))
  8135. (if (> eat 0) (setq eat (- eat 1)))
  8136. (cond
  8137. ((= 0 eat) ;multi byte
  8138. (setq ret (concat ret (org-char-to-string sum)))
  8139. (setq sum 0))
  8140. ((not bytes) ; single byte(s)
  8141. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8142. )) ;; end (while bytes
  8143. ret )))
  8144. (defun org-link-unescape-single-byte-sequence (hex)
  8145. "Unhexify hex-encoded single byte character sequences."
  8146. (mapconcat (lambda (byte)
  8147. (char-to-string (string-to-number byte 16)))
  8148. (cdr (split-string hex "%")) ""))
  8149. (defun org-xor (a b)
  8150. "Exclusive or."
  8151. (if a (not b) b))
  8152. (defun org-fixup-message-id-for-http (s)
  8153. "Replace special characters in a message id, so it can be used in an http query."
  8154. (when (string-match "%" s)
  8155. (setq s (mapconcat (lambda (c)
  8156. (if (eq c ?%)
  8157. "%25"
  8158. (char-to-string c)))
  8159. s "")))
  8160. (while (string-match "<" s)
  8161. (setq s (replace-match "%3C" t t s)))
  8162. (while (string-match ">" s)
  8163. (setq s (replace-match "%3E" t t s)))
  8164. (while (string-match "@" s)
  8165. (setq s (replace-match "%40" t t s)))
  8166. s)
  8167. ;;;###autoload
  8168. (defun org-insert-link-global ()
  8169. "Insert a link like Org-mode does.
  8170. This command can be called in any mode to insert a link in Org-mode syntax."
  8171. (interactive)
  8172. (org-load-modules-maybe)
  8173. (org-run-like-in-org-mode 'org-insert-link))
  8174. (defun org-insert-link (&optional complete-file link-location default-description)
  8175. "Insert a link. At the prompt, enter the link.
  8176. Completion can be used to insert any of the link protocol prefixes like
  8177. http or ftp in use.
  8178. The history can be used to select a link previously stored with
  8179. `org-store-link'. When the empty string is entered (i.e. if you just
  8180. press RET at the prompt), the link defaults to the most recently
  8181. stored link. As SPC triggers completion in the minibuffer, you need to
  8182. use M-SPC or C-q SPC to force the insertion of a space character.
  8183. You will also be prompted for a description, and if one is given, it will
  8184. be displayed in the buffer instead of the link.
  8185. If there is already a link at point, this command will allow you to edit link
  8186. and description parts.
  8187. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8188. be selected using completion. The path to the file will be relative to the
  8189. current directory if the file is in the current directory or a subdirectory.
  8190. Otherwise, the link will be the absolute path as completed in the minibuffer
  8191. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8192. option `org-link-file-path-type'.
  8193. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8194. the current directory or below.
  8195. With three \\[universal-argument] prefixes, negate the meaning of
  8196. `org-keep-stored-link-after-insertion'.
  8197. If `org-make-link-description-function' is non-nil, this function will be
  8198. called with the link target, and the result will be the default
  8199. link description.
  8200. If the LINK-LOCATION parameter is non-nil, this value will be
  8201. used as the link location instead of reading one interactively.
  8202. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8203. be used as the default description."
  8204. (interactive "P")
  8205. (let* ((wcf (current-window-configuration))
  8206. (region (if (org-region-active-p)
  8207. (buffer-substring (region-beginning) (region-end))))
  8208. (remove (and region (list (region-beginning) (region-end))))
  8209. (desc region)
  8210. tmphist ; byte-compile incorrectly complains about this
  8211. (link link-location)
  8212. entry file all-prefixes)
  8213. (cond
  8214. (link-location) ; specified by arg, just use it.
  8215. ((org-in-regexp org-bracket-link-regexp 1)
  8216. ;; We do have a link at point, and we are going to edit it.
  8217. (setq remove (list (match-beginning 0) (match-end 0)))
  8218. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8219. (setq link (read-string "Link: "
  8220. (org-link-unescape
  8221. (org-match-string-no-properties 1)))))
  8222. ((or (org-in-regexp org-angle-link-re)
  8223. (org-in-regexp org-plain-link-re))
  8224. ;; Convert to bracket link
  8225. (setq remove (list (match-beginning 0) (match-end 0))
  8226. link (read-string "Link: "
  8227. (org-remove-angle-brackets (match-string 0)))))
  8228. ((member complete-file '((4) (16)))
  8229. ;; Completing read for file names.
  8230. (setq link (org-file-complete-link complete-file)))
  8231. (t
  8232. ;; Read link, with completion for stored links.
  8233. (with-output-to-temp-buffer "*Org Links*"
  8234. (princ "Insert a link.
  8235. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8236. (when org-stored-links
  8237. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8238. (princ (mapconcat
  8239. (lambda (x)
  8240. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8241. (reverse org-stored-links) "\n"))))
  8242. (let ((cw (selected-window)))
  8243. (select-window (get-buffer-window "*Org Links*" 'visible))
  8244. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8245. (unless (pos-visible-in-window-p (point-max))
  8246. (org-fit-window-to-buffer))
  8247. (and (window-live-p cw) (select-window cw)))
  8248. ;; Fake a link history, containing the stored links.
  8249. (setq tmphist (append (mapcar 'car org-stored-links)
  8250. org-insert-link-history))
  8251. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8252. (mapcar 'car org-link-abbrev-alist)
  8253. org-link-types))
  8254. (unwind-protect
  8255. (progn
  8256. (setq link
  8257. (let ((org-completion-use-ido nil)
  8258. (org-completion-use-iswitchb nil))
  8259. (org-completing-read
  8260. "Link: "
  8261. (append
  8262. (mapcar (lambda (x) (list (concat x ":")))
  8263. all-prefixes)
  8264. (mapcar 'car org-stored-links))
  8265. nil nil nil
  8266. 'tmphist
  8267. (car (car org-stored-links)))))
  8268. (if (not (string-match "\\S-" link))
  8269. (error "No link selected"))
  8270. (if (or (member link all-prefixes)
  8271. (and (equal ":" (substring link -1))
  8272. (member (substring link 0 -1) all-prefixes)
  8273. (setq link (substring link 0 -1))))
  8274. (setq link (org-link-try-special-completion link))))
  8275. (set-window-configuration wcf)
  8276. (kill-buffer "*Org Links*"))
  8277. (setq entry (assoc link org-stored-links))
  8278. (or entry (push link org-insert-link-history))
  8279. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8280. (not org-keep-stored-link-after-insertion))
  8281. (setq org-stored-links (delq (assoc link org-stored-links)
  8282. org-stored-links)))
  8283. (setq desc (or desc (nth 1 entry)))))
  8284. (if (string-match org-plain-link-re link)
  8285. ;; URL-like link, normalize the use of angular brackets.
  8286. (setq link (org-make-link (org-remove-angle-brackets link))))
  8287. ;; Check if we are linking to the current file with a search option
  8288. ;; If yes, simplify the link by using only the search option.
  8289. (when (and buffer-file-name
  8290. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8291. (let* ((path (match-string 1 link))
  8292. (case-fold-search nil)
  8293. (search (match-string 2 link)))
  8294. (save-match-data
  8295. (if (equal (file-truename buffer-file-name) (file-truename path))
  8296. ;; We are linking to this same file, with a search option
  8297. (setq link search)))))
  8298. ;; Check if we can/should use a relative path. If yes, simplify the link
  8299. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8300. (let* ((type (match-string 1 link))
  8301. (path (match-string 2 link))
  8302. (origpath path)
  8303. (case-fold-search nil))
  8304. (cond
  8305. ((or (eq org-link-file-path-type 'absolute)
  8306. (equal complete-file '(16)))
  8307. (setq path (abbreviate-file-name (expand-file-name path))))
  8308. ((eq org-link-file-path-type 'noabbrev)
  8309. (setq path (expand-file-name path)))
  8310. ((eq org-link-file-path-type 'relative)
  8311. (setq path (file-relative-name path)))
  8312. (t
  8313. (save-match-data
  8314. (if (string-match (concat "^" (regexp-quote
  8315. (expand-file-name
  8316. (file-name-as-directory
  8317. default-directory))))
  8318. (expand-file-name path))
  8319. ;; We are linking a file with relative path name.
  8320. (setq path (substring (expand-file-name path)
  8321. (match-end 0)))
  8322. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8323. (setq link (concat type path))
  8324. (if (equal desc origpath)
  8325. (setq desc path))))
  8326. (if org-make-link-description-function
  8327. (setq desc (funcall org-make-link-description-function link desc))
  8328. (if default-description (setq desc default-description)))
  8329. (setq desc (read-string "Description: " desc))
  8330. (unless (string-match "\\S-" desc) (setq desc nil))
  8331. (if remove (apply 'delete-region remove))
  8332. (insert (org-make-link-string link desc))))
  8333. (defun org-link-try-special-completion (type)
  8334. "If there is completion support for link type TYPE, offer it."
  8335. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8336. (if (functionp fun)
  8337. (funcall fun)
  8338. (read-string "Link (no completion support): " (concat type ":")))))
  8339. (defun org-file-complete-link (&optional arg)
  8340. "Create a file link using completion."
  8341. (let (file link)
  8342. (setq file (read-file-name "File: "))
  8343. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8344. (pwd1 (file-name-as-directory (abbreviate-file-name
  8345. (expand-file-name ".")))))
  8346. (cond
  8347. ((equal arg '(16))
  8348. (setq link (org-make-link
  8349. "file:"
  8350. (abbreviate-file-name (expand-file-name file)))))
  8351. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8352. (setq link (org-make-link "file:" (match-string 1 file))))
  8353. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8354. (expand-file-name file))
  8355. (setq link (org-make-link
  8356. "file:" (match-string 1 (expand-file-name file)))))
  8357. (t (setq link (org-make-link "file:" file)))))
  8358. link))
  8359. (defun org-completing-read (&rest args)
  8360. "Completing-read with SPACE being a normal character."
  8361. (let ((enable-recursive-minibuffers t)
  8362. (minibuffer-local-completion-map
  8363. (copy-keymap minibuffer-local-completion-map)))
  8364. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8365. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8366. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8367. (apply 'org-icompleting-read args)))
  8368. (defun org-completing-read-no-i (&rest args)
  8369. (let (org-completion-use-ido org-completion-use-iswitchb)
  8370. (apply 'org-completing-read args)))
  8371. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8372. "Use iswitch as a completing-read replacement to choose from choices.
  8373. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8374. from."
  8375. (let* ((iswitchb-use-virtual-buffers nil)
  8376. (iswitchb-make-buflist-hook
  8377. (lambda ()
  8378. (setq iswitchb-temp-buflist choices))))
  8379. (iswitchb-read-buffer prompt)))
  8380. (defun org-icompleting-read (&rest args)
  8381. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8382. (org-without-partial-completion
  8383. (if (and org-completion-use-ido
  8384. (fboundp 'ido-completing-read)
  8385. (boundp 'ido-mode) ido-mode
  8386. (listp (second args)))
  8387. (let ((ido-enter-matching-directory nil))
  8388. (apply 'ido-completing-read (concat (car args))
  8389. (if (consp (car (nth 1 args)))
  8390. (mapcar 'car (nth 1 args))
  8391. (nth 1 args))
  8392. (cddr args)))
  8393. (if (and org-completion-use-iswitchb
  8394. (boundp 'iswitchb-mode) iswitchb-mode
  8395. (listp (second args)))
  8396. (apply 'org-iswitchb-completing-read (concat (car args))
  8397. (if (consp (car (nth 1 args)))
  8398. (mapcar 'car (nth 1 args))
  8399. (nth 1 args))
  8400. (cddr args))
  8401. (apply 'completing-read args)))))
  8402. (defun org-extract-attributes (s)
  8403. "Extract the attributes cookie from a string and set as text property."
  8404. (let (a attr (start 0) key value)
  8405. (save-match-data
  8406. (when (string-match "{{\\([^}]+\\)}}$" s)
  8407. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8408. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8409. (setq key (match-string 1 a) value (match-string 2 a)
  8410. start (match-end 0)
  8411. attr (plist-put attr (intern key) value))))
  8412. (org-add-props s nil 'org-attr attr))
  8413. s))
  8414. (defun org-extract-attributes-from-string (tag)
  8415. (let (key value attr)
  8416. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8417. (setq key (match-string 1 tag) value (match-string 2 tag)
  8418. tag (replace-match "" t t tag)
  8419. attr (plist-put attr (intern key) value)))
  8420. (cons tag attr)))
  8421. (defun org-attributes-to-string (plist)
  8422. "Format a property list into an HTML attribute list."
  8423. (let ((s "") key value)
  8424. (while plist
  8425. (setq key (pop plist) value (pop plist))
  8426. (and value
  8427. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8428. s))
  8429. ;;; Opening/following a link
  8430. (defvar org-link-search-failed nil)
  8431. (defvar org-open-link-functions nil
  8432. "Hook for functions finding a plain text link.
  8433. These functions must take a single argument, the link content.
  8434. They will be called for links that look like [[link text][description]]
  8435. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8436. like a filename (e.g. \"./blue.png\").
  8437. These functions will be called *before* Org attempts to resolve the
  8438. link by doing text searches in the current buffer - so if you want a
  8439. link \"[[target]]\" to still find \"<<target>>\", your function should
  8440. handle this as a special case.
  8441. When the function does handle the link, it must return a non-nil value.
  8442. If it decides that it is not responsible for this link, it must return
  8443. nil to indicate that that Org-mode can continue with other options
  8444. like exact and fuzzy text search.")
  8445. (defun org-next-link ()
  8446. "Move forward to the next link.
  8447. If the link is in hidden text, expose it."
  8448. (interactive)
  8449. (when (and org-link-search-failed (eq this-command last-command))
  8450. (goto-char (point-min))
  8451. (message "Link search wrapped back to beginning of buffer"))
  8452. (setq org-link-search-failed nil)
  8453. (let* ((pos (point))
  8454. (ct (org-context))
  8455. (a (assoc :link ct)))
  8456. (if a (goto-char (nth 2 a)))
  8457. (if (re-search-forward org-any-link-re nil t)
  8458. (progn
  8459. (goto-char (match-beginning 0))
  8460. (if (outline-invisible-p) (org-show-context)))
  8461. (goto-char pos)
  8462. (setq org-link-search-failed t)
  8463. (error "No further link found"))))
  8464. (defun org-previous-link ()
  8465. "Move backward to the previous link.
  8466. If the link is in hidden text, expose it."
  8467. (interactive)
  8468. (when (and org-link-search-failed (eq this-command last-command))
  8469. (goto-char (point-max))
  8470. (message "Link search wrapped back to end of buffer"))
  8471. (setq org-link-search-failed nil)
  8472. (let* ((pos (point))
  8473. (ct (org-context))
  8474. (a (assoc :link ct)))
  8475. (if a (goto-char (nth 1 a)))
  8476. (if (re-search-backward org-any-link-re nil t)
  8477. (progn
  8478. (goto-char (match-beginning 0))
  8479. (if (outline-invisible-p) (org-show-context)))
  8480. (goto-char pos)
  8481. (setq org-link-search-failed t)
  8482. (error "No further link found"))))
  8483. (defun org-translate-link (s)
  8484. "Translate a link string if a translation function has been defined."
  8485. (if (and org-link-translation-function
  8486. (fboundp org-link-translation-function)
  8487. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8488. (progn
  8489. (setq s (funcall org-link-translation-function
  8490. (match-string 1) (match-string 2)))
  8491. (concat (car s) ":" (cdr s)))
  8492. s))
  8493. (defun org-translate-link-from-planner (type path)
  8494. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8495. This is still an experimental function, your mileage may vary."
  8496. (cond
  8497. ((member type '("http" "https" "news" "ftp"))
  8498. ;; standard Internet links are the same.
  8499. nil)
  8500. ((and (equal type "irc") (string-match "^//" path))
  8501. ;; Planner has two / at the beginning of an irc link, we have 1.
  8502. ;; We should have zero, actually....
  8503. (setq path (substring path 1)))
  8504. ((and (equal type "lisp") (string-match "^/" path))
  8505. ;; Planner has a slash, we do not.
  8506. (setq type "elisp" path (substring path 1)))
  8507. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8508. ;; A typical message link. Planner has the id after the final slash,
  8509. ;; we separate it with a hash mark
  8510. (setq path (concat (match-string 1 path) "#"
  8511. (org-remove-angle-brackets (match-string 2 path)))))
  8512. )
  8513. (cons type path))
  8514. (defun org-find-file-at-mouse (ev)
  8515. "Open file link or URL at mouse."
  8516. (interactive "e")
  8517. (mouse-set-point ev)
  8518. (org-open-at-point 'in-emacs))
  8519. (defun org-open-at-mouse (ev)
  8520. "Open file link or URL at mouse.
  8521. See the docstring of `org-open-file' for details."
  8522. (interactive "e")
  8523. (mouse-set-point ev)
  8524. (if (eq major-mode 'org-agenda-mode)
  8525. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8526. (org-open-at-point))
  8527. (defvar org-window-config-before-follow-link nil
  8528. "The window configuration before following a link.
  8529. This is saved in case the need arises to restore it.")
  8530. (defvar org-open-link-marker (make-marker)
  8531. "Marker pointing to the location where `org-open-at-point; was called.")
  8532. ;;;###autoload
  8533. (defun org-open-at-point-global ()
  8534. "Follow a link like Org-mode does.
  8535. This command can be called in any mode to follow a link that has
  8536. Org-mode syntax."
  8537. (interactive)
  8538. (org-run-like-in-org-mode 'org-open-at-point))
  8539. ;;;###autoload
  8540. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8541. "Open a link in the string S, as if it was in Org-mode."
  8542. (interactive "sLink: \nP")
  8543. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8544. (with-temp-buffer
  8545. (let ((org-inhibit-startup (not reference-buffer)))
  8546. (org-mode)
  8547. (insert s)
  8548. (goto-char (point-min))
  8549. (when reference-buffer
  8550. (setq org-link-abbrev-alist-local
  8551. (with-current-buffer reference-buffer
  8552. org-link-abbrev-alist-local)))
  8553. (org-open-at-point arg reference-buffer)))))
  8554. (defvar org-open-at-point-functions nil
  8555. "Hook that is run when following a link at point.
  8556. Functions in this hook must return t if they identify and follow
  8557. a link at point. If they don't find anything interesting at point,
  8558. they must return nil.")
  8559. (defun org-open-at-point (&optional arg reference-buffer)
  8560. "Open link at or after point.
  8561. If there is no link at point, this function will search forward up to
  8562. the end of the current line.
  8563. Normally, files will be opened by an appropriate application. If the
  8564. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8565. With a double prefix argument, try to open outside of Emacs, in the
  8566. application the system uses for this file type."
  8567. (interactive "P")
  8568. ;; if in a code block, then open the block's results
  8569. (unless (call-interactively #'org-babel-open-src-block-result)
  8570. (org-load-modules-maybe)
  8571. (move-marker org-open-link-marker (point))
  8572. (setq org-window-config-before-follow-link (current-window-configuration))
  8573. (org-remove-occur-highlights nil nil t)
  8574. (cond
  8575. ((and (org-at-heading-p)
  8576. (not (org-in-regexp
  8577. (concat org-plain-link-re "\\|"
  8578. org-bracket-link-regexp "\\|"
  8579. org-angle-link-re "\\|"
  8580. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8581. (not (get-text-property (point) 'org-linked-text)))
  8582. (or (org-offer-links-in-entry arg)
  8583. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8584. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8585. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8586. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8587. (not (org-in-regexp org-bracket-link-regexp)))
  8588. (org-footnote-action))
  8589. (t
  8590. (let (type path link line search (pos (point)))
  8591. (catch 'match
  8592. (save-excursion
  8593. (skip-chars-forward "^]\n\r")
  8594. (when (org-in-regexp org-bracket-link-regexp 1)
  8595. (setq link (org-extract-attributes
  8596. (org-link-unescape (org-match-string-no-properties 1))))
  8597. (while (string-match " *\n *" link)
  8598. (setq link (replace-match " " t t link)))
  8599. (setq link (org-link-expand-abbrev link))
  8600. (cond
  8601. ((or (file-name-absolute-p link)
  8602. (string-match "^\\.\\.?/" link))
  8603. (setq type "file" path link))
  8604. ((string-match org-link-re-with-space3 link)
  8605. (setq type (match-string 1 link) path (match-string 2 link)))
  8606. (t (setq type "thisfile" path link)))
  8607. (throw 'match t)))
  8608. (when (get-text-property (point) 'org-linked-text)
  8609. (setq type "thisfile"
  8610. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8611. (1+ (point)) (point))
  8612. path (buffer-substring
  8613. (or (previous-single-property-change pos 'org-linked-text)
  8614. (point-min))
  8615. (or (next-single-property-change pos 'org-linked-text)
  8616. (point-max))))
  8617. (throw 'match t))
  8618. (save-excursion
  8619. (when (or (org-in-regexp org-angle-link-re)
  8620. (org-in-regexp org-plain-link-re))
  8621. (setq type (match-string 1)
  8622. path (org-link-unescape (match-string 2)))
  8623. (throw 'match t)))
  8624. (save-excursion
  8625. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8626. (setq type "tags"
  8627. path (match-string 1))
  8628. (while (string-match ":" path)
  8629. (setq path (replace-match "+" t t path)))
  8630. (throw 'match t)))
  8631. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8632. (setq type "tree-match"
  8633. path (match-string 1))
  8634. (throw 'match t)))
  8635. (unless path
  8636. (error "No link found"))
  8637. ;; switch back to reference buffer
  8638. ;; needed when if called in a temporary buffer through
  8639. ;; org-open-link-from-string
  8640. (with-current-buffer (or reference-buffer (current-buffer))
  8641. ;; Remove any trailing spaces in path
  8642. (if (string-match " +\\'" path)
  8643. (setq path (replace-match "" t t path)))
  8644. (if (and org-link-translation-function
  8645. (fboundp org-link-translation-function))
  8646. ;; Check if we need to translate the link
  8647. (let ((tmp (funcall org-link-translation-function type path)))
  8648. (setq type (car tmp) path (cdr tmp))))
  8649. (cond
  8650. ((assoc type org-link-protocols)
  8651. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8652. ((equal type "mailto")
  8653. (let ((cmd (car org-link-mailto-program))
  8654. (args (cdr org-link-mailto-program)) args1
  8655. (address path) (subject "") a)
  8656. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8657. (setq address (match-string 1 path)
  8658. subject (org-link-escape (match-string 2 path))))
  8659. (while args
  8660. (cond
  8661. ((not (stringp (car args))) (push (pop args) args1))
  8662. (t (setq a (pop args))
  8663. (if (string-match "%a" a)
  8664. (setq a (replace-match address t t a)))
  8665. (if (string-match "%s" a)
  8666. (setq a (replace-match subject t t a)))
  8667. (push a args1))))
  8668. (apply cmd (nreverse args1))))
  8669. ((member type '("http" "https" "ftp" "news"))
  8670. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8671. (org-link-escape
  8672. path org-link-escape-chars-browser)
  8673. path))))
  8674. ((string= type "doi")
  8675. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8676. (org-link-escape
  8677. path org-link-escape-chars-browser)
  8678. path))))
  8679. ((member type '("message"))
  8680. (browse-url (concat type ":" path)))
  8681. ((string= type "tags")
  8682. (org-tags-view arg path))
  8683. ((string= type "tree-match")
  8684. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8685. ((string= type "file")
  8686. (if (string-match "::\\([0-9]+\\)\\'" path)
  8687. (setq line (string-to-number (match-string 1 path))
  8688. path (substring path 0 (match-beginning 0)))
  8689. (if (string-match "::\\(.+\\)\\'" path)
  8690. (setq search (match-string 1 path)
  8691. path (substring path 0 (match-beginning 0)))))
  8692. (if (string-match "[*?{]" (file-name-nondirectory path))
  8693. (dired path)
  8694. (org-open-file path arg line search)))
  8695. ((string= type "shell")
  8696. (let ((cmd path))
  8697. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8698. (string-match org-confirm-shell-link-not-regexp cmd))
  8699. (not org-confirm-shell-link-function)
  8700. (funcall org-confirm-shell-link-function
  8701. (format "Execute \"%s\" in shell? "
  8702. (org-add-props cmd nil
  8703. 'face 'org-warning))))
  8704. (progn
  8705. (message "Executing %s" cmd)
  8706. (shell-command cmd))
  8707. (error "Abort"))))
  8708. ((string= type "elisp")
  8709. (let ((cmd path))
  8710. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8711. (string-match org-confirm-elisp-link-not-regexp cmd))
  8712. (not org-confirm-elisp-link-function)
  8713. (funcall org-confirm-elisp-link-function
  8714. (format "Execute \"%s\" as elisp? "
  8715. (org-add-props cmd nil
  8716. 'face 'org-warning))))
  8717. (message "%s => %s" cmd
  8718. (if (equal (string-to-char cmd) ?\()
  8719. (eval (read cmd))
  8720. (call-interactively (read cmd))))
  8721. (error "Abort"))))
  8722. ((and (string= type "thisfile")
  8723. (run-hook-with-args-until-success
  8724. 'org-open-link-functions path)))
  8725. ((string= type "thisfile")
  8726. (if arg
  8727. (switch-to-buffer-other-window
  8728. (org-get-buffer-for-internal-link (current-buffer)))
  8729. (org-mark-ring-push))
  8730. (let ((cmd `(org-link-search
  8731. ,path
  8732. ,(cond ((equal arg '(4)) ''occur)
  8733. ((equal arg '(16)) ''org-occur)
  8734. (t nil))
  8735. ,pos)))
  8736. (condition-case nil (let ((org-link-search-inhibit-query t))
  8737. (eval cmd))
  8738. (error (progn (widen) (eval cmd))))))
  8739. (t
  8740. (browse-url-at-point)))))))
  8741. (move-marker org-open-link-marker nil)
  8742. (run-hook-with-args 'org-follow-link-hook)))
  8743. (defun org-offer-links-in-entry (&optional nth zero)
  8744. "Offer links in the current entry and follow the selected link.
  8745. If there is only one link, follow it immediately as well.
  8746. If NTH is an integer, immediately pick the NTH link found.
  8747. If ZERO is a string, check also this string for a link, and if
  8748. there is one, offer it as link number zero."
  8749. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8750. "\\(" org-angle-link-re "\\)\\|"
  8751. "\\(" org-plain-link-re "\\)"))
  8752. (cnt ?0)
  8753. (in-emacs (if (integerp nth) nil nth))
  8754. have-zero end links link c)
  8755. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8756. (push (match-string 0 zero) links)
  8757. (setq cnt (1- cnt) have-zero t))
  8758. (save-excursion
  8759. (org-back-to-heading t)
  8760. (setq end (save-excursion (outline-next-heading) (point)))
  8761. (while (re-search-forward re end t)
  8762. (push (match-string 0) links))
  8763. (setq links (org-uniquify (reverse links))))
  8764. (cond
  8765. ((null links)
  8766. (message "No links"))
  8767. ((equal (length links) 1)
  8768. (setq link (list (car links))))
  8769. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8770. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8771. (t ; we have to select a link
  8772. (save-excursion
  8773. (save-window-excursion
  8774. (delete-other-windows)
  8775. (with-output-to-temp-buffer "*Select Link*"
  8776. (mapc (lambda (l)
  8777. (if (not (string-match org-bracket-link-regexp l))
  8778. (princ (format "[%c] %s\n" (incf cnt)
  8779. (org-remove-angle-brackets l)))
  8780. (if (match-end 3)
  8781. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8782. (match-string 3 l) (match-string 1 l)))
  8783. (princ (format "[%c] %s\n" (incf cnt)
  8784. (match-string 1 l))))))
  8785. links))
  8786. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8787. (message "Select link to open, RET to open all:")
  8788. (setq c (read-char-exclusive))
  8789. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8790. (when (equal c ?q) (error "Abort"))
  8791. (if (equal c ?\C-m)
  8792. (setq link links)
  8793. (setq nth (- c ?0))
  8794. (if have-zero (setq nth (1+ nth)))
  8795. (unless (and (integerp nth) (>= (length links) nth))
  8796. (error "Invalid link selection"))
  8797. (setq link (list (nth (1- nth) links))))))
  8798. (if link
  8799. (let ((buf (current-buffer)))
  8800. (dolist (l link)
  8801. (org-open-link-from-string l in-emacs buf))
  8802. t)
  8803. nil)))
  8804. ;; Add special file links that specify the way of opening
  8805. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8806. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8807. (defun org-open-file-with-system (path)
  8808. "Open file at PATH using the system way of opening it."
  8809. (org-open-file path 'system))
  8810. (defun org-open-file-with-emacs (path)
  8811. "Open file at PATH in Emacs."
  8812. (org-open-file path 'emacs))
  8813. (defun org-remove-file-link-modifiers ()
  8814. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8815. (goto-char (point-min))
  8816. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8817. (org-if-unprotected
  8818. (replace-match "file:" t t))))
  8819. (eval-after-load "org-exp"
  8820. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8821. 'org-remove-file-link-modifiers))
  8822. ;;;; Time estimates
  8823. (defun org-get-effort (&optional pom)
  8824. "Get the effort estimate for the current entry."
  8825. (org-entry-get pom org-effort-property))
  8826. ;;; File search
  8827. (defvar org-create-file-search-functions nil
  8828. "List of functions to construct the right search string for a file link.
  8829. These functions are called in turn with point at the location to
  8830. which the link should point.
  8831. A function in the hook should first test if it would like to
  8832. handle this file type, for example by checking the `major-mode'
  8833. or the file extension. If it decides not to handle this file, it
  8834. should just return nil to give other functions a chance. If it
  8835. does handle the file, it must return the search string to be used
  8836. when following the link. The search string will be part of the
  8837. file link, given after a double colon, and `org-open-at-point'
  8838. will automatically search for it. If special measures must be
  8839. taken to make the search successful, another function should be
  8840. added to the companion hook `org-execute-file-search-functions',
  8841. which see.
  8842. A function in this hook may also use `setq' to set the variable
  8843. `description' to provide a suggestion for the descriptive text to
  8844. be used for this link when it gets inserted into an Org-mode
  8845. buffer with \\[org-insert-link].")
  8846. (defvar org-execute-file-search-functions nil
  8847. "List of functions to execute a file search triggered by a link.
  8848. Functions added to this hook must accept a single argument, the
  8849. search string that was part of the file link, the part after the
  8850. double colon. The function must first check if it would like to
  8851. handle this search, for example by checking the `major-mode' or
  8852. the file extension. If it decides not to handle this search, it
  8853. should just return nil to give other functions a chance. If it
  8854. does handle the search, it must return a non-nil value to keep
  8855. other functions from trying.
  8856. Each function can access the current prefix argument through the
  8857. variable `current-prefix-argument'. Note that a single prefix is
  8858. used to force opening a link in Emacs, so it may be good to only
  8859. use a numeric or double prefix to guide the search function.
  8860. In case this is needed, a function in this hook can also restore
  8861. the window configuration before `org-open-at-point' was called using:
  8862. (set-window-configuration org-window-config-before-follow-link)")
  8863. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8864. (defun org-link-search (s &optional type avoid-pos stealth)
  8865. "Search for a link search option.
  8866. If S is surrounded by forward slashes, it is interpreted as a
  8867. regular expression. In org-mode files, this will create an `org-occur'
  8868. sparse tree. In ordinary files, `occur' will be used to list matches.
  8869. If the current buffer is in `dired-mode', grep will be used to search
  8870. in all files. If AVOID-POS is given, ignore matches near that position.
  8871. When optional argument STEALTH is non-nil, do not modify
  8872. visibility around point, thus ignoring
  8873. `org-show-hierarchy-above', `org-show-following-heading' and
  8874. `org-show-siblings' variables."
  8875. (let ((case-fold-search t)
  8876. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8877. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8878. (append '(("") (" ") ("\t") ("\n"))
  8879. org-emphasis-alist)
  8880. "\\|") "\\)"))
  8881. (pos (point))
  8882. (pre nil) (post nil)
  8883. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8884. (cond
  8885. ;; First check if there are any special search functions
  8886. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8887. ;; Now try the builtin stuff
  8888. ((and (equal (string-to-char s0) ?#)
  8889. (> (length s0) 1)
  8890. (save-excursion
  8891. (goto-char (point-min))
  8892. (and
  8893. (re-search-forward
  8894. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8895. (setq type 'dedicated
  8896. pos (match-beginning 0))))
  8897. ;; There is an exact target for this
  8898. (goto-char pos)
  8899. (org-back-to-heading t)))
  8900. ((save-excursion
  8901. (goto-char (point-min))
  8902. (and
  8903. (re-search-forward
  8904. (concat "<<" (regexp-quote s0) ">>") nil t)
  8905. (setq type 'dedicated
  8906. pos (match-beginning 0))))
  8907. ;; There is an exact target for this
  8908. (goto-char pos))
  8909. ((save-excursion
  8910. (goto-char (point-min))
  8911. (and
  8912. (re-search-forward
  8913. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  8914. (setq type 'dedicated pos (match-beginning 0))))
  8915. ;; Found an invisible target.
  8916. (goto-char pos))
  8917. ((save-excursion
  8918. (goto-char (point-min))
  8919. (and
  8920. (re-search-forward
  8921. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  8922. (setq type 'dedicated pos (match-beginning 0))))
  8923. ;; Found an element with a matching #+name affiliated keyword.
  8924. (goto-char pos))
  8925. ((and (string-match "^(\\(.*\\))$" s0)
  8926. (save-excursion
  8927. (goto-char (point-min))
  8928. (and
  8929. (re-search-forward
  8930. (concat "[^[]" (regexp-quote
  8931. (format org-coderef-label-format
  8932. (match-string 1 s0))))
  8933. nil t)
  8934. (setq type 'dedicated
  8935. pos (1+ (match-beginning 0))))))
  8936. ;; There is a coderef target for this
  8937. (goto-char pos))
  8938. ((string-match "^/\\(.*\\)/$" s)
  8939. ;; A regular expression
  8940. (cond
  8941. ((eq major-mode 'org-mode)
  8942. (org-occur (match-string 1 s)))
  8943. ;;((eq major-mode 'dired-mode)
  8944. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8945. (t (org-do-occur (match-string 1 s)))))
  8946. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8947. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8948. (goto-char (point-min))
  8949. (cond
  8950. ((let (case-fold-search)
  8951. (re-search-forward (format org-complex-heading-regexp-format
  8952. (regexp-quote s))
  8953. nil t))
  8954. ;; OK, found a match
  8955. (setq type 'dedicated)
  8956. (goto-char (match-beginning 0)))
  8957. ((and (not org-link-search-inhibit-query)
  8958. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8959. (y-or-n-p "No match - create this as a new heading? "))
  8960. (goto-char (point-max))
  8961. (or (bolp) (newline))
  8962. (insert "* " s "\n")
  8963. (beginning-of-line 0))
  8964. (t
  8965. (goto-char pos)
  8966. (error "No match"))))
  8967. (t
  8968. ;; A normal search string
  8969. (when (equal (string-to-char s) ?*)
  8970. ;; Anchor on headlines, post may include tags.
  8971. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8972. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8973. s (substring s 1)))
  8974. (remove-text-properties
  8975. 0 (length s)
  8976. '(face nil mouse-face nil keymap nil fontified nil) s)
  8977. ;; Make a series of regular expressions to find a match
  8978. (setq words (org-split-string s "[ \n\r\t]+")
  8979. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8980. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8981. "\\)" markers)
  8982. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8983. re2a (concat "[ \t\r\n]" re2a_)
  8984. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8985. re4 (concat "[^a-zA-Z_]" re4_)
  8986. re1 (concat pre re2 post)
  8987. re3 (concat pre (if pre re4_ re4) post)
  8988. re5 (concat pre ".*" re4)
  8989. re2 (concat pre re2)
  8990. re2a (concat pre (if pre re2a_ re2a))
  8991. re4 (concat pre (if pre re4_ re4))
  8992. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8993. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8994. re5 "\\)"
  8995. ))
  8996. (cond
  8997. ((eq type 'org-occur) (org-occur reall))
  8998. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8999. (t (goto-char (point-min))
  9000. (setq type 'fuzzy)
  9001. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9002. (org-search-not-self 1 re1 nil t)
  9003. (org-search-not-self 1 re2 nil t)
  9004. (org-search-not-self 1 re2a nil t)
  9005. (org-search-not-self 1 re3 nil t)
  9006. (org-search-not-self 1 re4 nil t)
  9007. (org-search-not-self 1 re5 nil t)
  9008. )
  9009. (goto-char (match-beginning 1))
  9010. (goto-char pos)
  9011. (error "No match"))))))
  9012. (and (eq major-mode 'org-mode)
  9013. (not stealth)
  9014. (org-show-context 'link-search))
  9015. type))
  9016. (defun org-search-not-self (group &rest args)
  9017. "Execute `re-search-forward', but only accept matches that do not
  9018. enclose the position of `org-open-link-marker'."
  9019. (let ((m org-open-link-marker))
  9020. (catch 'exit
  9021. (while (apply 're-search-forward args)
  9022. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9023. (goto-char (match-end group))
  9024. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9025. (> (match-beginning 0) (marker-position m))
  9026. (< (match-end 0) (marker-position m)))
  9027. (save-match-data
  9028. (or (not (org-in-regexp
  9029. org-bracket-link-analytic-regexp 1))
  9030. (not (match-end 4)) ; no description
  9031. (and (<= (match-beginning 4) (point))
  9032. (>= (match-end 4) (point))))))
  9033. (throw 'exit (point))))))))
  9034. (defun org-get-buffer-for-internal-link (buffer)
  9035. "Return a buffer to be used for displaying the link target of internal links."
  9036. (cond
  9037. ((not org-display-internal-link-with-indirect-buffer)
  9038. buffer)
  9039. ((string-match "(Clone)$" (buffer-name buffer))
  9040. (message "Buffer is already a clone, not making another one")
  9041. ;; we also do not modify visibility in this case
  9042. buffer)
  9043. (t ; make a new indirect buffer for displaying the link
  9044. (let* ((bn (buffer-name buffer))
  9045. (ibn (concat bn "(Clone)"))
  9046. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9047. (with-current-buffer ib (org-overview))
  9048. ib))))
  9049. (defun org-do-occur (regexp &optional cleanup)
  9050. "Call the Emacs command `occur'.
  9051. If CLEANUP is non-nil, remove the printout of the regular expression
  9052. in the *Occur* buffer. This is useful if the regex is long and not useful
  9053. to read."
  9054. (occur regexp)
  9055. (when cleanup
  9056. (let ((cwin (selected-window)) win beg end)
  9057. (when (setq win (get-buffer-window "*Occur*"))
  9058. (select-window win))
  9059. (goto-char (point-min))
  9060. (when (re-search-forward "match[a-z]+" nil t)
  9061. (setq beg (match-end 0))
  9062. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9063. (setq end (1- (match-beginning 0)))))
  9064. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9065. (goto-char (point-min))
  9066. (select-window cwin))))
  9067. ;;; The mark ring for links jumps
  9068. (defvar org-mark-ring nil
  9069. "Mark ring for positions before jumps in Org-mode.")
  9070. (defvar org-mark-ring-last-goto nil
  9071. "Last position in the mark ring used to go back.")
  9072. ;; Fill and close the ring
  9073. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9074. (loop for i from 1 to org-mark-ring-length do
  9075. (push (make-marker) org-mark-ring))
  9076. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9077. org-mark-ring)
  9078. (defun org-mark-ring-push (&optional pos buffer)
  9079. "Put the current position or POS into the mark ring and rotate it."
  9080. (interactive)
  9081. (setq pos (or pos (point)))
  9082. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9083. (move-marker (car org-mark-ring)
  9084. (or pos (point))
  9085. (or buffer (current-buffer)))
  9086. (message "%s"
  9087. (substitute-command-keys
  9088. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9089. (defun org-mark-ring-goto (&optional n)
  9090. "Jump to the previous position in the mark ring.
  9091. With prefix arg N, jump back that many stored positions. When
  9092. called several times in succession, walk through the entire ring.
  9093. Org-mode commands jumping to a different position in the current file,
  9094. or to another Org-mode file, automatically push the old position
  9095. onto the ring."
  9096. (interactive "p")
  9097. (let (p m)
  9098. (if (eq last-command this-command)
  9099. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9100. (setq p org-mark-ring))
  9101. (setq org-mark-ring-last-goto p)
  9102. (setq m (car p))
  9103. (org-pop-to-buffer-same-window (marker-buffer m))
  9104. (goto-char m)
  9105. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9106. (defun org-remove-angle-brackets (s)
  9107. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9108. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9109. s)
  9110. (defun org-add-angle-brackets (s)
  9111. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9112. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9113. s)
  9114. (defun org-remove-double-quotes (s)
  9115. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9116. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9117. s)
  9118. ;;; Following specific links
  9119. (defun org-follow-timestamp-link ()
  9120. (cond
  9121. ((org-at-date-range-p t)
  9122. (let ((org-agenda-start-on-weekday)
  9123. (t1 (match-string 1))
  9124. (t2 (match-string 2)))
  9125. (setq t1 (time-to-days (org-time-string-to-time t1))
  9126. t2 (time-to-days (org-time-string-to-time t2)))
  9127. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9128. ((org-at-timestamp-p t)
  9129. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9130. (substring (match-string 1) 0 10)))
  9131. 1))
  9132. (t (error "This should not happen"))))
  9133. ;;; Following file links
  9134. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9135. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9136. (declare-function mailcap-mime-info
  9137. "mailcap" (string &optional request no-decode))
  9138. (defvar org-wait nil)
  9139. (defun org-open-file (path &optional in-emacs line search)
  9140. "Open the file at PATH.
  9141. First, this expands any special file name abbreviations. Then the
  9142. configuration variable `org-file-apps' is checked if it contains an
  9143. entry for this file type, and if yes, the corresponding command is launched.
  9144. If no application is found, Emacs simply visits the file.
  9145. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9146. With a double \\[universal-argument] \\[universal-argument] \
  9147. prefix arg, Org tries to avoid opening in Emacs
  9148. and to use an external application to visit the file.
  9149. Optional LINE specifies a line to go to, optional SEARCH a string
  9150. to search for. If LINE or SEARCH is given, the file will be
  9151. opened in Emacs, unless an entry from org-file-apps that makes
  9152. use of groups in a regexp matches.
  9153. If you want to change the way frames are used when following a
  9154. link, please customize `org-link-frame-setup'.
  9155. If the file does not exist, an error is thrown."
  9156. (let* ((file (if (equal path "")
  9157. buffer-file-name
  9158. (substitute-in-file-name (expand-file-name path))))
  9159. (file-apps (append org-file-apps (org-default-apps)))
  9160. (apps (org-remove-if
  9161. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9162. (apps-dlink (org-remove-if-not
  9163. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9164. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9165. (dirp (if remp nil (file-directory-p file)))
  9166. (file (if (and dirp org-open-directory-means-index-dot-org)
  9167. (concat (file-name-as-directory file) "index.org")
  9168. file))
  9169. (a-m-a-p (assq 'auto-mode apps))
  9170. (dfile (downcase file))
  9171. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9172. (link (cond ((and (eq line nil)
  9173. (eq search nil))
  9174. file)
  9175. (line
  9176. (concat file "::" (number-to-string line)))
  9177. (search
  9178. (concat file "::" search))))
  9179. (dlink (downcase link))
  9180. (old-buffer (current-buffer))
  9181. (old-pos (point))
  9182. (old-mode major-mode)
  9183. ext cmd link-match-data)
  9184. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9185. (setq ext (match-string 1 dfile))
  9186. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9187. (setq ext (match-string 1 dfile))))
  9188. (cond
  9189. ((member in-emacs '((16) system))
  9190. (setq cmd (cdr (assoc 'system apps))))
  9191. (in-emacs (setq cmd 'emacs))
  9192. (t
  9193. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9194. (and dirp (cdr (assoc 'directory apps)))
  9195. ; first, try matching against apps-dlink
  9196. ; if we get a match here, store the match data for later
  9197. (let ((match (assoc-default dlink apps-dlink
  9198. 'string-match)))
  9199. (if match
  9200. (progn (setq link-match-data (match-data))
  9201. match)
  9202. (progn (setq in-emacs (or in-emacs line search))
  9203. nil))) ; if we have no match in apps-dlink,
  9204. ; always open the file in emacs if line or search
  9205. ; is given (for backwards compatibility)
  9206. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9207. 'string-match)
  9208. (cdr (assoc ext apps))
  9209. (cdr (assoc t apps))))))
  9210. (when (eq cmd 'system)
  9211. (setq cmd (cdr (assoc 'system apps))))
  9212. (when (eq cmd 'default)
  9213. (setq cmd (cdr (assoc t apps))))
  9214. (when (eq cmd 'mailcap)
  9215. (require 'mailcap)
  9216. (mailcap-parse-mailcaps)
  9217. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9218. (command (mailcap-mime-info mime-type)))
  9219. (if (stringp command)
  9220. (setq cmd command)
  9221. (setq cmd 'emacs))))
  9222. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9223. (not (file-exists-p file))
  9224. (not org-open-non-existing-files))
  9225. (error "No such file: %s" file))
  9226. (cond
  9227. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9228. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9229. (while (string-match "['\"]%s['\"]" cmd)
  9230. (setq cmd (replace-match "%s" t t cmd)))
  9231. (while (string-match "%s" cmd)
  9232. (setq cmd (replace-match
  9233. (save-match-data
  9234. (shell-quote-argument
  9235. (convert-standard-filename file)))
  9236. t t cmd)))
  9237. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9238. (save-match-data
  9239. (let ((match-index 1)
  9240. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9241. (set-match-data link-match-data)
  9242. (while (<= match-index number-of-groups)
  9243. (let ((regex (concat "%" (number-to-string match-index)))
  9244. (replace-with (match-string match-index dlink)))
  9245. (while (string-match regex cmd)
  9246. (setq cmd (replace-match replace-with t t cmd))))
  9247. (setq match-index (+ match-index 1)))))
  9248. (save-window-excursion
  9249. (start-process-shell-command cmd nil cmd)
  9250. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9251. ))
  9252. ((or (stringp cmd)
  9253. (eq cmd 'emacs))
  9254. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9255. (widen)
  9256. (if line (org-goto-line line)
  9257. (if search (org-link-search search))))
  9258. ((consp cmd)
  9259. (let ((file (convert-standard-filename file)))
  9260. (save-match-data
  9261. (set-match-data link-match-data)
  9262. (eval cmd))))
  9263. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9264. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9265. (or (not (equal old-buffer (current-buffer)))
  9266. (not (equal old-pos (point))))
  9267. (org-mark-ring-push old-pos old-buffer))))
  9268. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9269. "This function returns non-nil if `entry' uses a regular
  9270. expression which should be matched against the whole link by
  9271. org-open-file.
  9272. It assumes that is the case when the entry uses a regular
  9273. expression which has at least one grouping construct and the
  9274. action is either a lisp form or a command string containing
  9275. '%1', i.e. using at least one subexpression match as a
  9276. parameter."
  9277. (let ((selector (car entry))
  9278. (action (cdr entry)))
  9279. (if (stringp selector)
  9280. (and (> (regexp-opt-depth selector) 0)
  9281. (or (and (stringp action)
  9282. (string-match "%[0-9]" action))
  9283. (consp action)))
  9284. nil)))
  9285. (defun org-default-apps ()
  9286. "Return the default applications for this operating system."
  9287. (cond
  9288. ((eq system-type 'darwin)
  9289. org-file-apps-defaults-macosx)
  9290. ((eq system-type 'windows-nt)
  9291. org-file-apps-defaults-windowsnt)
  9292. (t org-file-apps-defaults-gnu)))
  9293. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9294. "Convert extensions to regular expressions in the cars of LIST.
  9295. Also, weed out any non-string entries, because the return value is used
  9296. only for regexp matching.
  9297. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9298. point to the symbol `emacs', indicating that the file should
  9299. be opened in Emacs."
  9300. (append
  9301. (delq nil
  9302. (mapcar (lambda (x)
  9303. (if (not (stringp (car x)))
  9304. nil
  9305. (if (string-match "\\W" (car x))
  9306. x
  9307. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9308. list))
  9309. (if add-auto-mode
  9310. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9311. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9312. (defun org-file-remote-p (file)
  9313. "Test whether FILE specifies a location on a remote system.
  9314. Return non-nil if the location is indeed remote.
  9315. For example, the filename \"/user@host:/foo\" specifies a location
  9316. on the system \"/user@host:\"."
  9317. (cond ((fboundp 'file-remote-p)
  9318. (file-remote-p file))
  9319. ((fboundp 'tramp-handle-file-remote-p)
  9320. (tramp-handle-file-remote-p file))
  9321. ((and (boundp 'ange-ftp-name-format)
  9322. (string-match (car ange-ftp-name-format) file))
  9323. t)
  9324. (t nil)))
  9325. ;;;; Refiling
  9326. (defun org-get-org-file ()
  9327. "Read a filename, with default directory `org-directory'."
  9328. (let ((default (or org-default-notes-file remember-data-file)))
  9329. (read-file-name (format "File name [%s]: " default)
  9330. (file-name-as-directory org-directory)
  9331. default)))
  9332. (defun org-notes-order-reversed-p ()
  9333. "Check if the current file should receive notes in reversed order."
  9334. (cond
  9335. ((not org-reverse-note-order) nil)
  9336. ((eq t org-reverse-note-order) t)
  9337. ((not (listp org-reverse-note-order)) nil)
  9338. (t (catch 'exit
  9339. (let ((all org-reverse-note-order)
  9340. entry)
  9341. (while (setq entry (pop all))
  9342. (if (string-match (car entry) buffer-file-name)
  9343. (throw 'exit (cdr entry))))
  9344. nil)))))
  9345. (defvar org-refile-target-table nil
  9346. "The list of refile targets, created by `org-refile'.")
  9347. (defvar org-agenda-new-buffers nil
  9348. "Buffers created to visit agenda files.")
  9349. (defvar org-refile-cache nil
  9350. "Cache for refile targets.")
  9351. (defvar org-refile-markers nil
  9352. "All the markers used for caching refile locations.")
  9353. (defun org-refile-marker (pos)
  9354. "Get a new refile marker, but only if caching is in use."
  9355. (if (not org-refile-use-cache)
  9356. pos
  9357. (let ((m (make-marker)))
  9358. (move-marker m pos)
  9359. (push m org-refile-markers)
  9360. m)))
  9361. (defun org-refile-cache-clear ()
  9362. "Clear the refile cache and disable all the markers."
  9363. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9364. (setq org-refile-markers nil)
  9365. (setq org-refile-cache nil)
  9366. (message "Refile cache has been cleared"))
  9367. (defun org-refile-cache-check-set (set)
  9368. "Check if all the markers in the cache still have live buffers."
  9369. (let (marker)
  9370. (catch 'exit
  9371. (while (and set (setq marker (nth 3 (pop set))))
  9372. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9373. (when (and marker (null (marker-buffer marker)))
  9374. (message "not found") (sit-for 3)
  9375. (throw 'exit nil)))
  9376. t)))
  9377. (defun org-refile-cache-put (set &rest identifiers)
  9378. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9379. (let* ((key (sha1 (prin1-to-string identifiers)))
  9380. (entry (assoc key org-refile-cache)))
  9381. (if entry
  9382. (setcdr entry set)
  9383. (push (cons key set) org-refile-cache))))
  9384. (defun org-refile-cache-get (&rest identifiers)
  9385. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9386. (cond
  9387. ((not org-refile-cache) nil)
  9388. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9389. (t
  9390. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9391. org-refile-cache))))
  9392. (and set (org-refile-cache-check-set set) set)))))
  9393. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9394. "Produce a table with refile targets."
  9395. (let ((case-fold-search nil)
  9396. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9397. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9398. targets tgs txt re files f desc descre fast-path-p level pos0)
  9399. (message "Getting targets...")
  9400. (with-current-buffer (or default-buffer (current-buffer))
  9401. (while (setq entry (pop entries))
  9402. (setq files (car entry) desc (cdr entry))
  9403. (setq fast-path-p nil)
  9404. (cond
  9405. ((null files) (setq files (list (current-buffer))))
  9406. ((eq files 'org-agenda-files)
  9407. (setq files (org-agenda-files 'unrestricted)))
  9408. ((and (symbolp files) (fboundp files))
  9409. (setq files (funcall files)))
  9410. ((and (symbolp files) (boundp files))
  9411. (setq files (symbol-value files))))
  9412. (if (stringp files) (setq files (list files)))
  9413. (cond
  9414. ((eq (car desc) :tag)
  9415. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9416. ((eq (car desc) :todo)
  9417. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9418. ((eq (car desc) :regexp)
  9419. (setq descre (cdr desc)))
  9420. ((eq (car desc) :level)
  9421. (setq descre (concat "^\\*\\{" (number-to-string
  9422. (if org-odd-levels-only
  9423. (1- (* 2 (cdr desc)))
  9424. (cdr desc)))
  9425. "\\}[ \t]")))
  9426. ((eq (car desc) :maxlevel)
  9427. (setq fast-path-p t)
  9428. (setq descre (concat "^\\*\\{1," (number-to-string
  9429. (if org-odd-levels-only
  9430. (1- (* 2 (cdr desc)))
  9431. (cdr desc)))
  9432. "\\}[ \t]")))
  9433. (t (error "Bad refiling target description %s" desc)))
  9434. (while (setq f (pop files))
  9435. (with-current-buffer
  9436. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9437. (or
  9438. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9439. (progn
  9440. (if (bufferp f) (setq f (buffer-file-name
  9441. (buffer-base-buffer f))))
  9442. (setq f (and f (expand-file-name f)))
  9443. (if (eq org-refile-use-outline-path 'file)
  9444. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9445. (save-excursion
  9446. (save-restriction
  9447. (widen)
  9448. (goto-char (point-min))
  9449. (while (re-search-forward descre nil t)
  9450. (goto-char (setq pos0 (point-at-bol)))
  9451. (catch 'next
  9452. (when org-refile-target-verify-function
  9453. (save-match-data
  9454. (or (funcall org-refile-target-verify-function)
  9455. (throw 'next t))))
  9456. (when (and (looking-at org-complex-heading-regexp)
  9457. (not (member (match-string 4) excluded-entries))
  9458. (match-string 4))
  9459. (setq level (org-reduced-level
  9460. (- (match-end 1) (match-beginning 1)))
  9461. txt (org-link-display-format (match-string 4))
  9462. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9463. re (format org-complex-heading-regexp-format
  9464. (regexp-quote (match-string 4))))
  9465. (when org-refile-use-outline-path
  9466. (setq txt (mapconcat
  9467. 'org-protect-slash
  9468. (append
  9469. (if (eq org-refile-use-outline-path
  9470. 'file)
  9471. (list (file-name-nondirectory
  9472. (buffer-file-name
  9473. (buffer-base-buffer))))
  9474. (if (eq org-refile-use-outline-path
  9475. 'full-file-path)
  9476. (list (buffer-file-name
  9477. (buffer-base-buffer)))))
  9478. (org-get-outline-path fast-path-p
  9479. level txt)
  9480. (list txt))
  9481. "/")))
  9482. (push (list txt f re (org-refile-marker (point)))
  9483. tgs)))
  9484. (when (= (point) pos0)
  9485. ;; verification function has not moved point
  9486. (goto-char (point-at-eol))))))))
  9487. (when org-refile-use-cache
  9488. (org-refile-cache-put tgs (buffer-file-name) descre))
  9489. (setq targets (append tgs targets))
  9490. ))))
  9491. (message "Getting targets...done")
  9492. (nreverse targets)))
  9493. (defun org-protect-slash (s)
  9494. (while (string-match "/" s)
  9495. (setq s (replace-match "\\" t t s)))
  9496. s)
  9497. (defvar org-olpa (make-vector 20 nil))
  9498. (defun org-get-outline-path (&optional fastp level heading)
  9499. "Return the outline path to the current entry, as a list.
  9500. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9501. routine which makes outline path derivations for an entire file,
  9502. avoiding backtracing. Refile target collection makes use of that."
  9503. (if fastp
  9504. (progn
  9505. (if (> level 19)
  9506. (error "Outline path failure, more than 19 levels"))
  9507. (loop for i from level upto 19 do
  9508. (aset org-olpa i nil))
  9509. (prog1
  9510. (delq nil (append org-olpa nil))
  9511. (aset org-olpa level heading)))
  9512. (let (rtn case-fold-search)
  9513. (save-excursion
  9514. (save-restriction
  9515. (widen)
  9516. (while (org-up-heading-safe)
  9517. (when (looking-at org-complex-heading-regexp)
  9518. (push (org-match-string-no-properties 4) rtn)))
  9519. rtn)))))
  9520. (defun org-format-outline-path (path &optional width prefix)
  9521. "Format the outline path PATH for display.
  9522. Width is the maximum number of characters that is available.
  9523. Prefix is a prefix to be included in the returned string,
  9524. such as the file name."
  9525. (setq width (or width 79))
  9526. (if prefix (setq width (- width (length prefix))))
  9527. (if (not path)
  9528. (or prefix "")
  9529. (let* ((nsteps (length path))
  9530. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9531. (maxwidth (if (<= total-width width)
  9532. 10000 ;; everything fits
  9533. ;; we need to shorten the level headings
  9534. (/ (- width nsteps) nsteps)))
  9535. (org-odd-levels-only nil)
  9536. (n 0)
  9537. (total (1+ (length prefix))))
  9538. (setq maxwidth (max maxwidth 10))
  9539. (concat prefix
  9540. (mapconcat
  9541. (lambda (h)
  9542. (setq n (1+ n))
  9543. (if (and (= n nsteps) (< maxwidth 10000))
  9544. (setq maxwidth (- total-width total)))
  9545. (if (< (length h) maxwidth)
  9546. (progn (setq total (+ total (length h) 1)) h)
  9547. (setq h (substring h 0 (- maxwidth 2))
  9548. total (+ total maxwidth 1))
  9549. (if (string-match "[ \t]+\\'" h)
  9550. (setq h (substring h 0 (match-beginning 0))))
  9551. (setq h (concat h "..")))
  9552. (org-add-props h nil 'face
  9553. (nth (% (1- n) org-n-level-faces)
  9554. org-level-faces))
  9555. h)
  9556. path "/")))))
  9557. (defun org-display-outline-path (&optional file current)
  9558. "Display the current outline path in the echo area."
  9559. (interactive "P")
  9560. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9561. (case-fold-search nil)
  9562. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9563. (if current (setq path (append path
  9564. (save-excursion
  9565. (org-back-to-heading t)
  9566. (if (looking-at org-complex-heading-regexp)
  9567. (list (match-string 4)))))))
  9568. (message "%s"
  9569. (org-format-outline-path
  9570. path
  9571. (1- (frame-width))
  9572. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9573. (defvar org-refile-history nil
  9574. "History for refiling operations.")
  9575. (defvar org-after-refile-insert-hook nil
  9576. "Hook run after `org-refile' has inserted its stuff at the new location.
  9577. Note that this is still *before* the stuff will be removed from
  9578. the *old* location.")
  9579. (defvar org-capture-last-stored-marker)
  9580. (defun org-refile (&optional goto default-buffer rfloc)
  9581. "Move the entry or entries at point to another heading.
  9582. The list of target headings is compiled using the information in
  9583. `org-refile-targets', which see.
  9584. At the target location, the entry is filed as a subitem of the target
  9585. heading. Depending on `org-reverse-note-order', the new subitem will
  9586. either be the first or the last subitem.
  9587. If there is an active region, all entries in that region will be moved.
  9588. However, the region must fulfill the requirement that the first heading
  9589. is the first one sets the top-level of the moved text - at most siblings
  9590. below it are allowed.
  9591. With prefix arg GOTO, the command will only visit the target location
  9592. and not actually move anything.
  9593. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9594. go to the location where the last refiling operation has put the subtree.
  9595. With a prefix argument of `2', refile to the running clock.
  9596. RFLOC can be a refile location obtained in a different way.
  9597. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9598. If you are using target caching (see `org-refile-use-cache'),
  9599. You have to clear the target cache in order to find new targets.
  9600. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9601. prefix argument (`C-u C-u C-u C-c C-w')."
  9602. (interactive "P")
  9603. (if (member goto '(0 (64)))
  9604. (org-refile-cache-clear)
  9605. (let* ((cbuf (current-buffer))
  9606. (regionp (org-region-active-p))
  9607. (region-start (and regionp (region-beginning)))
  9608. (region-end (and regionp (region-end)))
  9609. (region-length (and regionp (- region-end region-start)))
  9610. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9611. pos it nbuf file re level reversed)
  9612. (setq last-command nil)
  9613. (when regionp
  9614. (goto-char region-start)
  9615. (or (bolp) (goto-char (point-at-bol)))
  9616. (setq region-start (point))
  9617. (unless (or (org-kill-is-subtree-p
  9618. (buffer-substring region-start region-end))
  9619. (prog1 org-refile-active-region-within-subtree
  9620. (org-toggle-heading)))
  9621. (error "The region is not a (sequence of) subtree(s)")))
  9622. (if (equal goto '(16))
  9623. (org-refile-goto-last-stored)
  9624. (when (or
  9625. (and (equal goto 2)
  9626. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9627. (prog1
  9628. (setq it (list (or org-clock-heading "running clock")
  9629. (buffer-file-name
  9630. (marker-buffer org-clock-hd-marker))
  9631. ""
  9632. (marker-position org-clock-hd-marker)))
  9633. (setq goto nil)))
  9634. (setq it (or rfloc
  9635. (let (heading-text)
  9636. (save-excursion
  9637. (unless goto
  9638. (org-back-to-heading t)
  9639. (setq heading-text
  9640. (nth 4 (org-heading-components))))
  9641. (org-refile-get-location
  9642. (cond (goto "Goto")
  9643. (regionp "Refile region to")
  9644. (t (concat "Refile subtree \""
  9645. heading-text "\" to")))
  9646. default-buffer
  9647. org-refile-allow-creating-parent-nodes
  9648. goto))))))
  9649. (setq file (nth 1 it)
  9650. re (nth 2 it)
  9651. pos (nth 3 it))
  9652. (if (and (not goto)
  9653. pos
  9654. (equal (buffer-file-name) file)
  9655. (if regionp
  9656. (and (>= pos region-start)
  9657. (<= pos region-end))
  9658. (and (>= pos (point))
  9659. (< pos (save-excursion
  9660. (org-end-of-subtree t t))))))
  9661. (error "Cannot refile to position inside the tree or region"))
  9662. (setq nbuf (or (find-buffer-visiting file)
  9663. (find-file-noselect file)))
  9664. (if goto
  9665. (progn
  9666. (org-pop-to-buffer-same-window nbuf)
  9667. (goto-char pos)
  9668. (org-show-context 'org-goto))
  9669. (if regionp
  9670. (progn
  9671. (org-kill-new (buffer-substring region-start region-end))
  9672. (org-save-markers-in-region region-start region-end))
  9673. (org-copy-subtree 1 nil t))
  9674. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9675. (find-file-noselect file)))
  9676. (setq reversed (org-notes-order-reversed-p))
  9677. (save-excursion
  9678. (save-restriction
  9679. (widen)
  9680. (if pos
  9681. (progn
  9682. (goto-char pos)
  9683. (looking-at org-outline-regexp)
  9684. (setq level (org-get-valid-level (funcall outline-level) 1))
  9685. (goto-char
  9686. (if reversed
  9687. (or (outline-next-heading) (point-max))
  9688. (or (save-excursion (org-get-next-sibling))
  9689. (org-end-of-subtree t t)
  9690. (point-max)))))
  9691. (setq level 1)
  9692. (if (not reversed)
  9693. (goto-char (point-max))
  9694. (goto-char (point-min))
  9695. (or (outline-next-heading) (goto-char (point-max)))))
  9696. (if (not (bolp)) (newline))
  9697. (org-paste-subtree level)
  9698. (when org-log-refile
  9699. (org-add-log-setup 'refile nil nil 'findpos
  9700. org-log-refile)
  9701. (unless (eq org-log-refile 'note)
  9702. (save-excursion (org-add-log-note))))
  9703. (and org-auto-align-tags (org-set-tags nil t))
  9704. (bookmark-set "org-refile-last-stored")
  9705. ;; If we are refiling for capture, make sure that the
  9706. ;; last-capture pointers point here
  9707. (when (org-bound-and-true-p org-refile-for-capture)
  9708. (bookmark-set "org-capture-last-stored-marker")
  9709. (move-marker org-capture-last-stored-marker (point)))
  9710. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9711. (run-hooks 'org-after-refile-insert-hook))))
  9712. (if regionp
  9713. (delete-region (point) (+ (point) region-length))
  9714. (org-cut-subtree))
  9715. (when (featurep 'org-inlinetask)
  9716. (org-inlinetask-remove-END-maybe))
  9717. (setq org-markers-to-move nil)
  9718. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9719. (defun org-refile-goto-last-stored ()
  9720. "Go to the location where the last refile was stored."
  9721. (interactive)
  9722. (bookmark-jump "org-refile-last-stored")
  9723. (message "This is the location of the last refile"))
  9724. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9725. no-exclude)
  9726. "Prompt the user for a refile location, using PROMPT.
  9727. PROMPT should not be suffixed with a colon and a space, because
  9728. this function appends the default value from
  9729. `org-refile-history' automatically, if that is not empty.
  9730. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9731. this is used for the GOTO interface."
  9732. (let ((org-refile-targets org-refile-targets)
  9733. (org-refile-use-outline-path org-refile-use-outline-path)
  9734. excluded-entries)
  9735. (when (and (eq major-mode 'org-mode)
  9736. (not org-refile-use-cache)
  9737. (not no-exclude))
  9738. (org-map-tree
  9739. (lambda()
  9740. (setq excluded-entries
  9741. (append excluded-entries (list (org-get-heading t t)))))))
  9742. (setq org-refile-target-table
  9743. (org-refile-get-targets default-buffer excluded-entries)))
  9744. (unless org-refile-target-table
  9745. (error "No refile targets"))
  9746. (let* ((prompt (concat prompt
  9747. (and (car org-refile-history)
  9748. (concat " (default " (car org-refile-history) ")"))
  9749. ": "))
  9750. (cbuf (current-buffer))
  9751. (partial-completion-mode nil)
  9752. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9753. (cfunc (if (and org-refile-use-outline-path
  9754. org-outline-path-complete-in-steps)
  9755. 'org-olpath-completing-read
  9756. 'org-icompleting-read))
  9757. (extra (if org-refile-use-outline-path "/" ""))
  9758. (filename (and cfn (expand-file-name cfn)))
  9759. (tbl (mapcar
  9760. (lambda (x)
  9761. (if (and (not (member org-refile-use-outline-path
  9762. '(file full-file-path)))
  9763. (not (equal filename (nth 1 x))))
  9764. (cons (concat (car x) extra " ("
  9765. (file-name-nondirectory (nth 1 x)) ")")
  9766. (cdr x))
  9767. (cons (concat (car x) extra) (cdr x))))
  9768. org-refile-target-table))
  9769. (completion-ignore-case t)
  9770. pa answ parent-target child parent old-hist)
  9771. (setq old-hist org-refile-history)
  9772. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9773. nil 'org-refile-history (car org-refile-history)))
  9774. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9775. (org-refile-check-position pa)
  9776. (if pa
  9777. (progn
  9778. (when (or (not org-refile-history)
  9779. (not (eq old-hist org-refile-history))
  9780. (not (equal (car pa) (car org-refile-history))))
  9781. (setq org-refile-history
  9782. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9783. org-refile-history
  9784. (cdr org-refile-history))))
  9785. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9786. (pop org-refile-history)))
  9787. pa)
  9788. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9789. (progn
  9790. (setq parent (match-string 1 answ)
  9791. child (match-string 2 answ))
  9792. (setq parent-target (or (assoc parent tbl)
  9793. (assoc (concat parent "/") tbl)))
  9794. (when (and parent-target
  9795. (or (eq new-nodes t)
  9796. (and (eq new-nodes 'confirm)
  9797. (y-or-n-p (format "Create new node \"%s\"? "
  9798. child)))))
  9799. (org-refile-new-child parent-target child)))
  9800. (error "Invalid target location")))))
  9801. (defun org-refile-check-position (refile-pointer)
  9802. "Check if the refile pointer matches the readline to which it points."
  9803. (let* ((file (nth 1 refile-pointer))
  9804. (re (nth 2 refile-pointer))
  9805. (pos (nth 3 refile-pointer))
  9806. buffer)
  9807. (when (org-string-nw-p re)
  9808. (setq buffer (if (markerp pos)
  9809. (marker-buffer pos)
  9810. (or (find-buffer-visiting file)
  9811. (find-file-noselect file))))
  9812. (with-current-buffer buffer
  9813. (save-excursion
  9814. (save-restriction
  9815. (widen)
  9816. (goto-char pos)
  9817. (beginning-of-line 1)
  9818. (unless (org-looking-at-p re)
  9819. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9820. (defun org-refile-new-child (parent-target child)
  9821. "Use refile target PARENT-TARGET to add new CHILD below it."
  9822. (unless parent-target
  9823. (error "Cannot find parent for new node"))
  9824. (let ((file (nth 1 parent-target))
  9825. (pos (nth 3 parent-target))
  9826. level)
  9827. (with-current-buffer (or (find-buffer-visiting file)
  9828. (find-file-noselect file))
  9829. (save-excursion
  9830. (save-restriction
  9831. (widen)
  9832. (if pos
  9833. (goto-char pos)
  9834. (goto-char (point-max))
  9835. (if (not (bolp)) (newline)))
  9836. (when (looking-at org-outline-regexp)
  9837. (setq level (funcall outline-level))
  9838. (org-end-of-subtree t t))
  9839. (org-back-over-empty-lines)
  9840. (insert "\n" (make-string
  9841. (if pos (org-get-valid-level level 1) 1) ?*)
  9842. " " child "\n")
  9843. (beginning-of-line 0)
  9844. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9845. (defun org-olpath-completing-read (prompt collection &rest args)
  9846. "Read an outline path like a file name."
  9847. (let ((thetable collection)
  9848. (org-completion-use-ido nil) ; does not work with ido.
  9849. (org-completion-use-iswitchb nil)) ; or iswitchb
  9850. (apply
  9851. 'org-icompleting-read prompt
  9852. (lambda (string predicate &optional flag)
  9853. (let (rtn r f (l (length string)))
  9854. (cond
  9855. ((eq flag nil)
  9856. ;; try completion
  9857. (try-completion string thetable))
  9858. ((eq flag t)
  9859. ;; all-completions
  9860. (setq rtn (all-completions string thetable predicate))
  9861. (mapcar
  9862. (lambda (x)
  9863. (setq r (substring x l))
  9864. (if (string-match " ([^)]*)$" x)
  9865. (setq f (match-string 0 x))
  9866. (setq f ""))
  9867. (if (string-match "/" r)
  9868. (concat string (substring r 0 (match-end 0)) f)
  9869. x))
  9870. rtn))
  9871. ((eq flag 'lambda)
  9872. ;; exact match?
  9873. (assoc string thetable)))
  9874. ))
  9875. args)))
  9876. ;;;; Dynamic blocks
  9877. (defun org-find-dblock (name)
  9878. "Find the first dynamic block with name NAME in the buffer.
  9879. If not found, stay at current position and return nil."
  9880. (let (pos)
  9881. (save-excursion
  9882. (goto-char (point-min))
  9883. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9884. nil t)
  9885. (match-beginning 0))))
  9886. (if pos (goto-char pos))
  9887. pos))
  9888. (defconst org-dblock-start-re
  9889. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9890. "Matches the start line of a dynamic block, with parameters.")
  9891. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9892. "Matches the end of a dynamic block.")
  9893. (defun org-create-dblock (plist)
  9894. "Create a dynamic block section, with parameters taken from PLIST.
  9895. PLIST must contain a :name entry which is used as name of the block."
  9896. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9897. (end-of-line 1)
  9898. (newline))
  9899. (let ((col (current-column))
  9900. (name (plist-get plist :name)))
  9901. (insert "#+BEGIN: " name)
  9902. (while plist
  9903. (if (eq (car plist) :name)
  9904. (setq plist (cddr plist))
  9905. (insert " " (prin1-to-string (pop plist)))))
  9906. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9907. (beginning-of-line -2)))
  9908. (defun org-prepare-dblock ()
  9909. "Prepare dynamic block for refresh.
  9910. This empties the block, puts the cursor at the insert position and returns
  9911. the property list including an extra property :name with the block name."
  9912. (unless (looking-at org-dblock-start-re)
  9913. (error "Not at a dynamic block"))
  9914. (let* ((begdel (1+ (match-end 0)))
  9915. (name (org-no-properties (match-string 1)))
  9916. (params (append (list :name name)
  9917. (read (concat "(" (match-string 3) ")")))))
  9918. (save-excursion
  9919. (beginning-of-line 1)
  9920. (skip-chars-forward " \t")
  9921. (setq params (plist-put params :indentation-column (current-column))))
  9922. (unless (re-search-forward org-dblock-end-re nil t)
  9923. (error "Dynamic block not terminated"))
  9924. (setq params
  9925. (append params
  9926. (list :content (buffer-substring
  9927. begdel (match-beginning 0)))))
  9928. (delete-region begdel (match-beginning 0))
  9929. (goto-char begdel)
  9930. (open-line 1)
  9931. params))
  9932. (defun org-map-dblocks (&optional command)
  9933. "Apply COMMAND to all dynamic blocks in the current buffer.
  9934. If COMMAND is not given, use `org-update-dblock'."
  9935. (let ((cmd (or command 'org-update-dblock)))
  9936. (save-excursion
  9937. (goto-char (point-min))
  9938. (while (re-search-forward org-dblock-start-re nil t)
  9939. (goto-char (match-beginning 0))
  9940. (save-excursion
  9941. (condition-case nil
  9942. (funcall cmd)
  9943. (error (message "Error during update of dynamic block"))))
  9944. (unless (re-search-forward org-dblock-end-re nil t)
  9945. (error "Dynamic block not terminated"))))))
  9946. (defun org-dblock-update (&optional arg)
  9947. "User command for updating dynamic blocks.
  9948. Update the dynamic block at point. With prefix ARG, update all dynamic
  9949. blocks in the buffer."
  9950. (interactive "P")
  9951. (if arg
  9952. (org-update-all-dblocks)
  9953. (or (looking-at org-dblock-start-re)
  9954. (org-beginning-of-dblock))
  9955. (org-update-dblock)))
  9956. (defun org-update-dblock ()
  9957. "Update the dynamic block at point.
  9958. This means to empty the block, parse for parameters and then call
  9959. the correct writing function."
  9960. (interactive)
  9961. (save-window-excursion
  9962. (let* ((pos (point))
  9963. (line (org-current-line))
  9964. (params (org-prepare-dblock))
  9965. (name (plist-get params :name))
  9966. (indent (plist-get params :indentation-column))
  9967. (cmd (intern (concat "org-dblock-write:" name))))
  9968. (message "Updating dynamic block `%s' at line %d..." name line)
  9969. (funcall cmd params)
  9970. (message "Updating dynamic block `%s' at line %d...done" name line)
  9971. (goto-char pos)
  9972. (when (and indent (> indent 0))
  9973. (setq indent (make-string indent ?\ ))
  9974. (save-excursion
  9975. (org-beginning-of-dblock)
  9976. (forward-line 1)
  9977. (while (not (looking-at org-dblock-end-re))
  9978. (insert indent)
  9979. (beginning-of-line 2))
  9980. (when (looking-at org-dblock-end-re)
  9981. (and (looking-at "[ \t]+")
  9982. (replace-match ""))
  9983. (insert indent)))))))
  9984. (defun org-beginning-of-dblock ()
  9985. "Find the beginning of the dynamic block at point.
  9986. Error if there is no such block at point."
  9987. (let ((pos (point))
  9988. beg)
  9989. (end-of-line 1)
  9990. (if (and (re-search-backward org-dblock-start-re nil t)
  9991. (setq beg (match-beginning 0))
  9992. (re-search-forward org-dblock-end-re nil t)
  9993. (> (match-end 0) pos))
  9994. (goto-char beg)
  9995. (goto-char pos)
  9996. (error "Not in a dynamic block"))))
  9997. (defun org-update-all-dblocks ()
  9998. "Update all dynamic blocks in the buffer.
  9999. This function can be used in a hook."
  10000. (interactive)
  10001. (when (eq major-mode 'org-mode)
  10002. (org-map-dblocks 'org-update-dblock)))
  10003. ;;;; Completion
  10004. (defconst org-additional-option-like-keywords
  10005. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10006. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10007. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10008. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10009. "BEGIN:" "END:"
  10010. "ORGTBL" "TBLFM:" "TBLNAME:"
  10011. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10012. "BEGIN_QUOTE" "END_QUOTE"
  10013. "BEGIN_VERSE" "END_VERSE"
  10014. "BEGIN_CENTER" "END_CENTER"
  10015. "BEGIN_SRC" "END_SRC"
  10016. "BEGIN_RESULT" "END_RESULT"
  10017. "NAME:" "RESULTS:"
  10018. "HEADER:" "HEADERS:"
  10019. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10020. "CAPTION:" "LABEL:"
  10021. "SETUPFILE:"
  10022. "INCLUDE:"
  10023. "BIND:"
  10024. "MACRO:"))
  10025. (defcustom org-structure-template-alist
  10026. '(
  10027. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10028. "<src lang=\"?\">\n\n</src>")
  10029. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10030. "<example>\n?\n</example>")
  10031. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10032. "<quote>\n?\n</quote>")
  10033. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10034. "<verse>\n?\n</verse>")
  10035. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10036. "<center>\n?\n</center>")
  10037. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10038. "<literal style=\"latex\">\n?\n</literal>")
  10039. ("L" "#+LaTeX: "
  10040. "<literal style=\"latex\">?</literal>")
  10041. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10042. "<literal style=\"html\">\n?\n</literal>")
  10043. ("H" "#+HTML: "
  10044. "<literal style=\"html\">?</literal>")
  10045. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10046. ("A" "#+ASCII: ")
  10047. ("i" "#+INDEX: ?"
  10048. "#+INDEX: ?")
  10049. ("I" "#+INCLUDE %file ?"
  10050. "<include file=%file markup=\"?\">")
  10051. )
  10052. "Structure completion elements.
  10053. This is a list of abbreviation keys and values. The value gets inserted
  10054. if you type `<' followed by the key and then press the completion key,
  10055. usually `M-TAB'. %file will be replaced by a file name after prompting
  10056. for the file using completion. The cursor will be placed at the position
  10057. of the `?` in the template.
  10058. There are two templates for each key, the first uses the original Org syntax,
  10059. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10060. the default when the /org-mtags.el/ module has been loaded. See also the
  10061. variable `org-mtags-prefer-muse-templates'."
  10062. :group 'org-completion
  10063. :type '(repeat
  10064. (string :tag "Key")
  10065. (string :tag "Template")
  10066. (string :tag "Muse Template")))
  10067. (defun org-try-structure-completion ()
  10068. "Try to complete a structure template before point.
  10069. This looks for strings like \"<e\" on an otherwise empty line and
  10070. expands them."
  10071. (let ((l (buffer-substring (point-at-bol) (point)))
  10072. a)
  10073. (when (and (looking-at "[ \t]*$")
  10074. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10075. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10076. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10077. (match-beginning 1)) a)
  10078. t)))
  10079. (defun org-complete-expand-structure-template (start cell)
  10080. "Expand a structure template."
  10081. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10082. (rpl (nth (if musep 2 1) cell))
  10083. (ind ""))
  10084. (delete-region start (point))
  10085. (when (string-match "\\`#\\+" rpl)
  10086. (cond
  10087. ((bolp))
  10088. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10089. (setq ind (buffer-substring (point-at-bol) (point))))
  10090. (t (newline))))
  10091. (setq start (point))
  10092. (if (string-match "%file" rpl)
  10093. (setq rpl (replace-match
  10094. (concat
  10095. "\""
  10096. (save-match-data
  10097. (abbreviate-file-name (read-file-name "Include file: ")))
  10098. "\"")
  10099. t t rpl)))
  10100. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10101. (concat "\n" ind)))
  10102. (insert rpl)
  10103. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10104. ;;;; TODO, DEADLINE, Comments
  10105. (defun org-toggle-comment ()
  10106. "Change the COMMENT state of an entry."
  10107. (interactive)
  10108. (save-excursion
  10109. (org-back-to-heading)
  10110. (let (case-fold-search)
  10111. (cond
  10112. ((looking-at (format org-heading-keyword-regexp-format
  10113. org-comment-string))
  10114. (goto-char (match-end 1))
  10115. (looking-at (concat " +" org-comment-string))
  10116. (replace-match "" t t)
  10117. (when (eolp) (insert " ")))
  10118. ((looking-at org-outline-regexp)
  10119. (goto-char (match-end 0))
  10120. (insert org-comment-string " "))))))
  10121. (defvar org-last-todo-state-is-todo nil
  10122. "This is non-nil when the last TODO state change led to a TODO state.
  10123. If the last change removed the TODO tag or switched to DONE, then
  10124. this is nil.")
  10125. (defvar org-setting-tags nil) ; dynamically skipped
  10126. (defvar org-todo-setup-filter-hook nil
  10127. "Hook for functions that pre-filter todo specs.
  10128. Each function takes a todo spec and returns either nil or the spec
  10129. transformed into canonical form." )
  10130. (defvar org-todo-get-default-hook nil
  10131. "Hook for functions that get a default item for todo.
  10132. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10133. nil or a string to be used for the todo mark." )
  10134. (defvar org-agenda-headline-snapshot-before-repeat)
  10135. (defun org-current-effective-time ()
  10136. "Return current time adjusted for `org-extend-today-until' variable"
  10137. (let* ((ct (org-current-time))
  10138. (dct (decode-time ct))
  10139. (ct1
  10140. (if (and org-use-effective-time
  10141. (< (nth 2 dct) org-extend-today-until))
  10142. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10143. ct)))
  10144. ct1))
  10145. (defun org-todo-yesterday (&optional arg)
  10146. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10147. (interactive "P")
  10148. (if (eq major-mode 'org-agenda-mode)
  10149. (apply 'org-agenda-todo-yesterday arg)
  10150. (let* ((hour (third (decode-time
  10151. (org-current-time))))
  10152. (org-extend-today-until (1+ hour)))
  10153. (org-todo arg))))
  10154. (defun org-todo (&optional arg)
  10155. "Change the TODO state of an item.
  10156. The state of an item is given by a keyword at the start of the heading,
  10157. like
  10158. *** TODO Write paper
  10159. *** DONE Call mom
  10160. The different keywords are specified in the variable `org-todo-keywords'.
  10161. By default the available states are \"TODO\" and \"DONE\".
  10162. So for this example: when the item starts with TODO, it is changed to DONE.
  10163. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10164. DONE are present, add TODO at the beginning of the heading.
  10165. With \\[universal-argument] prefix arg, use completion to determine the new \
  10166. state.
  10167. With numeric prefix arg, switch to that state.
  10168. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10169. keywords (nextset).
  10170. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10171. With a numeric prefix arg of 0, inhibit note taking for the change.
  10172. For calling through lisp, arg is also interpreted in the following way:
  10173. 'none -> empty state
  10174. \"\"(empty string) -> switch to empty state
  10175. 'done -> switch to DONE
  10176. 'nextset -> switch to the next set of keywords
  10177. 'previousset -> switch to the previous set of keywords
  10178. \"WAITING\" -> switch to the specified keyword, but only if it
  10179. really is a member of `org-todo-keywords'."
  10180. (interactive "P")
  10181. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10182. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10183. 'region-start-level 'region))
  10184. org-loop-over-headlines-in-active-region)
  10185. (org-map-entries
  10186. `(org-todo ,arg)
  10187. org-loop-over-headlines-in-active-region
  10188. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10189. (if (equal arg '(16)) (setq arg 'nextset))
  10190. (let ((org-blocker-hook org-blocker-hook)
  10191. (case-fold-search nil))
  10192. (when (equal arg '(64))
  10193. (setq arg nil org-blocker-hook nil))
  10194. (when (and org-blocker-hook
  10195. (or org-inhibit-blocking
  10196. (org-entry-get nil "NOBLOCKING")))
  10197. (setq org-blocker-hook nil))
  10198. (save-excursion
  10199. (catch 'exit
  10200. (org-back-to-heading t)
  10201. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10202. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10203. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10204. (let* ((match-data (match-data))
  10205. (startpos (point-at-bol))
  10206. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10207. (org-log-done org-log-done)
  10208. (org-log-repeat org-log-repeat)
  10209. (org-todo-log-states org-todo-log-states)
  10210. (org-inhibit-logging
  10211. (if (equal arg 0)
  10212. (progn (setq arg nil) 'note) org-inhibit-logging))
  10213. (this (match-string 1))
  10214. (hl-pos (match-beginning 0))
  10215. (head (org-get-todo-sequence-head this))
  10216. (ass (assoc head org-todo-kwd-alist))
  10217. (interpret (nth 1 ass))
  10218. (done-word (nth 3 ass))
  10219. (final-done-word (nth 4 ass))
  10220. (last-state (or this ""))
  10221. (completion-ignore-case t)
  10222. (member (member this org-todo-keywords-1))
  10223. (tail (cdr member))
  10224. (state (cond
  10225. ((and org-todo-key-trigger
  10226. (or (and (equal arg '(4))
  10227. (eq org-use-fast-todo-selection 'prefix))
  10228. (and (not arg) org-use-fast-todo-selection
  10229. (not (eq org-use-fast-todo-selection
  10230. 'prefix)))))
  10231. ;; Use fast selection
  10232. (org-fast-todo-selection))
  10233. ((and (equal arg '(4))
  10234. (or (not org-use-fast-todo-selection)
  10235. (not org-todo-key-trigger)))
  10236. ;; Read a state with completion
  10237. (org-icompleting-read
  10238. "State: " (mapcar (lambda(x) (list x))
  10239. org-todo-keywords-1)
  10240. nil t))
  10241. ((eq arg 'right)
  10242. (if this
  10243. (if tail (car tail) nil)
  10244. (car org-todo-keywords-1)))
  10245. ((eq arg 'left)
  10246. (if (equal member org-todo-keywords-1)
  10247. nil
  10248. (if this
  10249. (nth (- (length org-todo-keywords-1)
  10250. (length tail) 2)
  10251. org-todo-keywords-1)
  10252. (org-last org-todo-keywords-1))))
  10253. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10254. (setq arg nil))) ; hack to fall back to cycling
  10255. (arg
  10256. ;; user or caller requests a specific state
  10257. (cond
  10258. ((equal arg "") nil)
  10259. ((eq arg 'none) nil)
  10260. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10261. ((eq arg 'nextset)
  10262. (or (car (cdr (member head org-todo-heads)))
  10263. (car org-todo-heads)))
  10264. ((eq arg 'previousset)
  10265. (let ((org-todo-heads (reverse org-todo-heads)))
  10266. (or (car (cdr (member head org-todo-heads)))
  10267. (car org-todo-heads))))
  10268. ((car (member arg org-todo-keywords-1)))
  10269. ((stringp arg)
  10270. (error "State `%s' not valid in this file" arg))
  10271. ((nth (1- (prefix-numeric-value arg))
  10272. org-todo-keywords-1))))
  10273. ((null member) (or head (car org-todo-keywords-1)))
  10274. ((equal this final-done-word) nil) ;; -> make empty
  10275. ((null tail) nil) ;; -> first entry
  10276. ((memq interpret '(type priority))
  10277. (if (eq this-command last-command)
  10278. (car tail)
  10279. (if (> (length tail) 0)
  10280. (or done-word (car org-done-keywords))
  10281. nil)))
  10282. (t
  10283. (car tail))))
  10284. (state (or
  10285. (run-hook-with-args-until-success
  10286. 'org-todo-get-default-hook state last-state)
  10287. state))
  10288. (next (if state (concat " " state " ") " "))
  10289. (change-plist (list :type 'todo-state-change :from this :to state
  10290. :position startpos))
  10291. dolog now-done-p)
  10292. (when org-blocker-hook
  10293. (setq org-last-todo-state-is-todo
  10294. (not (member this org-done-keywords)))
  10295. (unless (save-excursion
  10296. (save-match-data
  10297. (org-with-wide-buffer
  10298. (run-hook-with-args-until-failure
  10299. 'org-blocker-hook change-plist))))
  10300. (if (org-called-interactively-p 'interactive)
  10301. (error "TODO state change from %s to %s blocked" this state)
  10302. ;; fail silently
  10303. (message "TODO state change from %s to %s blocked" this state)
  10304. (throw 'exit nil))))
  10305. (store-match-data match-data)
  10306. (replace-match next t t)
  10307. (unless (pos-visible-in-window-p hl-pos)
  10308. (message "TODO state changed to %s" (org-trim next)))
  10309. (unless head
  10310. (setq head (org-get-todo-sequence-head state)
  10311. ass (assoc head org-todo-kwd-alist)
  10312. interpret (nth 1 ass)
  10313. done-word (nth 3 ass)
  10314. final-done-word (nth 4 ass)))
  10315. (when (memq arg '(nextset previousset))
  10316. (message "Keyword-Set %d/%d: %s"
  10317. (- (length org-todo-sets) -1
  10318. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10319. (length org-todo-sets)
  10320. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10321. (setq org-last-todo-state-is-todo
  10322. (not (member state org-done-keywords)))
  10323. (setq now-done-p (and (member state org-done-keywords)
  10324. (not (member this org-done-keywords))))
  10325. (and logging (org-local-logging logging))
  10326. (when (and (or org-todo-log-states org-log-done)
  10327. (not (eq org-inhibit-logging t))
  10328. (not (memq arg '(nextset previousset))))
  10329. ;; we need to look at recording a time and note
  10330. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10331. (nth 2 (assoc this org-todo-log-states))))
  10332. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10333. (setq dolog 'time))
  10334. (when (and state
  10335. (member state org-not-done-keywords)
  10336. (not (member this org-not-done-keywords)))
  10337. ;; This is now a todo state and was not one before
  10338. ;; If there was a CLOSED time stamp, get rid of it.
  10339. (org-add-planning-info nil nil 'closed))
  10340. (when (and now-done-p org-log-done)
  10341. ;; It is now done, and it was not done before
  10342. (org-add-planning-info 'closed (org-current-effective-time))
  10343. (if (and (not dolog) (eq 'note org-log-done))
  10344. (org-add-log-setup 'done state this 'findpos 'note)))
  10345. (when (and state dolog)
  10346. ;; This is a non-nil state, and we need to log it
  10347. (org-add-log-setup 'state state this 'findpos dolog)))
  10348. ;; Fixup tag positioning
  10349. (org-todo-trigger-tag-changes state)
  10350. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10351. (when org-provide-todo-statistics
  10352. (org-update-parent-todo-statistics))
  10353. (run-hooks 'org-after-todo-state-change-hook)
  10354. (if (and arg (not (member state org-done-keywords)))
  10355. (setq head (org-get-todo-sequence-head state)))
  10356. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10357. ;; Do we need to trigger a repeat?
  10358. (when now-done-p
  10359. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10360. ;; This is for the agenda, take a snapshot of the headline.
  10361. (save-match-data
  10362. (setq org-agenda-headline-snapshot-before-repeat
  10363. (org-get-heading))))
  10364. (org-auto-repeat-maybe state))
  10365. ;; Fixup cursor location if close to the keyword
  10366. (if (and (outline-on-heading-p)
  10367. (not (bolp))
  10368. (save-excursion (beginning-of-line 1)
  10369. (looking-at org-todo-line-regexp))
  10370. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10371. (progn
  10372. (goto-char (or (match-end 2) (match-end 1)))
  10373. (and (looking-at " ") (just-one-space))))
  10374. (when org-trigger-hook
  10375. (save-excursion
  10376. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10377. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10378. "Block turning an entry into a TODO, using the hierarchy.
  10379. This checks whether the current task should be blocked from state
  10380. changes. Such blocking occurs when:
  10381. 1. The task has children which are not all in a completed state.
  10382. 2. A task has a parent with the property :ORDERED:, and there
  10383. are siblings prior to the current task with incomplete
  10384. status.
  10385. 3. The parent of the task is blocked because it has siblings that should
  10386. be done first, or is child of a block grandparent TODO entry."
  10387. (if (not org-enforce-todo-dependencies)
  10388. t ; if locally turned off don't block
  10389. (catch 'dont-block
  10390. ;; If this is not a todo state change, or if this entry is already DONE,
  10391. ;; do not block
  10392. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10393. (member (plist-get change-plist :from)
  10394. (cons 'done org-done-keywords))
  10395. (member (plist-get change-plist :to)
  10396. (cons 'todo org-not-done-keywords))
  10397. (not (plist-get change-plist :to)))
  10398. (throw 'dont-block t))
  10399. ;; If this task has children, and any are undone, it's blocked
  10400. (save-excursion
  10401. (org-back-to-heading t)
  10402. (let ((this-level (funcall outline-level)))
  10403. (outline-next-heading)
  10404. (let ((child-level (funcall outline-level)))
  10405. (while (and (not (eobp))
  10406. (> child-level this-level))
  10407. ;; this todo has children, check whether they are all
  10408. ;; completed
  10409. (if (and (not (org-entry-is-done-p))
  10410. (org-entry-is-todo-p))
  10411. (throw 'dont-block nil))
  10412. (outline-next-heading)
  10413. (setq child-level (funcall outline-level))))))
  10414. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10415. ;; any previous siblings are undone, it's blocked
  10416. (save-excursion
  10417. (org-back-to-heading t)
  10418. (let* ((pos (point))
  10419. (parent-pos (and (org-up-heading-safe) (point))))
  10420. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10421. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10422. (forward-line 1)
  10423. (re-search-forward org-not-done-heading-regexp pos t))
  10424. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10425. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10426. (while t
  10427. (goto-char parent-pos)
  10428. (if (not (looking-at org-not-done-heading-regexp))
  10429. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10430. (setq pos (point))
  10431. (setq parent-pos (and (org-up-heading-safe) (point)))
  10432. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10433. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10434. (forward-line 1)
  10435. (re-search-forward org-not-done-heading-regexp pos t))
  10436. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10437. (defcustom org-track-ordered-property-with-tag nil
  10438. "Should the ORDERED property also be shown as a tag?
  10439. The ORDERED property decides if an entry should require subtasks to be
  10440. completed in sequence. Since a property is not very visible, setting
  10441. this option means that toggling the ORDERED property with the command
  10442. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10443. not relevant for the behavior, but it makes things more visible.
  10444. Note that toggling the tag with tags commands will not change the property
  10445. and therefore not influence behavior!
  10446. This can be t, meaning the tag ORDERED should be used, It can also be a
  10447. string to select a different tag for this task."
  10448. :group 'org-todo
  10449. :type '(choice
  10450. (const :tag "No tracking" nil)
  10451. (const :tag "Track with ORDERED tag" t)
  10452. (string :tag "Use other tag")))
  10453. (defun org-toggle-ordered-property ()
  10454. "Toggle the ORDERED property of the current entry.
  10455. For better visibility, you can track the value of this property with a tag.
  10456. See variable `org-track-ordered-property-with-tag'."
  10457. (interactive)
  10458. (let* ((t1 org-track-ordered-property-with-tag)
  10459. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10460. (save-excursion
  10461. (org-back-to-heading)
  10462. (if (org-entry-get nil "ORDERED")
  10463. (progn
  10464. (org-delete-property "ORDERED")
  10465. (and tag (org-toggle-tag tag 'off))
  10466. (message "Subtasks can be completed in arbitrary order"))
  10467. (org-entry-put nil "ORDERED" "t")
  10468. (and tag (org-toggle-tag tag 'on))
  10469. (message "Subtasks must be completed in sequence")))))
  10470. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10471. (defun org-block-todo-from-checkboxes (change-plist)
  10472. "Block turning an entry into a TODO, using checkboxes.
  10473. This checks whether the current task should be blocked from state
  10474. changes because there are unchecked boxes in this entry."
  10475. (if (not org-enforce-todo-checkbox-dependencies)
  10476. t ; if locally turned off don't block
  10477. (catch 'dont-block
  10478. ;; If this is not a todo state change, or if this entry is already DONE,
  10479. ;; do not block
  10480. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10481. (member (plist-get change-plist :from)
  10482. (cons 'done org-done-keywords))
  10483. (member (plist-get change-plist :to)
  10484. (cons 'todo org-not-done-keywords))
  10485. (not (plist-get change-plist :to)))
  10486. (throw 'dont-block t))
  10487. ;; If this task has checkboxes that are not checked, it's blocked
  10488. (save-excursion
  10489. (org-back-to-heading t)
  10490. (let ((beg (point)) end)
  10491. (outline-next-heading)
  10492. (setq end (point))
  10493. (goto-char beg)
  10494. (if (org-list-search-forward
  10495. (concat (org-item-beginning-re)
  10496. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10497. "\\[[- ]\\]")
  10498. end t)
  10499. (progn
  10500. (if (boundp 'org-blocked-by-checkboxes)
  10501. (setq org-blocked-by-checkboxes t))
  10502. (throw 'dont-block nil)))))
  10503. t))) ; do not block
  10504. (defun org-entry-blocked-p ()
  10505. "Is the current entry blocked?"
  10506. (if (org-entry-get nil "NOBLOCKING")
  10507. nil ;; Never block this entry
  10508. (not
  10509. (run-hook-with-args-until-failure
  10510. 'org-blocker-hook
  10511. (list :type 'todo-state-change
  10512. :position (point)
  10513. :from 'todo
  10514. :to 'done)))))
  10515. (defun org-update-statistics-cookies (all)
  10516. "Update the statistics cookie, either from TODO or from checkboxes.
  10517. This should be called with the cursor in a line with a statistics cookie."
  10518. (interactive "P")
  10519. (if all
  10520. (progn
  10521. (org-update-checkbox-count 'all)
  10522. (org-map-entries 'org-update-parent-todo-statistics))
  10523. (if (not (org-at-heading-p))
  10524. (org-update-checkbox-count)
  10525. (let ((pos (move-marker (make-marker) (point)))
  10526. end l1 l2)
  10527. (ignore-errors (org-back-to-heading t))
  10528. (if (not (org-at-heading-p))
  10529. (org-update-checkbox-count)
  10530. (setq l1 (org-outline-level))
  10531. (setq end (save-excursion
  10532. (outline-next-heading)
  10533. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10534. (point)))
  10535. (if (and (save-excursion
  10536. (re-search-forward
  10537. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10538. (not (save-excursion (re-search-forward
  10539. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10540. (org-update-checkbox-count)
  10541. (if (and l2 (> l2 l1))
  10542. (progn
  10543. (goto-char end)
  10544. (org-update-parent-todo-statistics))
  10545. (goto-char pos)
  10546. (beginning-of-line 1)
  10547. (while (re-search-forward
  10548. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10549. (point-at-eol) t)
  10550. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10551. (goto-char pos)
  10552. (move-marker pos nil)))))
  10553. (defvar org-entry-property-inherited-from) ;; defined below
  10554. (defun org-update-parent-todo-statistics ()
  10555. "Update any statistics cookie in the parent of the current headline.
  10556. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10557. the entire subtree and this will travel up the hierarchy and update
  10558. statistics everywhere."
  10559. (let* ((prop (save-excursion (org-up-heading-safe)
  10560. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10561. (recursive (or (not org-hierarchical-todo-statistics)
  10562. (and prop (string-match "\\<recursive\\>" prop))))
  10563. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10564. 0))
  10565. (first t)
  10566. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10567. level ltoggle l1 new ndel
  10568. (cnt-all 0) (cnt-done 0) is-percent kwd
  10569. checkbox-beg ov ovs ove cookie-present)
  10570. (catch 'exit
  10571. (save-excursion
  10572. (beginning-of-line 1)
  10573. (setq ltoggle (funcall outline-level))
  10574. ;; Three situations are to consider:
  10575. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10576. ;; to the top-level ancestor on the headline;
  10577. ;; 2. If parent has "recursive" property, repeat up to the
  10578. ;; headline setting that property, taking inheritance into
  10579. ;; account;
  10580. ;; 3. Else, move up to direct parent and proceed only once.
  10581. (while (and (setq level (org-up-heading-safe))
  10582. (or recursive first)
  10583. (>= (point) lim))
  10584. (setq first nil cookie-present nil)
  10585. (unless (and level
  10586. (not (string-match
  10587. "\\<checkbox\\>"
  10588. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10589. "")))))
  10590. (throw 'exit nil))
  10591. (while (re-search-forward box-re (point-at-eol) t)
  10592. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10593. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10594. (save-match-data
  10595. (unless (outline-next-heading) (throw 'exit nil))
  10596. (while (and (looking-at org-complex-heading-regexp)
  10597. (> (setq l1 (length (match-string 1))) level))
  10598. (setq kwd (and (or recursive (= l1 ltoggle))
  10599. (match-string 2)))
  10600. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10601. (and (listp org-provide-todo-statistics)
  10602. (or (member kwd org-provide-todo-statistics)
  10603. (member kwd org-done-keywords))))
  10604. (setq cnt-all (1+ cnt-all))
  10605. (if (eq org-provide-todo-statistics t)
  10606. (and kwd (setq cnt-all (1+ cnt-all)))))
  10607. (and (member kwd org-done-keywords)
  10608. (setq cnt-done (1+ cnt-done)))
  10609. (outline-next-heading)))
  10610. (setq new
  10611. (if is-percent
  10612. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10613. (format "[%d/%d]" cnt-done cnt-all))
  10614. ndel (- (match-end 0) checkbox-beg))
  10615. ;; handle overlays when updating cookie from column view
  10616. (when (setq ov (car (overlays-at checkbox-beg)))
  10617. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10618. (delete-overlay ov))
  10619. (goto-char checkbox-beg)
  10620. (insert new)
  10621. (delete-region (point) (+ (point) ndel))
  10622. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10623. (when ov (move-overlay ov ovs ove)))
  10624. (when cookie-present
  10625. (run-hook-with-args 'org-after-todo-statistics-hook
  10626. cnt-done (- cnt-all cnt-done))))))
  10627. (run-hooks 'org-todo-statistics-hook)))
  10628. (defvar org-after-todo-statistics-hook nil
  10629. "Hook that is called after a TODO statistics cookie has been updated.
  10630. Each function is called with two arguments: the number of not-done entries
  10631. and the number of done entries.
  10632. For example, the following function, when added to this hook, will switch
  10633. an entry to DONE when all children are done, and back to TODO when new
  10634. entries are set to a TODO status. Note that this hook is only called
  10635. when there is a statistics cookie in the headline!
  10636. (defun org-summary-todo (n-done n-not-done)
  10637. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10638. (let (org-log-done org-log-states) ; turn off logging
  10639. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10640. ")
  10641. (defvar org-todo-statistics-hook nil
  10642. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10643. This hook runs even if there is no statistics cookie present, in which case
  10644. `org-after-todo-statistics-hook' would not run.")
  10645. (defun org-todo-trigger-tag-changes (state)
  10646. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10647. (let ((l org-todo-state-tags-triggers)
  10648. changes)
  10649. (when (or (not state) (equal state ""))
  10650. (setq changes (append changes (cdr (assoc "" l)))))
  10651. (when (and (stringp state) (> (length state) 0))
  10652. (setq changes (append changes (cdr (assoc state l)))))
  10653. (when (member state org-not-done-keywords)
  10654. (setq changes (append changes (cdr (assoc 'todo l)))))
  10655. (when (member state org-done-keywords)
  10656. (setq changes (append changes (cdr (assoc 'done l)))))
  10657. (dolist (c changes)
  10658. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10659. (defun org-local-logging (value)
  10660. "Get logging settings from a property VALUE."
  10661. (let* (words w a)
  10662. ;; directly set the variables, they are already local.
  10663. (setq org-log-done nil
  10664. org-log-repeat nil
  10665. org-todo-log-states nil)
  10666. (setq words (org-split-string value))
  10667. (while (setq w (pop words))
  10668. (cond
  10669. ((setq a (assoc w org-startup-options))
  10670. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10671. (set (nth 1 a) (nth 2 a))))
  10672. ((setq a (org-extract-log-state-settings w))
  10673. (and (member (car a) org-todo-keywords-1)
  10674. (push a org-todo-log-states)))))))
  10675. (defun org-get-todo-sequence-head (kwd)
  10676. "Return the head of the TODO sequence to which KWD belongs.
  10677. If KWD is not set, check if there is a text property remembering the
  10678. right sequence."
  10679. (let (p)
  10680. (cond
  10681. ((not kwd)
  10682. (or (get-text-property (point-at-bol) 'org-todo-head)
  10683. (progn
  10684. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10685. nil (point-at-eol)))
  10686. (get-text-property p 'org-todo-head))))
  10687. ((not (member kwd org-todo-keywords-1))
  10688. (car org-todo-keywords-1))
  10689. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10690. (defun org-fast-todo-selection ()
  10691. "Fast TODO keyword selection with single keys.
  10692. Returns the new TODO keyword, or nil if no state change should occur."
  10693. (let* ((fulltable org-todo-key-alist)
  10694. (done-keywords org-done-keywords) ;; needed for the faces.
  10695. (maxlen (apply 'max (mapcar
  10696. (lambda (x)
  10697. (if (stringp (car x)) (string-width (car x)) 0))
  10698. fulltable)))
  10699. (expert nil)
  10700. (fwidth (+ maxlen 3 1 3))
  10701. (ncol (/ (- (window-width) 4) fwidth))
  10702. tg cnt e c tbl
  10703. groups ingroup)
  10704. (save-excursion
  10705. (save-window-excursion
  10706. (if expert
  10707. (set-buffer (get-buffer-create " *Org todo*"))
  10708. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10709. (erase-buffer)
  10710. (org-set-local 'org-done-keywords done-keywords)
  10711. (setq tbl fulltable cnt 0)
  10712. (while (setq e (pop tbl))
  10713. (cond
  10714. ((equal e '(:startgroup))
  10715. (push '() groups) (setq ingroup t)
  10716. (when (not (= cnt 0))
  10717. (setq cnt 0)
  10718. (insert "\n"))
  10719. (insert "{ "))
  10720. ((equal e '(:endgroup))
  10721. (setq ingroup nil cnt 0)
  10722. (insert "}\n"))
  10723. ((equal e '(:newline))
  10724. (when (not (= cnt 0))
  10725. (setq cnt 0)
  10726. (insert "\n")
  10727. (setq e (car tbl))
  10728. (while (equal (car tbl) '(:newline))
  10729. (insert "\n")
  10730. (setq tbl (cdr tbl)))))
  10731. (t
  10732. (setq tg (car e) c (cdr e))
  10733. (if ingroup (push tg (car groups)))
  10734. (setq tg (org-add-props tg nil 'face
  10735. (org-get-todo-face tg)))
  10736. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10737. (insert "[" c "] " tg (make-string
  10738. (- fwidth 4 (length tg)) ?\ ))
  10739. (when (= (setq cnt (1+ cnt)) ncol)
  10740. (insert "\n")
  10741. (if ingroup (insert " "))
  10742. (setq cnt 0)))))
  10743. (insert "\n")
  10744. (goto-char (point-min))
  10745. (if (not expert) (org-fit-window-to-buffer))
  10746. (message "[a-z..]:Set [SPC]:clear")
  10747. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10748. (cond
  10749. ((or (= c ?\C-g)
  10750. (and (= c ?q) (not (rassoc c fulltable))))
  10751. (setq quit-flag t))
  10752. ((= c ?\ ) nil)
  10753. ((setq e (rassoc c fulltable) tg (car e))
  10754. tg)
  10755. (t (setq quit-flag t)))))))
  10756. (defun org-entry-is-todo-p ()
  10757. (member (org-get-todo-state) org-not-done-keywords))
  10758. (defun org-entry-is-done-p ()
  10759. (member (org-get-todo-state) org-done-keywords))
  10760. (defun org-get-todo-state ()
  10761. (save-excursion
  10762. (org-back-to-heading t)
  10763. (and (looking-at org-todo-line-regexp)
  10764. (match-end 2)
  10765. (match-string 2))))
  10766. (defun org-at-date-range-p (&optional inactive-ok)
  10767. "Is the cursor inside a date range?"
  10768. (interactive)
  10769. (save-excursion
  10770. (catch 'exit
  10771. (let ((pos (point)))
  10772. (skip-chars-backward "^[<\r\n")
  10773. (skip-chars-backward "<[")
  10774. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10775. (>= (match-end 0) pos)
  10776. (throw 'exit t))
  10777. (skip-chars-backward "^<[\r\n")
  10778. (skip-chars-backward "<[")
  10779. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10780. (>= (match-end 0) pos)
  10781. (throw 'exit t)))
  10782. nil)))
  10783. (defun org-get-repeat (&optional tagline)
  10784. "Check if there is a deadline/schedule with repeater in this entry."
  10785. (save-match-data
  10786. (save-excursion
  10787. (org-back-to-heading t)
  10788. (and (re-search-forward (if tagline
  10789. (concat tagline "\\s-*" org-repeat-re)
  10790. org-repeat-re)
  10791. (org-entry-end-position) t)
  10792. (match-string-no-properties 1)))))
  10793. (defvar org-last-changed-timestamp)
  10794. (defvar org-last-inserted-timestamp)
  10795. (defvar org-log-post-message)
  10796. (defvar org-log-note-purpose)
  10797. (defvar org-log-note-how)
  10798. (defvar org-log-note-extra)
  10799. (defun org-auto-repeat-maybe (done-word)
  10800. "Check if the current headline contains a repeated deadline/schedule.
  10801. If yes, set TODO state back to what it was and change the base date
  10802. of repeating deadline/scheduled time stamps to new date.
  10803. This function is run automatically after each state change to a DONE state."
  10804. ;; last-state is dynamically scoped into this function
  10805. (let* ((repeat (org-get-repeat))
  10806. (aa (assoc last-state org-todo-kwd-alist))
  10807. (interpret (nth 1 aa))
  10808. (head (nth 2 aa))
  10809. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10810. (msg "Entry repeats: ")
  10811. (org-log-done nil)
  10812. (org-todo-log-states nil)
  10813. re type n what ts time to-state)
  10814. (when repeat
  10815. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10816. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10817. org-todo-repeat-to-state))
  10818. (unless (and to-state (member to-state org-todo-keywords-1))
  10819. (setq to-state (if (eq interpret 'type) last-state head)))
  10820. (org-todo to-state)
  10821. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10822. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10823. (org-time-stamp-format t t))))
  10824. (when org-log-repeat
  10825. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10826. (memq 'org-add-log-note post-command-hook))
  10827. ;; OK, we are already setup for some record
  10828. (if (eq org-log-repeat 'note)
  10829. ;; make sure we take a note, not only a time stamp
  10830. (setq org-log-note-how 'note))
  10831. ;; Set up for taking a record
  10832. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10833. last-state
  10834. 'findpos org-log-repeat)))
  10835. (org-back-to-heading t)
  10836. (org-add-planning-info nil nil 'closed)
  10837. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10838. org-deadline-time-regexp "\\)\\|\\("
  10839. org-ts-regexp "\\)"))
  10840. (while (re-search-forward
  10841. re (save-excursion (outline-next-heading) (point)) t)
  10842. (setq type (if (match-end 1) org-scheduled-string
  10843. (if (match-end 3) org-deadline-string "Plain:"))
  10844. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10845. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10846. (setq n (string-to-number (match-string 2 ts))
  10847. what (match-string 3 ts))
  10848. (if (equal what "w") (setq n (* n 7) what "d"))
  10849. ;; Preparation, see if we need to modify the start date for the change
  10850. (when (match-end 1)
  10851. (setq time (save-match-data (org-time-string-to-time ts)))
  10852. (cond
  10853. ((equal (match-string 1 ts) ".")
  10854. ;; Shift starting date to today
  10855. (org-timestamp-change
  10856. (- (org-today) (time-to-days time))
  10857. 'day))
  10858. ((equal (match-string 1 ts) "+")
  10859. (let ((nshiftmax 10) (nshift 0))
  10860. (while (or (= nshift 0)
  10861. (<= (time-to-days time)
  10862. (time-to-days (current-time))))
  10863. (when (= (incf nshift) nshiftmax)
  10864. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10865. (error "Abort")))
  10866. (org-timestamp-change n (cdr (assoc what whata)))
  10867. (org-at-timestamp-p t)
  10868. (setq ts (match-string 1))
  10869. (setq time (save-match-data (org-time-string-to-time ts)))))
  10870. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10871. ;; rematch, so that we have everything in place for the real shift
  10872. (org-at-timestamp-p t)
  10873. (setq ts (match-string 1))
  10874. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10875. (org-timestamp-change n (cdr (assoc what whata)))
  10876. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10877. (setq org-log-post-message msg)
  10878. (message "%s" msg))))
  10879. (defun org-show-todo-tree (arg)
  10880. "Make a compact tree which shows all headlines marked with TODO.
  10881. The tree will show the lines where the regexp matches, and all higher
  10882. headlines above the match.
  10883. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10884. With a numeric prefix N, construct a sparse tree for the Nth element
  10885. of `org-todo-keywords-1'."
  10886. (interactive "P")
  10887. (let ((case-fold-search nil)
  10888. (kwd-re
  10889. (cond ((null arg) org-not-done-regexp)
  10890. ((equal arg '(4))
  10891. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10892. (mapcar 'list org-todo-keywords-1))))
  10893. (concat "\\("
  10894. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10895. "\\)\\>")))
  10896. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10897. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10898. org-todo-keywords-1)))
  10899. (t (error "Invalid prefix argument: %s" arg)))))
  10900. (message "%d TODO entries found"
  10901. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10902. (defun org-deadline (&optional remove time)
  10903. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10904. With argument REMOVE, remove any deadline from the item.
  10905. With argument TIME, set the deadline at the corresponding date. TIME
  10906. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10907. (interactive "P")
  10908. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10909. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10910. 'region-start-level 'region))
  10911. org-loop-over-headlines-in-active-region)
  10912. (org-map-entries
  10913. `(org-deadline ',remove ,time)
  10914. org-loop-over-headlines-in-active-region
  10915. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10916. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10917. (repeater (and old-date
  10918. (string-match
  10919. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10920. old-date)
  10921. (match-string 1 old-date))))
  10922. (if remove
  10923. (progn
  10924. (when (and old-date org-log-redeadline)
  10925. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10926. org-log-redeadline))
  10927. (org-remove-timestamp-with-keyword org-deadline-string)
  10928. (message "Item no longer has a deadline."))
  10929. (org-add-planning-info 'deadline time 'closed)
  10930. (when (and old-date org-log-redeadline
  10931. (not (equal old-date
  10932. (substring org-last-inserted-timestamp 1 -1))))
  10933. (org-add-log-setup 'redeadline nil old-date 'findpos
  10934. org-log-redeadline))
  10935. (when repeater
  10936. (save-excursion
  10937. (org-back-to-heading t)
  10938. (when (re-search-forward (concat org-deadline-string " "
  10939. org-last-inserted-timestamp)
  10940. (save-excursion
  10941. (outline-next-heading) (point)) t)
  10942. (goto-char (1- (match-end 0)))
  10943. (insert " " repeater)
  10944. (setq org-last-inserted-timestamp
  10945. (concat (substring org-last-inserted-timestamp 0 -1)
  10946. " " repeater
  10947. (substring org-last-inserted-timestamp -1))))))
  10948. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10949. (defun org-schedule (&optional remove time)
  10950. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10951. With argument REMOVE, remove any scheduling date from the item.
  10952. With argument TIME, scheduled at the corresponding date. TIME can
  10953. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10954. (interactive "P")
  10955. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10956. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10957. 'region-start-level 'region))
  10958. org-loop-over-headlines-in-active-region)
  10959. (org-map-entries
  10960. `(org-schedule ',remove ,time)
  10961. org-loop-over-headlines-in-active-region
  10962. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10963. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10964. (repeater (and old-date
  10965. (string-match
  10966. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10967. old-date)
  10968. (match-string 1 old-date))))
  10969. (if remove
  10970. (progn
  10971. (when (and old-date org-log-reschedule)
  10972. (org-add-log-setup 'delschedule nil old-date 'findpos
  10973. org-log-reschedule))
  10974. (org-remove-timestamp-with-keyword org-scheduled-string)
  10975. (message "Item is no longer scheduled."))
  10976. (org-add-planning-info 'scheduled time 'closed)
  10977. (when (and old-date org-log-reschedule
  10978. (not (equal old-date
  10979. (substring org-last-inserted-timestamp 1 -1))))
  10980. (org-add-log-setup 'reschedule nil old-date 'findpos
  10981. org-log-reschedule))
  10982. (when repeater
  10983. (save-excursion
  10984. (org-back-to-heading t)
  10985. (when (re-search-forward (concat org-scheduled-string " "
  10986. org-last-inserted-timestamp)
  10987. (save-excursion
  10988. (outline-next-heading) (point)) t)
  10989. (goto-char (1- (match-end 0)))
  10990. (insert " " repeater)
  10991. (setq org-last-inserted-timestamp
  10992. (concat (substring org-last-inserted-timestamp 0 -1)
  10993. " " repeater
  10994. (substring org-last-inserted-timestamp -1))))))
  10995. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10996. (defun org-get-scheduled-time (pom &optional inherit)
  10997. "Get the scheduled time as a time tuple, of a format suitable
  10998. for calling org-schedule with, or if there is no scheduling,
  10999. returns nil."
  11000. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11001. (when time
  11002. (apply 'encode-time (org-parse-time-string time)))))
  11003. (defun org-get-deadline-time (pom &optional inherit)
  11004. "Get the deadline as a time tuple, of a format suitable for
  11005. calling org-deadline with, or if there is no scheduling, returns
  11006. nil."
  11007. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11008. (when time
  11009. (apply 'encode-time (org-parse-time-string time)))))
  11010. (defun org-remove-timestamp-with-keyword (keyword)
  11011. "Remove all time stamps with KEYWORD in the current entry."
  11012. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11013. beg)
  11014. (save-excursion
  11015. (org-back-to-heading t)
  11016. (setq beg (point))
  11017. (outline-next-heading)
  11018. (while (re-search-backward re beg t)
  11019. (replace-match "")
  11020. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11021. (equal (char-before) ?\ ))
  11022. (backward-delete-char 1)
  11023. (if (string-match "^[ \t]*$" (buffer-substring
  11024. (point-at-bol) (point-at-eol)))
  11025. (delete-region (point-at-bol)
  11026. (min (point-max) (1+ (point-at-eol))))))))))
  11027. (defun org-add-planning-info (what &optional time &rest remove)
  11028. "Insert new timestamp with keyword in the line directly after the headline.
  11029. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11030. If non is given, the user is prompted for a date.
  11031. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11032. be removed."
  11033. (interactive)
  11034. (let (org-time-was-given org-end-time-was-given ts
  11035. end default-time default-input)
  11036. (catch 'exit
  11037. (when (and (memq what '(scheduled deadline))
  11038. (or (not time)
  11039. (and (stringp time)
  11040. (string-match "^[-+]+[0-9]" time))))
  11041. ;; Try to get a default date/time from existing timestamp
  11042. (save-excursion
  11043. (org-back-to-heading t)
  11044. (setq end (save-excursion (outline-next-heading) (point)))
  11045. (when (re-search-forward (if (eq what 'scheduled)
  11046. org-scheduled-time-regexp
  11047. org-deadline-time-regexp)
  11048. end t)
  11049. (setq ts (match-string 1)
  11050. default-time
  11051. (apply 'encode-time (org-parse-time-string ts))
  11052. default-input (and ts (org-get-compact-tod ts))))))
  11053. (when what
  11054. (setq time
  11055. (if (stringp time)
  11056. ;; This is a string (relative or absolute), set proper date
  11057. (apply 'encode-time
  11058. (org-read-date-analyze
  11059. time default-time (decode-time default-time)))
  11060. ;; If necessary, get the time from the user
  11061. (or time (org-read-date nil 'to-time nil nil
  11062. default-time default-input)))))
  11063. (when (and org-insert-labeled-timestamps-at-point
  11064. (member what '(scheduled deadline)))
  11065. (insert
  11066. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11067. (org-insert-time-stamp time org-time-was-given
  11068. nil nil nil (list org-end-time-was-given))
  11069. (setq what nil))
  11070. (save-excursion
  11071. (save-restriction
  11072. (let (col list elt ts buffer-invisibility-spec)
  11073. (org-back-to-heading t)
  11074. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11075. (goto-char (match-end 1))
  11076. (setq col (current-column))
  11077. (goto-char (match-end 0))
  11078. (if (eobp) (insert "\n") (forward-char 1))
  11079. (when (and (not what)
  11080. (not (looking-at
  11081. (concat "[ \t]*"
  11082. org-keyword-time-not-clock-regexp))))
  11083. ;; Nothing to add, nothing to remove...... :-)
  11084. (throw 'exit nil))
  11085. (if (and (not (looking-at org-outline-regexp))
  11086. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11087. "[^\r\n]*"))
  11088. (not (equal (match-string 1) org-clock-string)))
  11089. (narrow-to-region (match-beginning 0) (match-end 0))
  11090. (insert-before-markers "\n")
  11091. (backward-char 1)
  11092. (narrow-to-region (point) (point))
  11093. (and org-adapt-indentation (org-indent-to-column col)))
  11094. ;; Check if we have to remove something.
  11095. (setq list (cons what remove))
  11096. (while list
  11097. (setq elt (pop list))
  11098. (when (or (and (eq elt 'scheduled)
  11099. (re-search-forward org-scheduled-time-regexp nil t))
  11100. (and (eq elt 'deadline)
  11101. (re-search-forward org-deadline-time-regexp nil t))
  11102. (and (eq elt 'closed)
  11103. (re-search-forward org-closed-time-regexp nil t)))
  11104. (replace-match "")
  11105. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11106. (and (looking-at "[ \t]+") (replace-match ""))
  11107. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11108. (when what
  11109. (insert
  11110. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11111. (cond ((eq what 'scheduled) org-scheduled-string)
  11112. ((eq what 'deadline) org-deadline-string)
  11113. ((eq what 'closed) org-closed-string))
  11114. " ")
  11115. (setq ts (org-insert-time-stamp
  11116. time
  11117. (or org-time-was-given
  11118. (and (eq what 'closed) org-log-done-with-time))
  11119. (eq what 'closed)
  11120. nil nil (list org-end-time-was-given)))
  11121. (insert
  11122. (if (not (or (bolp) (eq (char-before) ?\ )
  11123. (memq (char-after) '(32 10))
  11124. (eobp))) " " ""))
  11125. (end-of-line 1))
  11126. (goto-char (point-min))
  11127. (widen)
  11128. (if (and (looking-at "[ \t]*\n")
  11129. (equal (char-before) ?\n))
  11130. (delete-region (1- (point)) (point-at-eol)))
  11131. ts))))))
  11132. (defvar org-log-note-marker (make-marker))
  11133. (defvar org-log-note-purpose nil)
  11134. (defvar org-log-note-state nil)
  11135. (defvar org-log-note-previous-state nil)
  11136. (defvar org-log-note-how nil)
  11137. (defvar org-log-note-extra nil)
  11138. (defvar org-log-note-window-configuration nil)
  11139. (defvar org-log-note-return-to (make-marker))
  11140. (defvar org-log-note-effective-time nil
  11141. "Remembered current time so that dynamically scoped
  11142. `org-extend-today-until' affects tha timestamps in state change
  11143. log")
  11144. (defvar org-log-post-message nil
  11145. "Message to be displayed after a log note has been stored.
  11146. The auto-repeater uses this.")
  11147. (defun org-add-note ()
  11148. "Add a note to the current entry.
  11149. This is done in the same way as adding a state change note."
  11150. (interactive)
  11151. (org-add-log-setup 'note nil nil 'findpos nil))
  11152. (defvar org-property-end-re)
  11153. (defun org-add-log-setup (&optional purpose state prev-state
  11154. findpos how extra)
  11155. "Set up the post command hook to take a note.
  11156. If this is about to TODO state change, the new state is expected in STATE.
  11157. When FINDPOS is non-nil, find the correct position for the note in
  11158. the current entry. If not, assume that it can be inserted at point.
  11159. HOW is an indicator what kind of note should be created.
  11160. EXTRA is additional text that will be inserted into the notes buffer."
  11161. (let* ((org-log-into-drawer (org-log-into-drawer))
  11162. (drawer (cond ((stringp org-log-into-drawer)
  11163. org-log-into-drawer)
  11164. (org-log-into-drawer "LOGBOOK")
  11165. (t nil))))
  11166. (save-restriction
  11167. (save-excursion
  11168. (when findpos
  11169. (org-back-to-heading t)
  11170. (narrow-to-region (point) (save-excursion
  11171. (outline-next-heading) (point)))
  11172. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11173. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11174. "[^\r\n]*\\)?"))
  11175. (goto-char (match-end 0))
  11176. (cond
  11177. (drawer
  11178. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11179. nil t)
  11180. (progn
  11181. (goto-char (match-end 0))
  11182. (or org-log-states-order-reversed
  11183. (and (re-search-forward org-property-end-re nil t)
  11184. (goto-char (1- (match-beginning 0))))))
  11185. (insert "\n:" drawer ":\n:END:")
  11186. (beginning-of-line 0)
  11187. (org-indent-line-function)
  11188. (beginning-of-line 2)
  11189. (org-indent-line-function)
  11190. (end-of-line 0)))
  11191. ((and org-log-state-notes-insert-after-drawers
  11192. (save-excursion
  11193. (forward-line) (looking-at org-drawer-regexp)))
  11194. (forward-line)
  11195. (while (looking-at org-drawer-regexp)
  11196. (goto-char (match-end 0))
  11197. (re-search-forward org-property-end-re (point-max) t)
  11198. (forward-line))
  11199. (forward-line -1)))
  11200. (unless org-log-states-order-reversed
  11201. (and (= (char-after) ?\n) (forward-char 1))
  11202. (org-skip-over-state-notes)
  11203. (skip-chars-backward " \t\n\r")))
  11204. (move-marker org-log-note-marker (point))
  11205. (setq org-log-note-purpose purpose
  11206. org-log-note-state state
  11207. org-log-note-previous-state prev-state
  11208. org-log-note-how how
  11209. org-log-note-extra extra
  11210. org-log-note-effective-time (org-current-effective-time))
  11211. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11212. (defun org-skip-over-state-notes ()
  11213. "Skip past the list of State notes in an entry."
  11214. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11215. (when (ignore-errors (goto-char (org-in-item-p)))
  11216. (let* ((struct (org-list-struct))
  11217. (prevs (org-list-prevs-alist struct)))
  11218. (while (looking-at "[ \t]*- State")
  11219. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11220. (org-list-get-item-end (point) struct)))))))
  11221. (defun org-add-log-note (&optional purpose)
  11222. "Pop up a window for taking a note, and add this note later at point."
  11223. (remove-hook 'post-command-hook 'org-add-log-note)
  11224. (setq org-log-note-window-configuration (current-window-configuration))
  11225. (delete-other-windows)
  11226. (move-marker org-log-note-return-to (point))
  11227. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11228. (goto-char org-log-note-marker)
  11229. (org-switch-to-buffer-other-window "*Org Note*")
  11230. (erase-buffer)
  11231. (if (memq org-log-note-how '(time state))
  11232. (let (current-prefix-arg) (org-store-log-note))
  11233. (let ((org-inhibit-startup t)) (org-mode))
  11234. (insert (format "# Insert note for %s.
  11235. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11236. (cond
  11237. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11238. ((eq org-log-note-purpose 'done) "closed todo item")
  11239. ((eq org-log-note-purpose 'state)
  11240. (format "state change from \"%s\" to \"%s\""
  11241. (or org-log-note-previous-state "")
  11242. (or org-log-note-state "")))
  11243. ((eq org-log-note-purpose 'reschedule)
  11244. "rescheduling")
  11245. ((eq org-log-note-purpose 'delschedule)
  11246. "no longer scheduled")
  11247. ((eq org-log-note-purpose 'redeadline)
  11248. "changing deadline")
  11249. ((eq org-log-note-purpose 'deldeadline)
  11250. "removing deadline")
  11251. ((eq org-log-note-purpose 'refile)
  11252. "refiling")
  11253. ((eq org-log-note-purpose 'note)
  11254. "this entry")
  11255. (t (error "This should not happen")))))
  11256. (if org-log-note-extra (insert org-log-note-extra))
  11257. (org-set-local 'org-finish-function 'org-store-log-note)
  11258. (run-hooks 'org-log-buffer-setup-hook)))
  11259. (defvar org-note-abort nil) ; dynamically scoped
  11260. (defun org-store-log-note ()
  11261. "Finish taking a log note, and insert it to where it belongs."
  11262. (let ((txt (buffer-string))
  11263. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11264. lines ind bul)
  11265. (kill-buffer (current-buffer))
  11266. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11267. (setq txt (replace-match "" t t txt)))
  11268. (if (string-match "\\s-+\\'" txt)
  11269. (setq txt (replace-match "" t t txt)))
  11270. (setq lines (org-split-string txt "\n"))
  11271. (when (and note (string-match "\\S-" note))
  11272. (setq note
  11273. (org-replace-escapes
  11274. note
  11275. (list (cons "%u" (user-login-name))
  11276. (cons "%U" user-full-name)
  11277. (cons "%t" (format-time-string
  11278. (org-time-stamp-format 'long 'inactive)
  11279. org-log-note-effective-time))
  11280. (cons "%T" (format-time-string
  11281. (org-time-stamp-format 'long nil)
  11282. org-log-note-effective-time))
  11283. (cons "%d" (format-time-string
  11284. (org-time-stamp-format nil 'inactive)
  11285. org-log-note-effective-time))
  11286. (cons "%D" (format-time-string
  11287. (org-time-stamp-format nil nil)
  11288. org-log-note-effective-time))
  11289. (cons "%s" (if org-log-note-state
  11290. (concat "\"" org-log-note-state "\"")
  11291. ""))
  11292. (cons "%S" (if org-log-note-previous-state
  11293. (concat "\"" org-log-note-previous-state "\"")
  11294. "\"\"")))))
  11295. (if lines (setq note (concat note " \\\\")))
  11296. (push note lines))
  11297. (when (or current-prefix-arg org-note-abort)
  11298. (when org-log-into-drawer
  11299. (org-remove-empty-drawer-at
  11300. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11301. org-log-note-marker))
  11302. (setq lines nil))
  11303. (when lines
  11304. (with-current-buffer (marker-buffer org-log-note-marker)
  11305. (save-excursion
  11306. (goto-char org-log-note-marker)
  11307. (move-marker org-log-note-marker nil)
  11308. (end-of-line 1)
  11309. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11310. (setq ind (save-excursion
  11311. (if (ignore-errors (goto-char (org-in-item-p)))
  11312. (let ((struct (org-list-struct)))
  11313. (org-list-get-ind
  11314. (org-list-get-top-point struct) struct))
  11315. (skip-chars-backward " \r\t\n")
  11316. (cond
  11317. ((and (org-at-heading-p)
  11318. org-adapt-indentation)
  11319. (1+ (org-current-level)))
  11320. ((org-at-heading-p) 0)
  11321. (t (org-get-indentation))))))
  11322. (setq bul (org-list-bullet-string "-"))
  11323. (org-indent-line-to ind)
  11324. (insert bul (pop lines))
  11325. (let ((ind-body (+ (length bul) ind)))
  11326. (while lines
  11327. (insert "\n")
  11328. (org-indent-line-to ind-body)
  11329. (insert (pop lines))))
  11330. (message "Note stored")
  11331. (org-back-to-heading t)
  11332. (org-cycle-hide-drawers 'children)))))
  11333. (set-window-configuration org-log-note-window-configuration)
  11334. (with-current-buffer (marker-buffer org-log-note-return-to)
  11335. (goto-char org-log-note-return-to))
  11336. (move-marker org-log-note-return-to nil)
  11337. (and org-log-post-message (message "%s" org-log-post-message)))
  11338. (defun org-remove-empty-drawer-at (drawer pos)
  11339. "Remove an empty drawer DRAWER at position POS.
  11340. POS may also be a marker."
  11341. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11342. (save-excursion
  11343. (save-restriction
  11344. (widen)
  11345. (goto-char pos)
  11346. (if (org-in-regexp
  11347. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11348. (replace-match ""))))))
  11349. (defun org-sparse-tree (&optional arg)
  11350. "Create a sparse tree, prompt for the details.
  11351. This command can create sparse trees. You first need to select the type
  11352. of match used to create the tree:
  11353. t Show all TODO entries.
  11354. T Show entries with a specific TODO keyword.
  11355. m Show entries selected by a tags/property match.
  11356. p Enter a property name and its value (both with completion on existing
  11357. names/values) and show entries with that property.
  11358. r Show entries matching a regular expression (`/' can be used as well)
  11359. d Show deadlines due within `org-deadline-warning-days'.
  11360. b Show deadlines and scheduled items before a date.
  11361. a Show deadlines and scheduled items after a date."
  11362. (interactive "P")
  11363. (let (ans kwd value)
  11364. (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")
  11365. (setq ans (read-char-exclusive))
  11366. (cond
  11367. ((equal ans ?d)
  11368. (call-interactively 'org-check-deadlines))
  11369. ((equal ans ?b)
  11370. (call-interactively 'org-check-before-date))
  11371. ((equal ans ?a)
  11372. (call-interactively 'org-check-after-date))
  11373. ((equal ans ?D)
  11374. (call-interactively 'org-check-dates-range))
  11375. ((equal ans ?t)
  11376. (org-show-todo-tree nil))
  11377. ((equal ans ?T)
  11378. (org-show-todo-tree '(4)))
  11379. ((member ans '(?T ?m))
  11380. (call-interactively 'org-match-sparse-tree))
  11381. ((member ans '(?p ?P))
  11382. (setq kwd (org-icompleting-read "Property: "
  11383. (mapcar 'list (org-buffer-property-keys))))
  11384. (setq value (org-icompleting-read "Value: "
  11385. (mapcar 'list (org-property-values kwd))))
  11386. (unless (string-match "\\`{.*}\\'" value)
  11387. (setq value (concat "\"" value "\"")))
  11388. (org-match-sparse-tree arg (concat kwd "=" value)))
  11389. ((member ans '(?r ?R ?/))
  11390. (call-interactively 'org-occur))
  11391. (t (error "No such sparse tree command \"%c\"" ans)))))
  11392. (defvar org-occur-highlights nil
  11393. "List of overlays used for occur matches.")
  11394. (make-variable-buffer-local 'org-occur-highlights)
  11395. (defvar org-occur-parameters nil
  11396. "Parameters of the active org-occur calls.
  11397. This is a list, each call to org-occur pushes as cons cell,
  11398. containing the regular expression and the callback, onto the list.
  11399. The list can contain several entries if `org-occur' has been called
  11400. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11401. will only contain one set of parameters. When the highlights are
  11402. removed (for example with `C-c C-c', or with the next edit (depending
  11403. on `org-remove-highlights-with-change'), this variable is emptied
  11404. as well.")
  11405. (make-variable-buffer-local 'org-occur-parameters)
  11406. (defun org-occur (regexp &optional keep-previous callback)
  11407. "Make a compact tree which shows all matches of REGEXP.
  11408. The tree will show the lines where the regexp matches, and all higher
  11409. headlines above the match. It will also show the heading after the match,
  11410. to make sure editing the matching entry is easy.
  11411. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11412. call to `org-occur' will be kept, to allow stacking of calls to this
  11413. command.
  11414. If CALLBACK is non-nil, it is a function which is called to confirm
  11415. that the match should indeed be shown."
  11416. (interactive "sRegexp: \nP")
  11417. (when (equal regexp "")
  11418. (error "Regexp cannot be empty"))
  11419. (unless keep-previous
  11420. (org-remove-occur-highlights nil nil t))
  11421. (push (cons regexp callback) org-occur-parameters)
  11422. (let ((cnt 0))
  11423. (save-excursion
  11424. (goto-char (point-min))
  11425. (if (or (not keep-previous) ; do not want to keep
  11426. (not org-occur-highlights)) ; no previous matches
  11427. ;; hide everything
  11428. (org-overview))
  11429. (while (re-search-forward regexp nil t)
  11430. (when (or (not callback)
  11431. (save-match-data (funcall callback)))
  11432. (setq cnt (1+ cnt))
  11433. (when org-highlight-sparse-tree-matches
  11434. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11435. (org-show-context 'occur-tree))))
  11436. (when org-remove-highlights-with-change
  11437. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11438. nil 'local))
  11439. (unless org-sparse-tree-open-archived-trees
  11440. (org-hide-archived-subtrees (point-min) (point-max)))
  11441. (run-hooks 'org-occur-hook)
  11442. (if (org-called-interactively-p 'interactive)
  11443. (message "%d match(es) for regexp %s" cnt regexp))
  11444. cnt))
  11445. (defun org-occur-next-match (&optional n reset)
  11446. "Function for `next-error-function' to find sparse tree matches.
  11447. N is the number of matches to move, when negative move backwards.
  11448. RESET is entirely ignored - this function always goes back to the
  11449. starting point when no match is found."
  11450. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11451. (search-func (if (< n 0)
  11452. 'previous-single-char-property-change
  11453. 'next-single-char-property-change))
  11454. (n (abs n))
  11455. (pos (point))
  11456. p1)
  11457. (catch 'exit
  11458. (while (setq p1 (funcall search-func (point) 'org-type))
  11459. (when (equal p1 limit)
  11460. (goto-char pos)
  11461. (error "No more matches"))
  11462. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11463. (setq n (1- n))
  11464. (when (= n 0)
  11465. (goto-char p1)
  11466. (throw 'exit (point))))
  11467. (goto-char p1))
  11468. (goto-char p1)
  11469. (error "No more matches"))))
  11470. (defun org-show-context (&optional key)
  11471. "Make sure point and context are visible.
  11472. How much context is shown depends upon the variables
  11473. `org-show-hierarchy-above', `org-show-following-heading',
  11474. `org-show-entry-below' and `org-show-siblings'."
  11475. (let ((heading-p (org-at-heading-p t))
  11476. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11477. (following-p (org-get-alist-option org-show-following-heading key))
  11478. (entry-p (org-get-alist-option org-show-entry-below key))
  11479. (siblings-p (org-get-alist-option org-show-siblings key)))
  11480. (catch 'exit
  11481. ;; Show heading or entry text
  11482. (if (and heading-p (not entry-p))
  11483. (org-flag-heading nil) ; only show the heading
  11484. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11485. (org-show-hidden-entry))) ; show entire entry
  11486. (when following-p
  11487. ;; Show next sibling, or heading below text
  11488. (save-excursion
  11489. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11490. (org-flag-heading nil))))
  11491. (when siblings-p (org-show-siblings))
  11492. (when hierarchy-p
  11493. ;; show all higher headings, possibly with siblings
  11494. (save-excursion
  11495. (while (and (condition-case nil
  11496. (progn (org-up-heading-all 1) t)
  11497. (error nil))
  11498. (not (bobp)))
  11499. (org-flag-heading nil)
  11500. (when siblings-p (org-show-siblings))))))))
  11501. (defvar org-reveal-start-hook nil
  11502. "Hook run before revealing a location.")
  11503. (defun org-reveal (&optional siblings)
  11504. "Show current entry, hierarchy above it, and the following headline.
  11505. This can be used to show a consistent set of context around locations
  11506. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11507. not t for the search context.
  11508. With optional argument SIBLINGS, on each level of the hierarchy all
  11509. siblings are shown. This repairs the tree structure to what it would
  11510. look like when opened with hierarchical calls to `org-cycle'.
  11511. With double optional argument \\[universal-argument] \\[universal-argument], \
  11512. go to the parent and show the
  11513. entire tree."
  11514. (interactive "P")
  11515. (run-hooks 'org-reveal-start-hook)
  11516. (let ((org-show-hierarchy-above t)
  11517. (org-show-following-heading t)
  11518. (org-show-siblings (if siblings t org-show-siblings)))
  11519. (org-show-context nil))
  11520. (when (equal siblings '(16))
  11521. (save-excursion
  11522. (when (org-up-heading-safe)
  11523. (org-show-subtree)
  11524. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11525. (defun org-highlight-new-match (beg end)
  11526. "Highlight from BEG to END and mark the highlight is an occur headline."
  11527. (let ((ov (make-overlay beg end)))
  11528. (overlay-put ov 'face 'secondary-selection)
  11529. (overlay-put ov 'org-type 'org-occur)
  11530. (push ov org-occur-highlights)))
  11531. (defun org-remove-occur-highlights (&optional beg end noremove)
  11532. "Remove the occur highlights from the buffer.
  11533. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11534. from the `before-change-functions' in the current buffer."
  11535. (interactive)
  11536. (unless org-inhibit-highlight-removal
  11537. (mapc 'delete-overlay org-occur-highlights)
  11538. (setq org-occur-highlights nil)
  11539. (setq org-occur-parameters nil)
  11540. (unless noremove
  11541. (remove-hook 'before-change-functions
  11542. 'org-remove-occur-highlights 'local))))
  11543. ;;;; Priorities
  11544. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11545. "Regular expression matching the priority indicator.")
  11546. (defvar org-remove-priority-next-time nil)
  11547. (defun org-priority-up ()
  11548. "Increase the priority of the current item."
  11549. (interactive)
  11550. (org-priority 'up))
  11551. (defun org-priority-down ()
  11552. "Decrease the priority of the current item."
  11553. (interactive)
  11554. (org-priority 'down))
  11555. (defun org-priority (&optional action)
  11556. "Change the priority of an item by ARG.
  11557. ACTION can be `set', `up', `down', or a character."
  11558. (interactive)
  11559. (unless org-enable-priority-commands
  11560. (error "Priority commands are disabled"))
  11561. (setq action (or action 'set))
  11562. (let (current new news have remove)
  11563. (save-excursion
  11564. (org-back-to-heading t)
  11565. (if (looking-at org-priority-regexp)
  11566. (setq current (string-to-char (match-string 2))
  11567. have t))
  11568. (cond
  11569. ((eq action 'remove)
  11570. (setq remove t new ?\ ))
  11571. ((or (eq action 'set)
  11572. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11573. (if (not (eq action 'set))
  11574. (setq new action)
  11575. (message "Priority %c-%c, SPC to remove: "
  11576. org-highest-priority org-lowest-priority)
  11577. (save-match-data
  11578. (setq new (read-char-exclusive))))
  11579. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11580. (= (upcase org-lowest-priority) org-lowest-priority))
  11581. (setq new (upcase new)))
  11582. (cond ((equal new ?\ ) (setq remove t))
  11583. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11584. (error "Priority must be between `%c' and `%c'"
  11585. org-highest-priority org-lowest-priority))))
  11586. ((eq action 'up)
  11587. (setq new (if have
  11588. (1- current) ; normal cycling
  11589. ;; last priority was empty
  11590. (if (eq last-command this-command)
  11591. org-lowest-priority ; wrap around empty to lowest
  11592. ;; default
  11593. (if org-priority-start-cycle-with-default
  11594. org-default-priority
  11595. (1- org-default-priority))))))
  11596. ((eq action 'down)
  11597. (setq new (if have
  11598. (1+ current) ; normal cycling
  11599. ;; last priority was empty
  11600. (if (eq last-command this-command)
  11601. org-highest-priority ; wrap around empty to highest
  11602. ;; default
  11603. (if org-priority-start-cycle-with-default
  11604. org-default-priority
  11605. (1+ org-default-priority))))))
  11606. (t (error "Invalid action")))
  11607. (if (or (< (upcase new) org-highest-priority)
  11608. (> (upcase new) org-lowest-priority))
  11609. (if (and (memq action '(up down))
  11610. (not have) (not (eq last-command this-command)))
  11611. ;; `new' is from default priority
  11612. (error
  11613. "The default can not be set, see `org-default-priority' why")
  11614. ;; normal cycling: `new' is beyond highest/lowest priority
  11615. ;; and is wrapped around to the empty priority
  11616. (setq remove t)))
  11617. (setq news (format "%c" new))
  11618. (if have
  11619. (if remove
  11620. (replace-match "" t t nil 1)
  11621. (replace-match news t t nil 2))
  11622. (if remove
  11623. (error "No priority cookie found in line")
  11624. (let ((case-fold-search nil))
  11625. (looking-at org-todo-line-regexp))
  11626. (if (match-end 2)
  11627. (progn
  11628. (goto-char (match-end 2))
  11629. (insert " [#" news "]"))
  11630. (goto-char (match-beginning 3))
  11631. (insert "[#" news "] "))))
  11632. (org-preserve-lc (org-set-tags nil 'align)))
  11633. (if remove
  11634. (message "Priority removed")
  11635. (message "Priority of current item set to %s" news))))
  11636. (defun org-get-priority (s)
  11637. "Find priority cookie and return priority."
  11638. (if (functionp org-get-priority-function)
  11639. (funcall org-get-priority-function)
  11640. (save-match-data
  11641. (if (not (string-match org-priority-regexp s))
  11642. (* 1000 (- org-lowest-priority org-default-priority))
  11643. (* 1000 (- org-lowest-priority
  11644. (string-to-char (match-string 2 s))))))))
  11645. ;;;; Tags
  11646. (defvar org-agenda-archives-mode)
  11647. (defvar org-map-continue-from nil
  11648. "Position from where mapping should continue.
  11649. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11650. (defvar org-scanner-tags nil
  11651. "The current tag list while the tags scanner is running.")
  11652. (defvar org-trust-scanner-tags nil
  11653. "Should `org-get-tags-at' use the tags for the scanner.
  11654. This is for internal dynamical scoping only.
  11655. When this is non-nil, the function `org-get-tags-at' will return the value
  11656. of `org-scanner-tags' instead of building the list by itself. This
  11657. can lead to large speed-ups when the tags scanner is used in a file with
  11658. many entries, and when the list of tags is retrieved, for example to
  11659. obtain a list of properties. Building the tags list for each entry in such
  11660. a file becomes an N^2 operation - but with this variable set, it scales
  11661. as N.")
  11662. (defun org-scan-tags (action matcher &optional todo-only start-level)
  11663. "Scan headline tags with inheritance and produce output ACTION.
  11664. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11665. or `agenda' to produce an entry list for an agenda view. It can also be
  11666. a Lisp form or a function that should be called at each matched headline, in
  11667. this case the return value is a list of all return values from these calls.
  11668. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11669. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11670. only lines with a TODO keyword are included in the output.
  11671. START-LEVEL can be a string with asterisks, reducing the scope to
  11672. headlines matching this string."
  11673. (require 'org-agenda)
  11674. (let* ((re (concat "^"
  11675. (if start-level
  11676. ;; Get the correct level to match
  11677. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11678. org-outline-regexp)
  11679. " *\\(\\<\\("
  11680. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11681. (org-re
  11682. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11683. (props (list 'face 'default
  11684. 'done-face 'org-agenda-done
  11685. 'undone-face 'default
  11686. 'mouse-face 'highlight
  11687. 'org-not-done-regexp org-not-done-regexp
  11688. 'org-todo-regexp org-todo-regexp
  11689. 'org-complex-heading-regexp org-complex-heading-regexp
  11690. 'help-echo
  11691. (format "mouse-2 or RET jump to org file %s"
  11692. (abbreviate-file-name
  11693. (or (buffer-file-name (buffer-base-buffer))
  11694. (buffer-name (buffer-base-buffer)))))))
  11695. (case-fold-search nil)
  11696. (org-map-continue-from nil)
  11697. lspos tags
  11698. (tags-alist (list (cons 0 org-file-tags)))
  11699. (llast 0) rtn rtn1 level category i txt
  11700. todo marker entry priority)
  11701. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11702. (setq action (list 'lambda nil action)))
  11703. (save-excursion
  11704. (goto-char (point-min))
  11705. (when (eq action 'sparse-tree)
  11706. (org-overview)
  11707. (org-remove-occur-highlights))
  11708. (while (re-search-forward re nil t)
  11709. (setq org-map-continue-from nil)
  11710. (catch :skip
  11711. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11712. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11713. (goto-char (setq lspos (match-beginning 0)))
  11714. (setq level (org-reduced-level (funcall outline-level))
  11715. category (org-get-category))
  11716. (setq i llast llast level)
  11717. ;; remove tag lists from same and sublevels
  11718. (while (>= i level)
  11719. (when (setq entry (assoc i tags-alist))
  11720. (setq tags-alist (delete entry tags-alist)))
  11721. (setq i (1- i)))
  11722. ;; add the next tags
  11723. (when tags
  11724. (setq tags (org-split-string tags ":")
  11725. tags-alist
  11726. (cons (cons level tags) tags-alist)))
  11727. ;; compile tags for current headline
  11728. (setq tags-list
  11729. (if org-use-tag-inheritance
  11730. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11731. tags)
  11732. org-scanner-tags tags-list)
  11733. (when org-use-tag-inheritance
  11734. (setcdr (car tags-alist)
  11735. (mapcar (lambda (x)
  11736. (setq x (copy-sequence x))
  11737. (org-add-prop-inherited x))
  11738. (cdar tags-alist))))
  11739. (when (and tags org-use-tag-inheritance
  11740. (or (not (eq t org-use-tag-inheritance))
  11741. org-tags-exclude-from-inheritance))
  11742. ;; selective inheritance, remove uninherited ones
  11743. (setcdr (car tags-alist)
  11744. (org-remove-uninherited-tags (cdar tags-alist))))
  11745. (when (and
  11746. ;; eval matcher only when the todo condition is OK
  11747. (and (or (not todo-only) (member todo org-not-done-keywords))
  11748. (let ((case-fold-search t)) (eval matcher)))
  11749. ;; Call the skipper, but return t if it does not skip,
  11750. ;; so that the `and' form continues evaluating
  11751. (progn
  11752. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11753. t)
  11754. ;; Check if timestamps are deselecting this entry
  11755. (or (not todo-only)
  11756. (and (member todo org-not-done-keywords)
  11757. (or (not org-agenda-tags-todo-honor-ignore-options)
  11758. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11759. ;; Extra check for the archive tag
  11760. ;; FIXME: Does the skipper already do this????
  11761. (or
  11762. (not (member org-archive-tag tags-list))
  11763. ;; we have an archive tag, should we use this anyway?
  11764. (or (not org-agenda-skip-archived-trees)
  11765. (and (eq action 'agenda) org-agenda-archives-mode))))
  11766. ;; select this headline
  11767. (cond
  11768. ((eq action 'sparse-tree)
  11769. (and org-highlight-sparse-tree-matches
  11770. (org-get-heading) (match-end 0)
  11771. (org-highlight-new-match
  11772. (match-beginning 1) (match-end 1)))
  11773. (org-show-context 'tags-tree))
  11774. ((eq action 'agenda)
  11775. (setq txt (org-agenda-format-item
  11776. ""
  11777. (concat
  11778. (if (eq org-tags-match-list-sublevels 'indented)
  11779. (make-string (1- level) ?.) "")
  11780. (org-get-heading))
  11781. category
  11782. tags-list)
  11783. priority (org-get-priority txt))
  11784. (goto-char lspos)
  11785. (setq marker (org-agenda-new-marker))
  11786. (org-add-props txt props
  11787. 'org-marker marker 'org-hd-marker marker 'org-category category
  11788. 'todo-state todo
  11789. 'priority priority 'type "tagsmatch")
  11790. (push txt rtn))
  11791. ((functionp action)
  11792. (setq org-map-continue-from nil)
  11793. (save-excursion
  11794. (setq rtn1 (funcall action))
  11795. (push rtn1 rtn)))
  11796. (t (error "Invalid action")))
  11797. ;; if we are to skip sublevels, jump to end of subtree
  11798. (unless org-tags-match-list-sublevels
  11799. (org-end-of-subtree t)
  11800. (backward-char 1))))
  11801. ;; Get the correct position from where to continue
  11802. (if org-map-continue-from
  11803. (goto-char org-map-continue-from)
  11804. (and (= (point) lspos) (end-of-line 1)))))
  11805. (when (and (eq action 'sparse-tree)
  11806. (not org-sparse-tree-open-archived-trees))
  11807. (org-hide-archived-subtrees (point-min) (point-max)))
  11808. (nreverse rtn)))
  11809. (defun org-remove-uninherited-tags (tags)
  11810. "Remove all tags that are not inherited from the list TAGS."
  11811. (cond
  11812. ((eq org-use-tag-inheritance t)
  11813. (if org-tags-exclude-from-inheritance
  11814. (org-delete-all org-tags-exclude-from-inheritance tags)
  11815. tags))
  11816. ((not org-use-tag-inheritance) nil)
  11817. ((stringp org-use-tag-inheritance)
  11818. (delq nil (mapcar
  11819. (lambda (x)
  11820. (if (and (string-match org-use-tag-inheritance x)
  11821. (not (member x org-tags-exclude-from-inheritance)))
  11822. x nil))
  11823. tags)))
  11824. ((listp org-use-tag-inheritance)
  11825. (delq nil (mapcar
  11826. (lambda (x)
  11827. (if (member x org-use-tag-inheritance) x nil))
  11828. tags)))))
  11829. (defvar todo-only) ;; dynamically scoped
  11830. (defun org-match-sparse-tree (&optional todo-only match)
  11831. "Create a sparse tree according to tags string MATCH.
  11832. MATCH can contain positive and negative selection of tags, like
  11833. \"+WORK+URGENT-WITHBOSS\".
  11834. If optional argument TODO-ONLY is non-nil, only select lines that are
  11835. also TODO lines."
  11836. (interactive "P")
  11837. (org-prepare-agenda-buffers (list (current-buffer)))
  11838. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11839. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11840. (defvar org-cached-props nil)
  11841. (defun org-cached-entry-get (pom property)
  11842. (if (or (eq t org-use-property-inheritance)
  11843. (and (stringp org-use-property-inheritance)
  11844. (string-match org-use-property-inheritance property))
  11845. (and (listp org-use-property-inheritance)
  11846. (member property org-use-property-inheritance)))
  11847. ;; Caching is not possible, check it directly
  11848. (org-entry-get pom property 'inherit)
  11849. ;; Get all properties, so that we can do complicated checks easily
  11850. (cdr (assoc property (or org-cached-props
  11851. (setq org-cached-props
  11852. (org-entry-properties pom)))))))
  11853. (defun org-global-tags-completion-table (&optional files)
  11854. "Return the list of all tags in all agenda buffer/files.
  11855. Optional FILES argument is a list of files to which can be used
  11856. instead of the agenda files."
  11857. (save-excursion
  11858. (org-uniquify
  11859. (delq nil
  11860. (apply 'append
  11861. (mapcar
  11862. (lambda (file)
  11863. (set-buffer (find-file-noselect file))
  11864. (append (org-get-buffer-tags)
  11865. (mapcar (lambda (x) (if (stringp (car-safe x))
  11866. (list (car-safe x)) nil))
  11867. org-tag-alist)))
  11868. (if (and files (car files))
  11869. files
  11870. (org-agenda-files))))))))
  11871. (defun org-make-tags-matcher (match)
  11872. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11873. ;; todo-only is scoped dynamically into this function, and the function
  11874. ;; may change it if the matcher asks for it.
  11875. (unless match
  11876. ;; Get a new match request, with completion
  11877. (let ((org-last-tags-completion-table
  11878. (org-global-tags-completion-table)))
  11879. (setq match (org-completing-read-no-i
  11880. "Match: " 'org-tags-completion-function nil nil nil
  11881. 'org-tags-history))))
  11882. ;; Parse the string and create a lisp form
  11883. (let ((match0 match)
  11884. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11885. minus tag mm
  11886. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11887. orterms term orlist re-p str-p level-p level-op time-p
  11888. prop-p pn pv po gv rest)
  11889. (if (string-match "/+" match)
  11890. ;; match contains also a todo-matching request
  11891. (progn
  11892. (setq tagsmatch (substring match 0 (match-beginning 0))
  11893. todomatch (substring match (match-end 0)))
  11894. (if (string-match "^!" todomatch)
  11895. (setq todo-only t todomatch (substring todomatch 1)))
  11896. (if (string-match "^\\s-*$" todomatch)
  11897. (setq todomatch nil)))
  11898. ;; only matching tags
  11899. (setq tagsmatch match todomatch nil))
  11900. ;; Make the tags matcher
  11901. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11902. (setq tagsmatcher t)
  11903. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11904. (while (setq term (pop orterms))
  11905. (while (and (equal (substring term -1) "\\") orterms)
  11906. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11907. (while (string-match re term)
  11908. (setq rest (substring term (match-end 0))
  11909. minus (and (match-end 1)
  11910. (equal (match-string 1 term) "-"))
  11911. tag (save-match-data (replace-regexp-in-string
  11912. "\\\\-" "-"
  11913. (match-string 2 term)))
  11914. re-p (equal (string-to-char tag) ?{)
  11915. level-p (match-end 4)
  11916. prop-p (match-end 5)
  11917. mm (cond
  11918. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11919. (level-p
  11920. (setq level-op (org-op-to-function (match-string 3 term)))
  11921. `(,level-op level ,(string-to-number
  11922. (match-string 4 term))))
  11923. (prop-p
  11924. (setq pn (match-string 5 term)
  11925. po (match-string 6 term)
  11926. pv (match-string 7 term)
  11927. re-p (equal (string-to-char pv) ?{)
  11928. str-p (equal (string-to-char pv) ?\")
  11929. time-p (save-match-data
  11930. (string-match "^\"[[<].*[]>]\"$" pv))
  11931. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11932. (if time-p (setq pv (org-matcher-time pv)))
  11933. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11934. (cond
  11935. ((equal pn "CATEGORY")
  11936. (setq gv '(get-text-property (point) 'org-category)))
  11937. ((equal pn "TODO")
  11938. (setq gv 'todo))
  11939. (t
  11940. (setq gv `(org-cached-entry-get nil ,pn))))
  11941. (if re-p
  11942. (if (eq po 'org<>)
  11943. `(not (string-match ,pv (or ,gv "")))
  11944. `(string-match ,pv (or ,gv "")))
  11945. (if str-p
  11946. `(,po (or ,gv "") ,pv)
  11947. `(,po (string-to-number (or ,gv ""))
  11948. ,(string-to-number pv) ))))
  11949. (t `(member ,tag tags-list)))
  11950. mm (if minus (list 'not mm) mm)
  11951. term rest)
  11952. (push mm tagsmatcher))
  11953. (push (if (> (length tagsmatcher) 1)
  11954. (cons 'and tagsmatcher)
  11955. (car tagsmatcher))
  11956. orlist)
  11957. (setq tagsmatcher nil))
  11958. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11959. (setq tagsmatcher
  11960. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11961. ;; Make the todo matcher
  11962. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11963. (setq todomatcher t)
  11964. (setq orterms (org-split-string todomatch "|") orlist nil)
  11965. (while (setq term (pop orterms))
  11966. (while (string-match re term)
  11967. (setq minus (and (match-end 1)
  11968. (equal (match-string 1 term) "-"))
  11969. kwd (match-string 2 term)
  11970. re-p (equal (string-to-char kwd) ?{)
  11971. term (substring term (match-end 0))
  11972. mm (if re-p
  11973. `(string-match ,(substring kwd 1 -1) todo)
  11974. (list 'equal 'todo kwd))
  11975. mm (if minus (list 'not mm) mm))
  11976. (push mm todomatcher))
  11977. (push (if (> (length todomatcher) 1)
  11978. (cons 'and todomatcher)
  11979. (car todomatcher))
  11980. orlist)
  11981. (setq todomatcher nil))
  11982. (setq todomatcher (if (> (length orlist) 1)
  11983. (cons 'or orlist) (car orlist))))
  11984. ;; Return the string and lisp forms of the matcher
  11985. (setq matcher (if todomatcher
  11986. (list 'and tagsmatcher todomatcher)
  11987. tagsmatcher))
  11988. (cons match0 matcher)))
  11989. (defun org-op-to-function (op &optional stringp)
  11990. "Turn an operator into the appropriate function."
  11991. (setq op
  11992. (cond
  11993. ((equal op "<" ) '(< string< org-time<))
  11994. ((equal op ">" ) '(> org-string> org-time>))
  11995. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11996. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11997. ((member op '("=" "==")) '(= string= org-time=))
  11998. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11999. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12000. (defun org<> (a b) (not (= a b)))
  12001. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12002. (defun org-string>= (a b) (not (string< a b)))
  12003. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12004. (defun org-string<> (a b) (not (string= a b)))
  12005. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12006. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12007. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12008. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12009. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12010. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12011. (defun org-2ft (s)
  12012. "Convert S to a floating point time.
  12013. If S is already a number, just return it. If it is a string, parse
  12014. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12015. (cond
  12016. ((numberp s) s)
  12017. ((stringp s)
  12018. (condition-case nil
  12019. (float-time (apply 'encode-time (org-parse-time-string s)))
  12020. (error 0.)))
  12021. (t 0.)))
  12022. (defun org-time-today ()
  12023. "Time in seconds today at 0:00.
  12024. Returns the float number of seconds since the beginning of the
  12025. epoch to the beginning of today (00:00)."
  12026. (float-time (apply 'encode-time
  12027. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12028. (defun org-matcher-time (s)
  12029. "Interpret a time comparison value."
  12030. (save-match-data
  12031. (cond
  12032. ((string= s "<now>") (float-time))
  12033. ((string= s "<today>") (org-time-today))
  12034. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12035. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12036. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  12037. (+ (org-time-today)
  12038. (* (string-to-number (match-string 1 s))
  12039. (cdr (assoc (match-string 2 s)
  12040. '(("d" . 86400.0) ("w" . 604800.0)
  12041. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12042. (t (org-2ft s)))))
  12043. (defun org-match-any-p (re list)
  12044. "Does re match any element of list?"
  12045. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12046. (delq nil list))
  12047. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12048. (defvar org-tags-overlay (make-overlay 1 1))
  12049. (org-detach-overlay org-tags-overlay)
  12050. (defun org-get-local-tags-at (&optional pos)
  12051. "Get a list of tags defined in the current headline."
  12052. (org-get-tags-at pos 'local))
  12053. (defun org-get-local-tags ()
  12054. "Get a list of tags defined in the current headline."
  12055. (org-get-tags-at nil 'local))
  12056. (defun org-get-tags-at (&optional pos local)
  12057. "Get a list of all headline tags applicable at POS.
  12058. POS defaults to point. If tags are inherited, the list contains
  12059. the targets in the same sequence as the headlines appear, i.e.
  12060. the tags of the current headline come last.
  12061. When LOCAL is non-nil, only return tags from the current headline,
  12062. ignore inherited ones."
  12063. (interactive)
  12064. (if (and org-trust-scanner-tags
  12065. (or (not pos) (equal pos (point)))
  12066. (not local))
  12067. org-scanner-tags
  12068. (let (tags ltags lastpos parent)
  12069. (save-excursion
  12070. (save-restriction
  12071. (widen)
  12072. (goto-char (or pos (point)))
  12073. (save-match-data
  12074. (catch 'done
  12075. (condition-case nil
  12076. (progn
  12077. (org-back-to-heading t)
  12078. (while (not (equal lastpos (point)))
  12079. (setq lastpos (point))
  12080. (when (looking-at
  12081. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12082. (setq ltags (org-split-string
  12083. (org-match-string-no-properties 1) ":"))
  12084. (when parent
  12085. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12086. (setq tags (append
  12087. (if parent
  12088. (org-remove-uninherited-tags ltags)
  12089. ltags)
  12090. tags)))
  12091. (or org-use-tag-inheritance (throw 'done t))
  12092. (if local (throw 'done t))
  12093. (or (org-up-heading-safe) (error nil))
  12094. (setq parent t)))
  12095. (error nil)))))
  12096. (if local
  12097. tags
  12098. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12099. (defun org-add-prop-inherited (s)
  12100. (add-text-properties 0 (length s) '(inherited t) s)
  12101. s)
  12102. (defun org-toggle-tag (tag &optional onoff)
  12103. "Toggle the tag TAG for the current line.
  12104. If ONOFF is `on' or `off', don't toggle but set to this state."
  12105. (let (res current)
  12106. (save-excursion
  12107. (org-back-to-heading t)
  12108. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12109. (point-at-eol) t)
  12110. (progn
  12111. (setq current (match-string 1))
  12112. (replace-match ""))
  12113. (setq current ""))
  12114. (setq current (nreverse (org-split-string current ":")))
  12115. (cond
  12116. ((eq onoff 'on)
  12117. (setq res t)
  12118. (or (member tag current) (push tag current)))
  12119. ((eq onoff 'off)
  12120. (or (not (member tag current)) (setq current (delete tag current))))
  12121. (t (if (member tag current)
  12122. (setq current (delete tag current))
  12123. (setq res t)
  12124. (push tag current))))
  12125. (end-of-line 1)
  12126. (if current
  12127. (progn
  12128. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12129. (org-set-tags nil t))
  12130. (delete-horizontal-space))
  12131. (run-hooks 'org-after-tags-change-hook))
  12132. res))
  12133. (defun org-align-tags-here (to-col)
  12134. ;; Assumes that this is a headline
  12135. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12136. (beginning-of-line 1)
  12137. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12138. (< pos (match-beginning 2)))
  12139. (progn
  12140. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12141. (goto-char (match-beginning 1))
  12142. (insert " ")
  12143. (delete-region (point) (1+ (match-beginning 2)))
  12144. (setq ncol (max (current-column)
  12145. (1+ col)
  12146. (if (> to-col 0)
  12147. to-col
  12148. (- (abs to-col) tags-l))))
  12149. (setq p (point))
  12150. (insert (make-string (- ncol (current-column)) ?\ ))
  12151. (setq ncol (current-column))
  12152. (when indent-tabs-mode (tabify p (point-at-eol)))
  12153. (org-move-to-column (min ncol col) t))
  12154. (goto-char pos))))
  12155. (defun org-set-tags-command (&optional arg just-align)
  12156. "Call the set-tags command for the current entry."
  12157. (interactive "P")
  12158. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12159. (org-set-tags arg just-align)
  12160. (save-excursion
  12161. (org-back-to-heading t)
  12162. (org-set-tags arg just-align))))
  12163. (defun org-set-tags-to (data)
  12164. "Set the tags of the current entry to DATA, replacing the current tags.
  12165. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12166. If DATA is nil or the empty string, any tags will be removed."
  12167. (interactive "sTags: ")
  12168. (setq data
  12169. (cond
  12170. ((eq data nil) "")
  12171. ((equal data "") "")
  12172. ((stringp data)
  12173. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12174. ":"))
  12175. ((listp data)
  12176. (concat ":" (mapconcat 'identity data ":") ":"))
  12177. (t nil)))
  12178. (when data
  12179. (save-excursion
  12180. (org-back-to-heading t)
  12181. (when (looking-at org-complex-heading-regexp)
  12182. (if (match-end 5)
  12183. (progn
  12184. (goto-char (match-beginning 5))
  12185. (insert data)
  12186. (delete-region (point) (point-at-eol))
  12187. (org-set-tags nil 'align))
  12188. (goto-char (point-at-eol))
  12189. (insert " " data)
  12190. (org-set-tags nil 'align)))
  12191. (beginning-of-line 1)
  12192. (if (looking-at ".*?\\([ \t]+\\)$")
  12193. (delete-region (match-beginning 1) (match-end 1))))))
  12194. (defun org-align-all-tags ()
  12195. "Align the tags i all headings."
  12196. (interactive)
  12197. (save-excursion
  12198. (or (ignore-errors (org-back-to-heading t))
  12199. (outline-next-heading))
  12200. (if (org-at-heading-p)
  12201. (org-set-tags t)
  12202. (message "No headings"))))
  12203. (defvar org-indent-indentation-per-level)
  12204. (defun org-set-tags (&optional arg just-align)
  12205. "Set the tags for the current headline.
  12206. With prefix ARG, realign all tags in headings in the current buffer."
  12207. (interactive "P")
  12208. (let* ((re org-outline-regexp-bol)
  12209. (current (unless arg (org-get-tags-string)))
  12210. (col (current-column))
  12211. (org-setting-tags t)
  12212. table current-tags inherited-tags ; computed below when needed
  12213. tags p0 c0 c1 rpl di tc level)
  12214. (if arg
  12215. (save-excursion
  12216. (goto-char (point-min))
  12217. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12218. (while (re-search-forward re nil t)
  12219. (org-set-tags nil t)
  12220. (end-of-line 1)))
  12221. (message "All tags realigned to column %d" org-tags-column))
  12222. (if just-align
  12223. (setq tags current)
  12224. ;; Get a new set of tags from the user
  12225. (save-excursion
  12226. (setq table (append org-tag-persistent-alist
  12227. (or org-tag-alist (org-get-buffer-tags))
  12228. (and
  12229. org-complete-tags-always-offer-all-agenda-tags
  12230. (org-global-tags-completion-table
  12231. (org-agenda-files))))
  12232. org-last-tags-completion-table table
  12233. current-tags (org-split-string current ":")
  12234. inherited-tags (nreverse
  12235. (nthcdr (length current-tags)
  12236. (nreverse (org-get-tags-at))))
  12237. tags
  12238. (if (or (eq t org-use-fast-tag-selection)
  12239. (and org-use-fast-tag-selection
  12240. (delq nil (mapcar 'cdr table))))
  12241. (org-fast-tag-selection
  12242. current-tags inherited-tags table
  12243. (if org-fast-tag-selection-include-todo
  12244. org-todo-key-alist))
  12245. (let ((org-add-colon-after-tag-completion t))
  12246. (org-trim
  12247. (org-icompleting-read "Tags: "
  12248. 'org-tags-completion-function
  12249. nil nil current 'org-tags-history))))))
  12250. (while (string-match "[-+&]+" tags)
  12251. ;; No boolean logic, just a list
  12252. (setq tags (replace-match ":" t t tags))))
  12253. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12254. (if org-tags-sort-function
  12255. (setq tags (mapconcat 'identity
  12256. (sort (org-split-string
  12257. tags (org-re "[^[:alnum:]_@#%]+"))
  12258. org-tags-sort-function) ":")))
  12259. (if (string-match "\\`[\t ]*\\'" tags)
  12260. (setq tags "")
  12261. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12262. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12263. ;; Insert new tags at the correct column
  12264. (beginning-of-line 1)
  12265. (setq level (or (and (looking-at org-outline-regexp)
  12266. (- (match-end 0) (point) 1))
  12267. 1))
  12268. (cond
  12269. ((and (equal current "") (equal tags "")))
  12270. ((re-search-forward
  12271. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12272. (point-at-eol) t)
  12273. (if (equal tags "")
  12274. (setq rpl "")
  12275. (goto-char (match-beginning 0))
  12276. (setq c0 (current-column)
  12277. ;; compute offset for the case of org-indent-mode active
  12278. di (if org-indent-mode
  12279. (* (1- org-indent-indentation-per-level) (1- level))
  12280. 0)
  12281. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12282. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12283. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12284. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12285. (replace-match rpl t t)
  12286. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12287. tags)
  12288. (t (error "Tags alignment failed")))
  12289. (org-move-to-column col)
  12290. (unless just-align
  12291. (run-hooks 'org-after-tags-change-hook)))))
  12292. (defun org-change-tag-in-region (beg end tag off)
  12293. "Add or remove TAG for each entry in the region.
  12294. This works in the agenda, and also in an org-mode buffer."
  12295. (interactive
  12296. (list (region-beginning) (region-end)
  12297. (let ((org-last-tags-completion-table
  12298. (if (eq major-mode 'org-mode)
  12299. (org-get-buffer-tags)
  12300. (org-global-tags-completion-table))))
  12301. (org-icompleting-read
  12302. "Tag: " 'org-tags-completion-function nil nil nil
  12303. 'org-tags-history))
  12304. (progn
  12305. (message "[s]et or [r]emove? ")
  12306. (equal (read-char-exclusive) ?r))))
  12307. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12308. (let ((agendap (equal major-mode 'org-agenda-mode))
  12309. l1 l2 m buf pos newhead (cnt 0))
  12310. (goto-char end)
  12311. (setq l2 (1- (org-current-line)))
  12312. (goto-char beg)
  12313. (setq l1 (org-current-line))
  12314. (loop for l from l1 to l2 do
  12315. (org-goto-line l)
  12316. (setq m (get-text-property (point) 'org-hd-marker))
  12317. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12318. (and agendap m))
  12319. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12320. pos (if agendap m (point)))
  12321. (with-current-buffer buf
  12322. (save-excursion
  12323. (save-restriction
  12324. (goto-char pos)
  12325. (setq cnt (1+ cnt))
  12326. (org-toggle-tag tag (if off 'off 'on))
  12327. (setq newhead (org-get-heading)))))
  12328. (and agendap (org-agenda-change-all-lines newhead m))))
  12329. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12330. (defun org-tags-completion-function (string predicate &optional flag)
  12331. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12332. (confirm (lambda (x) (stringp (car x)))))
  12333. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12334. (setq s1 (match-string 1 string)
  12335. s2 (match-string 2 string))
  12336. (setq s1 "" s2 string))
  12337. (cond
  12338. ((eq flag nil)
  12339. ;; try completion
  12340. (setq rtn (try-completion s2 ctable confirm))
  12341. (if (stringp rtn)
  12342. (setq rtn
  12343. (concat s1 s2 (substring rtn (length s2))
  12344. (if (and org-add-colon-after-tag-completion
  12345. (assoc rtn ctable))
  12346. ":" ""))))
  12347. rtn)
  12348. ((eq flag t)
  12349. ;; all-completions
  12350. (all-completions s2 ctable confirm)
  12351. )
  12352. ((eq flag 'lambda)
  12353. ;; exact match?
  12354. (assoc s2 ctable)))
  12355. ))
  12356. (defun org-fast-tag-insert (kwd tags face &optional end)
  12357. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12358. (insert (format "%-12s" (concat kwd ":"))
  12359. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12360. (or end "")))
  12361. (defun org-fast-tag-show-exit (flag)
  12362. (save-excursion
  12363. (org-goto-line 3)
  12364. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12365. (replace-match ""))
  12366. (when flag
  12367. (end-of-line 1)
  12368. (org-move-to-column (- (window-width) 19) t)
  12369. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12370. (defun org-set-current-tags-overlay (current prefix)
  12371. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12372. (if (featurep 'xemacs)
  12373. (org-overlay-display org-tags-overlay (concat prefix s)
  12374. 'secondary-selection)
  12375. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12376. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12377. (defvar org-last-tag-selection-key nil)
  12378. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12379. "Fast tag selection with single keys.
  12380. CURRENT is the current list of tags in the headline, INHERITED is the
  12381. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12382. possibly with grouping information. TODO-TABLE is a similar table with
  12383. TODO keywords, should these have keys assigned to them.
  12384. If the keys are nil, a-z are automatically assigned.
  12385. Returns the new tags string, or nil to not change the current settings."
  12386. (let* ((fulltable (append table todo-table))
  12387. (maxlen (apply 'max (mapcar
  12388. (lambda (x)
  12389. (if (stringp (car x)) (string-width (car x)) 0))
  12390. fulltable)))
  12391. (buf (current-buffer))
  12392. (expert (eq org-fast-tag-selection-single-key 'expert))
  12393. (buffer-tags nil)
  12394. (fwidth (+ maxlen 3 1 3))
  12395. (ncol (/ (- (window-width) 4) fwidth))
  12396. (i-face 'org-done)
  12397. (c-face 'org-todo)
  12398. tg cnt e c char c1 c2 ntable tbl rtn
  12399. ov-start ov-end ov-prefix
  12400. (exit-after-next org-fast-tag-selection-single-key)
  12401. (done-keywords org-done-keywords)
  12402. groups ingroup)
  12403. (save-excursion
  12404. (beginning-of-line 1)
  12405. (if (looking-at
  12406. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12407. (setq ov-start (match-beginning 1)
  12408. ov-end (match-end 1)
  12409. ov-prefix "")
  12410. (setq ov-start (1- (point-at-eol))
  12411. ov-end (1+ ov-start))
  12412. (skip-chars-forward "^\n\r")
  12413. (setq ov-prefix
  12414. (concat
  12415. (buffer-substring (1- (point)) (point))
  12416. (if (> (current-column) org-tags-column)
  12417. " "
  12418. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12419. (move-overlay org-tags-overlay ov-start ov-end)
  12420. (save-window-excursion
  12421. (if expert
  12422. (set-buffer (get-buffer-create " *Org tags*"))
  12423. (delete-other-windows)
  12424. (split-window-vertically)
  12425. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12426. (erase-buffer)
  12427. (org-set-local 'org-done-keywords done-keywords)
  12428. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12429. (org-fast-tag-insert "Current" current c-face "\n\n")
  12430. (org-fast-tag-show-exit exit-after-next)
  12431. (org-set-current-tags-overlay current ov-prefix)
  12432. (setq tbl fulltable char ?a cnt 0)
  12433. (while (setq e (pop tbl))
  12434. (cond
  12435. ((equal (car e) :startgroup)
  12436. (push '() groups) (setq ingroup t)
  12437. (when (not (= cnt 0))
  12438. (setq cnt 0)
  12439. (insert "\n"))
  12440. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12441. ((equal (car e) :endgroup)
  12442. (setq ingroup nil cnt 0)
  12443. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12444. ((equal e '(:newline))
  12445. (when (not (= cnt 0))
  12446. (setq cnt 0)
  12447. (insert "\n")
  12448. (setq e (car tbl))
  12449. (while (equal (car tbl) '(:newline))
  12450. (insert "\n")
  12451. (setq tbl (cdr tbl)))))
  12452. (t
  12453. (setq tg (copy-sequence (car e)) c2 nil)
  12454. (if (cdr e)
  12455. (setq c (cdr e))
  12456. ;; automatically assign a character.
  12457. (setq c1 (string-to-char
  12458. (downcase (substring
  12459. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12460. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12461. (while (or (rassoc char ntable) (rassoc char table))
  12462. (setq char (1+ char)))
  12463. (setq c2 c1))
  12464. (setq c (or c2 char)))
  12465. (if ingroup (push tg (car groups)))
  12466. (setq tg (org-add-props tg nil 'face
  12467. (cond
  12468. ((not (assoc tg table))
  12469. (org-get-todo-face tg))
  12470. ((member tg current) c-face)
  12471. ((member tg inherited) i-face)
  12472. (t nil))))
  12473. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12474. (insert "[" c "] " tg (make-string
  12475. (- fwidth 4 (length tg)) ?\ ))
  12476. (push (cons tg c) ntable)
  12477. (when (= (setq cnt (1+ cnt)) ncol)
  12478. (insert "\n")
  12479. (if ingroup (insert " "))
  12480. (setq cnt 0)))))
  12481. (setq ntable (nreverse ntable))
  12482. (insert "\n")
  12483. (goto-char (point-min))
  12484. (if (not expert) (org-fit-window-to-buffer))
  12485. (setq rtn
  12486. (catch 'exit
  12487. (while t
  12488. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12489. (if (not groups) "no " "")
  12490. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12491. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12492. (setq org-last-tag-selection-key c)
  12493. (cond
  12494. ((= c ?\r) (throw 'exit t))
  12495. ((= c ?!)
  12496. (setq groups (not groups))
  12497. (goto-char (point-min))
  12498. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12499. ((= c ?\C-c)
  12500. (if (not expert)
  12501. (org-fast-tag-show-exit
  12502. (setq exit-after-next (not exit-after-next)))
  12503. (setq expert nil)
  12504. (delete-other-windows)
  12505. (set-window-buffer (split-window-vertically) " *Org tags*")
  12506. (org-switch-to-buffer-other-window " *Org tags*")
  12507. (org-fit-window-to-buffer)))
  12508. ((or (= c ?\C-g)
  12509. (and (= c ?q) (not (rassoc c ntable))))
  12510. (org-detach-overlay org-tags-overlay)
  12511. (setq quit-flag t))
  12512. ((= c ?\ )
  12513. (setq current nil)
  12514. (if exit-after-next (setq exit-after-next 'now)))
  12515. ((= c ?\t)
  12516. (condition-case nil
  12517. (setq tg (org-icompleting-read
  12518. "Tag: "
  12519. (or buffer-tags
  12520. (with-current-buffer buf
  12521. (org-get-buffer-tags)))))
  12522. (quit (setq tg "")))
  12523. (when (string-match "\\S-" tg)
  12524. (add-to-list 'buffer-tags (list tg))
  12525. (if (member tg current)
  12526. (setq current (delete tg current))
  12527. (push tg current)))
  12528. (if exit-after-next (setq exit-after-next 'now)))
  12529. ((setq e (rassoc c todo-table) tg (car e))
  12530. (with-current-buffer buf
  12531. (save-excursion (org-todo tg)))
  12532. (if exit-after-next (setq exit-after-next 'now)))
  12533. ((setq e (rassoc c ntable) tg (car e))
  12534. (if (member tg current)
  12535. (setq current (delete tg current))
  12536. (loop for g in groups do
  12537. (if (member tg g)
  12538. (mapc (lambda (x)
  12539. (setq current (delete x current)))
  12540. g)))
  12541. (push tg current))
  12542. (if exit-after-next (setq exit-after-next 'now))))
  12543. ;; Create a sorted list
  12544. (setq current
  12545. (sort current
  12546. (lambda (a b)
  12547. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12548. (if (eq exit-after-next 'now) (throw 'exit t))
  12549. (goto-char (point-min))
  12550. (beginning-of-line 2)
  12551. (delete-region (point) (point-at-eol))
  12552. (org-fast-tag-insert "Current" current c-face)
  12553. (org-set-current-tags-overlay current ov-prefix)
  12554. (while (re-search-forward
  12555. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12556. (setq tg (match-string 1))
  12557. (add-text-properties
  12558. (match-beginning 1) (match-end 1)
  12559. (list 'face
  12560. (cond
  12561. ((member tg current) c-face)
  12562. ((member tg inherited) i-face)
  12563. (t (get-text-property (match-beginning 1) 'face))))))
  12564. (goto-char (point-min)))))
  12565. (org-detach-overlay org-tags-overlay)
  12566. (if rtn
  12567. (mapconcat 'identity current ":")
  12568. nil))))
  12569. (defun org-get-tags-string ()
  12570. "Get the TAGS string in the current headline."
  12571. (unless (org-at-heading-p t)
  12572. (error "Not on a heading"))
  12573. (save-excursion
  12574. (beginning-of-line 1)
  12575. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12576. (org-match-string-no-properties 1)
  12577. "")))
  12578. (defun org-get-tags ()
  12579. "Get the list of tags specified in the current headline."
  12580. (org-split-string (org-get-tags-string) ":"))
  12581. (defun org-get-buffer-tags ()
  12582. "Get a table of all tags used in the buffer, for completion."
  12583. (let (tags)
  12584. (save-excursion
  12585. (goto-char (point-min))
  12586. (while (re-search-forward
  12587. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12588. (when (equal (char-after (point-at-bol 0)) ?*)
  12589. (mapc (lambda (x) (add-to-list 'tags x))
  12590. (org-split-string (org-match-string-no-properties 1) ":")))))
  12591. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12592. (mapcar 'list tags)))
  12593. ;;;; The mapping API
  12594. ;;;###autoload
  12595. (defun org-map-entries (func &optional match scope &rest skip)
  12596. "Call FUNC at each headline selected by MATCH in SCOPE.
  12597. FUNC is a function or a lisp form. The function will be called without
  12598. arguments, with the cursor positioned at the beginning of the headline.
  12599. The return values of all calls to the function will be collected and
  12600. returned as a list.
  12601. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12602. does not need to preserve point. After evaluation, the cursor will be
  12603. moved to the end of the line (presumably of the headline of the
  12604. processed entry) and search continues from there. Under some
  12605. circumstances, this may not produce the wanted results. For example,
  12606. if you have removed (e.g. archived) the current (sub)tree it could
  12607. mean that the next entry will be skipped entirely. In such cases, you
  12608. can specify the position from where search should continue by making
  12609. FUNC set the variable `org-map-continue-from' to the desired buffer
  12610. position.
  12611. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12612. Only headlines that are matched by this query will be considered during
  12613. the iteration. When MATCH is nil or t, all headlines will be
  12614. visited by the iteration.
  12615. SCOPE determines the scope of this command. It can be any of:
  12616. nil The current buffer, respecting the restriction if any
  12617. tree The subtree started with the entry at point
  12618. region The entries within the active region, if any
  12619. region-start-level
  12620. The entries within the active region, but only those at
  12621. the same level than the first one.
  12622. file The current buffer, without restriction
  12623. file-with-archives
  12624. The current buffer, and any archives associated with it
  12625. agenda All agenda files
  12626. agenda-with-archives
  12627. All agenda files with any archive files associated with them
  12628. \(file1 file2 ...)
  12629. If this is a list, all files in the list will be scanned
  12630. The remaining args are treated as settings for the skipping facilities of
  12631. the scanner. The following items can be given here:
  12632. archive skip trees with the archive tag.
  12633. comment skip trees with the COMMENT keyword
  12634. function or Emacs Lisp form:
  12635. will be used as value for `org-agenda-skip-function', so whenever
  12636. the function returns t, FUNC will not be called for that
  12637. entry and search will continue from the point where the
  12638. function leaves it.
  12639. If your function needs to retrieve the tags including inherited tags
  12640. at the *current* entry, you can use the value of the variable
  12641. `org-scanner-tags' which will be much faster than getting the value
  12642. with `org-get-tags-at'. If your function gets properties with
  12643. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12644. to t around the call to `org-entry-properties' to get the same speedup.
  12645. Note that if your function moves around to retrieve tags and properties at
  12646. a *different* entry, you cannot use these techniques."
  12647. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12648. (not (org-region-active-p)))
  12649. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12650. (org-agenda-skip-archived-trees (memq 'archive skip))
  12651. (org-agenda-skip-comment-trees (memq 'comment skip))
  12652. (org-agenda-skip-function
  12653. (car (org-delete-all '(comment archive) skip)))
  12654. (org-tags-match-list-sublevels t)
  12655. (start-level (eq scope 'region-start-level))
  12656. matcher file res
  12657. org-todo-keywords-for-agenda
  12658. org-done-keywords-for-agenda
  12659. org-todo-keyword-alist-for-agenda
  12660. org-drawers-for-agenda
  12661. org-tag-alist-for-agenda)
  12662. (cond
  12663. ((eq match t) (setq matcher t))
  12664. ((eq match nil) (setq matcher t))
  12665. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12666. (save-excursion
  12667. (save-restriction
  12668. (cond ((eq scope 'tree)
  12669. (org-back-to-heading t)
  12670. (org-narrow-to-subtree)
  12671. (setq scope nil))
  12672. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12673. (org-region-active-p))
  12674. ;; If needed, set start-level to a string like "2"
  12675. (when start-level
  12676. (save-excursion
  12677. (goto-char (region-beginning))
  12678. (unless (org-at-heading-p) (outline-next-heading))
  12679. (setq start-level (org-current-level))))
  12680. (narrow-to-region (region-beginning)
  12681. (save-excursion
  12682. (goto-char (region-end))
  12683. (unless (and (bolp) (org-at-heading-p))
  12684. (outline-next-heading))
  12685. (point)))
  12686. (setq scope nil)))
  12687. (if (not scope)
  12688. (progn
  12689. (org-prepare-agenda-buffers
  12690. (list (buffer-file-name (current-buffer))))
  12691. (setq res (org-scan-tags func matcher nil start-level)))
  12692. ;; Get the right scope
  12693. (cond
  12694. ((and scope (listp scope) (symbolp (car scope)))
  12695. (setq scope (eval scope)))
  12696. ((eq scope 'agenda)
  12697. (setq scope (org-agenda-files t)))
  12698. ((eq scope 'agenda-with-archives)
  12699. (setq scope (org-agenda-files t))
  12700. (setq scope (org-add-archive-files scope)))
  12701. ((eq scope 'file)
  12702. (setq scope (list (buffer-file-name))))
  12703. ((eq scope 'file-with-archives)
  12704. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12705. (org-prepare-agenda-buffers scope)
  12706. (while (setq file (pop scope))
  12707. (with-current-buffer (org-find-base-buffer-visiting file)
  12708. (save-excursion
  12709. (save-restriction
  12710. (widen)
  12711. (goto-char (point-min))
  12712. (setq res (append res (org-scan-tags func matcher))))))))))
  12713. res)))
  12714. ;;;; Properties
  12715. ;;; Setting and retrieving properties
  12716. (defconst org-special-properties
  12717. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12718. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12719. "The special properties valid in Org-mode.
  12720. These are properties that are not defined in the property drawer,
  12721. but in some other way.")
  12722. (defconst org-default-properties
  12723. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12724. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12725. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12726. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12727. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12728. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12729. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12730. "Some properties that are used by Org-mode for various purposes.
  12731. Being in this list makes sure that they are offered for completion.")
  12732. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12733. "Regular expression matching the first line of a property drawer.")
  12734. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12735. "Regular expression matching the last line of a property drawer.")
  12736. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12737. "Regular expression matching the first line of a property drawer.")
  12738. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12739. "Regular expression matching the first line of a property drawer.")
  12740. (defconst org-property-drawer-re
  12741. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12742. org-property-end-re "\\)\n?")
  12743. "Matches an entire property drawer.")
  12744. (defconst org-clock-drawer-re
  12745. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12746. org-property-end-re "\\)\n?")
  12747. "Matches an entire clock drawer.")
  12748. (defsubst org-re-property (property)
  12749. "Return a regexp matching PROPERTY.
  12750. Match group 1 will be set to the value "
  12751. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12752. (defun org-property-action ()
  12753. "Do an action on properties."
  12754. (interactive)
  12755. (let (c)
  12756. (org-at-property-p)
  12757. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12758. (setq c (read-char-exclusive))
  12759. (cond
  12760. ((equal c ?s)
  12761. (call-interactively 'org-set-property))
  12762. ((equal c ?d)
  12763. (call-interactively 'org-delete-property))
  12764. ((equal c ?D)
  12765. (call-interactively 'org-delete-property-globally))
  12766. ((equal c ?c)
  12767. (call-interactively 'org-compute-property-at-point))
  12768. (t (error "No such property action %c" c)))))
  12769. (defun org-set-effort (&optional value)
  12770. "Set the effort property of the current entry.
  12771. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12772. allowed value."
  12773. (interactive "P")
  12774. (if (equal value 0) (setq value 10))
  12775. (let* ((completion-ignore-case t)
  12776. (prop org-effort-property)
  12777. (cur (org-entry-get nil prop))
  12778. (allowed (org-property-get-allowed-values nil prop 'table))
  12779. (existing (mapcar 'list (org-property-values prop)))
  12780. rpl
  12781. (val (cond
  12782. ((stringp value) value)
  12783. ((and allowed (integerp value))
  12784. (or (car (nth (1- value) allowed))
  12785. (car (org-last allowed))))
  12786. (allowed
  12787. (message "Select 1-9,0, [RET%s]: %s"
  12788. (if cur (concat "=" cur) "")
  12789. (mapconcat 'car allowed " "))
  12790. (setq rpl (read-char-exclusive))
  12791. (if (equal rpl ?\r)
  12792. cur
  12793. (setq rpl (- rpl ?0))
  12794. (if (equal rpl 0) (setq rpl 10))
  12795. (if (and (> rpl 0) (<= rpl (length allowed)))
  12796. (car (nth (1- rpl) allowed))
  12797. (org-completing-read "Effort: " allowed nil))))
  12798. (t
  12799. (let (org-completion-use-ido org-completion-use-iswitchb)
  12800. (org-completing-read
  12801. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12802. (concat "[" cur "]") "")
  12803. ": ")
  12804. existing nil nil "" nil cur))))))
  12805. (unless (equal (org-entry-get nil prop) val)
  12806. (org-entry-put nil prop val))
  12807. (message "%s is now %s" prop val)))
  12808. (defun org-at-property-p ()
  12809. "Is cursor inside a property drawer?"
  12810. (save-excursion
  12811. (beginning-of-line 1)
  12812. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12813. (save-match-data ;; Used by calling procedures
  12814. (let ((p (point))
  12815. (range (unless (org-before-first-heading-p)
  12816. (org-get-property-block))))
  12817. (and range (<= (car range) p) (< p (cdr range))))))))
  12818. (defun org-get-property-block (&optional beg end force)
  12819. "Return the (beg . end) range of the body of the property drawer.
  12820. BEG and END can be beginning and end of subtree, if not given
  12821. they will be found.
  12822. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12823. (catch 'exit
  12824. (save-excursion
  12825. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12826. (end (or end (progn (outline-next-heading) (point)))))
  12827. (goto-char beg)
  12828. (if (re-search-forward org-property-start-re end t)
  12829. (setq beg (1+ (match-end 0)))
  12830. (if force
  12831. (save-excursion
  12832. (org-insert-property-drawer)
  12833. (setq end (progn (outline-next-heading) (point))))
  12834. (throw 'exit nil))
  12835. (goto-char beg)
  12836. (if (re-search-forward org-property-start-re end t)
  12837. (setq beg (1+ (match-end 0)))))
  12838. (if (re-search-forward org-property-end-re end t)
  12839. (setq end (match-beginning 0))
  12840. (or force (throw 'exit nil))
  12841. (goto-char beg)
  12842. (setq end beg)
  12843. (org-indent-line-function)
  12844. (insert ":END:\n"))
  12845. (cons beg end)))))
  12846. (defun org-entry-properties (&optional pom which specific)
  12847. "Get all properties of the entry at point-or-marker POM.
  12848. This includes the TODO keyword, the tags, time strings for deadline,
  12849. scheduled, and clocking, and any additional properties defined in the
  12850. entry. The return value is an alist, keys may occur multiple times
  12851. if the property key was used several times.
  12852. POM may also be nil, in which case the current entry is used.
  12853. If WHICH is nil or `all', get all properties. If WHICH is
  12854. `special' or `standard', only get that subclass. If WHICH
  12855. is a string only get exactly this property. SPECIFIC can be a string, the
  12856. specific property we are interested in. Specifying it can speed
  12857. things up because then unnecessary parsing is avoided."
  12858. (setq which (or which 'all))
  12859. (org-with-point-at pom
  12860. (let ((clockstr (substring org-clock-string 0 -1))
  12861. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12862. (case-fold-search nil)
  12863. beg end range props sum-props key key1 value string clocksum)
  12864. (save-excursion
  12865. (when (condition-case nil
  12866. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12867. (error nil))
  12868. (setq beg (point))
  12869. (setq sum-props (get-text-property (point) 'org-summaries))
  12870. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12871. (outline-next-heading)
  12872. (setq end (point))
  12873. (when (memq which '(all special))
  12874. ;; Get the special properties, like TODO and tags
  12875. (goto-char beg)
  12876. (when (and (or (not specific) (string= specific "TODO"))
  12877. (looking-at org-todo-line-regexp) (match-end 2))
  12878. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12879. (when (and (or (not specific) (string= specific "PRIORITY"))
  12880. (looking-at org-priority-regexp))
  12881. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12882. (when (or (not specific) (string= specific "FILE"))
  12883. (push (cons "FILE" buffer-file-name) props))
  12884. (when (and (or (not specific) (string= specific "TAGS"))
  12885. (setq value (org-get-tags-string))
  12886. (string-match "\\S-" value))
  12887. (push (cons "TAGS" value) props))
  12888. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12889. (setq value (org-get-tags-at)))
  12890. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12891. ":"))
  12892. props))
  12893. (when (or (not specific) (string= specific "BLOCKED"))
  12894. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12895. (when (or (not specific)
  12896. (member specific
  12897. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12898. "TIMESTAMP" "TIMESTAMP_IA")))
  12899. (catch 'match
  12900. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12901. (setq key (if (match-end 1)
  12902. (substring (org-match-string-no-properties 1)
  12903. 0 -1))
  12904. string (if (equal key clockstr)
  12905. (org-no-properties
  12906. (org-trim
  12907. (buffer-substring
  12908. (match-beginning 3) (goto-char
  12909. (point-at-eol)))))
  12910. (substring (org-match-string-no-properties 3)
  12911. 1 -1)))
  12912. ;; Get the correct property name from the key. This is
  12913. ;; necessary if the user has configured time keywords.
  12914. (setq key1 (concat key ":"))
  12915. (cond
  12916. ((not key)
  12917. (setq key
  12918. (if (= (char-after (match-beginning 3)) ?\[)
  12919. "TIMESTAMP_IA" "TIMESTAMP")))
  12920. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12921. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12922. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12923. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12924. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12925. (progn
  12926. (push (cons key string) props)
  12927. ;; no need to search further if match is found
  12928. (throw 'match t))
  12929. (when (or (equal key "CLOCK") (not (assoc key props)))
  12930. (push (cons key string) props))))))
  12931. )
  12932. (when (memq which '(all standard))
  12933. ;; Get the standard properties, like :PROP: ...
  12934. (setq range (org-get-property-block beg end))
  12935. (when range
  12936. (goto-char (car range))
  12937. (while (re-search-forward
  12938. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12939. (cdr range) t)
  12940. (setq key (org-match-string-no-properties 1)
  12941. value (org-trim (or (org-match-string-no-properties 2) "")))
  12942. (unless (member key excluded)
  12943. (push (cons key (or value "")) props)))))
  12944. (if clocksum
  12945. (push (cons "CLOCKSUM"
  12946. (org-columns-number-to-string (/ (float clocksum) 60.)
  12947. 'add_times))
  12948. props))
  12949. (unless (assoc "CATEGORY" props)
  12950. (push (cons "CATEGORY" (org-get-category)) props))
  12951. (append sum-props (nreverse props)))))))
  12952. (defun org-entry-get (pom property &optional inherit literal-nil)
  12953. "Get value of PROPERTY for entry at point-or-marker POM.
  12954. If INHERIT is non-nil and the entry does not have the property,
  12955. then also check higher levels of the hierarchy.
  12956. If INHERIT is the symbol `selective', use inheritance only if the setting
  12957. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12958. If the property is present but empty, the return value is the empty string.
  12959. If the property is not present at all, nil is returned.
  12960. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12961. do not interpret it as the list atom nil. This is used for inheritance
  12962. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12963. (org-with-point-at pom
  12964. (if (and inherit (if (eq inherit 'selective)
  12965. (org-property-inherit-p property)
  12966. t))
  12967. (org-entry-get-with-inheritance property literal-nil)
  12968. (if (member property org-special-properties)
  12969. ;; We need a special property. Use `org-entry-properties' to
  12970. ;; retrieve it, but specify the wanted property
  12971. (cdr (assoc property (org-entry-properties nil 'special property)))
  12972. (let ((range (unless (org-before-first-heading-p)
  12973. (org-get-property-block)))
  12974. (props (list (or (assoc property org-file-properties)
  12975. (assoc property org-global-properties)
  12976. (assoc property org-global-properties-fixed))))
  12977. val)
  12978. (flet ((ap (key)
  12979. (when (re-search-forward
  12980. (org-re-property key) (cdr range) t)
  12981. (setq props
  12982. (org-update-property-plist
  12983. key
  12984. (if (match-end 1)
  12985. (org-match-string-no-properties 1) "")
  12986. props)))))
  12987. (when (and range (goto-char (car range)))
  12988. (ap property)
  12989. (goto-char (car range))
  12990. (while (ap (concat property "+")))
  12991. (setq val (cdr (assoc property props)))
  12992. (when val (if literal-nil val (org-not-nil val))))))))))
  12993. (defun org-property-or-variable-value (var &optional inherit)
  12994. "Check if there is a property fixing the value of VAR.
  12995. If yes, return this value. If not, return the current value of the variable."
  12996. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12997. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12998. (read prop)
  12999. (symbol-value var))))
  13000. (defun org-entry-delete (pom property)
  13001. "Delete the property PROPERTY from entry at point-or-marker POM."
  13002. (org-with-point-at pom
  13003. (if (member property org-special-properties)
  13004. nil ; cannot delete these properties.
  13005. (let ((range (org-get-property-block)))
  13006. (if (and range
  13007. (goto-char (car range))
  13008. (re-search-forward
  13009. (org-re-property property)
  13010. (cdr range) t))
  13011. (progn
  13012. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13013. t)
  13014. nil)))))
  13015. ;; Multi-values properties are properties that contain multiple values
  13016. ;; These values are assumed to be single words, separated by whitespace.
  13017. (defun org-entry-add-to-multivalued-property (pom property value)
  13018. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13019. (let* ((old (org-entry-get pom property))
  13020. (values (and old (org-split-string old "[ \t]"))))
  13021. (setq value (org-entry-protect-space value))
  13022. (unless (member value values)
  13023. (setq values (cons value values))
  13024. (org-entry-put pom property
  13025. (mapconcat 'identity values " ")))))
  13026. (defun org-entry-remove-from-multivalued-property (pom property value)
  13027. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13028. (let* ((old (org-entry-get pom property))
  13029. (values (and old (org-split-string old "[ \t]"))))
  13030. (setq value (org-entry-protect-space value))
  13031. (when (member value values)
  13032. (setq values (delete value values))
  13033. (org-entry-put pom property
  13034. (mapconcat 'identity values " ")))))
  13035. (defun org-entry-member-in-multivalued-property (pom property value)
  13036. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13037. (let* ((old (org-entry-get pom property))
  13038. (values (and old (org-split-string old "[ \t]"))))
  13039. (setq value (org-entry-protect-space value))
  13040. (member value values)))
  13041. (defun org-entry-get-multivalued-property (pom property)
  13042. "Return a list of values in a multivalued property."
  13043. (let* ((value (org-entry-get pom property))
  13044. (values (and value (org-split-string value "[ \t]"))))
  13045. (mapcar 'org-entry-restore-space values)))
  13046. (defun org-entry-put-multivalued-property (pom property &rest values)
  13047. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13048. VALUES should be a list of strings. Spaces will be protected."
  13049. (org-entry-put pom property
  13050. (mapconcat 'org-entry-protect-space values " "))
  13051. (let* ((value (org-entry-get pom property))
  13052. (values (and value (org-split-string value "[ \t]"))))
  13053. (mapcar 'org-entry-restore-space values)))
  13054. (defun org-entry-protect-space (s)
  13055. "Protect spaces and newline in string S."
  13056. (while (string-match " " s)
  13057. (setq s (replace-match "%20" t t s)))
  13058. (while (string-match "\n" s)
  13059. (setq s (replace-match "%0A" t t s)))
  13060. s)
  13061. (defun org-entry-restore-space (s)
  13062. "Restore spaces and newline in string S."
  13063. (while (string-match "%20" s)
  13064. (setq s (replace-match " " t t s)))
  13065. (while (string-match "%0A" s)
  13066. (setq s (replace-match "\n" t t s)))
  13067. s)
  13068. (defvar org-entry-property-inherited-from (make-marker)
  13069. "Marker pointing to the entry from where a property was inherited.
  13070. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13071. location of the entry where the inheritance search matched. If there was
  13072. no match, the marker will point nowhere.
  13073. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13074. is set.")
  13075. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13076. "Get entry property, and search higher levels if not present.
  13077. The search will stop at the first ancestor which has the property defined.
  13078. If the value found is \"nil\", return nil to show that the property
  13079. should be considered as undefined (this is the meaning of nil here).
  13080. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13081. (move-marker org-entry-property-inherited-from nil)
  13082. (let (tmp)
  13083. (unless (org-before-first-heading-p)
  13084. (save-excursion
  13085. (save-restriction
  13086. (widen)
  13087. (catch 'ex
  13088. (while t
  13089. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13090. (org-back-to-heading t)
  13091. (move-marker org-entry-property-inherited-from (point))
  13092. (throw 'ex tmp))
  13093. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13094. (setq tmp (or tmp
  13095. (cdr (assoc property org-file-properties))
  13096. (cdr (assoc property org-global-properties))
  13097. (cdr (assoc property org-global-properties-fixed))))
  13098. (if literal-nil tmp (org-not-nil tmp))))
  13099. (defvar org-property-changed-functions nil
  13100. "Hook called when the value of a property has changed.
  13101. Each hook function should accept two arguments, the name of the property
  13102. and the new value.")
  13103. (defun org-entry-put (pom property value)
  13104. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13105. (org-with-point-at pom
  13106. (org-back-to-heading t)
  13107. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13108. range)
  13109. (cond
  13110. ((equal property "TODO")
  13111. (when (and (stringp value) (string-match "\\S-" value)
  13112. (not (member value org-todo-keywords-1)))
  13113. (error "\"%s\" is not a valid TODO state" value))
  13114. (if (or (not value)
  13115. (not (string-match "\\S-" value)))
  13116. (setq value 'none))
  13117. (org-todo value)
  13118. (org-set-tags nil 'align))
  13119. ((equal property "PRIORITY")
  13120. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13121. (string-to-char value) ?\ ))
  13122. (org-set-tags nil 'align))
  13123. ((equal property "SCHEDULED")
  13124. (if (re-search-forward org-scheduled-time-regexp end t)
  13125. (cond
  13126. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13127. ((eq value 'later) (org-timestamp-change 1 'day))
  13128. (t (call-interactively 'org-schedule)))
  13129. (call-interactively 'org-schedule)))
  13130. ((equal property "DEADLINE")
  13131. (if (re-search-forward org-deadline-time-regexp end t)
  13132. (cond
  13133. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13134. ((eq value 'later) (org-timestamp-change 1 'day))
  13135. (t (call-interactively 'org-deadline)))
  13136. (call-interactively 'org-deadline)))
  13137. ((member property org-special-properties)
  13138. (error "The %s property can not yet be set with `org-entry-put'"
  13139. property))
  13140. (t ; a non-special property
  13141. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13142. (setq range (org-get-property-block beg end 'force))
  13143. (goto-char (car range))
  13144. (if (re-search-forward
  13145. (org-re-property property) (cdr range) t)
  13146. (progn
  13147. (delete-region (match-beginning 0) (match-end 0))
  13148. (goto-char (match-beginning 0)))
  13149. (goto-char (cdr range))
  13150. (insert "\n")
  13151. (backward-char 1)
  13152. (org-indent-line-function))
  13153. (insert ":" property ":")
  13154. (and value (insert " " value))
  13155. (org-indent-line-function)))))
  13156. (run-hook-with-args 'org-property-changed-functions property value)))
  13157. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13158. "Get all property keys in the current buffer.
  13159. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13160. like tags and TODO state.
  13161. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13162. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13163. and others.
  13164. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13165. formats in the current buffer."
  13166. (let (rtn range cfmt s p)
  13167. (save-excursion
  13168. (save-restriction
  13169. (widen)
  13170. (goto-char (point-min))
  13171. (while (re-search-forward org-property-start-re nil t)
  13172. (setq range (org-get-property-block))
  13173. (goto-char (car range))
  13174. (while (re-search-forward
  13175. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13176. (cdr range) t)
  13177. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13178. (outline-next-heading))))
  13179. (when include-specials
  13180. (setq rtn (append org-special-properties rtn)))
  13181. (when include-defaults
  13182. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13183. (add-to-list 'rtn org-effort-property))
  13184. (when include-columns
  13185. (save-excursion
  13186. (save-restriction
  13187. (widen)
  13188. (goto-char (point-min))
  13189. (while (re-search-forward
  13190. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13191. nil t)
  13192. (setq cfmt (match-string 2) s 0)
  13193. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13194. cfmt s)
  13195. (setq s (match-end 0)
  13196. p (match-string 1 cfmt))
  13197. (unless (or (equal p "ITEM")
  13198. (member p org-special-properties))
  13199. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13200. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13201. (defun org-property-values (key)
  13202. "Return a list of all values of property KEY in the current buffer."
  13203. (save-excursion
  13204. (save-restriction
  13205. (widen)
  13206. (goto-char (point-min))
  13207. (let ((re (org-re-property key))
  13208. values)
  13209. (while (re-search-forward re nil t)
  13210. (add-to-list 'values (org-trim (match-string 1))))
  13211. (delete "" values)))))
  13212. (defun org-insert-property-drawer ()
  13213. "Insert a property drawer into the current entry."
  13214. (org-back-to-heading t)
  13215. (looking-at org-outline-regexp)
  13216. (let ((indent (if org-adapt-indentation
  13217. (- (match-end 0) (match-beginning 0))
  13218. 0))
  13219. (beg (point))
  13220. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13221. end hiddenp)
  13222. (outline-next-heading)
  13223. (setq end (point))
  13224. (goto-char beg)
  13225. (while (re-search-forward re end t))
  13226. (setq hiddenp (outline-invisible-p))
  13227. (end-of-line 1)
  13228. (and (equal (char-after) ?\n) (forward-char 1))
  13229. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13230. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13231. ;; just skip this line
  13232. (beginning-of-line 2)
  13233. ;; Drawer start, find the end
  13234. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13235. (beginning-of-line 1)))
  13236. (org-skip-over-state-notes)
  13237. (skip-chars-backward " \t\n\r")
  13238. (if (eq (char-before) ?*) (forward-char 1))
  13239. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13240. (beginning-of-line 0)
  13241. (org-indent-to-column indent)
  13242. (beginning-of-line 2)
  13243. (org-indent-to-column indent)
  13244. (beginning-of-line 0)
  13245. (if hiddenp
  13246. (save-excursion
  13247. (org-back-to-heading t)
  13248. (hide-entry))
  13249. (org-flag-drawer t))))
  13250. (defun org-insert-drawer (&optional arg drawer)
  13251. "Insert a drawer at point.
  13252. Optional argument DRAWER, when non-nil, is a string representing
  13253. drawer's name. Otherwise, the user is prompted for a name.
  13254. If a region is active, insert the drawer around that region
  13255. instead.
  13256. Point is left between drawer's boundaries."
  13257. (interactive "P")
  13258. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13259. "LOGBOOK"))
  13260. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13261. ;; internally by Org. They are meant to be inserted
  13262. ;; automatically.
  13263. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13264. ;; Remove system drawers from list. Note: For some reason,
  13265. ;; `org-completing-read' ignores the predicate while
  13266. ;; `completing-read' handles it fine.
  13267. (drawer (if arg "PROPERTIES"
  13268. (or drawer
  13269. (completing-read
  13270. "Drawer: " org-drawers
  13271. (lambda (d) (not (member d system-drawers))))))))
  13272. (cond
  13273. ;; With C-u, fall back on `org-insert-property-drawer'
  13274. (arg (org-insert-property-drawer))
  13275. ;; With an active region, insert a drawer at point.
  13276. ((not (org-region-active-p))
  13277. (progn
  13278. (unless (bolp) (insert "\n"))
  13279. (insert (format ":%s:\n\n:END:\n" drawer))
  13280. (forward-line -2)))
  13281. ;; Otherwise, insert the drawer at point
  13282. (t
  13283. (let ((rbeg (region-beginning))
  13284. (rend (copy-marker (region-end))))
  13285. (unwind-protect
  13286. (progn
  13287. (goto-char rbeg)
  13288. (beginning-of-line)
  13289. (when (save-excursion
  13290. (re-search-forward org-outline-regexp-bol rend t))
  13291. (error "Drawers cannot contain headlines"))
  13292. ;; Position point at the beginning of the first
  13293. ;; non-blank line in region. Insert drawer's opening
  13294. ;; there, then indent it.
  13295. (org-skip-whitespace)
  13296. (beginning-of-line)
  13297. (insert ":" drawer ":\n")
  13298. (forward-line -1)
  13299. (indent-for-tab-command)
  13300. ;; Move point to the beginning of the first blank line
  13301. ;; after the last non-blank line in region. Insert
  13302. ;; drawer's closing, then indent it.
  13303. (goto-char rend)
  13304. (skip-chars-backward " \r\t\n")
  13305. (insert "\n:END:")
  13306. (indent-for-tab-command)
  13307. (unless (eolp) (insert "\n")))
  13308. ;; Clear marker, whatever the outcome of insertion is.
  13309. (set-marker rend nil)))))))
  13310. (defvar org-property-set-functions-alist nil
  13311. "Property set function alist.
  13312. Each entry should have the following format:
  13313. (PROPERTY . READ-FUNCTION)
  13314. The read function will be called with the same argument as
  13315. `org-completing-read'.")
  13316. (defun org-set-property-function (property)
  13317. "Get the function that should be used to set PROPERTY.
  13318. This is computed according to `org-property-set-functions-alist'."
  13319. (or (cdr (assoc property org-property-set-functions-alist))
  13320. 'org-completing-read))
  13321. (defun org-read-property-value (property)
  13322. "Read PROPERTY value from user."
  13323. (let* ((completion-ignore-case t)
  13324. (allowed (org-property-get-allowed-values nil property 'table))
  13325. (cur (org-entry-get nil property))
  13326. (prompt (concat property " value"
  13327. (if (and cur (string-match "\\S-" cur))
  13328. (concat " [" cur "]") "") ": "))
  13329. (set-function (org-set-property-function property))
  13330. (val (if allowed
  13331. (funcall set-function prompt allowed nil
  13332. (not (get-text-property 0 'org-unrestricted
  13333. (caar allowed))))
  13334. (let (org-completion-use-ido org-completion-use-iswitchb)
  13335. (funcall set-function prompt
  13336. (mapcar 'list (org-property-values property))
  13337. nil nil "" nil cur)))))
  13338. (if (equal val "")
  13339. cur
  13340. val)))
  13341. (defvar org-last-set-property nil)
  13342. (defun org-read-property-name ()
  13343. "Read a property name."
  13344. (let* ((completion-ignore-case t)
  13345. (keys (org-buffer-property-keys nil t t))
  13346. (default-prop (or (save-excursion
  13347. (save-match-data
  13348. (beginning-of-line)
  13349. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13350. (null (string= (match-string 1) "END"))
  13351. (match-string 1))))
  13352. org-last-set-property))
  13353. (property (org-icompleting-read
  13354. (concat "Property"
  13355. (if default-prop (concat " [" default-prop "]") "")
  13356. ": ")
  13357. (mapcar 'list keys)
  13358. nil nil nil nil
  13359. default-prop
  13360. )))
  13361. (if (member property keys)
  13362. property
  13363. (or (cdr (assoc (downcase property)
  13364. (mapcar (lambda (x) (cons (downcase x) x))
  13365. keys)))
  13366. property))))
  13367. (defun org-set-property (property value)
  13368. "In the current entry, set PROPERTY to VALUE.
  13369. When called interactively, this will prompt for a property name, offering
  13370. completion on existing and default properties. And then it will prompt
  13371. for a value, offering completion either on allowed values (via an inherited
  13372. xxx_ALL property) or on existing values in other instances of this property
  13373. in the current file."
  13374. (interactive (list nil nil))
  13375. (let* ((property (or property (org-read-property-name)))
  13376. (value (or value (org-read-property-value property)))
  13377. (fn (assoc property org-properties-postprocess-alist)))
  13378. (setq org-last-set-property property)
  13379. ;; Possibly postprocess the inserted value:
  13380. (when fn (setq value (funcall (cadr fn) value)))
  13381. (unless (equal (org-entry-get nil property) value)
  13382. (org-entry-put nil property value))))
  13383. (defun org-delete-property (property)
  13384. "In the current entry, delete PROPERTY."
  13385. (interactive
  13386. (let* ((completion-ignore-case t)
  13387. (prop (org-icompleting-read "Property: "
  13388. (org-entry-properties nil 'standard))))
  13389. (list prop)))
  13390. (message "Property %s %s" property
  13391. (if (org-entry-delete nil property)
  13392. "deleted"
  13393. "was not present in the entry")))
  13394. (defun org-delete-property-globally (property)
  13395. "Remove PROPERTY globally, from all entries."
  13396. (interactive
  13397. (let* ((completion-ignore-case t)
  13398. (prop (org-icompleting-read
  13399. "Globally remove property: "
  13400. (mapcar 'list (org-buffer-property-keys)))))
  13401. (list prop)))
  13402. (save-excursion
  13403. (save-restriction
  13404. (widen)
  13405. (goto-char (point-min))
  13406. (let ((cnt 0))
  13407. (while (re-search-forward
  13408. (org-re-property property)
  13409. nil t)
  13410. (setq cnt (1+ cnt))
  13411. (replace-match ""))
  13412. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13413. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13414. (defun org-compute-property-at-point ()
  13415. "Compute the property at point.
  13416. This looks for an enclosing column format, extracts the operator and
  13417. then applies it to the property in the column format's scope."
  13418. (interactive)
  13419. (unless (org-at-property-p)
  13420. (error "Not at a property"))
  13421. (let ((prop (org-match-string-no-properties 2)))
  13422. (org-columns-get-format-and-top-level)
  13423. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13424. (error "No operator defined for property %s" prop))
  13425. (org-columns-compute prop)))
  13426. (defvar org-property-allowed-value-functions nil
  13427. "Hook for functions supplying allowed values for a specific property.
  13428. The functions must take a single argument, the name of the property, and
  13429. return a flat list of allowed values. If \":ETC\" is one of
  13430. the values, this means that these values are intended as defaults for
  13431. completion, but that other values should be allowed too.
  13432. The functions must return nil if they are not responsible for this
  13433. property.")
  13434. (defun org-property-get-allowed-values (pom property &optional table)
  13435. "Get allowed values for the property PROPERTY.
  13436. When TABLE is non-nil, return an alist that can directly be used for
  13437. completion."
  13438. (let (vals)
  13439. (cond
  13440. ((equal property "TODO")
  13441. (setq vals (org-with-point-at pom
  13442. (append org-todo-keywords-1 '("")))))
  13443. ((equal property "PRIORITY")
  13444. (let ((n org-lowest-priority))
  13445. (while (>= n org-highest-priority)
  13446. (push (char-to-string n) vals)
  13447. (setq n (1- n)))))
  13448. ((member property org-special-properties))
  13449. ((setq vals (run-hook-with-args-until-success
  13450. 'org-property-allowed-value-functions property)))
  13451. (t
  13452. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13453. (when (and vals (string-match "\\S-" vals))
  13454. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13455. (setq vals (mapcar (lambda (x)
  13456. (cond ((stringp x) x)
  13457. ((numberp x) (number-to-string x))
  13458. ((symbolp x) (symbol-name x))
  13459. (t "???")))
  13460. vals)))))
  13461. (when (member ":ETC" vals)
  13462. (setq vals (remove ":ETC" vals))
  13463. (org-add-props (car vals) '(org-unrestricted t)))
  13464. (if table (mapcar 'list vals) vals)))
  13465. (defun org-property-previous-allowed-value (&optional previous)
  13466. "Switch to the next allowed value for this property."
  13467. (interactive)
  13468. (org-property-next-allowed-value t))
  13469. (defun org-property-next-allowed-value (&optional previous)
  13470. "Switch to the next allowed value for this property."
  13471. (interactive)
  13472. (unless (org-at-property-p)
  13473. (error "Not at a property"))
  13474. (let* ((key (match-string 2))
  13475. (value (match-string 3))
  13476. (allowed (or (org-property-get-allowed-values (point) key)
  13477. (and (member value '("[ ]" "[-]" "[X]"))
  13478. '("[ ]" "[X]"))))
  13479. nval)
  13480. (unless allowed
  13481. (error "Allowed values for this property have not been defined"))
  13482. (if previous (setq allowed (reverse allowed)))
  13483. (if (member value allowed)
  13484. (setq nval (car (cdr (member value allowed)))))
  13485. (setq nval (or nval (car allowed)))
  13486. (if (equal nval value)
  13487. (error "Only one allowed value for this property"))
  13488. (org-at-property-p)
  13489. (replace-match (concat " :" key ": " nval) t t)
  13490. (org-indent-line-function)
  13491. (beginning-of-line 1)
  13492. (skip-chars-forward " \t")
  13493. (run-hook-with-args 'org-property-changed-functions key nval)))
  13494. (defun org-find-olp (path &optional this-buffer)
  13495. "Return a marker pointing to the entry at outline path OLP.
  13496. If anything goes wrong, throw an error.
  13497. You can wrap this call to catch the error like this:
  13498. (condition-case msg
  13499. (org-mobile-locate-entry (match-string 4))
  13500. (error (nth 1 msg)))
  13501. The return value will then be either a string with the error message,
  13502. or a marker if everything is OK.
  13503. If THIS-BUFFER is set, the outline path does not contain a file,
  13504. only headings."
  13505. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13506. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13507. (level 1)
  13508. (lmin 1)
  13509. (lmax 1)
  13510. limit re end found pos heading cnt flevel)
  13511. (unless buffer (error "File not found :%s" file))
  13512. (with-current-buffer buffer
  13513. (save-excursion
  13514. (save-restriction
  13515. (widen)
  13516. (setq limit (point-max))
  13517. (goto-char (point-min))
  13518. (while (setq heading (pop path))
  13519. (setq re (format org-complex-heading-regexp-format
  13520. (regexp-quote heading)))
  13521. (setq cnt 0 pos (point))
  13522. (while (re-search-forward re end t)
  13523. (setq level (- (match-end 1) (match-beginning 1)))
  13524. (if (and (>= level lmin) (<= level lmax))
  13525. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13526. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13527. lmax heading))
  13528. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13529. lmax heading))
  13530. (goto-char found)
  13531. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13532. (setq end (save-excursion (org-end-of-subtree t t))))
  13533. (when (org-at-heading-p)
  13534. (move-marker (make-marker) (point))))))))
  13535. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13536. "Find node HEADING in BUFFER.
  13537. Return a marker to the heading if it was found, or nil if not.
  13538. If POS-ONLY is set, return just the position instead of a marker.
  13539. The heading text must match exact, but it may have a TODO keyword,
  13540. a priority cookie and tags in the standard locations."
  13541. (with-current-buffer (or buffer (current-buffer))
  13542. (save-excursion
  13543. (save-restriction
  13544. (widen)
  13545. (goto-char (point-min))
  13546. (let (case-fold-search)
  13547. (if (re-search-forward
  13548. (format org-complex-heading-regexp-format
  13549. (regexp-quote heading)) nil t)
  13550. (if pos-only
  13551. (match-beginning 0)
  13552. (move-marker (make-marker) (match-beginning 0)))))))))
  13553. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13554. "Find Org node headline HEADING in all .org files in directory DIR.
  13555. When the target headline is found, return a marker to this location."
  13556. (let ((files (directory-files (or dir default-directory)
  13557. nil "\\`[^.#].*\\.org\\'"))
  13558. file visiting m buffer)
  13559. (catch 'found
  13560. (while (setq file (pop files))
  13561. (message "trying %s" file)
  13562. (setq visiting (org-find-base-buffer-visiting file))
  13563. (setq buffer (or visiting (find-file-noselect file)))
  13564. (setq m (org-find-exact-headline-in-buffer
  13565. heading buffer))
  13566. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13567. (and m (throw 'found m))))))
  13568. (defun org-find-entry-with-id (ident)
  13569. "Locate the entry that contains the ID property with exact value IDENT.
  13570. IDENT can be a string, a symbol or a number, this function will search for
  13571. the string representation of it.
  13572. Return the position where this entry starts, or nil if there is no such entry."
  13573. (interactive "sID: ")
  13574. (let ((id (cond
  13575. ((stringp ident) ident)
  13576. ((symbol-name ident) (symbol-name ident))
  13577. ((numberp ident) (number-to-string ident))
  13578. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13579. (case-fold-search nil))
  13580. (save-excursion
  13581. (save-restriction
  13582. (widen)
  13583. (goto-char (point-min))
  13584. (when (re-search-forward
  13585. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13586. nil t)
  13587. (org-back-to-heading t)
  13588. (point))))))
  13589. ;;;; Timestamps
  13590. (defvar org-last-changed-timestamp nil)
  13591. (defvar org-last-inserted-timestamp nil
  13592. "The last time stamp inserted with `org-insert-time-stamp'.")
  13593. (defvar org-time-was-given) ; dynamically scoped parameter
  13594. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13595. (defvar org-ts-what) ; dynamically scoped parameter
  13596. (defun org-time-stamp (arg &optional inactive)
  13597. "Prompt for a date/time and insert a time stamp.
  13598. If the user specifies a time like HH:MM, or if this command is called
  13599. with a prefix argument, the time stamp will contain date and time.
  13600. Otherwise, only the date will be included. All parts of a date not
  13601. specified by the user will be filled in from the current date/time.
  13602. So if you press just return without typing anything, the time stamp
  13603. will represent the current date/time. If there is already a timestamp
  13604. at the cursor, it will be modified."
  13605. (interactive "P")
  13606. (let* ((ts nil)
  13607. (default-time
  13608. ;; Default time is either today, or, when entering a range,
  13609. ;; the range start.
  13610. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13611. (save-excursion
  13612. (re-search-backward
  13613. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13614. (- (point) 20) t)))
  13615. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13616. (current-time)))
  13617. (default-input (and ts (org-get-compact-tod ts)))
  13618. (repeater (save-excursion
  13619. (save-match-data
  13620. (beginning-of-line)
  13621. (when (re-search-forward
  13622. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13623. (save-excursion (progn (end-of-line) (point))) t)
  13624. (match-string 0)))))
  13625. org-time-was-given org-end-time-was-given time)
  13626. (cond
  13627. ((and (org-at-timestamp-p t)
  13628. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13629. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13630. (insert "--")
  13631. (setq time (let ((this-command this-command))
  13632. (org-read-date arg 'totime nil nil
  13633. default-time default-input inactive)))
  13634. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13635. ((org-at-timestamp-p t)
  13636. (setq time (let ((this-command this-command))
  13637. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13638. (when (org-at-timestamp-p t) ; just to get the match data
  13639. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13640. (replace-match "")
  13641. (setq org-last-changed-timestamp
  13642. (org-insert-time-stamp
  13643. time (or org-time-was-given arg)
  13644. inactive nil nil (list org-end-time-was-given)))
  13645. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13646. (setq org-last-changed-timestamp
  13647. (concat (substring org-last-inserted-timestamp 0 -1)
  13648. " " repeater ">"))))
  13649. (message "Timestamp updated"))
  13650. (t
  13651. (setq time (let ((this-command this-command))
  13652. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13653. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13654. nil nil (list org-end-time-was-given))))))
  13655. ;; FIXME: can we use this for something else, like computing time differences?
  13656. (defun org-get-compact-tod (s)
  13657. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13658. (let* ((t1 (match-string 1 s))
  13659. (h1 (string-to-number (match-string 2 s)))
  13660. (m1 (string-to-number (match-string 3 s)))
  13661. (t2 (and (match-end 4) (match-string 5 s)))
  13662. (h2 (and t2 (string-to-number (match-string 6 s))))
  13663. (m2 (and t2 (string-to-number (match-string 7 s))))
  13664. dh dm)
  13665. (if (not t2)
  13666. t1
  13667. (setq dh (- h2 h1) dm (- m2 m1))
  13668. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13669. (concat t1 "+" (number-to-string dh)
  13670. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13671. (defun org-time-stamp-inactive (&optional arg)
  13672. "Insert an inactive time stamp.
  13673. An inactive time stamp is enclosed in square brackets instead of angle
  13674. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13675. does not link to the calendar and cannot be changed with the S-cursor keys.
  13676. So these are more for recording a certain time/date."
  13677. (interactive "P")
  13678. (org-time-stamp arg 'inactive))
  13679. (defvar org-date-ovl (make-overlay 1 1))
  13680. (overlay-put org-date-ovl 'face 'org-warning)
  13681. (org-detach-overlay org-date-ovl)
  13682. (defvar org-ans1) ; dynamically scoped parameter
  13683. (defvar org-ans2) ; dynamically scoped parameter
  13684. (defvar org-plain-time-of-day-regexp) ; defined below
  13685. (defvar org-overriding-default-time nil) ; dynamically scoped
  13686. (defvar org-read-date-overlay nil)
  13687. (defvar org-dcst nil) ; dynamically scoped
  13688. (defvar org-read-date-history nil)
  13689. (defvar org-read-date-final-answer nil)
  13690. (defvar org-read-date-analyze-futurep nil)
  13691. (defvar org-read-date-analyze-forced-year nil)
  13692. (defun org-read-date (&optional with-time to-time from-string prompt
  13693. default-time default-input inactive)
  13694. "Read a date, possibly a time, and make things smooth for the user.
  13695. The prompt will suggest to enter an ISO date, but you can also enter anything
  13696. which will at least partially be understood by `parse-time-string'.
  13697. Unrecognized parts of the date will default to the current day, month, year,
  13698. hour and minute. If this command is called to replace a timestamp at point,
  13699. or to enter the second timestamp of a range, the default time is taken
  13700. from the existing stamp. Furthermore, the command prefers the future,
  13701. so if you are giving a date where the year is not given, and the day-month
  13702. combination is already past in the current year, it will assume you
  13703. mean next year. For details, see the manual. A few examples:
  13704. 3-2-5 --> 2003-02-05
  13705. feb 15 --> currentyear-02-15
  13706. 2/15 --> currentyear-02-15
  13707. sep 12 9 --> 2009-09-12
  13708. 12:45 --> today 12:45
  13709. 22 sept 0:34 --> currentyear-09-22 0:34
  13710. 12 --> currentyear-currentmonth-12
  13711. Fri --> nearest Friday (today or later)
  13712. etc.
  13713. Furthermore you can specify a relative date by giving, as the *first* thing
  13714. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13715. change in days weeks, months, years.
  13716. With a single plus or minus, the date is relative to today. With a double
  13717. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13718. +4d --> four days from today
  13719. +4 --> same as above
  13720. +2w --> two weeks from today
  13721. ++5 --> five days from default date
  13722. The function understands only English month and weekday abbreviations,
  13723. but this can be configured with the variables `parse-time-months' and
  13724. `parse-time-weekdays'.
  13725. While prompting, a calendar is popped up - you can also select the
  13726. date with the mouse (button 1). The calendar shows a period of three
  13727. months. To scroll it to other months, use the keys `>' and `<'.
  13728. If you don't like the calendar, turn it off with
  13729. \(setq org-read-date-popup-calendar nil)
  13730. With optional argument TO-TIME, the date will immediately be converted
  13731. to an internal time.
  13732. With an optional argument WITH-TIME, the prompt will suggest to also
  13733. insert a time. Note that when WITH-TIME is not set, you can still
  13734. enter a time, and this function will inform the calling routine about
  13735. this change. The calling routine may then choose to change the format
  13736. used to insert the time stamp into the buffer to include the time.
  13737. With optional argument FROM-STRING, read from this string instead from
  13738. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13739. the time/date that is used for everything that is not specified by the
  13740. user."
  13741. (require 'parse-time)
  13742. (let* ((org-time-stamp-rounding-minutes
  13743. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13744. (org-dcst org-display-custom-times)
  13745. (ct (org-current-time))
  13746. (def (or org-overriding-default-time default-time ct))
  13747. (defdecode (decode-time def))
  13748. (dummy (progn
  13749. (when (< (nth 2 defdecode) org-extend-today-until)
  13750. (setcar (nthcdr 2 defdecode) -1)
  13751. (setcar (nthcdr 1 defdecode) 59)
  13752. (setq def (apply 'encode-time defdecode)
  13753. defdecode (decode-time def)))))
  13754. (calendar-frame-setup nil)
  13755. (calendar-setup nil)
  13756. (calendar-move-hook nil)
  13757. (calendar-view-diary-initially-flag nil)
  13758. (calendar-view-holidays-initially-flag nil)
  13759. (timestr (format-time-string
  13760. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13761. (prompt (concat (if prompt (concat prompt " ") "")
  13762. (format "Date+time [%s]: " timestr)))
  13763. ans (org-ans0 "") org-ans1 org-ans2 final)
  13764. (cond
  13765. (from-string (setq ans from-string))
  13766. (org-read-date-popup-calendar
  13767. (save-excursion
  13768. (save-window-excursion
  13769. (calendar)
  13770. (unwind-protect
  13771. (progn
  13772. (calendar-forward-day (- (time-to-days def)
  13773. (calendar-absolute-from-gregorian
  13774. (calendar-current-date))))
  13775. (org-eval-in-calendar nil t)
  13776. (let* ((old-map (current-local-map))
  13777. (map (copy-keymap calendar-mode-map))
  13778. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13779. (org-defkey map (kbd "RET") 'org-calendar-select)
  13780. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13781. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13782. (org-defkey minibuffer-local-map [(meta shift left)]
  13783. (lambda () (interactive)
  13784. (org-eval-in-calendar '(calendar-backward-month 1))))
  13785. (org-defkey minibuffer-local-map [(meta shift right)]
  13786. (lambda () (interactive)
  13787. (org-eval-in-calendar '(calendar-forward-month 1))))
  13788. (org-defkey minibuffer-local-map [(meta shift up)]
  13789. (lambda () (interactive)
  13790. (org-eval-in-calendar '(calendar-backward-year 1))))
  13791. (org-defkey minibuffer-local-map [(meta shift down)]
  13792. (lambda () (interactive)
  13793. (org-eval-in-calendar '(calendar-forward-year 1))))
  13794. (org-defkey minibuffer-local-map [?\e (shift left)]
  13795. (lambda () (interactive)
  13796. (org-eval-in-calendar '(calendar-backward-month 1))))
  13797. (org-defkey minibuffer-local-map [?\e (shift right)]
  13798. (lambda () (interactive)
  13799. (org-eval-in-calendar '(calendar-forward-month 1))))
  13800. (org-defkey minibuffer-local-map [?\e (shift up)]
  13801. (lambda () (interactive)
  13802. (org-eval-in-calendar '(calendar-backward-year 1))))
  13803. (org-defkey minibuffer-local-map [?\e (shift down)]
  13804. (lambda () (interactive)
  13805. (org-eval-in-calendar '(calendar-forward-year 1))))
  13806. (org-defkey minibuffer-local-map [(shift up)]
  13807. (lambda () (interactive)
  13808. (org-eval-in-calendar '(calendar-backward-week 1))))
  13809. (org-defkey minibuffer-local-map [(shift down)]
  13810. (lambda () (interactive)
  13811. (org-eval-in-calendar '(calendar-forward-week 1))))
  13812. (org-defkey minibuffer-local-map [(shift left)]
  13813. (lambda () (interactive)
  13814. (org-eval-in-calendar '(calendar-backward-day 1))))
  13815. (org-defkey minibuffer-local-map [(shift right)]
  13816. (lambda () (interactive)
  13817. (org-eval-in-calendar '(calendar-forward-day 1))))
  13818. (org-defkey minibuffer-local-map ">"
  13819. (lambda () (interactive)
  13820. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13821. (org-defkey minibuffer-local-map "<"
  13822. (lambda () (interactive)
  13823. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13824. (org-defkey minibuffer-local-map "\C-v"
  13825. (lambda () (interactive)
  13826. (org-eval-in-calendar
  13827. '(calendar-scroll-left-three-months 1))))
  13828. (org-defkey minibuffer-local-map "\M-v"
  13829. (lambda () (interactive)
  13830. (org-eval-in-calendar
  13831. '(calendar-scroll-right-three-months 1))))
  13832. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13833. (unwind-protect
  13834. (progn
  13835. (use-local-map map)
  13836. (setq org-read-date-inactive inactive)
  13837. (add-hook 'post-command-hook 'org-read-date-display)
  13838. (setq org-ans0 (read-string prompt default-input
  13839. 'org-read-date-history nil))
  13840. ;; org-ans0: from prompt
  13841. ;; org-ans1: from mouse click
  13842. ;; org-ans2: from calendar motion
  13843. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13844. (remove-hook 'post-command-hook 'org-read-date-display)
  13845. (use-local-map old-map)
  13846. (when org-read-date-overlay
  13847. (delete-overlay org-read-date-overlay)
  13848. (setq org-read-date-overlay nil)))))
  13849. (bury-buffer "*Calendar*")))))
  13850. (t ; Naked prompt only
  13851. (unwind-protect
  13852. (setq ans (read-string prompt default-input
  13853. 'org-read-date-history timestr))
  13854. (when org-read-date-overlay
  13855. (delete-overlay org-read-date-overlay)
  13856. (setq org-read-date-overlay nil)))))
  13857. (setq final (org-read-date-analyze ans def defdecode))
  13858. (when org-read-date-analyze-forced-year
  13859. (message "Year was forced into %s"
  13860. (if org-read-date-force-compatible-dates
  13861. "compatible range (1970-2037)"
  13862. "range representable on this machine"))
  13863. (ding))
  13864. ;; One round trip to get rid of 34th of August and stuff like that....
  13865. (setq final (decode-time (apply 'encode-time final)))
  13866. (setq org-read-date-final-answer ans)
  13867. (if to-time
  13868. (apply 'encode-time final)
  13869. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13870. (format "%04d-%02d-%02d %02d:%02d"
  13871. (nth 5 final) (nth 4 final) (nth 3 final)
  13872. (nth 2 final) (nth 1 final))
  13873. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13874. (defvar def)
  13875. (defvar defdecode)
  13876. (defvar with-time)
  13877. (defvar org-read-date-inactive)
  13878. (defun org-read-date-display ()
  13879. "Display the current date prompt interpretation in the minibuffer."
  13880. (when org-read-date-display-live
  13881. (when org-read-date-overlay
  13882. (delete-overlay org-read-date-overlay))
  13883. (let ((p (point)))
  13884. (end-of-line 1)
  13885. (while (not (equal (buffer-substring
  13886. (max (point-min) (- (point) 4)) (point))
  13887. " "))
  13888. (insert " "))
  13889. (goto-char p))
  13890. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13891. " " (or org-ans1 org-ans2)))
  13892. (org-end-time-was-given nil)
  13893. (f (org-read-date-analyze ans def defdecode))
  13894. (fmts (if org-dcst
  13895. org-time-stamp-custom-formats
  13896. org-time-stamp-formats))
  13897. (fmt (if (or with-time
  13898. (and (boundp 'org-time-was-given) org-time-was-given))
  13899. (cdr fmts)
  13900. (car fmts)))
  13901. (txt (format-time-string fmt (apply 'encode-time f)))
  13902. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13903. (txt (concat "=> " txt)))
  13904. (when (and org-end-time-was-given
  13905. (string-match org-plain-time-of-day-regexp txt))
  13906. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13907. org-end-time-was-given
  13908. (substring txt (match-end 0)))))
  13909. (when org-read-date-analyze-futurep
  13910. (setq txt (concat txt " (=>F)")))
  13911. (setq org-read-date-overlay
  13912. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13913. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13914. (defun org-read-date-analyze (ans def defdecode)
  13915. "Analyze the combined answer of the date prompt."
  13916. ;; FIXME: cleanup and comment
  13917. (let ((nowdecode (decode-time (current-time)))
  13918. delta deltan deltaw deltadef year month day
  13919. hour minute second wday pm h2 m2 tl wday1
  13920. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13921. (setq org-read-date-analyze-futurep nil
  13922. org-read-date-analyze-forced-year nil)
  13923. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13924. (setq ans "+0"))
  13925. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13926. (setq ans (replace-match "" t t ans)
  13927. deltan (car delta)
  13928. deltaw (nth 1 delta)
  13929. deltadef (nth 2 delta)))
  13930. ;; Check if there is an iso week date in there
  13931. ;; If yes, store the info and postpone interpreting it until the rest
  13932. ;; of the parsing is done
  13933. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13934. (setq iso-year (if (match-end 1)
  13935. (org-small-year-to-year
  13936. (string-to-number (match-string 1 ans))))
  13937. iso-weekday (if (match-end 3)
  13938. (string-to-number (match-string 3 ans)))
  13939. iso-week (string-to-number (match-string 2 ans)))
  13940. (setq ans (replace-match "" t t ans)))
  13941. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13942. (when (string-match
  13943. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13944. (setq year (if (match-end 2)
  13945. (string-to-number (match-string 2 ans))
  13946. (progn (setq kill-year t)
  13947. (string-to-number (format-time-string "%Y"))))
  13948. month (string-to-number (match-string 3 ans))
  13949. day (string-to-number (match-string 4 ans)))
  13950. (if (< year 100) (setq year (+ 2000 year)))
  13951. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13952. t nil ans)))
  13953. ;; Help matching dotted european dates
  13954. (when (string-match
  13955. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13956. (setq year (if (match-end 3)
  13957. (string-to-number (match-string 3 ans))
  13958. (progn (setq kill-year t)
  13959. (string-to-number (format-time-string "%Y"))))
  13960. day (string-to-number (match-string 1 ans))
  13961. month (string-to-number (match-string 2 ans))
  13962. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13963. t nil ans)))
  13964. ;; Help matching american dates, like 5/30 or 5/30/7
  13965. (when (string-match
  13966. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13967. (setq year (if (match-end 4)
  13968. (string-to-number (match-string 4 ans))
  13969. (progn (setq kill-year t)
  13970. (string-to-number (format-time-string "%Y"))))
  13971. month (string-to-number (match-string 1 ans))
  13972. day (string-to-number (match-string 2 ans)))
  13973. (if (< year 100) (setq year (+ 2000 year)))
  13974. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13975. t nil ans)))
  13976. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13977. ;; If there is a time with am/pm, and *no* time without it, we convert
  13978. ;; so that matching will be successful.
  13979. (loop for i from 1 to 2 do ; twice, for end time as well
  13980. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13981. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13982. (setq hour (string-to-number (match-string 1 ans))
  13983. minute (if (match-end 3)
  13984. (string-to-number (match-string 3 ans))
  13985. 0)
  13986. pm (equal ?p
  13987. (string-to-char (downcase (match-string 4 ans)))))
  13988. (if (and (= hour 12) (not pm))
  13989. (setq hour 0)
  13990. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13991. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13992. t t ans))))
  13993. ;; Check if a time range is given as a duration
  13994. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13995. (setq hour (string-to-number (match-string 1 ans))
  13996. h2 (+ hour (string-to-number (match-string 3 ans)))
  13997. minute (string-to-number (match-string 2 ans))
  13998. m2 (+ minute (if (match-end 5) (string-to-number
  13999. (match-string 5 ans))0)))
  14000. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14001. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14002. t t ans)))
  14003. ;; Check if there is a time range
  14004. (when (boundp 'org-end-time-was-given)
  14005. (setq org-time-was-given nil)
  14006. (when (and (string-match org-plain-time-of-day-regexp ans)
  14007. (match-end 8))
  14008. (setq org-end-time-was-given (match-string 8 ans))
  14009. (setq ans (concat (substring ans 0 (match-beginning 7))
  14010. (substring ans (match-end 7))))))
  14011. (setq tl (parse-time-string ans)
  14012. day (or (nth 3 tl) (nth 3 defdecode))
  14013. month (or (nth 4 tl)
  14014. (if (and org-read-date-prefer-future
  14015. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14016. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14017. (nth 4 defdecode)))
  14018. year (or (and (not kill-year) (nth 5 tl))
  14019. (if (and org-read-date-prefer-future
  14020. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14021. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14022. (nth 5 defdecode)))
  14023. hour (or (nth 2 tl) (nth 2 defdecode))
  14024. minute (or (nth 1 tl) (nth 1 defdecode))
  14025. second (or (nth 0 tl) 0)
  14026. wday (nth 6 tl))
  14027. (when (and (eq org-read-date-prefer-future 'time)
  14028. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14029. (equal day (nth 3 nowdecode))
  14030. (equal month (nth 4 nowdecode))
  14031. (equal year (nth 5 nowdecode))
  14032. (nth 2 tl)
  14033. (or (< (nth 2 tl) (nth 2 nowdecode))
  14034. (and (= (nth 2 tl) (nth 2 nowdecode))
  14035. (nth 1 tl)
  14036. (< (nth 1 tl) (nth 1 nowdecode)))))
  14037. (setq day (1+ day)
  14038. futurep t))
  14039. ;; Special date definitions below
  14040. (cond
  14041. (iso-week
  14042. ;; There was an iso week
  14043. (require 'cal-iso)
  14044. (setq futurep nil)
  14045. (setq year (or iso-year year)
  14046. day (or iso-weekday wday 1)
  14047. wday nil ; to make sure that the trigger below does not match
  14048. iso-date (calendar-gregorian-from-absolute
  14049. (calendar-absolute-from-iso
  14050. (list iso-week day year))))
  14051. ; FIXME: Should we also push ISO weeks into the future?
  14052. ; (when (and org-read-date-prefer-future
  14053. ; (not iso-year)
  14054. ; (< (calendar-absolute-from-gregorian iso-date)
  14055. ; (time-to-days (current-time))))
  14056. ; (setq year (1+ year)
  14057. ; iso-date (calendar-gregorian-from-absolute
  14058. ; (calendar-absolute-from-iso
  14059. ; (list iso-week day year)))))
  14060. (setq month (car iso-date)
  14061. year (nth 2 iso-date)
  14062. day (nth 1 iso-date)))
  14063. (deltan
  14064. (setq futurep nil)
  14065. (unless deltadef
  14066. (let ((now (decode-time (current-time))))
  14067. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14068. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14069. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14070. ((equal deltaw "m") (setq month (+ month deltan)))
  14071. ((equal deltaw "y") (setq year (+ year deltan)))))
  14072. ((and wday (not (nth 3 tl)))
  14073. (setq futurep nil)
  14074. ;; Weekday was given, but no day, so pick that day in the week
  14075. ;; on or after the derived date.
  14076. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14077. (unless (equal wday wday1)
  14078. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14079. (if (and (boundp 'org-time-was-given)
  14080. (nth 2 tl))
  14081. (setq org-time-was-given t))
  14082. (if (< year 100) (setq year (+ 2000 year)))
  14083. ;; Check of the date is representable
  14084. (if org-read-date-force-compatible-dates
  14085. (progn
  14086. (if (< year 1970)
  14087. (setq year 1970 org-read-date-analyze-forced-year t))
  14088. (if (> year 2037)
  14089. (setq year 2037 org-read-date-analyze-forced-year t)))
  14090. (condition-case nil
  14091. (ignore (encode-time second minute hour day month year))
  14092. (error
  14093. (setq year (nth 5 defdecode))
  14094. (setq org-read-date-analyze-forced-year t))))
  14095. (setq org-read-date-analyze-futurep futurep)
  14096. (list second minute hour day month year)))
  14097. (defvar parse-time-weekdays)
  14098. (defun org-read-date-get-relative (s today default)
  14099. "Check string S for special relative date string.
  14100. TODAY and DEFAULT are internal times, for today and for a default.
  14101. Return shift list (N what def-flag)
  14102. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14103. N is the number of WHATs to shift.
  14104. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14105. the DEFAULT date rather than TODAY."
  14106. (require 'parse-time)
  14107. (when (and
  14108. (string-match
  14109. (concat
  14110. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14111. "\\([0-9]+\\)?"
  14112. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14113. "\\([ \t]\\|$\\)") s)
  14114. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14115. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14116. (string-to-char (substring (match-string 1 s) -1))
  14117. ?+))
  14118. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14119. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14120. (what (if (match-end 3) (match-string 3 s) "d"))
  14121. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14122. (date (if rel default today))
  14123. (wday (nth 6 (decode-time date)))
  14124. delta)
  14125. (if wday1
  14126. (progn
  14127. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14128. (if (= dir ?-) (setq delta (- delta 7)))
  14129. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14130. (list delta "d" rel))
  14131. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14132. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14133. "Turn a user-specified date into the internal representation.
  14134. The internal representation needed by the calendar is (month day year).
  14135. This is a wrapper to handle the brain-dead convention in calendar that
  14136. user function argument order change dependent on argument order."
  14137. (if (boundp 'calendar-date-style)
  14138. (cond
  14139. ((eq calendar-date-style 'american)
  14140. (list arg1 arg2 arg3))
  14141. ((eq calendar-date-style 'european)
  14142. (list arg2 arg1 arg3))
  14143. ((eq calendar-date-style 'iso)
  14144. (list arg2 arg3 arg1)))
  14145. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14146. (if (org-bound-and-true-p european-calendar-style)
  14147. (list arg2 arg1 arg3)
  14148. (list arg1 arg2 arg3)))))
  14149. (defun org-eval-in-calendar (form &optional keepdate)
  14150. "Eval FORM in the calendar window and return to current window.
  14151. Also, store the cursor date in variable org-ans2."
  14152. (let ((sf (selected-frame))
  14153. (sw (selected-window)))
  14154. (select-window (get-buffer-window "*Calendar*" t))
  14155. (eval form)
  14156. (when (and (not keepdate) (calendar-cursor-to-date))
  14157. (let* ((date (calendar-cursor-to-date))
  14158. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14159. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14160. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14161. (select-window sw)
  14162. (org-select-frame-set-input-focus sf)))
  14163. (defun org-calendar-select ()
  14164. "Return to `org-read-date' with the date currently selected.
  14165. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14166. (interactive)
  14167. (when (calendar-cursor-to-date)
  14168. (let* ((date (calendar-cursor-to-date))
  14169. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14170. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14171. (if (active-minibuffer-window) (exit-minibuffer))))
  14172. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14173. "Insert a date stamp for the date given by the internal TIME.
  14174. WITH-HM means use the stamp format that includes the time of the day.
  14175. INACTIVE means use square brackets instead of angular ones, so that the
  14176. stamp will not contribute to the agenda.
  14177. PRE and POST are optional strings to be inserted before and after the
  14178. stamp.
  14179. The command returns the inserted time stamp."
  14180. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14181. stamp)
  14182. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14183. (insert-before-markers (or pre ""))
  14184. (when (listp extra)
  14185. (setq extra (car extra))
  14186. (if (and (stringp extra)
  14187. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14188. (setq extra (format "-%02d:%02d"
  14189. (string-to-number (match-string 1 extra))
  14190. (string-to-number (match-string 2 extra))))
  14191. (setq extra nil)))
  14192. (when extra
  14193. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14194. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14195. (insert-before-markers (or post ""))
  14196. (setq org-last-inserted-timestamp stamp)))
  14197. (defun org-toggle-time-stamp-overlays ()
  14198. "Toggle the use of custom time stamp formats."
  14199. (interactive)
  14200. (setq org-display-custom-times (not org-display-custom-times))
  14201. (unless org-display-custom-times
  14202. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14203. (while (setq p (next-single-property-change p 'display))
  14204. (if (and (get-text-property p 'display)
  14205. (eq (get-text-property p 'face) 'org-date))
  14206. (remove-text-properties
  14207. p (setq p (next-single-property-change p 'display))
  14208. '(display t))))
  14209. (set-buffer-modified-p bmp)))
  14210. (if (featurep 'xemacs)
  14211. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14212. (org-restart-font-lock)
  14213. (setq org-table-may-need-update t)
  14214. (if org-display-custom-times
  14215. (message "Time stamps are overlaid with custom format")
  14216. (message "Time stamp overlays removed")))
  14217. (defun org-display-custom-time (beg end)
  14218. "Overlay modified time stamp format over timestamp between BEG and END."
  14219. (let* ((ts (buffer-substring beg end))
  14220. t1 w1 with-hm tf time str w2 (off 0))
  14221. (save-match-data
  14222. (setq t1 (org-parse-time-string ts t))
  14223. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14224. (setq off (- (match-end 0) (match-beginning 0)))))
  14225. (setq end (- end off))
  14226. (setq w1 (- end beg)
  14227. with-hm (and (nth 1 t1) (nth 2 t1))
  14228. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14229. time (org-fix-decoded-time t1)
  14230. str (org-add-props
  14231. (format-time-string
  14232. (substring tf 1 -1) (apply 'encode-time time))
  14233. nil 'mouse-face 'highlight)
  14234. w2 (length str))
  14235. (if (not (= w2 w1))
  14236. (add-text-properties (1+ beg) (+ 2 beg)
  14237. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14238. (if (featurep 'xemacs)
  14239. (progn
  14240. (put-text-property beg end 'invisible t)
  14241. (put-text-property beg end 'end-glyph (make-glyph str)))
  14242. (put-text-property beg end 'display str))))
  14243. (defun org-translate-time (string)
  14244. "Translate all timestamps in STRING to custom format.
  14245. But do this only if the variable `org-display-custom-times' is set."
  14246. (when org-display-custom-times
  14247. (save-match-data
  14248. (let* ((start 0)
  14249. (re org-ts-regexp-both)
  14250. t1 with-hm inactive tf time str beg end)
  14251. (while (setq start (string-match re string start))
  14252. (setq beg (match-beginning 0)
  14253. end (match-end 0)
  14254. t1 (save-match-data
  14255. (org-parse-time-string (substring string beg end) t))
  14256. with-hm (and (nth 1 t1) (nth 2 t1))
  14257. inactive (equal (substring string beg (1+ beg)) "[")
  14258. tf (funcall (if with-hm 'cdr 'car)
  14259. org-time-stamp-custom-formats)
  14260. time (org-fix-decoded-time t1)
  14261. str (format-time-string
  14262. (concat
  14263. (if inactive "[" "<") (substring tf 1 -1)
  14264. (if inactive "]" ">"))
  14265. (apply 'encode-time time))
  14266. string (replace-match str t t string)
  14267. start (+ start (length str)))))))
  14268. string)
  14269. (defun org-fix-decoded-time (time)
  14270. "Set 0 instead of nil for the first 6 elements of time.
  14271. Don't touch the rest."
  14272. (let ((n 0))
  14273. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14274. (defun org-days-to-time (timestamp-string)
  14275. "Difference between TIMESTAMP-STRING and now in days."
  14276. (- (time-to-days (org-time-string-to-time timestamp-string))
  14277. (time-to-days (current-time))))
  14278. (defun org-deadline-close (timestamp-string &optional ndays)
  14279. "Is the time in TIMESTAMP-STRING close to the current date?"
  14280. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14281. (and (< (org-days-to-time timestamp-string) ndays)
  14282. (not (org-entry-is-done-p))))
  14283. (defun org-get-wdays (ts)
  14284. "Get the deadline lead time appropriate for timestring TS."
  14285. (cond
  14286. ((<= org-deadline-warning-days 0)
  14287. ;; 0 or negative, enforce this value no matter what
  14288. (- org-deadline-warning-days))
  14289. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14290. ;; lead time is specified.
  14291. (floor (* (string-to-number (match-string 1 ts))
  14292. (cdr (assoc (match-string 2 ts)
  14293. '(("d" . 1) ("w" . 7)
  14294. ("m" . 30.4) ("y" . 365.25)))))))
  14295. ;; go for the default.
  14296. (t org-deadline-warning-days)))
  14297. (defun org-calendar-select-mouse (ev)
  14298. "Return to `org-read-date' with the date currently selected.
  14299. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14300. (interactive "e")
  14301. (mouse-set-point ev)
  14302. (when (calendar-cursor-to-date)
  14303. (let* ((date (calendar-cursor-to-date))
  14304. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14305. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14306. (if (active-minibuffer-window) (exit-minibuffer))))
  14307. (defun org-check-deadlines (ndays)
  14308. "Check if there are any deadlines due or past due.
  14309. A deadline is considered due if it happens within `org-deadline-warning-days'
  14310. days from today's date. If the deadline appears in an entry marked DONE,
  14311. it is not shown. The prefix arg NDAYS can be used to test that many
  14312. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14313. (interactive "P")
  14314. (let* ((org-warn-days
  14315. (cond
  14316. ((equal ndays '(4)) 100000)
  14317. (ndays (prefix-numeric-value ndays))
  14318. (t (abs org-deadline-warning-days))))
  14319. (case-fold-search nil)
  14320. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14321. (callback
  14322. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14323. (message "%d deadlines past-due or due within %d days"
  14324. (org-occur regexp nil callback)
  14325. org-warn-days)))
  14326. (defun org-check-before-date (date)
  14327. "Check if there are deadlines or scheduled entries before DATE."
  14328. (interactive (list (org-read-date)))
  14329. (let ((case-fold-search nil)
  14330. (regexp (concat "\\<\\(" org-deadline-string
  14331. "\\|" org-scheduled-string
  14332. "\\) *<\\([^>]+\\)>"))
  14333. (callback
  14334. (lambda () (time-less-p
  14335. (org-time-string-to-time (match-string 2))
  14336. (org-time-string-to-time date)))))
  14337. (message "%d entries before %s"
  14338. (org-occur regexp nil callback) date)))
  14339. (defun org-check-after-date (date)
  14340. "Check if there are deadlines or scheduled entries after DATE."
  14341. (interactive (list (org-read-date)))
  14342. (let ((case-fold-search nil)
  14343. (regexp (concat "\\<\\(" org-deadline-string
  14344. "\\|" org-scheduled-string
  14345. "\\) *<\\([^>]+\\)>"))
  14346. (callback
  14347. (lambda () (not
  14348. (time-less-p
  14349. (org-time-string-to-time (match-string 2))
  14350. (org-time-string-to-time date))))))
  14351. (message "%d entries after %s"
  14352. (org-occur regexp nil callback) date)))
  14353. (defun org-check-dates-range (start-date end-date)
  14354. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14355. (interactive (list (org-read-date nil nil nil "Range starts")
  14356. (org-read-date nil nil nil "Range end")))
  14357. (let ((case-fold-search nil)
  14358. (regexp (concat "\\<\\(" org-deadline-string
  14359. "\\|" org-scheduled-string
  14360. "\\) *<\\([^>]+\\)>"))
  14361. (callback
  14362. (lambda ()
  14363. (let ((match (match-string 2)))
  14364. (and
  14365. (not (time-less-p
  14366. (org-time-string-to-time match)
  14367. (org-time-string-to-time start-date)))
  14368. (time-less-p
  14369. (org-time-string-to-time match)
  14370. (org-time-string-to-time end-date)))))))
  14371. (message "%d entries between %s and %s"
  14372. (org-occur regexp nil callback) start-date end-date)))
  14373. (defun org-evaluate-time-range (&optional to-buffer)
  14374. "Evaluate a time range by computing the difference between start and end.
  14375. Normally the result is just printed in the echo area, but with prefix arg
  14376. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14377. If the time range is actually in a table, the result is inserted into the
  14378. next column.
  14379. For time difference computation, a year is assumed to be exactly 365
  14380. days in order to avoid rounding problems."
  14381. (interactive "P")
  14382. (or
  14383. (org-clock-update-time-maybe)
  14384. (save-excursion
  14385. (unless (org-at-date-range-p t)
  14386. (goto-char (point-at-bol))
  14387. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14388. (if (not (org-at-date-range-p t))
  14389. (error "Not at a time-stamp range, and none found in current line")))
  14390. (let* ((ts1 (match-string 1))
  14391. (ts2 (match-string 2))
  14392. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14393. (match-end (match-end 0))
  14394. (time1 (org-time-string-to-time ts1))
  14395. (time2 (org-time-string-to-time ts2))
  14396. (t1 (org-float-time time1))
  14397. (t2 (org-float-time time2))
  14398. (diff (abs (- t2 t1)))
  14399. (negative (< (- t2 t1) 0))
  14400. ;; (ys (floor (* 365 24 60 60)))
  14401. (ds (* 24 60 60))
  14402. (hs (* 60 60))
  14403. (fy "%dy %dd %02d:%02d")
  14404. (fy1 "%dy %dd")
  14405. (fd "%dd %02d:%02d")
  14406. (fd1 "%dd")
  14407. (fh "%02d:%02d")
  14408. y d h m align)
  14409. (if havetime
  14410. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14411. y 0
  14412. d (floor (/ diff ds)) diff (mod diff ds)
  14413. h (floor (/ diff hs)) diff (mod diff hs)
  14414. m (floor (/ diff 60)))
  14415. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14416. y 0
  14417. d (floor (+ (/ diff ds) 0.5))
  14418. h 0 m 0))
  14419. (if (not to-buffer)
  14420. (message "%s" (org-make-tdiff-string y d h m))
  14421. (if (org-at-table-p)
  14422. (progn
  14423. (goto-char match-end)
  14424. (setq align t)
  14425. (and (looking-at " *|") (goto-char (match-end 0))))
  14426. (goto-char match-end))
  14427. (if (looking-at
  14428. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14429. (replace-match ""))
  14430. (if negative (insert " -"))
  14431. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14432. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14433. (insert " " (format fh h m))))
  14434. (if align (org-table-align))
  14435. (message "Time difference inserted")))))
  14436. (defun org-make-tdiff-string (y d h m)
  14437. (let ((fmt "")
  14438. (l nil))
  14439. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14440. l (push y l)))
  14441. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14442. l (push d l)))
  14443. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14444. l (push h l)))
  14445. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14446. l (push m l)))
  14447. (apply 'format fmt (nreverse l))))
  14448. (defun org-time-string-to-time (s &optional buffer pos)
  14449. (condition-case errdata
  14450. (apply 'encode-time (org-parse-time-string s))
  14451. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14452. s (if (not (and buffer pos))
  14453. ""
  14454. (format " at %d in buffer `%s'" pos buffer))
  14455. (cdr errdata)))))
  14456. (defun org-time-string-to-seconds (s)
  14457. (org-float-time (org-time-string-to-time s)))
  14458. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14459. "Convert a time stamp to an absolute day number.
  14460. If there is a specifier for a cyclic time stamp, get the closest date to
  14461. DAYNR.
  14462. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14463. The variable date is bound by the calendar when this is called."
  14464. (cond
  14465. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14466. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14467. daynr
  14468. (+ daynr 1000)))
  14469. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14470. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14471. (time-to-days (current-time))) (match-string 0 s)
  14472. prefer show-all))
  14473. (t (time-to-days
  14474. (condition-case errdata
  14475. (apply 'encode-time (org-parse-time-string s))
  14476. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14477. s (if (not (and buffer pos))
  14478. ""
  14479. (format " at %d in buffer `%s'" pos buffer))
  14480. (cdr errdata))))))))
  14481. (defun org-days-to-iso-week (days)
  14482. "Return the iso week number."
  14483. (require 'cal-iso)
  14484. (car (calendar-iso-from-absolute days)))
  14485. (defun org-small-year-to-year (year)
  14486. "Convert 2-digit years into 4-digit years.
  14487. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14488. The year 2000 cannot be abbreviated. Any year larger than 99
  14489. is returned unchanged."
  14490. (if (< year 38)
  14491. (setq year (+ 2000 year))
  14492. (if (< year 100)
  14493. (setq year (+ 1900 year))))
  14494. year)
  14495. (defun org-time-from-absolute (d)
  14496. "Return the time corresponding to date D.
  14497. D may be an absolute day number, or a calendar-type list (month day year)."
  14498. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14499. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14500. (defun org-calendar-holiday ()
  14501. "List of holidays, for Diary display in Org-mode."
  14502. (require 'holidays)
  14503. (let ((hl (funcall
  14504. (if (fboundp 'calendar-check-holidays)
  14505. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14506. (if hl (mapconcat 'identity hl "; "))))
  14507. (defun org-diary-sexp-entry (sexp entry date)
  14508. "Process a SEXP diary ENTRY for DATE."
  14509. (require 'diary-lib)
  14510. (let ((result (if calendar-debug-sexp
  14511. (let ((stack-trace-on-error t))
  14512. (eval (car (read-from-string sexp))))
  14513. (condition-case nil
  14514. (eval (car (read-from-string sexp)))
  14515. (error
  14516. (beep)
  14517. (message "Bad sexp at line %d in %s: %s"
  14518. (org-current-line)
  14519. (buffer-file-name) sexp)
  14520. (sleep-for 2))))))
  14521. (cond ((stringp result) (split-string result "; "))
  14522. ((and (consp result)
  14523. (not (consp (cdr result)))
  14524. (stringp (cdr result))) (cdr result))
  14525. ((and (consp result)
  14526. (stringp (car result))) result)
  14527. (result entry)
  14528. (t nil))))
  14529. (defun org-diary-to-ical-string (frombuf)
  14530. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14531. This uses the icalendar.el library."
  14532. (let* ((tmpdir (if (featurep 'xemacs)
  14533. (temp-directory)
  14534. temporary-file-directory))
  14535. (tmpfile (make-temp-name
  14536. (expand-file-name "orgics" tmpdir)))
  14537. buf rtn b e)
  14538. (with-current-buffer frombuf
  14539. (icalendar-export-region (point-min) (point-max) tmpfile)
  14540. (setq buf (find-buffer-visiting tmpfile))
  14541. (set-buffer buf)
  14542. (goto-char (point-min))
  14543. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14544. (setq b (match-beginning 0)))
  14545. (goto-char (point-max))
  14546. (if (re-search-backward "^END:VEVENT" nil t)
  14547. (setq e (match-end 0)))
  14548. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14549. (kill-buffer buf)
  14550. (delete-file tmpfile)
  14551. rtn))
  14552. (defun org-closest-date (start current change prefer show-all)
  14553. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14554. When PREFER is `past', return a date that is either CURRENT or past.
  14555. When PREFER is `future', return a date that is either CURRENT or future.
  14556. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14557. ;; Make the proper lists from the dates
  14558. (catch 'exit
  14559. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14560. dn dw sday cday n1 n2 n0
  14561. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14562. (setq start (org-date-to-gregorian start)
  14563. current (org-date-to-gregorian
  14564. (if show-all
  14565. current
  14566. (time-to-days (current-time))))
  14567. sday (calendar-absolute-from-gregorian start)
  14568. cday (calendar-absolute-from-gregorian current))
  14569. (if (<= cday sday) (throw 'exit sday))
  14570. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14571. (setq dn (string-to-number (match-string 1 change))
  14572. dw (cdr (assoc (match-string 2 change) a1)))
  14573. (error "Invalid change specifier: %s" change))
  14574. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14575. (cond
  14576. ((eq dw 'day)
  14577. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14578. n2 (+ n1 dn)))
  14579. ((eq dw 'year)
  14580. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14581. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14582. (setq date1 (list m d y1)
  14583. n1 (calendar-absolute-from-gregorian date1)
  14584. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14585. n2 (calendar-absolute-from-gregorian date2)))
  14586. ((eq dw 'month)
  14587. ;; approx number of month between the two dates
  14588. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14589. ;; How often does dn fit in there?
  14590. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14591. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14592. m (+ m nm)
  14593. ny (floor (/ m 12))
  14594. y (+ y ny)
  14595. m (- m (* ny 12)))
  14596. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14597. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14598. (setq m2 (+ m dn) y2 y)
  14599. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14600. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14601. (while (<= n2 cday)
  14602. (setq n1 n2 m m2 y y2)
  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. ;; Make sure n1 is the earlier date
  14607. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14608. (if show-all
  14609. (cond
  14610. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14611. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14612. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14613. (cond
  14614. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14615. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14616. (t (if (= cday n1) n1 n2)))))))
  14617. (defun org-date-to-gregorian (date)
  14618. "Turn any specification of DATE into a Gregorian date for the calendar."
  14619. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14620. ((and (listp date) (= (length date) 3)) date)
  14621. ((stringp date)
  14622. (setq date (org-parse-time-string date))
  14623. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14624. ((listp date)
  14625. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14626. (defun org-parse-time-string (s &optional nodefault)
  14627. "Parse the standard Org-mode time string.
  14628. This should be a lot faster than the normal `parse-time-string'.
  14629. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14630. hour and minute fields will be nil if not given."
  14631. (if (string-match org-ts-regexp0 s)
  14632. (list 0
  14633. (if (or (match-beginning 8) (not nodefault))
  14634. (string-to-number (or (match-string 8 s) "0")))
  14635. (if (or (match-beginning 7) (not nodefault))
  14636. (string-to-number (or (match-string 7 s) "0")))
  14637. (string-to-number (match-string 4 s))
  14638. (string-to-number (match-string 3 s))
  14639. (string-to-number (match-string 2 s))
  14640. nil nil nil)
  14641. (error "Not a standard Org-mode time string: %s" s)))
  14642. (defun org-timestamp-up (&optional arg)
  14643. "Increase the date item at the cursor by one.
  14644. If the cursor is on the year, change the year. If it is on the month,
  14645. the day or the time, change that.
  14646. With prefix ARG, change by that many units."
  14647. (interactive "p")
  14648. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14649. (defun org-timestamp-down (&optional arg)
  14650. "Decrease the date item at the cursor by one.
  14651. If the cursor is on the year, change the year. If it is on the month,
  14652. the day or the time, change that.
  14653. With prefix ARG, change by that many units."
  14654. (interactive "p")
  14655. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14656. (defun org-timestamp-up-day (&optional arg)
  14657. "Increase the date in the time stamp by one day.
  14658. With prefix ARG, change that many days."
  14659. (interactive "p")
  14660. (if (and (not (org-at-timestamp-p t))
  14661. (org-at-heading-p))
  14662. (org-todo 'up)
  14663. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14664. (defun org-timestamp-down-day (&optional arg)
  14665. "Decrease the date in the time stamp by one day.
  14666. With prefix ARG, change that many days."
  14667. (interactive "p")
  14668. (if (and (not (org-at-timestamp-p t))
  14669. (org-at-heading-p))
  14670. (org-todo 'down)
  14671. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14672. (defun org-at-timestamp-p (&optional inactive-ok)
  14673. "Determine if the cursor is in or at a timestamp."
  14674. (interactive)
  14675. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14676. (pos (point))
  14677. (ans (or (looking-at tsr)
  14678. (save-excursion
  14679. (skip-chars-backward "^[<\n\r\t")
  14680. (if (> (point) (point-min)) (backward-char 1))
  14681. (and (looking-at tsr)
  14682. (> (- (match-end 0) pos) -1))))))
  14683. (and ans
  14684. (boundp 'org-ts-what)
  14685. (setq org-ts-what
  14686. (cond
  14687. ((= pos (match-beginning 0)) 'bracket)
  14688. ;; Point is considered to be "on the bracket" whether
  14689. ;; it's really on it or right after it.
  14690. ((or (= pos (1- (match-end 0)))
  14691. (= pos (match-end 0))) 'bracket)
  14692. ((org-pos-in-match-range pos 2) 'year)
  14693. ((org-pos-in-match-range pos 3) 'month)
  14694. ((org-pos-in-match-range pos 7) 'hour)
  14695. ((org-pos-in-match-range pos 8) 'minute)
  14696. ((or (org-pos-in-match-range pos 4)
  14697. (org-pos-in-match-range pos 5)) 'day)
  14698. ((and (> pos (or (match-end 8) (match-end 5)))
  14699. (< pos (match-end 0)))
  14700. (- pos (or (match-end 8) (match-end 5))))
  14701. (t 'day))))
  14702. ans))
  14703. (defun org-toggle-timestamp-type ()
  14704. "Toggle the type (<active> or [inactive]) of a time stamp."
  14705. (interactive)
  14706. (when (org-at-timestamp-p t)
  14707. (let ((beg (match-beginning 0)) (end (match-end 0))
  14708. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14709. (save-excursion
  14710. (goto-char beg)
  14711. (while (re-search-forward "[][<>]" end t)
  14712. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14713. t t)))
  14714. (message "Timestamp is now %sactive"
  14715. (if (equal (char-after beg) ?<) "" "in")))))
  14716. (defun org-timestamp-change (n &optional what updown)
  14717. "Change the date in the time stamp at point.
  14718. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14719. `year', `minute', `second'. If WHAT is not given, the cursor position
  14720. in the timestamp determines what will be changed."
  14721. (let ((origin (point)) origin-cat
  14722. with-hm inactive
  14723. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14724. org-ts-what
  14725. extra rem
  14726. ts time time0)
  14727. (if (not (org-at-timestamp-p t))
  14728. (error "Not at a timestamp"))
  14729. (if (and (not what) (eq org-ts-what 'bracket))
  14730. (org-toggle-timestamp-type)
  14731. ;; Point isn't on brackets. Remember the part of the time-stamp
  14732. ;; the point was in. Indeed, size of time-stamps may change,
  14733. ;; but point must be kept in the same category nonetheless.
  14734. (setq origin-cat org-ts-what)
  14735. (if (and (not what) (not (eq org-ts-what 'day))
  14736. org-display-custom-times
  14737. (get-text-property (point) 'display)
  14738. (not (get-text-property (1- (point)) 'display)))
  14739. (setq org-ts-what 'day))
  14740. (setq org-ts-what (or what org-ts-what)
  14741. inactive (= (char-after (match-beginning 0)) ?\[)
  14742. ts (match-string 0))
  14743. (replace-match "")
  14744. (if (string-match
  14745. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14746. ts)
  14747. (setq extra (match-string 1 ts)))
  14748. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14749. (setq with-hm t))
  14750. (setq time0 (org-parse-time-string ts))
  14751. (when (and updown
  14752. (eq org-ts-what 'minute)
  14753. (not current-prefix-arg))
  14754. ;; This looks like s-up and s-down. Change by one rounding step.
  14755. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14756. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14757. (setcar (cdr time0) (+ (nth 1 time0)
  14758. (if (> n 0) (- rem) (- dm rem))))))
  14759. (setq time
  14760. (encode-time (or (car time0) 0)
  14761. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14762. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14763. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14764. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14765. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14766. (nthcdr 6 time0)))
  14767. (when (and (member org-ts-what '(hour minute))
  14768. extra
  14769. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14770. (setq extra (org-modify-ts-extra
  14771. extra
  14772. (if (eq org-ts-what 'hour) 2 5)
  14773. n dm)))
  14774. (when (integerp org-ts-what)
  14775. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14776. (if (eq what 'calendar)
  14777. (let ((cal-date (org-get-date-from-calendar)))
  14778. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14779. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14780. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14781. (setcar time0 (or (car time0) 0))
  14782. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14783. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14784. (setq time (apply 'encode-time time0))))
  14785. ;; Insert the new time-stamp, and ensure point stays in the same
  14786. ;; category as before (i.e. not after the last position in that
  14787. ;; category).
  14788. (let ((pos (point)))
  14789. ;; Stay before inserted string. `save-excursion' is of no use.
  14790. (setq org-last-changed-timestamp
  14791. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14792. (goto-char pos))
  14793. (save-match-data
  14794. (looking-at org-ts-regexp3)
  14795. (goto-char (cond
  14796. ;; `day' category ends before `hour' if any, or at
  14797. ;; the end of the day name.
  14798. ((eq origin-cat 'day)
  14799. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14800. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14801. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14802. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14803. ;; `year' and `month' have both fixed size: point
  14804. ;; couldn't have moved into another part.
  14805. (t origin))))
  14806. ;; Update clock if on a CLOCK line.
  14807. (org-clock-update-time-maybe)
  14808. ;; Try to recenter the calendar window, if any.
  14809. (if (and org-calendar-follow-timestamp-change
  14810. (get-buffer-window "*Calendar*" t)
  14811. (memq org-ts-what '(day month year)))
  14812. (org-recenter-calendar (time-to-days time))))))
  14813. (defun org-modify-ts-extra (s pos n dm)
  14814. "Change the different parts of the lead-time and repeat fields in timestamp."
  14815. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14816. ng h m new rem)
  14817. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14818. (cond
  14819. ((or (org-pos-in-match-range pos 2)
  14820. (org-pos-in-match-range pos 3))
  14821. (setq m (string-to-number (match-string 3 s))
  14822. h (string-to-number (match-string 2 s)))
  14823. (if (org-pos-in-match-range pos 2)
  14824. (setq h (+ h n))
  14825. (setq n (* dm (org-no-warnings (signum n))))
  14826. (when (not (= 0 (setq rem (% m dm))))
  14827. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14828. (setq m (+ m n)))
  14829. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14830. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14831. (setq h (min 24 (max 0 h)))
  14832. (setq ng 1 new (format "-%02d:%02d" h m)))
  14833. ((org-pos-in-match-range pos 6)
  14834. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14835. ((org-pos-in-match-range pos 5)
  14836. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14837. ((org-pos-in-match-range pos 9)
  14838. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14839. ((org-pos-in-match-range pos 8)
  14840. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14841. (when ng
  14842. (setq s (concat
  14843. (substring s 0 (match-beginning ng))
  14844. new
  14845. (substring s (match-end ng))))))
  14846. s))
  14847. (defun org-recenter-calendar (date)
  14848. "If the calendar is visible, recenter it to DATE."
  14849. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14850. (when cwin
  14851. (let ((calendar-move-hook nil))
  14852. (with-selected-window cwin
  14853. (calendar-goto-date (if (listp date) date
  14854. (calendar-gregorian-from-absolute date))))))))
  14855. (defun org-goto-calendar (&optional arg)
  14856. "Go to the Emacs calendar at the current date.
  14857. If there is a time stamp in the current line, go to that date.
  14858. A prefix ARG can be used to force the current date."
  14859. (interactive "P")
  14860. (let ((tsr org-ts-regexp) diff
  14861. (calendar-move-hook nil)
  14862. (calendar-view-holidays-initially-flag nil)
  14863. (calendar-view-diary-initially-flag nil))
  14864. (if (or (org-at-timestamp-p)
  14865. (save-excursion
  14866. (beginning-of-line 1)
  14867. (looking-at (concat ".*" tsr))))
  14868. (let ((d1 (time-to-days (current-time)))
  14869. (d2 (time-to-days
  14870. (org-time-string-to-time (match-string 1)))))
  14871. (setq diff (- d2 d1))))
  14872. (calendar)
  14873. (calendar-goto-today)
  14874. (if (and diff (not arg)) (calendar-forward-day diff))))
  14875. (defun org-get-date-from-calendar ()
  14876. "Return a list (month day year) of date at point in calendar."
  14877. (with-current-buffer "*Calendar*"
  14878. (save-match-data
  14879. (calendar-cursor-to-date))))
  14880. (defun org-date-from-calendar ()
  14881. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14882. If there is already a time stamp at the cursor position, update it."
  14883. (interactive)
  14884. (if (org-at-timestamp-p t)
  14885. (org-timestamp-change 0 'calendar)
  14886. (let ((cal-date (org-get-date-from-calendar)))
  14887. (org-insert-time-stamp
  14888. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14889. (defun org-minutes-to-hh:mm-string (m)
  14890. "Compute H:MM from a number of minutes."
  14891. (let ((h (/ m 60)))
  14892. (setq m (- m (* 60 h)))
  14893. (format org-time-clocksum-format h m)))
  14894. (defun org-hh:mm-string-to-minutes (s)
  14895. "Convert a string H:MM to a number of minutes.
  14896. If the string is just a number, interpret it as minutes.
  14897. In fact, the first hh:mm or number in the string will be taken,
  14898. there can be extra stuff in the string.
  14899. If no number is found, the return value is 0."
  14900. (cond
  14901. ((integerp s) s)
  14902. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14903. (+ (* (string-to-number (match-string 1 s)) 60)
  14904. (string-to-number (match-string 2 s))))
  14905. ((string-match "\\([0-9]+\\)" s)
  14906. (string-to-number (match-string 1 s)))
  14907. (t 0)))
  14908. (defcustom org-effort-durations
  14909. `(("h" . 60)
  14910. ("d" . ,(* 60 8))
  14911. ("w" . ,(* 60 8 5))
  14912. ("m" . ,(* 60 8 5 4))
  14913. ("y" . ,(* 60 8 5 40)))
  14914. "Conversion factor to minutes for an effort modifier.
  14915. Each entry has the form (MODIFIER . MINUTES).
  14916. In an effort string, a number followed by MODIFIER is multiplied
  14917. by the specified number of MINUTES to obtain an effort in
  14918. minutes.
  14919. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14920. effort string \"2hours\" is equivalent to 120 minutes."
  14921. :group 'org-agenda
  14922. :version "24.1"
  14923. :type '(alist :key-type (string :tag "Modifier")
  14924. :value-type (number :tag "Minutes")))
  14925. (defun org-duration-string-to-minutes (s)
  14926. "Convert a duration string S to minutes.
  14927. A bare number is interpreted as minutes, modifiers can be set by
  14928. customizing `org-effort-durations' (which see).
  14929. Entries containing a colon are interpreted as H:MM by
  14930. `org-hh:mm-string-to-minutes'."
  14931. (let ((result 0)
  14932. (re (concat "\\([0-9]+\\) *\\("
  14933. (regexp-opt (mapcar 'car org-effort-durations))
  14934. "\\)")))
  14935. (while (string-match re s)
  14936. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14937. (string-to-number (match-string 1 s))))
  14938. (setq s (replace-match "" nil t s)))
  14939. (incf result (org-hh:mm-string-to-minutes s))
  14940. result))
  14941. ;;;; Files
  14942. (defun org-save-all-org-buffers ()
  14943. "Save all Org-mode buffers without user confirmation."
  14944. (interactive)
  14945. (message "Saving all Org-mode buffers...")
  14946. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14947. (when (featurep 'org-id) (org-id-locations-save))
  14948. (message "Saving all Org-mode buffers... done"))
  14949. (defun org-revert-all-org-buffers ()
  14950. "Revert all Org-mode buffers.
  14951. Prompt for confirmation when there are unsaved changes.
  14952. Be sure you know what you are doing before letting this function
  14953. overwrite your changes.
  14954. This function is useful in a setup where one tracks org files
  14955. with a version control system, to revert on one machine after pulling
  14956. changes from another. I believe the procedure must be like this:
  14957. 1. M-x org-save-all-org-buffers
  14958. 2. Pull changes from the other machine, resolve conflicts
  14959. 3. M-x org-revert-all-org-buffers"
  14960. (interactive)
  14961. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14962. (error "Abort"))
  14963. (save-excursion
  14964. (save-window-excursion
  14965. (mapc
  14966. (lambda (b)
  14967. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14968. (with-current-buffer b buffer-file-name))
  14969. (org-pop-to-buffer-same-window b)
  14970. (revert-buffer t 'no-confirm)))
  14971. (buffer-list))
  14972. (when (and (featurep 'org-id) org-id-track-globally)
  14973. (org-id-locations-load)))))
  14974. ;;;; Agenda files
  14975. ;;;###autoload
  14976. (defun org-switchb (&optional arg)
  14977. "Switch between Org buffers.
  14978. With one prefix argument, restrict available buffers to files.
  14979. With two prefix arguments, restrict available buffers to agenda files.
  14980. Defaults to `iswitchb' for buffer name completion.
  14981. Set `org-completion-use-ido' to make it use ido instead."
  14982. (interactive "P")
  14983. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14984. ((equal arg '(16)) (org-buffer-list 'agenda))
  14985. (t (org-buffer-list))))
  14986. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14987. (org-completion-use-ido org-completion-use-ido))
  14988. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14989. (setq org-completion-use-iswitchb t))
  14990. (org-pop-to-buffer-same-window
  14991. (org-icompleting-read "Org buffer: "
  14992. (mapcar 'list (mapcar 'buffer-name blist))
  14993. nil t))))
  14994. ;;; Define some older names previously used for this functionality
  14995. ;;;###autoload
  14996. (defalias 'org-ido-switchb 'org-switchb)
  14997. ;;;###autoload
  14998. (defalias 'org-iswitchb 'org-switchb)
  14999. (defun org-buffer-list (&optional predicate exclude-tmp)
  15000. "Return a list of Org buffers.
  15001. PREDICATE can be `export', `files' or `agenda'.
  15002. export restrict the list to Export buffers.
  15003. files restrict the list to buffers visiting Org files.
  15004. agenda restrict the list to buffers visiting agenda files.
  15005. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15006. (let* ((bfn nil)
  15007. (agenda-files (and (eq predicate 'agenda)
  15008. (mapcar 'file-truename (org-agenda-files t))))
  15009. (filter
  15010. (cond
  15011. ((eq predicate 'files)
  15012. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  15013. ((eq predicate 'export)
  15014. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15015. ((eq predicate 'agenda)
  15016. (lambda (b)
  15017. (with-current-buffer b
  15018. (and (eq major-mode 'org-mode)
  15019. (setq bfn (buffer-file-name b))
  15020. (member (file-truename bfn) agenda-files)))))
  15021. (t (lambda (b) (with-current-buffer b
  15022. (or (eq major-mode 'org-mode)
  15023. (string-match "\*Org .*Export"
  15024. (buffer-name b)))))))))
  15025. (delq nil
  15026. (mapcar
  15027. (lambda(b)
  15028. (if (and (funcall filter b)
  15029. (or (not exclude-tmp)
  15030. (not (string-match "tmp" (buffer-name b)))))
  15031. b
  15032. nil))
  15033. (buffer-list)))))
  15034. (defun org-agenda-files (&optional unrestricted archives)
  15035. "Get the list of agenda files.
  15036. Optional UNRESTRICTED means return the full list even if a restriction
  15037. is currently in place.
  15038. When ARCHIVES is t, include all archive files that are really being
  15039. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15040. `org-agenda-archives-mode' is t."
  15041. (let ((files
  15042. (cond
  15043. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15044. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15045. ((listp org-agenda-files) org-agenda-files)
  15046. (t (error "Invalid value of `org-agenda-files'")))))
  15047. (setq files (apply 'append
  15048. (mapcar (lambda (f)
  15049. (if (file-directory-p f)
  15050. (directory-files
  15051. f t org-agenda-file-regexp)
  15052. (list f)))
  15053. files)))
  15054. (when org-agenda-skip-unavailable-files
  15055. (setq files (delq nil
  15056. (mapcar (function
  15057. (lambda (file)
  15058. (and (file-readable-p file) file)))
  15059. files))))
  15060. (when (or (eq archives t)
  15061. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15062. (setq files (org-add-archive-files files)))
  15063. files))
  15064. (defun org-agenda-file-p (&optional file)
  15065. "Return non-nil, if FILE is an agenda file.
  15066. If FILE is omitted, use the file associated with the current
  15067. buffer."
  15068. (member (or file (buffer-file-name))
  15069. (org-agenda-files t)))
  15070. (defun org-edit-agenda-file-list ()
  15071. "Edit the list of agenda files.
  15072. Depending on setup, this either uses customize to edit the variable
  15073. `org-agenda-files', or it visits the file that is holding the list. In the
  15074. latter case, the buffer is set up in a way that saving it automatically kills
  15075. the buffer and restores the previous window configuration."
  15076. (interactive)
  15077. (if (stringp org-agenda-files)
  15078. (let ((cw (current-window-configuration)))
  15079. (find-file org-agenda-files)
  15080. (org-set-local 'org-window-configuration cw)
  15081. (org-add-hook 'after-save-hook
  15082. (lambda ()
  15083. (set-window-configuration
  15084. (prog1 org-window-configuration
  15085. (kill-buffer (current-buffer))))
  15086. (org-install-agenda-files-menu)
  15087. (message "New agenda file list installed"))
  15088. nil 'local)
  15089. (message "%s" (substitute-command-keys
  15090. "Edit list and finish with \\[save-buffer]")))
  15091. (customize-variable 'org-agenda-files)))
  15092. (defun org-store-new-agenda-file-list (list)
  15093. "Set new value for the agenda file list and save it correctly."
  15094. (if (stringp org-agenda-files)
  15095. (let ((fe (org-read-agenda-file-list t)) b u)
  15096. (while (setq b (find-buffer-visiting org-agenda-files))
  15097. (kill-buffer b))
  15098. (with-temp-file org-agenda-files
  15099. (insert
  15100. (mapconcat
  15101. (lambda (f) ;; Keep un-expanded entries.
  15102. (if (setq u (assoc f fe))
  15103. (cdr u)
  15104. f))
  15105. list "\n")
  15106. "\n")))
  15107. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15108. (org-insert-mode-line-in-empty-file nil))
  15109. (setq org-agenda-files list)
  15110. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15111. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15112. "Read the list of agenda files from a file.
  15113. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15114. filenames, used by `org-store-new-agenda-file-list' to write back
  15115. un-expanded file names."
  15116. (when (file-directory-p org-agenda-files)
  15117. (error "`org-agenda-files' cannot be a single directory"))
  15118. (when (stringp org-agenda-files)
  15119. (with-temp-buffer
  15120. (insert-file-contents org-agenda-files)
  15121. (mapcar
  15122. (lambda (f)
  15123. (let ((e (expand-file-name (substitute-in-file-name f)
  15124. org-directory)))
  15125. (if pair-with-expansion
  15126. (cons e f)
  15127. e)))
  15128. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15129. ;;;###autoload
  15130. (defun org-cycle-agenda-files ()
  15131. "Cycle through the files in `org-agenda-files'.
  15132. If the current buffer visits an agenda file, find the next one in the list.
  15133. If the current buffer does not, find the first agenda file."
  15134. (interactive)
  15135. (let* ((fs (org-agenda-files t))
  15136. (files (append fs (list (car fs))))
  15137. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15138. file)
  15139. (unless files (error "No agenda files"))
  15140. (catch 'exit
  15141. (while (setq file (pop files))
  15142. (if (equal (file-truename file) tcf)
  15143. (when (car files)
  15144. (find-file (car files))
  15145. (throw 'exit t))))
  15146. (find-file (car fs)))
  15147. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15148. (defun org-agenda-file-to-front (&optional to-end)
  15149. "Move/add the current file to the top of the agenda file list.
  15150. If the file is not present in the list, it is added to the front. If it is
  15151. present, it is moved there. With optional argument TO-END, add/move to the
  15152. end of the list."
  15153. (interactive "P")
  15154. (let ((org-agenda-skip-unavailable-files nil)
  15155. (file-alist (mapcar (lambda (x)
  15156. (cons (file-truename x) x))
  15157. (org-agenda-files t)))
  15158. (ctf (file-truename buffer-file-name))
  15159. x had)
  15160. (setq x (assoc ctf file-alist) had x)
  15161. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15162. (if to-end
  15163. (setq file-alist (append (delq x file-alist) (list x)))
  15164. (setq file-alist (cons x (delq x file-alist))))
  15165. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15166. (org-install-agenda-files-menu)
  15167. (message "File %s to %s of agenda file list"
  15168. (if had "moved" "added") (if to-end "end" "front"))))
  15169. (defun org-remove-file (&optional file)
  15170. "Remove current file from the list of files in variable `org-agenda-files'.
  15171. These are the files which are being checked for agenda entries.
  15172. Optional argument FILE means use this file instead of the current."
  15173. (interactive)
  15174. (let* ((org-agenda-skip-unavailable-files nil)
  15175. (file (or file buffer-file-name))
  15176. (true-file (file-truename file))
  15177. (afile (abbreviate-file-name file))
  15178. (files (delq nil (mapcar
  15179. (lambda (x)
  15180. (if (equal true-file
  15181. (file-truename x))
  15182. nil x))
  15183. (org-agenda-files t)))))
  15184. (if (not (= (length files) (length (org-agenda-files t))))
  15185. (progn
  15186. (org-store-new-agenda-file-list files)
  15187. (org-install-agenda-files-menu)
  15188. (message "Removed file: %s" afile))
  15189. (message "File was not in list: %s (not removed)" afile))))
  15190. (defun org-file-menu-entry (file)
  15191. (vector file (list 'find-file file) t))
  15192. (defun org-check-agenda-file (file)
  15193. "Make sure FILE exists. If not, ask user what to do."
  15194. (when (not (file-exists-p file))
  15195. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15196. (abbreviate-file-name file))
  15197. (let ((r (downcase (read-char-exclusive))))
  15198. (cond
  15199. ((equal r ?r)
  15200. (org-remove-file file)
  15201. (throw 'nextfile t))
  15202. (t (error "Abort"))))))
  15203. (defun org-get-agenda-file-buffer (file)
  15204. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15205. it to the list of buffers which might be released later."
  15206. (let ((buf (org-find-base-buffer-visiting file)))
  15207. (if buf
  15208. buf ; just return it
  15209. ;; Make a new buffer and remember it
  15210. (setq buf (find-file-noselect file))
  15211. (if buf (push buf org-agenda-new-buffers))
  15212. buf)))
  15213. (defun org-release-buffers (blist)
  15214. "Release all buffers in list, asking the user for confirmation when needed.
  15215. When a buffer is unmodified, it is just killed. When modified, it is saved
  15216. \(if the user agrees) and then killed."
  15217. (let (buf file)
  15218. (while (setq buf (pop blist))
  15219. (setq file (buffer-file-name buf))
  15220. (when (and (buffer-modified-p buf)
  15221. file
  15222. (y-or-n-p (format "Save file %s? " file)))
  15223. (with-current-buffer buf (save-buffer)))
  15224. (kill-buffer buf))))
  15225. (defun org-prepare-agenda-buffers (files)
  15226. "Create buffers for all agenda files, protect archived trees and comments."
  15227. (interactive)
  15228. (let ((pa '(:org-archived t))
  15229. (pc '(:org-comment t))
  15230. (pall '(:org-archived t :org-comment t))
  15231. (inhibit-read-only t)
  15232. (rea (concat ":" org-archive-tag ":"))
  15233. bmp file re)
  15234. (save-excursion
  15235. (save-restriction
  15236. (while (setq file (pop files))
  15237. (catch 'nextfile
  15238. (if (bufferp file)
  15239. (set-buffer file)
  15240. (org-check-agenda-file file)
  15241. (set-buffer (org-get-agenda-file-buffer file)))
  15242. (widen)
  15243. (setq bmp (buffer-modified-p))
  15244. (org-refresh-category-properties)
  15245. (setq org-todo-keywords-for-agenda
  15246. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15247. (setq org-done-keywords-for-agenda
  15248. (append org-done-keywords-for-agenda org-done-keywords))
  15249. (setq org-todo-keyword-alist-for-agenda
  15250. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15251. (setq org-drawers-for-agenda
  15252. (append org-drawers-for-agenda org-drawers))
  15253. (setq org-tag-alist-for-agenda
  15254. (append org-tag-alist-for-agenda org-tag-alist))
  15255. (save-excursion
  15256. (remove-text-properties (point-min) (point-max) pall)
  15257. (when org-agenda-skip-archived-trees
  15258. (goto-char (point-min))
  15259. (while (re-search-forward rea nil t)
  15260. (if (org-at-heading-p t)
  15261. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15262. (goto-char (point-min))
  15263. (setq re (format org-heading-keyword-regexp-format
  15264. org-comment-string))
  15265. (while (re-search-forward re nil t)
  15266. (add-text-properties
  15267. (match-beginning 0) (org-end-of-subtree t) pc)))
  15268. (set-buffer-modified-p bmp)))))
  15269. (setq org-todo-keywords-for-agenda
  15270. (org-uniquify org-todo-keywords-for-agenda))
  15271. (setq org-todo-keyword-alist-for-agenda
  15272. (org-uniquify org-todo-keyword-alist-for-agenda)
  15273. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15274. ;;;; Embedded LaTeX
  15275. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15276. "Keymap for the minor `org-cdlatex-mode'.")
  15277. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15278. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15279. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15280. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15281. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15282. (defvar org-cdlatex-texmathp-advice-is-done nil
  15283. "Flag remembering if we have applied the advice to texmathp already.")
  15284. (define-minor-mode org-cdlatex-mode
  15285. "Toggle the minor `org-cdlatex-mode'.
  15286. This mode supports entering LaTeX environment and math in LaTeX fragments
  15287. in Org-mode.
  15288. \\{org-cdlatex-mode-map}"
  15289. nil " OCDL" nil
  15290. (when org-cdlatex-mode
  15291. (require 'cdlatex)
  15292. (run-hooks 'cdlatex-mode-hook)
  15293. (cdlatex-compute-tables))
  15294. (unless org-cdlatex-texmathp-advice-is-done
  15295. (setq org-cdlatex-texmathp-advice-is-done t)
  15296. (defadvice texmathp (around org-math-always-on activate)
  15297. "Always return t in org-mode buffers.
  15298. This is because we want to insert math symbols without dollars even outside
  15299. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15300. an embedded LaTeX fragment, let texmathp do its job.
  15301. \\[org-cdlatex-mode-map]"
  15302. (interactive)
  15303. (let (p)
  15304. (cond
  15305. ((not (eq major-mode 'org-mode)) ad-do-it)
  15306. ((eq this-command 'cdlatex-math-symbol)
  15307. (setq ad-return-value t
  15308. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15309. (t
  15310. (let ((p (org-inside-LaTeX-fragment-p)))
  15311. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15312. (setq ad-return-value t
  15313. texmathp-why '("Org-mode embedded math" . 0))
  15314. (if p ad-do-it)))))))))
  15315. (defun turn-on-org-cdlatex ()
  15316. "Unconditionally turn on `org-cdlatex-mode'."
  15317. (org-cdlatex-mode 1))
  15318. (defun org-inside-LaTeX-fragment-p ()
  15319. "Test if point is inside a LaTeX fragment.
  15320. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15321. sequence appearing also before point.
  15322. Even though the matchers for math are configurable, this function assumes
  15323. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15324. delimiters are skipped when they have been removed by customization.
  15325. The return value is nil, or a cons cell with the delimiter and the
  15326. position of this delimiter.
  15327. This function does a reasonably good job, but can locally be fooled by
  15328. for example currency specifications. For example it will assume being in
  15329. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15330. fragments that are properly closed, but during editing, we have to live
  15331. with the uncertainty caused by missing closing delimiters. This function
  15332. looks only before point, not after."
  15333. (catch 'exit
  15334. (let ((pos (point))
  15335. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15336. (lim (progn
  15337. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15338. (point)))
  15339. dd-on str (start 0) m re)
  15340. (goto-char pos)
  15341. (when dodollar
  15342. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15343. re (nth 1 (assoc "$" org-latex-regexps)))
  15344. (while (string-match re str start)
  15345. (cond
  15346. ((= (match-end 0) (length str))
  15347. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15348. ((= (match-end 0) (- (length str) 5))
  15349. (throw 'exit nil))
  15350. (t (setq start (match-end 0))))))
  15351. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15352. (goto-char pos)
  15353. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15354. (and (match-beginning 2) (throw 'exit nil))
  15355. ;; count $$
  15356. (while (re-search-backward "\\$\\$" lim t)
  15357. (setq dd-on (not dd-on)))
  15358. (goto-char pos)
  15359. (if dd-on (cons "$$" m))))))
  15360. (defun org-inside-latex-macro-p ()
  15361. "Is point inside a LaTeX macro or its arguments?"
  15362. (save-match-data
  15363. (org-in-regexp
  15364. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15365. (defun org-try-cdlatex-tab ()
  15366. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15367. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15368. - inside a LaTeX fragment, or
  15369. - after the first word in a line, where an abbreviation expansion could
  15370. insert a LaTeX environment."
  15371. (when org-cdlatex-mode
  15372. (cond
  15373. ;; Before any word on the line: No expansion possible.
  15374. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15375. ;; Just after first word on the line: Expand it. Make sure it
  15376. ;; cannot happen on headlines, though.
  15377. ((save-excursion
  15378. (skip-chars-backward "a-zA-Z0-9*")
  15379. (skip-chars-backward " \t")
  15380. (and (bolp) (not (org-at-heading-p))))
  15381. (cdlatex-tab) t)
  15382. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15383. (defun org-cdlatex-underscore-caret (&optional arg)
  15384. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15385. Revert to the normal definition outside of these fragments."
  15386. (interactive "P")
  15387. (if (org-inside-LaTeX-fragment-p)
  15388. (call-interactively 'cdlatex-sub-superscript)
  15389. (let (org-cdlatex-mode)
  15390. (call-interactively (key-binding (vector last-input-event))))))
  15391. (defun org-cdlatex-math-modify (&optional arg)
  15392. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15393. Revert to the normal definition outside of these fragments."
  15394. (interactive "P")
  15395. (if (org-inside-LaTeX-fragment-p)
  15396. (call-interactively 'cdlatex-math-modify)
  15397. (let (org-cdlatex-mode)
  15398. (call-interactively (key-binding (vector last-input-event))))))
  15399. (defvar org-latex-fragment-image-overlays nil
  15400. "List of overlays carrying the images of latex fragments.")
  15401. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15402. (defun org-remove-latex-fragment-image-overlays ()
  15403. "Remove all overlays with LaTeX fragment images in current buffer."
  15404. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15405. (setq org-latex-fragment-image-overlays nil))
  15406. (defun org-preview-latex-fragment (&optional subtree)
  15407. "Preview the LaTeX fragment at point, or all locally or globally.
  15408. If the cursor is in a LaTeX fragment, create the image and overlay
  15409. it over the source code. If there is no fragment at point, display
  15410. all fragments in the current text, from one headline to the next. With
  15411. prefix SUBTREE, display all fragments in the current subtree. With a
  15412. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15413. the cursor is before the first headline,
  15414. display all fragments in the buffer.
  15415. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15416. (interactive "P")
  15417. (unless buffer-file-name
  15418. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15419. (org-remove-latex-fragment-image-overlays)
  15420. (save-excursion
  15421. (save-restriction
  15422. (let (beg end at msg)
  15423. (cond
  15424. ((or (equal subtree '(16))
  15425. (not (save-excursion
  15426. (re-search-backward org-outline-regexp-bol nil t))))
  15427. (setq beg (point-min) end (point-max)
  15428. msg "Creating images for buffer...%s"))
  15429. ((equal subtree '(4))
  15430. (org-back-to-heading)
  15431. (setq beg (point) end (org-end-of-subtree t)
  15432. msg "Creating images for subtree...%s"))
  15433. (t
  15434. (if (setq at (org-inside-LaTeX-fragment-p))
  15435. (goto-char (max (point-min) (- (cdr at) 2)))
  15436. (org-back-to-heading))
  15437. (setq beg (point) end (progn (outline-next-heading) (point))
  15438. msg (if at "Creating image...%s"
  15439. "Creating images for entry...%s"))))
  15440. (message msg "")
  15441. (narrow-to-region beg end)
  15442. (goto-char beg)
  15443. (org-format-latex
  15444. (concat "ltxpng/" (file-name-sans-extension
  15445. (file-name-nondirectory
  15446. buffer-file-name)))
  15447. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15448. (message msg "done. Use `C-c C-c' to remove images.")))))
  15449. (defvar org-latex-regexps
  15450. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15451. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15452. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15453. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15454. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15455. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15456. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15457. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15458. "Regular expressions for matching embedded LaTeX.")
  15459. (defvar org-export-have-math nil) ;; dynamic scoping
  15460. (defun org-format-latex (prefix &optional dir overlays msg at
  15461. forbuffer processing-type)
  15462. "Replace LaTeX fragments with links to an image, and produce images.
  15463. Some of the options can be changed using the variable
  15464. `org-format-latex-options'."
  15465. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15466. (let* ((prefixnodir (file-name-nondirectory prefix))
  15467. (absprefix (expand-file-name prefix dir))
  15468. (todir (file-name-directory absprefix))
  15469. (opt org-format-latex-options)
  15470. (matchers (plist-get opt :matchers))
  15471. (re-list org-latex-regexps)
  15472. (org-format-latex-header-extra
  15473. (plist-get (org-infile-export-plist) :latex-header-extra))
  15474. (cnt 0) txt hash link beg end re e checkdir
  15475. executables-checked string
  15476. m n block-type block linkfile movefile ov)
  15477. ;; Check the different regular expressions
  15478. (while (setq e (pop re-list))
  15479. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15480. block (if block-type "\n\n" ""))
  15481. (when (member m matchers)
  15482. (goto-char (point-min))
  15483. (while (re-search-forward re nil t)
  15484. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15485. (not (get-text-property (match-beginning n)
  15486. 'org-protected))
  15487. (or (not overlays)
  15488. (not (eq (get-char-property (match-beginning n)
  15489. 'org-overlay-type)
  15490. 'org-latex-overlay))))
  15491. (setq org-export-have-math t)
  15492. (cond
  15493. ((eq processing-type 'verbatim)
  15494. ;; Leave the text verbatim, just protect it
  15495. (add-text-properties (match-beginning n) (match-end n)
  15496. '(org-protected t)))
  15497. ((eq processing-type 'mathjax)
  15498. ;; Prepare for MathJax processing
  15499. (setq string (match-string n))
  15500. (if (member m '("$" "$1"))
  15501. (save-excursion
  15502. (delete-region (match-beginning n) (match-end n))
  15503. (goto-char (match-beginning n))
  15504. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15505. "\\)")
  15506. '(org-protected t))))
  15507. (add-text-properties (match-beginning n) (match-end n)
  15508. '(org-protected t))))
  15509. ((eq processing-type 'dvipng)
  15510. ;; Process to an image
  15511. (setq txt (match-string n)
  15512. beg (match-beginning n) end (match-end n)
  15513. cnt (1+ cnt))
  15514. (let (print-length print-level) ; make sure full list is printed
  15515. (setq hash (sha1 (prin1-to-string
  15516. (list org-format-latex-header
  15517. org-format-latex-header-extra
  15518. org-export-latex-default-packages-alist
  15519. org-export-latex-packages-alist
  15520. org-format-latex-options
  15521. forbuffer txt)))
  15522. linkfile (format "%s_%s.png" prefix hash)
  15523. movefile (format "%s_%s.png" absprefix hash)))
  15524. (setq link (concat block "[[file:" linkfile "]]" block))
  15525. (if msg (message msg cnt))
  15526. (goto-char beg)
  15527. (unless checkdir ; make sure the directory exists
  15528. (setq checkdir t)
  15529. (or (file-directory-p todir) (make-directory todir t)))
  15530. (unless executables-checked
  15531. (org-check-external-command
  15532. "latex" "needed to convert LaTeX fragments to images")
  15533. (org-check-external-command
  15534. "dvipng" "needed to convert LaTeX fragments to images")
  15535. (setq executables-checked t))
  15536. (unless (file-exists-p movefile)
  15537. (org-create-formula-image
  15538. txt movefile opt forbuffer))
  15539. (if overlays
  15540. (progn
  15541. (mapc (lambda (o)
  15542. (if (eq (overlay-get o 'org-overlay-type)
  15543. 'org-latex-overlay)
  15544. (delete-overlay o)))
  15545. (overlays-in beg end))
  15546. (setq ov (make-overlay beg end))
  15547. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15548. (if (featurep 'xemacs)
  15549. (progn
  15550. (overlay-put ov 'invisible t)
  15551. (overlay-put
  15552. ov 'end-glyph
  15553. (make-glyph (vector 'png :file movefile))))
  15554. (overlay-put
  15555. ov 'display
  15556. (list 'image :type 'png :file movefile :ascent 'center)))
  15557. (push ov org-latex-fragment-image-overlays)
  15558. (goto-char end))
  15559. (delete-region beg end)
  15560. (insert (org-add-props link
  15561. (list 'org-latex-src
  15562. (replace-regexp-in-string
  15563. "\"" "" txt)
  15564. 'org-latex-src-embed-type
  15565. (if block-type 'paragraph 'character))))))
  15566. ((eq processing-type 'mathml)
  15567. ;; Process to MathML
  15568. (unless executables-checked
  15569. (unless (save-match-data (org-format-latex-mathml-available-p))
  15570. (error "LaTeX to MathML converter not configured"))
  15571. (setq executables-checked t))
  15572. (setq txt (match-string n)
  15573. beg (match-beginning n) end (match-end n)
  15574. cnt (1+ cnt))
  15575. (if msg (message msg cnt))
  15576. (goto-char beg)
  15577. (delete-region beg end)
  15578. (insert (org-format-latex-as-mathml
  15579. txt block-type prefix dir)))
  15580. (t
  15581. (error "Unknown conversion type %s for latex fragments"
  15582. processing-type)))))))))
  15583. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15584. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15585. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15586. sucessful, return the portion between \"<math...> </math>\"
  15587. elements otherwise return nil. When MATHML-FILE is specified,
  15588. write the results in to that file. When invoked as an
  15589. interactive command, prompt for LATEX-FRAG, with initial value
  15590. set to the current active region and echo the results for user
  15591. inspection."
  15592. (interactive (list (let ((frag (when (region-active-p)
  15593. (buffer-substring-no-properties
  15594. (region-beginning) (region-end)))))
  15595. (read-string "LaTeX Fragment: " frag nil frag))))
  15596. (unless latex-frag (error "Invalid latex-frag"))
  15597. (let* ((tmp-in-file (file-relative-name
  15598. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15599. (ignore (write-region latex-frag nil tmp-in-file))
  15600. (tmp-out-file (file-relative-name
  15601. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15602. (cmd (format-spec
  15603. org-latex-to-mathml-convert-command
  15604. `((?j . ,(shell-quote-argument
  15605. (expand-file-name org-latex-to-mathml-jar-file)))
  15606. (?I . ,(shell-quote-argument tmp-in-file))
  15607. (?o . ,(shell-quote-argument tmp-out-file)))))
  15608. mathml shell-command-output)
  15609. (when (org-called-interactively-p 'any)
  15610. (unless (org-format-latex-mathml-available-p)
  15611. (error "LaTeX to MathML converter not configured")))
  15612. (message "Running %s" cmd)
  15613. (setq shell-command-output (shell-command-to-string cmd))
  15614. (setq mathml
  15615. (when (file-readable-p tmp-out-file)
  15616. (with-current-buffer (find-file-noselect tmp-out-file t)
  15617. (goto-char (point-min))
  15618. (when (re-search-forward
  15619. (concat
  15620. (regexp-quote
  15621. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15622. "\\(.\\|\n\\)*"
  15623. (regexp-quote "</math>")) nil t)
  15624. (prog1 (match-string 0) (kill-buffer))))))
  15625. (cond
  15626. (mathml
  15627. (setq mathml
  15628. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15629. (when mathml-file
  15630. (write-region mathml nil mathml-file))
  15631. (when (org-called-interactively-p 'any)
  15632. (message mathml)))
  15633. ((message "LaTeX to MathML conversion failed")
  15634. (message shell-command-output)))
  15635. (delete-file tmp-in-file)
  15636. (when (file-exists-p tmp-out-file)
  15637. (delete-file tmp-out-file))
  15638. mathml))
  15639. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15640. prefix &optional dir)
  15641. "Use `org-create-math-formula' but check local cache first."
  15642. (let* ((absprefix (expand-file-name prefix dir))
  15643. (print-length nil) (print-level nil)
  15644. (formula-id (concat
  15645. "formula-"
  15646. (sha1
  15647. (prin1-to-string
  15648. (list latex-frag
  15649. org-latex-to-mathml-convert-command)))))
  15650. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15651. (formula-cache-dir (file-name-directory formula-cache)))
  15652. (unless (file-directory-p formula-cache-dir)
  15653. (make-directory formula-cache-dir t))
  15654. (unless (file-exists-p formula-cache)
  15655. (org-create-math-formula latex-frag formula-cache))
  15656. (if (file-exists-p formula-cache)
  15657. ;; Successful conversion. Return the link to MathML file.
  15658. (org-add-props
  15659. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15660. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15661. 'org-latex-src-embed-type (if latex-frag-type
  15662. 'paragraph 'character)))
  15663. ;; Failed conversion. Return the LaTeX fragment verbatim
  15664. (add-text-properties
  15665. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15666. latex-frag)))
  15667. ;; This function borrows from Ganesh Swami's latex2png.el
  15668. (defun org-create-formula-image (string tofile options buffer)
  15669. "This calls dvipng."
  15670. (require 'org-latex)
  15671. (let* ((tmpdir (if (featurep 'xemacs)
  15672. (temp-directory)
  15673. temporary-file-directory))
  15674. (texfilebase (make-temp-name
  15675. (expand-file-name "orgtex" tmpdir)))
  15676. (texfile (concat texfilebase ".tex"))
  15677. (dvifile (concat texfilebase ".dvi"))
  15678. (pngfile (concat texfilebase ".png"))
  15679. (fnh (if (featurep 'xemacs)
  15680. (font-height (get-face-font 'default))
  15681. (face-attribute 'default :height nil)))
  15682. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15683. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15684. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15685. "Black"))
  15686. (bg (or (plist-get options (if buffer :background :html-background))
  15687. "Transparent")))
  15688. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15689. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15690. (with-temp-file texfile
  15691. (insert (org-splice-latex-header
  15692. org-format-latex-header
  15693. org-export-latex-default-packages-alist
  15694. org-export-latex-packages-alist t
  15695. org-format-latex-header-extra))
  15696. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15697. (require 'org-latex)
  15698. (org-export-latex-fix-inputenc))
  15699. (let ((dir default-directory))
  15700. (condition-case nil
  15701. (progn
  15702. (cd tmpdir)
  15703. (call-process "latex" nil nil nil texfile))
  15704. (error nil))
  15705. (cd dir))
  15706. (if (not (file-exists-p dvifile))
  15707. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15708. (condition-case nil
  15709. (call-process "dvipng" nil nil nil
  15710. "-fg" fg "-bg" bg
  15711. "-D" dpi
  15712. ;;"-x" scale "-y" scale
  15713. "-T" "tight"
  15714. "-o" pngfile
  15715. dvifile)
  15716. (error nil))
  15717. (if (not (file-exists-p pngfile))
  15718. (if org-format-latex-signal-error
  15719. (error "Failed to create png file from %s" texfile)
  15720. (message "Failed to create png file from %s" texfile)
  15721. nil)
  15722. ;; Use the requested file name and clean up
  15723. (copy-file pngfile tofile 'replace)
  15724. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15725. (delete-file (concat texfilebase e)))
  15726. pngfile))))
  15727. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15728. "Fill a LaTeX header template TPL.
  15729. In the template, the following place holders will be recognized:
  15730. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15731. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15732. [PACKAGES] \\usepackage statements for PKG
  15733. [NO-PACKAGES] do not include PKG
  15734. [EXTRA] the string EXTRA
  15735. [NO-EXTRA] do not include EXTRA
  15736. For backward compatibility, if both the positive and the negative place
  15737. holder is missing, the positive one (without the \"NO-\") will be
  15738. assumed to be present at the end of the template.
  15739. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15740. EXTRA is a string.
  15741. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15742. (let (rpl (end ""))
  15743. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15744. (setq rpl (if (or (match-end 1) (not def-pkg))
  15745. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15746. tpl (replace-match rpl t t tpl))
  15747. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15748. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15749. (setq rpl (if (or (match-end 1) (not pkg))
  15750. "" (org-latex-packages-to-string pkg snippets-p t))
  15751. tpl (replace-match rpl t t tpl))
  15752. (if pkg (setq end
  15753. (concat end "\n"
  15754. (org-latex-packages-to-string pkg snippets-p)))))
  15755. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15756. (setq rpl (if (or (match-end 1) (not extra))
  15757. "" (concat extra "\n"))
  15758. tpl (replace-match rpl t t tpl))
  15759. (if (and extra (string-match "\\S-" extra))
  15760. (setq end (concat end "\n" extra))))
  15761. (if (string-match "\\S-" end)
  15762. (concat tpl "\n" end)
  15763. tpl)))
  15764. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15765. "Turn an alist of packages into a string with the \\usepackage macros."
  15766. (setq pkg (mapconcat (lambda(p)
  15767. (cond
  15768. ((stringp p) p)
  15769. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15770. (format "%% Package %s omitted" (cadr p)))
  15771. ((equal "" (car p))
  15772. (format "\\usepackage{%s}" (cadr p)))
  15773. (t
  15774. (format "\\usepackage[%s]{%s}"
  15775. (car p) (cadr p)))))
  15776. pkg
  15777. "\n"))
  15778. (if newline (concat pkg "\n") pkg))
  15779. (defun org-dvipng-color (attr)
  15780. "Return an rgb color specification for dvipng."
  15781. (apply 'format "rgb %s %s %s"
  15782. (mapcar 'org-normalize-color
  15783. (color-values (face-attribute 'default attr nil)))))
  15784. (defun org-normalize-color (value)
  15785. "Return string to be used as color value for an RGB component."
  15786. (format "%g" (/ value 65535.0)))
  15787. ;; Image display
  15788. (defvar org-inline-image-overlays nil)
  15789. (make-variable-buffer-local 'org-inline-image-overlays)
  15790. (defun org-toggle-inline-images (&optional include-linked)
  15791. "Toggle the display of inline images.
  15792. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15793. (interactive "P")
  15794. (if org-inline-image-overlays
  15795. (progn
  15796. (org-remove-inline-images)
  15797. (message "Inline image display turned off"))
  15798. (org-display-inline-images include-linked)
  15799. (if org-inline-image-overlays
  15800. (message "%d images displayed inline"
  15801. (length org-inline-image-overlays))
  15802. (message "No images to display inline"))))
  15803. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15804. "Display inline images.
  15805. Normally only links without a description part are inlined, because this
  15806. is how it will work for export. When INCLUDE-LINKED is set, also links
  15807. with a description part will be inlined. This can be nice for a quick
  15808. look at those images, but it does not reflect what exported files will look
  15809. like.
  15810. When REFRESH is set, refresh existing images between BEG and END.
  15811. This will create new image displays only if necessary.
  15812. BEG and END default to the buffer boundaries."
  15813. (interactive "P")
  15814. (unless refresh
  15815. (org-remove-inline-images)
  15816. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15817. (save-excursion
  15818. (save-restriction
  15819. (widen)
  15820. (setq beg (or beg (point-min)) end (or end (point-max)))
  15821. (goto-char (point-min))
  15822. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15823. (substring (org-image-file-name-regexp) 0 -2)
  15824. "\\)\\]" (if include-linked "" "\\]")))
  15825. old file ov img)
  15826. (while (re-search-forward re end t)
  15827. (setq old (get-char-property-and-overlay (match-beginning 1)
  15828. 'org-image-overlay))
  15829. (setq file (expand-file-name
  15830. (concat (or (match-string 3) "") (match-string 4))))
  15831. (when (file-exists-p file)
  15832. (if (and (car-safe old) refresh)
  15833. (image-refresh (overlay-get (cdr old) 'display))
  15834. (setq img (save-match-data (create-image file)))
  15835. (when img
  15836. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15837. (overlay-put ov 'display img)
  15838. (overlay-put ov 'face 'default)
  15839. (overlay-put ov 'org-image-overlay t)
  15840. (overlay-put ov 'modification-hooks
  15841. (list 'org-display-inline-modification-hook))
  15842. (push ov org-inline-image-overlays)))))))))
  15843. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15844. "Remove inline-display overlay if a corresponding region is modified."
  15845. (let ((inhibit-modification-hooks t))
  15846. (when (and ov after)
  15847. (delete ov org-inline-image-overlays)
  15848. (delete-overlay ov))))
  15849. (defun org-remove-inline-images ()
  15850. "Remove inline display of images."
  15851. (interactive)
  15852. (mapc 'delete-overlay org-inline-image-overlays)
  15853. (setq org-inline-image-overlays nil))
  15854. ;;;; Key bindings
  15855. ;; Outline functions from `outline-mode-prefix-map'
  15856. ;; that can be remapped in Org:
  15857. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15858. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15859. (define-key org-mode-map [remap outline-forward-same-level]
  15860. 'org-forward-same-level)
  15861. (define-key org-mode-map [remap outline-backward-same-level]
  15862. 'org-backward-same-level)
  15863. (define-key org-mode-map [remap show-branches]
  15864. 'org-kill-note-or-show-branches)
  15865. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15866. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15867. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15868. ;; Outline functions from `outline-mode-prefix-map'
  15869. ;; that can not be remapped in Org:
  15870. ;; - the column "key binding" shows whether the Outline function is still
  15871. ;; available in Org mode on the same key that it has been bound to in
  15872. ;; Outline mode:
  15873. ;; - "overridden": key used for a different functionality in Org mode
  15874. ;; - else: key still bound to the same Outline function in Org mode
  15875. ;; | Outline function | key binding | Org replacement |
  15876. ;; |------------------------------------+-------------+-----------------------|
  15877. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15878. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15879. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15880. ;; | `hide-subtree' | overridden | visibility cycling |
  15881. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15882. ;; | `hide-body' | overridden | no replacement |
  15883. ;; | `show-all' | overridden | no replacement |
  15884. ;; | `hide-entry' | overridden | visibility cycling |
  15885. ;; | `show-entry' | overridden | no replacement |
  15886. ;; | `hide-leaves' | overridden | no replacement |
  15887. ;; | `hide-sublevels' | overridden | no replacement |
  15888. ;; | `hide-other' | overridden | no replacement |
  15889. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15890. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15891. ;; Make `C-c C-x' a prefix key
  15892. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15893. ;; TAB key with modifiers
  15894. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15895. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15896. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15897. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15898. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15899. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15900. ;; The following line is necessary under Suse GNU/Linux
  15901. (unless (featurep 'xemacs)
  15902. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15903. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15904. (define-key org-mode-map [backtab] 'org-shifttab)
  15905. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15906. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15907. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15908. ;; Cursor keys with modifiers
  15909. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15910. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15911. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15912. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15913. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15914. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15915. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15916. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15917. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15918. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15919. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15920. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15921. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15922. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15923. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15924. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15925. ;; Babel keys
  15926. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15927. (mapc (lambda (pair)
  15928. (define-key org-babel-map (car pair) (cdr pair)))
  15929. org-babel-key-bindings)
  15930. ;;; Extra keys for tty access.
  15931. ;; We only set them when really needed because otherwise the
  15932. ;; menus don't show the simple keys
  15933. (when (or org-use-extra-keys
  15934. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15935. (not window-system))
  15936. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15937. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15938. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15939. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15940. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15941. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15942. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15943. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15944. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15945. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15946. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15947. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15948. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15949. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15950. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15951. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15952. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15953. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15954. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15955. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15956. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15957. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15958. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15959. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15960. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15961. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15962. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15963. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15964. ;; All the other keys
  15965. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15966. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15967. (if (boundp 'narrow-map)
  15968. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15969. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15970. (if (boundp 'narrow-map)
  15971. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15972. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15973. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15974. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15975. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15976. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15977. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15978. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  15979. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15980. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15981. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15982. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15983. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15984. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15985. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15986. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15987. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15988. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15989. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15990. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15991. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15992. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15993. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15994. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15995. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15996. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15997. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15998. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15999. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16000. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16001. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16002. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16003. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16004. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16005. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16006. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16007. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16008. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16009. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16010. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16011. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16012. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16013. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16014. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16015. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16016. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16017. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16018. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16019. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16020. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16021. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16022. (org-defkey org-mode-map "\C-m" 'org-return)
  16023. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16024. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16025. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16026. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16027. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16028. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16029. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16030. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16031. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16032. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16033. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16034. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16035. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16036. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16037. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16038. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16039. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16040. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16041. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16042. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16043. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16044. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16045. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16046. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16047. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16048. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16049. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16050. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16051. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16052. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16053. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16054. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16055. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16056. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16057. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16058. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16059. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16060. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16061. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16062. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16063. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16064. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16065. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16066. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16067. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16068. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16069. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16070. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16071. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16072. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16073. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16074. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16075. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16076. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16077. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16078. (when (featurep 'xemacs)
  16079. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16080. (defconst org-speed-commands-default
  16081. '(
  16082. ("Outline Navigation")
  16083. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16084. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16085. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16086. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16087. ("u" . (org-speed-move-safe 'outline-up-heading))
  16088. ("j" . org-goto)
  16089. ("g" . (org-refile t))
  16090. ("Outline Visibility")
  16091. ("c" . org-cycle)
  16092. ("C" . org-shifttab)
  16093. (" " . org-display-outline-path)
  16094. ("Outline Structure Editing")
  16095. ("U" . org-shiftmetaup)
  16096. ("D" . org-shiftmetadown)
  16097. ("r" . org-metaright)
  16098. ("l" . org-metaleft)
  16099. ("R" . org-shiftmetaright)
  16100. ("L" . org-shiftmetaleft)
  16101. ("i" . (progn (forward-char 1) (call-interactively
  16102. 'org-insert-heading-respect-content)))
  16103. ("^" . org-sort)
  16104. ("w" . org-refile)
  16105. ("a" . org-archive-subtree-default-with-confirmation)
  16106. ("." . org-mark-subtree)
  16107. ("Clock Commands")
  16108. ("I" . org-clock-in)
  16109. ("O" . org-clock-out)
  16110. ("Meta Data Editing")
  16111. ("t" . org-todo)
  16112. ("0" . (org-priority ?\ ))
  16113. ("1" . (org-priority ?A))
  16114. ("2" . (org-priority ?B))
  16115. ("3" . (org-priority ?C))
  16116. (";" . org-set-tags-command)
  16117. ("e" . org-set-effort)
  16118. ("Agenda Views etc")
  16119. ("v" . org-agenda)
  16120. ("/" . org-sparse-tree)
  16121. ("Misc")
  16122. ("o" . org-open-at-point)
  16123. ("?" . org-speed-command-help)
  16124. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16125. (">" . (org-agenda-remove-restriction-lock))
  16126. )
  16127. "The default speed commands.")
  16128. (defun org-print-speed-command (e)
  16129. (if (> (length (car e)) 1)
  16130. (progn
  16131. (princ "\n")
  16132. (princ (car e))
  16133. (princ "\n")
  16134. (princ (make-string (length (car e)) ?-))
  16135. (princ "\n"))
  16136. (princ (car e))
  16137. (princ " ")
  16138. (if (symbolp (cdr e))
  16139. (princ (symbol-name (cdr e)))
  16140. (prin1 (cdr e)))
  16141. (princ "\n")))
  16142. (defun org-speed-command-help ()
  16143. "Show the available speed commands."
  16144. (interactive)
  16145. (if (not org-use-speed-commands)
  16146. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16147. (with-output-to-temp-buffer "*Help*"
  16148. (princ "User-defined Speed commands\n===========================\n")
  16149. (mapc 'org-print-speed-command org-speed-commands-user)
  16150. (princ "\n")
  16151. (princ "Built-in Speed commands\n=======================\n")
  16152. (mapc 'org-print-speed-command org-speed-commands-default))
  16153. (with-current-buffer "*Help*"
  16154. (setq truncate-lines t))))
  16155. (defun org-speed-move-safe (cmd)
  16156. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16157. If not, return to the original position and throw an error."
  16158. (interactive)
  16159. (let ((pos (point)))
  16160. (call-interactively cmd)
  16161. (unless (and (bolp) (org-at-heading-p))
  16162. (goto-char pos)
  16163. (error "Boundary reached while executing %s" cmd))))
  16164. (defvar org-self-insert-command-undo-counter 0)
  16165. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16166. (defvar org-speed-command nil)
  16167. (defun org-speed-command-default-hook (keys)
  16168. "Hook for activating single-letter speed commands.
  16169. `org-speed-commands-default' specifies a minimal command set.
  16170. Use `org-speed-commands-user' for further customization."
  16171. (when (or (and (bolp) (looking-at org-outline-regexp))
  16172. (and (functionp org-use-speed-commands)
  16173. (funcall org-use-speed-commands)))
  16174. (cdr (assoc keys (append org-speed-commands-user
  16175. org-speed-commands-default)))))
  16176. (defun org-babel-speed-command-hook (keys)
  16177. "Hook for activating single-letter code block commands."
  16178. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16179. (cdr (assoc keys org-babel-key-bindings))))
  16180. (defcustom org-speed-command-hook
  16181. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16182. "Hook for activating speed commands at strategic locations.
  16183. Hook functions are called in sequence until a valid handler is
  16184. found.
  16185. Each hook takes a single argument, a user-pressed command key
  16186. which is also a `self-insert-command' from the global map.
  16187. Within the hook, examine the cursor position and the command key
  16188. and return nil or a valid handler as appropriate. Handler could
  16189. be one of an interactive command, a function, or a form.
  16190. Set `org-use-speed-commands' to non-nil value to enable this
  16191. hook. The default setting is `org-speed-command-default-hook'."
  16192. :group 'org-structure
  16193. :version "24.1"
  16194. :type 'hook)
  16195. (defun org-self-insert-command (N)
  16196. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16197. If the cursor is in a table looking at whitespace, the whitespace is
  16198. overwritten, and the table is not marked as requiring realignment."
  16199. (interactive "p")
  16200. (org-check-before-invisible-edit 'insert)
  16201. (cond
  16202. ((and org-use-speed-commands
  16203. (setq org-speed-command
  16204. (run-hook-with-args-until-success
  16205. 'org-speed-command-hook (this-command-keys))))
  16206. (cond
  16207. ((commandp org-speed-command)
  16208. (setq this-command org-speed-command)
  16209. (call-interactively org-speed-command))
  16210. ((functionp org-speed-command)
  16211. (funcall org-speed-command))
  16212. ((and org-speed-command (listp org-speed-command))
  16213. (eval org-speed-command))
  16214. (t (let (org-use-speed-commands)
  16215. (call-interactively 'org-self-insert-command)))))
  16216. ((and
  16217. (org-table-p)
  16218. (progn
  16219. ;; check if we blank the field, and if that triggers align
  16220. (and (featurep 'org-table) org-table-auto-blank-field
  16221. (member last-command
  16222. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16223. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16224. ;; got extra space, this field does not determine column width
  16225. (let (org-table-may-need-update) (org-table-blank-field))
  16226. ;; no extra space, this field may determine column width
  16227. (org-table-blank-field)))
  16228. t)
  16229. (eq N 1)
  16230. (looking-at "[^|\n]* |"))
  16231. (let (org-table-may-need-update)
  16232. (goto-char (1- (match-end 0)))
  16233. (delete-char -1)
  16234. (goto-char (match-beginning 0))
  16235. (self-insert-command N)))
  16236. (t
  16237. (setq org-table-may-need-update t)
  16238. (self-insert-command N)
  16239. (org-fix-tags-on-the-fly)
  16240. (if org-self-insert-cluster-for-undo
  16241. (if (not (eq last-command 'org-self-insert-command))
  16242. (setq org-self-insert-command-undo-counter 1)
  16243. (if (>= org-self-insert-command-undo-counter 20)
  16244. (setq org-self-insert-command-undo-counter 1)
  16245. (and (> org-self-insert-command-undo-counter 0)
  16246. buffer-undo-list (listp buffer-undo-list)
  16247. (not (cadr buffer-undo-list)) ; remove nil entry
  16248. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16249. (setq org-self-insert-command-undo-counter
  16250. (1+ org-self-insert-command-undo-counter))))))))
  16251. (defun org-check-before-invisible-edit (kind)
  16252. "Check is editing if kind KIND would be dangerous with invisible text around.
  16253. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16254. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16255. (if (and org-catch-invisible-edits
  16256. (or (not (boundp 'visible-mode)) (not visible-mode))
  16257. (or (get-char-property (point) 'invisible)
  16258. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16259. ;; OK, we need to take a closer look
  16260. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16261. (invisible-before-point (if (bobp) nil (get-char-property
  16262. (1- (point)) 'invisible)))
  16263. (border-and-ok-direction
  16264. (or
  16265. ;; Check if we are acting predictably before invisible text
  16266. (and invisible-at-point (not invisible-before-point)
  16267. (memq kind '(insert delete-backward)))
  16268. ;; Check if we are acting predictably after invisible text
  16269. ;; This works not well, and I have turned it off. It seems
  16270. ;; better to always show and stop after invisible text.
  16271. ;; (and (not invisible-at-point) invisible-before-point
  16272. ;; (memq kind '(insert delete)))
  16273. )))
  16274. (when (or (memq invisible-at-point '(outline org-hide-block))
  16275. (memq invisible-before-point '(outline org-hide-block)))
  16276. (if (eq org-catch-invisible-edits 'error)
  16277. (error "Editing in invisible areas is prohibited - make visible first"))
  16278. ;; Make the area visible
  16279. (save-excursion
  16280. (if invisible-before-point
  16281. (goto-char (previous-single-char-property-change
  16282. (point) 'invisible)))
  16283. (org-cycle))
  16284. (cond
  16285. ((eq org-catch-invisible-edits 'show)
  16286. ;; That's it, we do the edit after showing
  16287. (message
  16288. "Unfolding invisible region around point before editing")
  16289. (sit-for 1))
  16290. ((and (eq org-catch-invisible-edits 'smart)
  16291. border-and-ok-direction)
  16292. (message "Unfolding invisible region around point before editing"))
  16293. (t
  16294. ;; Don't do the edit, make the user repeat it in full visibility
  16295. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16296. (defun org-fix-tags-on-the-fly ()
  16297. (when (and (equal (char-after (point-at-bol)) ?*)
  16298. (org-at-heading-p))
  16299. (org-align-tags-here org-tags-column)))
  16300. (defun org-delete-backward-char (N)
  16301. "Like `delete-backward-char', insert whitespace at field end in tables.
  16302. When deleting backwards, in tables this function will insert whitespace in
  16303. front of the next \"|\" separator, to keep the table aligned. The table will
  16304. still be marked for re-alignment if the field did fill the entire column,
  16305. because, in this case the deletion might narrow the column."
  16306. (interactive "p")
  16307. (org-check-before-invisible-edit 'delete-backward)
  16308. (if (and (org-table-p)
  16309. (eq N 1)
  16310. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16311. (looking-at ".*?|"))
  16312. (let ((pos (point))
  16313. (noalign (looking-at "[^|\n\r]* |"))
  16314. (c org-table-may-need-update))
  16315. (backward-delete-char N)
  16316. (if (not overwrite-mode)
  16317. (progn
  16318. (skip-chars-forward "^|")
  16319. (insert " ")
  16320. (goto-char (1- pos))))
  16321. ;; noalign: if there were two spaces at the end, this field
  16322. ;; does not determine the width of the column.
  16323. (if noalign (setq org-table-may-need-update c)))
  16324. (backward-delete-char N)
  16325. (org-fix-tags-on-the-fly)))
  16326. (defun org-delete-char (N)
  16327. "Like `delete-char', but insert whitespace at field end in tables.
  16328. When deleting characters, in tables this function will insert whitespace in
  16329. front of the next \"|\" separator, to keep the table aligned. The table will
  16330. still be marked for re-alignment if the field did fill the entire column,
  16331. because, in this case the deletion might narrow the column."
  16332. (interactive "p")
  16333. (org-check-before-invisible-edit 'delete)
  16334. (if (and (org-table-p)
  16335. (not (bolp))
  16336. (not (= (char-after) ?|))
  16337. (eq N 1))
  16338. (if (looking-at ".*?|")
  16339. (let ((pos (point))
  16340. (noalign (looking-at "[^|\n\r]* |"))
  16341. (c org-table-may-need-update))
  16342. (replace-match (concat
  16343. (substring (match-string 0) 1 -1)
  16344. " |"))
  16345. (goto-char pos)
  16346. ;; noalign: if there were two spaces at the end, this field
  16347. ;; does not determine the width of the column.
  16348. (if noalign (setq org-table-may-need-update c)))
  16349. (delete-char N))
  16350. (delete-char N)
  16351. (org-fix-tags-on-the-fly)))
  16352. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16353. (put 'org-self-insert-command 'delete-selection t)
  16354. (put 'orgtbl-self-insert-command 'delete-selection t)
  16355. (put 'org-delete-char 'delete-selection 'supersede)
  16356. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16357. (put 'org-yank 'delete-selection 'yank)
  16358. ;; Make `flyspell-mode' delay after some commands
  16359. (put 'org-self-insert-command 'flyspell-delayed t)
  16360. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16361. (put 'org-delete-char 'flyspell-delayed t)
  16362. (put 'org-delete-backward-char 'flyspell-delayed t)
  16363. ;; Make pabbrev-mode expand after org-mode commands
  16364. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16365. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16366. ;; How to do this: Measure non-white length of current string
  16367. ;; If equal to column width, we should realign.
  16368. (defun org-remap (map &rest commands)
  16369. "In MAP, remap the functions given in COMMANDS.
  16370. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16371. (let (new old)
  16372. (while commands
  16373. (setq old (pop commands) new (pop commands))
  16374. (if (fboundp 'command-remapping)
  16375. (org-defkey map (vector 'remap old) new)
  16376. (substitute-key-definition old new map global-map)))))
  16377. (when (eq org-enable-table-editor 'optimized)
  16378. ;; If the user wants maximum table support, we need to hijack
  16379. ;; some standard editing functions
  16380. (org-remap org-mode-map
  16381. 'self-insert-command 'org-self-insert-command
  16382. 'delete-char 'org-delete-char
  16383. 'delete-backward-char 'org-delete-backward-char)
  16384. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16385. (defvar org-ctrl-c-ctrl-c-hook nil
  16386. "Hook for functions attaching themselves to `C-c C-c'.
  16387. This can be used to add additional functionality to the C-c C-c
  16388. key which executes context-dependent commands. This hook is run
  16389. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16390. run after the last test.
  16391. Each function will be called with no arguments. The function
  16392. must check if the context is appropriate for it to act. If yes,
  16393. it should do its thing and then return a non-nil value. If the
  16394. context is wrong, just do nothing and return nil.")
  16395. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16396. "Hook for functions attaching themselves to `C-c C-c'.
  16397. This can be used to add additional functionality to the C-c C-c
  16398. key which executes context-dependent commands. This hook is run
  16399. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16400. before the first test.
  16401. Each function will be called with no arguments. The function
  16402. must check if the context is appropriate for it to act. If yes,
  16403. it should do its thing and then return a non-nil value. If the
  16404. context is wrong, just do nothing and return nil.")
  16405. (defvar org-tab-first-hook nil
  16406. "Hook for functions to attach themselves to TAB.
  16407. See `org-ctrl-c-ctrl-c-hook' for more information.
  16408. This hook runs as the first action when TAB is pressed, even before
  16409. `org-cycle' messes around with the `outline-regexp' to cater for
  16410. inline tasks and plain list item folding.
  16411. If any function in this hook returns t, any other actions that
  16412. would have been caused by TAB (such as table field motion or visibility
  16413. cycling) will not occur.")
  16414. (defvar org-tab-after-check-for-table-hook nil
  16415. "Hook for functions to attach themselves to TAB.
  16416. See `org-ctrl-c-ctrl-c-hook' for more information.
  16417. This hook runs after it has been established that the cursor is not in a
  16418. table, but before checking if the cursor is in a headline or if global cycling
  16419. should be done.
  16420. If any function in this hook returns t, not other actions like visibility
  16421. cycling will be done.")
  16422. (defvar org-tab-after-check-for-cycling-hook nil
  16423. "Hook for functions to attach themselves to TAB.
  16424. See `org-ctrl-c-ctrl-c-hook' for more information.
  16425. This hook runs after it has been established that not table field motion and
  16426. not visibility should be done because of current context. This is probably
  16427. the place where a package like yasnippets can hook in.")
  16428. (defvar org-tab-before-tab-emulation-hook nil
  16429. "Hook for functions to attach themselves to TAB.
  16430. See `org-ctrl-c-ctrl-c-hook' for more information.
  16431. This hook runs after every other options for TAB have been exhausted, but
  16432. before indentation and \t insertion takes place.")
  16433. (defvar org-metaleft-hook nil
  16434. "Hook for functions attaching themselves to `M-left'.
  16435. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16436. (defvar org-metaright-hook nil
  16437. "Hook for functions attaching themselves to `M-right'.
  16438. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16439. (defvar org-metaup-hook nil
  16440. "Hook for functions attaching themselves to `M-up'.
  16441. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16442. (defvar org-metadown-hook nil
  16443. "Hook for functions attaching themselves to `M-down'.
  16444. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16445. (defvar org-shiftmetaleft-hook nil
  16446. "Hook for functions attaching themselves to `M-S-left'.
  16447. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16448. (defvar org-shiftmetaright-hook nil
  16449. "Hook for functions attaching themselves to `M-S-right'.
  16450. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16451. (defvar org-shiftmetaup-hook nil
  16452. "Hook for functions attaching themselves to `M-S-up'.
  16453. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16454. (defvar org-shiftmetadown-hook nil
  16455. "Hook for functions attaching themselves to `M-S-down'.
  16456. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16457. (defvar org-metareturn-hook nil
  16458. "Hook for functions attaching themselves to `M-RET'.
  16459. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16460. (defvar org-shiftup-hook nil
  16461. "Hook for functions attaching themselves to `S-up'.
  16462. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16463. (defvar org-shiftup-final-hook nil
  16464. "Hook for functions attaching themselves to `S-up'.
  16465. This one runs after all other options except shift-select have been excluded.
  16466. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16467. (defvar org-shiftdown-hook nil
  16468. "Hook for functions attaching themselves to `S-down'.
  16469. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16470. (defvar org-shiftdown-final-hook nil
  16471. "Hook for functions attaching themselves to `S-down'.
  16472. This one runs after all other options except shift-select have been excluded.
  16473. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16474. (defvar org-shiftleft-hook nil
  16475. "Hook for functions attaching themselves to `S-left'.
  16476. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16477. (defvar org-shiftleft-final-hook nil
  16478. "Hook for functions attaching themselves to `S-left'.
  16479. This one runs after all other options except shift-select have been excluded.
  16480. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16481. (defvar org-shiftright-hook nil
  16482. "Hook for functions attaching themselves to `S-right'.
  16483. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16484. (defvar org-shiftright-final-hook nil
  16485. "Hook for functions attaching themselves to `S-right'.
  16486. This one runs after all other options except shift-select have been excluded.
  16487. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16488. (defun org-modifier-cursor-error ()
  16489. "Throw an error, a modified cursor command was applied in wrong context."
  16490. (error "This command is active in special context like tables, headlines or items"))
  16491. (defun org-shiftselect-error ()
  16492. "Throw an error because Shift-Cursor command was applied in wrong context."
  16493. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16494. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16495. (error "This command works only in special context like headlines or timestamps")))
  16496. (defun org-call-for-shift-select (cmd)
  16497. (let ((this-command-keys-shift-translated t))
  16498. (call-interactively cmd)))
  16499. (defun org-shifttab (&optional arg)
  16500. "Global visibility cycling or move to previous table field.
  16501. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16502. on context.
  16503. See the individual commands for more information."
  16504. (interactive "P")
  16505. (cond
  16506. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16507. ((integerp arg)
  16508. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16509. (message "Content view to level: %d" arg)
  16510. (org-content (prefix-numeric-value arg2))
  16511. (setq org-cycle-global-status 'overview)))
  16512. (t (call-interactively 'org-global-cycle))))
  16513. (defun org-shiftmetaleft ()
  16514. "Promote subtree or delete table column.
  16515. Calls `org-promote-subtree', `org-outdent-item',
  16516. or `org-table-delete-column', depending on context.
  16517. See the individual commands for more information."
  16518. (interactive)
  16519. (cond
  16520. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16521. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16522. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16523. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16524. (t (org-modifier-cursor-error))))
  16525. (defun org-shiftmetaright ()
  16526. "Demote subtree or insert table column.
  16527. Calls `org-demote-subtree', `org-indent-item',
  16528. or `org-table-insert-column', depending on context.
  16529. See the individual commands for more information."
  16530. (interactive)
  16531. (cond
  16532. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16533. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16534. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16535. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16536. (t (org-modifier-cursor-error))))
  16537. (defun org-shiftmetaup (&optional arg)
  16538. "Move subtree up or kill table row.
  16539. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16540. `org-move-item-up' depending on context. See the individual commands
  16541. for more information."
  16542. (interactive "P")
  16543. (cond
  16544. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16545. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16546. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16547. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16548. (t (org-modifier-cursor-error))))
  16549. (defun org-shiftmetadown (&optional arg)
  16550. "Move subtree down or insert table row.
  16551. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16552. `org-move-item-down', depending on context. See the individual
  16553. commands for more information."
  16554. (interactive "P")
  16555. (cond
  16556. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16557. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16558. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16559. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16560. (t (org-modifier-cursor-error))))
  16561. (defsubst org-hidden-tree-error ()
  16562. (error
  16563. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16564. (defun org-metaleft (&optional arg)
  16565. "Promote heading or move table column to left.
  16566. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16567. With no specific context, calls the Emacs default `backward-word'.
  16568. See the individual commands for more information."
  16569. (interactive "P")
  16570. (cond
  16571. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16572. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16573. ((org-with-limited-levels
  16574. (or (org-at-heading-p)
  16575. (and (org-region-active-p)
  16576. (save-excursion
  16577. (goto-char (region-beginning))
  16578. (org-at-heading-p)))))
  16579. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16580. (call-interactively 'org-do-promote))
  16581. ;; At an inline task.
  16582. ((org-at-heading-p)
  16583. (call-interactively 'org-inlinetask-promote))
  16584. ((or (org-at-item-p)
  16585. (and (org-region-active-p)
  16586. (save-excursion
  16587. (goto-char (region-beginning))
  16588. (org-at-item-p))))
  16589. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16590. (call-interactively 'org-outdent-item))
  16591. (t (call-interactively 'backward-word))))
  16592. (defun org-metaright (&optional arg)
  16593. "Demote a subtree, a list item or move table column to right.
  16594. In front of a drawer or a block keyword, indent it correctly.
  16595. With no specific context, calls the Emacs default `forward-word'.
  16596. See the individual commands for more information."
  16597. (interactive "P")
  16598. (cond
  16599. ((run-hook-with-args-until-success 'org-metaright-hook))
  16600. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16601. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16602. ((org-at-block-p) (call-interactively 'org-indent-block))
  16603. ((org-with-limited-levels
  16604. (or (org-at-heading-p)
  16605. (and (org-region-active-p)
  16606. (save-excursion
  16607. (goto-char (region-beginning))
  16608. (org-at-heading-p)))))
  16609. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16610. (call-interactively 'org-do-demote))
  16611. ;; At an inline task.
  16612. ((org-at-heading-p)
  16613. (call-interactively 'org-inlinetask-demote))
  16614. ((or (org-at-item-p)
  16615. (and (org-region-active-p)
  16616. (save-excursion
  16617. (goto-char (region-beginning))
  16618. (org-at-item-p))))
  16619. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16620. (call-interactively 'org-indent-item))
  16621. (t (call-interactively 'forward-word))))
  16622. (defun org-check-for-hidden (what)
  16623. "Check if there are hidden headlines/items in the current visual line.
  16624. WHAT can be either `headlines' or `items'. If the current line is
  16625. an outline or item heading and it has a folded subtree below it,
  16626. this function returns t, nil otherwise."
  16627. (let ((re (cond
  16628. ((eq what 'headlines) org-outline-regexp-bol)
  16629. ((eq what 'items) (org-item-beginning-re))
  16630. (t (error "This should not happen"))))
  16631. beg end)
  16632. (save-excursion
  16633. (catch 'exit
  16634. (unless (org-region-active-p)
  16635. (setq beg (point-at-bol))
  16636. (beginning-of-line 2)
  16637. (while (and (not (eobp)) ;; this is like `next-line'
  16638. (get-char-property (1- (point)) 'invisible))
  16639. (beginning-of-line 2))
  16640. (setq end (point))
  16641. (goto-char beg)
  16642. (goto-char (point-at-eol))
  16643. (setq end (max end (point)))
  16644. (while (re-search-forward re end t)
  16645. (if (get-char-property (match-beginning 0) 'invisible)
  16646. (throw 'exit t))))
  16647. nil))))
  16648. (defun org-metaup (&optional arg)
  16649. "Move subtree up or move table row up.
  16650. Calls `org-move-subtree-up' or `org-table-move-row' or
  16651. `org-move-item-up', depending on context. See the individual commands
  16652. for more information."
  16653. (interactive "P")
  16654. (cond
  16655. ((run-hook-with-args-until-success 'org-metaup-hook))
  16656. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16657. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16658. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16659. (t (transpose-lines 1) (beginning-of-line -1))))
  16660. (defun org-metadown (&optional arg)
  16661. "Move subtree down or move table row down.
  16662. Calls `org-move-subtree-down' or `org-table-move-row' or
  16663. `org-move-item-down', depending on context. See the individual
  16664. commands for more information."
  16665. (interactive "P")
  16666. (cond
  16667. ((run-hook-with-args-until-success 'org-metadown-hook))
  16668. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16669. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16670. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16671. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16672. (defun org-shiftup (&optional arg)
  16673. "Increase item in timestamp or increase priority of current headline.
  16674. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16675. depending on context. See the individual commands for more information."
  16676. (interactive "P")
  16677. (cond
  16678. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16679. ((and org-support-shift-select (org-region-active-p))
  16680. (org-call-for-shift-select 'previous-line))
  16681. ((org-at-timestamp-p t)
  16682. (call-interactively (if org-edit-timestamp-down-means-later
  16683. 'org-timestamp-down 'org-timestamp-up)))
  16684. ((and (not (eq org-support-shift-select 'always))
  16685. org-enable-priority-commands
  16686. (org-at-heading-p))
  16687. (call-interactively 'org-priority-up))
  16688. ((and (not org-support-shift-select) (org-at-item-p))
  16689. (call-interactively 'org-previous-item))
  16690. ((org-clocktable-try-shift 'up arg))
  16691. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16692. (org-support-shift-select
  16693. (org-call-for-shift-select 'previous-line))
  16694. (t (org-shiftselect-error))))
  16695. (defun org-shiftdown (&optional arg)
  16696. "Decrease item in timestamp or decrease priority of current headline.
  16697. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16698. depending on context. See the individual commands for more information."
  16699. (interactive "P")
  16700. (cond
  16701. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16702. ((and org-support-shift-select (org-region-active-p))
  16703. (org-call-for-shift-select 'next-line))
  16704. ((org-at-timestamp-p t)
  16705. (call-interactively (if org-edit-timestamp-down-means-later
  16706. 'org-timestamp-up 'org-timestamp-down)))
  16707. ((and (not (eq org-support-shift-select 'always))
  16708. org-enable-priority-commands
  16709. (org-at-heading-p))
  16710. (call-interactively 'org-priority-down))
  16711. ((and (not org-support-shift-select) (org-at-item-p))
  16712. (call-interactively 'org-next-item))
  16713. ((org-clocktable-try-shift 'down arg))
  16714. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16715. (org-support-shift-select
  16716. (org-call-for-shift-select 'next-line))
  16717. (t (org-shiftselect-error))))
  16718. (defun org-shiftright (&optional arg)
  16719. "Cycle the thing at point or in the current line, depending on context.
  16720. Depending on context, this does one of the following:
  16721. - switch a timestamp at point one day into the future
  16722. - on a headline, switch to the next TODO keyword.
  16723. - on an item, switch entire list to the next bullet type
  16724. - on a property line, switch to the next allowed value
  16725. - on a clocktable definition line, move time block into the future"
  16726. (interactive "P")
  16727. (cond
  16728. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16729. ((and org-support-shift-select (org-region-active-p))
  16730. (org-call-for-shift-select 'forward-char))
  16731. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16732. ((and (not (eq org-support-shift-select 'always))
  16733. (org-at-heading-p))
  16734. (let ((org-inhibit-logging
  16735. (not org-treat-S-cursor-todo-selection-as-state-change))
  16736. (org-inhibit-blocking
  16737. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16738. (org-call-with-arg 'org-todo 'right)))
  16739. ((or (and org-support-shift-select
  16740. (not (eq org-support-shift-select 'always))
  16741. (org-at-item-bullet-p))
  16742. (and (not org-support-shift-select) (org-at-item-p)))
  16743. (org-call-with-arg 'org-cycle-list-bullet nil))
  16744. ((and (not (eq org-support-shift-select 'always))
  16745. (org-at-property-p))
  16746. (call-interactively 'org-property-next-allowed-value))
  16747. ((org-clocktable-try-shift 'right arg))
  16748. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16749. (org-support-shift-select
  16750. (org-call-for-shift-select 'forward-char))
  16751. (t (org-shiftselect-error))))
  16752. (defun org-shiftleft (&optional arg)
  16753. "Cycle the thing at point or in the current line, depending on context.
  16754. Depending on context, this does one of the following:
  16755. - switch a timestamp at point one day into the past
  16756. - on a headline, switch to the previous TODO keyword.
  16757. - on an item, switch entire list to the previous bullet type
  16758. - on a property line, switch to the previous allowed value
  16759. - on a clocktable definition line, move time block into the past"
  16760. (interactive "P")
  16761. (cond
  16762. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16763. ((and org-support-shift-select (org-region-active-p))
  16764. (org-call-for-shift-select 'backward-char))
  16765. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16766. ((and (not (eq org-support-shift-select 'always))
  16767. (org-at-heading-p))
  16768. (let ((org-inhibit-logging
  16769. (not org-treat-S-cursor-todo-selection-as-state-change))
  16770. (org-inhibit-blocking
  16771. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16772. (org-call-with-arg 'org-todo 'left)))
  16773. ((or (and org-support-shift-select
  16774. (not (eq org-support-shift-select 'always))
  16775. (org-at-item-bullet-p))
  16776. (and (not org-support-shift-select) (org-at-item-p)))
  16777. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16778. ((and (not (eq org-support-shift-select 'always))
  16779. (org-at-property-p))
  16780. (call-interactively 'org-property-previous-allowed-value))
  16781. ((org-clocktable-try-shift 'left arg))
  16782. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16783. (org-support-shift-select
  16784. (org-call-for-shift-select 'backward-char))
  16785. (t (org-shiftselect-error))))
  16786. (defun org-shiftcontrolright ()
  16787. "Switch to next TODO set."
  16788. (interactive)
  16789. (cond
  16790. ((and org-support-shift-select (org-region-active-p))
  16791. (org-call-for-shift-select 'forward-word))
  16792. ((and (not (eq org-support-shift-select 'always))
  16793. (org-at-heading-p))
  16794. (org-call-with-arg 'org-todo 'nextset))
  16795. (org-support-shift-select
  16796. (org-call-for-shift-select 'forward-word))
  16797. (t (org-shiftselect-error))))
  16798. (defun org-shiftcontrolleft ()
  16799. "Switch to previous TODO set."
  16800. (interactive)
  16801. (cond
  16802. ((and org-support-shift-select (org-region-active-p))
  16803. (org-call-for-shift-select 'backward-word))
  16804. ((and (not (eq org-support-shift-select 'always))
  16805. (org-at-heading-p))
  16806. (org-call-with-arg 'org-todo 'previousset))
  16807. (org-support-shift-select
  16808. (org-call-for-shift-select 'backward-word))
  16809. (t (org-shiftselect-error))))
  16810. (defun org-shiftcontrolup ()
  16811. "Change timestamps synchronously up in CLOCK log lines."
  16812. (interactive)
  16813. (cond ((and (not org-support-shift-select)
  16814. (org-at-clock-log-p)
  16815. (org-at-timestamp-p t))
  16816. (org-clock-timestamps-up))
  16817. (t (org-shiftselect-error))))
  16818. (defun org-shiftcontroldown ()
  16819. "Change timestamps synchronously down in CLOCK log lines."
  16820. (interactive)
  16821. (cond ((and (not org-support-shift-select)
  16822. (org-at-clock-log-p)
  16823. (org-at-timestamp-p t))
  16824. (org-clock-timestamps-down))
  16825. (t (org-shiftselect-error))))
  16826. (defun org-ctrl-c-ret ()
  16827. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16828. (interactive)
  16829. (cond
  16830. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16831. (t (call-interactively 'org-insert-heading))))
  16832. (defun org-find-visible ()
  16833. (let ((s (point)))
  16834. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16835. (get-char-property s 'invisible)))
  16836. s))
  16837. (defun org-find-invisible ()
  16838. (let ((s (point)))
  16839. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16840. (not (get-char-property s 'invisible))))
  16841. s))
  16842. (defun org-copy-visible (beg end)
  16843. "Copy the visible parts of the region."
  16844. (interactive "r")
  16845. (let (snippets s)
  16846. (save-excursion
  16847. (save-restriction
  16848. (narrow-to-region beg end)
  16849. (setq s (goto-char (point-min)))
  16850. (while (not (= (point) (point-max)))
  16851. (goto-char (org-find-invisible))
  16852. (push (buffer-substring s (point)) snippets)
  16853. (setq s (goto-char (org-find-visible))))))
  16854. (kill-new (apply 'concat (nreverse snippets)))))
  16855. (defun org-copy-special ()
  16856. "Copy region in table or copy current subtree.
  16857. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16858. See the individual commands for more information."
  16859. (interactive)
  16860. (call-interactively
  16861. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16862. (defun org-cut-special ()
  16863. "Cut region in table or cut current subtree.
  16864. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16865. See the individual commands for more information."
  16866. (interactive)
  16867. (call-interactively
  16868. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16869. (defun org-paste-special (arg)
  16870. "Paste rectangular region into table, or past subtree relative to level.
  16871. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16872. See the individual commands for more information."
  16873. (interactive "P")
  16874. (if (org-at-table-p)
  16875. (org-table-paste-rectangle)
  16876. (org-paste-subtree arg)))
  16877. (defun org-edit-special (&optional arg)
  16878. "Call a special editor for the stuff at point.
  16879. When at a table, call the formula editor with `org-table-edit-formulas'.
  16880. When at the first line of an src example, call `org-edit-src-code'.
  16881. When in an #+include line, visit the include file. Otherwise call
  16882. `ffap' to visit the file at point."
  16883. (interactive)
  16884. ;; possibly prep session before editing source
  16885. (when arg
  16886. (let* ((info (org-babel-get-src-block-info))
  16887. (lang (nth 0 info))
  16888. (params (nth 2 info))
  16889. (session (cdr (assoc :session params))))
  16890. (when (and info session) ;; we are in a source-code block with a session
  16891. (funcall
  16892. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16893. (cond ;; proceed with `org-edit-special'
  16894. ((save-excursion
  16895. (beginning-of-line 1)
  16896. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16897. (find-file (org-trim (match-string 1))))
  16898. ((org-edit-src-code))
  16899. ((org-edit-fixed-width-region))
  16900. ((org-at-table.el-p)
  16901. (org-edit-src-code))
  16902. ((or (org-at-table-p)
  16903. (save-excursion
  16904. (beginning-of-line 1)
  16905. (looking-at "[ \t]*#\\+TBLFM:")))
  16906. (call-interactively 'org-table-edit-formulas))
  16907. (t (call-interactively 'ffap))))
  16908. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16909. (defun org-ctrl-c-ctrl-c (&optional arg)
  16910. "Set tags in headline, or update according to changed information at point.
  16911. This command does many different things, depending on context:
  16912. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16913. this is what we do.
  16914. - If the cursor is on a statistics cookie, update it.
  16915. - If the cursor is in a headline, prompt for tags and insert them
  16916. into the current line, aligned to `org-tags-column'. When called
  16917. with prefix arg, realign all tags in the current buffer.
  16918. - If the cursor is in one of the special #+KEYWORD lines, this
  16919. triggers scanning the buffer for these lines and updating the
  16920. information.
  16921. - If the cursor is inside a table, realign the table. This command
  16922. works even if the automatic table editor has been turned off.
  16923. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16924. the entire table.
  16925. - If the cursor is at a footnote reference or definition, jump to
  16926. the corresponding definition or references, respectively.
  16927. - If the cursor is a the beginning of a dynamic block, update it.
  16928. - If the current buffer is a capture buffer, close note and file it.
  16929. - If the cursor is on a <<<target>>>, update radio targets and
  16930. corresponding links in this buffer.
  16931. - If the cursor is on a numbered item in a plain list, renumber the
  16932. ordered list.
  16933. - If the cursor is on a checkbox, toggle it.
  16934. - If the cursor is on a code block, evaluate it. The variable
  16935. `org-confirm-babel-evaluate' can be used to control prompting
  16936. before code block evaluation, by default every code block
  16937. evaluation requires confirmation. Code block evaluation can be
  16938. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16939. (interactive "P")
  16940. (let ((org-enable-table-editor t))
  16941. (cond
  16942. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16943. org-occur-highlights
  16944. org-latex-fragment-image-overlays)
  16945. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16946. (org-remove-occur-highlights)
  16947. (org-remove-latex-fragment-image-overlays)
  16948. (message "Temporary highlights/overlays removed from current buffer"))
  16949. ((and (local-variable-p 'org-finish-function (current-buffer))
  16950. (fboundp org-finish-function))
  16951. (funcall org-finish-function))
  16952. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16953. ((org-in-regexp org-ts-regexp-both)
  16954. (org-timestamp-change 0 'day))
  16955. ((or (looking-at org-property-start-re)
  16956. (org-at-property-p))
  16957. (call-interactively 'org-property-action))
  16958. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  16959. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16960. (or (org-at-heading-p) (org-at-item-p)))
  16961. (call-interactively 'org-update-statistics-cookies))
  16962. ((org-at-heading-p) (call-interactively 'org-set-tags))
  16963. ((org-at-table.el-p)
  16964. (message "Use C-c ' to edit table.el tables"))
  16965. ((org-at-table-p)
  16966. (org-table-maybe-eval-formula)
  16967. (if arg
  16968. (call-interactively 'org-table-recalculate)
  16969. (org-table-maybe-recalculate-line))
  16970. (call-interactively 'org-table-align)
  16971. (orgtbl-send-table 'maybe))
  16972. ((or (org-footnote-at-reference-p)
  16973. (org-footnote-at-definition-p))
  16974. (call-interactively 'org-footnote-action))
  16975. ((org-at-item-checkbox-p)
  16976. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16977. ;; list only if at top item.
  16978. (let* ((cbox (match-string 1))
  16979. (struct (org-list-struct))
  16980. (old-struct (copy-tree struct))
  16981. (parents (org-list-parents-alist struct))
  16982. (orderedp (org-entry-get nil "ORDERED"))
  16983. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16984. block-item)
  16985. ;; Use a light version of `org-toggle-checkbox' to avoid
  16986. ;; computing list structure twice.
  16987. (let ((new-box (cond
  16988. ((equal arg '(16)) "[-]")
  16989. ((equal arg '(4)) nil)
  16990. ((equal "[X]" cbox) "[ ]")
  16991. (t "[X]"))))
  16992. (if (and firstp arg)
  16993. ;; If at first item of sub-list, remove check-box from
  16994. ;; every item at the same level.
  16995. (mapc
  16996. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  16997. (org-list-get-all-items
  16998. (point-at-bol) struct (org-list-prevs-alist struct)))
  16999. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17000. ;; Replicate `org-list-write-struct', while grabbing a return
  17001. ;; value from `org-list-struct-fix-box'.
  17002. (org-list-struct-fix-ind struct parents 2)
  17003. (org-list-struct-fix-item-end struct)
  17004. (let ((prevs (org-list-prevs-alist struct)))
  17005. (org-list-struct-fix-bul struct prevs)
  17006. (org-list-struct-fix-ind struct parents)
  17007. (setq block-item
  17008. (org-list-struct-fix-box struct parents prevs orderedp)))
  17009. (org-list-struct-apply-struct struct old-struct)
  17010. (org-update-checkbox-count-maybe)
  17011. (when block-item
  17012. (message
  17013. "Checkboxes were removed due to unchecked box at line %d"
  17014. (org-current-line block-item)))
  17015. (when firstp (org-list-send-list 'maybe))))
  17016. ((org-at-item-p)
  17017. ;; Cursor at an item: repair list. Do checkbox related actions
  17018. ;; only if function was called with an argument. Send list only
  17019. ;; if at top item.
  17020. (let* ((struct (org-list-struct))
  17021. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17022. old-struct)
  17023. (when arg
  17024. (setq old-struct (copy-tree struct))
  17025. (if firstp
  17026. ;; If at first item of sub-list, add check-box to every
  17027. ;; item at the same level.
  17028. (mapc
  17029. (lambda (pos)
  17030. (unless (org-list-get-checkbox pos struct)
  17031. (org-list-set-checkbox pos struct "[ ]")))
  17032. (org-list-get-all-items
  17033. (point-at-bol) struct (org-list-prevs-alist struct)))
  17034. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17035. (org-list-write-struct
  17036. struct (org-list-parents-alist struct) old-struct)
  17037. (when arg (org-update-checkbox-count-maybe))
  17038. (when firstp (org-list-send-list 'maybe))))
  17039. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17040. ;; Dynamic block
  17041. (beginning-of-line 1)
  17042. (save-excursion (org-update-dblock)))
  17043. ((save-excursion
  17044. (beginning-of-line 1)
  17045. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  17046. (cond
  17047. ((equal (match-string 1) "TBLFM")
  17048. ;; Recalculate the table before this line
  17049. (save-excursion
  17050. (beginning-of-line 1)
  17051. (skip-chars-backward " \r\n\t")
  17052. (if (org-at-table-p)
  17053. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17054. (t
  17055. (let ((org-inhibit-startup-visibility-stuff t)
  17056. (org-startup-align-all-tables nil))
  17057. (when (boundp 'org-table-coordinate-overlays)
  17058. (mapc 'delete-overlay org-table-coordinate-overlays)
  17059. (setq org-table-coordinate-overlays nil))
  17060. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17061. (message "Local setup has been refreshed"))))
  17062. ((org-clock-update-time-maybe))
  17063. (t
  17064. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17065. (error "C-c C-c can do nothing useful at this location"))))))
  17066. (defun org-mode-restart ()
  17067. "Restart Org-mode, to scan again for special lines.
  17068. Also updates the keyword regular expressions."
  17069. (interactive)
  17070. (org-mode)
  17071. (message "Org-mode restarted"))
  17072. (defun org-kill-note-or-show-branches ()
  17073. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17074. (interactive)
  17075. (if (not org-finish-function)
  17076. (progn
  17077. (hide-subtree)
  17078. (call-interactively 'show-branches))
  17079. (let ((org-note-abort t))
  17080. (funcall org-finish-function))))
  17081. (defun org-return (&optional indent)
  17082. "Goto next table row or insert a newline.
  17083. Calls `org-table-next-row' or `newline', depending on context.
  17084. See the individual commands for more information."
  17085. (interactive)
  17086. (cond
  17087. ((or (bobp) (org-in-src-block-p))
  17088. (if indent (newline-and-indent) (newline)))
  17089. ((org-at-table-p)
  17090. (org-table-justify-field-maybe)
  17091. (call-interactively 'org-table-next-row))
  17092. ;; when `newline-and-indent' is called within a list, make sure
  17093. ;; text moved stays inside the item.
  17094. ((and (org-in-item-p) indent)
  17095. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17096. (progn
  17097. (save-match-data (newline))
  17098. (org-indent-line-to (length (match-string 0))))
  17099. (let ((ind (org-get-indentation)))
  17100. (newline)
  17101. (if (org-looking-back org-list-end-re)
  17102. (org-indent-line-function)
  17103. (org-indent-line-to ind)))))
  17104. ((and org-return-follows-link
  17105. (let ((tprop (get-text-property (point) 'face)))
  17106. (or (eq tprop 'org-link)
  17107. (and (listp tprop) (memq 'org-link tprop)))))
  17108. (call-interactively 'org-open-at-point))
  17109. ((and (org-at-heading-p)
  17110. (looking-at
  17111. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17112. (org-show-entry)
  17113. (end-of-line 1)
  17114. (newline))
  17115. (t (if indent (newline-and-indent) (newline)))))
  17116. (defun org-return-indent ()
  17117. "Goto next table row or insert a newline and indent.
  17118. Calls `org-table-next-row' or `newline-and-indent', depending on
  17119. context. See the individual commands for more information."
  17120. (interactive)
  17121. (org-return t))
  17122. (defun org-ctrl-c-star ()
  17123. "Compute table, or change heading status of lines.
  17124. Calls `org-table-recalculate' or `org-toggle-heading',
  17125. depending on context."
  17126. (interactive)
  17127. (cond
  17128. ((org-at-table-p)
  17129. (call-interactively 'org-table-recalculate))
  17130. (t
  17131. ;; Convert all lines in region to list items
  17132. (call-interactively 'org-toggle-heading))))
  17133. (defun org-ctrl-c-minus ()
  17134. "Insert separator line in table or modify bullet status of line.
  17135. Also turns a plain line or a region of lines into list items.
  17136. Calls `org-table-insert-hline', `org-toggle-item', or
  17137. `org-cycle-list-bullet', depending on context."
  17138. (interactive)
  17139. (cond
  17140. ((org-at-table-p)
  17141. (call-interactively 'org-table-insert-hline))
  17142. ((org-region-active-p)
  17143. (call-interactively 'org-toggle-item))
  17144. ((org-in-item-p)
  17145. (call-interactively 'org-cycle-list-bullet))
  17146. (t
  17147. (call-interactively 'org-toggle-item))))
  17148. (defun org-toggle-item (arg)
  17149. "Convert headings or normal lines to items, items to normal lines.
  17150. If there is no active region, only the current line is considered.
  17151. If the first non blank line in the region is an headline, convert
  17152. all headlines to items, shifting text accordingly.
  17153. If it is an item, convert all items to normal lines.
  17154. If it is normal text, change region into an item. With a prefix
  17155. argument ARG, change each line in region into an item."
  17156. (interactive "P")
  17157. (let ((shift-text
  17158. (function
  17159. ;; Shift text in current section to IND, from point to END.
  17160. ;; The function leaves point to END line.
  17161. (lambda (ind end)
  17162. (let ((min-i 1000) (end (copy-marker end)))
  17163. ;; First determine the minimum indentation (MIN-I) of
  17164. ;; the text.
  17165. (save-excursion
  17166. (catch 'exit
  17167. (while (< (point) end)
  17168. (let ((i (org-get-indentation)))
  17169. (cond
  17170. ;; Skip blank lines and inline tasks.
  17171. ((looking-at "^[ \t]*$"))
  17172. ((looking-at org-outline-regexp-bol))
  17173. ;; We can't find less than 0 indentation.
  17174. ((zerop i) (throw 'exit (setq min-i 0)))
  17175. ((< i min-i) (setq min-i i))))
  17176. (forward-line))))
  17177. ;; Then indent each line so that a line indented to
  17178. ;; MIN-I becomes indented to IND. Ignore blank lines
  17179. ;; and inline tasks in the process.
  17180. (let ((delta (- ind min-i)))
  17181. (while (< (point) end)
  17182. (unless (or (looking-at "^[ \t]*$")
  17183. (looking-at org-outline-regexp-bol))
  17184. (org-indent-line-to (+ (org-get-indentation) delta)))
  17185. (forward-line)))))))
  17186. (skip-blanks
  17187. (function
  17188. ;; Return beginning of first non-blank line, starting from
  17189. ;; line at POS.
  17190. (lambda (pos)
  17191. (save-excursion
  17192. (goto-char pos)
  17193. (skip-chars-forward " \r\t\n")
  17194. (point-at-bol)))))
  17195. beg end)
  17196. ;; Determine boundaries of changes.
  17197. (if (org-region-active-p)
  17198. (setq beg (funcall skip-blanks (region-beginning))
  17199. end (copy-marker (region-end)))
  17200. (setq beg (funcall skip-blanks (point-at-bol))
  17201. end (copy-marker (point-at-eol))))
  17202. ;; Depending on the starting line, choose an action on the text
  17203. ;; between BEG and END.
  17204. (org-with-limited-levels
  17205. (save-excursion
  17206. (goto-char beg)
  17207. (cond
  17208. ;; Case 1. Start at an item: de-itemize. Note that it only
  17209. ;; happens when a region is active: `org-ctrl-c-minus'
  17210. ;; would call `org-cycle-list-bullet' otherwise.
  17211. ((org-at-item-p)
  17212. (while (< (point) end)
  17213. (when (org-at-item-p)
  17214. (skip-chars-forward " \t")
  17215. (delete-region (point) (match-end 0)))
  17216. (forward-line)))
  17217. ;; Case 2. Start at an heading: convert to items.
  17218. ((org-at-heading-p)
  17219. (let* ((bul (org-list-bullet-string "-"))
  17220. (bul-len (length bul))
  17221. ;; Indentation of the first heading. It should be
  17222. ;; relative to the indentation of its parent, if any.
  17223. (start-ind (save-excursion
  17224. (cond
  17225. ((not org-adapt-indentation) 0)
  17226. ((not (outline-previous-heading)) 0)
  17227. (t (length (match-string 0))))))
  17228. ;; Level of first heading. Further headings will be
  17229. ;; compared to it to determine hierarchy in the list.
  17230. (ref-level (org-reduced-level (org-outline-level))))
  17231. (while (< (point) end)
  17232. (let* ((level (org-reduced-level (org-outline-level)))
  17233. (delta (max 0 (- level ref-level))))
  17234. ;; If current headline is less indented than the first
  17235. ;; one, set it as reference, in order to preserve
  17236. ;; subtrees.
  17237. (when (< level ref-level) (setq ref-level level))
  17238. (replace-match bul t t)
  17239. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17240. ;; Ensure all text down to END (or SECTION-END) belongs
  17241. ;; to the newly created item.
  17242. (let ((section-end (save-excursion
  17243. (or (outline-next-heading) (point)))))
  17244. (forward-line)
  17245. (funcall shift-text
  17246. (+ start-ind (* (1+ delta) bul-len))
  17247. (min end section-end)))))))
  17248. ;; Case 3. Normal line with ARG: turn each non-item line into
  17249. ;; an item.
  17250. (arg
  17251. (while (< (point) end)
  17252. (unless (or (org-at-heading-p) (org-at-item-p))
  17253. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17254. (replace-match
  17255. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17256. (forward-line)))
  17257. ;; Case 4. Normal line without ARG: make the first line of
  17258. ;; region an item, and shift indentation of others
  17259. ;; lines to set them as item's body.
  17260. (t (let* ((bul (org-list-bullet-string "-"))
  17261. (bul-len (length bul))
  17262. (ref-ind (org-get-indentation)))
  17263. (skip-chars-forward " \t")
  17264. (insert bul)
  17265. (forward-line)
  17266. (while (< (point) end)
  17267. ;; Ensure that lines less indented than first one
  17268. ;; still get included in item body.
  17269. (funcall shift-text
  17270. (+ ref-ind bul-len)
  17271. (min end (save-excursion (or (outline-next-heading)
  17272. (point)))))
  17273. (forward-line)))))))))
  17274. (defun org-toggle-heading (&optional nstars)
  17275. "Convert headings to normal text, or items or text to headings.
  17276. If there is no active region, only the current line is considered.
  17277. If the first non blank line is an headline, remove the stars from
  17278. all headlines in the region.
  17279. If it is a plain list item, turn all plain list items into headings.
  17280. If it is a normal line, turn each and every normal line (i.e. not
  17281. an heading or an item) in the region into a heading.
  17282. When converting a line into a heading, the number of stars is chosen
  17283. such that the lines become children of the current entry. However,
  17284. when a prefix argument is given, its value determines the number of
  17285. stars to add."
  17286. (interactive "P")
  17287. (let ((skip-blanks
  17288. (function
  17289. ;; Return beginning of first non-blank line, starting from
  17290. ;; line at POS.
  17291. (lambda (pos)
  17292. (save-excursion
  17293. (goto-char pos)
  17294. (skip-chars-forward " \r\t\n")
  17295. (point-at-bol)))))
  17296. beg end)
  17297. ;; Determine boundaries of changes. If region ends at a bol, do
  17298. ;; not consider the last line to be in the region.
  17299. (if (org-region-active-p)
  17300. (setq beg (funcall skip-blanks (region-beginning))
  17301. end (copy-marker (save-excursion
  17302. (goto-char (region-end))
  17303. (if (bolp) (point) (point-at-eol)))))
  17304. (setq beg (funcall skip-blanks (point-at-bol))
  17305. end (copy-marker (point-at-eol))))
  17306. ;; Ensure inline tasks don't count as headings.
  17307. (org-with-limited-levels
  17308. (save-excursion
  17309. (goto-char beg)
  17310. (cond
  17311. ;; Case 1. Started at an heading: de-star headings.
  17312. ((org-at-heading-p)
  17313. (while (< (point) end)
  17314. (when (org-at-heading-p t)
  17315. (looking-at org-outline-regexp) (replace-match ""))
  17316. (forward-line)))
  17317. ;; Case 2. Started at an item: change items into headlines.
  17318. ;; One star will be added by `org-list-to-subtree'.
  17319. ((org-at-item-p)
  17320. (let* ((stars (make-string
  17321. (if nstars
  17322. ;; subtract the star that will be added again by
  17323. ;; `org-list-to-subtree'
  17324. (1- (prefix-numeric-value current-prefix-arg))
  17325. (or (org-current-level) 0))
  17326. ?*))
  17327. (add-stars
  17328. (cond (nstars "") ; stars from prefix only
  17329. ((equal stars "") "") ; before first heading
  17330. (org-odd-levels-only "*") ; inside heading, odd
  17331. (t "")))) ; inside heading, oddeven
  17332. (while (< (point) end)
  17333. (when (org-at-item-p)
  17334. ;; Pay attention to cases when region ends before list.
  17335. (let* ((struct (org-list-struct))
  17336. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17337. (save-restriction
  17338. (narrow-to-region (point) list-end)
  17339. (insert
  17340. (org-list-to-subtree
  17341. (org-list-parse-list t)
  17342. '(:istart (concat stars add-stars (funcall get-stars depth))
  17343. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17344. (forward-line))))
  17345. ;; Case 3. Started at normal text: make every line an heading,
  17346. ;; skipping headlines and items.
  17347. (t (let* ((stars (make-string
  17348. (if nstars
  17349. (prefix-numeric-value current-prefix-arg)
  17350. (or (org-current-level) 0))
  17351. ?*))
  17352. (add-stars
  17353. (cond (nstars "") ; stars from prefix only
  17354. ((equal stars "") "*") ; before first heading
  17355. (org-odd-levels-only "**") ; inside heading, odd
  17356. (t "*"))) ; inside heading, oddeven
  17357. (rpl (concat stars add-stars " ")))
  17358. (while (< (point) end)
  17359. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17360. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17361. (replace-match (concat rpl (match-string 2))))
  17362. (forward-line)))))))))
  17363. (defun org-meta-return (&optional arg)
  17364. "Insert a new heading or wrap a region in a table.
  17365. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17366. See the individual commands for more information."
  17367. (interactive "P")
  17368. (cond
  17369. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17370. ((or (org-at-drawer-p) (org-at-property-p))
  17371. (newline-and-indent))
  17372. ((org-at-table-p)
  17373. (call-interactively 'org-table-wrap-region))
  17374. (t (call-interactively 'org-insert-heading))))
  17375. ;;; Menu entries
  17376. ;; Define the Org-mode menus
  17377. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17378. '("Tbl"
  17379. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17380. ["Next Field" org-cycle (org-at-table-p)]
  17381. ["Previous Field" org-shifttab (org-at-table-p)]
  17382. ["Next Row" org-return (org-at-table-p)]
  17383. "--"
  17384. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17385. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17386. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17387. "--"
  17388. ("Column"
  17389. ["Move Column Left" org-metaleft (org-at-table-p)]
  17390. ["Move Column Right" org-metaright (org-at-table-p)]
  17391. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17392. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17393. ("Row"
  17394. ["Move Row Up" org-metaup (org-at-table-p)]
  17395. ["Move Row Down" org-metadown (org-at-table-p)]
  17396. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17397. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17398. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17399. "--"
  17400. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17401. ("Rectangle"
  17402. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17403. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17404. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17405. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17406. "--"
  17407. ("Calculate"
  17408. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17409. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17410. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17411. "--"
  17412. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17413. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17414. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17415. "--"
  17416. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17417. "--"
  17418. ["Sum Column/Rectangle" org-table-sum
  17419. (or (org-at-table-p) (org-region-active-p))]
  17420. ["Which Column?" org-table-current-column (org-at-table-p)])
  17421. ["Debug Formulas"
  17422. org-table-toggle-formula-debugger
  17423. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17424. ["Show Col/Row Numbers"
  17425. org-table-toggle-coordinate-overlays
  17426. :style toggle
  17427. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17428. "--"
  17429. ["Create" org-table-create (and (not (org-at-table-p))
  17430. org-enable-table-editor)]
  17431. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17432. ["Import from File" org-table-import (not (org-at-table-p))]
  17433. ["Export to File" org-table-export (org-at-table-p)]
  17434. "--"
  17435. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17436. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17437. '("Org"
  17438. ("Show/Hide"
  17439. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17440. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17441. ["Sparse Tree..." org-sparse-tree t]
  17442. ["Reveal Context" org-reveal t]
  17443. ["Show All" show-all t]
  17444. "--"
  17445. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17446. "--"
  17447. ["New Heading" org-insert-heading t]
  17448. ("Navigate Headings"
  17449. ["Up" outline-up-heading t]
  17450. ["Next" outline-next-visible-heading t]
  17451. ["Previous" outline-previous-visible-heading t]
  17452. ["Next Same Level" outline-forward-same-level t]
  17453. ["Previous Same Level" outline-backward-same-level t]
  17454. "--"
  17455. ["Jump" org-goto t])
  17456. ("Edit Structure"
  17457. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17458. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17459. "--"
  17460. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17461. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17462. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17463. "--"
  17464. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17465. "--"
  17466. ["Copy visible text" org-copy-visible t]
  17467. "--"
  17468. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17469. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17470. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17471. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17472. "--"
  17473. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17474. "--"
  17475. ["Convert to odd levels" org-convert-to-odd-levels t]
  17476. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17477. ("Editing"
  17478. ["Emphasis..." org-emphasize t]
  17479. ["Edit Source Example" org-edit-special t]
  17480. "--"
  17481. ["Footnote new/jump" org-footnote-action t]
  17482. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17483. ("Archive"
  17484. ["Archive (default method)" org-archive-subtree-default t]
  17485. "--"
  17486. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17487. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17488. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17489. )
  17490. "--"
  17491. ("Hyperlinks"
  17492. ["Store Link (Global)" org-store-link t]
  17493. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17494. ["Insert Link" org-insert-link t]
  17495. ["Follow Link" org-open-at-point t]
  17496. "--"
  17497. ["Next link" org-next-link t]
  17498. ["Previous link" org-previous-link t]
  17499. "--"
  17500. ["Descriptive Links"
  17501. org-toggle-link-display
  17502. :style radio
  17503. :selected org-descriptive-links
  17504. ]
  17505. ["Literal Links"
  17506. org-toggle-link-display
  17507. :style radio
  17508. :selected (not org-descriptive-links)])
  17509. "--"
  17510. ("TODO Lists"
  17511. ["TODO/DONE/-" org-todo t]
  17512. ("Select keyword"
  17513. ["Next keyword" org-shiftright (org-at-heading-p)]
  17514. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17515. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17516. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17517. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17518. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17519. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17520. "--"
  17521. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17522. :selected org-enforce-todo-dependencies :style toggle :active t]
  17523. "Settings for tree at point"
  17524. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17525. :selected (org-entry-get nil "ORDERED")
  17526. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17527. ["Do Children parallel" org-toggle-ordered-property :style radio
  17528. :selected (not (org-entry-get nil "ORDERED"))
  17529. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17530. "--"
  17531. ["Set Priority" org-priority t]
  17532. ["Priority Up" org-shiftup t]
  17533. ["Priority Down" org-shiftdown t]
  17534. "--"
  17535. ["Get news from all feeds" org-feed-update-all t]
  17536. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17537. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17538. ("TAGS and Properties"
  17539. ["Set Tags" org-set-tags-command t]
  17540. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17541. "--"
  17542. ["Set property" org-set-property t]
  17543. ["Column view of properties" org-columns t]
  17544. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17545. ("Dates and Scheduling"
  17546. ["Timestamp" org-time-stamp t]
  17547. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17548. ("Change Date"
  17549. ["1 Day Later" org-shiftright t]
  17550. ["1 Day Earlier" org-shiftleft t]
  17551. ["1 ... Later" org-shiftup t]
  17552. ["1 ... Earlier" org-shiftdown t])
  17553. ["Compute Time Range" org-evaluate-time-range t]
  17554. ["Schedule Item" org-schedule t]
  17555. ["Deadline" org-deadline t]
  17556. "--"
  17557. ["Custom time format" org-toggle-time-stamp-overlays
  17558. :style radio :selected org-display-custom-times]
  17559. "--"
  17560. ["Goto Calendar" org-goto-calendar t]
  17561. ["Date from Calendar" org-date-from-calendar t]
  17562. "--"
  17563. ["Start/Restart Timer" org-timer-start t]
  17564. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17565. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17566. ["Insert Timer String" org-timer t]
  17567. ["Insert Timer Item" org-timer-item t])
  17568. ("Logging work"
  17569. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17570. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17571. ["Clock out" org-clock-out t]
  17572. ["Clock cancel" org-clock-cancel t]
  17573. "--"
  17574. ["Mark as default task" org-clock-mark-default-task t]
  17575. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17576. ["Goto running clock" org-clock-goto t]
  17577. "--"
  17578. ["Display times" org-clock-display t]
  17579. ["Create clock table" org-clock-report t]
  17580. "--"
  17581. ["Record DONE time"
  17582. (progn (setq org-log-done (not org-log-done))
  17583. (message "Switching to %s will %s record a timestamp"
  17584. (car org-done-keywords)
  17585. (if org-log-done "automatically" "not")))
  17586. :style toggle :selected org-log-done])
  17587. "--"
  17588. ["Agenda Command..." org-agenda t]
  17589. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17590. ("File List for Agenda")
  17591. ("Special views current file"
  17592. ["TODO Tree" org-show-todo-tree t]
  17593. ["Check Deadlines" org-check-deadlines t]
  17594. ["Timeline" org-timeline t]
  17595. ["Tags/Property tree" org-match-sparse-tree t])
  17596. "--"
  17597. ["Export/Publish..." org-export t]
  17598. ("LaTeX"
  17599. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17600. :selected org-cdlatex-mode]
  17601. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17602. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17603. ["Modify math symbol" org-cdlatex-math-modify
  17604. (org-inside-LaTeX-fragment-p)]
  17605. ["Insert citation" org-reftex-citation t]
  17606. "--"
  17607. ["Template for BEAMER" org-insert-beamer-options-template t])
  17608. "--"
  17609. ("MobileOrg"
  17610. ["Push Files and Views" org-mobile-push t]
  17611. ["Get Captured and Flagged" org-mobile-pull t]
  17612. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17613. "--"
  17614. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17615. "--"
  17616. ("Documentation"
  17617. ["Show Version" org-version t]
  17618. ["Info Documentation" org-info t])
  17619. ("Customize"
  17620. ["Browse Org Group" org-customize t]
  17621. "--"
  17622. ["Expand This Menu" org-create-customize-menu
  17623. (fboundp 'customize-menu-create)])
  17624. ["Send bug report" org-submit-bug-report t]
  17625. "--"
  17626. ("Refresh/Reload"
  17627. ["Refresh setup current buffer" org-mode-restart t]
  17628. ["Reload Org (after update)" org-reload t]
  17629. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17630. ))
  17631. (defun org-info (&optional node)
  17632. "Read documentation for Org-mode in the info system.
  17633. With optional NODE, go directly to that node."
  17634. (interactive)
  17635. (info (format "(org)%s" (or node ""))))
  17636. ;;;###autoload
  17637. (defun org-submit-bug-report ()
  17638. "Submit a bug report on Org-mode via mail.
  17639. Don't hesitate to report any problems or inaccurate documentation.
  17640. If you don't have setup sending mail from (X)Emacs, please copy the
  17641. output buffer into your mail program, as it gives us important
  17642. information about your Org-mode version and configuration."
  17643. (interactive)
  17644. (require 'reporter)
  17645. (org-load-modules-maybe)
  17646. (org-require-autoloaded-modules)
  17647. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17648. (reporter-submit-bug-report
  17649. "emacs-orgmode@gnu.org"
  17650. (org-version)
  17651. (let (list)
  17652. (save-window-excursion
  17653. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17654. (delete-other-windows)
  17655. (erase-buffer)
  17656. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17657. We would like to add your full Org-mode and Outline configuration to the
  17658. bug report. This greatly simplifies the work of the maintainer and
  17659. other experts on the mailing list.
  17660. HOWEVER, some variables you have customized may contain private
  17661. information. The names of customers, colleagues, or friends, might
  17662. appear in the form of file names, tags, todo states, or search strings.
  17663. If you answer yes to the prompt, you might want to check and remove
  17664. such private information before sending the email.")
  17665. (add-text-properties (point-min) (point-max) '(face org-warning))
  17666. (when (yes-or-no-p "Include your Org-mode configuration ")
  17667. (mapatoms
  17668. (lambda (v)
  17669. (and (boundp v)
  17670. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17671. (or (and (symbol-value v)
  17672. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17673. (and
  17674. (get v 'custom-type) (get v 'standard-value)
  17675. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17676. (push v list)))))
  17677. (kill-buffer (get-buffer "*Warn about privacy*"))
  17678. list))
  17679. nil nil
  17680. "Remember to cover the basics, that is, what you expected to happen and
  17681. what in fact did happen. You don't know how to make a good report? See
  17682. http://orgmode.org/manual/Feedback.html#Feedback
  17683. Your bug report will be posted to the Org-mode mailing list.
  17684. ------------------------------------------------------------------------")
  17685. (save-excursion
  17686. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17687. (replace-match "\\1Bug: \\3 [\\2]")))))
  17688. (defun org-install-agenda-files-menu ()
  17689. (let ((bl (buffer-list)))
  17690. (save-excursion
  17691. (while bl
  17692. (set-buffer (pop bl))
  17693. (if (eq major-mode 'org-mode) (setq bl nil)))
  17694. (when (eq major-mode 'org-mode)
  17695. (easy-menu-change
  17696. '("Org") "File List for Agenda"
  17697. (append
  17698. (list
  17699. ["Edit File List" (org-edit-agenda-file-list) t]
  17700. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17701. ["Remove Current File from List" org-remove-file t]
  17702. ["Cycle through agenda files" org-cycle-agenda-files t]
  17703. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17704. "--")
  17705. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17706. ;;;; Documentation
  17707. ;;;###autoload
  17708. (defun org-require-autoloaded-modules ()
  17709. (interactive)
  17710. (mapc 'require
  17711. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17712. org-docbook org-exp org-html org-icalendar
  17713. org-id org-latex
  17714. org-publish org-remember org-table
  17715. org-timer org-xoxo)))
  17716. ;;;###autoload
  17717. (defun org-reload (&optional uncompiled)
  17718. "Reload all org lisp files.
  17719. With prefix arg UNCOMPILED, load the uncompiled versions."
  17720. (interactive "P")
  17721. (require 'find-func)
  17722. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17723. (dir-org (file-name-directory (org-find-library-name "org")))
  17724. (dir-org-contrib (ignore-errors
  17725. (file-name-directory
  17726. (org-find-library-name "org-contribdir"))))
  17727. (babel-files
  17728. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17729. (append (list nil "comint" "eval" "exp" "keys"
  17730. "lob" "ref" "table" "tangle")
  17731. (delq nil
  17732. (mapcar
  17733. (lambda (lang)
  17734. (when (cdr lang) (symbol-name (car lang))))
  17735. org-babel-load-languages)))))
  17736. (files
  17737. (append (directory-files dir-org t file-re)
  17738. babel-files
  17739. (and dir-org-contrib
  17740. (directory-files dir-org-contrib t file-re))))
  17741. (remove-re (concat (if (featurep 'xemacs)
  17742. "org-colview" "org-colview-xemacs")
  17743. "\\'")))
  17744. (setq files (mapcar 'file-name-sans-extension files))
  17745. (setq files (mapcar
  17746. (lambda (x) (if (string-match remove-re x) nil x))
  17747. files))
  17748. (setq files (delq nil files))
  17749. (mapc
  17750. (lambda (f)
  17751. (when (featurep (intern (file-name-nondirectory f)))
  17752. (if (and (not uncompiled)
  17753. (file-exists-p (concat f ".elc")))
  17754. (load (concat f ".elc") nil nil t)
  17755. (load (concat f ".el") nil nil t))))
  17756. files))
  17757. (org-version))
  17758. ;;;###autoload
  17759. (defun org-customize ()
  17760. "Call the customize function with org as argument."
  17761. (interactive)
  17762. (org-load-modules-maybe)
  17763. (org-require-autoloaded-modules)
  17764. (customize-browse 'org))
  17765. (defun org-create-customize-menu ()
  17766. "Create a full customization menu for Org-mode, insert it into the menu."
  17767. (interactive)
  17768. (org-load-modules-maybe)
  17769. (org-require-autoloaded-modules)
  17770. (if (fboundp 'customize-menu-create)
  17771. (progn
  17772. (easy-menu-change
  17773. '("Org") "Customize"
  17774. `(["Browse Org group" org-customize t]
  17775. "--"
  17776. ,(customize-menu-create 'org)
  17777. ["Set" Custom-set t]
  17778. ["Save" Custom-save t]
  17779. ["Reset to Current" Custom-reset-current t]
  17780. ["Reset to Saved" Custom-reset-saved t]
  17781. ["Reset to Standard Settings" Custom-reset-standard t]))
  17782. (message "\"Org\"-menu now contains full customization menu"))
  17783. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17784. ;;;; Miscellaneous stuff
  17785. ;;; Generally useful functions
  17786. (defun org-get-at-bol (property)
  17787. "Get text property PROPERTY at beginning of line."
  17788. (get-text-property (point-at-bol) property))
  17789. (defun org-find-text-property-in-string (prop s)
  17790. "Return the first non-nil value of property PROP in string S."
  17791. (or (get-text-property 0 prop s)
  17792. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17793. prop s)))
  17794. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17795. "Display the given MESSAGE as a warning."
  17796. (if (fboundp 'display-warning)
  17797. (display-warning 'org message
  17798. (if (featurep 'xemacs) 'warning :warning))
  17799. (let ((buf (get-buffer-create "*Org warnings*")))
  17800. (with-current-buffer buf
  17801. (goto-char (point-max))
  17802. (insert "Warning (Org): " message)
  17803. (unless (bolp)
  17804. (newline)))
  17805. (display-buffer buf)
  17806. (sit-for 0))))
  17807. (defun org-eval (form)
  17808. "Eval FORM and return result."
  17809. (condition-case error
  17810. (eval form)
  17811. (error (format "%%![Error: %s]" error))))
  17812. (defun org-in-clocktable-p ()
  17813. "Check if the cursor is in a clocktable."
  17814. (let ((pos (point)) start)
  17815. (save-excursion
  17816. (end-of-line 1)
  17817. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  17818. (setq start (match-beginning 0))
  17819. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  17820. (>= (match-end 0) pos)
  17821. start))))
  17822. (defun org-in-commented-line ()
  17823. "Is point in a line starting with `#'?"
  17824. (equal (char-after (point-at-bol)) ?#))
  17825. (defun org-in-indented-comment-line ()
  17826. "Is point in a line starting with `#' after some white space?"
  17827. (save-excursion
  17828. (save-match-data
  17829. (goto-char (point-at-bol))
  17830. (looking-at "[ \t]*#"))))
  17831. (defun org-in-verbatim-emphasis ()
  17832. (save-match-data
  17833. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17834. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17835. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17836. (if (and marker (marker-buffer marker)
  17837. (buffer-live-p (marker-buffer marker)))
  17838. (progn
  17839. (org-pop-to-buffer-same-window (marker-buffer marker))
  17840. (if (or (> marker (point-max)) (< marker (point-min)))
  17841. (widen))
  17842. (goto-char marker)
  17843. (org-show-context 'org-goto))
  17844. (if bookmark
  17845. (bookmark-jump bookmark)
  17846. (error "Cannot find location"))))
  17847. (defun org-quote-csv-field (s)
  17848. "Quote field for inclusion in CSV material."
  17849. (if (string-match "[\",]" s)
  17850. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17851. s))
  17852. (defun org-force-self-insert (N)
  17853. "Needed to enforce self-insert under remapping."
  17854. (interactive "p")
  17855. (self-insert-command N))
  17856. (defun org-string-width (s)
  17857. "Compute width of string, ignoring invisible characters.
  17858. This ignores character with invisibility property `org-link', and also
  17859. characters with property `org-cwidth', because these will become invisible
  17860. upon the next fontification round."
  17861. (let (b l)
  17862. (when (or (eq t buffer-invisibility-spec)
  17863. (assq 'org-link buffer-invisibility-spec))
  17864. (while (setq b (text-property-any 0 (length s)
  17865. 'invisible 'org-link s))
  17866. (setq s (concat (substring s 0 b)
  17867. (substring s (or (next-single-property-change
  17868. b 'invisible s) (length s)))))))
  17869. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17870. (setq s (concat (substring s 0 b)
  17871. (substring s (or (next-single-property-change
  17872. b 'org-cwidth s) (length s))))))
  17873. (setq l (string-width s) b -1)
  17874. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17875. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17876. l))
  17877. (defun org-shorten-string (s maxlength)
  17878. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17879. If the string is shorter or has length MAXLENGTH, just return the
  17880. original string. If it is longer, the functions finds a space in the
  17881. string, breaks this string off at that locations and adds three dots
  17882. as ellipsis. Including the ellipsis, the string will not be longer
  17883. than MAXLENGTH. If finding a good breaking point in the string does
  17884. not work, the string is just chopped off in the middle of a word
  17885. if necessary."
  17886. (if (<= (length s) maxlength)
  17887. s
  17888. (let* ((n (max (- maxlength 4) 1))
  17889. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17890. (if (string-match re s)
  17891. (concat (match-string 1 s) "...")
  17892. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17893. (defun org-get-indentation (&optional line)
  17894. "Get the indentation of the current line, interpreting tabs.
  17895. When LINE is given, assume it represents a line and compute its indentation."
  17896. (if line
  17897. (if (string-match "^ *" (org-remove-tabs line))
  17898. (match-end 0))
  17899. (save-excursion
  17900. (beginning-of-line 1)
  17901. (skip-chars-forward " \t")
  17902. (current-column))))
  17903. (defun org-get-string-indentation (s)
  17904. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17905. (let ((n -1) (i 0) (w tab-width) c)
  17906. (catch 'exit
  17907. (while (< (setq n (1+ n)) (length s))
  17908. (setq c (aref s n))
  17909. (cond ((= c ?\ ) (setq i (1+ i)))
  17910. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17911. (t (throw 'exit t)))))
  17912. i))
  17913. (defun org-remove-tabs (s &optional width)
  17914. "Replace tabulators in S with spaces.
  17915. Assumes that s is a single line, starting in column 0."
  17916. (setq width (or width tab-width))
  17917. (while (string-match "\t" s)
  17918. (setq s (replace-match
  17919. (make-string
  17920. (- (* width (/ (+ (match-beginning 0) width) width))
  17921. (match-beginning 0)) ?\ )
  17922. t t s)))
  17923. s)
  17924. (defun org-fix-indentation (line ind)
  17925. "Fix indentation in LINE.
  17926. IND is a cons cell with target and minimum indentation.
  17927. If the current indentation in LINE is smaller than the minimum,
  17928. leave it alone. If it is larger than ind, set it to the target."
  17929. (let* ((l (org-remove-tabs line))
  17930. (i (org-get-indentation l))
  17931. (i1 (car ind)) (i2 (cdr ind)))
  17932. (if (>= i i2) (setq l (substring line i2)))
  17933. (if (> i1 0)
  17934. (concat (make-string i1 ?\ ) l)
  17935. l)))
  17936. (defun org-remove-indentation (code &optional n)
  17937. "Remove the maximum common indentation from the lines in CODE.
  17938. N may optionally be the number of spaces to remove."
  17939. (with-temp-buffer
  17940. (insert code)
  17941. (org-do-remove-indentation n)
  17942. (buffer-string)))
  17943. (defun org-do-remove-indentation (&optional n)
  17944. "Remove the maximum common indentation from the buffer."
  17945. (untabify (point-min) (point-max))
  17946. (let ((min 10000) re)
  17947. (if n
  17948. (setq min n)
  17949. (goto-char (point-min))
  17950. (while (re-search-forward "^ *[^ \n]" nil t)
  17951. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17952. (unless (or (= min 0) (= min 10000))
  17953. (setq re (format "^ \\{%d\\}" min))
  17954. (goto-char (point-min))
  17955. (while (re-search-forward re nil t)
  17956. (replace-match "")
  17957. (end-of-line 1))
  17958. min)))
  17959. (defun org-fill-template (template alist)
  17960. "Find each %key of ALIST in TEMPLATE and replace it."
  17961. (let ((case-fold-search nil)
  17962. entry key value)
  17963. (setq alist (sort (copy-sequence alist)
  17964. (lambda (a b) (< (length (car a)) (length (car b))))))
  17965. (while (setq entry (pop alist))
  17966. (setq template
  17967. (replace-regexp-in-string
  17968. (concat "%" (regexp-quote (car entry)))
  17969. (cdr entry) template t t)))
  17970. template))
  17971. (defun org-base-buffer (buffer)
  17972. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17973. (if (not buffer)
  17974. buffer
  17975. (or (buffer-base-buffer buffer)
  17976. buffer)))
  17977. (defun org-trim (s)
  17978. "Remove whitespace at beginning and end of string."
  17979. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17980. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17981. s)
  17982. (defun org-wrap (string &optional width lines)
  17983. "Wrap string to either a number of lines, or a width in characters.
  17984. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17985. that costs. If there is a word longer than WIDTH, the text is actually
  17986. wrapped to the length of that word.
  17987. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17988. many lines, whatever width that takes.
  17989. The return value is a list of lines, without newlines at the end."
  17990. (let* ((words (org-split-string string "[ \t\n]+"))
  17991. (maxword (apply 'max (mapcar 'org-string-width words)))
  17992. w ll)
  17993. (cond (width
  17994. (org-do-wrap words (max maxword width)))
  17995. (lines
  17996. (setq w maxword)
  17997. (setq ll (org-do-wrap words maxword))
  17998. (if (<= (length ll) lines)
  17999. ll
  18000. (setq ll words)
  18001. (while (> (length ll) lines)
  18002. (setq w (1+ w))
  18003. (setq ll (org-do-wrap words w)))
  18004. ll))
  18005. (t (error "Cannot wrap this")))))
  18006. (defun org-do-wrap (words width)
  18007. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18008. (let (lines line)
  18009. (while words
  18010. (setq line (pop words))
  18011. (while (and words (< (+ (length line) (length (car words))) width))
  18012. (setq line (concat line " " (pop words))))
  18013. (setq lines (push line lines)))
  18014. (nreverse lines)))
  18015. (defun org-split-string (string &optional separators)
  18016. "Splits STRING into substrings at SEPARATORS.
  18017. No empty strings are returned if there are matches at the beginning
  18018. and end of string."
  18019. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18020. (start 0)
  18021. notfirst
  18022. (list nil))
  18023. (while (and (string-match rexp string
  18024. (if (and notfirst
  18025. (= start (match-beginning 0))
  18026. (< start (length string)))
  18027. (1+ start) start))
  18028. (< (match-beginning 0) (length string)))
  18029. (setq notfirst t)
  18030. (or (eq (match-beginning 0) 0)
  18031. (and (eq (match-beginning 0) (match-end 0))
  18032. (eq (match-beginning 0) start))
  18033. (setq list
  18034. (cons (substring string start (match-beginning 0))
  18035. list)))
  18036. (setq start (match-end 0)))
  18037. (or (eq start (length string))
  18038. (setq list
  18039. (cons (substring string start)
  18040. list)))
  18041. (nreverse list)))
  18042. (defun org-quote-vert (s)
  18043. "Replace \"|\" with \"\\vert\"."
  18044. (while (string-match "|" s)
  18045. (setq s (replace-match "\\vert" t t s)))
  18046. s)
  18047. (defun org-uuidgen-p (s)
  18048. "Is S an ID created by UUIDGEN?"
  18049. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18050. (defun org-in-src-block-p nil
  18051. "Whether point is in a code source block."
  18052. (let (ov)
  18053. (when (setq ov (overlays-at (point)))
  18054. (memq 'org-block-background
  18055. (overlay-properties
  18056. (car ov))))))
  18057. (defun org-context ()
  18058. "Return a list of contexts of the current cursor position.
  18059. If several contexts apply, all are returned.
  18060. Each context entry is a list with a symbol naming the context, and
  18061. two positions indicating start and end of the context. Possible
  18062. contexts are:
  18063. :headline anywhere in a headline
  18064. :headline-stars on the leading stars in a headline
  18065. :todo-keyword on a TODO keyword (including DONE) in a headline
  18066. :tags on the TAGS in a headline
  18067. :priority on the priority cookie in a headline
  18068. :item on the first line of a plain list item
  18069. :item-bullet on the bullet/number of a plain list item
  18070. :checkbox on the checkbox in a plain list item
  18071. :table in an org-mode table
  18072. :table-special on a special filed in a table
  18073. :table-table in a table.el table
  18074. :clocktable in a clocktable
  18075. :src-block in a source block
  18076. :link on a hyperlink
  18077. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18078. :target on a <<target>>
  18079. :radio-target on a <<<radio-target>>>
  18080. :latex-fragment on a LaTeX fragment
  18081. :latex-preview on a LaTeX fragment with overlaid preview image
  18082. This function expects the position to be visible because it uses font-lock
  18083. faces as a help to recognize the following contexts: :table-special, :link,
  18084. and :keyword."
  18085. (let* ((f (get-text-property (point) 'face))
  18086. (faces (if (listp f) f (list f)))
  18087. (case-fold-search t)
  18088. (p (point)) clist o)
  18089. ;; First the large context
  18090. (cond
  18091. ((org-at-heading-p t)
  18092. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18093. (when (progn
  18094. (beginning-of-line 1)
  18095. (looking-at org-todo-line-tags-regexp))
  18096. (push (org-point-in-group p 1 :headline-stars) clist)
  18097. (push (org-point-in-group p 2 :todo-keyword) clist)
  18098. (push (org-point-in-group p 4 :tags) clist))
  18099. (goto-char p)
  18100. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18101. (if (looking-at "\\[#[A-Z0-9]\\]")
  18102. (push (org-point-in-group p 0 :priority) clist)))
  18103. ((org-at-item-p)
  18104. (push (org-point-in-group p 2 :item-bullet) clist)
  18105. (push (list :item (point-at-bol)
  18106. (save-excursion (org-end-of-item) (point)))
  18107. clist)
  18108. (and (org-at-item-checkbox-p)
  18109. (push (org-point-in-group p 0 :checkbox) clist)))
  18110. ((org-at-table-p)
  18111. (push (list :table (org-table-begin) (org-table-end)) clist)
  18112. (if (memq 'org-formula faces)
  18113. (push (list :table-special
  18114. (previous-single-property-change p 'face)
  18115. (next-single-property-change p 'face)) clist)))
  18116. ((org-at-table-p 'any)
  18117. (push (list :table-table) clist)))
  18118. (goto-char p)
  18119. ;; New the "medium" contexts: clocktables, source blocks
  18120. (cond ((org-in-clocktable-p)
  18121. (push (list :clocktable
  18122. (and (or (looking-at "#\\+BEGIN: clocktable")
  18123. (search-backward "#+BEGIN: clocktable" nil t))
  18124. (match-beginning 0))
  18125. (and (re-search-forward "#\\+END:?" nil t)
  18126. (match-end 0))) clist))
  18127. ((org-in-src-block-p)
  18128. (push (list :src-block
  18129. (and (or (looking-at "#\\+BEGIN_SRC")
  18130. (search-backward "#+BEGIN_SRC" nil t))
  18131. (match-beginning 0))
  18132. (and (search-forward "#+END_SRC" nil t)
  18133. (match-beginning 0))) clist)))
  18134. (goto-char p)
  18135. ;; Now the small context
  18136. (cond
  18137. ((org-at-timestamp-p)
  18138. (push (org-point-in-group p 0 :timestamp) clist))
  18139. ((memq 'org-link faces)
  18140. (push (list :link
  18141. (previous-single-property-change p 'face)
  18142. (next-single-property-change p 'face)) clist))
  18143. ((memq 'org-special-keyword faces)
  18144. (push (list :keyword
  18145. (previous-single-property-change p 'face)
  18146. (next-single-property-change p 'face)) clist))
  18147. ((org-at-target-p)
  18148. (push (org-point-in-group p 0 :target) clist)
  18149. (goto-char (1- (match-beginning 0)))
  18150. (if (looking-at org-radio-target-regexp)
  18151. (push (org-point-in-group p 0 :radio-target) clist))
  18152. (goto-char p))
  18153. ((setq o (car (delq nil
  18154. (mapcar
  18155. (lambda (x)
  18156. (if (memq x org-latex-fragment-image-overlays) x))
  18157. (overlays-at (point))))))
  18158. (push (list :latex-fragment
  18159. (overlay-start o) (overlay-end o)) clist)
  18160. (push (list :latex-preview
  18161. (overlay-start o) (overlay-end o)) clist))
  18162. ((org-inside-LaTeX-fragment-p)
  18163. ;; FIXME: positions wrong.
  18164. (push (list :latex-fragment (point) (point)) clist)))
  18165. (setq clist (nreverse (delq nil clist)))
  18166. clist))
  18167. ;; FIXME: Compare with at-regexp-p Do we need both?
  18168. (defun org-in-regexp (re &optional nlines visually)
  18169. "Check if point is inside a match of regexp.
  18170. Normally only the current line is checked, but you can include NLINES extra
  18171. lines both before and after point into the search.
  18172. If VISUALLY is set, require that the cursor is not after the match but
  18173. really on, so that the block visually is on the match."
  18174. (catch 'exit
  18175. (let ((pos (point))
  18176. (eol (point-at-eol (+ 1 (or nlines 0))))
  18177. (inc (if visually 1 0)))
  18178. (save-excursion
  18179. (beginning-of-line (- 1 (or nlines 0)))
  18180. (while (re-search-forward re eol t)
  18181. (if (and (<= (match-beginning 0) pos)
  18182. (>= (+ inc (match-end 0)) pos))
  18183. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18184. (defun org-at-regexp-p (regexp)
  18185. "Is point inside a match of REGEXP in the current line?"
  18186. (catch 'exit
  18187. (save-excursion
  18188. (let ((pos (point)) (end (point-at-eol)))
  18189. (beginning-of-line 1)
  18190. (while (re-search-forward regexp end t)
  18191. (if (and (<= (match-beginning 0) pos)
  18192. (>= (match-end 0) pos))
  18193. (throw 'exit t)))
  18194. nil))))
  18195. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18196. "Non-nil when point is between matches of START-RE and END-RE.
  18197. Also return a non-nil value when point is on one of the matches.
  18198. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18199. buffer positions. Default values are the positions of headlines
  18200. surrounding the point.
  18201. The functions returns a cons cell whose car (resp. cdr) is the
  18202. position before START-RE (resp. after END-RE)."
  18203. (save-match-data
  18204. (let ((pos (point))
  18205. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18206. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18207. beg end)
  18208. (save-excursion
  18209. ;; Point is on a block when on START-RE or if START-RE can be
  18210. ;; found before it...
  18211. (and (or (org-at-regexp-p start-re)
  18212. (re-search-backward start-re limit-up t))
  18213. (setq beg (match-beginning 0))
  18214. ;; ... and END-RE after it...
  18215. (goto-char (match-end 0))
  18216. (re-search-forward end-re limit-down t)
  18217. (> (setq end (match-end 0)) pos)
  18218. ;; ... without another START-RE in-between.
  18219. (goto-char (match-beginning 0))
  18220. (not (re-search-backward start-re (1+ beg) t))
  18221. ;; Return value.
  18222. (cons beg end))))))
  18223. (defun org-in-block-p (names)
  18224. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18225. NAMES is a list of strings containing names of blocks.
  18226. Return first block name matched, or nil. Beware that in case of
  18227. nested blocks, the returned name may not belong to the closest
  18228. block from point."
  18229. (save-match-data
  18230. (catch 'exit
  18231. (let ((case-fold-search t)
  18232. (lim-up (save-excursion (outline-previous-heading)))
  18233. (lim-down (save-excursion (outline-next-heading))))
  18234. (mapc (lambda (name)
  18235. (let ((n (regexp-quote name)))
  18236. (when (org-between-regexps-p
  18237. (concat "^[ \t]*#\\+begin_" n)
  18238. (concat "^[ \t]*#\\+end_" n)
  18239. lim-up lim-down)
  18240. (throw 'exit n))))
  18241. names))
  18242. nil)))
  18243. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18244. "Call `multi-occur' with buffers for all agenda files."
  18245. (interactive "sOrg-files matching: \np")
  18246. (let* ((files (org-agenda-files))
  18247. (tnames (mapcar 'file-truename files))
  18248. (extra org-agenda-text-search-extra-files)
  18249. f)
  18250. (when (eq (car extra) 'agenda-archives)
  18251. (setq extra (cdr extra))
  18252. (setq files (org-add-archive-files files)))
  18253. (while (setq f (pop extra))
  18254. (unless (member (file-truename f) tnames)
  18255. (add-to-list 'files f 'append)
  18256. (add-to-list 'tnames (file-truename f) 'append)))
  18257. (multi-occur
  18258. (mapcar (lambda (x)
  18259. (with-current-buffer
  18260. (or (get-file-buffer x) (find-file-noselect x))
  18261. (widen)
  18262. (current-buffer)))
  18263. files)
  18264. regexp)))
  18265. (if (boundp 'occur-mode-find-occurrence-hook)
  18266. ;; Emacs 23
  18267. (add-hook 'occur-mode-find-occurrence-hook
  18268. (lambda ()
  18269. (when (eq major-mode 'org-mode)
  18270. (org-reveal))))
  18271. ;; Emacs 22
  18272. (defadvice occur-mode-goto-occurrence
  18273. (after org-occur-reveal activate)
  18274. (and (eq major-mode 'org-mode) (org-reveal)))
  18275. (defadvice occur-mode-goto-occurrence-other-window
  18276. (after org-occur-reveal activate)
  18277. (and (eq major-mode 'org-mode) (org-reveal)))
  18278. (defadvice occur-mode-display-occurrence
  18279. (after org-occur-reveal activate)
  18280. (when (eq major-mode 'org-mode)
  18281. (let ((pos (occur-mode-find-occurrence)))
  18282. (with-current-buffer (marker-buffer pos)
  18283. (save-excursion
  18284. (goto-char pos)
  18285. (org-reveal)))))))
  18286. (defun org-occur-link-in-agenda-files ()
  18287. "Create a link and search for it in the agendas.
  18288. The link is not stored in `org-stored-links', it is just created
  18289. for the search purpose."
  18290. (interactive)
  18291. (let ((link (condition-case nil
  18292. (org-store-link nil)
  18293. (error "Unable to create a link to here"))))
  18294. (org-occur-in-agenda-files (regexp-quote link))))
  18295. (defun org-uniquify (list)
  18296. "Remove duplicate elements from LIST."
  18297. (let (res)
  18298. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18299. res))
  18300. (defun org-delete-all (elts list)
  18301. "Remove all elements in ELTS from LIST."
  18302. (while elts
  18303. (setq list (delete (pop elts) list)))
  18304. list)
  18305. (defun org-count (cl-item cl-seq)
  18306. "Count the number of occurrences of ITEM in SEQ.
  18307. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18308. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18309. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18310. (while (< cl-start cl-end)
  18311. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18312. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18313. (setq cl-start (1+ cl-start)))
  18314. cl-count))
  18315. (defun org-remove-if (predicate seq)
  18316. "Remove everything from SEQ that fulfills PREDICATE."
  18317. (let (res e)
  18318. (while seq
  18319. (setq e (pop seq))
  18320. (if (not (funcall predicate e)) (push e res)))
  18321. (nreverse res)))
  18322. (defun org-remove-if-not (predicate seq)
  18323. "Remove everything from SEQ that does not fulfill PREDICATE."
  18324. (let (res e)
  18325. (while seq
  18326. (setq e (pop seq))
  18327. (if (funcall predicate e) (push e res)))
  18328. (nreverse res)))
  18329. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18330. "Reduce two-argument FUNCTION across SEQ.
  18331. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18332. \":initial-value\" removed."
  18333. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18334. (cadr (memq :initial-value cl-keys)))
  18335. (cl-seq (pop cl-seq))
  18336. (t (funcall cl-func)))))
  18337. (while cl-seq
  18338. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18339. cl-accum))
  18340. (defun org-back-over-empty-lines ()
  18341. "Move backwards over whitespace, to the beginning of the first empty line.
  18342. Returns the number of empty lines passed."
  18343. (let ((pos (point)))
  18344. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18345. (skip-chars-backward " \t\n\r")
  18346. (unless (eobp)
  18347. (forward-line -1)))
  18348. (beginning-of-line 2)
  18349. (goto-char (min (point) pos))
  18350. (count-lines (point) pos)))
  18351. (defun org-skip-whitespace ()
  18352. (skip-chars-forward " \t\n\r"))
  18353. (defun org-point-in-group (point group &optional context)
  18354. "Check if POINT is in match-group GROUP.
  18355. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18356. match. If the match group does not exist or point is not inside it,
  18357. return nil."
  18358. (and (match-beginning group)
  18359. (>= point (match-beginning group))
  18360. (<= point (match-end group))
  18361. (if context
  18362. (list context (match-beginning group) (match-end group))
  18363. t)))
  18364. (defun org-switch-to-buffer-other-window (&rest args)
  18365. "Switch to buffer in a second window on the current frame.
  18366. In particular, do not allow pop-up frames.
  18367. Returns the newly created buffer."
  18368. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18369. special-display-function)
  18370. (apply 'switch-to-buffer-other-window args)))
  18371. (defun org-combine-plists (&rest plists)
  18372. "Create a single property list from all plists in PLISTS.
  18373. The process starts by copying the first list, and then setting properties
  18374. from the other lists. Settings in the last list are the most significant
  18375. ones and overrule settings in the other lists."
  18376. (let ((rtn (copy-sequence (pop plists)))
  18377. p v ls)
  18378. (while plists
  18379. (setq ls (pop plists))
  18380. (while ls
  18381. (setq p (pop ls) v (pop ls))
  18382. (setq rtn (plist-put rtn p v))))
  18383. rtn))
  18384. (defun org-move-line-down (arg)
  18385. "Move the current line down. With prefix argument, move it past ARG lines."
  18386. (interactive "p")
  18387. (let ((col (current-column))
  18388. beg end pos)
  18389. (beginning-of-line 1) (setq beg (point))
  18390. (beginning-of-line 2) (setq end (point))
  18391. (beginning-of-line (+ 1 arg))
  18392. (setq pos (move-marker (make-marker) (point)))
  18393. (insert (delete-and-extract-region beg end))
  18394. (goto-char pos)
  18395. (org-move-to-column col)))
  18396. (defun org-move-line-up (arg)
  18397. "Move the current line up. With prefix argument, move it past ARG lines."
  18398. (interactive "p")
  18399. (let ((col (current-column))
  18400. beg end pos)
  18401. (beginning-of-line 1) (setq beg (point))
  18402. (beginning-of-line 2) (setq end (point))
  18403. (beginning-of-line (- arg))
  18404. (setq pos (move-marker (make-marker) (point)))
  18405. (insert (delete-and-extract-region beg end))
  18406. (goto-char pos)
  18407. (org-move-to-column col)))
  18408. (defun org-replace-escapes (string table)
  18409. "Replace %-escapes in STRING with values in TABLE.
  18410. TABLE is an association list with keys like \"%a\" and string values.
  18411. The sequences in STRING may contain normal field width and padding information,
  18412. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18413. so values can contain further %-escapes if they are define later in TABLE."
  18414. (let ((tbl (copy-alist table))
  18415. (case-fold-search nil)
  18416. (pchg 0)
  18417. e re rpl)
  18418. (while (setq e (pop tbl))
  18419. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18420. (when (and (cdr e) (string-match re (cdr e)))
  18421. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18422. (safe "SREF"))
  18423. (add-text-properties 0 3 (list 'sref sref) safe)
  18424. (setcdr e (replace-match safe t t (cdr e)))))
  18425. (while (string-match re string)
  18426. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18427. (cdr e)))
  18428. (setq string (replace-match rpl t t string))))
  18429. (while (setq pchg (next-property-change pchg string))
  18430. (let ((sref (get-text-property pchg 'sref string)))
  18431. (when (and sref (string-match "SREF" string pchg))
  18432. (setq string (replace-match sref t t string)))))
  18433. string))
  18434. (defun org-sublist (list start end)
  18435. "Return a section of LIST, from START to END.
  18436. Counting starts at 1."
  18437. (let (rtn (c start))
  18438. (setq list (nthcdr (1- start) list))
  18439. (while (and list (<= c end))
  18440. (push (pop list) rtn)
  18441. (setq c (1+ c)))
  18442. (nreverse rtn)))
  18443. (defun org-find-base-buffer-visiting (file)
  18444. "Like `find-buffer-visiting' but always return the base buffer and
  18445. not an indirect buffer."
  18446. (let ((buf (or (get-file-buffer file)
  18447. (find-buffer-visiting file))))
  18448. (if buf
  18449. (or (buffer-base-buffer buf) buf)
  18450. nil)))
  18451. (defun org-image-file-name-regexp (&optional extensions)
  18452. "Return regexp matching the file names of images.
  18453. If EXTENSIONS is given, only match these."
  18454. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18455. (image-file-name-regexp)
  18456. (let ((image-file-name-extensions
  18457. (or extensions
  18458. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18459. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18460. (concat "\\."
  18461. (regexp-opt (nconc (mapcar 'upcase
  18462. image-file-name-extensions)
  18463. image-file-name-extensions)
  18464. t)
  18465. "\\'"))))
  18466. (defun org-file-image-p (file &optional extensions)
  18467. "Return non-nil if FILE is an image."
  18468. (save-match-data
  18469. (string-match (org-image-file-name-regexp extensions) file)))
  18470. (defun org-get-cursor-date ()
  18471. "Return the date at cursor in as a time.
  18472. This works in the calendar and in the agenda, anywhere else it just
  18473. returns the current time."
  18474. (let (date day defd)
  18475. (cond
  18476. ((eq major-mode 'calendar-mode)
  18477. (setq date (calendar-cursor-to-date)
  18478. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18479. ((eq major-mode 'org-agenda-mode)
  18480. (setq day (get-text-property (point) 'day))
  18481. (if day
  18482. (setq date (calendar-gregorian-from-absolute day)
  18483. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18484. (nth 2 date))))))
  18485. (or defd (current-time))))
  18486. (defvar org-agenda-action-marker (make-marker)
  18487. "Marker pointing to the entry for the next agenda action.")
  18488. (defun org-mark-entry-for-agenda-action ()
  18489. "Mark the current entry as target of an agenda action.
  18490. Agenda actions are actions executed from the agenda with the key `k',
  18491. which make use of the date at the cursor."
  18492. (interactive)
  18493. (move-marker org-agenda-action-marker
  18494. (save-excursion (org-back-to-heading t) (point))
  18495. (current-buffer))
  18496. (message
  18497. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18498. (defun org-mark-subtree ()
  18499. "Mark the current subtree.
  18500. This puts point at the start of the current subtree, and mark at the end.
  18501. If point is in an inline task, mark that task instead."
  18502. (interactive)
  18503. (let ((inline-task-p
  18504. (and (featurep 'org-inlinetask)
  18505. (org-inlinetask-in-task-p)))
  18506. (beg))
  18507. ;; Get beginning of subtree
  18508. (cond
  18509. (inline-task-p (org-inlinetask-goto-beginning))
  18510. ((org-at-heading-p) (beginning-of-line))
  18511. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18512. (setq beg (point))
  18513. ;; Get end of it
  18514. (if inline-task-p
  18515. (org-inlinetask-goto-end)
  18516. (org-end-of-subtree))
  18517. ;; Mark zone
  18518. (push-mark (point) nil t)
  18519. (goto-char beg)))
  18520. ;;; Paragraph filling stuff.
  18521. ;; We want this to be just right, so use the full arsenal.
  18522. (defun org-indent-line-function ()
  18523. "Indent line depending on context."
  18524. (interactive)
  18525. (let* ((pos (point))
  18526. (itemp (org-at-item-p))
  18527. (case-fold-search t)
  18528. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18529. (inline-task-p (and (featurep 'org-inlinetask)
  18530. (org-inlinetask-in-task-p)))
  18531. (inline-re (and inline-task-p
  18532. (org-inlinetask-outline-regexp)))
  18533. column)
  18534. (beginning-of-line 1)
  18535. (cond
  18536. ;; Comments
  18537. ((looking-at "# ") (setq column 0))
  18538. ;; Headings
  18539. ((looking-at org-outline-regexp) (setq column 0))
  18540. ;; Included files
  18541. ((looking-at "#\\+include:") (setq column 0))
  18542. ;; Footnote definition
  18543. ((looking-at org-footnote-definition-re) (setq column 0))
  18544. ;; Literal examples
  18545. ((looking-at "[ \t]*:\\( \\|$\\)")
  18546. (setq column (org-get-indentation))) ; do nothing
  18547. ;; Lists
  18548. ((ignore-errors (goto-char (org-in-item-p)))
  18549. (setq column (if itemp
  18550. (org-get-indentation)
  18551. (org-list-item-body-column (point))))
  18552. (goto-char pos))
  18553. ;; Drawers
  18554. ((and (looking-at "[ \t]*:END:")
  18555. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18556. (save-excursion
  18557. (goto-char (1- (match-beginning 1)))
  18558. (setq column (current-column))))
  18559. ;; Special blocks
  18560. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18561. (save-excursion
  18562. (re-search-backward
  18563. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18564. (setq column (org-get-indentation (match-string 0))))
  18565. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18566. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18567. (save-excursion
  18568. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18569. (setq column
  18570. (cond ((equal (downcase (match-string 1)) "src")
  18571. ;; src blocks: let `org-edit-src-exit' handle them
  18572. (org-get-indentation))
  18573. ((equal (downcase (match-string 1)) "example")
  18574. (max (org-get-indentation)
  18575. (org-get-indentation (match-string 0))))
  18576. (t
  18577. (org-get-indentation (match-string 0))))))
  18578. ;; This line has nothing special, look at the previous relevant
  18579. ;; line to compute indentation
  18580. (t
  18581. (beginning-of-line 0)
  18582. (while (and (not (bobp))
  18583. (not (looking-at org-drawer-regexp))
  18584. ;; When point started in an inline task, do not move
  18585. ;; above task starting line.
  18586. (not (and inline-task-p (looking-at inline-re)))
  18587. ;; Skip drawers, blocks, empty lines, verbatim,
  18588. ;; comments, tables, footnotes definitions, lists,
  18589. ;; inline tasks.
  18590. (or (and (looking-at "[ \t]*:END:")
  18591. (re-search-backward org-drawer-regexp nil t))
  18592. (and (looking-at "[ \t]*#\\+end_")
  18593. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18594. (looking-at "[ \t]*[\n:#|]")
  18595. (looking-at org-footnote-definition-re)
  18596. (and (ignore-errors (goto-char (org-in-item-p)))
  18597. (goto-char
  18598. (org-list-get-top-point (org-list-struct))))
  18599. (and (not inline-task-p)
  18600. (featurep 'org-inlinetask)
  18601. (org-inlinetask-in-task-p)
  18602. (or (org-inlinetask-goto-beginning) t))))
  18603. (beginning-of-line 0))
  18604. (cond
  18605. ;; There was an heading above.
  18606. ((looking-at "\\*+[ \t]+")
  18607. (if (not org-adapt-indentation)
  18608. (setq column 0)
  18609. (goto-char (match-end 0))
  18610. (setq column (current-column))))
  18611. ;; A drawer had started and is unfinished
  18612. ((looking-at org-drawer-regexp)
  18613. (goto-char (1- (match-beginning 1)))
  18614. (setq column (current-column)))
  18615. ;; Else, nothing noticeable found: get indentation and go on.
  18616. (t (setq column (org-get-indentation))))))
  18617. ;; Now apply indentation and move cursor accordingly
  18618. (goto-char pos)
  18619. (if (<= (current-column) (current-indentation))
  18620. (org-indent-line-to column)
  18621. (save-excursion (org-indent-line-to column)))
  18622. ;; Special polishing for properties, see `org-property-format'
  18623. (setq column (current-column))
  18624. (beginning-of-line 1)
  18625. (if (looking-at
  18626. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18627. (replace-match (concat (match-string 1)
  18628. (format org-property-format
  18629. (match-string 2) (match-string 3)))
  18630. t t))
  18631. (org-move-to-column column)))
  18632. (defun org-indent-drawer ()
  18633. "Indent the drawer at point."
  18634. (interactive)
  18635. (let ((p (point))
  18636. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18637. (match-end 0)))
  18638. (folded
  18639. (save-excursion
  18640. (end-of-line)
  18641. (when (overlays-at (point))
  18642. (member 'invisible (overlay-properties
  18643. (car (overlays-at (point)))))))))
  18644. (when folded (org-cycle))
  18645. (indent-for-tab-command)
  18646. (while (and (move-beginning-of-line 2) (< (point) e))
  18647. (indent-for-tab-command))
  18648. (goto-char p)
  18649. (when folded (org-cycle)))
  18650. (message "Drawer at point indented"))
  18651. (defun org-indent-block ()
  18652. "Indent the block at point."
  18653. (interactive)
  18654. (let ((p (point))
  18655. (case-fold-search t)
  18656. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18657. (match-end 0)))
  18658. (folded
  18659. (save-excursion
  18660. (end-of-line)
  18661. (when (overlays-at (point))
  18662. (member 'invisible (overlay-properties
  18663. (car (overlays-at (point)))))))))
  18664. (when folded (org-cycle))
  18665. (indent-for-tab-command)
  18666. (while (and (move-beginning-of-line 2) (< (point) e))
  18667. (indent-for-tab-command))
  18668. (goto-char p)
  18669. (when folded (org-cycle)))
  18670. (message "Block at point indented"))
  18671. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18672. "Variable to store copy of `adaptive-fill-regexp'.
  18673. Since `adaptive-fill-regexp' is set to never match, we need to
  18674. store a backup of its value before entering `org-mode' so that
  18675. the functionality can be provided as a fall-back.")
  18676. (defun org-set-autofill-regexps ()
  18677. (interactive)
  18678. ;; In the paragraph separator we include headlines, because filling
  18679. ;; text in a line directly attached to a headline would otherwise
  18680. ;; fill the headline as well.
  18681. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18682. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18683. ;; The paragraph starter includes hand-formatted lists.
  18684. (org-set-local
  18685. 'paragraph-start
  18686. (concat
  18687. "\f" "\\|"
  18688. "[ ]*$" "\\|"
  18689. org-outline-regexp "\\|"
  18690. "[ \t]*#" "\\|"
  18691. (org-item-re) "\\|"
  18692. "[ \t]*[:|]" "\\|"
  18693. "\\$\\$" "\\|"
  18694. "\\\\\\(begin\\|end\\|[][]\\)"))
  18695. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18696. ;; But only if the user has not turned off tables or fixed-width regions
  18697. (org-set-local
  18698. 'auto-fill-inhibit-regexp
  18699. (concat org-outline-regexp
  18700. "\\|#\\+"
  18701. "\\|[ \t]*" org-keyword-time-regexp
  18702. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18703. (concat
  18704. "\\|[ \t]*["
  18705. (if org-enable-table-editor "|" "")
  18706. (if org-enable-fixed-width-editor ":" "")
  18707. "]"))))
  18708. ;; We use our own fill-paragraph function, to make sure that tables
  18709. ;; and fixed-width regions are not wrapped. That function will pass
  18710. ;; through to `fill-paragraph' when appropriate.
  18711. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18712. ;; Prevent auto-fill from inserting unwanted new items.
  18713. (if (boundp 'fill-nobreak-predicate)
  18714. (org-set-local
  18715. 'fill-nobreak-predicate
  18716. (org-uniquify
  18717. (append fill-nobreak-predicate
  18718. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  18719. ;; Adaptive filling: To get full control, first make sure that
  18720. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18721. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18722. (org-set-local 'org-adaptive-fill-regexp-backup
  18723. adaptive-fill-regexp))
  18724. (org-set-local 'adaptive-fill-regexp "\000")
  18725. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18726. (org-set-local 'adaptive-fill-function
  18727. 'org-adaptive-fill-function)
  18728. (org-set-local
  18729. 'align-mode-rules-list
  18730. '((org-in-buffer-settings
  18731. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18732. (modes . '(org-mode))))))
  18733. (defun org-fill-item-nobreak-p ()
  18734. "Non-nil when a line break at point would insert a new item."
  18735. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18736. (defun org-fill-line-break-nobreak-p ()
  18737. "Non-nil when a line break at point would create an Org line break."
  18738. (save-excursion
  18739. (skip-chars-backward "[ \t]")
  18740. (skip-chars-backward "\\\\")
  18741. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  18742. (defun org-fill-paragraph (&optional justify)
  18743. "Re-align a table, pass through to fill-paragraph if no table."
  18744. (let ((table-p (org-at-table-p))
  18745. (table.el-p (org-at-table.el-p))
  18746. (itemp (org-in-item-p)))
  18747. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18748. (save-excursion (goto-char (point-at-bol))
  18749. (looking-at org-outline-regexp)))
  18750. t) ; skip headlines
  18751. (table.el-p t) ; skip table.el tables
  18752. (table-p (org-table-align) t) ; align Org tables
  18753. (itemp ; align text in items
  18754. (let* ((struct (save-excursion (goto-char itemp)
  18755. (org-list-struct)))
  18756. (parents (org-list-parents-alist struct))
  18757. (children (org-list-get-children itemp struct parents))
  18758. beg end prev next prefix)
  18759. ;; Determine in which part of item point is: before
  18760. ;; first child, after last child, between two
  18761. ;; sub-lists, or simply in item if there's no child.
  18762. (cond
  18763. ((not children)
  18764. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18765. beg itemp
  18766. end (org-list-get-item-end itemp struct)))
  18767. ((< (point) (setq next (car children)))
  18768. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18769. beg itemp
  18770. end next))
  18771. ((> (point) (setq prev (car (last children))))
  18772. (setq beg (org-list-get-item-end prev struct)
  18773. end (org-list-get-item-end itemp struct)
  18774. prefix (save-excursion
  18775. (goto-char beg)
  18776. (skip-chars-forward " \t")
  18777. (make-string (current-column) ?\ ))))
  18778. (t (catch 'exit
  18779. (while (setq next (pop children))
  18780. (if (> (point) next)
  18781. (setq prev next)
  18782. (setq beg (org-list-get-item-end prev struct)
  18783. end next
  18784. prefix (save-excursion
  18785. (goto-char beg)
  18786. (skip-chars-forward " \t")
  18787. (make-string (current-column) ?\ )))
  18788. (throw 'exit nil))))))
  18789. ;; Use `fill-paragraph' with buffer narrowed to item
  18790. ;; without any child, and with our computed PREFIX.
  18791. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18792. (save-restriction
  18793. (narrow-to-region beg end)
  18794. (save-excursion (fill-paragraph justify)))) t))
  18795. ;; Special case where point is not in a list but is on
  18796. ;; a paragraph adjacent to a list: make sure this paragraph
  18797. ;; doesn't get merged with the end of the list by narrowing
  18798. ;; buffer first.
  18799. ((save-excursion (forward-paragraph -1)
  18800. (setq itemp (org-in-item-p)))
  18801. (let ((struct (save-excursion (goto-char itemp)
  18802. (org-list-struct))))
  18803. (save-restriction
  18804. (narrow-to-region (org-list-get-bottom-point struct)
  18805. (save-excursion (forward-paragraph 1)
  18806. (point)))
  18807. (fill-paragraph justify) t)))
  18808. ;; Else simply call `fill-paragraph'.
  18809. (t nil))))
  18810. ;; For reference, this is the default value of adaptive-fill-regexp
  18811. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18812. (defun org-adaptive-fill-function ()
  18813. "Return a fill prefix for org-mode files."
  18814. (let (itemp)
  18815. (save-excursion
  18816. (cond
  18817. ;; Comment line
  18818. ((looking-at "#[ \t]+")
  18819. (match-string-no-properties 0))
  18820. ;; Plain list item
  18821. ((org-at-item-p)
  18822. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18823. ;; Point is in a list after `backward-paragraph': original
  18824. ;; point wasn't in the list, or filling would have been taken
  18825. ;; care of by `org-auto-fill-function', but the list and the
  18826. ;; real paragraph are not separated by a blank line. Thus, move
  18827. ;; point after the list to go back to real paragraph and
  18828. ;; determine fill-prefix.
  18829. ((setq itemp (org-in-item-p))
  18830. (goto-char itemp)
  18831. (let* ((struct (org-list-struct))
  18832. (bottom (org-list-get-bottom-point struct)))
  18833. (goto-char bottom)
  18834. (make-string (org-get-indentation) ?\ )))
  18835. ;; Other text
  18836. ((looking-at org-adaptive-fill-regexp-backup)
  18837. (match-string-no-properties 0))))))
  18838. (defun org-auto-fill-function ()
  18839. "Auto-fill function."
  18840. (let (itemp prefix)
  18841. ;; When in a list, compute an appropriate fill-prefix and make
  18842. ;; sure it will be used by `do-auto-fill'.
  18843. (if (setq itemp (org-in-item-p))
  18844. (progn
  18845. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18846. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18847. (do-auto-fill)))
  18848. ;; Else just use `do-auto-fill'.
  18849. (do-auto-fill))))
  18850. ;;; Other stuff.
  18851. (defun org-toggle-fixed-width-section (arg)
  18852. "Toggle the fixed-width export.
  18853. If there is no active region, the QUOTE keyword at the current headline is
  18854. inserted or removed. When present, it causes the text between this headline
  18855. and the next to be exported as fixed-width text, and unmodified.
  18856. If there is an active region, this command adds or removes a colon as the
  18857. first character of this line. If the first character of a line is a colon,
  18858. this line is also exported in fixed-width font."
  18859. (interactive "P")
  18860. (let* ((cc 0)
  18861. (regionp (org-region-active-p))
  18862. (beg (if regionp (region-beginning) (point)))
  18863. (end (if regionp (region-end)))
  18864. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18865. (case-fold-search nil)
  18866. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18867. off)
  18868. (if regionp
  18869. (save-excursion
  18870. (goto-char beg)
  18871. (setq cc (current-column))
  18872. (beginning-of-line 1)
  18873. (setq off (looking-at re))
  18874. (while (> nlines 0)
  18875. (setq nlines (1- nlines))
  18876. (beginning-of-line 1)
  18877. (cond
  18878. (arg
  18879. (org-move-to-column cc t)
  18880. (insert ": \n")
  18881. (forward-line -1))
  18882. ((and off (looking-at re))
  18883. (replace-match "" t t nil 1))
  18884. ((not off) (org-move-to-column cc t) (insert ": ")))
  18885. (forward-line 1)))
  18886. (save-excursion
  18887. (org-back-to-heading)
  18888. (cond
  18889. ((looking-at (format org-heading-keyword-regexp-format
  18890. org-quote-string))
  18891. (goto-char (match-end 1))
  18892. (looking-at (concat " +" org-quote-string))
  18893. (replace-match "" t t)
  18894. (when (eolp) (insert " ")))
  18895. ((looking-at org-outline-regexp)
  18896. (goto-char (match-end 0))
  18897. (insert org-quote-string " ")))))))
  18898. (defun org-reftex-citation ()
  18899. "Use reftex-citation to insert a citation into the buffer.
  18900. This looks for a line like
  18901. #+BIBLIOGRAPHY: foo plain option:-d
  18902. and derives from it that foo.bib is the bibliography file relevant
  18903. for this document. It then installs the necessary environment for RefTeX
  18904. to work in this buffer and calls `reftex-citation' to insert a citation
  18905. into the buffer.
  18906. Export of such citations to both LaTeX and HTML is handled by the contributed
  18907. package org-exp-bibtex by Taru Karttunen."
  18908. (interactive)
  18909. (let ((reftex-docstruct-symbol 'rds)
  18910. (reftex-cite-format "\\cite{%l}")
  18911. rds bib)
  18912. (save-excursion
  18913. (save-restriction
  18914. (widen)
  18915. (let ((case-fold-search t)
  18916. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18917. (if (not (save-excursion
  18918. (or (re-search-forward re nil t)
  18919. (re-search-backward re nil t))))
  18920. (error "No bibliography defined in file")
  18921. (setq bib (concat (match-string 1) ".bib")
  18922. rds (list (list 'bib bib)))))))
  18923. (call-interactively 'reftex-citation)))
  18924. ;;;; Functions extending outline functionality
  18925. (defun org-beginning-of-line (&optional arg)
  18926. "Go to the beginning of the current line. If that is invisible, continue
  18927. to a visible line beginning. This makes the function of C-a more intuitive.
  18928. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18929. first attempt, and only move to after the tags when the cursor is already
  18930. beyond the end of the headline."
  18931. (interactive "P")
  18932. (let ((pos (point))
  18933. (special (if (consp org-special-ctrl-a/e)
  18934. (car org-special-ctrl-a/e)
  18935. org-special-ctrl-a/e))
  18936. refpos)
  18937. (if (org-bound-and-true-p line-move-visual)
  18938. (beginning-of-visual-line 1)
  18939. (beginning-of-line 1))
  18940. (if (and arg (fboundp 'move-beginning-of-line))
  18941. (call-interactively 'move-beginning-of-line)
  18942. (if (bobp)
  18943. nil
  18944. (backward-char 1)
  18945. (if (org-truely-invisible-p)
  18946. (while (and (not (bobp)) (org-truely-invisible-p))
  18947. (backward-char 1)
  18948. (beginning-of-line 1))
  18949. (forward-char 1))))
  18950. (when special
  18951. (cond
  18952. ((and (looking-at org-complex-heading-regexp)
  18953. (= (char-after (match-end 1)) ?\ ))
  18954. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18955. (point-at-eol)))
  18956. (goto-char
  18957. (if (eq special t)
  18958. (cond ((> pos refpos) refpos)
  18959. ((= pos (point)) refpos)
  18960. (t (point)))
  18961. (cond ((> pos (point)) (point))
  18962. ((not (eq last-command this-command)) (point))
  18963. (t refpos)))))
  18964. ((org-at-item-p)
  18965. ;; Being at an item and not looking at an the item means point
  18966. ;; was previously moved to beginning of a visual line, whiche
  18967. ;; doesn't contain the item. Therefore, do nothing special,
  18968. ;; just stay here.
  18969. (when (looking-at org-list-full-item-re)
  18970. ;; Set special position at first white space character after
  18971. ;; bullet, and check-box, if any.
  18972. (let ((after-bullet
  18973. (let ((box (match-end 3)))
  18974. (if (not box) (match-end 1)
  18975. (let ((after (char-after box)))
  18976. (if (and after (= after ? )) (1+ box) box))))))
  18977. ;; Special case: Move point to special position when
  18978. ;; currently after it or at beginning of line.
  18979. (if (eq special t)
  18980. (when (or (> pos after-bullet) (= (point) pos))
  18981. (goto-char after-bullet))
  18982. ;; Reversed case: Move point to special position when
  18983. ;; point was already at beginning of line and command is
  18984. ;; repeated.
  18985. (when (and (= (point) pos) (eq last-command this-command))
  18986. (goto-char after-bullet))))))))
  18987. (org-no-warnings
  18988. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18989. (defun org-end-of-line (&optional arg)
  18990. "Go to the end of the line.
  18991. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18992. first attempt, and only move to after the tags when the cursor is already
  18993. beyond the end of the headline."
  18994. (interactive "P")
  18995. (let ((special (if (consp org-special-ctrl-a/e)
  18996. (cdr org-special-ctrl-a/e)
  18997. org-special-ctrl-a/e)))
  18998. (cond
  18999. ((or (not special) arg
  19000. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19001. (call-interactively
  19002. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19003. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19004. (t 'end-of-line))))
  19005. ((org-at-heading-p)
  19006. (let ((pos (point)))
  19007. (beginning-of-line 1)
  19008. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19009. (if (eq special t)
  19010. (if (or (< pos (match-beginning 1))
  19011. (= pos (match-end 0)))
  19012. (goto-char (match-beginning 1))
  19013. (goto-char (match-end 0)))
  19014. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19015. (goto-char (match-end 0))
  19016. (goto-char (match-beginning 1))))
  19017. (call-interactively (if (fboundp 'move-end-of-line)
  19018. 'move-end-of-line
  19019. 'end-of-line)))))
  19020. ((org-at-drawer-p)
  19021. (move-end-of-line 1)
  19022. (when (overlays-at (1- (point))) (backward-char 1)))
  19023. ;; At an item: Move before any hidden text.
  19024. (t (call-interactively
  19025. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19026. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19027. (t 'end-of-line)))))
  19028. (org-no-warnings
  19029. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19030. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19031. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19032. (defun org-backward-sentence (&optional arg)
  19033. "Go to beginning of sentence, or beginning of table field.
  19034. This will call `backward-sentence' or `org-table-beginning-of-field',
  19035. depending on context."
  19036. (interactive "P")
  19037. (cond
  19038. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19039. (t (call-interactively 'backward-sentence))))
  19040. (defun org-forward-sentence (&optional arg)
  19041. "Go to end of sentence, or end of table field.
  19042. This will call `forward-sentence' or `org-table-end-of-field',
  19043. depending on context."
  19044. (interactive "P")
  19045. (cond
  19046. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19047. (t (call-interactively 'forward-sentence))))
  19048. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19049. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19050. (defun org-kill-line (&optional arg)
  19051. "Kill line, to tags or end of line."
  19052. (interactive "P")
  19053. (cond
  19054. ((or (not org-special-ctrl-k)
  19055. (bolp)
  19056. (not (org-at-heading-p)))
  19057. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19058. org-ctrl-k-protect-subtree)
  19059. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19060. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19061. (error "C-k aborted - would kill hidden subtree")))
  19062. (call-interactively
  19063. (if visual-line-mode 'kill-visual-line 'kill-line)))
  19064. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19065. (kill-region (point) (match-beginning 1))
  19066. (org-set-tags nil t))
  19067. (t (kill-region (point) (point-at-eol)))))
  19068. (define-key org-mode-map "\C-k" 'org-kill-line)
  19069. (defun org-yank (&optional arg)
  19070. "Yank. If the kill is a subtree, treat it specially.
  19071. This command will look at the current kill and check if is a single
  19072. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19073. cursor is at the beginning of a line or after the stars of a currently
  19074. empty headline, then the yank is handled specially. How exactly depends
  19075. on the value of the following variables, both set by default.
  19076. org-yank-folded-subtrees
  19077. When set, the subtree(s) will be folded after insertion, but only
  19078. if doing so would now swallow text after the yanked text.
  19079. org-yank-adjusted-subtrees
  19080. When set, the subtree will be promoted or demoted in order to
  19081. fit into the local outline tree structure, which means that the level
  19082. will be adjusted so that it becomes the smaller one of the two
  19083. *visible* surrounding headings.
  19084. Any prefix to this command will cause `yank' to be called directly with
  19085. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19086. will just
  19087. plainly yank the text as it is.
  19088. \[1] The test checks if the first non-white line is a heading
  19089. and if there are no other headings with fewer stars."
  19090. (interactive "P")
  19091. (org-yank-generic 'yank arg))
  19092. (defun org-yank-generic (command arg)
  19093. "Perform some yank-like command.
  19094. This function implements the behavior described in the `org-yank'
  19095. documentation. However, it has been generalized to work for any
  19096. interactive command with similar behavior."
  19097. ;; pretend to be command COMMAND
  19098. (setq this-command command)
  19099. (if arg
  19100. (call-interactively command)
  19101. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19102. (and (org-kill-is-subtree-p)
  19103. (or (bolp)
  19104. (and (looking-at "[ \t]*$")
  19105. (string-match
  19106. "\\`\\*+\\'"
  19107. (buffer-substring (point-at-bol) (point)))))))
  19108. swallowp)
  19109. (cond
  19110. ((and subtreep org-yank-folded-subtrees)
  19111. (let ((beg (point))
  19112. end)
  19113. (if (and subtreep org-yank-adjusted-subtrees)
  19114. (org-paste-subtree nil nil 'for-yank)
  19115. (call-interactively command))
  19116. (setq end (point))
  19117. (goto-char beg)
  19118. (when (and (bolp) subtreep
  19119. (not (setq swallowp
  19120. (org-yank-folding-would-swallow-text beg end))))
  19121. (org-with-limited-levels
  19122. (or (looking-at org-outline-regexp)
  19123. (re-search-forward org-outline-regexp-bol end t))
  19124. (while (and (< (point) end) (looking-at org-outline-regexp))
  19125. (hide-subtree)
  19126. (org-cycle-show-empty-lines 'folded)
  19127. (condition-case nil
  19128. (outline-forward-same-level 1)
  19129. (error (goto-char end))))))
  19130. (when swallowp
  19131. (message
  19132. "Inserted text not folded because that would swallow text"))
  19133. (goto-char end)
  19134. (skip-chars-forward " \t\n\r")
  19135. (beginning-of-line 1)
  19136. (push-mark beg 'nomsg)))
  19137. ((and subtreep org-yank-adjusted-subtrees)
  19138. (let ((beg (point-at-bol)))
  19139. (org-paste-subtree nil nil 'for-yank)
  19140. (push-mark beg 'nomsg)))
  19141. (t
  19142. (call-interactively command))))))
  19143. (defun org-yank-folding-would-swallow-text (beg end)
  19144. "Would hide-subtree at BEG swallow any text after END?"
  19145. (let (level)
  19146. (org-with-limited-levels
  19147. (save-excursion
  19148. (goto-char beg)
  19149. (when (or (looking-at org-outline-regexp)
  19150. (re-search-forward org-outline-regexp-bol end t))
  19151. (setq level (org-outline-level)))
  19152. (goto-char end)
  19153. (skip-chars-forward " \t\r\n\v\f")
  19154. (if (or (eobp)
  19155. (and (bolp) (looking-at org-outline-regexp)
  19156. (<= (org-outline-level) level)))
  19157. nil ; Nothing would be swallowed
  19158. t))))) ; something would swallow
  19159. (define-key org-mode-map "\C-y" 'org-yank)
  19160. (defun org-truely-invisible-p ()
  19161. "Check if point is at a character currently not visible.
  19162. This version does not only check the character property, but also
  19163. `visible-mode'."
  19164. ;; Early versions of noutline don't have `outline-invisible-p'.
  19165. (if (org-bound-and-true-p visible-mode)
  19166. nil
  19167. (outline-invisible-p)))
  19168. (defun org-invisible-p2 ()
  19169. "Check if point is at a character currently not visible."
  19170. (save-excursion
  19171. (if (and (eolp) (not (bobp))) (backward-char 1))
  19172. ;; Early versions of noutline don't have `outline-invisible-p'.
  19173. (outline-invisible-p)))
  19174. (defun org-back-to-heading (&optional invisible-ok)
  19175. "Call `outline-back-to-heading', but provide a better error message."
  19176. (condition-case nil
  19177. (outline-back-to-heading invisible-ok)
  19178. (error (error "Before first headline at position %d in buffer %s"
  19179. (point) (current-buffer)))))
  19180. (defun org-beginning-of-defun ()
  19181. "Go to the beginning of the subtree, i.e. back to the heading."
  19182. (org-back-to-heading))
  19183. (defun org-end-of-defun ()
  19184. "Go to the end of the subtree."
  19185. (org-end-of-subtree nil t))
  19186. (defun org-before-first-heading-p ()
  19187. "Before first heading?"
  19188. (save-excursion
  19189. (end-of-line)
  19190. (null (re-search-backward org-outline-regexp-bol nil t))))
  19191. (defun org-at-heading-p (&optional ignored)
  19192. (outline-on-heading-p t))
  19193. ;; Compatibility alias with Org versions < 7.8.03
  19194. (defalias 'org-on-heading-p 'org-at-heading-p)
  19195. (defun org-at-drawer-p nil
  19196. "Is cursor at a drawer keyword?"
  19197. (save-excursion
  19198. (move-beginning-of-line 1)
  19199. (looking-at org-drawer-regexp)))
  19200. (defun org-at-block-p nil
  19201. "Is cursor at a block keyword?"
  19202. (save-excursion
  19203. (move-beginning-of-line 1)
  19204. (looking-at org-block-regexp)))
  19205. (defun org-point-at-end-of-empty-headline ()
  19206. "If point is at the end of an empty headline, return t, else nil.
  19207. If the heading only contains a TODO keyword, it is still still considered
  19208. empty."
  19209. (and (looking-at "[ \t]*$")
  19210. (save-excursion
  19211. (beginning-of-line 1)
  19212. (let ((case-fold-search nil))
  19213. (looking-at org-todo-line-regexp)))
  19214. (string= (match-string 3) "")))
  19215. (defun org-at-heading-or-item-p ()
  19216. (or (org-at-heading-p) (org-at-item-p)))
  19217. (defun org-at-target-p ()
  19218. (or (org-in-regexp org-radio-target-regexp)
  19219. (org-in-regexp org-target-regexp)))
  19220. ;; Compatibility alias with Org versions < 7.8.03
  19221. (defalias 'org-on-target-p 'org-at-target-p)
  19222. (defun org-up-heading-all (arg)
  19223. "Move to the heading line of which the present line is a subheading.
  19224. This function considers both visible and invisible heading lines.
  19225. With argument, move up ARG levels."
  19226. (if (fboundp 'outline-up-heading-all)
  19227. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19228. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19229. (defun org-up-heading-safe ()
  19230. "Move to the heading line of which the present line is a subheading.
  19231. This version will not throw an error. It will return the level of the
  19232. headline found, or nil if no higher level is found.
  19233. Also, this function will be a lot faster than `outline-up-heading',
  19234. because it relies on stars being the outline starters. This can really
  19235. make a significant difference in outlines with very many siblings."
  19236. (let (start-level re)
  19237. (org-back-to-heading t)
  19238. (setq start-level (funcall outline-level))
  19239. (if (equal start-level 1)
  19240. nil
  19241. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19242. (if (re-search-backward re nil t)
  19243. (funcall outline-level)))))
  19244. (defun org-first-sibling-p ()
  19245. "Is this heading the first child of its parents?"
  19246. (interactive)
  19247. (let ((re org-outline-regexp-bol)
  19248. level l)
  19249. (unless (org-at-heading-p t)
  19250. (error "Not at a heading"))
  19251. (setq level (funcall outline-level))
  19252. (save-excursion
  19253. (if (not (re-search-backward re nil t))
  19254. t
  19255. (setq l (funcall outline-level))
  19256. (< l level)))))
  19257. (defun org-goto-sibling (&optional previous)
  19258. "Goto the next sibling, even if it is invisible.
  19259. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19260. when a sibling was found. When none is found, return nil and don't
  19261. move point."
  19262. (let ((fun (if previous 're-search-backward 're-search-forward))
  19263. (pos (point))
  19264. (re org-outline-regexp-bol)
  19265. level l)
  19266. (when (condition-case nil (org-back-to-heading t) (error nil))
  19267. (setq level (funcall outline-level))
  19268. (catch 'exit
  19269. (or previous (forward-char 1))
  19270. (while (funcall fun re nil t)
  19271. (setq l (funcall outline-level))
  19272. (when (< l level) (goto-char pos) (throw 'exit nil))
  19273. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19274. (goto-char pos)
  19275. nil))))
  19276. (defun org-show-siblings ()
  19277. "Show all siblings of the current headline."
  19278. (save-excursion
  19279. (while (org-goto-sibling) (org-flag-heading nil)))
  19280. (save-excursion
  19281. (while (org-goto-sibling 'previous)
  19282. (org-flag-heading nil))))
  19283. (defun org-goto-first-child ()
  19284. "Goto the first child, even if it is invisible.
  19285. Return t when a child was found. Otherwise don't move point and
  19286. return nil."
  19287. (let (level (pos (point)) (re org-outline-regexp-bol))
  19288. (when (condition-case nil (org-back-to-heading t) (error nil))
  19289. (setq level (outline-level))
  19290. (forward-char 1)
  19291. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19292. (progn (goto-char (match-beginning 0)) t)
  19293. (goto-char pos) nil))))
  19294. (defun org-show-hidden-entry ()
  19295. "Show an entry where even the heading is hidden."
  19296. (save-excursion
  19297. (org-show-entry)))
  19298. (defun org-flag-heading (flag &optional entry)
  19299. "Flag the current heading. FLAG non-nil means make invisible.
  19300. When ENTRY is non-nil, show the entire entry."
  19301. (save-excursion
  19302. (org-back-to-heading t)
  19303. ;; Check if we should show the entire entry
  19304. (if entry
  19305. (progn
  19306. (org-show-entry)
  19307. (save-excursion
  19308. (and (outline-next-heading)
  19309. (org-flag-heading nil))))
  19310. (outline-flag-region (max (point-min) (1- (point)))
  19311. (save-excursion (outline-end-of-heading) (point))
  19312. flag))))
  19313. (defun org-get-next-sibling ()
  19314. "Move to next heading of the same level, and return point.
  19315. If there is no such heading, return nil.
  19316. This is like outline-next-sibling, but invisible headings are ok."
  19317. (let ((level (funcall outline-level)))
  19318. (outline-next-heading)
  19319. (while (and (not (eobp)) (> (funcall outline-level) level))
  19320. (outline-next-heading))
  19321. (if (or (eobp) (< (funcall outline-level) level))
  19322. nil
  19323. (point))))
  19324. (defun org-get-last-sibling ()
  19325. "Move to previous heading of the same level, and return point.
  19326. If there is no such heading, return nil."
  19327. (let ((opoint (point))
  19328. (level (funcall outline-level)))
  19329. (outline-previous-heading)
  19330. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19331. (while (and (> (funcall outline-level) level)
  19332. (not (bobp)))
  19333. (outline-previous-heading))
  19334. (if (< (funcall outline-level) level)
  19335. nil
  19336. (point)))))
  19337. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19338. ;; This contains an exact copy of the original function, but it uses
  19339. ;; `org-back-to-heading', to make it work also in invisible
  19340. ;; trees. And is uses an invisible-OK argument.
  19341. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19342. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19343. ;; with many children and grandchildren etc, this can be much faster
  19344. ;; than the outline version.
  19345. (org-back-to-heading invisible-OK)
  19346. (let ((first t)
  19347. (level (funcall outline-level)))
  19348. (if (and (eq major-mode 'org-mode) (< level 1000))
  19349. ;; A true heading (not a plain list item), in Org-mode
  19350. ;; This means we can easily find the end by looking
  19351. ;; only for the right number of stars. Using a regexp to do
  19352. ;; this is so much faster than using a Lisp loop.
  19353. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19354. (forward-char 1)
  19355. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19356. ;; something else, do it the slow way
  19357. (while (and (not (eobp))
  19358. (or first (> (funcall outline-level) level)))
  19359. (setq first nil)
  19360. (outline-next-heading)))
  19361. (unless to-heading
  19362. (if (memq (preceding-char) '(?\n ?\^M))
  19363. (progn
  19364. ;; Go to end of line before heading
  19365. (forward-char -1)
  19366. (if (memq (preceding-char) '(?\n ?\^M))
  19367. ;; leave blank line before heading
  19368. (forward-char -1))))))
  19369. (point))
  19370. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19371. "Use Org version in org-mode, for dramatic speed-up."
  19372. (if (eq major-mode 'org-mode)
  19373. (progn
  19374. (org-end-of-subtree nil t)
  19375. (unless (eobp) (backward-char 1)))
  19376. ad-do-it))
  19377. (defun org-end-of-meta-data-and-drawers ()
  19378. "Jump to the first text after meta data and drawers in the current entry.
  19379. This will move over empty lines, lines with planning time stamps,
  19380. clocking lines, and drawers."
  19381. (org-back-to-heading t)
  19382. (let ((end (save-excursion (outline-next-heading) (point)))
  19383. (re (concat "\\(" org-drawer-regexp "\\)"
  19384. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19385. (forward-line 1)
  19386. (while (re-search-forward re end t)
  19387. (if (not (match-end 1))
  19388. ;; empty or planning line
  19389. (forward-line 1)
  19390. ;; a drawer, find the end
  19391. (re-search-forward "^[ \t]*:END:" end 'move)
  19392. (forward-line 1)))
  19393. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19394. (point)))
  19395. (defun org-forward-same-level (arg &optional invisible-ok)
  19396. "Move forward to the arg'th subheading at same level as this one.
  19397. Stop at the first and last subheadings of a superior heading.
  19398. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19399. it wil also look at invisible ones."
  19400. (interactive "p")
  19401. (org-back-to-heading invisible-ok)
  19402. (org-at-heading-p)
  19403. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19404. (re (format "^\\*\\{1,%d\\} " level))
  19405. l)
  19406. (forward-char 1)
  19407. (while (> arg 0)
  19408. (while (and (re-search-forward re nil 'move)
  19409. (setq l (- (match-end 0) (match-beginning 0) 1))
  19410. (= l level)
  19411. (not invisible-ok)
  19412. (progn (backward-char 1) (outline-invisible-p)))
  19413. (if (< l level) (setq arg 1)))
  19414. (setq arg (1- arg)))
  19415. (beginning-of-line 1)))
  19416. (defun org-backward-same-level (arg &optional invisible-ok)
  19417. "Move backward to the arg'th subheading at same level as this one.
  19418. Stop at the first and last subheadings of a superior heading."
  19419. (interactive "p")
  19420. (org-back-to-heading)
  19421. (org-at-heading-p)
  19422. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19423. (re (format "^\\*\\{1,%d\\} " level))
  19424. l)
  19425. (while (> arg 0)
  19426. (while (and (re-search-backward re nil 'move)
  19427. (setq l (- (match-end 0) (match-beginning 0) 1))
  19428. (= l level)
  19429. (not invisible-ok)
  19430. (outline-invisible-p))
  19431. (if (< l level) (setq arg 1)))
  19432. (setq arg (1- arg)))))
  19433. (defun org-show-subtree ()
  19434. "Show everything after this heading at deeper levels."
  19435. (outline-flag-region
  19436. (point)
  19437. (save-excursion
  19438. (org-end-of-subtree t t))
  19439. nil))
  19440. (defun org-show-entry ()
  19441. "Show the body directly following this heading.
  19442. Show the heading too, if it is currently invisible."
  19443. (interactive)
  19444. (save-excursion
  19445. (condition-case nil
  19446. (progn
  19447. (org-back-to-heading t)
  19448. (outline-flag-region
  19449. (max (point-min) (1- (point)))
  19450. (save-excursion
  19451. (if (re-search-forward
  19452. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19453. (match-beginning 1)
  19454. (point-max)))
  19455. nil)
  19456. (org-cycle-hide-drawers 'children))
  19457. (error nil))))
  19458. (defun org-make-options-regexp (kwds &optional extra)
  19459. "Make a regular expression for keyword lines."
  19460. (concat
  19461. "^"
  19462. "#?[ \t]*\\+\\("
  19463. (mapconcat 'regexp-quote kwds "\\|")
  19464. (if extra (concat "\\|" extra))
  19465. "\\):[ \t]*"
  19466. "\\(.*\\)"))
  19467. ;; Make isearch reveal the necessary context
  19468. (defun org-isearch-end ()
  19469. "Reveal context after isearch exits."
  19470. (when isearch-success ; only if search was successful
  19471. (if (featurep 'xemacs)
  19472. ;; Under XEmacs, the hook is run in the correct place,
  19473. ;; we directly show the context.
  19474. (org-show-context 'isearch)
  19475. ;; In Emacs the hook runs *before* restoring the overlays.
  19476. ;; So we have to use a one-time post-command-hook to do this.
  19477. ;; (Emacs 22 has a special variable, see function `org-mode')
  19478. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19479. isearch-mode-end-hook-quit)
  19480. ;; Only when the isearch was not quitted.
  19481. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19482. 'append 'local)))))
  19483. (defun org-isearch-post-command ()
  19484. "Remove self from hook, and show context."
  19485. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19486. (org-show-context 'isearch))
  19487. ;;;; Integration with and fixes for other packages
  19488. ;;; Imenu support
  19489. (defvar org-imenu-markers nil
  19490. "All markers currently used by Imenu.")
  19491. (make-variable-buffer-local 'org-imenu-markers)
  19492. (defun org-imenu-new-marker (&optional pos)
  19493. "Return a new marker for use by Imenu, and remember the marker."
  19494. (let ((m (make-marker)))
  19495. (move-marker m (or pos (point)))
  19496. (push m org-imenu-markers)
  19497. m))
  19498. (defun org-imenu-get-tree ()
  19499. "Produce the index for Imenu."
  19500. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19501. (setq org-imenu-markers nil)
  19502. (let* ((n org-imenu-depth)
  19503. (re (concat "^" (org-get-limited-outline-regexp)))
  19504. (subs (make-vector (1+ n) nil))
  19505. (last-level 0)
  19506. m level head)
  19507. (save-excursion
  19508. (save-restriction
  19509. (widen)
  19510. (goto-char (point-max))
  19511. (while (re-search-backward re nil t)
  19512. (setq level (org-reduced-level (funcall outline-level)))
  19513. (when (<= level n)
  19514. (looking-at org-complex-heading-regexp)
  19515. (setq head (org-link-display-format
  19516. (org-match-string-no-properties 4))
  19517. m (org-imenu-new-marker))
  19518. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19519. (if (>= level last-level)
  19520. (push (cons head m) (aref subs level))
  19521. (push (cons head (aref subs (1+ level))) (aref subs level))
  19522. (loop for i from (1+ level) to n do (aset subs i nil)))
  19523. (setq last-level level)))))
  19524. (aref subs 1)))
  19525. (eval-after-load "imenu"
  19526. '(progn
  19527. (add-hook 'imenu-after-jump-hook
  19528. (lambda ()
  19529. (if (eq major-mode 'org-mode)
  19530. (org-show-context 'org-goto))))))
  19531. (defun org-link-display-format (link)
  19532. "Replace a link with either the description, or the link target
  19533. if no description is present"
  19534. (save-match-data
  19535. (if (string-match org-bracket-link-analytic-regexp link)
  19536. (replace-match (if (match-end 5)
  19537. (match-string 5 link)
  19538. (concat (match-string 1 link)
  19539. (match-string 3 link)))
  19540. nil t link)
  19541. link)))
  19542. (defun org-toggle-link-display ()
  19543. "Toggle the literal or descriptive display of links."
  19544. (interactive)
  19545. (if org-descriptive-links
  19546. (progn (org-remove-from-invisibility-spec '(org-link))
  19547. (org-restart-font-lock)
  19548. (setq org-descriptive-links nil))
  19549. (progn (add-to-invisibility-spec '(org-link))
  19550. (org-restart-font-lock)
  19551. (setq org-descriptive-links t))))
  19552. ;; Speedbar support
  19553. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19554. "Overlay marking the agenda restriction line in speedbar.")
  19555. (overlay-put org-speedbar-restriction-lock-overlay
  19556. 'face 'org-agenda-restriction-lock)
  19557. (overlay-put org-speedbar-restriction-lock-overlay
  19558. 'help-echo "Agendas are currently limited to this item.")
  19559. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19560. (defun org-speedbar-set-agenda-restriction ()
  19561. "Restrict future agenda commands to the location at point in speedbar.
  19562. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19563. (interactive)
  19564. (require 'org-agenda)
  19565. (let (p m tp np dir txt)
  19566. (cond
  19567. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19568. 'org-imenu t))
  19569. (setq m (get-text-property p 'org-imenu-marker))
  19570. (with-current-buffer (marker-buffer m)
  19571. (goto-char m)
  19572. (org-agenda-set-restriction-lock 'subtree)))
  19573. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19574. 'speedbar-function 'speedbar-find-file))
  19575. (setq tp (previous-single-property-change
  19576. (1+ p) 'speedbar-function)
  19577. np (next-single-property-change
  19578. tp 'speedbar-function)
  19579. dir (speedbar-line-directory)
  19580. txt (buffer-substring-no-properties (or tp (point-min))
  19581. (or np (point-max))))
  19582. (with-current-buffer (find-file-noselect
  19583. (let ((default-directory dir))
  19584. (expand-file-name txt)))
  19585. (unless (eq major-mode 'org-mode)
  19586. (error "Cannot restrict to non-Org-mode file"))
  19587. (org-agenda-set-restriction-lock 'file)))
  19588. (t (error "Don't know how to restrict Org-mode's agenda")))
  19589. (move-overlay org-speedbar-restriction-lock-overlay
  19590. (point-at-bol) (point-at-eol))
  19591. (setq current-prefix-arg nil)
  19592. (org-agenda-maybe-redo)))
  19593. (eval-after-load "speedbar"
  19594. '(progn
  19595. (speedbar-add-supported-extension ".org")
  19596. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19597. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19598. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19599. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19600. (add-hook 'speedbar-visiting-tag-hook
  19601. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19602. ;;; Fixes and Hacks for problems with other packages
  19603. ;; Make flyspell not check words in links, to not mess up our keymap
  19604. (defun org-mode-flyspell-verify ()
  19605. "Don't let flyspell put overlays at active buttons, or on
  19606. {todo,all-time,additional-option-like}-keywords."
  19607. (let ((pos (max (1- (point)) (point-min)))
  19608. (word (thing-at-point 'word)))
  19609. (and (not (get-text-property pos 'keymap))
  19610. (not (get-text-property pos 'org-no-flyspell))
  19611. (not (member word org-todo-keywords-1))
  19612. (not (member word org-all-time-keywords))
  19613. (not (member word org-additional-option-like-keywords)))))
  19614. (defun org-remove-flyspell-overlays-in (beg end)
  19615. "Remove flyspell overlays in region."
  19616. (and (org-bound-and-true-p flyspell-mode)
  19617. (fboundp 'flyspell-delete-region-overlays)
  19618. (flyspell-delete-region-overlays beg end))
  19619. (add-text-properties beg end '(org-no-flyspell t)))
  19620. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19621. (eval-after-load "bookmark"
  19622. '(if (boundp 'bookmark-after-jump-hook)
  19623. ;; We can use the hook
  19624. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19625. ;; Hook not available, use advice
  19626. (defadvice bookmark-jump (after org-make-visible activate)
  19627. "Make the position visible."
  19628. (org-bookmark-jump-unhide))))
  19629. ;; Make sure saveplace shows the location if it was hidden
  19630. (eval-after-load "saveplace"
  19631. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19632. "Make the position visible."
  19633. (org-bookmark-jump-unhide)))
  19634. ;; Make sure ecb shows the location if it was hidden
  19635. (eval-after-load "ecb"
  19636. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19637. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19638. (if (eq major-mode 'org-mode)
  19639. (org-show-context))))
  19640. (defun org-bookmark-jump-unhide ()
  19641. "Unhide the current position, to show the bookmark location."
  19642. (and (eq major-mode 'org-mode)
  19643. (or (outline-invisible-p)
  19644. (save-excursion (goto-char (max (point-min) (1- (point))))
  19645. (outline-invisible-p)))
  19646. (org-show-context 'bookmark-jump)))
  19647. ;; Make session.el ignore our circular variable
  19648. (eval-after-load "session"
  19649. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19650. ;;;; Experimental code
  19651. (defun org-closed-in-range ()
  19652. "Sparse tree of items closed in a certain time range.
  19653. Still experimental, may disappear in the future."
  19654. (interactive)
  19655. ;; Get the time interval from the user.
  19656. (let* ((time1 (org-float-time
  19657. (org-read-date nil 'to-time nil "Starting date: ")))
  19658. (time2 (org-float-time
  19659. (org-read-date nil 'to-time nil "End date:")))
  19660. ;; callback function
  19661. (callback (lambda ()
  19662. (let ((time
  19663. (org-float-time
  19664. (apply 'encode-time
  19665. (org-parse-time-string
  19666. (match-string 1))))))
  19667. ;; check if time in interval
  19668. (and (>= time time1) (<= time time2))))))
  19669. ;; make tree, check each match with the callback
  19670. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19671. ;;;; Finish up
  19672. (provide 'org)
  19673. (run-hooks 'org-load-hook)
  19674. ;;; org.el ends here